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_size
= (*buf
)[pos
];
26 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+1];
27 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+2];
28 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+3];
31 DVLOG(1) << "nal_size is 0";
35 std::copy(kAnnexBStartCode
, kAnnexBStartCode
+ kAnnexBStartCodeSize
,
37 pos
+= kLengthSize
+ nal_size
;
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 RCHECK(length_size
== 1 || length_size
== 2 || length_size
== 4);
66 return ConvertAVCToAnnexBInPlaceForLengthSize4(buffer
);
68 std::vector
<uint8
> temp
;
70 buffer
->reserve(temp
.size() + 32);
73 while (pos
+ length_size
< temp
.size()) {
74 int nal_size
= temp
[pos
];
75 if (length_size
== 2) nal_size
= (nal_size
<< 8) + temp
[pos
+1];
79 DVLOG(1) << "nal_size is 0";
83 RCHECK(pos
+ nal_size
<= temp
.size());
84 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
85 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
86 buffer
->insert(buffer
->end(), temp
.begin() + pos
,
87 temp
.begin() + pos
+ nal_size
);
90 return pos
== temp
.size();
94 bool AVC::InsertParamSetsAnnexB(const AVCDecoderConfigurationRecord
& avc_config
,
95 std::vector
<uint8
>* buffer
,
96 std::vector
<SubsampleEntry
>* subsamples
) {
97 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
99 scoped_ptr
<H264Parser
> parser(new H264Parser());
100 const uint8
* start
= &(*buffer
)[0];
101 parser
->SetEncryptedStream(start
, buffer
->size(), *subsamples
);
104 if (parser
->AdvanceToNextNALU(&nalu
) != H264Parser::kOk
)
107 std::vector
<uint8
>::iterator config_insert_point
= buffer
->begin();
109 if (nalu
.nal_unit_type
== H264NALU::kAUD
) {
110 // Move insert point to just after the AUD.
111 config_insert_point
+= (nalu
.data
+ nalu
.size
) - start
;
114 // Clear |parser| and |start| since they aren't needed anymore and
115 // will hold stale pointers once the insert happens.
119 std::vector
<uint8
> param_sets
;
120 RCHECK(AVC::ConvertConfigToAnnexB(avc_config
, ¶m_sets
));
122 if (subsamples
&& !subsamples
->empty()) {
123 int subsample_index
= FindSubsampleIndex(*buffer
, subsamples
,
124 &(*config_insert_point
));
125 // Update the size of the subsample where SPS/PPS is to be inserted.
126 (*subsamples
)[subsample_index
].clear_bytes
+= param_sets
.size();
129 buffer
->insert(config_insert_point
,
130 param_sets
.begin(), param_sets
.end());
132 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
137 bool AVC::ConvertConfigToAnnexB(
138 const AVCDecoderConfigurationRecord
& avc_config
,
139 std::vector
<uint8
>* buffer
) {
140 DCHECK(buffer
->empty());
143 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++)
144 total_size
+= avc_config
.sps_list
[i
].size() + kAnnexBStartCodeSize
;
145 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++)
146 total_size
+= avc_config
.pps_list
[i
].size() + kAnnexBStartCodeSize
;
147 buffer
->reserve(total_size
);
149 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++) {
150 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
151 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
152 buffer
->insert(buffer
->end(), avc_config
.sps_list
[i
].begin(),
153 avc_config
.sps_list
[i
].end());
156 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++) {
157 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
158 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
159 buffer
->insert(buffer
->end(), avc_config
.pps_list
[i
].begin(),
160 avc_config
.pps_list
[i
].end());
165 // Verifies AnnexB NALU order according to ISO/IEC 14496-10 Section 7.4.1.2.3
166 bool AVC::IsValidAnnexB(const std::vector
<uint8
>& buffer
,
167 const std::vector
<SubsampleEntry
>& subsamples
) {
168 return IsValidAnnexB(&buffer
[0], buffer
.size(), subsamples
);
171 bool AVC::IsValidAnnexB(const uint8
* buffer
, size_t size
,
172 const std::vector
<SubsampleEntry
>& subsamples
) {
173 DVLOG(1) << __FUNCTION__
;
180 parser
.SetEncryptedStream(buffer
, size
, subsamples
);
184 kBeforeFirstVCL
, // VCL == nal_unit_types 1-5
191 NALUOrderState order_state
= kAUDAllowed
;
192 int last_nalu_type
= H264NALU::kUnspecified
;
195 switch (parser
.AdvanceToNextNALU(&nalu
)) {
196 case H264Parser::kOk
:
197 DVLOG(1) << "nal_unit_type " << nalu
.nal_unit_type
;
199 switch (nalu
.nal_unit_type
) {
201 if (order_state
> kAUDAllowed
) {
202 DVLOG(1) << "Unexpected AUD in order_state " << order_state
;
205 order_state
= kBeforeFirstVCL
;
208 case H264NALU::kSEIMessage
:
209 case H264NALU::kReserved14
:
210 case H264NALU::kReserved15
:
211 case H264NALU::kReserved16
:
212 case H264NALU::kReserved17
:
213 case H264NALU::kReserved18
:
216 if (order_state
> kBeforeFirstVCL
) {
217 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
218 << " in order_state " << order_state
;
221 order_state
= kBeforeFirstVCL
;
224 case H264NALU::kSPSExt
:
225 if (last_nalu_type
!= H264NALU::kSPS
) {
226 DVLOG(1) << "SPS extension does not follow an SPS.";
231 case H264NALU::kNonIDRSlice
:
232 case H264NALU::kSliceDataA
:
233 case H264NALU::kSliceDataB
:
234 case H264NALU::kSliceDataC
:
235 case H264NALU::kIDRSlice
:
236 if (order_state
> kAfterFirstVCL
) {
237 DVLOG(1) << "Unexpected VCL in order_state " << order_state
;
240 order_state
= kAfterFirstVCL
;
243 case H264NALU::kCodedSliceAux
:
244 if (order_state
!= kAfterFirstVCL
) {
245 DVLOG(1) << "Unexpected extension in order_state " << order_state
;
250 case H264NALU::kEOSeq
:
251 if (order_state
!= kAfterFirstVCL
) {
252 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state
;
255 order_state
= kEOStreamAllowed
;
258 case H264NALU::kEOStream
:
259 if (order_state
< kAfterFirstVCL
) {
260 DVLOG(1) << "Unexpected EOStream in order_state " << order_state
;
263 order_state
= kNoMoreDataAllowed
;
266 case H264NALU::kFiller
:
267 case H264NALU::kUnspecified
:
268 if (!(order_state
>= kAfterFirstVCL
&&
269 order_state
< kEOStreamAllowed
)) {
270 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
271 << " in order_state " << order_state
;
277 DCHECK_GE(nalu
.nal_unit_type
, 20);
278 if (nalu
.nal_unit_type
>= 20 && nalu
.nal_unit_type
<= 31 &&
279 order_state
!= kAfterFirstVCL
) {
280 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
281 << " in order_state " << order_state
;
285 last_nalu_type
= nalu
.nal_unit_type
;
288 case H264Parser::kInvalidStream
:
291 case H264Parser::kUnsupportedStream
:
292 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
295 case H264Parser::kEOStream
:
300 return order_state
>= kAfterFirstVCL
;