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
22 #include <media/stagefright/foundation/ABitReader.h>
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/avc_utils.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 if (__builtin_mul_overflow(pic_width_in_mbs_minus1, 16, &pic_width_in_mbs_minus1) ||
171 __builtin_add_overflow(pic_width_in_mbs_minus1, 16, width)) {
172 *width = 0;
173 }
174
175 // *height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 * 16 + 16);
176 if (__builtin_mul_overflow(
177 pic_height_in_map_units_minus1, 16, &pic_height_in_map_units_minus1) ||
178 __builtin_add_overflow(
179 pic_height_in_map_units_minus1, 16, &pic_height_in_map_units_minus1) ||
180 __builtin_mul_overflow(
181 pic_height_in_map_units_minus1, (2 - frame_mbs_only_flag), height)) {
182 *height = 0;
183 }
184
185 if (!frame_mbs_only_flag) {
186 br.getBits(1); // mb_adaptive_frame_field_flag
187 }
188
189 br.getBits(1); // direct_8x8_inference_flag
190
191 if (br.getBits(1)) { // frame_cropping_flag
192 unsigned frame_crop_left_offset = parseUE(&br);
193 unsigned frame_crop_right_offset = parseUE(&br);
194 unsigned frame_crop_top_offset = parseUE(&br);
195 unsigned frame_crop_bottom_offset = parseUE(&br);
196
197 unsigned cropUnitX, cropUnitY;
198 if (chroma_format_idc == 0 /* monochrome */) {
199 cropUnitX = 1;
200 cropUnitY = 2 - frame_mbs_only_flag;
201 } else {
202 unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2;
203 unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1;
204
205 cropUnitX = subWidthC;
206 cropUnitY = subHeightC * (2 - frame_mbs_only_flag);
207 }
208
209 ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u",
210 frame_crop_left_offset, frame_crop_right_offset,
211 frame_crop_top_offset, frame_crop_bottom_offset,
212 cropUnitX, cropUnitY);
213
214
215 // *width -= (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX;
216 if(__builtin_add_overflow(
217 frame_crop_left_offset, frame_crop_right_offset, &frame_crop_left_offset) ||
218 __builtin_mul_overflow(frame_crop_left_offset, cropUnitX, &frame_crop_left_offset) ||
219 __builtin_sub_overflow(*width, frame_crop_left_offset, width) ||
220 *width < 0) {
221 *width = 0;
222 }
223
224 //*height -= (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY;
225 if(__builtin_add_overflow(
226 frame_crop_top_offset, frame_crop_bottom_offset, &frame_crop_top_offset) ||
227 __builtin_mul_overflow(frame_crop_top_offset, cropUnitY, &frame_crop_top_offset) ||
228 __builtin_sub_overflow(*height, frame_crop_top_offset, height) ||
229 *height < 0) {
230 *height = 0;
231 }
232 }
233
234 if (sarWidth != NULL) {
235 *sarWidth = 0;
236 }
237
238 if (sarHeight != NULL) {
239 *sarHeight = 0;
240 }
241
242 if (br.getBits(1)) { // vui_parameters_present_flag
243 unsigned sar_width = 0, sar_height = 0;
244
245 if (br.getBits(1)) { // aspect_ratio_info_present_flag
246 unsigned aspect_ratio_idc = br.getBits(8);
247
248 if (aspect_ratio_idc == 255 /* extendedSAR */) {
249 sar_width = br.getBits(16);
250 sar_height = br.getBits(16);
251 } else {
252 static const struct { unsigned width, height; } kFixedSARs[] = {
253 { 0, 0 }, // Invalid
254 { 1, 1 },
255 { 12, 11 },
256 { 10, 11 },
257 { 16, 11 },
258 { 40, 33 },
259 { 24, 11 },
260 { 20, 11 },
261 { 32, 11 },
262 { 80, 33 },
263 { 18, 11 },
264 { 15, 11 },
265 { 64, 33 },
266 { 160, 99 },
267 { 4, 3 },
268 { 3, 2 },
269 { 2, 1 },
270 };
271
272 if (aspect_ratio_idc > 0 && aspect_ratio_idc < NELEM(kFixedSARs)) {
273 sar_width = kFixedSARs[aspect_ratio_idc].width;
274 sar_height = kFixedSARs[aspect_ratio_idc].height;
275 }
276 }
277 }
278
279 ALOGV("sample aspect ratio = %u : %u", sar_width, sar_height);
280
281 if (sarWidth != NULL) {
282 *sarWidth = sar_width;
283 }
284
285 if (sarHeight != NULL) {
286 *sarHeight = sar_height;
287 }
288 }
289 }
290
getNextNALUnit(const uint8_t ** _data,size_t * _size,const uint8_t ** nalStart,size_t * nalSize,bool startCodeFollows)291 status_t getNextNALUnit(
292 const uint8_t **_data, size_t *_size,
293 const uint8_t **nalStart, size_t *nalSize,
294 bool startCodeFollows) {
295 const uint8_t *data = *_data;
296 size_t size = *_size;
297
298 *nalStart = NULL;
299 *nalSize = 0;
300
301 if (size < 3) {
302 return -EAGAIN;
303 }
304
305 size_t offset = 0;
306
307 // A valid startcode consists of at least two 0x00 bytes followed by 0x01.
308 for (; offset + 2 < size; ++offset) {
309 if (data[offset + 2] == 0x01 && data[offset] == 0x00
310 && data[offset + 1] == 0x00) {
311 break;
312 }
313 }
314 if (offset + 2 >= size) {
315 *_data = &data[offset];
316 *_size = 2;
317 return -EAGAIN;
318 }
319 offset += 3;
320
321 size_t startOffset = offset;
322
323 for (;;) {
324 while (offset < size && data[offset] != 0x01) {
325 ++offset;
326 }
327
328 if (offset == size) {
329 if (startCodeFollows) {
330 offset = size + 2;
331 break;
332 }
333
334 return -EAGAIN;
335 }
336
337 if (data[offset - 1] == 0x00 && data[offset - 2] == 0x00) {
338 break;
339 }
340
341 ++offset;
342 }
343
344 size_t endOffset = offset - 2;
345 while (endOffset > startOffset + 1 && data[endOffset - 1] == 0x00) {
346 --endOffset;
347 }
348
349 *nalStart = &data[startOffset];
350 *nalSize = endOffset - startOffset;
351
352 if (offset + 2 < size) {
353 *_data = &data[offset - 2];
354 *_size = size - offset + 2;
355 } else {
356 *_data = NULL;
357 *_size = 0;
358 }
359
360 return OK;
361 }
362
FindNAL(const uint8_t * data,size_t size,unsigned nalType)363 static sp<ABuffer> FindNAL(const uint8_t *data, size_t size, unsigned nalType) {
364 const uint8_t *nalStart;
365 size_t nalSize;
366 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
367 if (nalSize > 0 && (nalStart[0] & 0x1f) == nalType) {
368 sp<ABuffer> buffer = new ABuffer(nalSize);
369 memcpy(buffer->data(), nalStart, nalSize);
370 return buffer;
371 }
372 }
373
374 return NULL;
375 }
376
AVCProfileToString(uint8_t profile)377 const char *AVCProfileToString(uint8_t profile) {
378 switch (profile) {
379 case kAVCProfileBaseline:
380 return "Baseline";
381 case kAVCProfileMain:
382 return "Main";
383 case kAVCProfileExtended:
384 return "Extended";
385 case kAVCProfileHigh:
386 return "High";
387 case kAVCProfileHigh10:
388 return "High 10";
389 case kAVCProfileHigh422:
390 return "High 422";
391 case kAVCProfileHigh444:
392 return "High 444";
393 case kAVCProfileCAVLC444Intra:
394 return "CAVLC 444 Intra";
395 default: return "Unknown";
396 }
397 }
398
MakeAVCCodecSpecificData(const sp<ABuffer> & accessUnit,int32_t * width,int32_t * height,int32_t * sarWidth,int32_t * sarHeight)399 sp<ABuffer> MakeAVCCodecSpecificData(
400 const sp<ABuffer> &accessUnit, int32_t *width, int32_t *height,
401 int32_t *sarWidth, int32_t *sarHeight) {
402 const uint8_t *data = accessUnit->data();
403 size_t size = accessUnit->size();
404
405 sp<ABuffer> seqParamSet = FindNAL(data, size, 7);
406 if (seqParamSet == NULL) {
407 return NULL;
408 }
409
410 FindAVCDimensions(
411 seqParamSet, width, height, sarWidth, sarHeight);
412
413 sp<ABuffer> picParamSet = FindNAL(data, size, 8);
414 CHECK(picParamSet != NULL);
415
416 size_t csdSize =
417 1 + 3 + 1 + 1
418 + 2 * 1 + seqParamSet->size()
419 + 1 + 2 * 1 + picParamSet->size();
420
421 sp<ABuffer> csd = new ABuffer(csdSize);
422 uint8_t *out = csd->data();
423
424 *out++ = 0x01; // configurationVersion
425 memcpy(out, seqParamSet->data() + 1, 3); // profile/level...
426
427 uint8_t profile = out[0];
428 uint8_t level = out[2];
429
430 out += 3;
431 *out++ = (0x3f << 2) | 1; // lengthSize == 2 bytes
432 *out++ = 0xe0 | 1;
433
434 *out++ = seqParamSet->size() >> 8;
435 *out++ = seqParamSet->size() & 0xff;
436 memcpy(out, seqParamSet->data(), seqParamSet->size());
437 out += seqParamSet->size();
438
439 *out++ = 1;
440
441 *out++ = picParamSet->size() >> 8;
442 *out++ = picParamSet->size() & 0xff;
443 memcpy(out, picParamSet->data(), picParamSet->size());
444
445 #if 0
446 ALOGI("AVC seq param set");
447 hexdump(seqParamSet->data(), seqParamSet->size());
448 #endif
449
450
451 if (sarWidth != nullptr && sarHeight != nullptr) {
452 if ((*sarWidth > 0 && *sarHeight > 0) && (*sarWidth != 1 || *sarHeight != 1)) {
453 ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d) "
454 "SAR %d : %d",
455 *width,
456 *height,
457 AVCProfileToString(profile),
458 level / 10,
459 level % 10,
460 *sarWidth,
461 *sarHeight);
462 } else {
463 // We treat *:0 and 0:* (unspecified) as 1:1.
464 *sarWidth = 0;
465 *sarHeight = 0;
466 ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d)",
467 *width,
468 *height,
469 AVCProfileToString(profile),
470 level / 10,
471 level % 10);
472 }
473 }
474
475 return csd;
476 }
477
IsIDR(const uint8_t * data,size_t size)478 bool IsIDR(const uint8_t *data, size_t size) {
479 // const uint8_t *data = buffer->data();
480 // size_t size = buffer->size();
481 bool foundIDR = false;
482
483 const uint8_t *nalStart;
484 size_t nalSize;
485 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
486 if (nalSize == 0u) {
487 ALOGW("skipping empty nal unit from potentially malformed bitstream");
488 continue;
489 }
490
491 unsigned nalType = nalStart[0] & 0x1f;
492
493 if (nalType == 5) {
494 foundIDR = true;
495 break;
496 }
497 }
498
499 return foundIDR;
500 }
501
IsAVCReferenceFrame(const sp<ABuffer> & accessUnit)502 bool IsAVCReferenceFrame(const sp<ABuffer> &accessUnit) {
503 const uint8_t *data = accessUnit->data();
504 size_t size = accessUnit->size();
505 if (data == NULL) {
506 ALOGE("IsAVCReferenceFrame: called on NULL data (%p, %zu)", accessUnit.get(), size);
507 return false;
508 }
509
510 const uint8_t *nalStart;
511 size_t nalSize;
512 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
513 if (nalSize == 0) {
514 ALOGE("IsAVCReferenceFrame: invalid nalSize: 0 (%p, %zu)", accessUnit.get(), size);
515 return false;
516 }
517
518 unsigned nalType = nalStart[0] & 0x1f;
519
520 if (nalType == 5) {
521 return true;
522 } else if (nalType == 1) {
523 unsigned nal_ref_idc = (nalStart[0] >> 5) & 3;
524 return nal_ref_idc != 0;
525 }
526 }
527
528 return true;
529 }
530
FindAVCLayerId(const uint8_t * data,size_t size)531 uint32_t FindAVCLayerId(const uint8_t *data, size_t size) {
532 CHECK(data != NULL);
533
534 const unsigned kSvcNalType = 0xE;
535 const unsigned kSvcNalSearchRange = 32;
536 // SVC NAL
537 // |---0 1110|1--- ----|---- ----|iii- ---|
538 // ^ ^
539 // NAL-type = 0xE layer-Id
540 //
541 // layer_id 0 is for base layer, while 1, 2, ... are enhancement layers.
542 // Layer n uses reference frames from layer 0, 1, ..., n-1.
543
544 uint32_t layerId = 0;
545 sp<ABuffer> svcNAL = FindNAL(
546 data, size > kSvcNalSearchRange ? kSvcNalSearchRange : size, kSvcNalType);
547 if (svcNAL != NULL && svcNAL->size() >= 4) {
548 layerId = (*(svcNAL->data() + 3) >> 5) & 0x7;
549 }
550 return layerId;
551 }
552
ExtractDimensionsFromVOLHeader(const uint8_t * data,size_t size,int32_t * width,int32_t * height)553 bool ExtractDimensionsFromVOLHeader(
554 const uint8_t *data, size_t size, int32_t *width, int32_t *height) {
555 ABitReader br(&data[4], size - 4);
556 br.skipBits(1); // random_accessible_vol
557 unsigned video_object_type_indication = br.getBits(8);
558
559 CHECK_NE(video_object_type_indication,
560 0x21u /* Fine Granularity Scalable */);
561
562 unsigned video_object_layer_verid __unused;
563 unsigned video_object_layer_priority __unused;
564 if (br.getBits(1)) {
565 video_object_layer_verid = br.getBits(4);
566 video_object_layer_priority = br.getBits(3);
567 }
568 unsigned aspect_ratio_info = br.getBits(4);
569 if (aspect_ratio_info == 0x0f /* extended PAR */) {
570 br.skipBits(8); // par_width
571 br.skipBits(8); // par_height
572 }
573 if (br.getBits(1)) { // vol_control_parameters
574 br.skipBits(2); // chroma_format
575 br.skipBits(1); // low_delay
576 if (br.getBits(1)) { // vbv_parameters
577 br.skipBits(15); // first_half_bit_rate
578 CHECK(br.getBits(1)); // marker_bit
579 br.skipBits(15); // latter_half_bit_rate
580 CHECK(br.getBits(1)); // marker_bit
581 br.skipBits(15); // first_half_vbv_buffer_size
582 CHECK(br.getBits(1)); // marker_bit
583 br.skipBits(3); // latter_half_vbv_buffer_size
584 br.skipBits(11); // first_half_vbv_occupancy
585 CHECK(br.getBits(1)); // marker_bit
586 br.skipBits(15); // latter_half_vbv_occupancy
587 CHECK(br.getBits(1)); // marker_bit
588 }
589 }
590 unsigned video_object_layer_shape = br.getBits(2);
591 CHECK_EQ(video_object_layer_shape, 0x00u /* rectangular */);
592
593 CHECK(br.getBits(1)); // marker_bit
594 unsigned vop_time_increment_resolution = br.getBits(16);
595 CHECK(br.getBits(1)); // marker_bit
596
597 if (br.getBits(1)) { // fixed_vop_rate
598 // range [0..vop_time_increment_resolution)
599
600 // vop_time_increment_resolution
601 // 2 => 0..1, 1 bit
602 // 3 => 0..2, 2 bits
603 // 4 => 0..3, 2 bits
604 // 5 => 0..4, 3 bits
605 // ...
606
607 CHECK_GT(vop_time_increment_resolution, 0u);
608 --vop_time_increment_resolution;
609
610 unsigned numBits = 0;
611 while (vop_time_increment_resolution > 0) {
612 ++numBits;
613 vop_time_increment_resolution >>= 1;
614 }
615
616 br.skipBits(numBits); // fixed_vop_time_increment
617 }
618
619 CHECK(br.getBits(1)); // marker_bit
620 unsigned video_object_layer_width = br.getBits(13);
621 CHECK(br.getBits(1)); // marker_bit
622 unsigned video_object_layer_height = br.getBits(13);
623 CHECK(br.getBits(1)); // marker_bit
624
625 unsigned interlaced __unused = br.getBits(1);
626
627 *width = video_object_layer_width;
628 *height = video_object_layer_height;
629
630 return true;
631 }
632
GetMPEGAudioFrameSize(uint32_t header,size_t * frame_size,int * out_sampling_rate,int * out_channels,int * out_bitrate,int * out_num_samples)633 bool GetMPEGAudioFrameSize(
634 uint32_t header, size_t *frame_size,
635 int *out_sampling_rate, int *out_channels,
636 int *out_bitrate, int *out_num_samples) {
637 *frame_size = 0;
638
639 if (out_sampling_rate) {
640 *out_sampling_rate = 0;
641 }
642
643 if (out_channels) {
644 *out_channels = 0;
645 }
646
647 if (out_bitrate) {
648 *out_bitrate = 0;
649 }
650
651 if (out_num_samples) {
652 *out_num_samples = 1152;
653 }
654
655 if ((header & 0xffe00000) != 0xffe00000) {
656 return false;
657 }
658
659 unsigned version = (header >> 19) & 3;
660
661 if (version == 0x01) {
662 return false;
663 }
664
665 unsigned layer = (header >> 17) & 3;
666
667 if (layer == 0x00) {
668 return false;
669 }
670
671 unsigned protection __unused = (header >> 16) & 1;
672
673 unsigned bitrate_index = (header >> 12) & 0x0f;
674
675 if (bitrate_index == 0 || bitrate_index == 0x0f) {
676 // Disallow "free" bitrate.
677 return false;
678 }
679
680 unsigned sampling_rate_index = (header >> 10) & 3;
681
682 if (sampling_rate_index == 3) {
683 return false;
684 }
685
686 static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
687 int sampling_rate = kSamplingRateV1[sampling_rate_index];
688 if (version == 2 /* V2 */) {
689 sampling_rate /= 2;
690 } else if (version == 0 /* V2.5 */) {
691 sampling_rate /= 4;
692 }
693
694 unsigned padding = (header >> 9) & 1;
695
696 if (layer == 3) {
697 // layer I
698
699 static const int kBitrateV1[] = {
700 32, 64, 96, 128, 160, 192, 224, 256,
701 288, 320, 352, 384, 416, 448
702 };
703
704 static const int kBitrateV2[] = {
705 32, 48, 56, 64, 80, 96, 112, 128,
706 144, 160, 176, 192, 224, 256
707 };
708
709 int bitrate =
710 (version == 3 /* V1 */)
711 ? kBitrateV1[bitrate_index - 1]
712 : kBitrateV2[bitrate_index - 1];
713
714 if (out_bitrate) {
715 *out_bitrate = bitrate;
716 }
717
718 *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
719
720 if (out_num_samples) {
721 *out_num_samples = 384;
722 }
723 } else {
724 // layer II or III
725
726 static const int kBitrateV1L2[] = {
727 32, 48, 56, 64, 80, 96, 112, 128,
728 160, 192, 224, 256, 320, 384
729 };
730
731 static const int kBitrateV1L3[] = {
732 32, 40, 48, 56, 64, 80, 96, 112,
733 128, 160, 192, 224, 256, 320
734 };
735
736 static const int kBitrateV2[] = {
737 8, 16, 24, 32, 40, 48, 56, 64,
738 80, 96, 112, 128, 144, 160
739 };
740
741 int bitrate;
742 if (version == 3 /* V1 */) {
743 bitrate = (layer == 2 /* L2 */)
744 ? kBitrateV1L2[bitrate_index - 1]
745 : kBitrateV1L3[bitrate_index - 1];
746
747 if (out_num_samples) {
748 *out_num_samples = 1152;
749 }
750 } else {
751 // V2 (or 2.5)
752
753 bitrate = kBitrateV2[bitrate_index - 1];
754 if (out_num_samples) {
755 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
756 }
757 }
758
759 if (out_bitrate) {
760 *out_bitrate = bitrate;
761 }
762
763 if (version == 3 /* V1 */) {
764 *frame_size = 144000 * bitrate / sampling_rate + padding;
765 } else {
766 // V2 or V2.5
767 size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
768 *frame_size = tmp * bitrate / sampling_rate + padding;
769 }
770 }
771
772 if (out_sampling_rate) {
773 *out_sampling_rate = sampling_rate;
774 }
775
776 if (out_channels) {
777 int channel_mode = (header >> 6) & 3;
778
779 *out_channels = (channel_mode == 3) ? 1 : 2;
780 }
781
782 return true;
783 }
784
785 } // namespace android
786
787