Presentation is loading. Please wait.

Presentation is loading. Please wait.

H.264/AVC Reference Software Group 3 11 簡嘉宏 9762530 2009/05/22.

Similar presentations


Presentation on theme: "H.264/AVC Reference Software Group 3 11 簡嘉宏 9762530 2009/05/22."— Presentation transcript:

1 H.264/AVC Reference Software Group 3 11 簡嘉宏 9762530 2009/05/22

2 Over View Encode_one_macrob lock_high (2) Compute_mode_RD_cost PartitionMotionSearch submacroblock_mode_decision (2) Rdcost_for_macroblocks SetModesAndRefframeForBlock s store_macroblock_parameters init_enc_mb_params Get_Direct_Motion_Vectors BlockMotionSearch RDCost_for_8x8blocks IntraChromaPrediction set_stored_macroblock_paramet ers Encode_one_ma croblock

3 Call Graph Encode_one_macrob lock_high (2) Compute_mode_RD_cost PartitionMotionSearch submacroblock_mode_decision (2) Rdcost_for_macroblocks SetModesAndRefframeForBlock s store_macroblock_parameters RDCost_for_8x8blocks IntraChromaPrediction Encode_one_ma croblock Mode Decision for a macroblock Mode Decision for an 8x8 sub-macroblock Precompute all new chroma intra prediction modes RD decision process

4 Call Graph Encode_one_macrob lock_high (2) Compute_mode_RD_cost PartitionMotionSearch submacroblock_mode_decision (2) Rdcost_for_macroblocks SetModesAndRefframeForBlock s store_macroblock_parameters RDCost_for_8x8blocks IntraChromaPrediction Encode_one_ma croblock Mode Decision for a macroblock Mode Decision for an 8x8 sub-macroblock Precompute all new chroma intra prediction modes RD decision process

5 submacroblock_mode_decision Location: lencod\src\Mode_decision.c Brief: Mode Decision for an 8x8 sub-macroblock Arguments: – RD_PARAMS enc_mb// MB Level parameters, include MD/ME // lambda, best reference and mcost – RD_8x8DATA *dataTr//8x8 sub-macroblock data – Macroblock *currMB// 目前 Macroblock 的 pointer – int*** cofACtr//8x8macroblock coefficients – int *have_direct// 統計 direct mode submacroblock 的數目 – short bslice// 是否是 Bslice 編碼 – int block// 目前編碼的 block – int *cost_direct// 目前編碼的 macroblock 的 direct mode 的最 // 小 cost – int *cost// 目前編碼的 macroblock 的最小 cost – int *cost8x8_direct// 累計 8x8 direct mode 的最小 cost – int transform8x8// 是否使用 8x8size transform mode

6 Work Flow  Loop over possible coding modes for 8x8 sub-partition Modes: direct 8x8, inter 8x8, inter 8x4, inter 4x8, inter 4x4  Direct Mode  GetDirectCost8x8, DirectCost4x4  Compute cost and best reference  Not Direct Mode  Motion estimation for all reference frames  Get cost and reference frame for LIST0 prediction, and store  If bslice, compute LIST1 prediction  RDCost_for_8x8blocks  若此 loop 產生出最小的 cost 或 rdcost, 則更新各項參數  Loop 結束, 更新各項參數  儲存 motion data, motion vector, reference frame

7 Loop  Direct mode  Not Direct mode  RDCost_for_8x8blocks  若此 loop 產生出最小的 cost 或 rdcost, 則更新各項參數 //===== LOOP OVER POSSIBLE CODING MODES FOR 8x8 SUB-PARTITION ===== for (min_cost8x8=INT_MAX, min_rdcost=1e30, index=(bslice?0:1); index<maxindex; index++) { mode = b8_mode_table[index]; *cost = 0; if (enc_mb.valid[mode] && (transform8x8 == 0 || mode != 0 || (mode == 0 && active_sps->direct_8x8_inference_flag))) {…… const int b8_mode_table[6] = {0, 4, 5, 6, 7}; Mode #Mode Name 0Direct 8x8 4Inter 8x8 5Inter 8x4 6Inter 4x8 7Inter 4x4

