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

📄 rdopt.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:
    {
      decode_one_b8block (k, P8x8, block, mode, l0_ref);
      for (j=img->opix_y+pay; j<img->opix_y+pay+8; j++)
      {
        img_org = pCurImg[j];
        img_enc =  decs->decY[k][j];
        for (i=img->opix_x+pax; i<img->opix_x+pax+8; i++)
        {
          distortion += iabs2( img_org[i] - img_enc[i]);
        }
      }
    }
    distortion /= input->NoOfDecoders;
  }
  else
  {
    for (j=pay; j<pay+8; j++)
    {
      img_org = pCurImg[img->opix_y + j];
      img_enc =  enc_picture->imgY[img->pix_y + j];
      for (i=img->pix_x+pax; i<img->pix_x+pax+8; i++)
      {
        distortion += iabs2( img_org[i] -img_enc[i]);
      }
    }
    if (img->yuv_format==YUV444 && !IS_INDEPENDENT(input))
    {
      for (j=pay; j<pay+8; j++)
      {
        for (i=img->pix_x+pax; i<img->pix_x+pax+8; i++)
        {
          distortion += iabs2( imgUV_org[0][img->opix_y+j][i] - enc_picture->imgUV[0][img->pix_y+j][i]);
          distortion += iabs2( imgUV_org[1][img->opix_y+j][i] - enc_picture->imgUV[1][img->pix_y+j][i]);
        }
      }
    }
    
  }
  
  if( img->yuv_format==YUV444 && !IS_INDEPENDENT(input) ) 
  {   
    cbp |= cmp_cbp[1];
    cbp |= cmp_cbp[2];
    
    cmp_cbp[1] = cbp;
    cmp_cbp[2] = cbp;
  }

  //=====
  //=====   GET RATE
  //=====
  //----- block 8x8 mode -----
  if (input->symbol_mode == CAVLC)
  {
    ue_linfo (b8value, dummy, &mrate, &dummy);
    rate += mrate;
  }
  else
  {
    se.value1 = b8value;
    se.type   = SE_MBTYPE;
    dataPart  = &(currSlice->partArr[partMap[se.type]]);
    writeB8_typeInfo(&se, dataPart);
    rate += se.len;
  }

  //----- motion information -----
  if (!direct)
  {
    if ((img->num_ref_idx_l0_active > 1 ) && (pdir==0 || pdir==2))
      rate  += writeReferenceFrame (currMB, mode, i0, j0, 1, l0_ref);
    
    if ((img->num_ref_idx_l1_active > 1 && img->type== B_SLICE ) && (pdir==1 || pdir==2))
    {
      rate  += writeReferenceFrame (currMB, mode, i0, j0, 0, l1_ref);
    }
    
    if (pdir==0 || pdir==2)
    {
      rate  += writeMotionVector8x8 (currMB, i0, j0, i0 + 2, j0 + 2, l0_ref, LIST_0, mode);
    }
    if (pdir==1 || pdir==2)
    {
      rate  += writeMotionVector8x8 (currMB, i0, j0, i0 + 2, j0 + 2, l1_ref, LIST_1, mode);
    }
  }

  //----- coded block pattern (for CABAC only) -----
  if (input->symbol_mode == CABAC)
  {
    dataPart = &(currSlice->partArr[partMap[SE_CBP]]);
    eep_dp   = &(dataPart->ee_cabac);
    mrate    = arienco_bits_written (eep_dp);
    writeCBP_BIT_CABAC (block, ((*cnt_nonz>0)?1:0), cbp8x8, currMB, 1, eep_dp);
    mrate    = arienco_bits_written (eep_dp) - mrate;
    rate    += mrate;
  }

  //----- luminance coefficients -----
  if (*cnt_nonz)
  {
    rate += writeCoeff8x8 (currMB, PLANE_Y, block, mode, currMB->luma_transform_size_8x8_flag);
  }
  if( active_sps->chroma_format_idc == YUV444 && !IS_INDEPENDENT(input) )
  {
    rate += writeCoeff8x8( currMB, PLANE_U, block, mode, currMB->luma_transform_size_8x8_flag );
    rate += writeCoeff8x8( currMB, PLANE_V, block, mode, currMB->luma_transform_size_8x8_flag );
  }

  return (double)distortion + lambda * (double)rate;
}


/*!
 *************************************************************************************
 * \brief
 *    Gets mode offset for intra16x16 mode
 *************************************************************************************
 */
int I16Offset (int cbp, int i16mode)
{
  return (cbp&15?13:1) + i16mode + ((cbp&0x30)>>2);
}


/*!
 *************************************************************************************
 * \brief
 *    Sets modes and reference frames for a macroblock
 *************************************************************************************
 */
