1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/formats/mp4/avc.h"
9 #include "base/logging.h"
10 #include "media/base/decrypt_config.h"
11 #include "media/filters/h264_parser.h"
12 #include "media/formats/mp4/box_definitions.h"
13 #include "media/formats/mp4/box_reader.h"
18 static const uint8 kAnnexBStartCode
[] = {0, 0, 0, 1};
19 static const int kAnnexBStartCodeSize
= 4;
21 static bool ConvertAVCToAnnexBInPlaceForLengthSize4(std::vector
<uint8
>* buf
) {
22 const int kLengthSize
= 4;
24 while (pos
+ kLengthSize
< buf
->size()) {
25 uint32 nal_length
= (*buf
)[pos
];
26 nal_length
= (nal_length
<< 8) + (*buf
)[pos
+1];
27 nal_length
= (nal_length
<< 8) + (*buf
)[pos
+2];
28 nal_length
= (nal_length
<< 8) + (*buf
)[pos
+3];
30 if (nal_length
== 0) {
31 DVLOG(1) << "nal_length is 0";
35 std::copy(kAnnexBStartCode
, kAnnexBStartCode
+ kAnnexBStartCodeSize
,
37 pos
+= kLengthSize
+ nal_length
;
39 return pos
== buf
->size();
43 int AVC::FindSubsampleIndex(const std::vector
<uint8
>& buffer
,
44 const std::vector
<SubsampleEntry
>* subsamples
,
46 DCHECK(ptr
>= &buffer
[0]);
47 DCHECK(ptr
<= &buffer
[buffer
.size()-1]);
48 if (!subsamples
|| subsamples
->empty())
51 const uint8
* p
= &buffer
[0];
52 for (size_t i
= 0; i
< subsamples
->size(); ++i
) {
53 p
+= (*subsamples
)[i
].clear_bytes
+ (*subsamples
)[i
].cypher_bytes
;
62 bool AVC::ConvertFrameToAnnexB(int length_size
, std::vector
<uint8
>* buffer
,
63 std::vector
<SubsampleEntry
>* subsamples
) {
64 RCHECK(length_size
== 1 || length_size
== 2 || length_size
== 4);
67 return ConvertAVCToAnnexBInPlaceForLengthSize4(buffer
);
69 std::vector
<uint8
> temp
;
71 buffer
->reserve(temp
.size() + 32);
74 while (pos
+ length_size
< temp
.size()) {
75 int nal_length
= temp
[pos
];
76 if (length_size
== 2) nal_length
= (nal_length
<< 8) + temp
[pos
+1];
79 if (nal_length
== 0) {
80 DVLOG(1) << "nal_length is 0";
84 RCHECK(pos
+ nal_length
<= temp
.size());
85 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
86 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
87 if (subsamples
&& !subsamples
->empty()) {
88 uint8
* buffer_pos
= &(*(buffer
->end() - kAnnexBStartCodeSize
));
89 int subsample_index
= FindSubsampleIndex(*buffer
, subsamples
, buffer_pos
);
90 // We've replaced NALU size value with an AnnexB start code.
91 int size_adjustment
= kAnnexBStartCodeSize
- length_size
;
92 (*subsamples
)[subsample_index
].clear_bytes
+= size_adjustment
;
94 buffer
->insert(buffer
->end(), temp
.begin() + pos
,
95 temp
.begin() + pos
+ nal_length
);
98 return pos
== temp
.size();
102 bool AVC::InsertParamSetsAnnexB(const AVCDecoderConfigurationRecord
& avc_config
,
103 std::vector
<uint8
>* buffer
,
104 std::vector
<SubsampleEntry
>* subsamples
) {
105 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
107 scoped_ptr
<H264Parser
> parser(new H264Parser());
108 const uint8
* start
= &(*buffer
)[0];
109 parser
->SetEncryptedStream(start
, buffer
->size(), *subsamples
);
112 if (parser
->AdvanceToNextNALU(&nalu
) != H264Parser::kOk
)
115 std::vector
<uint8
>::iterator config_insert_point
= buffer
->begin();
117 if (nalu
.nal_unit_type
== H264NALU::kAUD
) {
118 // Move insert point to just after the AUD.
119 config_insert_point
+= (nalu
.data
+ nalu
.size
) - start
;
122 // Clear |parser| and |start| since they aren't needed anymore and
123 // will hold stale pointers once the insert happens.
127 std::vector
<uint8
> param_sets
;
128 RCHECK(AVC::ConvertConfigToAnnexB(avc_config
, ¶m_sets
));
130 if (subsamples
&& !subsamples
->empty()) {
131 int subsample_index
= FindSubsampleIndex(*buffer
, subsamples
,
132 &(*config_insert_point
));
133 // Update the size of the subsample where SPS/PPS is to be inserted.
134 (*subsamples
)[subsample_index
].clear_bytes
+= param_sets
.size();
137 buffer
->insert(config_insert_point
,
138 param_sets
.begin(), param_sets
.end());
140 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
145 bool AVC::ConvertConfigToAnnexB(
146 const AVCDecoderConfigurationRecord
& avc_config
,
147 std::vector
<uint8
>* buffer
) {
148 DCHECK(buffer
->empty());
151 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++)
152 total_size
+= avc_config
.sps_list
[i
].size() + kAnnexBStartCodeSize
;
153 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++)
154 total_size
+= avc_config
.pps_list
[i
].size() + kAnnexBStartCodeSize
;
155 buffer
->reserve(total_size
);
157 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++) {
158 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
159 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
160 buffer
->insert(buffer
->end(), avc_config
.sps_list
[i
].begin(),
161 avc_config
.sps_list
[i
].end());
164 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++) {
165 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
166 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
167 buffer
->insert(buffer
->end(), avc_config
.pps_list
[i
].begin(),
168 avc_config
.pps_list
[i
].end());
173 // Verifies AnnexB NALU order according to ISO/IEC 14496-10 Section 7.4.1.2.3
174 bool AVC::IsValidAnnexB(const std::vector
<uint8
>& buffer
,
175 const std::vector
<SubsampleEntry
>& subsamples
) {
176 return IsValidAnnexB(&buffer
[0], buffer
.size(), subsamples
);
179 bool AVC::IsValidAnnexB(const uint8
* buffer
, size_t size
,
180 const std::vector
<SubsampleEntry
>& subsamples
) {
181 DVLOG(1) << __FUNCTION__
;
188 parser
.SetEncryptedStream(buffer
, size
, subsamples
);
192 kBeforeFirstVCL
, // VCL == nal_unit_types 1-5
199 NALUOrderState order_state
= kAUDAllowed
;
200 int last_nalu_type
= H264NALU::kUnspecified
;
203 switch (parser
.AdvanceToNextNALU(&nalu
)) {
204 case H264Parser::kOk
:
205 DVLOG(1) << "nal_unit_type " << nalu
.nal_unit_type
;
207 switch (nalu
.nal_unit_type
) {
209 if (order_state
> kAUDAllowed
) {
210 DVLOG(1) << "Unexpected AUD in order_state " << order_state
;
213 order_state
= kBeforeFirstVCL
;
216 case H264NALU::kSEIMessage
:
217 case H264NALU::kReserved14
:
218 case H264NALU::kReserved15
:
219 case H264NALU::kReserved16
:
220 case H264NALU::kReserved17
:
221 case H264NALU::kReserved18
:
224 if (order_state
> kBeforeFirstVCL
) {
225 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
226 << " in order_state " << order_state
;
229 order_state
= kBeforeFirstVCL
;
232 case H264NALU::kSPSExt
:
233 if (last_nalu_type
!= H264NALU::kSPS
) {
234 DVLOG(1) << "SPS extension does not follow an SPS.";
239 case H264NALU::kNonIDRSlice
:
240 case H264NALU::kSliceDataA
:
241 case H264NALU::kSliceDataB
:
242 case H264NALU::kSliceDataC
:
243 case H264NALU::kIDRSlice
:
244 if (order_state
> kAfterFirstVCL
) {
245 DVLOG(1) << "Unexpected VCL in order_state " << order_state
;
248 order_state
= kAfterFirstVCL
;
251 case H264NALU::kCodedSliceAux
:
252 if (order_state
!= kAfterFirstVCL
) {
253 DVLOG(1) << "Unexpected extension in order_state " << order_state
;
258 case H264NALU::kEOSeq
:
259 if (order_state
!= kAfterFirstVCL
) {
260 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state
;
263 order_state
= kEOStreamAllowed
;
266 case H264NALU::kEOStream
:
267 if (order_state
< kAfterFirstVCL
) {
268 DVLOG(1) << "Unexpected EOStream in order_state " << order_state
;
271 order_state
= kNoMoreDataAllowed
;
274 case H264NALU::kFiller
:
275 case H264NALU::kUnspecified
:
276 if (!(order_state
>= kAfterFirstVCL
&&
277 order_state
< kEOStreamAllowed
)) {
278 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
279 << " in order_state " << order_state
;
285 DCHECK_GE(nalu
.nal_unit_type
, 20);
286 if (nalu
.nal_unit_type
>= 20 && nalu
.nal_unit_type
<= 31 &&
287 order_state
!= kAfterFirstVCL
) {
288 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
289 << " in order_state " << order_state
;
293 last_nalu_type
= nalu
.nal_unit_type
;
296 case H264Parser::kInvalidStream
:
299 case H264Parser::kUnsupportedStream
:
300 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
303 case H264Parser::kEOStream
:
308 return order_state
>= kAfterFirstVCL
;
311 AVCBitstreamConverter::AVCBitstreamConverter(
312 scoped_ptr
<AVCDecoderConfigurationRecord
> avc_config
)
313 : avc_config_(avc_config
.Pass()) {
317 AVCBitstreamConverter::~AVCBitstreamConverter() {
320 bool AVCBitstreamConverter::ConvertFrame(
321 std::vector
<uint8
>* frame_buf
,
323 std::vector
<SubsampleEntry
>* subsamples
) const {
324 // Convert the AVC NALU length fields to Annex B headers, as expected by
325 // decoding libraries. Since this may enlarge the size of the buffer, we also
326 // update the clear byte count for each subsample if encryption is used to
327 // account for the difference in size between the length prefix and Annex B
329 RCHECK(AVC::ConvertFrameToAnnexB(avc_config_
->length_size
, frame_buf
,
333 // If this is a keyframe, we (re-)inject SPS and PPS headers at the start of
334 // a frame. If subsample info is present, we also update the clear byte
335 // count for that first subsample.
336 RCHECK(AVC::InsertParamSetsAnnexB(*avc_config_
, frame_buf
, subsamples
));
339 DCHECK(AVC::IsValidAnnexB(*frame_buf
, *subsamples
));