⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cabac.c

📁 h.264/avc 视频编码程序,实现分数像素匹配功能,非原创.
💻 C
📖 第 1 页 / 共 4 页
字号:
      act_ctx = (count[0] == 0) ? 0 : ((prev_sym[0] == 0) ? 1 : 2);
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }
      prev_sym[0] = coeff_count;
      if(++count[0] == 2) count[0]=0;
      break;
    }
    case 1:
    case 2:
    {
      //context termination
      j = img->subblock_y;
      i = img->subblock_x;
      if (j==0)
        b = (currMB->mb_available[0][1] == NULL)?0:(((currMB->mb_available[0][1])->coeffs_count[BLOCK_SIZE-1][i]==0)?0:1);
      else
        b = (currMB->coeffs_count[j-1][i]==0)?0:1;
          
      if (i==0)
        a = (currMB->mb_available[1][0] == NULL)?0:(((currMB->mb_available[1][0])->coeffs_count[j][BLOCK_SIZE-1]==0)?0:1);
      else
        a = (currMB->coeffs_count[j][i-1]==0)?0:1;

      act_ctx = a+2*b;
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }      
      currMB->coeffs_count[j][i] = coeff_count; 
      break;
    }
    case 4://max 15 coeffs // 16x16 luma ac
    {
      //context determination
      act_ctx = (count[2] == 0) ? 0 : ((prev_sym[2] == 0) ? 1 : 2);
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }
      prev_sym[2] = coeff_count;
      if(++count[2] == 16) count[2]=0;
      break;
    }
    case 5:
    case 6://max 4 coeffs, chroma dc different ctx uv for first bin
    {
      act_ctx = (se->k == 0)?0:1;
      //act_ctx = (se->k < 4)?0:1;
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }
      break;
    }
    case 7:
    case 8://max 15 coeffs, chroma ac different ctx uv for first bin
    {
      act_ctx = (se->k < 4)?0:1;
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }
      break;
    }
    case 3:
    {
      act_ctx = 0;
      coeff_count = biari_decode_symbol(dep_dp, ctx->coeff_count_context[curr_ctx_idx] + act_ctx );
      if (coeff_count != 0)
      {
        coeff_count = unary_bin_max_decode(dep_dp,ctx->coeff_count_context[curr_ctx_idx]+4,1,max_coeff[curr_ctx_idx]-1);
        coeff_count++;
      }
      break;
    }
    default: printf("ERROR");
    }
    coeff_count_all = coeff_count;
    if (coeff_count == 0)
      send_eob = 1;
    else
      send_eob = 0;

    prevLevel = 0;
  }

  if (send_eob == 1)
  {
    se->value1 = 0;
    se->value2 =0;
    send_eob = 0;
#if TRACE
    fprintf(p_trace, "@%d%s\t\t\t%d\t%d\n",symbolCount++, se->tracestring, se->value1,se->value2);
    fflush(p_trace);
#endif
    return;
  }
  if (coeff_count != 0) //get the coeff. (run and level)
  {
    //determine run ctx
    switch (curr_ctx_idx)
    {
    case 1:
    case 2:
      curr_run_ctx = ((coeff_count_all) >= 4) ? 1 : 0;  
      break;                                        
    case 3:
      curr_run_ctx = ((coeff_count) >= 4) ? 1 : 0;
      break;
    case 4:
    case 7:
    case 8:
    case 0:
      curr_run_ctx = ((coeff_count) >= 3) ? 1 : 0;
      break;
    case 5:
    case 6:
      curr_run_ctx = ((coeff_count) >= 2) ? 1 : 0;
      break;
    }

    curr_run_ctx = 9*curr_run_ctx + curr_ctx_idx;
    // get run
    if (coeff_count == coeff_count_all)
      max_run = max_coeff[curr_ctx_idx]-coeff_count_all;
    if (max_run > 0) 
      run = unary_bin_max_decode(dep_dp,ctx->run_context[curr_run_ctx],1,max_run);
    max_run -= run;
    //get level
    changed_ctx_idx = curr_ctx_idx;
    changed_ctx_idx += 9*prevLevel;
    level = unary_level_decode(dep_dp,ctx->level_context[changed_ctx_idx]);
    level++;
    prevLevel     = level > 3 ? 3 : level;  
    //get sign
    curr_level_ctx = 3;
    sign_of_level = biari_decode_symbol(dep_dp, ctx->level_context[curr_ctx_idx] + curr_level_ctx);
    if (sign_of_level) level = (-1)*level;
    coeff_count--;
    if (coeff_count == 0)
    {
      send_eob = 1;
      prevLevel = 0;
    }
    else
      send_eob = 0;
  }

  se->value1 = level;
  se->value2 = run;

