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

📄 b_frame.c

📁 本源码是H.26L标准的Visual C++源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
          jj=(img->pix_c_y+j)*f1+dbMV[1][pic_block_y][pic_block_x+4];

          ii0= max (0, min (img->width_cr-1, ii/f1));
          jj0= max (0, min (img->height_cr-1, jj/f1));
          ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
          jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));

          if1=(ii & f2);
          jf1=(jj & f2);
          if0=f1-if1;
          jf0=f1-jf1;

          bw_pred=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
               if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;

          img->mpr[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);

          // LG : direct residual coding
          img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j];
        }
      }
    }
    *cr_cbp=dct_chroma(uv, *cr_cbp);
  }
  // LG : direct residual coding
  currMB->cbp += *cr_cbp*16;
}


/*!
 ************************************************************************
 * \brief
 *    Passes for a given MB of a B picture the reference frame
 *    parameter and motion parameters to the NAL
 ************************************************************************
 */
int writeMotionInfo2NAL_Bframe()
{
  int i,j,k,l,m;
  int step_h,step_v;
  int curr_mvd=0, fw_blk_shape=0, bw_blk_shape=0;
  int mb_nr = img->current_mb_nr;
  Macroblock *currMB = &img->mb_data[mb_nr];
  const int fw_predframe_no=currMB->ref_frame;
  SyntaxElement *currSE = &img->MB_SyntaxElements[currMB->currSEnr];
  int *bitCount = currMB->bitcounter;
  Slice *currSlice = img->currentSlice;
  DataPartition *dataPart;
  int *partMap = assignSE2partition[input->partition_mode];
  int no_bits = 0;


  // Write fw_predframe_no(Forward, Bidirect)
  if(img->imod==B_Forward || img->imod==B_Bidirect)
  {
#ifdef _ADDITIONAL_REFERENCE_FRAME_
    if (img->no_multpred > 1 || input->add_ref_frame > 0)
#else
      if (img->no_multpred > 1)
#endif
    {
      currSE->value1 = currMB->ref_frame;
      currSE->type = SE_REFFRAME;
      if (input->symbol_mode == UVLC)
        currSE->mapping = n_linfo2;
      else
        currSE->writing = writeRefFrame2Buffer_CABAC;

      dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
      dataPart->writeSyntaxElement( currSE, dataPart);
      bitCount[BITS_INTER_MB]+=currSE->len;
      no_bits += currSE->len;
#if TRACE
      snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_fw_Reference frame no %3d ", currMB->ref_frame);
#endif
      // proceed to next SE
      currSE++;
      currMB->currSEnr++;
    }
  }

  // Write Blk_size(Bidirect)
  if(img->imod==B_Bidirect)
  {
    // Write blockshape for forward pred
    fw_blk_shape = BlkSize2CodeNumber(img->fw_blc_size_h, img->fw_blc_size_v);

    currSE->value1 = fw_blk_shape;
    currSE->type = SE_BFRAME;
    if (input->symbol_mode == UVLC)
      currSE->mapping = n_linfo2;
    else
      currSE->writing = writeBiDirBlkSize2Buffer_CABAC;

    dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
    dataPart->writeSyntaxElement( currSE, dataPart);
    bitCount[BITS_INTER_MB]+=currSE->len;
    no_bits += currSE->len;
#if TRACE
    snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_fw_blk %3d x %3d ", img->fw_blc_size_h, img->fw_blc_size_v);
#endif

    // proceed to next SE
    currSE++;
    currMB->currSEnr++;

    // Write blockshape for backward pred
    bw_blk_shape = BlkSize2CodeNumber(img->bw_blc_size_h, img->bw_blc_size_v);

    currSE->value1 = bw_blk_shape;
    currSE->type = SE_BFRAME;
    if (input->symbol_mode == UVLC)
      currSE->mapping = n_linfo2;
    else
      currSE->writing = writeBiDirBlkSize2Buffer_CABAC;

    dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
    dataPart->writeSyntaxElement( currSE, dataPart);
    bitCount[BITS_INTER_MB]+=currSE->len;
    no_bits += currSE->len;
#if TRACE
    snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_bw_blk %3d x %3d ", img->bw_blc_size_h, img->bw_blc_size_v);
#endif

    // proceed to next SE
    currSE++;
    currMB->currSEnr++;

  }

  // Write MVDFW(Forward, Bidirect)
  if(img->imod==B_Forward || img->imod==B_Bidirect)
  {
    step_h=img->fw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
    step_v=img->fw_blc_size_v/BLOCK_SIZE;  // vertical stepsize

    for (j=0; j < BLOCK_SIZE; j += step_v)
    {
      for (i=0;i < BLOCK_SIZE; i += step_h)
      {
        for (k=0; k < 2; k++)
        {
          if(img->mb_mode==1) // fw 16x16
            curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
          else
            if(img->mb_mode==3) // bidirectinal
            {
              switch(fw_blk_shape)
              {
                case 0:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
                  break;
                case 1:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][2][k]);
                  break;
                case 2:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][3][k]);
                  break;
                case 3:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][4][k]);
                  break;
                case 4:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][5][k]);
                  break;
                case 5:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][6][k]);
                  break;
                case 6:
                  curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][7][k]);
                  break;
                default:
                  break;
              }
            }
            else
              curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][(img->mb_mode)/2][k]);

          // store (oversampled) mvd
          for (l=0; l < step_v; l++)
            for (m=0; m < step_h; m++)
              currMB->mvd[0][j+l][i+m][k] = curr_mvd;

          currSE->value1 = curr_mvd;
          currSE->type = SE_MVD;
          if (input->symbol_mode == UVLC)
            currSE->mapping = mvd_linfo2;
          else
          {
            img->subblock_x = i; // position used for context determination
            img->subblock_y = j; // position used for context determination
            currSE->value2 = 2*k; // identifies the component and the direction; only used for context determination
            currSE->writing = writeBiMVD2Buffer_CABAC;
          }
          dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
          dataPart->writeSyntaxElement( currSE, dataPart);
          bitCount[BITS_INTER_MB]+=currSE->len;
          no_bits += currSE->len;
