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