1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "avc_utils"
19 #include <utils/Log.h>
20
21 #include "include/avc_utils.h"
22
23 #include <media/stagefright/foundation/ABitReader.h>
24 #include <media/stagefright/foundation/ADebug.h>
25 #include <media/stagefright/foundation/hexdump.h>
26 #include <media/stagefright/MediaDefs.h>
27 #include <media/stagefright/MediaErrors.h>
28 #include <media/stagefright/MetaData.h>
29 #include <utils/misc.h>
30
31 namespace android {
32
parseUE(ABitReader * br)33 unsigned parseUE(ABitReader *br) {
34 unsigned numZeroes = 0;
35 while (br->getBits(1) == 0) {
36 ++numZeroes;
37 }
38
39 unsigned x = br->getBits(numZeroes);
40
41 return x + (1u << numZeroes) - 1;
42 }
43
parseUEWithFallback(ABitReader * br,unsigned fallback)44 unsigned parseUEWithFallback(ABitReader *br, unsigned fallback) {
45 unsigned numZeroes = 0;
46 while (br->getBitsWithFallback(1, 1) == 0) {
47 ++numZeroes;
48 }
49 uint32_t x;
50 if (numZeroes < 32) {
51 if (br->getBitsGraceful(numZeroes, &x)) {
52 return x + (1u << numZeroes) - 1;
53 } else {
54 return fallback;
55 }
56 } else {
57 br->skipBits(numZeroes);
58 return fallback;
59 }
60 }
61
parseSE(ABitReader * br)62 signed parseSE(ABitReader *br) {
63 unsigned codeNum = parseUE(br);
64
65 return (codeNum & 1) ? (codeNum + 1) / 2 : -signed(codeNum / 2);
66 }
67
parseSEWithFallback(ABitReader * br,signed fallback)68 signed parseSEWithFallback(ABitReader *br, signed fallback) {
69 // NOTE: parseUE cannot normally return ~0 as the max supported value is 0xFFFE
70 unsigned codeNum = parseUEWithFallback(br, ~0U);
71 if (codeNum == ~0U) {
72 return fallback;
73 }
74 return (codeNum & 1) ? (codeNum + 1) / 2 : -signed(codeNum / 2);
75 }
76
skipScalingList(ABitReader * br,size_t sizeOfScalingList)77 static void skipScalingList(ABitReader *br, size_t sizeOfScalingList) {
78 size_t lastScale = 8;
79 size_t nextScale = 8;
80 for (size_t j = 0; j < sizeOfScalingList; ++j) {
81 if (nextScale != 0) {
82 signed delta_scale = parseSE(br);
83 // ISO_IEC_14496-10_201402-ITU, 7.4.2.1.1.1, The value of delta_scale
84 // shall be in the range of −128 to +127, inclusive.
85 if (delta_scale < -128) {
86 ALOGW("delta_scale (%d) is below range, capped to -128", delta_scale);
87 delta_scale = -128;
88 } else if (delta_scale > 127) {
89 ALOGW("delta_scale (%d) is above range, capped to 127", delta_scale);
90 delta_scale = 127;
91 }
92 nextScale = (lastScale + (delta_scale + 256)) % 256;
93 }
94
95 lastScale = (nextScale == 0) ? lastScale : nextScale;
96 }
97 }
98
99 // Determine video dimensions from the sequence parameterset.
FindAVCDimensions(const sp<ABuffer> & seqParamSet,int32_t * width,int32_t * height,int32_t * sarWidth,int32_t * sarHeight)100 void FindAVCDimensions(
101 const sp<ABuffer> &seqParamSet,
102 int32_t *width, int32_t *height,
103 int32_t *sarWidth, int32_t *sarHeight) {
104 ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1);
105
106 unsigned profile_idc = br.getBits(8);
107 br.skipBits(16);
108 parseUE(&br); // seq_parameter_set_id
109
110 unsigned chroma_format_idc = 1; // 4:2:0 chroma format
111
112 if (profile_idc == 100 || profile_idc == 110
113 || profile_idc == 122 || profile_idc == 244
114 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) {
115 chroma_format_idc = parseUE(&br);
116 if (chroma_format_idc == 3) {
117 br.skipBits(1); // residual_colour_transform_flag
118 }
119 parseUE(&br); // bit_depth_luma_minus8
120 parseUE(&br); // bit_depth_chroma_minus8
121 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag
122
123 if (br.getBits(1)) { // seq_scaling_matrix_present_flag
124 for (size_t i = 0; i < 8; ++i) {
125 if (br.getBits(1)) { // seq_scaling_list_present_flag[i]
126
127 // WARNING: the code below has not ever been exercised...
128 // need a real-world example.
129
130 if (i < 6) {
131 // ScalingList4x4[i],16,...
132 skipScalingList(&br, 16);
133 } else {
134 // ScalingList8x8[i-6],64,...
135 skipScalingList(&br, 64);
136 }
137 }
138 }
139 }
140 }
141
142 parseUE(&br); // log2_max_frame_num_minus4
143 unsigned pic_order_cnt_type = parseUE(&br);
144
145 if (pic_order_cnt_type == 0) {
146 parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4
147 } else if (pic_order_cnt_type == 1) {
148 // offset_for_non_ref_pic, offset_for_top_to_bottom_field and
149 // offset_for_ref_frame are technically se(v), but since we are
150 // just skipping over them the midpoint does not matter.
151
152 br.getBits(1); // delta_pic_order_always_zero_flag
153 parseUE(&br); // offset_for_non_ref_pic
154 parseUE(&br); // offset_for_top_to_bottom_field
155
156 unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br);
157 for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) {
158 parseUE(&br); // offset_for_ref_frame
159 }
160 }
161
162 parseUE(&br); // num_ref_frames
163 br.getBits(1); // gaps_in_frame_num_value_allowed_flag
164
165 unsigned pic_width_in_mbs_minus1 = parseUE(&br);
166 unsigned pic_height_in_map_units_minus1 = parseUE(&br);
167 unsigned frame_mbs_only_flag = br.getBits(1);
168
169 *width = pic_width_in_mbs_minus1 * 16 + 16;
170
171 *height = (2 - frame_mbs_only_flag)
172 * (pic_height_in_map_units_minus1 * 16 + 16);
173
174 if (!frame_mbs_only_flag) {
175 br.getBits(1); // mb_adaptive_frame_field_flag
176 }
177
178 br.getBits(1); // direct_8x8_inference_flag
179
180 if (br.getBits(1)) { // frame_cropping_flag
181 unsigned frame_crop_left_offset = parseUE(&br);
182 unsigned frame_crop_right_offset = parseUE(&br);
183 unsigned frame_crop_top_offset = parseUE(&br);
184 unsigned frame_crop_bottom_offset = parseUE(&br);
185
186 unsigned cropUnitX, cropUnitY;
187 if (chroma_format_idc == 0 /* monochrome */) {
188 cropUnitX = 1;
189 cropUnitY = 2 - frame_mbs_only_flag;
190 } else {
191 unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2;
192 unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1;
193
194 cropUnitX = subWidthC;
195 cropUnitY = subHeightC * (2 - frame_mbs_only_flag);
196 }
197
198 ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u",
199 frame_crop_left_offset, frame_crop_right_offset,
200 frame_crop_top_offset, frame_crop_bottom_offset,
201 cropUnitX, cropUnitY);
202
203 *width -=
204 (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX;
205 *height -=
206 (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY;
207 }
208
209 if (sarWidth != NULL) {
210 *sarWidth = 0;
211 }
212
213 if (sarHeight != NULL) {
214 *sarHeight = 0;
215 }
216
217 if (br.getBits(1)) { // vui_parameters_present_flag
218 unsigned sar_width = 0, sar_height = 0;
219
220 if (br.getBits(1)) { // aspect_ratio_info_present_flag
221 unsigned aspect_ratio_idc = br.getBits(8);
222
223 if (aspect_ratio_idc == 255 /* extendedSAR */) {
224 sar_width = br.getBits(16);
225 sar_height = br.getBits(16);
226 } else {
227 static const struct { unsigned width, height; } kFixedSARs[] = {
228 { 0, 0 }, // Invalid
229 { 1, 1 },
230 { 12, 11 },
231 { 10, 11 },
232 { 16, 11 },
233 { 40, 33 },
234 { 24, 11 },
235 { 20, 11 },
236 { 32, 11 },
237 { 80, 33 },
238 { 18, 11 },
239 { 15, 11 },
240 { 64, 33 },
241 { 160, 99 },
242 { 4, 3 },
243 { 3, 2 },
244 { 2, 1 },
245 };
246
247 if (aspect_ratio_idc > 0 && aspect_ratio_idc < NELEM(kFixedSARs)) {
248 sar_width = kFixedSARs[aspect_ratio_idc].width;
249 sar_height = kFixedSARs[aspect_ratio_idc].height;
250 }
251 }
252 }
253
254 ALOGV("sample aspect ratio = %u : %u", sar_width, sar_height);
255
256 if (sarWidth != NULL) {
257 *sarWidth = sar_width;
258 }
259
260 if (sarHeight != NULL) {
261 *sarHeight = sar_height;
262 }
263 }
264 }
265
getNextNALUnit(const uint8_t ** _data,size_t * _size,const uint8_t ** nalStart,size_t * nalSize,bool startCodeFollows)266 status_t getNextNALUnit(
267 const uint8_t **_data, size_t *_size,
268 const uint8_t **nalStart, size_t *nalSize,
269 bool startCodeFollows) {
270 const uint8_t *data = *_data;
271 size_t size = *_size;
272
273 *nalStart = NULL;
274 *nalSize = 0;
275
276 if (size < 3) {
277 return -EAGAIN;
278 }
279
280 size_t offset = 0;
281
282 // A valid startcode consists of at least two 0x00 bytes followed by 0x01.
283 for (; offset + 2 < size; ++offset) {
284 if (data[offset + 2] == 0x01 && data[offset] == 0x00
285 && data[offset + 1] == 0x00) {
286 break;
287 }
288 }
289 if (offset + 2 >= size) {
290 *_data = &data[offset];
291 *_size = 2;
292 return -EAGAIN;
293 }
294 offset += 3;
295
296 size_t startOffset = offset;
297
298 for (;;) {
299 while (offset < size && data[offset] != 0x01) {
300 ++offset;
301 }
302
303 if (offset == size) {
304 if (startCodeFollows) {
305 offset = size + 2;
306 break;
307 }
308
309 return -EAGAIN;
310 }
311
312 if (data[offset - 1] == 0x00 && data[offset - 2] == 0x00) {
313 break;
314 }
315
316 ++offset;
317 }
318
319 size_t endOffset = offset - 2;
320 while (endOffset > startOffset + 1 && data[endOffset - 1] == 0x00) {
321 --endOffset;
322 }
323
324 *nalStart = &data[startOffset];
325 *nalSize = endOffset - startOffset;
326
327 if (offset + 2 < size) {
328 *_data = &data[offset - 2];
329 *_size = size - offset + 2;
330 } else {
331 *_data = NULL;
332 *_size = 0;
333 }
334
335 return OK;
336 }
337
FindNAL(const uint8_t * data,size_t size,unsigned nalType)338 static sp<ABuffer> FindNAL(const uint8_t *data, size_t size, unsigned nalType) {
339 const uint8_t *nalStart;
340 size_t nalSize;
341 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
342 if (nalSize > 0 && (nalStart[0] & 0x1f) == nalType) {
343 sp<ABuffer> buffer = new ABuffer(nalSize);
344 memcpy(buffer->data(), nalStart, nalSize);
345 return buffer;
346 }
347 }
348
349 return NULL;
350 }
351
AVCProfileToString(uint8_t profile)352 const char *AVCProfileToString(uint8_t profile) {
353 switch (profile) {
354 case kAVCProfileBaseline:
355 return "Baseline";
356 case kAVCProfileMain:
357 return "Main";
358 case kAVCProfileExtended:
359 return "Extended";
360 case kAVCProfileHigh:
361 return "High";
362 case kAVCProfileHigh10:
363 return "High 10";
364 case kAVCProfileHigh422:
365 return "High 422";
366 case kAVCProfileHigh444:
367 return "High 444";
368 case kAVCProfileCAVLC444Intra:
369 return "CAVLC 444 Intra";
370 default: return "Unknown";
371 }
372 }
373
MakeAVCCodecSpecificData(const sp<ABuffer> & accessUnit)374 sp<MetaData> MakeAVCCodecSpecificData(const sp<ABuffer> &accessUnit) {
375 const uint8_t *data = accessUnit->data();
376 size_t size = accessUnit->size();
377
378 sp<ABuffer> seqParamSet = FindNAL(data, size, 7);
379 if (seqParamSet == NULL) {
380 return NULL;
381 }
382
383 int32_t width, height;
384 int32_t sarWidth, sarHeight;
385 FindAVCDimensions(
386 seqParamSet, &width, &height, &sarWidth, &sarHeight);
387
388 sp<ABuffer> picParamSet = FindNAL(data, size, 8);
389 CHECK(picParamSet != NULL);
390
391 size_t csdSize =
392 1 + 3 + 1 + 1
393 + 2 * 1 + seqParamSet->size()
394 + 1 + 2 * 1 + picParamSet->size();
395
396 sp<ABuffer> csd = new ABuffer(csdSize);
397 uint8_t *out = csd->data();
398
399 *out++ = 0x01; // configurationVersion
400 memcpy(out, seqParamSet->data() + 1, 3); // profile/level...
401
402 uint8_t profile = out[0];
403 uint8_t level = out[2];
404
405 out += 3;
406 *out++ = (0x3f << 2) | 1; // lengthSize == 2 bytes
407 *out++ = 0xe0 | 1;
408
409 *out++ = seqParamSet->size() >> 8;
410 *out++ = seqParamSet->size() & 0xff;
411 memcpy(out, seqParamSet->data(), seqParamSet->size());
412 out += seqParamSet->size();
413
414 *out++ = 1;
415
416 *out++ = picParamSet->size() >> 8;
417 *out++ = picParamSet->size() & 0xff;
418 memcpy(out, picParamSet->data(), picParamSet->size());
419
420 #if 0
421 ALOGI("AVC seq param set");
422 hexdump(seqParamSet->data(), seqParamSet->size());
423 #endif
424
425 sp<MetaData> meta = new MetaData;
426 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
427
428 meta->setData(kKeyAVCC, kTypeAVCC, csd->data(), csd->size());
429 meta->setInt32(kKeyWidth, width);
430 meta->setInt32(kKeyHeight, height);
431
432 if ((sarWidth > 0 && sarHeight > 0) && (sarWidth != 1 || sarHeight != 1)) {
433 // We treat *:0 and 0:* (unspecified) as 1:1.
434
435 meta->setInt32(kKeySARWidth, sarWidth);
436 meta->setInt32(kKeySARHeight, sarHeight);
437
438 ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d) "
439 "SAR %d : %d",
440 width,
441 height,
442 AVCProfileToString(profile),
443 level / 10,
444 level % 10,
445 sarWidth,
446 sarHeight);
447 } else {
448 ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d)",
449 width,
450 height,
451 AVCProfileToString(profile),
452 level / 10,
453 level % 10);
454 }
455
456 return meta;
457 }
458
459 template <typename T>
IsIDRInternal(const sp<T> & buffer)460 bool IsIDRInternal(const sp<T> &buffer) {
461 const uint8_t *data = buffer->data();
462 size_t size = buffer->size();
463
464 bool foundIDR = false;
465
466 const uint8_t *nalStart;
467 size_t nalSize;
468 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
469 if (nalSize == 0u) {
470 ALOGW("skipping empty nal unit from potentially malformed bitstream");
471 continue;
472 }
473
474 unsigned nalType = nalStart[0] & 0x1f;
475
476 if (nalType == 5) {
477 foundIDR = true;
478 break;
479 }
480 }
481
482 return foundIDR;
483 }
484
IsIDR(const sp<ABuffer> & buffer)485 bool IsIDR(const sp<ABuffer> &buffer) {
486 return IsIDRInternal(buffer);
487 }
488
IsIDR(const sp<MediaCodecBuffer> & buffer)489 bool IsIDR(const sp<MediaCodecBuffer> &buffer) {
490 return IsIDRInternal(buffer);
491 }
492
IsAVCReferenceFrame(const sp<ABuffer> & accessUnit)493 bool IsAVCReferenceFrame(const sp<ABuffer> &accessUnit) {
494 const uint8_t *data = accessUnit->data();
495 size_t size = accessUnit->size();
496 if (data == NULL) {
497 ALOGE("IsAVCReferenceFrame: called on NULL data (%p, %zu)", accessUnit.get(), size);
498 return false;
499 }
500
501 const uint8_t *nalStart;
502 size_t nalSize;
503 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
504 if (nalSize == 0) {
505 ALOGE("IsAVCReferenceFrame: invalid nalSize: 0 (%p, %zu)", accessUnit.get(), size);
506 return false;
507 }
508
509 unsigned nalType = nalStart[0] & 0x1f;
510
511 if (nalType == 5) {
512 return true;
513 } else if (nalType == 1) {
514 unsigned nal_ref_idc = (nalStart[0] >> 5) & 3;
515 return nal_ref_idc != 0;
516 }
517 }
518
519 return true;
520 }
521
FindAVCLayerId(const uint8_t * data,size_t size)522 uint32_t FindAVCLayerId(const uint8_t *data, size_t size) {
523 CHECK(data != NULL);
524
525 const unsigned kSvcNalType = 0xE;
526 const unsigned kSvcNalSearchRange = 32;
527 // SVC NAL
528 // |---0 1110|1--- ----|---- ----|iii- ---|
529 // ^ ^
530 // NAL-type = 0xE layer-Id
531 //
532 // layer_id 0 is for base layer, while 1, 2, ... are enhancement layers.
533 // Layer n uses reference frames from layer 0, 1, ..., n-1.
534
535 uint32_t layerId = 0;
536 sp<ABuffer> svcNAL = FindNAL(
537 data, size > kSvcNalSearchRange ? kSvcNalSearchRange : size, kSvcNalType);
538 if (svcNAL != NULL && svcNAL->size() >= 4) {
539 layerId = (*(svcNAL->data() + 3) >> 5) & 0x7;
540 }
541 return layerId;
542 }
543
MakeAACCodecSpecificData(unsigned profile,unsigned sampling_freq_index,unsigned channel_configuration)544 sp<MetaData> MakeAACCodecSpecificData(
545 unsigned profile, unsigned sampling_freq_index,
546 unsigned channel_configuration) {
547 sp<MetaData> meta = new MetaData;
548 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
549
550 CHECK_LE(sampling_freq_index, 11u);
551 static const int32_t kSamplingFreq[] = {
552 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
553 16000, 12000, 11025, 8000
554 };
555 meta->setInt32(kKeySampleRate, kSamplingFreq[sampling_freq_index]);
556 meta->setInt32(kKeyChannelCount, channel_configuration);
557
558 static const uint8_t kStaticESDS[] = {
559 0x03, 22,
560 0x00, 0x00, // ES_ID
561 0x00, // streamDependenceFlag, URL_Flag, OCRstreamFlag
562
563 0x04, 17,
564 0x40, // Audio ISO/IEC 14496-3
565 0x00, 0x00, 0x00, 0x00,
566 0x00, 0x00, 0x00, 0x00,
567 0x00, 0x00, 0x00, 0x00,
568
569 0x05, 2,
570 // AudioSpecificInfo follows
571
572 // oooo offf fccc c000
573 // o - audioObjectType
574 // f - samplingFreqIndex
575 // c - channelConfig
576 };
577 sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
578 memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
579
580 csd->data()[sizeof(kStaticESDS)] =
581 ((profile + 1) << 3) | (sampling_freq_index >> 1);
582
583 csd->data()[sizeof(kStaticESDS) + 1] =
584 ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3);
585
586 meta->setData(kKeyESDS, 0, csd->data(), csd->size());
587
588 return meta;
589 }
590
ExtractDimensionsFromVOLHeader(const uint8_t * data,size_t size,int32_t * width,int32_t * height)591 bool ExtractDimensionsFromVOLHeader(
592 const uint8_t *data, size_t size, int32_t *width, int32_t *height) {
593 ABitReader br(&data[4], size - 4);
594 br.skipBits(1); // random_accessible_vol
595 unsigned video_object_type_indication = br.getBits(8);
596
597 CHECK_NE(video_object_type_indication,
598 0x21u /* Fine Granularity Scalable */);
599
600 unsigned video_object_layer_verid __unused;
601 unsigned video_object_layer_priority __unused;
602 if (br.getBits(1)) {
603 video_object_layer_verid = br.getBits(4);
604 video_object_layer_priority = br.getBits(3);
605 }
606 unsigned aspect_ratio_info = br.getBits(4);
607 if (aspect_ratio_info == 0x0f /* extended PAR */) {
608 br.skipBits(8); // par_width
609 br.skipBits(8); // par_height
610 }
611 if (br.getBits(1)) { // vol_control_parameters
612 br.skipBits(2); // chroma_format
613 br.skipBits(1); // low_delay
614 if (br.getBits(1)) { // vbv_parameters
615 br.skipBits(15); // first_half_bit_rate
616 CHECK(br.getBits(1)); // marker_bit
617 br.skipBits(15); // latter_half_bit_rate
618 CHECK(br.getBits(1)); // marker_bit
619 br.skipBits(15); // first_half_vbv_buffer_size
620 CHECK(br.getBits(1)); // marker_bit
621 br.skipBits(3); // latter_half_vbv_buffer_size
622 br.skipBits(11); // first_half_vbv_occupancy
623 CHECK(br.getBits(1)); // marker_bit
624 br.skipBits(15); // latter_half_vbv_occupancy
625 CHECK(br.getBits(1)); // marker_bit
626 }
627 }
628 unsigned video_object_layer_shape = br.getBits(2);
629 CHECK_EQ(video_object_layer_shape, 0x00u /* rectangular */);
630
631 CHECK(br.getBits(1)); // marker_bit
632 unsigned vop_time_increment_resolution = br.getBits(16);
633 CHECK(br.getBits(1)); // marker_bit
634
635 if (br.getBits(1)) { // fixed_vop_rate
636 // range [0..vop_time_increment_resolution)
637
638 // vop_time_increment_resolution
639 // 2 => 0..1, 1 bit
640 // 3 => 0..2, 2 bits
641 // 4 => 0..3, 2 bits
642 // 5 => 0..4, 3 bits
643 // ...
644
645 CHECK_GT(vop_time_increment_resolution, 0u);
646 --vop_time_increment_resolution;
647
648 unsigned numBits = 0;
649 while (vop_time_increment_resolution > 0) {
650 ++numBits;
651 vop_time_increment_resolution >>= 1;
652 }
653
654 br.skipBits(numBits); // fixed_vop_time_increment
655 }
656
657 CHECK(br.getBits(1)); // marker_bit
658 unsigned video_object_layer_width = br.getBits(13);
659 CHECK(br.getBits(1)); // marker_bit
660 unsigned video_object_layer_height = br.getBits(13);
661 CHECK(br.getBits(1)); // marker_bit
662
663 unsigned interlaced __unused = br.getBits(1);
664
665 *width = video_object_layer_width;
666 *height = video_object_layer_height;
667
668 return true;
669 }
670
GetMPEGAudioFrameSize(uint32_t header,size_t * frame_size,int * out_sampling_rate,int * out_channels,int * out_bitrate,int * out_num_samples)671 bool GetMPEGAudioFrameSize(
672 uint32_t header, size_t *frame_size,
673 int *out_sampling_rate, int *out_channels,
674 int *out_bitrate, int *out_num_samples) {
675 *frame_size = 0;
676
677 if (out_sampling_rate) {
678 *out_sampling_rate = 0;
679 }
680
681 if (out_channels) {
682 *out_channels = 0;
683 }
684
685 if (out_bitrate) {
686 *out_bitrate = 0;
687 }
688
689 if (out_num_samples) {
690 *out_num_samples = 1152;
691 }
692
693 if ((header & 0xffe00000) != 0xffe00000) {
694 return false;
695 }
696
697 unsigned version = (header >> 19) & 3;
698
699 if (version == 0x01) {
700 return false;
701 }
702
703 unsigned layer = (header >> 17) & 3;
704
705 if (layer == 0x00) {
706 return false;
707 }
708
709 unsigned protection __unused = (header >> 16) & 1;
710
711 unsigned bitrate_index = (header >> 12) & 0x0f;
712
713 if (bitrate_index == 0 || bitrate_index == 0x0f) {
714 // Disallow "free" bitrate.
715 return false;
716 }
717
718 unsigned sampling_rate_index = (header >> 10) & 3;
719
720 if (sampling_rate_index == 3) {
721 return false;
722 }
723
724 static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
725 int sampling_rate = kSamplingRateV1[sampling_rate_index];
726 if (version == 2 /* V2 */) {
727 sampling_rate /= 2;
728 } else if (version == 0 /* V2.5 */) {
729 sampling_rate /= 4;
730 }
731
732 unsigned padding = (header >> 9) & 1;
733
734 if (layer == 3) {
735 // layer I
736
737 static const int kBitrateV1[] = {
738 32, 64, 96, 128, 160, 192, 224, 256,
739 288, 320, 352, 384, 416, 448
740 };
741
742 static const int kBitrateV2[] = {
743 32, 48, 56, 64, 80, 96, 112, 128,
744 144, 160, 176, 192, 224, 256
745 };
746
747 int bitrate =
748 (version == 3 /* V1 */)
749 ? kBitrateV1[bitrate_index - 1]
750 : kBitrateV2[bitrate_index - 1];
751
752 if (out_bitrate) {
753 *out_bitrate = bitrate;
754 }
755
756 *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
757
758 if (out_num_samples) {
759 *out_num_samples = 384;
760 }
761 } else {
762 // layer II or III
763
764 static const int kBitrateV1L2[] = {
765 32, 48, 56, 64, 80, 96, 112, 128,
766 160, 192, 224, 256, 320, 384
767 };
768
769 static const int kBitrateV1L3[] = {
770 32, 40, 48, 56, 64, 80, 96, 112,
771 128, 160, 192, 224, 256, 320
772 };
773
774 static const int kBitrateV2[] = {
775 8, 16, 24, 32, 40, 48, 56, 64,
776 80, 96, 112, 128, 144, 160
777 };
778
779 int bitrate;
780 if (version == 3 /* V1 */) {
781 bitrate = (layer == 2 /* L2 */)
782 ? kBitrateV1L2[bitrate_index - 1]
783 : kBitrateV1L3[bitrate_index - 1];
784
785 if (out_num_samples) {
786 *out_num_samples = 1152;
787 }
788 } else {
789 // V2 (or 2.5)
790
791 bitrate = kBitrateV2[bitrate_index - 1];
792 if (out_num_samples) {
793 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
794 }
795 }
796
797 if (out_bitrate) {
798 *out_bitrate = bitrate;
799 }
800
801 if (version == 3 /* V1 */) {
802 *frame_size = 144000 * bitrate / sampling_rate + padding;
803 } else {
804 // V2 or V2.5
805 size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
806 *frame_size = tmp * bitrate / sampling_rate + padding;
807 }
808 }
809
810 if (out_sampling_rate) {
811 *out_sampling_rate = sampling_rate;
812 }
813
814 if (out_channels) {
815 int channel_mode = (header >> 6) & 3;
816
817 *out_channels = (channel_mode == 3) ? 1 : 2;
818 }
819
820 return true;
821 }
822
823 } // namespace android
824
825