JM8.6復号側はH.264符号ストリームをどのように読み取ったのか.(GetAnnexbNALU関数)

13145 ワード

元のforeman_part_qcif.yuvファイルを符号化すると(本人が符号化するフレーム)、testが生成される.264ファイル、今JM 8を使います.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を抽出するプロセスであり、1つのsliceは一般的に1つの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
"); free(Buf); return -1; } } if(Buf[pos-1]!=1) { printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1
"); free(Buf); return -1; } if(pos<3) { printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1
"); 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
"); 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
", pos, nalu->len, nalu->nal_reference_idc, nalu->nal_unit_type); #if TRACE fprintf (p_trace, "

Last NALU in File

"); fprintf (p_trace, "Annex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d

", 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
"); 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
", pos, nalu->len, nalu->nal_reference_idc, nalu->nal_unit_type); #if TRACE fprintf (p_trace, "

Annex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d

", 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で1つの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)
 ......