void SetModesAndRefframeForBlocks (Macroblock *currMB, int mode)
{
  int i,j,k,l;
  int  bframe  = (img->type==B_SLICE);
  int  block_x, block_y;
  int  cur_ref;
  int  clist;
  char refl0, refl1;

  //--- macroblock type ---
  currMB->mb_type = mode;
  currMB->bi_pred_me= (mode == 1 ? img->bi_pred_me[mode] : 0);

  //--- block 8x8 mode and prediction direction ---
  switch (mode)
  {
  case 0:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] = 0;
      currMB->b8pdir[i] = (bframe ? direct_pdir[img->block_y + ((i >> 1)<<1)][img->block_x + ((i & 0x01)<<1)] : 0);
    }
    break;
  case 1:
  case 2:
  case 3:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] = mode;
      currMB->b8pdir[i] = best8x8pdir[mode][i];
    }
    break;
  case P8x8:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i]   = best8x8mode[i];
      currMB->b8pdir[i]   = best8x8pdir[mode][i];
    }
    break;
  case I4MB:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] = IBLOCK;
      currMB->b8pdir[i] = -1;
    }
    break;
  case I16MB:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] =  0;
      currMB->b8pdir[i] = -1;
    }
    break;
  case I8MB:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] = I8MB;
      currMB->b8pdir[i] = -1;
    }
    //switch to 8x8 transform
    currMB->luma_transform_size_8x8_flag = 1;
    break;
  case IPCM:
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i] = IPCM;
      currMB->b8pdir[i] = -1;
    }
    currMB->luma_transform_size_8x8_flag = 0;
    break;
  default:
    printf ("Unsupported mode in SetModesAndRefframeForBlocks!\n");
    exit (1);
  }

