weighted_prediction.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:27k
源码类别:

Audio

开发平台:

Visual C++

  1. /*!
  2. *************************************************************************************
  3. * file weighted_prediction.c
  4. *
  5. * brief
  6. *    Estimate weights for WP using DC method
  7. *
  8. * author
  9. *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10. *     - Alexis Michael Tourapis         <alexismt@ieee.org>
  11. *     - Athanasios Leontaris            <aleon@dolby.com>
  12. *************************************************************************************
  13. */
  14. #include "contributors.h"
  15. #include "global.h"
  16. #include "image.h"
  17. #include "wp.h"
  18. /*!
  19. ************************************************************************
  20. * brief
  21. *    Initialize weighting parameter functions
  22. ************************************************************************
  23. */
  24. void InitWP(InputParameters *params)
  25. {
  26.   switch ( params->WPMethod )
  27.   {
  28.   default:
  29.   case 0:
  30.     EstimateWPPSlice = EstimateWPPSliceAlg0;
  31.     EstimateWPBSlice = EstimateWPBSliceAlg0;
  32.     TestWPPSlice = TestWPPSliceAlg0;
  33.     TestWPBSlice = TestWPBSliceAlg0;
  34.     break;
  35.   case 1:
  36.     EstimateWPPSlice = EstimateWPPSliceAlg1;
  37.     EstimateWPBSlice = EstimateWPBSliceAlg1;
  38.     TestWPPSlice = TestWPPSliceAlg1;
  39.     TestWPBSlice = TestWPBSliceAlg1;
  40.     break;
  41.   }
  42. }
  43. /*!
  44. ************************************************************************
  45. * brief
  46. *    Compute sum of samples in a picture
  47. ************************************************************************
  48. */
  49. double ComputeImgSum(imgpel **CurrentImage, int height, int width)
  50. {
  51.   int i, j;
  52.   double sum_value = 0.0;
  53.   for (i = 0; i < height; i++)
  54.   {
  55.     for (j = 0; j < width; j++)
  56.     {
  57.       sum_value += (double) CurrentImage[i][j];
  58.     }
  59.   }
  60.   return sum_value;
  61. }
  62. /*!
  63. ************************************************************************
  64. * brief
  65. *    Estimates reference picture weighting factors for P slices
  66. ************************************************************************
  67. */
  68. void EstimateWPPSliceAlg0(ImageParameters *img, InputParameters *params, int select_offset)
  69. {
  70.   double dc_org = 0.0;
  71.   double dc_org_UV[2] = {0.0};
  72.   double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
  73.   double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
  74.   int i, n, k;
  75.   int default_weight[3];
  76.   int list_offset   = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
  77.   int weight[2][MAX_REFERENCE_PICTURES][3];
  78.   int offset[2][MAX_REFERENCE_PICTURES][3];
  79.   int clist;
  80.   imgpel **tmpPtr;
  81.   luma_log_weight_denom   = 5;
  82.   chroma_log_weight_denom = 5;
  83.   wp_luma_round           = 1 << (luma_log_weight_denom - 1);
  84.   wp_chroma_round         = 1 << (chroma_log_weight_denom - 1);
  85.   default_weight[0]       = 1 << luma_log_weight_denom;
  86.   default_weight[1]       = default_weight[2] = 1 << chroma_log_weight_denom;
  87.   
  88.   dc_org = ComputeImgSum(pCurImg, img->height, img->width);
  89.   if (params->ChromaWeightSupport == 1)
  90.   {
  91.     for (k = 0; k < 2; k++)
  92.     {
  93.       dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
  94.     } 
  95.   }
  96.   for (clist = 0; clist < 2 + list_offset; clist++)
  97.   {
  98.     for (n = 0; n < listXsize[clist]; n++)
  99.     {
  100.       if ( wpxDetermineWP( params, img, clist, n ) )
  101.       {
  102.         /* set all values to defaults */
  103.         for (i = 0; i < 3; i++)
  104.         {
  105.           weight[clist][n][i]    = default_weight[i];
  106.           wp_weight[clist][n][i] = default_weight[i];
  107.           wp_offset[clist][n][i] = 0;
  108.           offset[clist][n][i]    = 0;
  109.         }
  110.         // Y
  111.         tmpPtr = listX[clist][n]->p_curr_img;      
  112.         dc_ref[n] = ComputeImgSum(tmpPtr, img->height, img->width);
  113.         if (params->ChromaWeightSupport == 1)
  114.         {
  115.           for (k = 0; k < 2; k++)
  116.           {
  117.             // UV
  118.             tmpPtr = listX[clist][n]->imgUV[k];
  119.             dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
  120.           }        
  121.         }
  122.         if (select_offset == 0)
  123.         {
  124.           if (dc_ref[n] != 0.0)
  125.             weight[clist][n][0] = (int) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
  126.           else
  127.             weight[clist][n][0] = default_weight[0];  // only used when reference picture is black
  128.           weight[clist][n][0] = iClip3(-128, 127, weight[clist][n][0]);
  129.           if (params->ChromaWeightSupport == 1)
  130.           {
  131.             if (dc_ref_UV[n][0] != 0)
  132.               weight[clist][n][1] = (int) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
  133.             else
  134.               weight[clist][n][1] = default_weight[1];  // only used when reference picture is black
  135.             weight[clist][n][1] = iClip3(-128, 127, weight[clist][n][1]);
  136.             if (dc_ref_UV[n][1] != 0)
  137.               weight[clist][n][2] = (int) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
  138.             else
  139.               weight[clist][n][2] = default_weight[2];  // only used when reference picture is black
  140.             weight[clist][n][2] = iClip3(-64, 128, weight[clist][n][2]);
  141.           }
  142.         }
  143.         else
  144.         {
  145.           offset[clist][n][0] = (int) ((dc_org - dc_ref[n])/(img->size)+0.5);
  146.           offset[clist][n][0] = (offset[clist][n][0]+((img->bitdepth_luma-8)>>1))>>(img->bitdepth_luma-8);
  147.           offset[clist][n][0] = iClip3( -128, 127, offset[clist][n][0]);
  148.           offset[clist][n][0] = offset[clist][n][0]<<(img->bitdepth_luma-8);
  149.           weight[clist][n][0] = default_weight[0];
  150.           if (params->ChromaWeightSupport == 1)
  151.           {
  152.             offset[clist][n][1] = (int) ((dc_org_UV[0] - dc_ref_UV[n][0])/(img->size_cr)+0.5);
  153.             offset[clist][n][1] = (offset[clist][n][1] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
  154.             offset[clist][n][1] = iClip3( -128, 127, offset[clist][n][1]);
  155.             offset[clist][n][1] = offset[clist][n][1]<<(img->bitdepth_chroma - 8);
  156.             
  157.             weight[clist][n][1] = default_weight[1];
  158.             offset[clist][n][2] = (int) ((dc_org_UV[1] - dc_ref_UV[n][1])/(img->size_cr)+0.5);
  159.             offset[clist][n][2] = (offset[clist][n][2] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
  160.             offset[clist][n][2] = iClip3( -128, 127, offset[clist][n][2]);
  161.             offset[clist][n][2] = offset[clist][n][2]<<(img->bitdepth_chroma - 8);
  162.             weight[clist][n][2] = default_weight[2];
  163.           }
  164.         }
  165.         for (i=0; i < 3; i ++)
  166.         {
  167.           wp_weight[clist][n][i] = weight[clist][n][i];
  168.           wp_offset[clist][n][i] = offset[clist][n][i];
  169. #if DEBUG_WP
  170.           printf("index %d component %d weight %d offset %dn",n,i,weight[0][n][i],offset[0][n][i]);
  171. #endif
  172.         }
  173.       }
  174.     }
  175.   }
  176. }
  177. /*!
  178. ************************************************************************
  179. * brief
  180. *    Estimates reference picture weighting factors for B slices
  181. ************************************************************************
  182. */
  183. void EstimateWPBSliceAlg0(ImageParameters *img, InputParameters *params)
  184. {
  185.   int i, j, k, n;
  186.   int tx,DistScaleFactor;
  187.   int index;
  188.   int comp;
  189.   double dc_org = 0.0;
  190.   double dc_org_UV[2] = { 0.0 };
  191.   double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };
  192.   double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
  193.   int default_weight[3];
  194.   int list_offset   = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
  195.   int weight[6][MAX_REFERENCE_PICTURES][3];
  196.   int offset[6][MAX_REFERENCE_PICTURES][3];
  197.   int im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
  198.   int clist;
  199.   int wf_weight, wf_offset;
  200.   imgpel **tmpPtr;
  201.   if (active_pps->weighted_bipred_idc == 2) //! implicit mode. Values are fixed and it is important to show it here
  202.   {
  203.     luma_log_weight_denom = 5;
  204.     chroma_log_weight_denom = 5;
  205.   }
  206.   else                                     //! explicit mode. Values can be changed for higher precision.
  207.   {
  208.     luma_log_weight_denom = 5;
  209.     chroma_log_weight_denom = 5;
  210.   }
  211.   wp_luma_round     = 1 << (luma_log_weight_denom - 1);
  212.   wp_chroma_round   = 1 << (chroma_log_weight_denom - 1);
  213.   default_weight[0] = 1 << luma_log_weight_denom;
  214.   default_weight[1] = 1 << chroma_log_weight_denom;
  215.   default_weight[2] = 1 << chroma_log_weight_denom;
  216.   if (active_pps->weighted_bipred_idc == 2) //! implicit mode
  217.   {
  218.     for (i = 0; i < listXsize[LIST_0]; i++)
  219.     {
  220.       for (j = 0; j < listXsize[LIST_1]; j++)
  221.       {
  222.         int td, tb;
  223.         td = iClip3(-128, 127,(listX[LIST_1][j]->poc - listX[LIST_0][i]->poc));
  224.         tb = iClip3(-128, 127,(enc_picture->poc - listX[LIST_0][i]->poc));
  225.         for (comp = 0; comp < 3; comp++)
  226.         {
  227.           // implicit weights
  228.           if (td == 0)
  229.           {
  230.             im_weight[0][i][j][comp] = default_weight[comp];
  231.             im_weight[1][i][j][comp] = default_weight[comp];
  232.           }
  233.           else
  234.           {
  235.             tx = (16384 + iabs(td/2))/td;
  236.             DistScaleFactor = iClip3(-1024, 1023, (tx*tb + 32 )>>6);
  237.             im_weight[1][i][j][comp] = DistScaleFactor>>2;
  238.             if (im_weight[1][i][j][comp] < -64 || im_weight[1][i][j][comp] >128)
  239.               im_weight[1][i][j][comp] = default_weight[comp];
  240.             im_weight[0][i][j][comp] = 64 - im_weight[1][i][j][comp];
  241.           }
  242.         }
  243. #if DEBUG_WP
  244.         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],
  245.           enc_picture->poc,listX[LIST_0][i]->poc, listX[LIST_1][j]->poc,
  246.           DistScaleFactor ,tx,td,tb);
  247. #endif
  248.       }
  249.     }
  250.     for (k = 0; k < 2; k++)
  251.     {
  252.       for (i = 0; i < listXsize[LIST_0]; i++)
  253.       {
  254.         for (j = 0; j < listXsize[LIST_1]; j++)
  255.         {
  256.           for (comp = 0; comp < 3; comp++)
  257.           {
  258.             wbp_weight[k][i][j][comp] = im_weight[k][i][j][comp];
  259.           }
  260.         }
  261.       }
  262.     }
  263.     for (clist=0; clist<2 + list_offset; clist++)
  264.     {
  265.       for (index = 0; index < listXsize[clist]; index++)
  266.       {
  267.         for (comp = 0; comp < 3; comp++)
  268.         {
  269.           wp_weight[clist][index][comp] = default_weight[comp];
  270.           wp_offset[clist][index][comp] = 0;
  271.         }
  272.       }
  273.     }
  274.   }
  275.   else
  276.   {
  277.     dc_org = ComputeImgSum(pCurImg, img->height, img->width);
  278.     if (params->ChromaWeightSupport == 1)
  279.     {
  280.       for (k = 0; k < 2; k++)
  281.       {
  282.         dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
  283.       } 
  284.     }
  285.     for (clist=0; clist<2 + list_offset; clist++)
  286.     {
  287.       for (n = 0; n < listXsize[clist]; n++)
  288.       {
  289.         if ( wpxDetermineWP( params, img, clist, n ) )
  290.         {
  291.           /* set all values to defaults */
  292.           for (i = 0; i < 3; i++)
  293.           {
  294.             wp_weight[clist][n][i] = default_weight[i];
  295.             wp_offset[clist][n][i] = 0;
  296.             offset   [clist][n][i] = 0;
  297.             weight   [clist][n][i] = default_weight[i];
  298.           }
  299.           // To simplify these computations we may wish to perform these after a reference is 
  300.           // stored in the reference buffer and attach them to the storedimage structure!!!
  301.           // Y
  302.           tmpPtr = listX[clist][n]->p_curr_img;
  303.           dc_ref[clist][n] = ComputeImgSum(tmpPtr, img->height, img->width);
  304.           if (dc_ref[clist][n] != 0.0)
  305.             wf_weight = (int) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
  306.           else
  307.             wf_weight = default_weight[0];  // only used when reference picture is black
  308.           wf_weight = iClip3(-128, 127, wf_weight);
  309.           wf_offset = 0;
  310.           //    printf("dc_org = %d, dc_ref = %d, weight[%d] = %dn",dc_org, dc_ref[n],n,weight[n][0]);
  311.           weight[clist][n][0] = wf_weight;
  312.           offset[clist][n][0] = wf_offset;
  313.           // UV
  314.           if (params->ChromaWeightSupport == 1)
  315.           {          
  316.             for (k = 0; k < 2; k++)
  317.             {        
  318.               tmpPtr = listX[clist][n]->imgUV[k];
  319.               dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
  320.               if (dc_ref_UV[clist][n][k] != 0.0)
  321.                 wf_weight = (int) (default_weight[k + 1] * dc_org_UV[k] / dc_ref_UV[clist][n][k] + 0.5);
  322.               else
  323.                 wf_weight = default_weight[k + 1];  // only used when reference picture is black
  324.               wf_weight = iClip3(-128, 127, wf_weight);
  325.               wf_offset = 0;
  326.               weight[clist][n][k + 1] = wf_weight;
  327.               offset[clist][n][k + 1] = wf_offset;
  328.             }
  329.           }
  330.           else
  331.           {
  332.             weight[clist][n][1] = default_weight[1];
  333.             weight[clist][n][2] = default_weight[2];        
  334.             offset[clist][n][1] = 0;
  335.             offset[clist][n][2] = 0;
  336.           }
  337.           for (i = 0; i < 3; i++)
  338.           {
  339.             wp_weight[clist][n][i] = weight[clist][n][i];
  340.             wp_offset[clist][n][i] = offset[clist][n][i];
  341. #if DEBUG_WP
  342.             printf("%d %dn",wp_weight[clist][index][comp],wp_offset[clist][index][comp]);
  343. #endif
  344.           }
  345.         }
  346.       }
  347.     }
  348.     if (active_pps->weighted_bipred_idc != 1)
  349.     {
  350.       for (clist=0; clist<2 + list_offset; clist++)
  351.       {
  352.         for (index = 0; index < listXsize[clist]; index++)
  353.         {
  354.           memcpy(wp_weight[clist][index], default_weight, 3 * sizeof(int));
  355.           memset(wp_offset[clist][index], 0, 3 * sizeof(int));
  356.         }
  357.       }
  358.     }
  359.     for (i = 0; i < listXsize[LIST_0]; i++)
  360.     {
  361.       for (j = 0; j < listXsize[LIST_1]; j++)
  362.       {
  363.         for (comp = 0; comp < 3; comp++)
  364.         {
  365.           wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
  366.           wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
  367.         }
  368. #if DEBUG_WP
  369.         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],
  370.           enc_picture->poc,listX[LIST_0][i]->poc, listX[LIST_1][j]->poc,
  371.           DistScaleFactor ,tx,tx,tx);
  372. #endif
  373.       }
  374.     }
  375.   }
  376. }
  377. /*!
  378. ************************************************************************
  379. * brief
  380. *    Tests P slice weighting factors to perform or not WP RD decision
  381. ************************************************************************
  382. */
  383. int TestWPPSliceAlg0(ImageParameters *img, InputParameters *params, int select_offset)
  384. {
  385.   int i, j, k, n;
  386.   int index;
  387.   int comp;
  388.   double dc_org = 0.0;
  389.   double dc_org_UV[2] = {0.0};  
  390.   double dc_ref[MAX_REFERENCE_PICTURES] = { 0.0 };
  391.   double dc_ref_UV[MAX_REFERENCE_PICTURES][2] = { {0.0}};
  392.   int default_weight[3];
  393.   int list_offset   = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
  394.   int weight[2][MAX_REFERENCE_PICTURES][3];
  395.   int offset[2][MAX_REFERENCE_PICTURES][3];
  396.   int clist;
  397.   int perform_wp = 0;
  398.   imgpel **tmpPtr;
  399.   luma_log_weight_denom = 5;
  400.   chroma_log_weight_denom = 5;
  401.   wp_luma_round = 1 << (luma_log_weight_denom - 1);
  402.   wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
  403.   default_weight[0] = 1 << luma_log_weight_denom;
  404.   default_weight[1] = default_weight[2] = 1 << chroma_log_weight_denom;
  405.   /* set all values to defaults */
  406.   for (i = 0; i < 2 + list_offset; i++)
  407.   {
  408.     for (j = 0; j < listXsize[i]; j++)
  409.     {
  410.       for (n = 0; n < 3; n++)
  411.       {
  412.         weight[i][j][n] = default_weight[n];
  413.         wp_weight[i][j][n] = default_weight[n];
  414.         wp_offset[i][j][n] = 0;
  415.         offset[i][j][n] = 0;
  416.       }
  417.     }
  418.   }
  419.   dc_org = ComputeImgSum(pCurImg, img->height, img->width);
  420.   if (params->ChromaWeightSupport == 1)
  421.   {
  422.     for (k = 0; k < 2; k++)
  423.     {
  424.       dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
  425.     } 
  426.   }
  427.   for (clist = 0; clist < 2 + list_offset; clist++)
  428.   {
  429.     for (n = 0; n < listXsize[clist]; n++)
  430.     {
  431.       tmpPtr = listX[clist][n]->p_curr_img;
  432.       dc_ref[n] = ComputeImgSum(tmpPtr, img->height, img->width);
  433.       if (params->ChromaWeightSupport == 1)
  434.       {
  435.         for (k = 0; k < 2; k++)
  436.         {
  437.           tmpPtr = listX[clist][n]->imgUV[k];
  438.           dc_ref_UV[n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
  439.         }        
  440.       }
  441.       if (select_offset == 0)
  442.       {
  443.         if (dc_ref[n] != 0.0)
  444.           weight[clist][n][0] = (int) (default_weight[0] * dc_org / dc_ref[n] + 0.5);
  445.         else
  446.           weight[clist][n][0] = default_weight[0];  // only used when reference picture is black
  447.         weight[clist][n][0] = iClip3(-128, 127, weight[clist][n][0]);
  448.         if (params->ChromaWeightSupport == 1)
  449.         {
  450.           if (dc_ref_UV[n][0] != 0)
  451.             weight[clist][n][1] = (int) (default_weight[1] * dc_org_UV[0] / dc_ref_UV[n][0] + 0.5);
  452.           else
  453.             weight[clist][n][1] = default_weight[1];  // only used when reference picture is black
  454.           weight[clist][n][1] = iClip3(-128, 127, weight[clist][n][1]);
  455.           if (dc_ref_UV[n][1] != 0)
  456.             weight[clist][n][2] = (int) (default_weight[2] * dc_org_UV[1] / dc_ref_UV[n][1] + 0.5);
  457.           else
  458.             weight[clist][n][2] = default_weight[2];  // only used when reference picture is black
  459.           weight[clist][n][2] = iClip3(-64, 128, weight[clist][n][2]);
  460.         }
  461.       }
  462.       else
  463.       {
  464.         offset[clist][n][0] = (int) ((dc_org - dc_ref[n])/(img->size)+0.5);
  465.         offset[clist][n][0] = (offset[clist][n][0]+((img->bitdepth_luma-8)>>1))>>(img->bitdepth_luma-8);
  466.         offset[clist][n][0] = iClip3( -128, 127, offset[clist][n][0]);
  467.         offset[clist][n][0] = offset[clist][n][0]<<(img->bitdepth_luma-8);
  468.         weight[clist][n][0] = default_weight[0];
  469.         if (params->ChromaWeightSupport == 1)
  470.         {
  471.             offset[clist][n][1] = (int) ((dc_org_UV[0] - dc_ref_UV[n][0])/(img->size_cr)+0.5);
  472.             offset[clist][n][1] = (offset[clist][n][1] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
  473.             offset[clist][n][1] = iClip3( -128, 127, offset[clist][n][1]);
  474.             offset[clist][n][1] = offset[clist][n][1]<<(img->bitdepth_chroma - 8);
  475.             
  476.             weight[clist][n][1] = default_weight[1];
  477.             offset[clist][n][2] = (int) ((dc_org_UV[1] - dc_ref_UV[n][1])/(img->size_cr)+0.5);
  478.             offset[clist][n][2] = (offset[clist][n][2] + ((img->bitdepth_chroma - 8)>>1))>>(img->bitdepth_chroma-8);
  479.             offset[clist][n][2] = iClip3( -128, 127, offset[clist][n][2]);
  480.             offset[clist][n][2] = offset[clist][n][2]<<(img->bitdepth_chroma - 8);
  481.             weight[clist][n][2] = default_weight[2];
  482.         }
  483.       }
  484.     }
  485.   }
  486.   for (clist=0; clist<2 + list_offset; clist++)
  487.   {
  488.     for (index = 0; index < listXsize[clist]; index++)
  489.     {
  490.       for (comp=0; comp < 3; comp ++)
  491.       {
  492.         int offset_test = params->RDPSliceBTest && active_sps->profile_idc != 66
  493.           ? iabs(offset[clist][index][comp]) > 2
  494.           : offset[clist][index][comp] != 0;
  495.         if (weight[clist][index][comp] != default_weight[0] || offset_test)
  496.         {
  497.           perform_wp = 1;
  498.           break;
  499.         }
  500.       }
  501.       if (perform_wp == 1) break;
  502.     }
  503.     if (perform_wp == 1) break;
  504.   }
  505.   return perform_wp;
  506. }
  507. /*!
  508. ************************************************************************
  509. * brief
  510. *    TestWPBSliceAlg0:
  511. *    Tests B slice weighting prediction
  512. ************************************************************************
  513. */
  514. int TestWPBSliceAlg0(ImageParameters *img, InputParameters *params, int select_method)
  515. {
  516.   int i, j, k, n;
  517.   int tx,DistScaleFactor;
  518.   int index;
  519.   int comp;
  520.   double dc_org = 0.0;
  521.   double dc_org_UV[2] = { 0.0 };    
  522.   double dc_ref[6][MAX_REFERENCE_PICTURES] = { {0.0} };  
  523.   double dc_ref_UV[6][MAX_REFERENCE_PICTURES][2] = { {{0.0}} };
  524.   int default_weight[3];
  525.   // this needs to be fixed.
  526.   int list_offset   = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? (img->current_mb_nr & 0x01) ? 4 : 2 : 0;
  527.   int weight[6][MAX_REFERENCE_PICTURES][3];
  528.   int offset[6][MAX_REFERENCE_PICTURES][3];
  529.   int im_weight[6][MAX_REFERENCE_PICTURES][MAX_REFERENCE_PICTURES][3];
  530.   int clist;
  531.   int wf_weight, wf_offset;
  532.   int perform_wp = 0;
  533.   imgpel **tmpPtr;
  534.   if (select_method == 1) //! implicit mode
  535.   {
  536.     luma_log_weight_denom = 5;
  537.     chroma_log_weight_denom = 5;
  538.   }
  539.   else
  540.   {
  541.     luma_log_weight_denom = 5;
  542.     chroma_log_weight_denom = 5;
  543.   }
  544.   wp_luma_round     = 1 << (luma_log_weight_denom - 1);
  545.   wp_chroma_round   = 1 << (chroma_log_weight_denom - 1);
  546.   default_weight[0] = 1 << luma_log_weight_denom;
  547.   default_weight[1] = 1 << chroma_log_weight_denom;
  548.   default_weight[2] = 1 << chroma_log_weight_denom;
  549.   /* set all values to defaults */
  550.   for (i = 0; i < 2 + list_offset; i++)
  551.   {
  552.     for (j = 0; j < listXsize[i]; j++)
  553.     {
  554.       for (n = 0; n < 3; n++)
  555.       {
  556.         wp_weight[i][j][n] = default_weight[n];
  557.         wp_offset[i][j][n] = 0;
  558.         offset   [i][j][n] = 0;
  559.         weight   [i][j][n] = default_weight[n];
  560.       }
  561.     }
  562.   }
  563.   for (i = 0; i < listXsize[LIST_0]; i++)
  564.   {
  565.     for (j = 0; j < listXsize[LIST_1]; j++)
  566.     {
  567.       int td, tb;
  568.       td = iClip3(-128, 127,(listX[LIST_1][j]->poc - listX[LIST_0][i]->poc));
  569.       tb = iClip3(-128, 127,(enc_picture->poc - listX[LIST_0][i]->poc));
  570.       for (comp = 0; comp < 3; comp++)
  571.       {
  572.         // implicit weights
  573.         if (td == 0)
  574.         {
  575.           im_weight[0][i][j][comp] = default_weight[comp];
  576.           im_weight[1][i][j][comp] = default_weight[comp];
  577.         }
  578.         else
  579.         {
  580.           tx = (16384 + iabs(td/2))/td;
  581.           DistScaleFactor = iClip3(-1024, 1023, (tx*tb + 32 )>>6);
  582.           im_weight[1][i][j][comp] = DistScaleFactor >> 2;
  583.           if (im_weight[1][i][j][comp] < -64 || im_weight[1][i][j][comp] >128)
  584.             im_weight[1][i][j][comp] = default_weight[comp];
  585.           im_weight[0][i][j][comp] = 64 - im_weight[1][i][j][comp];
  586.         }
  587.       }
  588.     }
  589.   }
  590.   if (select_method == 1) //! implicit mode
  591.   {
  592.     for (i = 0; i < listXsize[LIST_0]; i++)
  593.     {
  594.       for (j = 0; j < listXsize[LIST_1]; j++)
  595.       {
  596.         for (comp = 0; comp < 3; comp++)
  597.         {
  598.           wbp_weight[1][i][j][comp] = im_weight[1][i][j][comp] ;
  599.           wbp_weight[0][i][j][comp] = im_weight[0][i][j][comp];
  600.         }
  601.       }
  602.     }
  603.     for (clist=0; clist<2 + list_offset; clist++)
  604.     {
  605.       for (index = 0; index < listXsize[clist]; index++)
  606.       {
  607.         for (comp = 0; comp < 3; comp++)
  608.         {
  609.           wp_weight[clist][index][comp] = default_weight[comp];
  610.           wp_offset[clist][index][comp] = 0;
  611.         }
  612.       }
  613.     }
  614.   }
  615.   else
  616.   {
  617.     dc_org = ComputeImgSum(pCurImg, img->height, img->width);
  618.     if (params->ChromaWeightSupport == 1)
  619.     {
  620.       for (k = 0; k < 2; k++)
  621.       {
  622.         dc_org_UV[k] = ComputeImgSum(pImgOrg[k + 1], img->height_cr, img->width_cr);
  623.       } 
  624.     }
  625.     for (clist=0; clist<2 + list_offset; clist++)
  626.     {
  627.       for (n = 0; n < listXsize[clist]; n++)
  628.       {
  629.         // To simplify these computations we may wish to perform these after a reference is 
  630.         // stored in the reference buffer and attach them to the storedimage structure!!!
  631.         // Y
  632.         tmpPtr = listX[clist][n]->p_curr_img;
  633.         dc_ref[clist][n] = ComputeImgSum(tmpPtr, img->height, img->width);
  634.         if (dc_ref[clist][n] != 0.0)
  635.           wf_weight = (int) (default_weight[0] * dc_org / dc_ref[clist][n] + 0.5);
  636.         else
  637.           wf_weight = default_weight[0];  // only used when reference picture is black
  638.         wf_weight = iClip3(-128, 127, wf_weight);
  639.         wf_offset = 0;
  640.         weight[clist][n][0] = wf_weight;
  641.         offset[clist][n][0] = wf_offset;
  642.         // UV
  643.         if (params->ChromaWeightSupport == 1)
  644.         {          
  645.           for (k = 0; k < 2; k++)
  646.           {
  647.             tmpPtr = listX[clist][n]->imgUV[k];
  648.             dc_ref_UV[clist][n][k] = ComputeImgSum(tmpPtr, img->height_cr, img->width_cr);
  649.             if (dc_ref_UV[clist][n][k] != 0.0)
  650.               wf_weight = (int) (default_weight[k + 1] * dc_org_UV[k] / dc_ref_UV[clist][n][k] + 0.5);
  651.             else
  652.               wf_weight = default_weight[k + 1];  // only used when reference picture is black
  653.             wf_weight = iClip3(-128, 127, wf_weight);
  654.             wf_offset = 0;
  655.             weight[clist][n][k + 1] = wf_weight;
  656.             offset[clist][n][k + 1] = wf_offset;
  657.           }
  658.         }
  659.         else
  660.         {
  661.           weight[clist][n][1] = default_weight[1];
  662.           weight[clist][n][2] = default_weight[2];        
  663.           offset[clist][n][1] = 0;
  664.           offset[clist][n][2] = 0;
  665.         }
  666.       }
  667.     }
  668.     if (select_method == 0) //! explicit mode
  669.     {
  670.       for (clist=0; clist<2 + list_offset; clist++)
  671.       {
  672.         for (index = 0; index < listXsize[clist]; index++)
  673.         {
  674.           memcpy(wp_weight[clist][index], weight[clist][index], 3 * sizeof(int));
  675.           memcpy(wp_offset[clist][index], offset[clist][index], 3 * sizeof(int));
  676.         }
  677.       }
  678.     }
  679.     else
  680.     {
  681.       for (clist=0; clist<2 + list_offset; clist++)
  682.       {
  683.         for (index = 0; index < listXsize[clist]; index++)
  684.         {
  685.           memcpy(wp_weight[clist][index], default_weight, 3 * sizeof(int));
  686.           memset(wp_offset[clist][index], 0, 3 * sizeof(int));
  687.         }
  688.       }
  689.     }
  690.     for (i = 0; i < listXsize[LIST_0]; i++)
  691.     {
  692.       for (j = 0; j < listXsize[LIST_1]; j++)
  693.       {
  694.         for (comp = 0; comp < 3; comp++)
  695.         {
  696.           wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
  697.           wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
  698.         }
  699. #if DEBUG_WP
  700.         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],
  701.         enc_picture->poc,listX[LIST_0][i]->poc, listX[LIST_1][j]->poc,
  702.         DistScaleFactor ,tx,tx,tx);
  703. #endif
  704.       }
  705.     }
  706.   }
  707.   if (select_method == 0) //! implicit mode
  708.   {
  709.     int active_refs[2];
  710.     active_refs[0] = (params->B_List0_refs == 0 ? listXsize[0] : imin(params->B_List0_refs, listXsize[0]));
  711.     active_refs[1] = (params->B_List1_refs == 0 ? listXsize[1] : imin(params->B_List1_refs, listXsize[1]));
  712.     for (clist=0; clist<2 + list_offset; clist++)
  713.     {
  714.       for (index = 0; index < active_refs[clist]; index++)
  715.       {
  716.         for (comp=0; comp < 3; comp ++)
  717.         {
  718.           if (wp_weight[clist][index][comp] != default_weight[comp])
  719.           {
  720.             perform_wp = 1;
  721.             break;
  722.           }
  723.         }
  724.         if (perform_wp == 1) break;
  725.       }
  726.       if (perform_wp == 1) break;
  727.     }
  728.   }
  729.   return perform_wp;
  730. }