#if TRACE
  fprintf(p_trace, "@%d%s\t\t\t%d\t%d\n",symbolCount++, se->tracestring, se->value1, se->value2);
  fflush(p_trace);
#endif
}

/*!
 ************************************************************************
 * \brief
 *    arithmetic decoding
 ************************************************************************
 */
int readSyntaxElement_CABAC(SyntaxElement *se, struct img_par *img, struct inp_par *inp, DataPartition *this_dataPart)
{
  int curr_len;
  DecodingEnvironmentPtr dep_dp = &(this_dataPart->de_cabac);

  curr_len = arideco_bits_read(dep_dp);

  // perform the actual decoding by calling the appropriate method
  se->reading(se, inp, img, dep_dp);

  return (se->len = (arideco_bits_read(dep_dp) - curr_len));
}

/*!
 ************************************************************************
 * \brief
 *    get slice and header
 ************************************************************************
 */
int readSliceCABAC(struct img_par *img, struct inp_par *inp)
{
  Slice *currSlice = img->currentSlice;
  Bitstream *currStream = currSlice->partArr[0].bitstream;
  unsigned char *code_buffer = currStream->streamBuffer;
  int *read_len = &(currStream->read_len);
  DecodingEnvironmentPtr dep = &((currSlice->partArr[0]).de_cabac);
  int current_header;
  int BitstreamLengthInBytes;
  int info;
  int BitsUsedByHeader = 0, ByteStartPosition;
  int newframe = 0;   //WYK: Oct. 8, 2001, change the method to find a new frame

  currStream->frame_bitoffset =0;

  memset (code_buffer, 0xff, MAX_CODED_FRAME_SIZE);   // this prevents a buffer full with zeros
  BitstreamLengthInBytes = currStream->bitstream_length = GetOneSliceIntoSourceBitBuffer(img, inp, code_buffer);

  // Here we are ready to interpret the picture and slice headers.  Since
  // SliceHeader() gets the data out of the UVLC's len/info
  // array, we need to convert the start of our slice to such a format.


  if (BitstreamLengthInBytes < 4)
    return EOS;

  // Now we have the bits between the current startcode (inclusive) and the
  // next start code in code_buffer.  Now decode the start codes and the headers
  if (31 != GetVLCSymbol (code_buffer, 0, &info, BitstreamLengthInBytes))
  {
    snprintf (errortext, ET_SIZE, "readSliceCABAC: Panic, expected start code symbol, found wrong len");
    error(errortext, 600);
  }
  currStream->frame_bitoffset +=31;
  BitsUsedByHeader+=SliceHeader(img, inp);

  //WYK: Oct. 8, 2001, change the method to find a new frame
  if(img->tr != img->tr_old)
    newframe = 1;
  else 
    newframe = 0;
  img->tr_old = img->tr;
    
  // if the TR of current slice is not identical to the TR of previous received slice, we have a new frame
  if(newframe)
    current_header = SOP;
  else
    current_header = SOS;

  ByteStartPosition = currStream->frame_bitoffset/8;
  if ((currStream->frame_bitoffset)%8 != 0)
    ByteStartPosition++;
  arideco_start_decoding(dep, code_buffer, ByteStartPosition, read_len);


  currSlice->picture_id = img->tr;
  return current_header;

}