#define IS_FW ((best8x8pdir[mode][k]==0 || best8x8pdir[mode][k]==2) && (mode!=P8x8 || best8x8mode[k]!=0 || !bframe))
#define IS_BW ((best8x8pdir[mode][k]==1 || best8x8pdir[mode][k]==2) && (mode!=P8x8 || best8x8mode[k]!=0))
  //--- reference frame arrays ---
  if (mode==0 || mode==I4MB || mode==I16MB || mode==I8MB)
  {
    if (bframe)
    {
      if (!mode) // Direct
      {
        for (clist = LIST_0; clist <= LIST_1; clist++)
        {
          for (j = img->block_y; j < img->block_y + 4; j++)
          {
            memcpy(&enc_picture->ref_idx[clist][j][img->block_x], &direct_ref_idx[clist][j][img->block_x], 4 * sizeof(char));
          }
        }
      }
      else // Intra
      {
        for (clist = LIST_0; clist <= LIST_1; clist++)
        {
          for (j = img->block_y; j < img->block_y + 4; j++)
          {
            memset(&enc_picture->ref_idx[clist][j][img->block_x],-1, 4 * sizeof(char));
          }
        }
      }
    }
    else
    {
      if (!mode) // Skip
      {
        for (j = img->block_y; j < img->block_y + 4; j++)
          memset(&enc_picture->ref_idx[LIST_0][j][img->block_x],0, 4 * sizeof(char));
      }
      else // Intra
      {
        for (j = img->block_y; j < img->block_y + 4; j++)
          memset(&enc_picture->ref_idx[LIST_0][j][img->block_x],-1, 4 * sizeof(char));
      }
    }
  }
  else
  {
    if (bframe)
    {
      if (mode == 1)
      {
        if (currMB->bi_pred_me && best8x8pdir[mode][0] == 2)
        {
          for (j = img->block_y; j < img->block_y + 4;j++)
          {          
            memset(&enc_picture->ref_idx[LIST_0][j][img->block_x], 0, 4 * sizeof(char));
            memset(&enc_picture->ref_idx[LIST_1][j][img->block_x], 0, 4 * sizeof(char));
          }
        }
        else
        {
          refl0 = (best8x8pdir[mode][0] == 0 || best8x8pdir[mode][0] == 2) ? best8x8l0ref[mode][0] : -1;
          refl1 = (best8x8pdir[mode][0] == 1 || best8x8pdir[mode][0] == 2) ? best8x8l1ref[mode][0] : -1;
          for (j = img->block_y; j < img->block_y + 4;j++)
          {          
            memset(&enc_picture->ref_idx[LIST_0][j][img->block_x], refl0, 4 * sizeof(char));
            memset(&enc_picture->ref_idx[LIST_1][j][img->block_x], refl1, 4 * sizeof(char));
          }
        }
      }
      else if (mode == 2)
      {
        j = img->block_y;
        refl0 = (best8x8pdir[mode][0] == 0 || best8x8pdir[mode][0] == 2) ? best8x8l0ref[mode][0] : -1;
        refl1 = (best8x8pdir[mode][0] == 1 || best8x8pdir[mode][0] == 2) ? best8x8l1ref[mode][0] : -1;        
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_1][j++][img->block_x], refl1, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_1][j++][img->block_x], refl1, 4 * sizeof(char));
        
        refl0 = (best8x8pdir[mode][2] == 0 || best8x8pdir[mode][2] == 2) ? best8x8l0ref[mode][2] : -1;
        refl1 = (best8x8pdir[mode][2] == 1 || best8x8pdir[mode][2] == 2) ? best8x8l1ref[mode][2] : -1;
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_1][j++][img->block_x], refl1, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_1][j++][img->block_x], refl1, 4 * sizeof(char));
      }
      else if (mode == 3)
      {
        j = img->block_y;
        i = img->block_x;
        refl0 = (best8x8pdir[mode][0] == 0 || best8x8pdir[mode][0] == 2) ? best8x8l0ref[mode][0] : -1;
        refl1 = (best8x8pdir[mode][0] == 1 || best8x8pdir[mode][0] == 2) ? best8x8l1ref[mode][0] : -1;
        enc_picture->ref_idx[LIST_0][j  ][i  ] = refl0;
        enc_picture->ref_idx[LIST_1][j  ][i++] = refl1;
        enc_picture->ref_idx[LIST_0][j  ][i  ] = refl0;
        enc_picture->ref_idx[LIST_1][j  ][i++] = refl1;
        refl0 = (best8x8pdir[mode][1] == 0 || best8x8pdir[mode][1] == 2) ? best8x8l0ref[mode][1] : -1;
        refl1 = (best8x8pdir[mode][1] == 1 || best8x8pdir[mode][1] == 2) ? best8x8l1ref[mode][1] : -1;
        enc_picture->ref_idx[LIST_0][j  ][i  ] = refl0;
        enc_picture->ref_idx[LIST_1][j  ][i++] = refl1;
        enc_picture->ref_idx[LIST_0][j  ][i  ] = refl0;
        enc_picture->ref_idx[LIST_1][j++][i  ] = refl1;
        memcpy(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_1][j++][img->block_x], &enc_picture->ref_idx[LIST_1][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_1][j++][img->block_x], &enc_picture->ref_idx[LIST_1][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_1][j++][img->block_x], &enc_picture->ref_idx[LIST_1][img->block_y][img->block_x], 4 * sizeof(char));
      }      
      else
      {
        for (j=0;j<4;j++)
        {
          block_y = img->block_y + j;
          for (i=0;i<4;i++)
          {
            block_x = img->block_x + i;
            k = 2*(j >> 1) + (i >> 1);
            l = 2*(j & 0x01) + (i & 0x01);

            if(mode == P8x8 && best8x8mode[k]==0)
            {
              enc_picture->ref_idx[LIST_0][block_y][block_x] = direct_ref_idx[LIST_0][block_y][block_x];
              enc_picture->ref_idx[LIST_1][block_y][block_x] = direct_ref_idx[LIST_1][block_y][block_x];
            }
            else
            {
              enc_picture->ref_idx[LIST_0][block_y][block_x] = (IS_FW ? best8x8l0ref[mode][k] : -1);
              enc_picture->ref_idx[LIST_1][block_y][block_x] = (IS_BW ? best8x8l1ref[mode][k] : -1);
            }
          }        
        }
      }
    }
    else
    {
      if (mode == 1)
      {
        char refl0 = best8x8pdir[mode][0] == 0 ? best8x8l0ref[mode][0] : -1;
        j = img->block_y;
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
      }
      else if (mode == 2)
      {
        refl0 = best8x8pdir[mode][0] == 0 ? best8x8l0ref[mode][0] : -1;
        j = img->block_y;
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        refl0 = best8x8pdir[mode][2] == 0 ? best8x8l0ref[mode][2] : -1;
        memset(&enc_picture->ref_idx[LIST_0][j++][img->block_x], refl0, 4 * sizeof(char));
        memset(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], refl0, 4 * sizeof(char));
      }      
      else if (mode == 3)
      {
        j = img->block_y;
        i = img->block_x;
        refl0 = (best8x8pdir[mode][0] == 0) ? best8x8l0ref[mode][0] : -1;
        enc_picture->ref_idx[LIST_0][j  ][i++] = refl0;
        enc_picture->ref_idx[LIST_0][j  ][i++] = refl0;
        refl0 = (best8x8pdir[mode][1] == 0) ? best8x8l0ref[mode][1] : -1;
        enc_picture->ref_idx[LIST_0][j  ][i++] = refl0;
        enc_picture->ref_idx[LIST_0][j++][i  ] = refl0;
        memcpy(&enc_picture->ref_idx[LIST_0][j++][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_0][j++][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
        memcpy(&enc_picture->ref_idx[LIST_0][j  ][img->block_x], &enc_picture->ref_idx[LIST_0][img->block_y][img->block_x], 4 * sizeof(char));
      }      
      else
      {
        for (j=0;j<4;j++)
        {
          block_y = img->block_y + j;
          for (i=0;i<4;i++)
          {
            block_x = img->block_x + i;
            k = 2*(j >> 1) + (i >> 1);
            l = 2*(j & 0x01) + (i & 0x01);
            enc_picture->ref_idx[LIST_0][block_y][block_x] = (IS_FW ? best8x8l0ref[mode][k] : -1);
          }
        }
      }
    }
  }

  if (bframe)
  {
    for (clist = LIST_0; clist <= LIST_1; clist++)

⌨️ 快捷键说明

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