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

Audio

开发平台:

Visual C++

  1. /*!
  2.  *************************************************************************************
  3.  * file quant4x4.c
  4.  *
  5.  * brief
  6.  *    Quantization process for a 4x4 block
  7.  *
  8.  * author
  9.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10.  *    - Alexis Michael Tourapis                  <alexismt@ieee.org>
  11.  *    - Limin Liu                                <limin.liu@dolby.com>
  12.  *
  13.  *************************************************************************************
  14.  */
  15. #include "contributors.h"
  16. #include <math.h>
  17. #include "global.h"
  18. #include "image.h"
  19. #include "mb_access.h"
  20. #include "vlc.h"
  21. #include "transform.h"
  22. #include "mc_prediction.h"
  23. #include "q_offsets.h"
  24. #include "q_matrix.h"
  25. #include "quant4x4.h"
  26. /*!
  27.  ************************************************************************
  28.  * brief
  29.  *    Quantization process for All coefficients for a 4x4 block
  30.  *
  31.  * par Input:
  32.  *
  33.  * par Output:
  34.  *
  35.  ************************************************************************
  36.  */
  37. int quant_4x4_around(int (*tblock)[16], int block_y, int block_x, int  qp,                 
  38.                      int*  ACLevel, int*  ACRun, 
  39.                      int **fadjust4x4, int **levelscale, int **invlevelscale, int **leveloffset,
  40.                      int *coeff_cost, const byte (*pos_scan)[2], const byte *c_cost, int is_cavlc)
  41. {
  42.   static int i,j, coeff_ctr;
  43.   static int *m7;
  44.   static int scaled_coeff;
  45.   int   level, run = 0;
  46.   int   nonzero = FALSE;
  47.   int   qp_per = qp_per_matrix[qp];
  48.   int   q_bits = Q_BITS + qp_per;
  49.   const byte *p_scan = &pos_scan[0][0];
  50.   int*  ACL = &ACLevel[0];
  51.   int*  ACR = &ACRun[0];
  52.   // Quantization
  53.   for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
  54.   {
  55.     i = *p_scan++;  // horizontal position
  56.     j = *p_scan++;  // vertical position
  57.     m7 = &tblock[j][block_x + i];
  58.     if (*m7 != 0)
  59.     {
  60.       scaled_coeff = iabs (*m7) * levelscale[j][i];
  61.       level = (scaled_coeff + leveloffset[j][i]) >> q_bits;
  62.       if (level != 0)
  63.       {
  64.         if (is_cavlc)
  65.           level = imin(level, CAVLC_LEVEL_LIMIT);
  66.         fadjust4x4[j][block_x + i] = rshift_rnd_sf((AdaptRndWeight * (scaled_coeff - (level << q_bits))), q_bits + 1);
  67.         *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
  68.         level   = isignab(level, *m7);
  69.         *m7     = rshift_rnd_sf(((level * invlevelscale[j][i]) << qp_per), 4);
  70.         // inverse scale can be alternative performed as follows to ensure 16bit
  71.         // arithmetic is satisfied.
  72.         // *m7 = (qp_per<4) ? rshift_rnd_sf((level*invlevelscale[j][i]),4-qp_per) : (level*invlevelscale[j][i])<<(qp_per-4);
  73.         *ACL++  = level;
  74.         *ACR++  = run; 
  75.         // reset zero level counter
  76.         run     = 0;
  77.         nonzero = TRUE;        
  78.       }
  79.       else
  80.       {
  81.         fadjust4x4[j][block_x + i] = 0;
  82.         run++;
  83.         *m7 = 0;
  84.       } 
  85.     }
  86.     else
  87.     {
  88.       fadjust4x4[j][block_x + i] = 0;
  89.       run++;
  90.     } 
  91.   }
  92.   *ACL = 0;
  93.   return nonzero;
  94. }
  95. int quant_ac4x4_around(int (*tblock)[16], int block_y, int block_x, int qp,
  96.                        int*  ACLevel, int*  ACRun, 
  97.                        int **fadjust4x4, int **levelscale, int **invlevelscale, int **leveloffset,
  98.                        int *coeff_cost, const byte (*pos_scan)[2], const byte *c_cost, int type, int is_cavlc)
  99. {
  100.   static int i,j, coeff_ctr;
  101.   static int *m7;
  102.   static int scaled_coeff;
  103.   int   level, run = 0;
  104.   int   nonzero = FALSE;  
  105.   int   qp_per = qp_per_matrix[qp];
  106.   int   q_bits = Q_BITS + qp_per;
  107.   const byte *p_scan = &pos_scan[1][0];
  108.   int*  ACL = &ACLevel[0];
  109.   int*  ACR = &ACRun[0];
  110.   // Quantization
  111.   for (coeff_ctr = 1; coeff_ctr < 16; coeff_ctr++)
  112.   {
  113.     i = *p_scan++;  // horizontal position
  114.     j = *p_scan++;  // vertical position
  115.     m7 = &tblock[j][block_x + i];
  116.     if (*m7 != 0)
  117.     {
  118.       scaled_coeff = iabs (*m7) * levelscale[j][i];
  119.       level = (scaled_coeff + leveloffset[j][i]) >> q_bits;
  120.       if (level != 0)
  121.       {
  122.         if (is_cavlc)
  123.           level = imin(level, CAVLC_LEVEL_LIMIT);
  124.         fadjust4x4[j][block_x + i] = rshift_rnd_sf((AdaptRndWeight * (scaled_coeff - (level << q_bits))), (q_bits + 1));
  125.         *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
  126.         level  = isignab(level, *m7);
  127.         *m7    = rshift_rnd_sf(((level * invlevelscale[j][i]) << qp_per), 4);
  128.         // inverse scale can be alternative performed as follows to ensure 16bit
  129.         // arithmetic is satisfied.
  130.         // *m7 = (qp_per<4) ? rshift_rnd_sf((level*invlevelscale[j][i]),4-qp_per) : (level*invlevelscale[j][i])<<(qp_per-4);
  131.         *ACL++  = level;
  132.         *ACR++  = run; 
  133.         // reset zero level counter
  134.         run     = 0;
  135.         nonzero = TRUE;
  136.       }
  137.       else
  138.       {
  139.         fadjust4x4[j][block_x + i] = 0;
  140.         run++;
  141.         *m7 = 0;
  142.       }
  143.     }
  144.     else
  145.     {
  146.       fadjust4x4[j][block_x + i] = 0;
  147.       run++;
  148.     }          
  149.   }
  150.   *ACL = 0;
  151.   return nonzero;
  152. }
  153.  
  154. /*!
  155.  ************************************************************************
  156.  * brief
  157.  *    Quantization process for All coefficients for a 4x4 DC block
  158.  *
  159.  * par Input:
  160.  *
  161.  * par Output:
  162.  *
  163.  ************************************************************************
  164.  */
  165. int quant_dc4x4_around(int (*tblock)[4], int qp, int* DCLevel, int* DCRun, 
  166.                        int levelscale, int invlevelscale, int leveloffset, const byte (*pos_scan)[2], int is_calvc)
  167. {
  168.   static int i,j, coeff_ctr;
  169.   static int *m7;
  170.   static int scaled_coeff;
  171.   int   level, run = 0;
  172.   int   nonzero = FALSE;  
  173.   int   qp_per = qp_per_matrix[qp];
  174.   int   q_bits = Q_BITS + qp_per + 1;
  175.   const byte *p_scan = &pos_scan[0][0];
  176.   int*  DCL = &DCLevel[0];
  177.   int*  DCR = &DCRun[0];
  178.   // Quantization
  179.   for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
  180.   {
  181.     i = *p_scan++;  // horizontal position
  182.     j = *p_scan++;  // vertical position
  183.     m7 = &tblock[j][i];
  184.     if (*m7 != 0)
  185.     {    
  186.       scaled_coeff = iabs (*m7) * levelscale;
  187.       level = (scaled_coeff + (leveloffset << 1) ) >> q_bits;
  188.       if (level != 0)
  189.       {
  190.         if (is_calvc)
  191.           level = imin(level, CAVLC_LEVEL_LIMIT);
  192.         level   = isignab(level, *m7);
  193.         *m7     = level;
  194.         *DCL++  = level;
  195.         *DCR++  = run;
  196.         // reset zero level counter
  197.         run     = 0;
  198.         nonzero = TRUE;
  199.       }
  200.       else
  201.       {
  202.         run++;
  203.         *m7 = 0;
  204.       }
  205.     }
  206.     else
  207.     {
  208.       run++;
  209.     }                    
  210.   }
  211.   *DCL = 0;
  212.   return nonzero;
  213. }