8 Direct Mode if (mode==0) { //--- Direct Mode --- if (!input->rdopt ) { direct4x4_tmp = 0; direct8x8_tmp = 0; direct4x4_tmp = GetDirectCost8x8 ( block, &direct8x8_tmp); if ((direct4x4_tmp==INT_MAX)||(*cost_direct==INT_MAX)) { *cost_direct = INT_MAX; if (transform8x8) *cost8x8_direct = INT_MAX; } else { *cost_direct += direct4x4_tmp; if (transform8x8) *cost8x8_direct += direct8x8_tmp; } (*have_direct) ++; block_x = img->block_x+(block&1)*2; block_y = img->block_y+(block&2); best_ref[LIST_0] = direct_ref_idx[LIST_0][block_y][block_x]; best_ref[LIST_1] = direct_ref_idx[LIST_1][block_y][block_x]; best_pdir = direct_pdir[block_y][block_x]; } // if (mode==0) if (transform8x8) { switch(input->Transform8x8Mode) { case 1: // Mixture of 8x8 & 4x4 transform if((direct8x8_tmp < direct4x4_tmp) || !(enc_mb.valid[5] && enc_mb.valid[6] && enc_mb.valid[7])) *cost = direct8x8_tmp; else *cost = direct4x4_tmp; break; case 2: // 8x8 Transform only *cost = direct8x8_tmp; break; default: // 4x4 Transform only *cost = direct4x4_tmp; break; } if (input->Transform8x8Mode==2) *cost = INT_MAX; } else { *cost = direct4x4_tmp; }

9 Not Direct Mode  Inter mode  Motion estimation for all reference frames  Get cost and reference frame for LIST0 prediction, and store  If bslice, compute LIST1 prediction //--- 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); …. } // if (bslice)  Get cost and reference frame for LIST1 prediction  Compute bipredictive cost between best list 0 and best list 1 references  Get prediction direction  Store backward reference index for every block

10 RDCost_for_8x8blocks  Loop over possible coding modes for 8x8 sub-partition Modes: direct 8x8, inter 8x8, inter 8x4, inter 4x8, inter 4x4  Direct Mode  GetDirectCost8x8, DirectCost4x4  Compute cost and best reference  Not Direct Mode  Motion estimation for all reference frames  Get cost and reference frame for LIST0 prediction, and store  If bslice, compute LIST1 prediction  RDCost_for_8x8blocks  若此 loop 產生出最小的 cost 或 rdcost, 則更新各項參數  Loop 結束, 更新各項參數  儲存 motion data, motion vector, reference frame RDO – Rate-distortion optimization If Enable RDO Compute RD cost Else Compute weighted cost 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[Q_PEL], B8Mode2Value (mode, best_pdir), enc_mb.list_offset[(best_pdir<1?LIST_0:LIST_1)]) - 1); }

11 Loop 結束  Store coefficients  Store reconstruction and prediction  Set cbp and count of nonzero coefficients  Save motion data for 8x8 partition for transform size 8x8  Set motion vectors and reference frames (prediction)  Set the coding state after current block if (!transform8x8) { … } 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]);

12 Call Graph Encode_one_macrob lock_high (2) Compute_mode_RD_cost PartitionMotionSearch submacroblock_mode_decision (2) Rdcost_for_macroblocks SetModesAndRefframeForBlock s store_macroblock_parameters RDCost_for_8x8blocks IntraChromaPrediction Encode_one_ma croblock Mode Decision for a macroblock Mode Decision for an 8x8 sub-macroblock Precompute all new chroma intra prediction modes RD decision process

13 IntraChromaPrediction  Location: lencod\src\Macroblock.c  Brief: Intra prediction of the chrominance layers of one macroblock  Arguments:  int *mb_up// 上方的 macroblock  int *mb_left// 左方的 macroblock  int *mb_up_left// 左上方的 macroblock

14 Work Flow 取得上方, 左方以及左上方的 macroblock 的 address 計算所有 chroma intra prediction mode – DC prediction – Vertical prediction – Horizontal prediction – Plane prediction Loop 所有的 prediction mode, 計算出具有最小 SAD 的 prediction mode 設定 macroblock 的 chroma intra prediction mode

15 Work Flow 取得上方, 左方以及左上方的 macroblock 的 address 計算所有 chroma intra prediction mode – DC prediction – Vertical prediction – Horizontal prediction – Plane prediction Loop 所有的 prediction mode, 計算出具有最小 SAD 的 prediction mode 設定 macroblock 的 chroma intra prediction mode

16 Macroblock Address  取得上方, 左方以及左上方的 macroblock 的 address for (i=0;i<cr_MB_y+1;i++) getNeighbour(mb_nr, -1, i-1, IS_CHROMA, &left[i]); getNeighbour(mb_nr, 0, -1, IS_CHROMA, &up); mb_available_up = up.available; mb_available_up_left = left[0].available; mb_available_left[0] = mb_available_left[1] = left[1].available; if(input->UseConstrainedIntraPred) { mb_available_up = up.available ? img->intra_block[up.mb_addr] : 0; for (i=0, mb_available_left[0]=1; i >1);i++) mb_available_left[0] &= left[i+1].available ? img->intra_block[left[i+1].mb_addr]: 0; for (i=(cr_MB_y>>1), mb_available_left[1]=1; i<cr_MB_y;i++) mb_available_left[1] &= left[i+1].available ? img->intra_block[left[i+1].mb_addr]: 0; mb_available_up_left = left[0].available ? img->intra_block[left[0].mb_addr]: 0; } if (mb_up) *mb_up = mb_available_up; if (mb_left) *mb_left = mb_available_left[0] && mb_available_left[1]; if (mb_up_left) *mb_up_left = mb_available_up_left;