#if TRACE
          snprintf(currSE->tracestring, TRACESTRING_SIZE, " MVD(%d) = %3d",k, curr_mvd);
#endif

          // proceed to next SE
          currSE++;
          currMB->currSEnr++;
        }
      }
    }
  }

  // Write MVDBW(Backward, Bidirect)
  if(img->imod==B_Backward || img->imod==B_Bidirect)
  {
    step_h=img->bw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
    step_v=img->bw_blc_size_v/BLOCK_SIZE;  // vertical stepsize

    for (j=0; j < BLOCK_SIZE; j += step_v)
    {
      for (i=0;i < BLOCK_SIZE; i += step_h)
      {
        for (k=0; k < 2; k++)
        {
          if(img->mb_mode==2) // bw 16x16
            curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
          else
            if(img->mb_mode==3) // bidirectional
            {
              switch(bw_blk_shape)
              {
                case 0:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
                  break;
                case 1:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][2][k]);
                  break;
                case 2:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][3][k]);
                  break;
                case 3:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][4][k]);
                  break;
                case 4:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][5][k]);
                  break;
                case 5:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][6][k]);
                  break;
                case 6:
                  curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][7][k]);
                  break;
                default:
                  break;
              }
            }
            else // other bw
              curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][(img->mb_mode-1)/2][k]);


          // store (oversampled) mvd
          for (l=0; l < step_v; l++)
            for (m=0; m < step_h; m++)
              currMB->mvd[1][j+l][i+m][k] = curr_mvd;

          currSE->value1 = curr_mvd;
          currSE->type = SE_MVD;

          if (input->symbol_mode == UVLC)
            currSE->mapping = mvd_linfo2;
          else
          {
            img->subblock_x = i; // position used for context determination
            img->subblock_y = j; // position used for context determination
            currSE->value2 = 2*k+1; // identifies the component and the direction; only used for context determination
            currSE->writing = writeBiMVD2Buffer_CABAC;
          }

          dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
          dataPart->writeSyntaxElement( currSE, dataPart);
          bitCount[BITS_INTER_MB]+=currSE->len;
          no_bits += currSE->len;
