从H264的SPS中获取图像长宽_第1页
从H264的SPS中获取图像长宽_第2页
从H264的SPS中获取图像长宽_第3页
从H264的SPS中获取图像长宽_第4页
从H264的SPS中获取图像长宽_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、从H264的SPS中获取图像长宽作者:李国帅从开源项目mpegip中选取,并封装。1, 调用方法#include "H264Parser.h"h264_decode_t dec;memset(&dec, 0, sizeof(dec);uint8_t * buffer = (uint8_t*)cbSPS;h264_parse_sequence_parameter_set(&dec, buffer,nSPS * 8);outWidth = dec.pic_width;/1920;/outHeight = dec.pic_height;/1080;/2, H264

2、Parser.h代码#pragma once#ifndef _H264_H_#define _H264_H_ 1#include <stdio.h>#include <math.h>#include "stdint.h"#include "bitstream.h" /在mpegip中得到,就不粘贴了。typedef unsigned int uint;#define H264_START_CODE 0x000001#define H264_PREVENT_3_BYTE 0x000003#pragma pack(push)#prag

3、ma pack(1)typedef struct h264_decode_tuint8_t profile;uint8_t level;uint32_t chroma_format_idc;uint8_t residual_colour_transform_flag;uint32_t bit_depth_luma_minus8;uint32_t bit_depth_chroma_minus8;uint8_t qpprime_y_zero_transform_bypass_flag;uint8_t seq_scaling_matrix_present_flag;uint32_t log2_max

4、_frame_num_minus4;uint32_t log2_max_pic_order_cnt_lsb_minus4;uint32_t pic_order_cnt_type;uint8_t frame_mbs_only_flag;uint8_t pic_order_present_flag;uint8_t delta_pic_order_always_zero_flag;int32_t offset_for_non_ref_pic;int32_t offset_for_top_to_bottom_field;uint32_t pic_order_cnt_cycle_length;int16

5、_t offset_for_ref_frame256;uint8_t nal_ref_idc;uint8_t nal_unit_type;uint8_t field_pic_flag;uint8_t bottom_field_flag;uint32_t frame_num;uint32_t idr_pic_id;uint32_t pic_order_cnt_lsb;int32_t delta_pic_order_cnt_bottom;int32_t delta_pic_order_cnt2;uint32_t pic_width, pic_height;uint32_t slice_type;/

6、* POC state */int32_t pic_order_cnt; /* can be < 0 */uint32_t pic_order_cnt_msb;uint32_t pic_order_cnt_msb_prev;uint32_t pic_order_cnt_lsb_prev;uint32_t frame_num_prev;int32_t frame_num_offset;int32_t frame_num_offset_prev;uint8_t NalHrdBpPresentFlag;uint8_t VclHrdBpPresentFlag;uint8_t CpbDpbDela

7、ysPresentFlag;uint8_t pic_struct_present_flag;uint8_t cpb_removal_delay_length_minus1;uint8_t dpb_output_delay_length_minus1;uint8_t time_offset_length;uint32_t cpb_cnt_minus1;uint8_t initial_cpb_removal_delay_length_minus1; h264_decode_t;static uint8_t exp_golomb_bits =8, 7, 6, 6, 5, 5, 5, 5, 4, 4,

8、 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,

9、 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,

10、 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;/256static const uint8_t trailing_bits = 0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 ;/9#ifdef _cplusplusextern "C" #endifuint32_t h264_ue(CBitstream *bs)uint32_t bi

11、ts, 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->PeekBit

12、s(bits_left) <<(8 - bits_left);done = true;elseread = bs->PeekBits(8);if(read = 0)bs->GetBits(8);bits += 8;elsedone = true;coded = exp_golomb_bitsread;bs->GetBits(coded);bits += coded;/ printf("ue - bits %dn", bits);return bs->GetBits(bits + 1) - 1;int32_t h264_se(CBitstr

13、eam *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 %xn", c

14、ount, val);void scaling_list(uint32_t ix, uint32_t sizeOfScalingList, CBitstream *bs)uint32_t lastScale = 8, nextScale = 8;uint32_t jx;int deltaScale;for(jx = 0; jx < sizeOfScalingList; jx+)if(nextScale != 0)deltaScale = h264_se(bs);nextScale =(lastScale + deltaScale + 256) % 256;/printf(" d

15、elta: %dn", deltaScale);if(nextScale = 0)lastScale = lastScale;elselastScale = nextScale;/printf(" scaling list%u%u: %un", ix, jx, lastScale);void h264_parse_sequence_parameter_set(h264_decode_t *dec, uint8_t* Buffer,size_t Buffer_Size)CBitstream ourbs;ourbs.init(Buffer, Buffer_Size);

16、CBitstream *bs = &ourbs;uint32_t type = 0;h264_check_0s(bs, 1);/检查首字节dec->nal_ref_idc = bs->GetBits(2);/NAL 的优先级dec->nal_unit_type = type = bs->GetBits(5);/nal类型if(type != 0x7)return;uint32_t temp;dec->profile = bs->GetBits(8);/第个字节/printf(" profile: %un", dec->pro

17、file);temp = bs->GetBits(1);/printf(" constaint_set0_flag: %dn", temp);temp = bs->GetBits(1);/printf(" constaint_set1_flag: %dn", temp);temp = bs->GetBits(1);/printf(" constaint_set2_flag: %dn", temp);temp = bs->GetBits(1);/printf(" constaint_set3_flag:

18、 %dn", temp);h264_check_0s(bs, 4);/第个字节temp = bs->GetBits(8);/第个字节/printf(" level_idc: %un", temp);temp = h264_ue(bs);/printf(" seq parameter set id: %un", temp);if(dec->profile = 100 | dec->profile = 110 | dec->profile = 122 | dec->profile = 144)dec->chrom

19、a_format_idc = h264_ue(bs);if(dec->chroma_format_idc = 3)dec->residual_colour_transform_flag = bs->GetBits(1);dec->bit_depth_luma_minus8 = h264_ue(bs);dec->bit_depth_chroma_minus8 = h264_ue(bs);dec->qpprime_y_zero_transform_bypass_flag = bs->GetBits(1);dec->seq_scaling_matrix

20、_present_flag = bs->GetBits(1);if(dec->seq_scaling_matrix_present_flag)for(uint32_t ix = 0; ix < 8; ix+)temp = bs->GetBits(1);if(temp)scaling_list(ix, ix < 6 ? 16 : 64, bs);dec->log2_max_frame_num_minus4 = h264_ue(bs);dec->pic_order_cnt_type = h264_ue(bs);if(dec->pic_order_cn

21、t_type = 0)dec->log2_max_pic_order_cnt_lsb_minus4 = h264_ue(bs);else if(dec->pic_order_cnt_type = 1)dec->delta_pic_order_always_zero_flag = bs->GetBits(1);temp = h264_se(bs);/printf(" offset_for_non_ref_pic: %dn", temp);temp = h264_se(bs);/printf(" offset_for_top_to_bottom

22、_field: %dn",temp);temp = h264_ue(bs);for(uint32_t ix = 0; ix < temp; ix+)temp = h264_se(bs);/printf(" offset_for_ref_frame%u: %dn", ix, temp);temp = h264_ue(bs);/printf(" num_ref_frames: %un", temp);temp = bs->GetBits(1);/printf(" gaps_in_frame_num_value_allowed_

23、flag: %un", temp);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: %un", PicHeightInMapUnits - 1);dec->frame_mbs_

24、only_flag = bs->GetBits(1);if(!dec->frame_mbs_only_flag)temp = bs->GetBits(1);/printf(" mb_adaptive_frame_field_flag: %un", temp);temp = bs->GetBits(1);/printf(" direct_8x8_inference_flag: %un",temp);temp = bs->GetBits(1);if(temp)temp = h264_ue(bs);/printf(" f

25、rame_crop_left_offset: %un", temp);temp = h264_ue(bs);/printf(" frame_crop_right_offset: %un", temp);temp = h264_ue(bs);/printf(" frame_crop_top_offset: %un", temp);temp = h264_ue(bs);/printf(" frame_crop_bottom_offset: %un", temp);/temp = bs->GetBits(1);/if(tem

26、p)/h264_vui_parameters(dec, bs);/海康sps可能没有这个东西/dec->pic_width = PicWidthInMbs * 16;dec->pic_height =( 2 - dec->frame_mbs_only_flag ) * PicHeightInMapUnits * 16;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 &l

27、t;<= 1;ix+;return ix;void h264_parse_pic_parameter_set(h264_decode_t *dec, CBitstream *bs)uint32_t num_slice_groups, temp, iGroup;temp = h264_ue(bs);/printf(" pic_parameter_set_id: %un", temp);temp = h264_ue(bs);/printf(" seq_parameter_set_id: %un", temp);temp = bs->GetBits

28、(1);/printf(" entropy_coding_mode_flag: %un",temp);dec->pic_order_present_flag = bs->GetBits(1);/printf(" pic_order_present_flag: %un", dec->pic_order_present_flag);num_slice_groups = h264_ue(bs);/printf(" num_slice_groups_minus1: %un", num_slice_groups);if(num

29、_slice_groups > 0)temp = h264_ue(bs);/printf(" slice_group_map_type: %un", temp);if(temp = 0)for(iGroup = 0; iGroup <= num_slice_groups; iGroup+)temp = h264_ue(bs);/printf(" run_length_minus1%u: %un", iGroup,temp);else if(temp = 2)for(iGroup = 0; iGroup < num_slice_group

30、s; iGroup+)temp = h264_ue(bs);/printf(" top_left%u: %un", iGroup, temp);temp = h264_ue(bs);/printf(" bottom_right%u: %un", iGroup, temp);else if(temp < 6) / 3, 4, 5temp = bs->GetBits(1);/printf(" slice_group_change_direction_flag: %un", temp);temp = h264_ue(bs);/p

31、rintf(" slice_group_change_rate_minus1: %un", temp);else if(temp = 6)temp = h264_ue(bs);/printf(" pic_size_in_map_units_minus1: %un", temp);uint32_t bits = calc_ceil_log2(num_slice_groups + 1);/printf(" bits - %un", bits);for(iGroup = 0; iGroup <= temp; iGroup+)temp

32、= bs->GetBits(bits);/printf(" slice_group_id%u: %un", iGroup, temp);temp = h264_ue(bs);/printf(" num_ref_idx_l0_active_minus1: %un",temp);temp = h264_ue(bs);/printf(" num_ref_idx_l1_active_minus1: %un",temp);temp = bs->GetBits(1);/printf(" weighted_pred_flag: %un",temp);temp = bs->GetBits(2);/printf(" weighted_bipred_idc: %un",temp);temp = h264_se(bs);/printf(" pic_init_qp_minus26: %dn", temp);temp = h264_se(bs);/printf(" pic_init_qs_minus26: %dn", temp);temp = h264_se(bs

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论