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