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