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