void read_scaling_list(ABitReader *br, int32_t is8x8) { int32_t j, scanj, loop = (is8x8) ? (64) : (16); int32_t delta_scale, lastScale, nextScale; uint8_t *tab = (is8x8) ? (&scan2raster8[0]) : (&scan2raster[0]); lastScale = 8; nextScale = 8; for (j = 0; j < loop; j++) { scanj = tab[j]; if (nextScale != 0) { delta_scale = parseUE(br); nextScale = (lastScale + delta_scale + 256) & 0xFF; } lastScale = (nextScale == 0) ? lastScale : nextScale; } }
// Determine video dimensions from the sequence parameterset. void FindAVCDimensions( const sp<ABuffer> &seqParamSet, int32_t *width, int32_t *height) { ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1); unsigned profile_idc = br.getBits(8); br.skipBits(16); parseUE(&br); // seq_parameter_set_id unsigned chroma_format_idc = 1; // 4:2:0 chroma format if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) { chroma_format_idc = parseUE(&br); if (chroma_format_idc == 3) { br.skipBits(1); // residual_colour_transform_flag } parseUE(&br); // bit_depth_luma_minus8 parseUE(&br); // bit_depth_chroma_minus8 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag CHECK_EQ(br.getBits(1), 0u); // seq_scaling_matrix_present_flag } parseUE(&br); // log2_max_frame_num_minus4 unsigned pic_order_cnt_type = parseUE(&br); if (pic_order_cnt_type == 0) { parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4 } else if (pic_order_cnt_type == 1) { // offset_for_non_ref_pic, offset_for_top_to_bottom_field and // offset_for_ref_frame are technically se(v), but since we are // just skipping over them the midpoint does not matter. br.getBits(1); // delta_pic_order_always_zero_flag parseUE(&br); // offset_for_non_ref_pic parseUE(&br); // offset_for_top_to_bottom_field unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br); for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) { parseUE(&br); // offset_for_ref_frame } } parseUE(&br); // num_ref_frames br.getBits(1); // gaps_in_frame_num_value_allowed_flag unsigned pic_width_in_mbs_minus1 = parseUE(&br); unsigned pic_height_in_map_units_minus1 = parseUE(&br); unsigned frame_mbs_only_flag = br.getBits(1); *width = pic_width_in_mbs_minus1 * 16 + 16; *height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 * 16 + 16); if (!frame_mbs_only_flag) { br.getBits(1); // mb_adaptive_frame_field_flag } br.getBits(1); // direct_8x8_inference_flag if (br.getBits(1)) { // frame_cropping_flag unsigned frame_crop_left_offset = parseUE(&br); unsigned frame_crop_right_offset = parseUE(&br); unsigned frame_crop_top_offset = parseUE(&br); unsigned frame_crop_bottom_offset = parseUE(&br); unsigned cropUnitX, cropUnitY; if (chroma_format_idc == 0 /* monochrome */) { cropUnitX = 1; cropUnitY = 2 - frame_mbs_only_flag; } else { unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2; unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1; cropUnitX = subWidthC; cropUnitY = subHeightC * (2 - frame_mbs_only_flag); } ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u", frame_crop_left_offset, frame_crop_right_offset, frame_crop_top_offset, frame_crop_bottom_offset, cropUnitX, cropUnitY); *width -= (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX; *height -= (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY; } }
// Determine video dimensions from the sequence parameterset. void FindAVCDimensions( const sp<ABuffer> &seqParamSet, int32_t *width, int32_t *height, int32_t *sarWidth, int32_t *sarHeight) { ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1); unsigned profile_idc = br.getBits(8); br.skipBits(16); parseUE(&br); // seq_parameter_set_id unsigned chroma_format_idc = 1; // 4:2:0 chroma format if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) { chroma_format_idc = parseUE(&br); if (chroma_format_idc == 3) { br.skipBits(1); // residual_colour_transform_flag } parseUE(&br); // bit_depth_luma_minus8 parseUE(&br); // bit_depth_chroma_minus8 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag if (br.getBits(1)) { // seq_scaling_matrix_present_flag for (size_t i = 0; i < 8; ++i) { if (br.getBits(1)) { // seq_scaling_list_present_flag[i] // WARNING: the code below has not ever been exercised... // need a real-world example. if (i < 6) { // ScalingList4x4[i],16,... skipScalingList(&br, 16); } else { // ScalingList8x8[i-6],64,... skipScalingList(&br, 64); } } } } } parseUE(&br); // log2_max_frame_num_minus4 unsigned pic_order_cnt_type = parseUE(&br); if (pic_order_cnt_type == 0) { parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4 } else if (pic_order_cnt_type == 1) { // offset_for_non_ref_pic, offset_for_top_to_bottom_field and // offset_for_ref_frame are technically se(v), but since we are // just skipping over them the midpoint does not matter. br.getBits(1); // delta_pic_order_always_zero_flag parseUE(&br); // offset_for_non_ref_pic parseUE(&br); // offset_for_top_to_bottom_field unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br); for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) { parseUE(&br); // offset_for_ref_frame } } parseUE(&br); // num_ref_frames br.getBits(1); // gaps_in_frame_num_value_allowed_flag unsigned pic_width_in_mbs_minus1 = parseUE(&br); unsigned pic_height_in_map_units_minus1 = parseUE(&br); unsigned frame_mbs_only_flag = br.getBits(1); *width = pic_width_in_mbs_minus1 * 16 + 16; *height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 * 16 + 16); if (!frame_mbs_only_flag) { br.getBits(1); // mb_adaptive_frame_field_flag } br.getBits(1); // direct_8x8_inference_flag if (br.getBits(1)) { // frame_cropping_flag unsigned frame_crop_left_offset = parseUE(&br); unsigned frame_crop_right_offset = parseUE(&br); unsigned frame_crop_top_offset = parseUE(&br); unsigned frame_crop_bottom_offset = parseUE(&br); unsigned cropUnitX, cropUnitY; if (chroma_format_idc == 0 /* monochrome */) { cropUnitX = 1; cropUnitY = 2 - frame_mbs_only_flag; } else { unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2; unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1; cropUnitX = subWidthC; cropUnitY = subHeightC * (2 - frame_mbs_only_flag); } ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u", frame_crop_left_offset, frame_crop_right_offset, frame_crop_top_offset, frame_crop_bottom_offset, cropUnitX, cropUnitY); // *width -= (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX; if(__builtin_add_overflow(frame_crop_left_offset, frame_crop_right_offset, &frame_crop_left_offset) || __builtin_mul_overflow(frame_crop_left_offset, cropUnitX, &frame_crop_left_offset) || __builtin_sub_overflow(*width, frame_crop_left_offset, width) || *width < 0) { *width = 0; } //*height -= (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY; if(__builtin_add_overflow(frame_crop_top_offset, frame_crop_bottom_offset, &frame_crop_top_offset) || __builtin_mul_overflow(frame_crop_top_offset, cropUnitY, &frame_crop_top_offset) || __builtin_sub_overflow(*height, frame_crop_top_offset, height) || *height < 0) { *height = 0; } } if (sarWidth != NULL) { *sarWidth = 0; } if (sarHeight != NULL) { *sarHeight = 0; } if (br.getBits(1)) { // vui_parameters_present_flag unsigned sar_width = 0, sar_height = 0; if (br.getBits(1)) { // aspect_ratio_info_present_flag unsigned aspect_ratio_idc = br.getBits(8); if (aspect_ratio_idc == 255 /* extendedSAR */) { sar_width = br.getBits(16); sar_height = br.getBits(16); } else { static const struct { unsigned width, height; } kFixedSARs[] = { { 0, 0 }, // Invalid { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 }, }; if (aspect_ratio_idc > 0 && aspect_ratio_idc < NELEM(kFixedSARs)) { sar_width = kFixedSARs[aspect_ratio_idc].width; sar_height = kFixedSARs[aspect_ratio_idc].height; } } } ALOGV("sample aspect ratio = %u : %u", sar_width, sar_height); if (sarWidth != NULL) { *sarWidth = sar_width; } if (sarHeight != NULL) { *sarHeight = sar_height; } } }
// Determine video dimensions from the sequence parameterset. void FindAVCDimensions( const sp<ABuffer> &seqParamSet, int32_t *width, int32_t *height, int32_t *sarWidth, int32_t *sarHeight, int32_t *isInterlaced) { ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1); unsigned profile_idc = br.getBits(8); br.skipBits(16); parseUE(&br); // seq_parameter_set_id unsigned chroma_format_idc = 1; // 4:2:0 chroma format if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) { chroma_format_idc = parseUE(&br); if (chroma_format_idc == 3) { br.skipBits(1); // residual_colour_transform_flag } parseUE(&br); // bit_depth_luma_minus8 parseUE(&br); // bit_depth_chroma_minus8 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag bool seq_scaling_matrix_present = (br.getBits(1) != 0u); if (isInterlaced != NULL && seq_scaling_matrix_present) { return; } CHECK_EQ(seq_scaling_matrix_present, false); // seq_scaling_matrix_present_flag } parseUE(&br); // log2_max_frame_num_minus4 unsigned pic_order_cnt_type = parseUE(&br); if (pic_order_cnt_type == 0) { parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4 } else if (pic_order_cnt_type == 1) { // offset_for_non_ref_pic, offset_for_top_to_bottom_field and // offset_for_ref_frame are technically se(v), but since we are // just skipping over them the midpoint does not matter. br.getBits(1); // delta_pic_order_always_zero_flag parseUE(&br); // offset_for_non_ref_pic parseUE(&br); // offset_for_top_to_bottom_field unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br); for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) { parseUE(&br); // offset_for_ref_frame } } parseUE(&br); // num_ref_frames br.getBits(1); // gaps_in_frame_num_value_allowed_flag unsigned pic_width_in_mbs_minus1 = parseUE(&br); unsigned pic_height_in_map_units_minus1 = parseUE(&br); unsigned frame_mbs_only_flag = br.getBits(1); *width = pic_width_in_mbs_minus1 * 16 + 16; *height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 * 16 + 16); if (!frame_mbs_only_flag) { br.getBits(1); // mb_adaptive_frame_field_flag } br.getBits(1); // direct_8x8_inference_flag if (br.getBits(1)) { // frame_cropping_flag unsigned frame_crop_left_offset = parseUE(&br); unsigned frame_crop_right_offset = parseUE(&br); unsigned frame_crop_top_offset = parseUE(&br); unsigned frame_crop_bottom_offset = parseUE(&br); unsigned cropUnitX, cropUnitY; if (chroma_format_idc == 0 /* monochrome */) { cropUnitX = 1; cropUnitY = 2 - frame_mbs_only_flag; } else { unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2; unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1; cropUnitX = subWidthC; cropUnitY = subHeightC * (2 - frame_mbs_only_flag); } ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u", frame_crop_left_offset, frame_crop_right_offset, frame_crop_top_offset, frame_crop_bottom_offset, cropUnitX, cropUnitY); *width -= (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX; *height -= (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY; } if (isInterlaced != NULL) { *isInterlaced = !frame_mbs_only_flag; } if (sarWidth != NULL) { *sarWidth = 0; } if (sarHeight != NULL) { *sarHeight = 0; } if (br.getBits(1)) { // vui_parameters_present_flag unsigned sar_width = 0, sar_height = 0; if (br.getBits(1)) { // aspect_ratio_info_present_flag unsigned aspect_ratio_idc = br.getBits(8); if (aspect_ratio_idc == 255 /* extendedSAR */) { sar_width = br.getBits(16); sar_height = br.getBits(16); } else if (aspect_ratio_idc > 0 && aspect_ratio_idc < 14) { static const int32_t kFixedSARWidth[] = { 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160 }; static const int32_t kFixedSARHeight[] = { 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99 }; sar_width = kFixedSARWidth[aspect_ratio_idc - 1]; sar_height = kFixedSARHeight[aspect_ratio_idc - 1]; } } ALOGV("sample aspect ratio = %u : %u", sar_width, sar_height); if (sarWidth != NULL) { *sarWidth = sar_width; } if (sarHeight != NULL) { *sarHeight = sar_height; } } }
signed parseSE(ABitReader *br) { unsigned codeNum = parseUE(br); return (codeNum & 1) ? (codeNum + 1) / 2 : -signed(codeNum / 2); }