17 Work Flow 取得上方, 左方以及左上方的 macroblock 的 address 計算所有 chroma intra prediction mode – DC prediction – Vertical prediction – Horizontal prediction – Plane prediction Loop 所有的 prediction mode, 計算出具有最小 SAD 的 prediction mode 設定 macroblock 的 chroma intra prediction mode

18 DC Prediction  Get prediction value //===== get prediction value ===== switch (block_pos[yuv][b8][b4]) { case 0: //===== TOP LEFT ===== if (mb_available_up) for (i=blk_x;i<(blk_x+4);i++) s0 += image[up.pos_y][up.pos_x + i]; if (mb_available_left[0]) for (i=blk_y;i<(blk_y+4);i++) s2 += image[left[i].pos_y][left[i].pos_x]; if (mb_available_up && mb_available_left[0]) s = (s0+s2+4) >> 3; else if (mb_available_up) s = (s0 +2) >> 2; else if (mb_available_left[0]) s = (s2 +2) >> 2; break; case 1: //===== TOP RIGHT ===== if (mb_available_up) for (i=blk_x;i<(blk_x+4);i++) s1 += image[up.pos_y][up.pos_x + i]; else if (mb_available_left[0]) for (i=blk_y;i<(blk_y+4);i++) s2 += image[left[i].pos_y][left[i].pos_x]; if (mb_available_up) s = (s1 +2) >> 2; else if (mb_available_left[0]) s = (s2 +2) >> 2; break; case 2: //===== BOTTOM LEFT ===== … case 3: //===== BOTTOM RIGHT ===== … }

19 Vertical Prediction // vertical prediction if (mb_available_up) { memcpy(hline,&image[up.pos_y][up.pos_x], cr_MB_x * sizeof(imgpel)); for (j=0; j<cr_MB_y; j++) memcpy(img->mprr_c[uv][VERT_PRED_8][j], hline, cr_MB_x * sizeof(imgpel)); }

20 Horizontal Prediction // horizontal prediction if (mb_available_left[0] && mb_available_left[1]) { for (i=0; i<cr_MB_y; i++) vline[i] = image[left[i+1].pos_y][left[i+1].pos_x]; for (i=0; i<cr_MB_x; i++) for (j=0; j<cr_MB_y; j++) img->mprr_c[uv][HOR_PRED_8][j][i] = vline[j]; }

21 Plane Prediction // plane prediction if (mb_available_left[0] && mb_available_left[1] && mb_available_up && mb_available_up_left) { ih = (cr_MB_x>>1)*(hline[cr_MB_x-1] - image[left[0].pos_y][left[0].pos_x]); for (i=0;i >1)-1;i++) ih += (i+1)*(hline[(cr_MB_x>>1)+i] - hline[(cr_MB_x>>1)-2-i]); iv = (cr_MB_y>>1)*(vline[cr_MB_y-1] - image[left[0].pos_y][left[0].pos_x]); for (i=0;i >1)-1;i++) iv += (i+1)*(vline[(cr_MB_y>>1)+i] - vline[(cr_MB_y>>1)-2-i]); ib= ((cr_MB_x == 8?17:5)*ih+2*cr_MB_x)>>(cr_MB_x == 8?5:6); ic= ((cr_MB_y == 8?17:5)*iv+2*cr_MB_y)>>(cr_MB_y == 8?5:6); iaa=16*(hline[cr_MB_x-1]+vline[cr_MB_y-1]); for (j=0; j<cr_MB_y; j++) for (i=0; i<cr_MB_x; i++) img->mprr_c[uv][PLANE_8][j][i]= iClip3(0, img->max_imgpel_value_uv, (iaa+(i-(cr_MB_x>>1)+1)*ib+(j-(cr_MB_y>>1)+1)*ic+16)>>5); }

22 Work Flow 取得上方, 左方以及左上方的 macroblock 的 address 計算所有 chroma intra prediction mode – DC prediction – Vertical prediction – Horizontal prediction – Plane prediction Loop 所有的 prediction mode, 計算出具有最小 SAD 的 prediction mode 設定 macroblock 的 chroma intra prediction mode

23 最小 SAD 的 Prediction Mode  設定 chroma intra prediction mode for (mode=DC_PRED_8; mode<=PLANE_8; mode++){ …… cost = 0; for (uv=0; uv<2; uv++) { image = imgUV_org[uv]; for (block_y=0; block_y<cr_MB_y; block_y+=4) for (block_x=0; block_x<cr_MB_x; block_x+=4) { for (k=0,j=block_y; j<block_y+4; j++) { for (i=block_x; i<block_x+4; i++,k++) diff[k] = image[left[j].pos_y][left[j].pos_x+i] - img->mprr_c[uv][mode][j][i]; } cost += distortion4x4(diff); } if (cost < min_cost) { best_mode = mode; min_cost = cost; } currMB->c_ipred_mode = best_mode;

24 Call Graph Encode_one_macrob lock_high (2) Compute_mode_RD_cost PartitionMotionSearch submacroblock_mode_decision (2) Rdcost_for_macroblocks SetModesAndRefframeForBlock s store_macroblock_parameters RDCost_for_8x8blocks IntraChromaPrediction Encode_one_ma croblock Mode Decision for a macroblock Mode Decision for an 8x8 sub-macroblock Precompute all new chroma intra prediction modes RD decision process

25 compute_mode_RD_cost  Location: lencod\src\Mode_decision.c  Brief: RD decision process  Arguments:  int mode  Macroblock *currMB// pointer to current MB  RD_PARAMS enc_mb  double *min_rdcost// pointer to minimum RD cost  double *min_rate// pointer to minimum rate  int i16mode  short bslice // 是否是 Bslice 編碼  short *inter_skip

26 Work Flow  Set Modes and Reference frame For Blocks  Encode with coefficients  While 1 loop  Go through transform modes  Try mb_types 1, 2, 3 with 8x8 transform  Try DIRECT-MODE with 8x8 transform  Try mb_type P8x8 for mode 4 with 4x4/ 8x8 transform  Encode with no coefficients

27 Work Flow  Set Modes and Reference frame For Blocks  Encode with coefficients  While 1 loop  Go through transform modes  Try mb_types 1, 2, 3 with 8x8 transform  Try DIRECT-MODE with 8x8 transform  Try mb_type P8x8 for mode 4 with 4x4/ 8x8 transform  Encode with no coefficients SetModesAndRefframeForBlocks (mode);

28 Work Flow  Set Modes and Reference frame For Blocks  Encode with coefficients  While 1 loop  Go through transform modes  Try mb_types 1, 2, 3 with 8x8 transform  Try DIRECT-MODE with 8x8 transform  Try mb_type P8x8 for mode 4 with 4x4/ 8x8 transform  Encode with no coefficients img->NoResidueDirect = 0;

29 while 1 loop  RDCost_for_macroblocks  Rate control  store_macroblock_parameters if (RDCost_for_macroblocks (enc_mb.lambda_md, mode, min_rdcost, min_rate, i16mode)) { //Rate control if (input->RCEnable) { if(mode == P8x8) rc_store_diff(img->opix_x,img->opix_y, currMB->luma_transform_size_8x8_flag == 1 ? tr8x8.mpr8x8 : tr4x4.mpr8x8); else rc_store_diff(img->opix_x, img->opix_y, pred); } store_macroblock_parameters (mode); … }

30 Go through transform modes // Go through transform modes. if (input->Transform8x8Mode==1){ //=========== try mb_types 1,2,3 with 8x8 transform =========== if ((mode >= 1 && mode luma_transform_size_8x8_flag == 0) { //try with 8x8 transform size currMB->luma_transform_size_8x8_flag = 1; continue; } //=========== try DIRECT-MODE with 8x8 transform =========== else if (mode == 0 && bslice && active_sps->direct_8x8_inference_flag && currMB->luma_transform_size_8x8_flag == 0){ //try with 8x8 transform size currMB->luma_transform_size_8x8_flag = 1; continue; } //=========== try mb_type P8x8 for mode 4 with 4x4/8x8 transform =========== else if ((mode == P8x8) && (enc_mb.valid[4]) && (currMB->luma_transform_size_8x8_flag == 0)) { currMB->luma_transform_size_8x8_flag = 1; //check 8x8 partition for transform size 8x8 continue; } else { currMB->luma_transform_size_8x8_flag = 0; break; } else break; }

31 Encode with no coefficients  Currently only for direct  NoResidueDirect = 1 if ( bslice && mode == 0 && (*inter_skip == 0) && enc_mb.valid[mode] && currMB->cbp && (currMB->cbp&15) != 15 && !input->nobskip) { img->NoResidueDirect = 1; if (RDCost_for_macroblocks (enc_mb.lambda_md, mode, min_rdcost, min_rate, i16mode)) { //Rate control if (input->RCEnable) rc_store_diff(img->opix_x,img->opix_y,pred); store_macroblock_parameters (mode); }

32 Thank you


Download ppt "H.264/AVC Reference Software Group 3 11 簡嘉宏 9762530 2009/05/22."

Similar presentations


Ads by Google