Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / formats / mp4 / avc.cc
blob33fce1d8062cf237e030de4fa9b90e9b1dcc8015
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>
8 #include <vector>
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"
16 namespace media {
17 namespace mp4 {
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;
24 size_t pos = 0;
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];
31 if (nal_size == 0) {
32 DVLOG(1) << "nal_size is 0";
33 return false;
36 std::copy(kAnnexBStartCode, kAnnexBStartCode + kAnnexBStartCodeSize,
37 buf->begin() + pos);
38 pos += kLengthSize + nal_size;
40 return pos == buf->size();
43 // static
44 int AVC::FindSubsampleIndex(const std::vector<uint8>& buffer,
45 const std::vector<SubsampleEntry>* subsamples,
46 const uint8* ptr) {
47 DCHECK(ptr >= &buffer[0]);
48 DCHECK(ptr <= &buffer[buffer.size()-1]);
49 if (!subsamples || subsamples->empty())
50 return 0;
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;
55 if (p > ptr)
56 return i;
58 NOTREACHED();
59 return 0;
62 // static
63 bool AVC::ConvertFrameToAnnexB(int length_size, std::vector<uint8>* buffer) {
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_size = temp[pos];
76 if (length_size == 2) nal_size = (nal_size << 8) + temp[pos+1];
77 pos += length_size;
79 if (nal_size == 0) {
80 DVLOG(1) << "nal_size is 0";
81 return false;
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);
89 pos += nal_size;
91 return pos == temp.size();
94 // static
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);
104 H264NALU nalu;
105 if (parser->AdvanceToNextNALU(&nalu) != H264Parser::kOk)
106 return false;
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.
117 parser.reset();
118 start = NULL;
120 std::vector<uint8> param_sets;
121 RCHECK(AVC::ConvertConfigToAnnexB(avc_config, &param_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));
134 return true;
137 // static
138 bool AVC::ConvertConfigToAnnexB(
139 const AVCDecoderConfigurationRecord& avc_config,
140 std::vector<uint8>* buffer) {
141 DCHECK(buffer->empty());
142 buffer->clear();
143 int total_size = 0;
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());
163 return true;
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__;
175 DCHECK(buffer);
177 if (size == 0)
178 return true;
180 H264Parser parser;
181 parser.SetEncryptedStream(buffer, size, subsamples);
183 typedef enum {
184 kAUDAllowed,
185 kBeforeFirstVCL, // VCL == nal_unit_types 1-5
186 kAfterFirstVCL,
187 kEOStreamAllowed,
188 kNoMoreDataAllowed,
189 } NALUOrderState;
191 H264NALU nalu;
192 NALUOrderState order_state = kAUDAllowed;
193 int last_nalu_type = H264NALU::kUnspecified;
194 bool done = false;
195 while (!done) {
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) {
201 case H264NALU::kAUD:
202 if (order_state > kAUDAllowed) {
203 DVLOG(1) << "Unexpected AUD in order_state " << order_state;
204 return false;
206 order_state = kBeforeFirstVCL;
207 break;
209 case H264NALU::kSEIMessage:
210 case H264NALU::kReserved14:
211 case H264NALU::kReserved15:
212 case H264NALU::kReserved16:
213 case H264NALU::kReserved17:
214 case H264NALU::kReserved18:
215 case H264NALU::kPPS:
216 case H264NALU::kSPS:
217 if (order_state > kBeforeFirstVCL) {
218 DVLOG(1) << "Unexpected NALU type " << nalu.nal_unit_type
219 << " in order_state " << order_state;
220 return false;
222 order_state = kBeforeFirstVCL;
223 break;
225 case H264NALU::kSPSExt:
226 if (last_nalu_type != H264NALU::kSPS) {
227 DVLOG(1) << "SPS extension does not follow an SPS.";
228 return false;
230 break;
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;
239 return false;
241 order_state = kAfterFirstVCL;
242 break;
244 case H264NALU::kCodedSliceAux:
245 if (order_state != kAfterFirstVCL) {
246 DVLOG(1) << "Unexpected extension in order_state " << order_state;
247 return false;
249 break;
251 case H264NALU::kEOSeq:
252 if (order_state != kAfterFirstVCL) {
253 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state;
254 return false;
256 order_state = kEOStreamAllowed;
257 break;
259 case H264NALU::kEOStream:
260 if (order_state < kAfterFirstVCL) {
261 DVLOG(1) << "Unexpected EOStream in order_state " << order_state;
262 return false;
264 order_state = kNoMoreDataAllowed;
265 break;
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;
273 return false;
275 break;
277 default:
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;
283 return false;
286 last_nalu_type = nalu.nal_unit_type;
287 break;
289 case H264Parser::kInvalidStream:
290 return false;
292 case H264Parser::kUnsupportedStream:
293 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
294 return false;
296 case H264Parser::kEOStream:
297 done = true;
301 return order_state >= kAfterFirstVCL;
303 } // namespace mp4
304 } // namespace media