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 == 0) {
    226         return -EAGAIN;
    227     }
    228 
    229     // Skip any number of leading 0x00.
    230 
    231     size_t offset = 0;
    232     while (offset < size && data[offset] == 0x00) {
    233         ++offset;
    234     }
    235 
    236     if (offset == size) {
    237         return -EAGAIN;
    238     }
    239 
    240     // A valid startcode consists of at least two 0x00 bytes followed by 0x01.
    241 
    242     if (offset < 2 || data[offset] != 0x01) {
    243         return ERROR_MALFORMED;
    244     }
    245 
    246     ++offset;
    247 
    248     size_t startOffset = offset;
    249 
    250     for (;;) {
    251         while (offset < size && data[offset] != 0x01) {
    252             ++offset;
    253         }
    254 
    255         if (offset == size) {
    256             if (startCodeFollows) {
    257                 offset = size + 2;
    258                 break;
    259             }
    260 
    261             return -EAGAIN;
    262         }
    263 
    264         if (data[offset - 1] == 0x00 && data[offset - 2] == 0x00) {
    265             break;
    266         }
    267 
    268         ++offset;
    269     }
    270 
    271     size_t endOffset = offset - 2;
    272     while (endOffset > startOffset + 1 && data[endOffset - 1] == 0x00) {
    273         --endOffset;
    274     }
    275 
    276     *nalStart = &data[startOffset];
    277     *nalSize = endOffset - startOffset;
    278 
    279     if (offset + 2 < size) {
    280         *_data = &data[offset - 2];
    281         *_size = size - offset + 2;
    282     } else {
    283         *_data = NULL;
    284         *_size = 0;
    285     }
    286 
    287     return OK;
    288 }
    289 
    290 static sp<ABuffer> FindNAL(const uint8_t *data, size_t size, unsigned nalType) {
    291     const uint8_t *nalStart;
    292     size_t nalSize;
    293     while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
    294         if ((nalStart[0] & 0x1f) == nalType) {
    295             sp<ABuffer> buffer = new ABuffer(nalSize);
    296             memcpy(buffer->data(), nalStart, nalSize);
    297             return buffer;
    298         }
    299     }
    300 
    301     return NULL;
    302 }
    303 
    304 const char *AVCProfileToString(uint8_t profile) {
    305     switch (profile) {
    306         case kAVCProfileBaseline:
    307             return "Baseline";
    308         case kAVCProfileMain:
    309             return "Main";
    310         case kAVCProfileExtended:
    311             return "Extended";
    312         case kAVCProfileHigh:
    313             return "High";
    314         case kAVCProfileHigh10:
    315             return "High 10";
    316         case kAVCProfileHigh422:
    317             return "High 422";
    318         case kAVCProfileHigh444:
    319             return "High 444";
    320         case kAVCProfileCAVLC444Intra:
    321             return "CAVLC 444 Intra";
    322         default:   return "Unknown";
    323     }
    324 }
    325 
    326 sp<MetaData> MakeAVCCodecSpecificData(const sp<ABuffer> &accessUnit) {
    327     const uint8_t *data = accessUnit->data();
    328     size_t size = accessUnit->size();
    329 
    330     sp<ABuffer> seqParamSet = FindNAL(data, size, 7);
    331     if (seqParamSet == NULL) {
    332         return NULL;
    333     }
    334 
    335     int32_t width, height;
    336     int32_t sarWidth, sarHeight;
    337     FindAVCDimensions(
    338             seqParamSet, &width, &height, &sarWidth, &sarHeight);
    339 
    340     sp<ABuffer> picParamSet = FindNAL(data, size, 8);
    341     CHECK(picParamSet != NULL);
    342 
    343     size_t csdSize =
    344         1 + 3 + 1 + 1
    345         + 2 * 1 + seqParamSet->size()
    346         + 1 + 2 * 1 + picParamSet->size();
    347 
    348     sp<ABuffer> csd = new ABuffer(csdSize);
    349     uint8_t *out = csd->data();
    350 
    351     *out++ = 0x01;  // configurationVersion
    352     memcpy(out, seqParamSet->data() + 1, 3);  // profile/level...
    353 
    354     uint8_t profile = out[0];
    355     uint8_t level = out[2];
    356 
    357     out += 3;
    358     *out++ = (0x3f << 2) | 1;  // lengthSize == 2 bytes
    359     *out++ = 0xe0 | 1;
    360 
    361     *out++ = seqParamSet->size() >> 8;
    362     *out++ = seqParamSet->size() & 0xff;
    363     memcpy(out, seqParamSet->data(), seqParamSet->size());
    364     out += seqParamSet->size();
    365 
    366     *out++ = 1;
    367 
    368     *out++ = picParamSet->size() >> 8;
    369     *out++ = picParamSet->size() & 0xff;
    370     memcpy(out, picParamSet->data(), picParamSet->size());
    371 
    372 #if 0
    373     ALOGI("AVC seq param set");
    374     hexdump(seqParamSet->data(), seqParamSet->size());
    375 #endif
    376 
    377     sp<MetaData> meta = new MetaData;
    378     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
    379 
    380     meta->setData(kKeyAVCC, kTypeAVCC, csd->data(), csd->size());
    381     meta->setInt32(kKeyWidth, width);
    382     meta->setInt32(kKeyHeight, height);
    383 
    384     if (sarWidth > 1 || sarHeight > 1) {
    385         // We treat 0:0 (unspecified) as 1:1.
    386 
    387         meta->setInt32(kKeySARWidth, sarWidth);
    388         meta->setInt32(kKeySARHeight, sarHeight);
    389 
    390         ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d) "
    391               "SAR %d : %d",
    392              width,
    393              height,
    394              AVCProfileToString(profile),
    395              level / 10,
    396              level % 10,
    397              sarWidth,
    398              sarHeight);
    399     } else {
    400         ALOGI("found AVC codec config (%d x %d, %s-profile level %d.%d)",
    401              width,
    402              height,
    403              AVCProfileToString(profile),
    404              level / 10,
    405              level % 10);
    406     }
    407 
    408     return meta;
    409 }
    410 
    411 bool IsIDR(const sp<ABuffer> &buffer) {
    412     const uint8_t *data = buffer->data();
    413     size_t size = buffer->size();
    414 
    415     bool foundIDR = false;
    416 
    417     const uint8_t *nalStart;
    418     size_t nalSize;
    419     while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
    420         CHECK_GT(nalSize, 0u);
    421 
    422         unsigned nalType = nalStart[0] & 0x1f;
    423 
    424         if (nalType == 5) {
    425             foundIDR = true;
    426             break;
    427         }
    428     }
    429 
    430     return foundIDR;
    431 }
    432 
    433 bool IsAVCReferenceFrame(const sp<ABuffer> &accessUnit) {
    434     const uint8_t *data = accessUnit->data();
    435     size_t size = accessUnit->size();
    436 
    437     const uint8_t *nalStart;
    438     size_t nalSize;
    439     while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
    440         CHECK_GT(nalSize, 0u);
    441 
    442         unsigned nalType = nalStart[0] & 0x1f;
    443 
    444         if (nalType == 5) {
    445             return true;
    446         } else if (nalType == 1) {
    447             unsigned nal_ref_idc = (nalStart[0] >> 5) & 3;
    448             return nal_ref_idc != 0;
    449         }
    450     }
    451 
    452     return true;
    453 }
    454 
    455 sp<MetaData> MakeAACCodecSpecificData(
    456         unsigned profile, unsigned sampling_freq_index,
    457         unsigned channel_configuration) {
    458     sp<MetaData> meta = new MetaData;
    459     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
    460 
    461     CHECK_LE(sampling_freq_index, 11u);
    462     static const int32_t kSamplingFreq[] = {
    463         96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
    464         16000, 12000, 11025, 8000
    465     };
    466     meta->setInt32(kKeySampleRate, kSamplingFreq[sampling_freq_index]);
    467     meta->setInt32(kKeyChannelCount, channel_configuration);
    468 
    469     static const uint8_t kStaticESDS[] = {
    470         0x03, 22,
    471         0x00, 0x00,     // ES_ID
    472         0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
    473 
    474         0x04, 17,
    475         0x40,                       // Audio ISO/IEC 14496-3
    476         0x00, 0x00, 0x00, 0x00,
    477         0x00, 0x00, 0x00, 0x00,
    478         0x00, 0x00, 0x00, 0x00,
    479 
    480         0x05, 2,
    481         // AudioSpecificInfo follows
    482 
    483         // oooo offf fccc c000
    484         // o - audioObjectType
    485         // f - samplingFreqIndex
    486         // c - channelConfig
    487     };
    488     sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
    489     memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
    490 
    491     csd->data()[sizeof(kStaticESDS)] =
    492         ((profile + 1) << 3) | (sampling_freq_index >> 1);
    493 
    494     csd->data()[sizeof(kStaticESDS) + 1] =
    495         ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3);
    496 
    497     meta->setData(kKeyESDS, 0, csd->data(), csd->size());
    498 
    499     return meta;
    500 }
    501 
    502 bool ExtractDimensionsFromVOLHeader(
    503         const uint8_t *data, size_t size, int32_t *width, int32_t *height) {
    504     ABitReader br(&data[4], size - 4);
    505     br.skipBits(1);  // random_accessible_vol
    506     unsigned video_object_type_indication = br.getBits(8);
    507 
    508     CHECK_NE(video_object_type_indication,
    509              0x21u /* Fine Granularity Scalable */);
    510 
    511     unsigned video_object_layer_verid;
    512     unsigned video_object_layer_priority;
    513     if (br.getBits(1)) {
    514         video_object_layer_verid = br.getBits(4);
    515         video_object_layer_priority = br.getBits(3);
    516     }
    517     unsigned aspect_ratio_info = br.getBits(4);
    518     if (aspect_ratio_info == 0x0f /* extended PAR */) {
    519         br.skipBits(8);  // par_width
    520         br.skipBits(8);  // par_height
    521     }
    522     if (br.getBits(1)) {  // vol_control_parameters
    523         br.skipBits(2);  // chroma_format
    524         br.skipBits(1);  // low_delay
    525         if (br.getBits(1)) {  // vbv_parameters
    526             br.skipBits(15);  // first_half_bit_rate
    527             CHECK(br.getBits(1));  // marker_bit
    528             br.skipBits(15);  // latter_half_bit_rate
    529             CHECK(br.getBits(1));  // marker_bit
    530             br.skipBits(15);  // first_half_vbv_buffer_size
    531             CHECK(br.getBits(1));  // marker_bit
    532             br.skipBits(3);  // latter_half_vbv_buffer_size
    533             br.skipBits(11);  // first_half_vbv_occupancy
    534             CHECK(br.getBits(1));  // marker_bit
    535             br.skipBits(15);  // latter_half_vbv_occupancy
    536             CHECK(br.getBits(1));  // marker_bit
    537         }
    538     }
    539     unsigned video_object_layer_shape = br.getBits(2);
    540     CHECK_EQ(video_object_layer_shape, 0x00u /* rectangular */);
    541 
    542     CHECK(br.getBits(1));  // marker_bit
    543     unsigned vop_time_increment_resolution = br.getBits(16);
    544     CHECK(br.getBits(1));  // marker_bit
    545 
    546     if (br.getBits(1)) {  // fixed_vop_rate
    547         // range [0..vop_time_increment_resolution)
    548 
    549         // vop_time_increment_resolution
    550         // 2 => 0..1, 1 bit
    551         // 3 => 0..2, 2 bits
    552         // 4 => 0..3, 2 bits
    553         // 5 => 0..4, 3 bits
    554         // ...
    555 
    556         CHECK_GT(vop_time_increment_resolution, 0u);
    557         --vop_time_increment_resolution;
    558 
    559         unsigned numBits = 0;
    560         while (vop_time_increment_resolution > 0) {
    561             ++numBits;
    562             vop_time_increment_resolution >>= 1;
    563         }
    564 
    565         br.skipBits(numBits);  // fixed_vop_time_increment
    566     }
    567 
    568     CHECK(br.getBits(1));  // marker_bit
    569     unsigned video_object_layer_width = br.getBits(13);
    570     CHECK(br.getBits(1));  // marker_bit
    571     unsigned video_object_layer_height = br.getBits(13);
    572     CHECK(br.getBits(1));  // marker_bit
    573 
    574     unsigned interlaced = br.getBits(1);
    575 
    576     *width = video_object_layer_width;
    577     *height = video_object_layer_height;
    578 
    579     return true;
    580 }
    581 
    582 bool GetMPEGAudioFrameSize(
    583         uint32_t header, size_t *frame_size,
    584         int *out_sampling_rate, int *out_channels,
    585         int *out_bitrate, int *out_num_samples) {
    586     *frame_size = 0;
    587 
    588     if (out_sampling_rate) {
    589         *out_sampling_rate = 0;
    590     }
    591 
    592     if (out_channels) {
    593         *out_channels = 0;
    594     }
    595 
    596     if (out_bitrate) {
    597         *out_bitrate = 0;
    598     }
    599 
    600     if (out_num_samples) {
    601         *out_num_samples = 1152;
    602     }
    603 
    604     if ((header & 0xffe00000) != 0xffe00000) {
    605         return false;
    606     }
    607 
    608     unsigned version = (header >> 19) & 3;
    609 
    610     if (version == 0x01) {
    611         return false;
    612     }
    613 
    614     unsigned layer = (header >> 17) & 3;
    615 
    616     if (layer == 0x00) {
    617         return false;
    618     }
    619 
    620     unsigned protection = (header >> 16) & 1;
    621 
    622     unsigned bitrate_index = (header >> 12) & 0x0f;
    623 
    624     if (bitrate_index == 0 || bitrate_index == 0x0f) {
    625         // Disallow "free" bitrate.
    626         return false;
    627     }
    628 
    629     unsigned sampling_rate_index = (header >> 10) & 3;
    630 
    631     if (sampling_rate_index == 3) {
    632         return false;
    633     }
    634 
    635     static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
    636     int sampling_rate = kSamplingRateV1[sampling_rate_index];
    637     if (version == 2 /* V2 */) {
    638         sampling_rate /= 2;
    639     } else if (version == 0 /* V2.5 */) {
    640         sampling_rate /= 4;
    641     }
    642 
    643     unsigned padding = (header >> 9) & 1;
    644 
    645     if (layer == 3) {
    646         // layer I
    647 
    648         static const int kBitrateV1[] = {
    649             32, 64, 96, 128, 160, 192, 224, 256,
    650             288, 320, 352, 384, 416, 448
    651         };
    652 
    653         static const int kBitrateV2[] = {
    654             32, 48, 56, 64, 80, 96, 112, 128,
    655             144, 160, 176, 192, 224, 256
    656         };
    657 
    658         int bitrate =
    659             (version == 3 /* V1 */)
    660                 ? kBitrateV1[bitrate_index - 1]
    661                 : kBitrateV2[bitrate_index - 1];
    662 
    663         if (out_bitrate) {
    664             *out_bitrate = bitrate;
    665         }
    666 
    667         *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
    668 
    669         if (out_num_samples) {
    670             *out_num_samples = 384;
    671         }
    672     } else {
    673         // layer II or III
    674 
    675         static const int kBitrateV1L2[] = {
    676             32, 48, 56, 64, 80, 96, 112, 128,
    677             160, 192, 224, 256, 320, 384
    678         };
    679 
    680         static const int kBitrateV1L3[] = {
    681             32, 40, 48, 56, 64, 80, 96, 112,
    682             128, 160, 192, 224, 256, 320
    683         };
    684 
    685         static const int kBitrateV2[] = {
    686             8, 16, 24, 32, 40, 48, 56, 64,
    687             80, 96, 112, 128, 144, 160
    688         };
    689 
    690         int bitrate;
    691         if (version == 3 /* V1 */) {
    692             bitrate = (layer == 2 /* L2 */)
    693                 ? kBitrateV1L2[bitrate_index - 1]
    694                 : kBitrateV1L3[bitrate_index - 1];
    695 
    696             if (out_num_samples) {
    697                 *out_num_samples = 1152;
    698             }
    699         } else {
    700             // V2 (or 2.5)
    701 
    702             bitrate = kBitrateV2[bitrate_index - 1];
    703             if (out_num_samples) {
    704                 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
    705             }
    706         }
    707 
    708         if (out_bitrate) {
    709             *out_bitrate = bitrate;
    710         }
    711 
    712         if (version == 3 /* V1 */) {
    713             *frame_size = 144000 * bitrate / sampling_rate + padding;
    714         } else {
    715             // V2 or V2.5
    716             size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
    717             *frame_size = tmp * bitrate / sampling_rate + padding;
    718         }
    719     }
    720 
    721     if (out_sampling_rate) {
    722         *out_sampling_rate = sampling_rate;
    723     }
    724 
    725     if (out_channels) {
    726         int channel_mode = (header >> 6) & 3;
    727 
    728         *out_channels = (channel_mode == 3) ? 1 : 2;
    729     }
    730 
    731     return true;
    732 }
    733 
    734 }  // namespace android
    735 
    736