00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "contributors.h"
00011
00012 #include <math.h>
00013 #include <float.h>
00014
00015 #include "global.h"
00016 #include "image.h"
00017 #include "fmo.h"
00018 #include "macroblock.h"
00019 #include "mb_access.h"
00020 #include "rdopt.h"
00021 #include "rdoq.h"
00022 #include "mv_search.h"
00023
00024 #define RDOQ_BASE 0
00025
00026
00027
00028
00029
00030
00031
00032
00033 void init_rdoq_slice(Slice *currSlice)
00034 {
00035
00036
00037 currSlice->norm_factor_4x4 = pow(2, (2 * DQ_BITS + 19));
00038 currSlice->norm_factor_8x8 = pow(2, (2 * Q_BITS_8 + 9));
00039 }
00040
00041
00042
00043
00044
00045
00046 int init_trellis_data_DC_cr_CAVLC(Macroblock *currMB, int **tblock, int qp_per, int qp_rem,
00047 LevelQuantParams *q_params_4x4, const byte *p_scan,
00048 levelDataStruct *dataLevel)
00049 {
00050 Slice *currSlice = currMB->p_slice;
00051 ImageParameters *p_Img = currMB->p_Img;
00052 int i, j, coeff_ctr, end_coeff_ctr = p_Img->num_cdc_coeff;
00053 int *m7;
00054 int q_bits = Q_BITS + qp_per + 1;
00055 int q_offset = ( 1 << (q_bits - 1) );
00056 double err;
00057 int level, lowerInt, k;
00058 double estErr = (double) estErr4x4[qp_rem][0][0] / currSlice->norm_factor_4x4;
00059
00060 for (coeff_ctr = 0; coeff_ctr < end_coeff_ctr; coeff_ctr++)
00061 {
00062 j = *p_scan++;
00063 i = *p_scan++;
00064
00065 m7 = &tblock[j][i];
00066 if (*m7 == 0)
00067 {
00068 dataLevel->levelDouble = 0;
00069 dataLevel->level[0] = 0;
00070 dataLevel->noLevels = 1;
00071 err = 0.0;
00072 dataLevel->errLevel[0] = 0.0;
00073 dataLevel->pre_level = 0;
00074 dataLevel->sign = 0;
00075 }
00076 else
00077 {
00078 dataLevel->levelDouble = iabs(*m7 * q_params_4x4->ScaleComp);
00079 level = (dataLevel->levelDouble >> q_bits);
00080
00081 lowerInt=( ((int)dataLevel->levelDouble - (level << q_bits)) < q_offset )? 1 : 0;
00082 dataLevel->level[0] = 0;
00083 if (level == 0 && lowerInt == 1)
00084 {
00085 dataLevel->noLevels = 1;
00086 }
00087 else if (level == 0 && lowerInt == 0)
00088 {
00089 dataLevel->level[1] = level + 1;
00090 dataLevel->noLevels = 2;
00091 }
00092 else if (level > 0 && lowerInt == 1)
00093 {
00094 dataLevel->level[1] = level;
00095 dataLevel->noLevels = 2;
00096 }
00097 else
00098 {
00099 dataLevel->level[1] = level;
00100 dataLevel->level[2] = level + 1;
00101 dataLevel->noLevels = 3;
00102 }
00103
00104 for (k = 0; k < dataLevel->noLevels; k++)
00105 {
00106 err = (double)(dataLevel->level[k] << q_bits) - (double)dataLevel->levelDouble;
00107 dataLevel->errLevel[k] = (err * err * estErr);
00108 }
00109
00110 if(dataLevel->noLevels == 1)
00111 dataLevel->pre_level = 0;
00112 else
00113 dataLevel->pre_level = (iabs (*m7) * q_params_4x4->ScaleComp + q_params_4x4->OffsetComp) >> q_bits;
00114 dataLevel->sign = isign(*m7);
00115 }
00116 dataLevel++;
00117 }
00118 return 0;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 int init_trellis_data_DC_cr_CABAC(Macroblock *currMB, int **tblock, int qp_per, int qp_rem,
00129 LevelQuantParams *q_params_4x4, const byte *p_scan,
00130 levelDataStruct *dataLevel, int* kStart, int* kStop)
00131 {
00132 Slice *currSlice = currMB->p_slice;
00133 ImageParameters *p_Img = currMB->p_Img;
00134 int noCoeff = 0;
00135 int i, j, coeff_ctr, end_coeff_ctr = p_Img->num_cdc_coeff;
00136 int *m7;
00137 int q_bits = Q_BITS + qp_per + 1;
00138 int q_offset = ( 1 << (q_bits - 1) );
00139 double err;
00140 int level, lowerInt, k;
00141 double estErr = (double) estErr4x4[qp_rem][0][0] / currSlice->norm_factor_4x4;
00142
00143 for (coeff_ctr = 0; coeff_ctr < end_coeff_ctr; coeff_ctr++)
00144 {
00145 j = *p_scan++;
00146 i = *p_scan++;
00147
00148 m7 = &tblock[j][i];
00149 if (*m7 == 0)
00150 {
00151 dataLevel->levelDouble = 0;
00152 dataLevel->level[0] = 0;
00153 dataLevel->noLevels = 1;
00154 err = 0.0;
00155 dataLevel->errLevel[0] = 0.0;
00156 }
00157 else
00158 {
00159 dataLevel->levelDouble = iabs(*m7 * q_params_4x4->ScaleComp);
00160 level = (dataLevel->levelDouble >> q_bits);
00161
00162 lowerInt=( ((int)dataLevel->levelDouble - (level << q_bits)) < q_offset )? 1 : 0;
00163 dataLevel->level[0] = 0;
00164 if (level == 0 && lowerInt == 1)
00165 {
00166 dataLevel->noLevels = 1;
00167 }
00168 else if (level == 0 && lowerInt == 0)
00169 {
00170 dataLevel->level[1] = level + 1;
00171 dataLevel->noLevels = 2;
00172 *kStop = coeff_ctr;
00173 noCoeff++;
00174 }
00175 else if (level > 0 && lowerInt == 1)
00176 {
00177 dataLevel->level[1] = level;
00178 dataLevel->noLevels = 2;
00179 *kStop = coeff_ctr;
00180 noCoeff++;
00181 }
00182 else
00183 {
00184 dataLevel->level[1] = level;
00185 dataLevel->level[2] = level + 1;
00186 dataLevel->noLevels = 3;
00187 *kStop = coeff_ctr;
00188 *kStart = coeff_ctr;
00189 noCoeff++;
00190 }
00191
00192 for (k = 0; k < dataLevel->noLevels; k++)
00193 {
00194 err = (double)(dataLevel->level[k] << q_bits) - (double)dataLevel->levelDouble;
00195 dataLevel->errLevel[k] = (err * err * estErr);
00196 }
00197 }
00198 dataLevel++;
00199 }
00200 return (noCoeff);
00201 }
00202
00203 void get_dQP_table(Slice *currSlice)
00204 {
00205 int qp_offset = (currSlice->slice_type == B_SLICE) ? (currSlice->RDOQ_QP_Num / 3): (currSlice->RDOQ_QP_Num >> 1);
00206 int deltaQPCnt, deltaQP;
00207
00208 memset(currSlice->deltaQPTable, 0, sizeof(int)*9);
00209
00210 for(deltaQPCnt = 0; deltaQPCnt < currSlice->RDOQ_QP_Num; deltaQPCnt++)
00211 {
00212 if (deltaQPCnt == 0)
00213 deltaQP = 0;
00214 else
00215 {
00216 if (deltaQPCnt <= qp_offset)
00217 deltaQP = deltaQPCnt - 1 - qp_offset;
00218 else
00219 deltaQP = deltaQPCnt - qp_offset;
00220 }
00221 currSlice->deltaQPTable[deltaQPCnt] = deltaQP;
00222 }
00223 }
00224
00225 void trellis_mp(Macroblock *currMB, Boolean prev_recode_mb)
00226 {
00227 Slice *currSlice = currMB->p_slice;
00228 ImageParameters *p_Img = currMB->p_Img;
00229 InputParameters *p_Inp = currMB->p_Inp;
00230
00231 int masterQP = 0, deltaQP;
00232 int qp_left, qp_up;
00233 #if RDOQ_BASE
00234 const int deltaQPTabB[] = {0, 1, -1, 2, 3, -2, 4, 5, -3};
00235 const int deltaQPTabP[] = {0, -1, 1, -2, 2, -3, 3, -4, 4};
00236 #endif
00237 int deltaQPCnt;
00238 int qp_anchor;
00239
00240 masterQP = p_Img->masterQP = p_Img->qp;
00241 p_Img->Motion_Selected = 0;
00242 currSlice->rddata_trellis_best.min_rdcost = 1e30;
00243
00244 if (p_Inp->symbol_mode == CABAC)
00245 {
00246 estRunLevel_CABAC(currMB, LUMA_4x4);
00247 estRunLevel_CABAC(currMB, LUMA_16AC);
00248 estRunLevel_CABAC(currMB, LUMA_16DC);
00249 if (p_Inp->Transform8x8Mode)
00250 estRunLevel_CABAC(currMB, LUMA_8x8);
00251 if (p_Img->yuv_format != YUV400)
00252 {
00253 estRunLevel_CABAC(currMB, CHROMA_AC);
00254 if (p_Img->yuv_format == YUV420)
00255 estRunLevel_CABAC(currMB, CHROMA_DC);
00256 else
00257 estRunLevel_CABAC(currMB, CHROMA_DC_2x4);
00258 }
00259 }
00260
00261 qp_left = (currMB->mb_left) ? currMB->mb_left->qp : p_Img->masterQP;
00262 qp_up = (currMB->mb_up) ? currMB->mb_up->qp : p_Img->masterQP;
00263 qp_anchor = (qp_left + qp_up + 1)>>1;
00264
00265 for (deltaQPCnt=0; deltaQPCnt < currSlice->RDOQ_QP_Num; deltaQPCnt++)
00266 {
00267 currSlice->rddata = &currSlice->rddata_trellis_curr;
00268 currSlice->rddata->min_dcost = 1e30;
00269 #if RDOQ_BASE
00270 if (currSlice->slice_type == B_SLICE)
00271 deltaQP = deltaQPTabB[deltaQPCnt];
00272 else
00273 deltaQP = deltaQPTabP[deltaQPCnt];
00274 #else
00275
00276
00277
00278 deltaQP = currSlice->deltaQPTable[deltaQPCnt];
00279 #endif
00280
00281 p_Img->qp = iClip3(-p_Img->bitdepth_luma_qp_scale, 51, masterQP + deltaQP);
00282 deltaQP = p_Img->qp - masterQP;
00283
00284
00285 #if RDOQ_BASE
00286 if(deltaQP != 0 && !(p_Img->qp - qp_anchor >= -2 && p_Img->qp - qp_anchor <= 1) && currMB->mb_left && currMB->mb_up && currSlice->slice_type == P_SLICE)
00287 continue;
00288 if(deltaQP != 0 && !(p_Img->qp - qp_anchor >= -1 && p_Img->qp - qp_anchor <= 2) && currMB->mb_left && currMB->mb_up && currSlice->slice_type == B_SLICE)
00289 continue;
00290 #endif
00291
00292 reset_macroblock(currMB);
00293 currMB->qp = (short) p_Img->qp;
00294 update_qp (currMB);
00295
00296 currSlice->encode_one_macroblock (currMB);
00297 end_encode_one_macroblock(currMB);
00298
00299
00300 if ( currSlice->rddata_trellis_curr.min_rdcost < currSlice->rddata_trellis_best.min_rdcost)
00301 copy_rddata_trellis(currMB, &currSlice->rddata_trellis_best, currSlice->rddata);
00302
00303 if (p_Inp->RDOQ_CP_MV)
00304 p_Img->Motion_Selected = 1;
00305
00306 #if (!RDOQ_BASE)
00307 if (p_Inp->RDOQ_Fast == 1)
00308 {
00309 if ((p_Img->qp - currSlice->rddata_trellis_best.qp > 1))
00310 break;
00311 if ((currSlice->rddata_trellis_curr.cbp == 0) && (currSlice->rddata_trellis_curr.mb_type != 0))
00312 break;
00313 if ((currSlice->rddata_trellis_best.mb_type == 0) && (currSlice->rddata_trellis_best.cbp == 0))
00314 break;
00315 }
00316 #endif
00317 }
00318
00319 reset_macroblock(currMB);
00320 currSlice->rddata = &currSlice->rddata_trellis_best;
00321
00322 copy_rdopt_data (currMB);
00323 write_macroblock (currMB, 1, prev_recode_mb);
00324 p_Img->qp = masterQP;
00325 }
00326
00327 void trellis_sp(Macroblock *currMB, Boolean prev_recode_mb)
00328 {
00329 ImageParameters *p_Img = currMB->p_Img;
00330 InputParameters *p_Inp = currMB->p_Inp;
00331 Slice *currSlice = currMB->p_slice;
00332
00333 p_Img->masterQP = p_Img->qp;
00334
00335 if (currSlice->symbol_mode == CABAC)
00336 {
00337 estRunLevel_CABAC(currMB, LUMA_4x4);
00338 estRunLevel_CABAC(currMB, LUMA_16AC);
00339
00340 estRunLevel_CABAC(currMB, LUMA_16DC);
00341 if (p_Inp->Transform8x8Mode)
00342 estRunLevel_CABAC(currMB, LUMA_8x8);
00343
00344 if (p_Img->yuv_format != YUV400)
00345 {
00346 estRunLevel_CABAC(currMB, CHROMA_AC);
00347
00348 if (p_Img->yuv_format == YUV420)
00349 estRunLevel_CABAC(currMB, CHROMA_DC);
00350 else
00351 estRunLevel_CABAC(currMB, CHROMA_DC_2x4);
00352 }
00353 }
00354
00355 currSlice->encode_one_macroblock (currMB);
00356 end_encode_one_macroblock(currMB);
00357
00358
00359
00360 write_macroblock (currMB, 1, prev_recode_mb);
00361 }
00362
00363 void trellis_coding(Macroblock *currMB, Boolean prev_recode_mb)
00364 {
00365 if (currMB->p_slice->RDOQ_QP_Num > 1)
00366 {
00367 trellis_mp(currMB, prev_recode_mb);
00368 }
00369 else
00370 {
00371 trellis_sp(currMB, prev_recode_mb);
00372 }
00373 }
00374
00375 void RDOQ_update_mode(Slice *currSlice, RD_PARAMS *enc_mb)
00376 {
00377 InputParameters *p_Inp = currSlice->p_Inp;
00378
00379 int bslice = (currSlice->slice_type == B_SLICE);
00380 int mb_type = currSlice->rddata_trellis_best.mb_type;
00381 int i;
00382 for(i=0; i<MAXMODE; i++)
00383 enc_mb->valid[i] = 0;
00384
00385 enc_mb->valid[mb_type] = 1;
00386
00387 if(mb_type == P8x8)
00388 {
00389 enc_mb->valid[4] = (short) (p_Inp->InterSearch[bslice][4]);
00390 enc_mb->valid[5] = (short) (p_Inp->InterSearch[bslice][5] && !(p_Inp->Transform8x8Mode==2));
00391 enc_mb->valid[6] = (short) (p_Inp->InterSearch[bslice][6] && !(p_Inp->Transform8x8Mode==2));
00392 enc_mb->valid[7] = (short) (p_Inp->InterSearch[bslice][7] && !(p_Inp->Transform8x8Mode==2));
00393 }
00394 }
00395
00396 void copy_rddata_trellis (Macroblock *currMB, RD_DATA *dest, RD_DATA *src)
00397 {
00398 ImageParameters *p_Img = currMB->p_Img;
00399 int j;
00400
00401 dest->min_rdcost = src->min_rdcost;
00402 dest->min_dcost = src->min_dcost;
00403 dest->min_rate = src->min_rate;
00404
00405 memcpy(&dest->rec_mb[0][0][0],&src->rec_mb[0][0][0], MB_PIXELS * sizeof(imgpel));
00406
00407 if (p_Img->yuv_format != YUV400)
00408 {
00409 memcpy(&dest->rec_mb[1][0][0],&src->rec_mb[1][0][0], 2 * MB_PIXELS * sizeof(imgpel));
00410 }
00411
00412 memcpy(&dest->cofAC[0][0][0][0], &src->cofAC[0][0][0][0], (4 + p_Img->num_blk8x8_uv) * 4 * 2 * 65 * sizeof(int));
00413 memcpy(&dest->cofDC[0][0][0], &src->cofDC[0][0][0], 3 * 2 * 18 * sizeof(int));
00414
00415 dest->mb_type = src->mb_type;
00416
00417 memcpy(dest->b8x8, src->b8x8, BLOCK_MULTIPLE * sizeof(Info8x8));
00418
00419 dest->cbp = src->cbp;
00420 dest->mode = src->mode;
00421 dest->i16offset = src->i16offset;
00422 dest->c_ipred_mode = src->c_ipred_mode;
00423 dest->luma_transform_size_8x8_flag = src->luma_transform_size_8x8_flag;
00424 dest->NoMbPartLessThan8x8Flag = src->NoMbPartLessThan8x8Flag;
00425 dest->qp = src->qp;
00426
00427 dest->prev_qp = src->prev_qp;
00428 dest->prev_dqp = src->prev_dqp;
00429 dest->prev_cbp = src->prev_cbp;
00430 dest->cbp_blk = src->cbp_blk;
00431
00432
00433 if (p_Img->type != I_SLICE)
00434 {
00435
00436 memcpy(&dest->all_mv [0][0][0][0][0][0], &src->all_mv [0][0][0][0][0][0], p_Img->max_num_references * 576 * sizeof(short));
00437 }
00438
00439 memcpy(dest->intra_pred_modes,src->intra_pred_modes, MB_BLOCK_PARTITIONS * sizeof(char));
00440 memcpy(dest->intra_pred_modes8x8,src->intra_pred_modes8x8, MB_BLOCK_PARTITIONS * sizeof(char));
00441 for(j = currMB->block_y; j < currMB->block_y + BLOCK_MULTIPLE; j++)
00442 memcpy(&dest->ipredmode[j][currMB->block_x],&src->ipredmode[j][currMB->block_x], BLOCK_MULTIPLE * sizeof(char));
00443
00444 memcpy(&dest->refar[LIST_0][0][0], &src->refar[LIST_0][0][0], 2 * BLOCK_MULTIPLE * BLOCK_MULTIPLE * sizeof(char));
00445
00446 }
00447
00448 void updateMV_mp(Macroblock *currMB, int *m_cost, short ref, int list, int h, int v, int blocktype, int block8x8)
00449 {
00450 InputParameters *p_Inp = currMB->p_Inp;
00451 Slice *currSlice = currMB->p_slice;
00452
00453 int i, j;
00454 int bsx = block_size[blocktype][0] >> 2;
00455 int bsy = block_size[blocktype][1] >> 2;
00456 MotionVector all_mv;
00457
00458 if ( (p_Inp->Transform8x8Mode == 1) && (blocktype == 4) && currMB->luma_transform_size_8x8_flag)
00459 {
00460 all_mv = currSlice->tmp_mv8[list][ref][v][h];
00461 *m_cost = currSlice->motion_cost8[list][ref][block8x8];
00462 }
00463 else
00464 {
00465 all_mv.mv_x = currSlice->rddata_trellis_best.all_mv[list][ref][blocktype][v][h][0];
00466 all_mv.mv_y = currSlice->rddata_trellis_best.all_mv[list][ref][blocktype][v][h][1];
00467 }
00468
00469 for (j = 0; j < bsy; j++)
00470 {
00471 for (i = 0; i < bsx; i++)
00472 {
00473 currSlice->all_mv[list][ref][blocktype][v+j][h+i][0] = all_mv.mv_x;
00474 currSlice->all_mv[list][ref][blocktype][v+j][h+i][1] = all_mv.mv_y;
00475 }
00476 }
00477 }
00478
00479
00480