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