原始的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 header currSlice->next_header = -8888; // initialized to an impossible value for debugging -- correct value is taken from slice header img->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); else ret=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.264 while(!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_8bits while(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 TRACE fprintf (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); #endif free(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_8bits if(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 file rewind = 0; if(info3 == 1) rewind = -4; else if (info2 == 1) rewind = -3; else printf(" 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 TRACE fprintf (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); #endif free(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)
......