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"
10 #include "base/logging.h"
11 #include "media/base/decrypt_config.h"
12 #include "media/filters/h264_parser.h"
13 #include "media/formats/mp4/box_definitions.h"
14 #include "media/formats/mp4/box_reader.h"
19 static const uint8 kAnnexBStartCode
[] = {0, 0, 0, 1};
20 static const int kAnnexBStartCodeSize
= 4;
22 static bool ConvertAVCToAnnexBInPlaceForLengthSize4(std::vector
<uint8
>* buf
) {
23 const int kLengthSize
= 4;
25 while (pos
+ kLengthSize
< buf
->size()) {
26 uint32 nal_size
= (*buf
)[pos
];
27 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+1];
28 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+2];
29 nal_size
= (nal_size
<< 8) + (*buf
)[pos
+3];
32 DVLOG(1) << "nal_size is 0";
36 std::copy(kAnnexBStartCode
, kAnnexBStartCode
+ kAnnexBStartCodeSize
,
38 pos
+= kLengthSize
+ nal_size
;
40 return pos
== buf
->size();
44 int AVC::FindSubsampleIndex(const std::vector
<uint8
>& buffer
,
45 const std::vector
<SubsampleEntry
>* subsamples
,
47 DCHECK(ptr
>= &buffer
[0]);
48 DCHECK(ptr
<= &buffer
[buffer
.size()-1]);
49 if (!subsamples
|| subsamples
->empty())
52 const uint8
* p
= &buffer
[0];
53 for (size_t i
= 0; i
< subsamples
->size(); ++i
) {
54 p
+= (*subsamples
)[i
].clear_bytes
+ (*subsamples
)[i
].cypher_bytes
;
63 bool AVC::ConvertFrameToAnnexB(int length_size
, std::vector
<uint8
>* buffer
) {
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_size
= temp
[pos
];
76 if (length_size
== 2) nal_size
= (nal_size
<< 8) + temp
[pos
+1];
80 DVLOG(1) << "nal_size is 0";
84 RCHECK(pos
+ nal_size
<= temp
.size());
85 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
86 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
87 buffer
->insert(buffer
->end(), temp
.begin() + pos
,
88 temp
.begin() + pos
+ nal_size
);
91 return pos
== temp
.size();
95 bool AVC::InsertParamSetsAnnexB(const AVCDecoderConfigurationRecord
& avc_config
,
96 std::vector
<uint8
>* buffer
,
97 std::vector
<SubsampleEntry
>* subsamples
) {
98 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
100 scoped_ptr
<H264Parser
> parser(new H264Parser());
101 const uint8
* start
= &(*buffer
)[0];
102 parser
->SetEncryptedStream(start
, buffer
->size(), *subsamples
);
105 if (parser
->AdvanceToNextNALU(&nalu
) != H264Parser::kOk
)
108 std::vector
<uint8
>::iterator config_insert_point
= buffer
->begin();
110 if (nalu
.nal_unit_type
== H264NALU::kAUD
) {
111 // Move insert point to just after the AUD.
112 config_insert_point
+= (nalu
.data
+ nalu
.size
) - start
;
115 // Clear |parser| and |start| since they aren't needed anymore and
116 // will hold stale pointers once the insert happens.
120 std::vector
<uint8
> param_sets
;
121 RCHECK(AVC::ConvertConfigToAnnexB(avc_config
, ¶m_sets
));
123 if (subsamples
&& !subsamples
->empty()) {
124 int subsample_index
= FindSubsampleIndex(*buffer
, subsamples
,
125 &(*config_insert_point
));
126 // Update the size of the subsample where SPS/PPS is to be inserted.
127 (*subsamples
)[subsample_index
].clear_bytes
+= param_sets
.size();
130 buffer
->insert(config_insert_point
,
131 param_sets
.begin(), param_sets
.end());
133 DCHECK(AVC::IsValidAnnexB(*buffer
, *subsamples
));
138 bool AVC::ConvertConfigToAnnexB(
139 const AVCDecoderConfigurationRecord
& avc_config
,
140 std::vector
<uint8
>* buffer
) {
141 DCHECK(buffer
->empty());
144 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++)
145 total_size
+= avc_config
.sps_list
[i
].size() + kAnnexBStartCodeSize
;
146 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++)
147 total_size
+= avc_config
.pps_list
[i
].size() + kAnnexBStartCodeSize
;
148 buffer
->reserve(total_size
);
150 for (size_t i
= 0; i
< avc_config
.sps_list
.size(); i
++) {
151 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
152 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
153 buffer
->insert(buffer
->end(), avc_config
.sps_list
[i
].begin(),
154 avc_config
.sps_list
[i
].end());
157 for (size_t i
= 0; i
< avc_config
.pps_list
.size(); i
++) {
158 buffer
->insert(buffer
->end(), kAnnexBStartCode
,
159 kAnnexBStartCode
+ kAnnexBStartCodeSize
);
160 buffer
->insert(buffer
->end(), avc_config
.pps_list
[i
].begin(),
161 avc_config
.pps_list
[i
].end());
166 // Verifies AnnexB NALU order according to ISO/IEC 14496-10 Section 7.4.1.2.3
167 bool AVC::IsValidAnnexB(const std::vector
<uint8
>& buffer
,
168 const std::vector
<SubsampleEntry
>& subsamples
) {
169 return IsValidAnnexB(&buffer
[0], buffer
.size(), subsamples
);
172 bool AVC::IsValidAnnexB(const uint8
* buffer
, size_t size
,
173 const std::vector
<SubsampleEntry
>& subsamples
) {
174 DVLOG(1) << __FUNCTION__
;
181 parser
.SetEncryptedStream(buffer
, size
, subsamples
);
185 kBeforeFirstVCL
, // VCL == nal_unit_types 1-5
192 NALUOrderState order_state
= kAUDAllowed
;
193 int last_nalu_type
= H264NALU::kUnspecified
;
196 switch (parser
.AdvanceToNextNALU(&nalu
)) {
197 case H264Parser::kOk
:
198 DVLOG(1) << "nal_unit_type " << nalu
.nal_unit_type
;
200 switch (nalu
.nal_unit_type
) {
202 if (order_state
> kAUDAllowed
) {
203 DVLOG(1) << "Unexpected AUD in order_state " << order_state
;
206 order_state
= kBeforeFirstVCL
;
209 case H264NALU::kSEIMessage
:
210 case H264NALU::kReserved14
:
211 case H264NALU::kReserved15
:
212 case H264NALU::kReserved16
:
213 case H264NALU::kReserved17
:
214 case H264NALU::kReserved18
:
217 if (order_state
> kBeforeFirstVCL
) {
218 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
219 << " in order_state " << order_state
;
222 order_state
= kBeforeFirstVCL
;
225 case H264NALU::kSPSExt
:
226 if (last_nalu_type
!= H264NALU::kSPS
) {
227 DVLOG(1) << "SPS extension does not follow an SPS.";
232 case H264NALU::kNonIDRSlice
:
233 case H264NALU::kSliceDataA
:
234 case H264NALU::kSliceDataB
:
235 case H264NALU::kSliceDataC
:
236 case H264NALU::kIDRSlice
:
237 if (order_state
> kAfterFirstVCL
) {
238 DVLOG(1) << "Unexpected VCL in order_state " << order_state
;
241 order_state
= kAfterFirstVCL
;
244 case H264NALU::kCodedSliceAux
:
245 if (order_state
!= kAfterFirstVCL
) {
246 DVLOG(1) << "Unexpected extension in order_state " << order_state
;
251 case H264NALU::kEOSeq
:
252 if (order_state
!= kAfterFirstVCL
) {
253 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state
;
256 order_state
= kEOStreamAllowed
;
259 case H264NALU::kEOStream
:
260 if (order_state
< kAfterFirstVCL
) {
261 DVLOG(1) << "Unexpected EOStream in order_state " << order_state
;
264 order_state
= kNoMoreDataAllowed
;
267 case H264NALU::kFiller
:
268 case H264NALU::kUnspecified
:
269 if (!(order_state
>= kAfterFirstVCL
&&
270 order_state
< kEOStreamAllowed
)) {
271 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
272 << " in order_state " << order_state
;
278 DCHECK_GE(nalu
.nal_unit_type
, 20);
279 if (nalu
.nal_unit_type
>= 20 && nalu
.nal_unit_type
<= 31 &&
280 order_state
!= kAfterFirstVCL
) {
281 DVLOG(1) << "Unexpected NALU type " << nalu
.nal_unit_type
282 << " in order_state " << order_state
;
286 last_nalu_type
= nalu
.nal_unit_type
;
289 case H264Parser::kInvalidStream
:
292 case H264Parser::kUnsupportedStream
:
293 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
296 case H264Parser::kEOStream
:
301 return order_state
>= kAfterFirstVCL
;