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

📄 mode_decision.c

📁 H264视频编解码程序
💻 C
📖 第 1 页 / 共 5 页
字号:
        //-----------------------------------------------------------
        PartitionMotionSearch (mode, block, enc_mb.lambda_mf);
        
        //--- get cost and reference frame for LIST 0 prediction ---
        bmcost[LIST_0] = INT_MAX;
        list_prediction_cost(LIST_0, block, mode, enc_mb, bmcost, best_ref);
        
        //store LIST 0 reference index for every block
        block_x = img->block_x+(block&1)*2;
        block_y = img->block_y+(block&2);
        for (j = block_y; j< block_y + 2; j++)
        {
          for (i = block_x; i < block_x + 2; i++)
          {
            enc_picture->ref_idx   [LIST_0][j][i] = best_ref[LIST_0];
            enc_picture->ref_pic_id[LIST_0][j][i] = 
              enc_picture->ref_pic_num[enc_mb.list_offset[LIST_0]][(short)best_ref[LIST_0]];
          }
        } 
        
        if (bslice)
        {
          //--- get cost and reference frame for LIST 1 prediction ---
          bmcost[LIST_1] = INT_MAX;
          bmcost[BI_PRED] = INT_MAX;
          list_prediction_cost(LIST_1, block, mode, enc_mb, bmcost, best_ref);
          
          // Compute bipredictive cost between best list 0 and best list 1 references
          list_prediction_cost(BI_PRED, block, mode, enc_mb, bmcost, best_ref);
          
          //--- get prediction direction ----          
          determine_prediction_list(mode, bmcost, best_ref, &best_pdir, cost, &bi_pred_me);
          
          //store backward reference index for every block
          for (j = block_y; j< block_y + 2; j++)
          {
            for (i = block_x; i < block_x + 2; i++)
            {
              enc_picture->ref_idx[LIST_0][j][i] = best_ref[LIST_0];
              enc_picture->ref_idx[LIST_1][j][i] = best_ref[LIST_1];
            }
          }
        } // if (bslice)
        else
        {
          best_pdir = 0;
          *cost     = bmcost[LIST_0];
        }
      } // if (mode!=0)
      
      if (input->rdopt)
      {
        //--- get and check rate-distortion cost ---
        rdcost = RDCost_for_8x8blocks (&cnt_nonz, &curr_cbp_blk, enc_mb.lambda_md,
          block, mode, best_pdir, best_ref[LIST_0], best_ref[LIST_1]);
      }
      else
      {
        if (*cost!=INT_MAX)
          *cost += (REF_COST (enc_mb.lambda_mf, B8Mode2Value (mode, best_pdir),
          enc_mb.list_offset[(best_pdir<1?LIST_0:LIST_1)]) - 1);
      }
      
      //--- set variables if best mode has changed ---
      if ( ( input->rdopt && rdcost < min_rdcost) 
        || (!input->rdopt && *cost < min_cost8x8))
      {
        min_cost8x8                 = *cost;
        min_rdcost                  = rdcost;
        dataTr->part8x8mode [block] = mode;
        dataTr->part8x8pdir [block] = best_pdir;
        dataTr->part8x8fwref[block] = best_ref[LIST_0];
        dataTr->part8x8bwref[block] = best_ref[LIST_1];
        
        img->mb_data[img->current_mb_nr].b8mode[block] = mode;
        
#ifdef BEST_NZ_COEFF
        for(j = 0; j <= 1; j++)
        {
          for(i = 0; i <= 1; i++)
            best_nz_coeff[i][j]= cnt_nonz ? img->nz_coeff[img->current_mb_nr][i1 + i][j1 + j] : 0;
        }
#endif
        
        //--- store number of nonzero coefficients ---
        best_cnt_nonz  = cnt_nonz;
        
        if (input->rdopt)
        {
          //--- store block cbp ---
          cbp_blk8x8    &= (~(0x33 << (((block>>1)<<3)+((block%2)<<1)))); // delete bits for block
          cbp_blk8x8    |= curr_cbp_blk;
          
          //--- store coefficients ---
          for (k=0; k< 4; k++)
          {
            for (j=0; j< 2; j++)
              for (i=0; i<65; i++)  
                cofACtr[k][j][i] = img->cofAC[block][k][j][i]; // 18->65 for ABT
          }   
          //--- store reconstruction and prediction --- 
          if(!img->residue_transform_flag)
          {
            for (j=j0; j<j0+8; j++)    
            {
              pix_y = img->pix_y + j;
              for (i=i0; i<i0+8; i++)
              {
                pix_x = img->pix_x + i;
                dataTr->rec_mbY8x8[j][i] = enc_picture->imgY[pix_y][pix_x];
                dataTr->mpr8x8[j][i] = img->mpr[j][i];
                if(img->type==SP_SLICE && (!si_frame_indicator))
                  dataTr->lrec[j][i]=lrec[pix_y][pix_x]; // store the coefficients for primary SP slice
              }     
            }
          }
          else
          {
            for (j=j0; j<j0+8; j++)                
              for (i=i0; i<i0+8; i++)
              {
                dataTr->rec_resG_8x8  [j][i] = rec_resG  [j][i];
                dataTr->resTrans_R_8x8[j][i] = resTrans_R[j][i];
                dataTr->resTrans_B_8x8[j][i] = resTrans_B[j][i];
                dataTr->mprRGB_8x8 [0][j][i] = mprRGB [0][j][i];
                dataTr->mprRGB_8x8 [1][j][i] = mprRGB [1][j][i];
                dataTr->mprRGB_8x8 [2][j][i] = mprRGB [2][j][i];                  
              }     
          }
        }                  
        if (img->AdaptiveRounding)
        {
          for (j=j0; j<j0+8; j++)
            for (i=i0; i<i0+8; i++)                    
            {
              fadjust     [j][i] = fadjustTransform  [0]   [j][i];
              fadjustCr[0][j][i] = fadjustTransformCr[0][0][j][i];
              fadjustCr[1][j][i] = fadjustTransformCr[0][1][j][i];
            }
        }
        //--- store best 8x8 coding state ---
        if (block < 3)
        store_coding_state (cs_b8);
      } // if (rdcost <= min_rdcost)
      
      //--- re-set coding state as it was before coding with current mode was performed ---

      reset_coding_state (cs_cm);
    } // if ((enc_mb.valid[mode] && (transform8x8 == 0 || mode != 0 || (mode == 0 && active_sps->direct_8x8_inference_flag)))
  } // for (min_rdcost=1e30, index=(bslice?0:1); index<6; index++)  
  
