00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "contributors.h"
00016
00017 #include "global.h"
00018 #include "image.h"
00019 #include "wp.h"
00020
00021
00022
00023
00024
00025
00026
00027 void InitWP(ImageParameters *p_Img, InputParameters *p_Inp)
00028 {
00029 if (p_Inp->WPIterMC && p_Img->nal_reference_idc)
00030 {
00031 p_Img->EstimateWPPSlice = EstimateWPPSliceAlg2;
00032 p_Img->EstimateWPBSlice = EstimateWPBSliceAlg2;
00033 p_Img->TestWPPSlice = TestWPPSliceAlg2;
00034 p_Img->TestWPBSlice = TestWPBSliceAlg2;
00035 }
00036 else
00037 {
00038 switch ( p_Inp->WPMethod )
00039 {
00040 default:
00041 case 0:
00042 p_Img->EstimateWPPSlice = EstimateWPPSliceAlg0;
00043 p_Img->EstimateWPBSlice = EstimateWPBSliceAlg0;
00044 p_Img->TestWPPSlice = TestWPPSliceAlg0;
00045 p_Img->TestWPBSlice = TestWPBSliceAlg0;
00046 break;
00047 case 1:
00048 p_Img->EstimateWPPSlice = EstimateWPPSliceAlg1;
00049 p_Img->EstimateWPBSlice = EstimateWPBSliceAlg1;
00050 p_Img->TestWPPSlice = TestWPPSliceAlg1;
00051 p_Img->TestWPBSlice = TestWPBSliceAlg1;
00052 break;
00053 }
00054 }
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 double ComputeImgSum(imgpel **CurrentImage, int height, int width)
00064 {
00065 int i, j;
00066 double sum_value = 0.0;
00067
00068 for (i = 0; i < height; i++)
00069 {
00070 for (j = 0; j < width; j++)
00071 {
00072 sum_value += (double) CurrentImage[i][j];
00073 }
00074 }
00075 return sum_value;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 void EstimateWPPSliceAlg0(Slice *currSlice, int select_offset)
00086 {
00087 ImageParameters *p_Img = currSlice->p_Img;
00088 InputParameters *p_Inp = currSlice->p_Inp;
00089 double dc_org = 0.0;
00090 double dc_org_UV[2] = {0.0};
00091 double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
00092 double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
00093
00094 int i, n, k;
00095 short default_weight[3];
00096 int list_offset = ((currSlice->MbaffFrameFlag)&&(p_Img->mb_data[p_Img->current_mb_nr].mb_field))? (p_Img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00097 short weight[2][MAX_REFERENCE_PICTURES][3];
00098 short offset[2][MAX_REFERENCE_PICTURES][3];
00099 int clist;
00100
00101 imgpel **tmpPtr;
00102
00103 currSlice->luma_log_weight_denom = 5;
00104 currSlice->chroma_log_weight_denom = 5;
00105
00106 currSlice->wp_luma_round = 1 << (currSlice->luma_log_weight_denom - 1);
00107 currSlice->wp_chroma_round = 1 << (currSlice->chroma_log_weight_denom - 1);
00108 default_weight[0] = 1 << currSlice->luma_log_weight_denom;
00109 default_weight[1] = default_weight[2] = 1 << currSlice->chroma_log_weight_denom;
00110
00111 dc_org = ComputeImgSum(p_Img->pCurImg, p_Img->height, p_Img->width);
00112
00113 if (p_Inp->ChromaWeightSupport == 1)
00114 {
00115 for (k = 0; k < 2; k++)
00116 {
00117 dc_org_UV[k] = ComputeImgSum(p_Img->pImgOrg[k + 1], p_Img->height_cr, p_Img->width_cr);
00118 }
00119 }
00120
00121 for (clist = 0; clist < 2 + list_offset; clist++)
00122 {
00123 for (n = 0; n < p_Img->listXsize[clist]; n++)
00124 {
00125 if ( wpxDetermineWP( currSlice, clist, n ) )
00126 {
00127
00128 for (i = 0; i < 3; i++)
00129 {
00130 weight[clist][n][i] = default_weight[i];
00131 currSlice->wp_weight[clist][n][i] = default_weight[i];
00132 currSlice->wp_offset[clist][n][i] = 0;
00133 offset[clist][n][i] = 0;
00134 }
00135
00136
00137 tmpPtr = p_Img->listX[clist][n]->p_curr_img;
00138 dc_ref[n] = ComputeImgSum(tmpPtr, p_Img->height, p_Img->width);
00139
00140 if (p_Inp->ChromaWeightSupport == 1)
00141 {
00142 for (k = 0; k < 2; k++)
00143 {
00144
00145 tmpPtr = p_Img->listX[clist][n]->imgUV[k];
00146 dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, p_Img->height_cr, p_Img->width_cr);
00147 }
00148 }
00149
00150 if (select_offset == 0)
00151 {
00152 if (dc_ref[n] != 0.0)
00153 weight[clist][n][0] = (short) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
00154 else
00155 weight[clist][n][0] = default_weight[0];
00156 weight[clist][n][0] = sClip3(-128, 127, weight[clist][n][0]);
00157 if (p_Inp->ChromaWeightSupport == 1)
00158 {
00159 if (dc_ref_UV[n][0] != 0)
00160 weight[clist][n][1] = (short) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
00161 else
00162 weight[clist][n][1] = default_weight[1];
00163 weight[clist][n][1] = sClip3(-128, 127, weight[clist][n][1]);
00164
00165 if (dc_ref_UV[n][1] != 0)
00166 weight[clist][n][2] = (short) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
00167 else
00168 weight[clist][n][2] = default_weight[2];
00169 weight[clist][n][2] = sClip3(-128, 127, weight[clist][n][2]);
00170 }
00171 }
00172 else
00173 {
00174 offset[clist][n][0] = (short) ((dc_org - dc_ref[n])/(p_Img->size)+0.5);
00175 offset[clist][n][0] = (offset[clist][n][0]+((p_Img->bitdepth_luma-8)>>1))>>(p_Img->bitdepth_luma-8);
00176 offset[clist][n][0] = sClip3( -128, 127, offset[clist][n][0]);
00177 offset[clist][n][0] = offset[clist][n][0]<<(p_Img->bitdepth_luma-8);
00178 weight[clist][n][0] = default_weight[0];
00179
00180 if (p_Inp->ChromaWeightSupport == 1)
00181 {
00182 offset[clist][n][1] = (short) ((dc_org_UV[0] - dc_ref_UV[n][0])/(p_Img->size_cr)+0.5);
00183 offset[clist][n][1] = (offset[clist][n][1] + ((p_Img->bitdepth_chroma - 8)>>1))>>(p_Img->bitdepth_chroma-8);
00184 offset[clist][n][1] = sClip3( -128, 127, offset[clist][n][1]);
00185 offset[clist][n][1] = offset[clist][n][1]<<(p_Img->bitdepth_chroma - 8);
00186
00187 weight[clist][n][1] = default_weight[1];
00188
00189 offset[clist][n][2] = (short) ((dc_org_UV[1] - dc_ref_UV[n][1])/(p_Img->size_cr)+0.5);
00190 offset[clist][n][2] = (offset[clist][n][2] + ((p_Img->bitdepth_chroma - 8)>>1))>>(p_Img->bitdepth_chroma-8);
00191 offset[clist][n][2] = sClip3( -128, 127, offset[clist][n][2]);
00192 offset[clist][n][2] = offset[clist][n][2]<<(p_Img->bitdepth_chroma - 8);
00193
00194 weight[clist][n][2] = default_weight[2];
00195 }
00196 }
00197
00198 for (i=0; i < 3; i ++)
00199 {
00200 currSlice->wp_weight[clist][n][i] = weight[clist][n][i];
00201 currSlice->wp_offset[clist][n][i] = offset[clist][n][i];
00202 #if DEBUG_WP
00203 printf("index %d component %d weight %d offset %d\n",n,i,weight[0][n][i],offset[0][n][i]);
00204 #endif
00205 }
00206 }
00207 }
00208 }
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 void EstimateWPBSliceAlg0(Slice *currSlice)
00218 {
00219 ImageParameters *p_Img = currSlice->p_Img;
00220 InputParameters *p_Inp = currSlice->p_Inp;
00221 int i, j, k, n;
00222
00223 int tx;
00224 short DistScaleFactor;
00225
00226 int index;
00227 int comp;
00228 double dc_org = 0.0;
00229 double dc_org_UV[2] = { 0.0 };
00230 double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };
00231 double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
00232
00233 short default_weight[3];
00234 int list_offset = ((currSlice->MbaffFrameFlag)&&(p_Img->mb_data[p_Img->current_mb_nr].mb_field))? (p_Img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00235 short weight[6][MAX_REFERENCE_PICTURES][3];
00236 short offset[6][MAX_REFERENCE_PICTURES][3];
00237 short im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
00238 int clist;
00239 short wf_weight, wf_offset;
00240 imgpel **tmpPtr;
00241
00242 if (p_Img->active_pps->weighted_bipred_idc == 2)
00243 {
00244 currSlice->luma_log_weight_denom = 5;
00245 currSlice->chroma_log_weight_denom = 5;
00246 }
00247 else
00248 {
00249 currSlice->luma_log_weight_denom = 5;
00250 currSlice->chroma_log_weight_denom = 5;
00251 }
00252
00253 currSlice->wp_luma_round = 1 << (currSlice->luma_log_weight_denom - 1);
00254 currSlice->wp_chroma_round = 1 << (currSlice->chroma_log_weight_denom - 1);
00255 default_weight[0] = 1 << currSlice->luma_log_weight_denom;
00256 default_weight[1] = 1 << currSlice->chroma_log_weight_denom;
00257 default_weight[2] = 1 << currSlice->chroma_log_weight_denom;
00258
00259 if (p_Img->active_pps->weighted_bipred_idc == 2)
00260 {
00261 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00262 {
00263 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00264 {
00265 int td, tb;
00266 td = sClip3(-128, 127, (short) (p_Img->listX[LIST_1][j]->poc - p_Img->listX[LIST_0][i]->poc));
00267 tb = sClip3(-128, 127, (short) (p_Img->enc_picture->poc - p_Img->listX[LIST_0][i]->poc));
00268 for (comp = 0; comp < 3; comp++)
00269 {
00270
00271 if (td == 0)
00272 {
00273 im_weight[0][i][j][comp] = default_weight[comp];
00274 im_weight[1][i][j][comp] = default_weight[comp];
00275 }
00276 else
00277 {
00278 tx = (16384 + iabs(td >> 1))/td;
00279 DistScaleFactor = sClip3(-1024, 1023, (short) (tx*tb + 32 )>>6);
00280 im_weight[1][i][j][comp] = DistScaleFactor>>2;
00281 if (im_weight[1][i][j][comp] < -64 || im_weight[1][i][j][comp] >128)
00282 im_weight[1][i][j][comp] = default_weight[comp];
00283 im_weight[0][i][j][comp] = 64 - im_weight[1][i][j][comp];
00284 }
00285 }
00286 #if DEBUG_WP
00287 printf ("%d imp weight[%d][%d] = %d , %d (%d %d %d) (%d %d) (%d %d)\n",p_Img->enc_picture->poc, i, j, im_weight[0][i][j][0], im_weight[1][i][j][0],
00288 p_Img->enc_picture->poc,p_Img->listX[LIST_0][i]->poc, p_Img->listX[LIST_1][j]->poc,
00289 DistScaleFactor ,tx,td,tb);
00290 #endif
00291 }
00292 }
00293
00294 for (k = 0; k < 2; k++)
00295 {
00296 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00297 {
00298 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00299 {
00300 for (comp = 0; comp < 3; comp++)
00301 {
00302 currSlice->wbp_weight[k][i][j][comp] = im_weight[k][i][j][comp];
00303 }
00304 }
00305 }
00306 }
00307
00308 for (clist=0; clist<2 + list_offset; clist++)
00309 {
00310 for (index = 0; index < p_Img->listXsize[clist]; index++)
00311 {
00312 for (comp = 0; comp < 3; comp++)
00313 {
00314 currSlice->wp_weight[clist][index][comp] = default_weight[comp];
00315 currSlice->wp_offset[clist][index][comp] = 0;
00316 }
00317 }
00318 }
00319 }
00320 else
00321 {
00322 dc_org = ComputeImgSum(p_Img->pCurImg, p_Img->height, p_Img->width);
00323
00324 if (p_Inp->ChromaWeightSupport == 1)
00325 {
00326 for (k = 0; k < 2; k++)
00327 {
00328 dc_org_UV[k] = ComputeImgSum(p_Img->pImgOrg[k + 1], p_Img->height_cr, p_Img->width_cr);
00329 }
00330 }
00331
00332 for (clist=0; clist<2 + list_offset; clist++)
00333 {
00334 for (n = 0; n < p_Img->listXsize[clist]; n++)
00335 {
00336 if ( wpxDetermineWP( currSlice, clist, n ) )
00337 {
00338
00339 for (i = 0; i < 3; i++)
00340 {
00341 currSlice->wp_weight[clist][n][i] = default_weight[i];
00342 currSlice->wp_offset[clist][n][i] = 0;
00343 offset [clist][n][i] = 0;
00344 weight [clist][n][i] = default_weight[i];
00345 }
00346
00347
00348
00349 tmpPtr = p_Img->listX[clist][n]->p_curr_img;
00350 dc_ref[clist][n] = ComputeImgSum(tmpPtr, p_Img->height, p_Img->width);
00351
00352 if (dc_ref[clist][n] != 0.0)
00353 wf_weight = (short) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
00354 else
00355 wf_weight = default_weight[0];
00356 wf_weight = sClip3(-128, 127, wf_weight);
00357 wf_offset = 0;
00358
00359
00360
00361 weight[clist][n][0] = wf_weight;
00362 offset[clist][n][0] = wf_offset;
00363
00364
00365 if (p_Inp->ChromaWeightSupport == 1)
00366 {
00367 for (k = 0; k < 2; k++)
00368 {
00369 tmpPtr = p_Img->listX[clist][n]->imgUV[k];
00370 dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, p_Img->height_cr, p_Img->width_cr);
00371
00372 if (dc_ref_UV[clist][n][k] != 0.0)
00373 wf_weight = (short) (default_weight[k + 1] * dc_org_UV[k] / dc_ref_UV[clist][n][k] + 0.5);
00374 else
00375 wf_weight = default_weight[k + 1];
00376 wf_weight = sClip3(-128, 127, wf_weight);
00377 wf_offset = 0;
00378
00379 weight[clist][n][k + 1] = wf_weight;
00380 offset[clist][n][k + 1] = wf_offset;
00381 }
00382 }
00383 else
00384 {
00385 weight[clist][n][1] = default_weight[1];
00386 weight[clist][n][2] = default_weight[2];
00387 offset[clist][n][1] = 0;
00388 offset[clist][n][2] = 0;
00389 }
00390
00391 for (i = 0; i < 3; i++)
00392 {
00393 currSlice->wp_weight[clist][n][i] = weight[clist][n][i];
00394 currSlice->wp_offset[clist][n][i] = offset[clist][n][i];
00395 #if DEBUG_WP
00396 printf("%d %d\n",currSlice->wp_weight[clist][index][comp],currSlice->wp_offset[clist][index][comp]);
00397 #endif
00398 }
00399 }
00400 }
00401 }
00402
00403 if (p_Img->active_pps->weighted_bipred_idc != 1)
00404 {
00405 for (clist=0; clist<2 + list_offset; clist++)
00406 {
00407 for (index = 0; index < p_Img->listXsize[clist]; index++)
00408 {
00409 memcpy(currSlice->wp_weight[clist][index], default_weight, 3 * sizeof(short));
00410 memset(currSlice->wp_offset[clist][index], 0, 3 * sizeof(short));
00411 }
00412 }
00413 }
00414
00415
00416 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00417 {
00418 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00419 {
00420 for (comp = 0; comp < 3; comp++)
00421 {
00422 currSlice->wbp_weight[0][i][j][comp] = currSlice->wp_weight[0][i][comp];
00423 currSlice->wbp_weight[1][i][j][comp] = currSlice->wp_weight[1][j][comp];
00424 }
00425 #if DEBUG_WP
00426 printf ("bpw weight[%d][%d] = %d , %d (%d %d %d) (%d %d) (%d %d)\n", i, j, currSlice->wbp_weight[0][i][j][0], currSlice->wbp_weight[1][i][j][0],
00427 p_Img->enc_picture->poc,p_Img->listX[LIST_0][i]->poc, p_Img->listX[LIST_1][j]->poc,
00428 DistScaleFactor ,tx,tx,tx);
00429 #endif
00430 }
00431 }
00432 }
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 int TestWPPSliceAlg0(ImageParameters *p_Img, InputParameters *p_Inp, int select_offset)
00444 {
00445 int i, j, k, n;
00446
00447 int index;
00448 int comp;
00449 double dc_org = 0.0;
00450 double dc_org_UV[2] = {0.0};
00451 double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
00452 double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
00453
00454 short default_weight[3];
00455
00456 int list_offset = ((p_Img->MbaffFrameFlag)&&(p_Img->mb_data[p_Img->current_mb_nr].mb_field))? (p_Img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00457 short weight[2][MAX_REFERENCE_PICTURES][3];
00458 short offset[2][MAX_REFERENCE_PICTURES][3];
00459 short wp_weight[6][MAX_REFERENCE_PICTURES][3];
00460 short wp_offset[6][MAX_REFERENCE_PICTURES][3];
00461 int clist;
00462 int perform_wp = 0;
00463 imgpel **tmpPtr;
00464
00465 short luma_log_weight_denom = 5;
00466 short chroma_log_weight_denom = 5;
00467
00468 default_weight[0] = 1 << luma_log_weight_denom;
00469 default_weight[1] = default_weight[2] = 1 << chroma_log_weight_denom;
00470
00471
00472 for (i = 0; i < 2 + list_offset; i++)
00473 {
00474 for (j = 0; j < p_Img->listXsize[i]; j++)
00475 {
00476 for (n = 0; n < 3; n++)
00477 {
00478 weight[i][j][n] = default_weight[n];
00479 wp_weight[i][j][n] = default_weight[n];
00480 wp_offset[i][j][n] = 0;
00481 offset[i][j][n] = 0;
00482 }
00483 }
00484 }
00485
00486 dc_org = ComputeImgSum(p_Img->pCurImg, p_Img->height, p_Img->width);
00487
00488 if (p_Inp->ChromaWeightSupport == 1)
00489 {
00490 for (k = 0; k < 2; k++)
00491 {
00492 dc_org_UV[k] = ComputeImgSum(p_Img->pImgOrg[k + 1], p_Img->height_cr, p_Img->width_cr);
00493 }
00494 }
00495
00496 for (clist = 0; clist < 2 + list_offset; clist++)
00497 {
00498 for (n = 0; n < p_Img->listXsize[clist]; n++)
00499 {
00500 tmpPtr = p_Img->listX[clist][n]->p_curr_img;
00501 dc_ref[n] = ComputeImgSum(tmpPtr, p_Img->height, p_Img->width);
00502
00503 if (p_Inp->ChromaWeightSupport == 1)
00504 {
00505 for (k = 0; k < 2; k++)
00506 {
00507 tmpPtr = p_Img->listX[clist][n]->imgUV[k];
00508 dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, p_Img->height_cr, p_Img->width_cr);
00509 }
00510 }
00511
00512 if (select_offset == 0)
00513 {
00514 if (dc_ref[n] != 0.0)
00515 weight[clist][n][0] = (short) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
00516 else
00517 weight[clist][n][0] = default_weight[0];
00518 weight[clist][n][0] = sClip3(-128, 127, weight[clist][n][0]);
00519
00520 if (p_Inp->ChromaWeightSupport == 1)
00521 {
00522 if (dc_ref_UV[n][0] != 0)
00523 weight[clist][n][1] = (short) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
00524 else
00525 weight[clist][n][1] = default_weight[1];
00526 weight[clist][n][1] = sClip3(-128, 127, weight[clist][n][1]);
00527
00528 if (dc_ref_UV[n][1] != 0)
00529 weight[clist][n][2] = (short) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
00530 else
00531 weight[clist][n][2] = default_weight[2];
00532 weight[clist][n][2] = sClip3(-128, 127, weight[clist][n][2]);
00533 }
00534 }
00535 else
00536 {
00537 offset[clist][n][0] = (short) ((dc_org - dc_ref[n])/(p_Img->size)+0.5);
00538 offset[clist][n][0] = (offset[clist][n][0]+((p_Img->bitdepth_luma-8)>>1))>>(p_Img->bitdepth_luma-8);
00539 offset[clist][n][0] = sClip3( -128, 127, offset[clist][n][0]);
00540 offset[clist][n][0] = offset[clist][n][0]<<(p_Img->bitdepth_luma-8);
00541 weight[clist][n][0] = default_weight[0];
00542
00543 if (p_Inp->ChromaWeightSupport == 1)
00544 {
00545 offset[clist][n][1] = (short) ((dc_org_UV[0] - dc_ref_UV[n][0])/(p_Img->size_cr)+0.5);
00546 offset[clist][n][1] = (offset[clist][n][1] + ((p_Img->bitdepth_chroma - 8)>>1))>>(p_Img->bitdepth_chroma-8);
00547 offset[clist][n][1] = sClip3( -128, 127, offset[clist][n][1]);
00548 offset[clist][n][1] = offset[clist][n][1]<<(p_Img->bitdepth_chroma - 8);
00549
00550 weight[clist][n][1] = default_weight[1];
00551
00552 offset[clist][n][2] = (short) ((dc_org_UV[1] - dc_ref_UV[n][1])/(p_Img->size_cr)+0.5);
00553 offset[clist][n][2] = (offset[clist][n][2] + ((p_Img->bitdepth_chroma - 8)>>1))>>(p_Img->bitdepth_chroma-8);
00554 offset[clist][n][2] = sClip3( -128, 127, offset[clist][n][2]);
00555 offset[clist][n][2] = offset[clist][n][2]<<(p_Img->bitdepth_chroma - 8);
00556
00557 weight[clist][n][2] = default_weight[2];
00558 }
00559 }
00560 }
00561 }
00562
00563 for (clist=0; clist<2 + list_offset; clist++)
00564 {
00565 for (index = 0; index < p_Img->listXsize[clist]; index++)
00566 {
00567 for (comp=0; comp < 3; comp ++)
00568 {
00569 int offset_test = p_Inp->RDPSliceBTest && p_Img->active_sps->profile_idc != BASELINE
00570 ? iabs(offset[clist][index][comp]) > 2
00571 : offset[clist][index][comp] != 0;
00572
00573 if (weight[clist][index][comp] != default_weight[0] || offset_test)
00574 {
00575 perform_wp = 1;
00576 break;
00577 }
00578 }
00579 if (perform_wp == 1) break;
00580 }
00581 if (perform_wp == 1) break;
00582 }
00583
00584 return perform_wp;
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594 int TestWPBSliceAlg0(ImageParameters *p_Img, InputParameters *p_Inp, int select_method)
00595 {
00596 int i, j, k, n;
00597
00598 short tx, td, tb, DistScaleFactor;
00599
00600 int index;
00601 int comp;
00602 double dc_org = 0.0;
00603 double dc_org_UV[2] = { 0.0 };
00604 double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };
00605 double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
00606
00607 short default_weight[3];
00608
00609 int list_offset = ((p_Img->MbaffFrameFlag)&&(p_Img->mb_data[p_Img->current_mb_nr].mb_field))? (p_Img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00610 short weight[6][MAX_REFERENCE_PICTURES][3];
00611 short offset[6][MAX_REFERENCE_PICTURES][3];
00612 short im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
00613 short wp_weight[6][MAX_REFERENCE_PICTURES][3];
00614 short wp_offset[6][MAX_REFERENCE_PICTURES][3];
00615 short wbp_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
00616
00617 int clist;
00618 short wf_weight, wf_offset;
00619 int perform_wp = 0;
00620 imgpel **tmpPtr;
00621
00622 short luma_log_weight_denom = 5;
00623 short chroma_log_weight_denom = 5;
00624
00625 default_weight[0] = 1 << luma_log_weight_denom;
00626 default_weight[1] = 1 << chroma_log_weight_denom;
00627 default_weight[2] = 1 << chroma_log_weight_denom;
00628
00629
00630 for (i = 0; i < 2 + list_offset; i++)
00631 {
00632 for (j = 0; j < p_Img->listXsize[i]; j++)
00633 {
00634 for (n = 0; n < 3; n++)
00635 {
00636 wp_weight[i][j][n] = default_weight[n];
00637 wp_offset[i][j][n] = 0;
00638 offset [i][j][n] = 0;
00639 weight [i][j][n] = default_weight[n];
00640 }
00641 }
00642 }
00643
00644 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00645 {
00646 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00647 {
00648 td = (short) iClip3(-128, 127,(p_Img->listX[LIST_1][j]->poc - p_Img->listX[LIST_0][i]->poc));
00649 tb = (short) iClip3(-128, 127,(p_Img->enc_picture->poc - p_Img->listX[LIST_0][i]->poc));
00650 for (comp = 0; comp < 3; comp++)
00651 {
00652
00653 if (td == 0)
00654 {
00655 im_weight[0][i][j][comp] = default_weight[comp];
00656 im_weight[1][i][j][comp] = default_weight[comp];
00657 }
00658 else
00659 {
00660 tx = (short) (16384 + iabs(td >> 1))/td;
00661 DistScaleFactor = sClip3(-1024, 1023, (tx*tb + 32 )>>6);
00662 im_weight[1][i][j][comp] = DistScaleFactor >> 2;
00663 if (im_weight[1][i][j][comp] < -64 || im_weight[1][i][j][comp] >128)
00664 im_weight[1][i][j][comp] = default_weight[comp];
00665 im_weight[0][i][j][comp] = 64 - im_weight[1][i][j][comp];
00666 }
00667 }
00668 }
00669 }
00670
00671
00672 if (select_method == 1)
00673 {
00674 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00675 {
00676 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00677 {
00678 for (comp = 0; comp < 3; comp++)
00679 {
00680 wbp_weight[1][i][j][comp] = im_weight[1][i][j][comp] ;
00681 wbp_weight[0][i][j][comp] = im_weight[0][i][j][comp];
00682 }
00683 }
00684 }
00685
00686 for (clist=0; clist<2 + list_offset; clist++)
00687 {
00688 for (index = 0; index < p_Img->listXsize[clist]; index++)
00689 {
00690 for (comp = 0; comp < 3; comp++)
00691 {
00692 wp_weight[clist][index][comp] = default_weight[comp];
00693 wp_offset[clist][index][comp] = 0;
00694 }
00695 }
00696 }
00697 }
00698 else
00699 {
00700 dc_org = ComputeImgSum(p_Img->pCurImg, p_Img->height, p_Img->width);
00701
00702 if (p_Inp->ChromaWeightSupport == 1)
00703 {
00704 for (k = 0; k < 2; k++)
00705 {
00706 dc_org_UV[k] = ComputeImgSum(p_Img->pImgOrg[k + 1], p_Img->height_cr, p_Img->width_cr);
00707 }
00708 }
00709
00710 for (clist=0; clist<2 + list_offset; clist++)
00711 {
00712 for (n = 0; n < p_Img->listXsize[clist]; n++)
00713 {
00714
00715
00716
00717 tmpPtr = p_Img->listX[clist][n]->p_curr_img;
00718 dc_ref[clist][n] = ComputeImgSum(tmpPtr, p_Img->height, p_Img->width);
00719
00720 if (dc_ref[clist][n] != 0.0)
00721 wf_weight = (short) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
00722 else
00723 wf_weight = default_weight[0];
00724 wf_weight = sClip3(-128, 127, wf_weight);
00725 wf_offset = 0;
00726
00727 weight[clist][n][0] = wf_weight;
00728 offset[clist][n][0] = wf_offset;
00729
00730
00731 if (p_Inp->ChromaWeightSupport == 1)
00732 {
00733 for (k = 0; k < 2; k++)
00734 {
00735 tmpPtr = p_Img->listX[clist][n]->imgUV[k];
00736 dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, p_Img->height_cr, p_Img->width_cr);
00737
00738 if (dc_ref_UV[clist][n][k] != 0.0)
00739 wf_weight = (short) (default_weight[k + 1] * dc_org_UV[k] / dc_ref_UV[clist][n][k] + 0.5);
00740 else
00741 wf_weight = default_weight[k + 1];
00742 wf_weight = sClip3(-128, 127, wf_weight);
00743 wf_offset = 0;
00744
00745 weight[clist][n][k + 1] = wf_weight;
00746 offset[clist][n][k + 1] = wf_offset;
00747 }
00748 }
00749 else
00750 {
00751 weight[clist][n][1] = default_weight[1];
00752 weight[clist][n][2] = default_weight[2];
00753 offset[clist][n][1] = 0;
00754 offset[clist][n][2] = 0;
00755 }
00756 }
00757 }
00758
00759 if (select_method == 0)
00760 {
00761 for (clist=0; clist<2 + list_offset; clist++)
00762 {
00763 for (index = 0; index < p_Img->listXsize[clist]; index++)
00764 {
00765 memcpy(p_Img->currentSlice->wp_weight[clist][index], weight[clist][index], 3 * sizeof(short));
00766 memcpy(p_Img->currentSlice->wp_offset[clist][index], offset[clist][index], 3 * sizeof(short));
00767 }
00768 }
00769 }
00770 else
00771 {
00772 for (clist=0; clist<2 + list_offset; clist++)
00773 {
00774 for (index = 0; index < p_Img->listXsize[clist]; index++)
00775 {
00776 memcpy(wp_weight[clist][index], default_weight, 3 * sizeof(short));
00777 memset(wp_offset[clist][index], 0, 3 * sizeof(short));
00778 }
00779 }
00780 }
00781
00782 for (i = 0; i < p_Img->listXsize[LIST_0]; i++)
00783 {
00784 for (j = 0; j < p_Img->listXsize[LIST_1]; j++)
00785 {
00786 for (comp = 0; comp < 3; comp++)
00787 {
00788 wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
00789 wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
00790 }
00791 #if DEBUG_WP
00792 printf ("bpw weight[%d][%d] = %d , %d (%d %d %d) (%d %d) (%d %d)\n", i, j, p_Img->currentSlice->wbp_weight[0][i][j][0], p_Img->currentSlice->wbp_weight[1][i][j][0],
00793 p_Img->enc_picture->poc,p_Img->listX[LIST_0][i]->poc, p_Img->listX[LIST_1][j]->poc,
00794 DistScaleFactor ,tx,tx,tx);
00795 #endif
00796 }
00797 }
00798 }
00799
00800 if (select_method == 0)
00801 {
00802 int active_refs[2];
00803
00804 active_refs[0] = (p_Inp->B_List0_refs == 0 ? p_Img->listXsize[0] : imin(p_Inp->B_List0_refs, p_Img->listXsize[0]));
00805 active_refs[1] = (p_Inp->B_List1_refs == 0 ? p_Img->listXsize[1] : imin(p_Inp->B_List1_refs, p_Img->listXsize[1]));
00806
00807 for (clist=0; clist<2 + list_offset; clist++)
00808 {
00809 for (index = 0; index < active_refs[clist]; index++)
00810 {
00811 for (comp=0; comp < 3; comp ++)
00812 {
00813 if (wp_weight[clist][index][comp] != default_weight[comp])
00814 {
00815 perform_wp = 1;
00816 break;
00817 }
00818 }
00819 if (perform_wp == 1) break;
00820 }
00821 if (perform_wp == 1) break;
00822 }
00823 }
00824 return perform_wp;
00825 }