Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / formats / mp4 / avc.cc
blob0368d1ba5de1dc747312e5a43971596354a5f389
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_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];
30 if (nal_size == 0) {
31 DVLOG(1) << "nal_size is 0";
32 return false;
35 std::copy(kAnnexBStartCode, kAnnexBStartCode + kAnnexBStartCodeSize,
36 buf->begin() + pos);
37 pos += kLengthSize + nal_size;
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 RCHECK(length_size == 1 || length_size == 2 || length_size == 4);
65 if (length_size == 4)
66 return ConvertAVCToAnnexBInPlaceForLengthSize4(buffer);
68 std::vector<uint8> temp;
69 temp.swap(*buffer);
70 buffer->reserve(temp.size() + 32);
72 size_t pos = 0;
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];
76 pos += length_size;
78 if (nal_size == 0) {
79 DVLOG(1) << "nal_size is 0";
80 return false;
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);
88 pos += nal_size;
90 return pos == temp.size();
93 // static
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);
103 H264NALU nalu;
104 if (parser->AdvanceToNextNALU(&nalu) != H264Parser::kOk)
105 return false;
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.
116 parser.reset();
117 start = NULL;
119 std::vector<uint8> param_sets;
120 RCHECK(AVC::ConvertConfigToAnnexB(avc_config, &param_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));
133 return true;
136 // static
137 bool AVC::ConvertConfigToAnnexB(
138 const AVCDecoderConfigurationRecord& avc_config,
139 std::vector<uint8>* buffer) {
140 DCHECK(buffer->empty());
141 buffer->clear();
142 int total_size = 0;
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());
162 return true;
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__;
174 DCHECK(buffer);
176 if (size == 0)
177 return true;
179 H264Parser parser;
180 parser.SetEncryptedStream(buffer, size, subsamples);
182 typedef enum {
183 kAUDAllowed,
184 kBeforeFirstVCL, // VCL == nal_unit_types 1-5
185 kAfterFirstVCL,
186 kEOStreamAllowed,
187 kNoMoreDataAllowed,
188 } NALUOrderState;
190 H264NALU nalu;
191 NALUOrderState order_state = kAUDAllowed;
192 int last_nalu_type = H264NALU::kUnspecified;
193 bool done = false;
194 while (!done) {
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) {
200 case H264NALU::kAUD:
201 if (order_state > kAUDAllowed) {
202 DVLOG(1) << "Unexpected AUD in order_state " << order_state;
203 return false;
205 order_state = kBeforeFirstVCL;
206 break;
208 case H264NALU::kSEIMessage:
209 case H264NALU::kReserved14:
210 case H264NALU::kReserved15:
211 case H264NALU::kReserved16:
212 case H264NALU::kReserved17:
213 case H264NALU::kReserved18:
214 case H264NALU::kPPS:
215 case H264NALU::kSPS:
216 if (order_state > kBeforeFirstVCL) {
217 DVLOG(1) << "Unexpected NALU type " << nalu.nal_unit_type
218 << " in order_state " << order_state;
219 return false;
221 order_state = kBeforeFirstVCL;
222 break;
224 case H264NALU::kSPSExt:
225 if (last_nalu_type != H264NALU::kSPS) {
226 DVLOG(1) << "SPS extension does not follow an SPS.";
227 return false;
229 break;
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;
238 return false;
240 order_state = kAfterFirstVCL;
241 break;
243 case H264NALU::kCodedSliceAux:
244 if (order_state != kAfterFirstVCL) {
245 DVLOG(1) << "Unexpected extension in order_state " << order_state;
246 return false;
248 break;
250 case H264NALU::kEOSeq:
251 if (order_state != kAfterFirstVCL) {
252 DVLOG(1) << "Unexpected EOSeq in order_state " << order_state;
253 return false;
255 order_state = kEOStreamAllowed;
256 break;
258 case H264NALU::kEOStream:
259 if (order_state < kAfterFirstVCL) {
260 DVLOG(1) << "Unexpected EOStream in order_state " << order_state;
261 return false;
263 order_state = kNoMoreDataAllowed;
264 break;
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;
272 return false;
274 break;
276 default:
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;
282 return false;
285 last_nalu_type = nalu.nal_unit_type;
286 break;
288 case H264Parser::kInvalidStream:
289 return false;
291 case H264Parser::kUnsupportedStream:
292 NOTREACHED() << "AdvanceToNextNALU() returned kUnsupportedStream!";
293 return false;
295 case H264Parser::kEOStream:
296 done = true;
300 return order_state >= kAfterFirstVCL;
302 } // namespace mp4
303 } // namespace media