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 *img, InputParameters *params)
00028 {
00029 if (params->WPIterMC && img->nal_reference_idc)
00030 {
00031 EstimateWPPSlice = EstimateWPPSliceAlg2;
00032 EstimateWPBSlice = EstimateWPBSliceAlg2;
00033 TestWPPSlice = TestWPPSliceAlg2;
00034 TestWPBSlice = TestWPBSliceAlg2;
00035 }
00036 else
00037 {
00038 switch ( params->WPMethod )
00039 {
00040 default:
00041 case 0:
00042 EstimateWPPSlice = EstimateWPPSliceAlg0;
00043 EstimateWPBSlice = EstimateWPBSliceAlg0;
00044 TestWPPSlice = TestWPPSliceAlg0;
00045 TestWPBSlice = TestWPBSliceAlg0;
00046 break;
00047 case 1:
00048 EstimateWPPSlice = EstimateWPPSliceAlg1;
00049 EstimateWPBSlice = EstimateWPBSliceAlg1;
00050 TestWPPSlice = TestWPPSliceAlg1;
00051 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(ImageParameters *img, InputParameters *params, int select_offset)
00086 {
00087 double dc_org = 0.0;
00088 double dc_org_UV[2] = {0.0};
00089 double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
00090 double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
00091
00092 int i, n, k;
00093 int default_weight[3];
00094 int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00095 int weight[2][MAX_REFERENCE_PICTURES][3];
00096 int offset[2][MAX_REFERENCE_PICTURES][3];
00097 int clist;
00098
00099 imgpel **tmpPtr;
00100
00101 luma_log_weight_denom = 5;
00102 chroma_log_weight_denom = 5;
00103
00104 wp_luma_round = 1 << (luma_log_weight_denom - 1);
00105 wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
00106 default_weight[0] = 1 << luma_log_weight_denom;
00107 default_weight[1] = default_weight[2] = 1 << chroma_log_weight_denom;
00108
00109 dc_org = ComputeImgSum(pCurImg, img->height, img->width);
00110
00111 if (params->ChromaWeightSupport == 1)
00112 {
00113 for (k = 0; k < 2; k++)
00114 {
00115 dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
00116 }
00117 }
00118
00119 for (clist = 0; clist < 2 + list_offset; clist++)
00120 {
00121 for (n = 0; n < listXsize[clist]; n++)
00122 {
00123 if ( wpxDetermineWP( params, img, clist, n ) )
00124 {
00125
00126 for (i = 0; i < 3; i++)
00127 {
00128 weight[clist][n][i] = default_weight[i];
00129 wp_weight[clist][n][i] = default_weight[i];
00130 wp_offset[clist][n][i] = 0;
00131 offset[clist][n][i] = 0;
00132 }
00133
00134
00135 tmpPtr = listX[clist][n]->p_curr_img;
00136 dc_ref[n] = ComputeImgSum(tmpPtr, img->height, img->width);
00137
00138 if (params->ChromaWeightSupport == 1)
00139 {
00140 for (k = 0; k < 2; k++)
00141 {
00142
00143 tmpPtr = listX[clist][n]->imgUV[k];
00144 dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
00145 }
00146 }
00147
00148 if (select_offset == 0)
00149 {
00150 if (dc_ref[n] != 0.0)
00151 weight[clist][n][0] = (int) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
00152 else
00153 weight[clist][n][0] = default_weight[0];
00154 weight[clist][n][0] = iClip3(-128, 127, weight[clist][n][0]);
00155 if (params->ChromaWeightSupport == 1)
00156 {
00157 if (dc_ref_UV[n][0] != 0)
00158 weight[clist][n][1] = (int) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
00159 else
00160 weight[clist][n][1] = default_weight[1];
00161 weight[clist][n][1] = iClip3(-128, 127, weight[clist][n][1]);
00162
00163 if (dc_ref_UV[n][1] != 0)
00164 weight[clist][n][2] = (int) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
00165 else
00166 weight[clist][n][2] = default_weight[2];
00167 weight[clist][n][2] = iClip3(-64, 128, weight[clist][n][2]);
00168 }
00169 }
00170 else
00171 {
00172 offset[clist][n][0] = (int) ((dc_org - dc_ref[n])/(img->size)+0.5);
00173 offset[clist][n][0] = (offset[clist][n][0]+((img->bitdepth_luma-8)>>1))>>(img->bitdepth_luma-8);
00174 offset[clist][n][0] = iClip3( -128, 127, offset[clist][n][0]);
00175 offset[clist][n][0] = offset[clist][n][0]<<(img->bitdepth_luma-8);
00176 weight[clist][n][0] = default_weight[0];
00177
00178 if (params->ChromaWeightSupport == 1)
00179 {
00180 offset[clist][n][1] = (int) ((dc_org_UV[0] - dc_ref_UV[n][0])/(img->size_cr)+0.5);
00181 offset[clist][n][1] = (offset[clist][n][1] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
00182 offset[clist][n][1] = iClip3( -128, 127, offset[clist][n][1]);
00183 offset[clist][n][1] = offset[clist][n][1]<<(img->bitdepth_chroma - 8);
00184
00185 weight[clist][n][1] = default_weight[1];
00186
00187 offset[clist][n][2] = (int) ((dc_org_UV[1] - dc_ref_UV[n][1])/(img->size_cr)+0.5);
00188 offset[clist][n][2] = (offset[clist][n][2] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
00189 offset[clist][n][2] = iClip3( -128, 127, offset[clist][n][2]);
00190 offset[clist][n][2] = offset[clist][n][2]<<(img->bitdepth_chroma - 8);
00191
00192 weight[clist][n][2] = default_weight[2];
00193 }
00194 }
00195
00196 for (i=0; i < 3; i ++)
00197 {
00198 wp_weight[clist][n][i] = weight[clist][n][i];
00199 wp_offset[clist][n][i] = offset[clist][n][i];
00200 #if DEBUG_WP
00201 printf("index %d component %d weight %d offset %d\n",n,i,weight[0][n][i],offset[0][n][i]);
00202 #endif
00203 }
00204 }
00205 }
00206 }
00207 }
00208
00209
00210
00211
00212
00213
00214
00215 void EstimateWPBSliceAlg0(ImageParameters *img, InputParameters *params)
00216 {
00217 int i, j, k, n;
00218
00219 int tx,DistScaleFactor;
00220
00221 int index;
00222 int comp;
00223 double dc_org = 0.0;
00224 double dc_org_UV[2] = { 0.0 };
00225 double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };
00226 double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
00227
00228 int default_weight[3];
00229 int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00230 int weight[6][MAX_REFERENCE_PICTURES][3];
00231 int offset[6][MAX_REFERENCE_PICTURES][3];
00232 int im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
00233 int clist;
00234 int wf_weight, wf_offset;
00235 imgpel **tmpPtr;
00236
00237 if (active_pps->weighted_bipred_idc == 2)
00238 {
00239 luma_log_weight_denom = 5;
00240 chroma_log_weight_denom = 5;
00241 }
00242 else
00243 {
00244 luma_log_weight_denom = 5;
00245 chroma_log_weight_denom = 5;
00246 }
00247
00248 wp_luma_round = 1 << (luma_log_weight_denom - 1);
00249 wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
00250 default_weight[0] = 1 << luma_log_weight_denom;
00251 default_weight[1] = 1 << chroma_log_weight_denom;
00252 default_weight[2] = 1 << chroma_log_weight_denom;
00253
00254 if (active_pps->weighted_bipred_idc == 2)
00255 {
00256 for (i = 0; i < listXsize[LIST_0]; i++)
00257 {
00258 for (j = 0; j < listXsize[LIST_1]; j++)
00259 {
00260 int td, tb;
00261 td = iClip3(-128, 127,(listX[LIST_1][j]->poc - listX[LIST_0][i]->poc));
00262 tb = iClip3(-128, 127,(enc_picture->poc - listX[LIST_0][i]->poc));
00263 for (comp = 0; comp < 3; comp++)
00264 {
00265
00266 if (td == 0)
00267 {
00268 im_weight[0][i][j][comp] = default_weight[comp];
00269 im_weight[1][i][j][comp] = default_weight[comp];
00270 }
00271 else
00272 {
00273 tx = (16384 + iabs(td/2))/td;
00274 DistScaleFactor = iClip3(-1024, 1023, (tx*tb + 32 )>>6);
00275 im_weight[1][i][j][comp] = DistScaleFactor>>2;
00276 if (im_weight[1][i][j][comp] < -64 || im_weight[1][i][j][comp] >128)
00277 im_weight[1][i][j][comp] = default_weight[comp];
00278 im_weight[0][i][j][comp] = 64 - im_weight[1][i][j][comp];
00279 }
00280 }
00281 #if DEBUG_WP
00282 printf ("%d imp weight[%d][%d] = %d , %d (%d %d %d) (%d %d) (%d %d)\n",enc_picture->poc, i, j, im_weight[0][i][j][0], im_weight[1][i][j][0],
00283 enc_picture->poc,listX[LIST_0][i]->poc, listX[LIST_1][j]->poc,
00284 DistScaleFactor ,tx,td,tb);
00285 #endif
00286 }
00287 }
00288
00289 for (k = 0; k < 2; k++)
00290 {
00291 for (i = 0; i < listXsize[LIST_0]; i++)
00292 {
00293 for (j = 0; j < listXsize[LIST_1]; j++)
00294 {
00295 for (comp = 0; comp < 3; comp++)
00296 {
00297 wbp_weight[k][i][j][comp] = im_weight[k][i][j][comp];
00298 }
00299 }
00300 }
00301 }
00302
00303 for (clist=0; clist<2 + list_offset; clist++)
00304 {
00305 for (index = 0; index < listXsize[clist]; index++)
00306 {
00307 for (comp = 0; comp < 3; comp++)
00308 {
00309 wp_weight[clist][index][comp] = default_weight[comp];
00310 wp_offset[clist][index][comp] = 0;
00311 }
00312 }
00313 }
00314 }
00315 else
00316 {
00317 dc_org = ComputeImgSum(pCurImg, img->height, img->width);
00318
00319 if (params->ChromaWeightSupport == 1)
00320 {
00321 for (k = 0; k < 2; k++)
00322 {
00323 dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
00324 }
00325 }
00326
00327 for (clist=0; clist<2 + list_offset; clist++)
00328 {
00329 for (n = 0; n < listXsize[clist]; n++)
00330 {
00331 if ( wpxDetermineWP( params, img, clist, n ) )
00332 {
00333
00334 for (i = 0; i < 3; i++)
00335 {
00336 wp_weight[clist][n][i] = default_weight[i];
00337 wp_offset[clist][n][i] = 0;
00338 offset [clist][n][i] = 0;
00339 weight [clist][n][i] = default_weight[i];
00340 }
00341
00342
00343
00344 tmpPtr = listX[clist][n]->p_curr_img;
00345 dc_ref[clist][n] = ComputeImgSum(tmpPtr, img->height, img->width);
00346
00347 if (dc_ref[clist][n] != 0.0)
00348 wf_weight = (int) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
00349 else
00350 wf_weight = default_weight[0];
00351 wf_weight = iClip3(-128, 127, wf_weight);
00352 wf_offset = 0;
00353
00354
00355
00356 weight[clist][n][0] = wf_weight;
00357 offset[clist][n][0] = wf_offset;
00358
00359
00360 if (params->ChromaWeightSupport == 1)
00361 {
00362 for (k = 0; k < 2; k++)
00363 {
00364 tmpPtr = listX[clist][n]->imgUV[k];
00365 dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
00366
00367 if (dc_ref_UV[clist][n][k] != 0.0)
00368 wf_weight = (int) (default_weight[k + 1] * dc_org_UV[k] / dc_ref_UV[clist][n][k] + 0.5);
00369 else
00370 wf_weight = default_weight[k + 1];
00371 wf_weight = iClip3(-128, 127, wf_weight);
00372 wf_offset = 0;
00373
00374 weight[clist][n][k + 1] = wf_weight;
00375 offset[clist][n][k + 1] = wf_offset;
00376 }
00377 }
00378 else
00379 {
00380 weight[clist][n][1] = default_weight[1];
00381 weight[clist][n][2] = default_weight[2];
00382 offset[clist][n][1] = 0;
00383 offset[clist][n][2] = 0;
00384 }
00385
00386 for (i = 0; i < 3; i++)
00387 {
00388 wp_weight[clist][n][i] = weight[clist][n][i];
00389 wp_offset[clist][n][i] = offset[clist][n][i];
00390 #if DEBUG_WP
00391 printf("%d %d\n",wp_weight[clist][index][comp],wp_offset[clist][index][comp]);
00392 #endif
00393 }
00394 }
00395 }
00396 }
00397
00398 if (active_pps->weighted_bipred_idc != 1)
00399 {
00400 for (clist=0; clist<2 + list_offset; clist++)
00401 {
00402 for (index = 0; index < listXsize[clist]; index++)
00403 {
00404 memcpy(wp_weight[clist][index], default_weight, 3 * sizeof(int));
00405 memset(wp_offset[clist][index], 0, 3 * sizeof(int));
00406 }
00407 }
00408 }
00409
00410
00411 for (i = 0; i < listXsize[LIST_0]; i++)
00412 {
00413 for (j = 0; j < listXsize[LIST_1]; j++)
00414 {
00415 for (comp = 0; comp < 3; comp++)
00416 {
00417 wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
00418 wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
00419 }
00420 #if DEBUG_WP
00421 printf ("bpw weight[%d][%d] = %d , %d (%d %d %d) (%d %d) (%d %d)\n", i, j, wbp_weight[0][i][j][0], wbp_weight[1][i][j][0],
00422 enc_picture->poc,listX[LIST_0][i]->poc, listX[LIST_1][j]->poc,
00423 DistScaleFactor ,tx,tx,tx);
00424 #endif
00425 }
00426 }
00427 }
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 int TestWPPSliceAlg0(ImageParameters *img, InputParameters *params, int select_offset)
00439 {
00440 int i, j, k, n;
00441
00442 int index;
00443 int comp;
00444 double dc_org = 0.0;
00445 double dc_org_UV[2] = {0.0};
00446 double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
00447 double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
00448
00449 int default_weight[3];
00450
00451 int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00452 int weight[2][MAX_REFERENCE_PICTURES][3];
00453 int offset[2][MAX_REFERENCE_PICTURES][3];
00454 int clist;
00455 int perform_wp = 0;
00456 imgpel **tmpPtr;
00457
00458 luma_log_weight_denom = 5;
00459 chroma_log_weight_denom = 5;
00460 wp_luma_round = 1 << (luma_log_weight_denom - 1);
00461 wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
00462 default_weight[0] = 1 << luma_log_weight_denom;
00463 default_weight[1] = default_weight[2] = 1 << chroma_log_weight_denom;
00464
00465
00466 for (i = 0; i < 2 + list_offset; i++)
00467 {
00468 for (j = 0; j < listXsize[i]; j++)
00469 {
00470 for (n = 0; n < 3; n++)
00471 {
00472 weight[i][j][n] = default_weight[n];
00473 wp_weight[i][j][n] = default_weight[n];
00474 wp_offset[i][j][n] = 0;
00475 offset[i][j][n] = 0;
00476 }
00477 }
00478 }
00479
00480 dc_org = ComputeImgSum(pCurImg, img->height, img->width);
00481
00482 if (params->ChromaWeightSupport == 1)
00483 {
00484 for (k = 0; k < 2; k++)
00485 {
00486 dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
00487 }
00488 }
00489
00490 for (clist = 0; clist < 2 + list_offset; clist++)
00491 {
00492 for (n = 0; n < listXsize[clist]; n++)
00493 {
00494 tmpPtr = listX[clist][n]->p_curr_img;
00495 dc_ref[n] = ComputeImgSum(tmpPtr, img->height, img->width);
00496
00497 if (params->ChromaWeightSupport == 1)
00498 {
00499 for (k = 0; k < 2; k++)
00500 {
00501 tmpPtr = listX[clist][n]->imgUV[k];
00502 dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
00503 }
00504 }
00505
00506 if (select_offset == 0)
00507 {
00508 if (dc_ref[n] != 0.0)
00509 weight[clist][n][0] = (int) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
00510 else
00511 weight[clist][n][0] = default_weight[0];
00512 weight[clist][n][0] = iClip3(-128, 127, weight[clist][n][0]);
00513 if (params->ChromaWeightSupport == 1)
00514 {
00515 if (dc_ref_UV[n][0] != 0)
00516 weight[clist][n][1] = (int) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
00517 else
00518 weight[clist][n][1] = default_weight[1];
00519 weight[clist][n][1] = iClip3(-128, 127, weight[clist][n][1]);
00520
00521 if (dc_ref_UV[n][1] != 0)
00522 weight[clist][n][2] = (int) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
00523 else
00524 weight[clist][n][2] = default_weight[2];
00525 weight[clist][n][2] = iClip3(-64, 128, weight[clist][n][2]);
00526 }
00527 }
00528 else
00529 {
00530 offset[clist][n][0] = (int) ((dc_org - dc_ref[n])/(img->size)+0.5);
00531 offset[clist][n][0] = (offset[clist][n][0]+((img->bitdepth_luma-8)>>1))>>(img->bitdepth_luma-8);
00532 offset[clist][n][0] = iClip3( -128, 127, offset[clist][n][0]);
00533 offset[clist][n][0] = offset[clist][n][0]<<(img->bitdepth_luma-8);
00534 weight[clist][n][0] = default_weight[0];
00535
00536 if (params->ChromaWeightSupport == 1)
00537 {
00538 offset[clist][n][1] = (int) ((dc_org_UV[0] - dc_ref_UV[n][0])/(img->size_cr)+0.5);
00539 offset[clist][n][1] = (offset[clist][n][1] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
00540 offset[clist][n][1] = iClip3( -128, 127, offset[clist][n][1]);
00541 offset[clist][n][1] = offset[clist][n][1]<<(img->bitdepth_chroma - 8);
00542
00543 weight[clist][n][1] = default_weight[1];
00544
00545 offset[clist][n][2] = (int) ((dc_org_UV[1] - dc_ref_UV[n][1])/(img->size_cr)+0.5);
00546 offset[clist][n][2] = (offset[clist][n][2] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
00547 offset[clist][n][2] = iClip3( -128, 127, offset[clist][n][2]);
00548 offset[clist][n][2] = offset[clist][n][2]<<(img->bitdepth_chroma - 8);
00549
00550 weight[clist][n][2] = default_weight[2];
00551 }
00552 }
00553 }
00554 }
00555
00556 for (clist=0; clist<2 + list_offset; clist++)
00557 {
00558 for (index = 0; index < listXsize[clist]; index++)
00559 {
00560 for (comp=0; comp < 3; comp ++)
00561 {
00562 int offset_test = params->RDPSliceBTest && active_sps->profile_idc != 66
00563 ? iabs(offset[clist][index][comp]) > 2
00564 : offset[clist][index][comp] != 0;
00565
00566 if (weight[clist][index][comp] != default_weight[0] || offset_test)
00567 {
00568 perform_wp = 1;
00569 break;
00570 }
00571 }
00572 if (perform_wp == 1) break;
00573 }
00574 if (perform_wp == 1) break;
00575 }
00576
00577 return perform_wp;
00578 }
00579
00580
00581
00582
00583
00584
00585
00586
00587 int TestWPBSliceAlg0(ImageParameters *img, InputParameters *params, int select_method)
00588 {
00589 int i, j, k, n;
00590
00591 int tx,DistScaleFactor;
00592
00593 int index;
00594 int comp;
00595 double dc_org = 0.0;
00596 double dc_org_UV[2] = { 0.0 };
00597 double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };
00598 double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
00599
00600 int default_weight[3];
00601
00602 int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
00603 int weight[6][MAX_REFERENCE_PICTURES][3];
00604 int offset[6][MAX_REFERENCE_PICTURES][3];
00605 int im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
00606 int clist;
00607 int wf_weight, wf_offset;
00608 int perform_wp = 0;
00609 imgpel **tmpPtr;
00610
00611 if (select_method == 1)
00612 {
00613 luma_log_weight_denom = 5;
00614 chroma_log_weight_denom = 5;
00615 }
00616 else
00617 {
00618 luma_log_weight_denom = 5;
00619 chroma_log_weight_denom = 5;
00620 }
00621
00622 wp_luma_round = 1 << (luma_log_weight_denom - 1);
00623 wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
00624 default_weight[0] = 1 << luma_log_weight_denom;
00625 default_weight[1] = 1 << chroma_log_weight_denom;
00626 default_weight[2] = 1 << chroma_log_weight_denom;
00627
00628
00629 for (i = 0; i < 2 + list_offset; i++)
00630 {
00631 for (j = 0; j < listXsize[i]; j++)
00632 {
00633 for (n = 0; n < 3; n++)
00634 {
00635 wp_weight[i][j][n] = default_weight[n];
00636 wp_offset[i][j][n] = 0;
00637 offset [i][j][n] = 0;
00638 weight [i][j][n] = default_weight[n];
00639 }
00640 }
00641 }
00642
00643 for (i = 0; i < listXsize[LIST_0]; i++)
00644 {
00645 for (j = 0; j < listXsize[LIST_1]; j++)
00646 {
00647 int td, tb;
00648 td = iClip3(-128, 127,(listX[LIST_1][j]->poc - listX[LIST_0][i]->poc));
00649 tb = iClip3(-128, 127,(enc_picture->poc - 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 = (16384 + iabs(td/2))/td;
00661 DistScaleFactor = iClip3(-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 < listXsize[LIST_0]; i++)
00675 {
00676 for (j = 0; j < 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 < 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(pCurImg, img->height, img->width);
00701
00702 if (params->ChromaWeightSupport == 1)
00703 {
00704 for (k = 0; k < 2; k++)
00705 {
00706 dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
00707 }
00708 }
00709
00710 for (clist=0; clist<2 + list_offset; clist++)
00711 {
00712 for (n = 0; n < listXsize[clist]; n++)
00713 {
00714
00715
00716
00717 tmpPtr = listX[clist][n]->p_curr_img;
00718 dc_ref[clist][n] = ComputeImgSum(tmpPtr, img->height, img->width);
00719
00720 if (dc_ref[clist][n] != 0.0)
00721 wf_weight = (int) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
00722 else
00723 wf_weight = default_weight[0];
00724 wf_weight = iClip3(-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 (params->ChromaWeightSupport == 1)
00732 {
00733 for (k = 0; k < 2; k++)
00734 {
00735 tmpPtr = listX[clist][n]->imgUV[k];
00736 dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
00737
00738 if (dc_ref_UV[clist][n][k] != 0.0)
00739 wf_weight = (int) (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 = iClip3(-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 < listXsize[clist]; index++)
00764 {
00765 memcpy(wp_weight[clist][index], weight[clist][index], 3 * sizeof(int));
00766 memcpy(wp_offset[clist][index], offset[clist][index], 3 * sizeof(int));
00767 }
00768 }
00769 }
00770 else
00771 {
00772 for (clist=0; clist<2 + list_offset; clist++)
00773 {
00774 for (index = 0; index < listXsize[clist]; index++)
00775 {
00776 memcpy(wp_weight[clist][index], default_weight, 3 * sizeof(int));
00777 memset(wp_offset[clist][index], 0, 3 * sizeof(int));
00778 }
00779 }
00780 }
00781
00782 for (i = 0; i < listXsize[LIST_0]; i++)
00783 {
00784 for (j = 0; j < 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, wbp_weight[0][i][j][0], wbp_weight[1][i][j][0],
00793 enc_picture->poc,listX[LIST_0][i]->poc, 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] = (params->B_List0_refs == 0 ? listXsize[0] : imin(params->B_List0_refs, listXsize[0]));
00805 active_refs[1] = (params->B_List1_refs == 0 ? listXsize[1] : imin(params->B_List1_refs, 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 }