/*!
 ************************************************************************
 * \brief
 *    decoding of unary binarization using one or 2 distinct
 *    models for the first and all remaining bins; no terminating
 *    "0" for max_symbol
 ***********************************************************************
 */
unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
                                  BiContextTypePtr ctx,
                                  int ctx_offset,
                                  unsigned int max_symbol)
{
  unsigned int l;
  unsigned int symbol;
  BiContextTypePtr ictx;

  symbol =  biari_decode_symbol(dep_dp, ctx );

  if (symbol==0)
    return 0;
  else
  {
    if (max_symbol == 1)
    return symbol;
    symbol=0;
    ictx=ctx+ctx_offset;
    do
    {
      l=biari_decode_symbol(dep_dp, ictx);
      symbol++;
    }
    while( (l!=0) && (symbol<max_symbol-1) );
    if ((l!=0) && (symbol==max_symbol-1))
      symbol++;
    return symbol;
  }
}

/*!
 ************************************************************************
 * \brief
 *    decoding of unary binarization of the absolute value
 *    of a level using 3 distinct models by separating the first,
 *    the second and all remaining bins
 ***********************************************************************
 */
unsigned int unary_level_decode(DecodingEnvironmentPtr dep_dp,
                                BiContextTypePtr ctx)
{
  unsigned int l;
  unsigned int symbol;
  int bin=1;
  BiContextTypePtr ictx=ctx;

  symbol = biari_decode_symbol(dep_dp, ictx );

  if (symbol==0)
    return 0;
  else
  {
    symbol=0;
    ictx++;
    do
    {
      l=biari_decode_symbol(dep_dp, ictx  );
      if ((++bin)==2) ictx++;
      symbol++;
    }
    while (l!=0);
    return symbol;
  }
}

/*!
 ************************************************************************
 * \brief
 *    decoding of unary binarization using one or 2 distinct
 *    models for the first and all remaining bins
 ***********************************************************************
 */
unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
                              BiContextTypePtr ctx,
                              int ctx_offset)
{
  unsigned int l;
  unsigned int symbol;
  BiContextTypePtr ictx;

  symbol = biari_decode_symbol(dep_dp, ctx );

  if (symbol==0)
    return 0;
  else
  {
    symbol=0;
    ictx=ctx+ctx_offset;
    do
    {
      l=biari_decode_symbol(dep_dp, ictx);
      symbol++;
    }
    while( l!=0 );
    return symbol;
  }
}

/*!
 ************************************************************************
 * \brief
 *    decoding of unary binarization of the absolute value of a
 *    mv component using 4 distinct models by separating the first,
 *    the second, intermediate and all remaining bins
 ***********************************************************************
 */
unsigned int unary_mv_decode(DecodingEnvironmentPtr dep_dp,
                             BiContextTypePtr ctx,
                             unsigned int max_bin)
{
  unsigned int l;
  unsigned int bin=1;
  unsigned int symbol;

  BiContextTypePtr ictx=ctx;

  symbol = biari_decode_symbol(dep_dp, ictx );

  if (symbol==0)
    return 0;
  else
  {
    symbol=0;
    ictx++;
    do
    {
      l=biari_decode_symbol(dep_dp, ictx  );
      if ((++bin)==2) ictx++;
      if (bin==max_bin) ictx++;
      symbol++;
    }
    while (l!=0);
    return symbol;
  }
}

/*!
 ************************************************************************
 * \brief
 *    finding end of a slice in case this is not the end of a frame
 ************************************************************************
 */
int cabac_startcode_follows(struct img_par *img, struct inp_par *inp)
{
  Slice *currSlice = img->currentSlice;
  if (img->current_mb_nr == currSlice->last_mb_nr)
    return TRUE;
  return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -