JM8.6解码端是如何对H.264码流进行读取的?(GetAnnexbNALU 函数)

2024-02-06 16:38

本文主要是介绍JM8.6解码端是如何对H.264码流进行读取的?(GetAnnexbNALU 函数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       原始的foreman_part_qcif.yuv文件进行编码后(本人编码的是一帧),生成了test.264文件,现在要用JM8.6解码器对其进行解码,显然,首先要读取码流,然后对码流进行解析. 读二进制的数据,无非就是用到fread, fgetc这样的函数,在代码中简单搜索一下,就很容易定位到我们感兴趣的地方,即:

while(!feof(bits) && (Buf[pos++]=fgetc(bits))==0);

       而这个语句刚好就在GetAnnexbNALU函数中,经跟踪发现,GetAnnexbNALU函数被read_new_slice函数调用,而read_new_slice函数又被decode_one_frame函数调用,decode_one_frame函数进而被main函数调用.  调用关系在代码中为:

int main(int argc, char **argv)
{
......  while (decode_one_frame(img, input, snr) != EOS);
......
}
int decode_one_frame(struct img_par *img,struct inp_par *inp, struct snr_par *snr)
{int current_header;Slice *currSlice = img->currentSlice;img->current_slice_nr = 0;img->current_mb_nr = -4711;     // initialized to an impossible value for debugging -- correct value is taken from slice headercurrSlice->next_header = -8888; // initialized to an impossible value for debugging -- correct value is taken from slice headerimg->num_dec_mb = 0;img->newframe = 1;while ((currSlice->next_header != EOS && currSlice->next_header != SOP)){current_header = read_new_slice();if (current_header == EOS){exit_picture();return EOS;}decode_slice(img, inp, current_header);img->newframe = 0;img->current_slice_nr++;}exit_picture();return (SOP);
}
int read_new_slice()
{
......     while (1){......if (input->FileFormat == PAR_OF_ANNEXB)ret=GetAnnexbNALU (nalu);elseret=GetRTPNALU (nalu);......}...... 
}

       从上述跟踪可知:GetAnnexbNALU实际上就是从码流中提取一个NALU的过程, 一个slice一般就对应了一个NALU, 在decode_one_frame中,先调用read_new_slice, 进而decode_slice, 逻辑本就该如此.
    

        现在进入GetAnnexbNALU函数看看:

int GetAnnexbNALU (NALU_t *nalu)
{int info2, info3, pos = 0;int StartCodeFound, rewind;char *Buf;int LeadingZero8BitsCount=0, TrailingZero8Bits=0;if ((Buf = (char*)calloc (nalu->max_size , sizeof(char))) == NULL) no_mem_exit("GetAnnexbNALU: Buf");// bits是全局的文件指针,已经指向了打开的test.264while(!feof(bits) && (Buf[pos++]=fgetc(bits))==0);if(feof(bits)){if(pos==0)return 0;else{printf( "GetAnnexbNALU can't read start code\n");free(Buf);return -1;}}if(Buf[pos-1]!=1){printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1\n");free(Buf);return -1;}if(pos<3){printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1\n");free(Buf);return -1;}else if(pos==3){nalu->startcodeprefix_len = 3;LeadingZero8BitsCount = 0;}else{LeadingZero8BitsCount = pos-4;nalu->startcodeprefix_len = 4;}//the 1st byte stream NAL unit can has leading_zero_8bits, but subsequent ones are not//allowed to contain it since these zeros(if any) are considered trailing_zero_8bits//of the previous byte stream NAL unit.if(!IsFirstByteStreamNALU && LeadingZero8BitsCount>0){printf ("GetAnnexbNALU: The leading_zero_8bits syntax can only be present in the first byte stream NAL unit, return -1\n");free(Buf);return -1;}IsFirstByteStreamNALU=0;StartCodeFound = 0;info2 = 0;info3 = 0;while (!StartCodeFound){if (feof (bits)){//Count the trailing_zero_8bitswhile(Buf[pos-2-TrailingZero8Bits]==0)TrailingZero8Bits++;nalu->len = (pos-1)-nalu->startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits;// 关键地方:内存复制memcpy (nalu->buf, &Buf[LeadingZero8BitsCount+nalu->startcodeprefix_len], nalu->len);     nalu->forbidden_bit = (nalu->buf[0]>>7) & 1;nalu->nal_reference_idc = (nalu->buf[0]>>5) & 3;nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// printf ("GetAnnexbNALU, eof case: pos %d nalu->len %d, nalu->reference_idc %d, nal_unit_type %d \n", pos, nalu->len, nalu->nal_reference_idc, nalu->nal_unit_type);#if TRACEfprintf (p_trace, "\n\nLast NALU in File\n\n");fprintf (p_trace, "Annex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d\n\n",nalu->startcodeprefix_len == 4?"long":"short", nalu->len, nalu->forbidden_bit, nalu->nal_reference_idc, nalu->nal_unit_type);fflush (p_trace);
#endiffree(Buf);return pos-1;}Buf[pos++] = fgetc (bits);info3 = FindStartCode(&Buf[pos-4], 3);if(info3 != 1)info2 = FindStartCode(&Buf[pos-3], 2);StartCodeFound = (info2 == 1 || info3 == 1);}//Count the trailing_zero_8bitsif(info3==1)	//if the detected start code is 00 00 01, trailing_zero_8bits is sure not to be present{while(Buf[pos-5-TrailingZero8Bits]==0)TrailingZero8Bits++;}// Here, we have found another start code (and read length of startcode bytes more than we should// have.  Hence, go back in the filerewind = 0;if(info3 == 1)rewind = -4;else if (info2 == 1)rewind = -3;elseprintf(" Panic: Error in next start code search \n");if (0 != fseek (bits, rewind, SEEK_CUR)){snprintf (errortext, ET_SIZE, "GetAnnexbNALU: Cannot fseek %d in the bit stream file", rewind);free(Buf);error(errortext, 600);}// Here the leading zeros(if any), Start code, the complete NALU, trailing zeros(if any)// and the next start code is in the Buf.// The size of Buf is pos, pos+rewind are the number of bytes excluding the next// start code, and (pos+rewind)-startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits// is the size of the NALU.nalu->len = (pos+rewind)-nalu->startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits;// 关键地方:内存复制memcpy (nalu->buf, &Buf[LeadingZero8BitsCount+nalu->startcodeprefix_len], nalu->len);nalu->forbidden_bit = (nalu->buf[0]>>7) & 1;nalu->nal_reference_idc = (nalu->buf[0]>>5) & 3;nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;//printf ("GetAnnexbNALU, regular case: pos %d nalu->len %d, nalu->reference_idc %d, nal_unit_type %d \n", pos, nalu->len, nalu->nal_reference_idc, nalu->nal_unit_type);
#if TRACEfprintf (p_trace, "\n\nAnnex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d\n\n",nalu->startcodeprefix_len == 4?"long":"short", nalu->len, nalu->forbidden_bit, nalu->nal_reference_idc, nalu->nal_unit_type);fflush (p_trace);
#endiffree(Buf);return (pos+rewind);
}


      可见,GetAnnexbNALU函数正好实现了从test.264中读取一个NALU,调试代码后发现也正是如此,最后给出对应的trace_dec.txt的部分内容:

 

Annex B NALU w/ long startcode, len 8, forbidden_bit 0, nal_reference_idc 3, nal_unit_type 7

@0      SPS: profile_idc                                      01000010  ( 66)
@8      SPS: constrained_set0_flag                                   0  (  0)
@9      SPS: constrained_set1_flag                                   0  (  0)
@10     SPS: constrained_set2_flag                                   0  (  0)
@11     SPS: reserved_zero_5bits                                 00000  (  0)
@16     SPS: level_idc                                        00011110  ( 30)
@24     SPS: seq_parameter_set_id                                    1  (  0)
@25     SPS: log2_max_frame_num_minus4                               1  (  0)
@26     SPS: pic_order_cnt_type                                      1  (  0)
@27     SPS: log2_max_pic_order_cnt_lsb_minus4                       1  (  0)
@28     SPS: num_ref_frames                                    0001011  ( 10)
@35     SPS: gaps_in_frame_num_value_allowed_flag                    0  (  0)
@36     SPS: pic_width_in_mbs_minus1                           0001011  ( 10)
@43     SPS: pic_height_in_map_units_minus1                    0001001  (  8)
@50     SPS: frame_mbs_only_flag                                     1  (  1)
@51     SPS: direct_8x8_inference_flag                               0  (  0)
@52     SPS: frame_cropping_flag                                     0  (  0)
@53     SPS: vui_parameters_present_flag                             0  (  0)


Annex B NALU w/ long startcode, len 5, forbidden_bit 0, nal_reference_idc 3, nal_unit_type 8

@54     PPS: pic_parameter_set_id                                    1  (  0)
@55     PPS: seq_parameter_set_id                                    1  (  0)
@56     PPS: entropy_coding_mode_flag                                0  (  0)
@57     PPS: pic_order_present_flag                                  0  (  0)
@58     PPS: num_slice_groups_minus1                                 1  (  0)
@59     PPS: num_ref_idx_l0_active_minus1                      0001010  (  9)
@66     PPS: num_ref_idx_l1_active_minus1                      0001010  (  9)
@73     PPS: weighted prediction flag                                0  (  0)
@74     PPS: weighted_bipred_idc                                    00  (  0)
@76     PPS: pic_init_qp_minus26                                     1  (  0)
@77     PPS: pic_init_qs_minus26                                     1  (  0)
@78     PPS: chroma_qp_index_offset                                  1  (  0)
@79     PPS: deblocking_filter_control_present_flag                  0  (  0)
@80     PPS: constrained_intra_pred_flag                             0  (  0)
@81     PPS: redundant_pic_cnt_present_flag                          0  (  0)


Last NALU in File

Annex B NALU w/ long startcode, len 2741, forbidden_bit 0, nal_reference_idc 3, nal_unit_type 5

@82     SH: first_mb_in_slice                                        1  (  0)
@83     SH: slice_type                                         0001000  (  7)
@90     SH: pic_parameter_set_id                                     1  (  0)
@91     SH: frame_num                                             0000  (  0)
@95     SH: idr_pic_id                                               1  (  0)
@96     SH: pic_order_cnt_lsb                                     0000  (  0)
@100    SH: no_output_of_prior_pics_flag                             0  (  0)
@101    SH: long_term_reference_flag                                 0  (  0)
@102    SH: slice_qp_delta                                       00100  (  2)

*********** POC: 0 (I/P) MB: 0 Slice: 0 Type 2 **********
@107    mb_type                                                      1  (  0)
@108    intra4x4_pred_mode                                           1  ( -1)
@109    intra4x4_pred_mode                                        0001  (  1)
@113    intra4x4_pred_mode                                           1  ( -1)
@114    intra4x4_pred_mode                                        0110  (  6)
@118    intra4x4_pred_mode                                        0001  (  1)
@122    intra4x4_pred_mode                                        0111  (  7)
@126    intra4x4_pred_mode                                           1  ( -1)
@127    intra4x4_pred_mode                                           1  ( -1)
@128    intra4x4_pred_mode                                        0000  (  0)
@132    intra4x4_pred_mode                                        0000  (  0)
@136    intra4x4_pred_mode                                        0000  (  0)
@140    intra4x4_pred_mode                                        0111  (  7)
@144    intra4x4_pred_mode                                        0101  (  5)
@148    intra4x4_pred_mode                                           1  ( -1)
@149    intra4x4_pred_mode                                        0111  (  7)
@153    intra4x4_pred_mode                                        0111  (  7)

 ......

这篇关于JM8.6解码端是如何对H.264码流进行读取的?(GetAnnexbNALU 函数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684917

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb