Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / formats / mp4 / avc.cc
blob7451a5fe4a2c7e71fd55fbb626ea959185895afe
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"
7 #include <algorithm>
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"
15 namespace media {
16 namespace mp4 {
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;
23 size_t pos = 0;
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";
32 return false;
35 std::copy(kAnnexBStartCode, kAnnexBStartCode + kAnnexBStartCodeSize,
36 buf->begin() + pos);
37 pos += kLengthSize + nal_length;
39 return pos == buf->size();
42 // static
43 int AVC::FindSubsampleIndex(const std::vector<uint8>& buffer,
44 const std::vector<SubsampleEntry>* subsamples,
45 const uint8* ptr) {
46 DCHECK(ptr >= &buffer[0]);
47 DCHECK(ptr <= &buffer[buffer.size()-1]);
48 if (!subsamples || subsamples->empty())
49 return 0;
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;
54 if (p > ptr)
55 return i;
57 NOTREACHED();
58 return 0;
61 // static
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);
66 if (length_size == 4)
67 return ConvertAVCToAnnexBInPlaceForLengthSize4(buffer);
69 std::vector<uint8> temp;
70 temp.swap(*buffer);
71 buffer->reserve(temp.size() + 32);
73 size_t pos = 0;
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];
77 pos += length_size;
79 if (nal_length == 0) {
80 DVLOG(1) << "nal_length is 0";
81 return false;
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);
96 pos += nal_length;
98 return pos == temp.size();
101 // static
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);
111 H264NALU nalu;
112 if (parser->AdvanceToNextNALU(&nalu) != H264Parser::kOk)
113 return false;
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.
124 parser.reset();
125 start = NULL;
127 std::vector<uint8> param_sets;
128 RCHECK(AVC::ConvertConfigToAnnexB(avc_config, &param_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));
141 return true;
144 // static
145 bool AVC::ConvertConfigToAnnexB(
146 const AVCDecoderConfigurationRecord& avc_config,
147 std::vector<uint8>* buffer) {
148 DCHECK(buffer->empty());
149 buffer->clear();
150 int total_size = 0;
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());
170 return true;
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__;
182 DCHECK(buffer);
184 if (size == 0)
185 return true;
187 H264Parser parser;
188 parser.SetEncryptedStream(buffer, size, subsamples);
190 typedef enum {
191 kAUDAllowed,
192 kBeforeFirstVCL, // VCL == nal_unit_types 1-5
193 kAfterFirstVCL,
194 kEOStreamAllowed,
195 kNoMoreDataAllowed,
196 } NALUOrderState;
198 H264NALU nalu;
199 NALUOrderState order_state = kAUDAllowed;
200 int last_nalu_type = H264NALU::kUnspecified;
201 bool done = false;
202 while (!done) {
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) {
208 case H264NALU::kAUD:
209 if (order_state > kAUDAllowed) {
210 DVLOG(1) << "Unexpected AUD in order_state " << order_state;
211 return false;
213 order_state = kBeforeFirstVCL;
214 break;
216 case H264NALU::kSEIMessage:
217 case H264NALU::kReserved14:
218 case H264NALU::kReserved15:
219 case H264NALU::kReserved16:
220 case H264NALU::kReserved17:
221 case H264NALU::kReserved18:
222 case H264NALU::kPPS:
223 case H264NALU::kSPS:
224 if (order_state > kBeforeFirstVCL) {
225 DVLOG(1) << "Unexpected NALU type " << nalu.nal_unit_type
226 << " in order_state " << order_state;
227 return false;
229 order_state = kBeforeFirstVCL;
230 break;
232 case H264NALU::kSPSExt:
233 if (last_nalu_type != H264NALU::kSPS) {
234 DVLOG(1) << "SPS extension does not follow an SPS.";
235 return false;
237 break;
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;
246 return false;
248 order_state = kAfterFirstVCL;
249 break;
251 case H264NALU::kCodedSliceAux:
252 if (order_state != kAfterFirstVCL) {
253 DVLOG(1) << "Unexpected extension in order_state " << order_state;
254 return false;
256 break;
258 case H264NALU::kEOSeq:
259 if (order_state != kAfterFirstVCL) {
260 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state;
261 return false;
263 order_state = kEOStreamAllowed;
264 break;
266 case H264NALU::kEOStream:
267 if (order_state < kAfterFirstVCL) {
268 DVLOG(1) << "Unexpected EOStream in order_state " << order_state;
269 return false;
271 order_state = kNoMoreDataAllowed;
272 break;
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;
280 return false;
282 break;
284 default:
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;
290 return false;
293 last_nalu_type = nalu.nal_unit_type;
294 break;
296 case H264Parser::kInvalidStream:
297 return false;
299 case H264Parser::kUnsupportedStream:
300 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
301 return false;
303 case H264Parser::kEOStream:
304 done = true;
308 return order_state >= kAfterFirstVCL;
311 AVCBitstreamConverter::AVCBitstreamConverter(
312 scoped_ptr<AVCDecoderConfigurationRecord> avc_config)
313 : avc_config_(avc_config.Pass()) {
314 DCHECK(avc_config_);
317 AVCBitstreamConverter::~AVCBitstreamConverter() {
320 bool AVCBitstreamConverter::ConvertFrame(
321 std::vector<uint8>* frame_buf,
322 bool is_keyframe,
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
328 // start code.
329 RCHECK(AVC::ConvertFrameToAnnexB(avc_config_->length_size, frame_buf,
330 subsamples));
332 if (is_keyframe) {
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));
340 return true;
343 } // namespace mp4
344 } // namespace media