📄 main.cpp
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is MPEG4IP. * * The Initial Developer of the Original Code is Cisco Systems Inc. * Portions created by Cisco Systems Inc. are * Copyright (C) Cisco Systems Inc. 2005. All Rights Reserved. * * Contributor(s): * Bill May wmay@cisco.com */#include "mpeg4ip.h"#include "mpeg4ip_getopt.h"#include "mpeg4ip_bitstream.h"#include <math.h>#include "mp4av_h264.h"#define H264_START_CODE 0x000001#define H264_PREVENT_3_BYTE 0x000003static uint8_t exp_golomb_bits[256] = {8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };static const uint8_t trailing_bits[9] = { 0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };uint32_t h264_ue (CBitstream *bs){ uint32_t bits, read; int bits_left; uint8_t coded; bool done = false; bits = 0; // we want to read 8 bits at a time - if we don't have 8 bits, // read what's left, and shift. The exp_golomb_bits calc remains the // same. while (done == false) { bits_left = bs->bits_remain(); if (bits_left < 8) { read = bs->PeekBits(bits_left) << (8 - bits_left); done = true; } else { read = bs->PeekBits(8); if (read == 0) { bs->GetBits(8); bits += 8; } else { done = true; } } } coded = exp_golomb_bits[read]; bs->GetBits(coded); bits += coded; // printf("ue - bits %d\n", bits); return bs->GetBits(bits + 1) - 1;}int32_t h264_se (CBitstream *bs) { uint32_t ret; ret = h264_ue(bs); if ((ret & 0x1) == 0) { ret >>= 1; int32_t temp = 0 - ret; return temp; } return (ret + 1) >> 1;}void h264_check_0s (CBitstream *bs, int count){ uint32_t val; val = bs->GetBits(count); if (val != 0) { printf("field error - %d bits should be 0 is %x\n", count, val); }}void h264_hrd_parameters (h264_decode_t *dec, CBitstream *bs){ uint32_t cpb_cnt; dec->cpb_cnt_minus1 = cpb_cnt = h264_ue(bs); uint32_t temp; printf(" cpb_cnt_minus1: %u\n", cpb_cnt); printf(" bit_rate_scale: %u\n", bs->GetBits(4)); printf(" cpb_size_scale: %u\n", bs->GetBits(4)); for (uint32_t ix = 0; ix <= cpb_cnt; ix++) { printf(" bit_rate_value_minus1[%u]: %u\n", ix, h264_ue(bs)); printf(" cpb_size_value_minus1[%u]: %u\n", ix, h264_ue(bs)); printf(" cbr_flag[%u]: %u\n", ix, bs->GetBits(1)); } temp = dec->initial_cpb_removal_delay_length_minus1 = bs->GetBits(5); printf(" initial_cpb_removal_delay_length_minus1: %u\n", temp); dec->cpb_removal_delay_length_minus1 = temp = bs->GetBits(5); printf(" cpb_removal_delay_length_minus1: %u\n", temp); dec->dpb_output_delay_length_minus1 = temp = bs->GetBits(5); printf(" dpb_output_delay_length_minus1: %u\n", temp); dec->time_offset_length = temp = bs->GetBits(5); printf(" time_offset_length: %u\n", temp);}void h264_vui_parameters (h264_decode_t *dec, CBitstream *bs){ uint32_t temp, temp2; temp = bs->GetBits(1); printf(" aspect_ratio_info_present_flag: %u\n", temp); if (temp) { temp = bs->GetBits(8); printf(" aspect_ratio_idc:%u\n", temp); if (temp == 0xff) { // extended_SAR printf(" sar_width: %u\n", bs->GetBits(16)); printf(" sar_height: %u\n", bs->GetBits(16)); } } temp = bs->GetBits(1); printf(" overscan_info_present_flag: %u\n", temp); if (temp) { printf(" overscan_appropriate_flag: %u\n", bs->GetBits(1)); } temp = bs->GetBits(1); printf(" video_signal_info_present_flag: %u\n", temp); if (temp) { printf(" video_format: %u\n", bs->GetBits(3)); printf(" video_full_range_flag: %u\n", bs->GetBits(1)); temp = bs->GetBits(1); printf(" colour_description_present_flag: %u\n", temp); if (temp) { printf(" colour_primaries: %u\n", bs->GetBits(8)); printf(" transfer_characteristics: %u\n", bs->GetBits(8)); printf(" matrix_coefficients: %u\n", bs->GetBits(8)); } } temp = bs->GetBits(1); printf(" chroma_loc_info_present_flag: %u\n", temp); if (temp) { printf(" chroma_sample_loc_type_top_field: %u\n", h264_ue(bs)); printf(" chroma_sample_loc_type_bottom_field: %u\n", h264_ue(bs)); } temp = bs->GetBits(1); printf(" timing_info_present_flag: %u\n", temp); if (temp) { printf(" num_units_in_tick: %u\n", bs->GetBits(32)); printf(" time_scale: %u\n", bs->GetBits(32)); printf(" fixed_frame_scale: %u\n", bs->GetBits(1)); } temp = bs->GetBits(1); printf(" nal_hrd_parameters_present_flag: %u\n", temp); if (temp) { dec->NalHrdBpPresentFlag = 1; dec->CpbDpbDelaysPresentFlag = 1; h264_hrd_parameters(dec, bs); } temp2 = bs->GetBits(1); printf(" vcl_hrd_parameters_present_flag: %u\n", temp2); if (temp2) { dec->VclHrdBpPresentFlag = 1; dec->CpbDpbDelaysPresentFlag = 1; h264_hrd_parameters(dec, bs); } if (temp || temp2) { printf(" low_delay_hrd_flag: %u\n", bs->GetBits(1)); } dec->pic_struct_present_flag = temp = bs->GetBits(1); printf(" pic_struct_present_flag: %u\n", temp); temp = bs->GetBits(1); if (temp) { printf(" motion_vectors_over_pic_boundaries_flag: %u\n", bs->GetBits(1)); printf(" max_bytes_per_pic_denom: %u\n", h264_ue(bs)); printf(" max_bits_per_mb_denom: %u\n", h264_ue(bs)); printf(" log2_max_mv_length_horizontal: %u\n", h264_ue(bs)); printf(" log2_max_mv_length_vertical: %u\n", h264_ue(bs)); printf(" num_reorder_frames: %u\n", h264_ue(bs)); printf(" max_dec_frame_buffering: %u\n", h264_ue(bs)); }} static uint32_t calc_ceil_log2 (uint32_t val){ uint32_t ix, cval; ix = 0; cval = 1; while (ix < 32) { if (cval >= val) return ix; cval <<= 1; ix++; } return ix;}static void scaling_list (uint ix, uint sizeOfScalingList, CBitstream *bs){ uint lastScale = 8, nextScale = 8; uint jx; int deltaScale; for (jx = 0; jx < sizeOfScalingList; jx++) { if (nextScale != 0) { deltaScale = h264_se(bs); nextScale = (lastScale + deltaScale + 256) % 256; printf(" delta: %d\n", deltaScale); } if (nextScale == 0) { lastScale = lastScale; } else { lastScale = nextScale; } printf(" scaling list[%u][%u]: %u\n", ix, jx, lastScale); }}void h264_parse_sequence_parameter_set (h264_decode_t *dec, CBitstream *bs){ uint32_t temp; dec->profile = bs->GetBits(8); printf(" profile: %u\n", dec->profile); printf(" constaint_set0_flag: %d\n", bs->GetBits(1)); printf(" constaint_set1_flag: %d\n", bs->GetBits(1)); printf(" constaint_set2_flag: %d\n", bs->GetBits(1)); printf(" constaint_set3_flag: %d\n", bs->GetBits(1)); h264_check_0s(bs, 4); printf(" level_idc: %u\n", bs->GetBits(8)); printf(" seq parameter set id: %u\n", h264_ue(bs)); if (dec->profile == 100 || dec->profile == 110 || dec->profile == 122 || dec->profile == 144) { dec->chroma_format_idc = h264_ue(bs); printf(" chroma format idx: %u\n", dec->chroma_format_idc); if (dec->chroma_format_idc == 3) { dec->residual_colour_transform_flag = bs->GetBits(1); printf(" resigual colour transform flag: %u\n", dec->residual_colour_transform_flag); } dec->bit_depth_luma_minus8 = h264_ue(bs); printf(" bit depth luma minus8: %u\n", dec->bit_depth_luma_minus8); dec->bit_depth_chroma_minus8 = h264_ue(bs); printf(" bit depth chroma minus8: %u\n", dec->bit_depth_luma_minus8); dec->qpprime_y_zero_transform_bypass_flag = bs->GetBits(1); printf(" Qpprime Y Zero Transform Bypass flag: %u\n", dec->qpprime_y_zero_transform_bypass_flag); dec->seq_scaling_matrix_present_flag = bs->GetBits(1); printf(" Seq Scaling Matrix Present Flag: %u\n", dec->seq_scaling_matrix_present_flag); if (dec->seq_scaling_matrix_present_flag) { for (uint ix = 0; ix < 8; ix++) { temp = bs->GetBits(1); printf(" Seq Scaling List[%u] Present Flag: %u\n", ix, temp); if (temp) { scaling_list(ix, ix < 6 ? 16 : 64, bs); } } } } dec->log2_max_frame_num_minus4 = h264_ue(bs); printf(" log2_max_frame_num_minus4: %u\n", dec->log2_max_frame_num_minus4); dec->pic_order_cnt_type = h264_ue(bs); printf(" pic_order_cnt_type: %u\n", dec->pic_order_cnt_type); if (dec->pic_order_cnt_type == 0) { dec->log2_max_pic_order_cnt_lsb_minus4 = h264_ue(bs); printf(" log2_max_pic_order_cnt_lsb_minus4: %u\n", dec->log2_max_pic_order_cnt_lsb_minus4); } else if (dec->pic_order_cnt_type == 1) { dec->delta_pic_order_always_zero_flag = bs->GetBits(1); printf(" delta_pic_order_always_zero_flag: %u\n", dec->delta_pic_order_always_zero_flag); printf(" offset_for_non_ref_pic: %d\n", h264_se(bs)); printf(" offset_for_top_to_bottom_field: %d\n", h264_se(bs)); temp = h264_ue(bs); for (uint32_t ix = 0; ix < temp; ix++) { printf(" offset_for_ref_frame[%u]: %d\n", ix, h264_se(bs)); } } printf(" num_ref_frames: %u\n", h264_ue(bs)); printf(" gaps_in_frame_num_value_allowed_flag: %u\n", bs->GetBits(1)); uint32_t PicWidthInMbs = h264_ue(bs) + 1; printf(" pic_width_in_mbs_minus1: %u (%u)\n", PicWidthInMbs - 1, PicWidthInMbs * 16); uint32_t PicHeightInMapUnits = h264_ue(bs) + 1; printf(" pic_height_in_map_minus1: %u\n", PicHeightInMapUnits - 1); dec->frame_mbs_only_flag = bs->GetBits(1); printf(" frame_mbs_only_flag: %u\n", dec->frame_mbs_only_flag); printf(" derived height: %u\n", (2 - dec->frame_mbs_only_flag) * PicHeightInMapUnits * 16); if (!dec->frame_mbs_only_flag) { printf(" mb_adaptive_frame_field_flag: %u\n", bs->GetBits(1)); } printf(" direct_8x8_inference_flag: %u\n", bs->GetBits(1)); temp = bs->GetBits(1); printf(" frame_cropping_flag: %u\n", temp); if (temp) { printf(" frame_crop_left_offset: %u\n", h264_ue(bs)); printf(" frame_crop_right_offset: %u\n", h264_ue(bs)); printf(" frame_crop_top_offset: %u\n", h264_ue(bs)); printf(" frame_crop_bottom_offset: %u\n", h264_ue(bs)); } temp = bs->GetBits(1); printf(" vui_parameters_present_flag: %u\n", temp); if (temp) { h264_vui_parameters(dec, bs); }}static void h264_parse_seq_ext (h264_decode_t *dec, CBitstream *bs){ uint32_t temp; printf(" seq_parameter_set_id: %u\n", h264_ue(bs)); temp = h264_ue(bs); printf(" aux format idc: %u\n", temp); if (temp != 0) { temp = h264_ue(bs); printf(" bit depth aux minus8:%u\n", temp); printf(" alpha incr flag:%u\n", bs->GetBits(1)); printf(" alpha opaque value: %u\n", bs->GetBits(temp + 9)); printf(" alpha transparent value: %u\n", bs->GetBits(temp + 9));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -