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