#ifdef BEST_NZ_COEFF
  for(j = 0; j <= 1; j++)
  {
    for(i = 0; i <= 1; i++)
      img->nz_coeff[img->current_mb_nr][i1 + i][j1 + j] = best_nz_coeff[i][j];
  }
#endif              
  
  if (!transform8x8)
    dataTr->cost8x8 += min_cost8x8;
  
  if (!input->rdopt)
  {
    if (transform8x8)
    {
      dataTr->cost8x8 += min_cost8x8;              
      mode = dataTr->part8x8mode[block];
      pdir = dataTr->part8x8pdir[block];
    }
    else
    {
      mode = dataTr->part8x8mode[block];
      pdir = dataTr->part8x8pdir[block];    
    }
    curr_cbp_blk  = 0;
    best_cnt_nonz = LumaResidualCoding8x8 (&dummy, &curr_cbp_blk, block, pdir,
      (pdir==0||pdir==2?mode:0), (pdir==1||pdir==2?mode:0), dataTr->part8x8fwref[block], dataTr->part8x8bwref[block]);    
    
    cbp_blk8x8   &= (~(0x33 << (((block>>1)<<3)+((block%2)<<1)))); // delete bits for block
    cbp_blk8x8   |= curr_cbp_blk;
    
    //--- store coefficients ---
    for (k=0; k< 4; k++)
    {
        for (j=0; j< 2; j++)
          memcpy(cofACtr[k][j],img->cofAC[block][k][j],65 * sizeof(int));
    } 
    
    //--- store reconstruction and prediction ---
    if(!img->residue_transform_flag) // Residue Color Transform
    {
      for (j=j0; j<j0+2* BLOCK_SIZE; j++)
        {       
        memcpy(&dataTr->rec_mbY8x8[j][i0], &enc_picture->imgY[img->pix_y + j][img->pix_x + i0], 2* BLOCK_SIZE * sizeof (imgpel));
        memcpy(&dataTr->mpr8x8[j][i0], &img->mpr[j][i0], 2* BLOCK_SIZE * sizeof (imgpel));
        if(img->type==SP_SLICE &&(!si_frame_indicator))
          memcpy(&dataTr->lrec[j][i0],&lrec[img->pix_y+j][img->pix_x+i0],2*BLOCK_SIZE*sizeof(int)); // store coefficients for primary SP slice
      }
    }
    else 
    {   
      for (j=j0; j<j0+8; j++)
        for (i=i0; i<i0+8; i++)
        { 
          dataTr->rec_resG_8x8  [j][i] = rec_resG  [j][i];
          dataTr->resTrans_R_8x8[j][i] = resTrans_R[j][i];
          dataTr->resTrans_B_8x8[j][i] = resTrans_B[j][i];          
          dataTr->mprRGB_8x8 [0][j][i] = mprRGB [0][j][i];
          dataTr->mprRGB_8x8 [1][j][i] = mprRGB [1][j][i];
          dataTr->mprRGB_8x8 [2][j][i] = mprRGB [2][j][i];
        }
    }              
  }
  
  //----- set cbp and count of nonzero coefficients ---
  if (best_cnt_nonz)
  {
    cbp8x8       |= (1 << block);
    cnt_nonz_8x8 += best_cnt_nonz;
  }
  
  if (!transform8x8)
  {
    if (block<3)
    {
      //===== re-set reconstructed block =====
      j0   = 8*(block/2);
      i0   = 8*(block%2);
      for (j=j0; j<j0 + 2 * BLOCK_SIZE; j++)
        {
        memcpy(&enc_picture->imgY[img->pix_y + j][img->pix_x], dataTr->rec_mbY8x8[j], 2 * BLOCK_SIZE * sizeof(imgpel));
        if(img->type==SP_SLICE &&(!si_frame_indicator))
          memcpy(&lrec[img->pix_y + j][img->pix_x], dataTr->lrec[j],2*BLOCK_SIZE*sizeof(imgpel)); // reset the coefficients for SP slice
      }
    } // if (block<3)    
  }  
  else
  {  
    //======= save motion data for 8x8 partition for transform size 8x8 ========
    StoreNewMotionVectorsBlock8x8(0, block, dataTr->part8x8mode[block], dataTr->part8x8fwref[block], dataTr->part8x8bwref[block], dataTr->part8x8pdir[block], bslice);
  }  
  //===== set motion vectors and reference frames (prediction) =====
  SetRefAndMotionVectors (block, dataTr->part8x8mode[block], dataTr->part8x8pdir[block], dataTr->part8x8fwref[block], dataTr->part8x8bwref[block]);
  
  //===== set the coding state after current block =====
  //if (transform8x8 == 0 || block < 3)
  if (block < 3)
    reset_coding_state (cs_b8);
  
  if (img->AdaptiveRounding)
  {
    for (j=j0; j<j0+2 * BLOCK_SIZE; j++)
      {
        memcpy(&fadjustTransform  [lumaAdjustIndex  ]   [j][i0], &fadjust     [j][i0], 2 * BLOCK_SIZE * sizeof(int));
        memcpy(&fadjustTransformCr[chromaAdjustIndex][0][j][i0], &fadjustCr[0][j][i0], 2 * BLOCK_SIZE * sizeof(int));
        memcpy(&fadjustTransformCr[chromaAdjustIndex][1][j][i0], &fadjustCr[1][j][i0], 2 * BLOCK_SIZE * sizeof(int));
      }
  }
}

