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