#if TRACE
          snprintf(currSE->tracestring, TRACESTRING_SIZE, " MVD(%d) = %3d",k, curr_mvd);
#endif

          // proceed to next SE
          currSE++;
          currMB->currSEnr++;

        }
      }
    }
  }
  return no_bits;
}

/*!
 ************************************************************************
 * \brief
 *    Passes back the code number given the blocksize width and
 *    height (should be replaced by an appropriate table lookup)
 ************************************************************************
 */
int BlkSize2CodeNumber(int blc_size_h, int blc_size_v)
{

  if(blc_size_h==16 && blc_size_v==16)  // 16x16 : code_number 0
    return 0;
  else
    if(blc_size_h==16 && blc_size_v==8)  // 16x8 : code_number 1
      return 1;
    else
      if(blc_size_h==8 && blc_size_v==16) // 8x16 : code_number 2
        return 2;
      else
        if(blc_size_h==8 && blc_size_v==8) // 8x8 : code_number 3
          return 3;
        else
          if(blc_size_h==8 && blc_size_v==4)  // 8x4 : code_number 4
            return 4;
          else
            if(blc_size_h==4 && blc_size_v==8) // 4x8 : code_number 5
              return 5;
            else  // 4x4 : code_number 6
              return 6;

}

/*!
 ************************************************************************
 * \brief
 *    select intra, forward, backward, bidirectional, direct mode
 ************************************************************************
 */
int motion_search_Bframe(int tot_intra_sad)
{
  int fw_sad, bw_sad, bid_sad, dir_sad;
  int fw_predframe_no;

  fw_predframe_no=get_fwMV(&fw_sad, tot_intra_sad);

  get_bwMV(&bw_sad);

  get_bid(&bid_sad, fw_predframe_no);

  get_dir(&dir_sad);


  compare_sad(tot_intra_sad, fw_sad, bw_sad, bid_sad, dir_sad);

  return fw_predframe_no;
}


void get_bid(int *bid_sad, int fw_predframe_no)
{
  int mb_y,mb_x, block_y, block_x, pic_pix_y, pic_pix_x, pic_block_y, pic_block_x;
  int i, j, ii4, jj4, iii4, jjj4, i2, j2;
  int fw_pred, bw_pred, bid_pred[4][4];
  int code_num, step_h, step_v, mvd_x, mvd_y;

  // consider code number of fw_predframe_no
  *bid_sad = QP2QUANT[img->qp]*min(fw_predframe_no,1)*2;

  // consider bits of fw_blk_size
  if(img->fw_blc_size_h==16 && img->fw_blc_size_v==16)      // 16x16 : blocktype 1
    code_num=0;
  else if(img->fw_blc_size_h==16 && img->fw_blc_size_v==8)   // 16x8 : blocktype 2
    code_num=1;
  else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==16)  // 8x16 : blocktype 3
    code_num=2;
  else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==8)  // 8x8 : blocktype 4
    code_num=3;
  else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==4)   // 8x4 : blocktype 5
    code_num=4;
  else if(img->fw_blc_size_h==4 && img->fw_blc_size_v==8)   // 4x8 : blocktype 6
    code_num=5;
  else        // 4x4 : blocktype 7
    code_num=6;
  *bid_sad += QP2QUANT[img->qp]*img->blk_bituse[code_num];

  // consider bits of bw_blk_size
  if(img->bw_blc_size_h==16 && img->bw_blc_size_v==16)      // 16x16 : blocktype 1
    code_num=0;
  else if(img->bw_blc_size_h==16 && img->bw_blc_size_v==8)   // 16x8 : blocktype 2
    code_num=1;
  else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==16)  // 8x16 : blocktype 3
    code_num=2;
  else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==8)  // 8x8 : blocktype 4
    code_num=3;
  else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==4)   // 8x4 : blocktype 5
    code_num=4;
  else if(img->bw_blc_size_h==4 && img->bw_blc_size_v==8)   // 4x8 : blocktype 6

⌨️ 快捷键说明

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