/*!
*************************************************************************************
* \brief
*    Mode Decision for a macroblock
*************************************************************************************
*/
void encode_one_macroblock ()
{  
  int max_index;
  
  int         rerun, block, index, mode, i, j, k, ctr16x16, dummy;
  short       best_pdir;
  RD_PARAMS   enc_mb;
  double      min_rdcost, max_rdcost=1e30;
  char        best_ref[2] = {0, -1};
  int         bmcost[5] = {INT_MAX};
  int         cost=0;
  int         min_cost = INT_MAX, cost_direct=0, have_direct=0, i16mode=0;
  int         intra1 = 0;
  int         temp_cpb = 0;
  int         best_transform_flag = 0;
  int         cost8x8_direct = 0;  
  short       islice      = (img->type==I_SLICE);
  short       bslice      = (img->type==B_SLICE);
  short       pslice      = (img->type==P_SLICE) || (img->type==SP_SLICE);
  short       intra       = (islice || (pslice && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra));
  
  short       runs        = (input->RestrictRef==1 && input->rdopt==3 && (pslice  || (bslice && img->nal_reference_idc>0)) ? 2 : 1);
  
  int         pix_x, pix_y;
  Macroblock* currMB      = &img->mb_data[img->current_mb_nr];
  int         prev_mb_nr  = FmoGetPreviousMBNr(img->current_mb_nr);
  Macroblock* prevMB      = (prev_mb_nr >= 0) ? &img->mb_data[prev_mb_nr]:NULL ;
  
  char   **ipredmodes = img->ipredmode;
  short   *allmvs = img->all_mv[0][0][0][0][0];
  short   max_chroma_pred_mode;
  int     ****i4p;  //for non-RD-opt. mode
  
  int tmp_8x8_flag, tmp_no_mbpart;  
  // Residue Color Transform
  int residue_R, residue_G, residue_B, temp;
  int cr_cbp = 0;  
  // Fast Mode Decision
  short inter_skip = 0, intra_skip = 0;
  int cost16 = 0, mode16 = 0;
  double min_rate = 0, RDCost16 = DBL_MAX;
  
  
  if(input->FMEnable == 1)
  {
    decide_intrabk_SAD();
  }
  else if (input->FMEnable ==2)
  {
    simplified_decide_intrabk_SAD();
  }
  
  intra |= RandomIntra (img->current_mb_nr);    // Forced Pseudo-Random Intra
  
  //===== Setup Macroblock encoding parameters =====
  init_enc_mb_params(currMB, &enc_mb, intra, bslice);
  
  // Perform multiple encodings if rdopt with losses is enabled
  for (rerun=0; rerun<runs; rerun++)
  {
    if (runs==2)
      input->rdopt= (rerun==0) ? 1 : 3;
    
    // reset chroma intra predictor to default
    currMB->c_ipred_mode = DC_PRED_8;

    //=====   S T O R E   C O D I N G   S T A T E   =====
    //---------------------------------------------------
    store_coding_state (cs_cm);
    
    if (!intra)
    {
      //===== set direct motion vectors =====
      best_mode = 1;
      if (bslice)
      {
        Get_Direct_Motion_Vectors ();
        if (input->rdopt == 2 && enc_mb.valid[0])
        {
          best_mode = 0;
          currMB->c_ipred_mode=DC_PRED_8;
          min_rdcost = max_rdcost;
          compute_mode_RD_cost(0, currMB, enc_mb, &min_rdcost, &min_rate, i16mode, bslice, &inter_skip);
        }
      }
      
      //===== MOTION ESTIMATION FOR 16x16, 16x8, 8x16 BLOCKS =====      
      for (min_cost=INT_MAX, mode=1; mode<4; mode++)
      {
        bi_pred_me = 0;
        img->bi_pred_me[mode]=0;

⌨️ 快捷键说明

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