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

Audio

开发平台:

Visual C++

  1. /*!
  2.  *************************************************************************************
  3.  * file quant4x4_trellis.c
  4.  *
  5.  * brief
  6.  *    Quantization process for a 4x4 block using trellis based quantization
  7.  *
  8.  * author
  9.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10.  *    - Qualcomm      
  11.  *    - Limin Liu                                <limin.liu@dolby.com>
  12.  *    - Alexis Michael Tourapis                  <alexismt@ieee.org>
  13.  *
  14.  *************************************************************************************
  15.  */
  16. #include "contributors.h"
  17. #include <math.h>
  18. #include "global.h"
  19. #include "image.h"
  20. #include "mb_access.h"
  21. #include "vlc.h"
  22. #include "transform.h"
  23. #include "mc_prediction.h"
  24. #include "q_offsets.h"
  25. #include "q_matrix.h"
  26. #include "quant4x4.h"
  27. #include "rdoq.h"
  28. /*!
  29.  ************************************************************************
  30.  * brief
  31.  *    Quantization process for All coefficients for a 4x4 block
  32.  *
  33.  * par Input:
  34.  *
  35.  * par Output:
  36.  *
  37.  ************************************************************************
  38.  */
  39. int quant_4x4_trellis(int (*tblock)[16], int block_y, int block_x, int  qp,                
  40.                       int*  ACLevel, int*  ACRun, 
  41.                       int **fadjust4x4, int **levelscale, int **invlevelscale, int **leveloffset,
  42.                       int *coeff_cost, const byte (*pos_scan)[2], const byte *c_cost, int is_cavlc)
  43. {
  44.   static int i,j, coeff_ctr;
  45.   static int *m7;
  46.   int   level, run = 0;
  47.   int   nonzero = FALSE;
  48.   int   qp_per = qp_per_matrix[qp];
  49.   int   qp_rem = qp_rem_matrix[qp];
  50.   const byte *p_scan = &pos_scan[0][0];
  51.   int*  ACL = &ACLevel[0];
  52.   int*  ACR = &ACRun[0];
  53.   static int levelTrellis[16];
  54.   rdoq_4x4(tblock, block_y, block_x, qp_per, qp_rem, levelscale, leveloffset, pos_scan, levelTrellis);
  55.   // Quantization
  56.   for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
  57.   {
  58.     i = *p_scan++;  // horizontal position
  59.     j = *p_scan++;  // vertical position
  60.     m7 = &tblock[j][block_x + i];
  61.     if (*m7 != 0)
  62.     {    
  63.       /*
  64.       scaled_coeff = iabs (*m7) * levelscale[j][i];
  65.       level = (scaled_coeff + leveloffset[j][i]) >> q_bits;
  66.       */
  67.       level = levelTrellis[coeff_ctr];
  68.       if (level != 0)
  69.       {
  70.         if (is_cavlc)
  71.           level = imin(level, CAVLC_LEVEL_LIMIT);
  72.         *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
  73.         level   = isignab(level, *m7);
  74.         *m7     = rshift_rnd_sf(((level * invlevelscale[j][i]) << qp_per), 4);
  75.         *ACL++  = level;
  76.         *ACR++  = run; 
  77.         // reset zero level counter
  78.         run     = 0;
  79.         nonzero = TRUE;        
  80.       }
  81.       else
  82.       {
  83.         run++;
  84.         *m7 = 0;
  85.       } 
  86.     }
  87.     else
  88.     {
  89.       run++;
  90.     } 
  91.   }
  92.   *ACL = 0;
  93.   return nonzero;
  94. }
  95. /*!
  96. ************************************************************************
  97. * brief
  98. *    Rate distortion optimized Quantization process for 
  99. *    all coefficients in a 4x4 block (CAVLC)
  100. *
  101. ************************************************************************
  102. */
  103. void rdoq_4x4_CAVLC(int (*tblock)[16], int block_y, int block_x, int qp_per, int qp_rem, 
  104.                     int **levelscale, int **leveloffset, const byte (*pos_scan)[2], int levelTrellis[])
  105. {
  106.   const byte *p_scan = &pos_scan[0][0];
  107.   levelDataStruct levelData[16];  
  108.   double  lambda_md = 0;
  109.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  110.   int type = LUMA_4x4;
  111.   int   pos_x   = block_x >> BLOCK_SHIFT;
  112.   int   pos_y   = block_y >> BLOCK_SHIFT;
  113.   int   b8      = 2*(pos_y >> 1) + (pos_x >> 1);
  114.   int   b4      = 2*(pos_y & 0x01) + (pos_x & 0x01);
  115.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  116.   {
  117.     lambda_md = img->lambda_md[5][img->masterQP];  
  118.   }
  119.   else
  120.   {
  121.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  122.   }
  123.   init_trellis_data_4x4_CAVLC(tblock, block_x, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], type);
  124.   est_RunLevel_CAVLC(levelData, levelTrellis, LUMA, b8, b4, 16, lambda_md);
  125. }
  126. /*!
  127. ************************************************************************
  128. * brief
  129. *    Rate distortion optimized Quantization process for 
  130. *    all coefficients in a 4x4 block (CABAC)
  131. *
  132. ************************************************************************
  133. */
  134. void rdoq_4x4_CABAC(int (*tblock)[16], int block_y, int block_x, int qp_per, int qp_rem, 
  135.               int **levelscale, int **leveloffset, const byte (*pos_scan)[2], int levelTrellis[])
  136. {
  137.   const byte *p_scan = &pos_scan[0][0];
  138.   levelDataStruct levelData[16];
  139.   double  lambda_md = 0;
  140.   int kStart=0, kStop=0, noCoeff = 0, estBits;
  141.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  142.   int type = LUMA_4x4;
  143.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  144.   {
  145.     lambda_md = img->lambda_md[5][img->masterQP];  
  146.   }
  147.   else
  148.   {
  149.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  150.   }
  151.   noCoeff = init_trellis_data_4x4_CABAC(tblock, block_x, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], &kStart, &kStop, type);
  152.   estBits = est_write_and_store_CBP_block_bit(currMB, LUMA_4x4);
  153.   est_writeRunLevel_CABAC(levelData, levelTrellis, LUMA_4x4, lambda_md, kStart, kStop, noCoeff, estBits);
  154. }
  155. /*!
  156.  ************************************************************************
  157.  * brief
  158.  *    Quantization process for All coefficients for a 4x4 block (LUMA_16AC or CHROMA_AC)
  159.  *
  160.  * par Input:
  161.  *
  162.  * par Output:
  163.  *
  164.  ************************************************************************
  165.  */
  166. int quant_ac4x4_trellis(int (*tblock)[16], int block_y, int block_x, int qp,                
  167.                         int*  ACLevel, int*  ACRun, 
  168.                         int **fadjust4x4, int **levelscale, int **invlevelscale, int **leveloffset,
  169.                         int *coeff_cost, const byte (*pos_scan)[2], const byte *c_cost, int type, int is_calvc)
  170. {
  171.   static int i,j, coeff_ctr;
  172.   static int *m7;
  173.   int   level, run = 0;
  174.   int   nonzero = FALSE;  
  175.   int   qp_per = qp_per_matrix[qp];
  176.   int   qp_rem = qp_rem_matrix[qp];
  177.   const byte *p_scan = &pos_scan[1][0];
  178.   int*  ACL = &ACLevel[0];
  179.   int*  ACR = &ACRun[0];
  180.   static int levelTrellis[16]; 
  181.   rdoq_ac4x4(tblock, block_y, block_x, qp_per, qp_rem, levelscale, leveloffset, pos_scan, levelTrellis, type);
  182.   // Quantization
  183.   for (coeff_ctr = 1; coeff_ctr < 16; coeff_ctr++)
  184.   {
  185.     i = *p_scan++;  // horizontal position
  186.     j = *p_scan++;  // vertical position
  187.     m7 = &tblock[j][block_x + i];
  188.     if (*m7 != 0)
  189.     {    
  190.       /*
  191.       scaled_coeff = iabs (*m7) * levelscale[j][i];
  192.       level = (scaled_coeff + leveloffset[j][i]) >> q_bits;
  193.       */
  194.       level=levelTrellis[coeff_ctr - 1];
  195.       if (level != 0)
  196.       {
  197.         if (is_calvc)
  198.           level = imin(level, CAVLC_LEVEL_LIMIT);
  199.         *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
  200.         level  = isignab(level, *m7);
  201.         *m7    = rshift_rnd_sf(((level * invlevelscale[j][i]) << qp_per), 4);
  202.         // inverse scale can be alternative performed as follows to ensure 16bit
  203.         // arithmetic is satisfied.
  204.         // *m7 = (qp_per<4) ? rshift_rnd_sf((level*invlevelscale[j][i]),4-qp_per) : (level*invlevelscale[j][i])<<(qp_per-4);
  205.         *ACL++  = level;
  206.         *ACR++  = run; 
  207.         // reset zero level counter
  208.         run     = 0;
  209.         nonzero = TRUE;
  210.       }
  211.       else
  212.       {
  213.         run++;
  214.         *m7 = 0;
  215.       }
  216.     }
  217.     else
  218.     {
  219.       run++;
  220.     }          
  221.   }
  222.   *ACL = 0;
  223.   return nonzero;
  224. }
  225. /*!
  226. ************************************************************************
  227. * brief
  228. *    Rate distortion optimized Quantization process for 
  229. *    all coefficients in a 4x4 block (CAVLC)
  230. *
  231. ************************************************************************
  232. */
  233. void rdoq_ac4x4_CAVLC(int (*tblock)[16], int block_y, int block_x, int qp_per, int qp_rem, 
  234.                     int **levelscale, int **leveloffset, const byte (*pos_scan)[2], int levelTrellis[], int type)
  235. {
  236.   const byte *p_scan = &pos_scan[1][0];
  237.   levelDataStruct levelData[16];  
  238.   double  lambda_md = 0;
  239.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  240.   int   pos_x   = block_x >> BLOCK_SHIFT;
  241.   int   pos_y   = block_y >> BLOCK_SHIFT;
  242.   int   b8      = 2*(pos_y >> 1) + (pos_x >> 1);
  243.   int   b4      = 2*(pos_y & 0x01) + (pos_x & 0x01);
  244.   int   block_type = ( (type == CHROMA_AC) ? CHROMA_AC : LUMA_INTRA16x16AC);
  245.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  246.   {
  247.     lambda_md = img->lambda_md[5][img->masterQP];  
  248.   }
  249.   else
  250.   {
  251.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  252.   }
  253.   init_trellis_data_4x4_CAVLC(tblock, block_x, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], type);
  254.   est_RunLevel_CAVLC(levelData, levelTrellis, block_type, b8, b4, 15, lambda_md);
  255. }
  256. /*!
  257. ************************************************************************
  258. * brief
  259. *    Rate distortion optimized Quantization process for 
  260. *    all coefficients in a 4x4 block (LUMA_16AC or CHROMA_AC) - CABAC
  261. *
  262. ************************************************************************
  263. */
  264. void rdoq_ac4x4_CABAC(int (*tblock)[16] , int block_y, int block_x, int qp_per, int qp_rem, 
  265.                 int **levelscale, int **leveloffset, const byte (*pos_scan)[2], int levelTrellis[], int type)
  266. {
  267.   const byte *p_scan = &pos_scan[1][0];
  268.   levelDataStruct levelData[16];
  269.   double  lambda_md=0;
  270.   int kStart = 0, kStop = 0, noCoeff = 0, estBits;
  271.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  272.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  273.   {
  274.     lambda_md = img->lambda_md[5][img->masterQP];  
  275.   }
  276.   else
  277.   {
  278.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  279.   }
  280.   noCoeff = init_trellis_data_4x4_CABAC(tblock, block_x, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], &kStart, &kStop, type);
  281.   estBits = est_write_and_store_CBP_block_bit(currMB, type);
  282.   est_writeRunLevel_CABAC(levelData, levelTrellis, type, lambda_md, kStart, kStop, noCoeff, estBits);
  283. }
  284. /*!
  285.  ************************************************************************
  286.  * brief
  287.  *    Quantization process for All coefficients for a 4x4 DC block
  288.  *
  289.  * par Input:
  290.  *
  291.  * par Output:
  292.  *
  293.  ************************************************************************
  294.  */
  295. int quant_dc4x4_trellis(int (*tblock)[4], int qp, int* DCLevel, int* DCRun, 
  296.                        int levelscale, int invlevelscale, int leveloffset, const byte (*pos_scan)[2], int is_calvc)
  297. {
  298.   static int i,j, coeff_ctr;
  299.   static int *m7;
  300.   int   level, run = 0;
  301.   int   nonzero = FALSE;  
  302.   int   qp_per = qp_per_matrix[qp];
  303.   int   qp_rem = qp_rem_matrix[qp];
  304.   const byte *p_scan = &pos_scan[0][0];
  305.   int*  DCL = &DCLevel[0];
  306.   int*  DCR = &DCRun[0];
  307.   static int levelTrellis[16];
  308.   rdoq_dc(tblock, qp_per, qp_rem, levelscale, leveloffset, pos_scan, levelTrellis, LUMA_16DC);
  309.   // Quantization
  310.   for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
  311.   {
  312.     i = *p_scan++;  // horizontal position
  313.     j = *p_scan++;  // vertical position
  314.     m7 = &tblock[j][i];
  315.     if (*m7 != 0)
  316.     {    
  317.       level = levelTrellis[coeff_ctr];
  318.       if (level != 0)
  319.       {
  320.         if (is_calvc)
  321.           level = imin(level, CAVLC_LEVEL_LIMIT);
  322.         level   = isignab(level, *m7);
  323.         *m7     = level;
  324.         *DCL++  = level;
  325.         *DCR++  = run;
  326.         // reset zero level counter
  327.         run     = 0;
  328.         nonzero = TRUE;
  329.       }
  330.       else
  331.       {
  332.         run++;
  333.         *m7 = 0;
  334.       }
  335.     }
  336.     else
  337.     {
  338.       run++;
  339.     }                    
  340.   }
  341.   *DCL = 0;
  342.   return nonzero;
  343. }
  344. /*!
  345. ************************************************************************
  346. * brief
  347. *    Rate distortion optimized Quantization process for 
  348. *    all coefficients in a luma DC block 
  349. *
  350. ************************************************************************
  351. */
  352. void rdoq_dc_CAVLC(int (*tblock)[4], int qp_per, int qp_rem, int levelscale, int leveloffset, const byte (*pos_scan)[2], int levelTrellis[], int type)
  353. {
  354.   const byte *p_scan = &pos_scan[0][0];
  355.   levelDataStruct levelData[16];
  356.   double  lambda_md = 0;
  357.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  358.   
  359.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  360.   {
  361.     lambda_md = img->lambda_md[5][img->masterQP];  
  362.   }
  363.   else
  364.   {
  365.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  366.   }
  367.   init_trellis_data_DC_CAVLC(tblock, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], type);
  368.   est_RunLevel_CAVLC(levelData, levelTrellis, LUMA_INTRA16x16DC, 0, 0, 16, lambda_md);
  369. }
  370. /*!
  371. ************************************************************************
  372. * brief
  373. *    Rate distortion optimized Quantization process for 
  374. *    all coefficients in a luma DC block 
  375. *
  376. ************************************************************************
  377. */
  378. void rdoq_dc_CABAC(int (*tblock)[4], int qp_per, int qp_rem, int levelscale, int leveloffset, const byte (*pos_scan)[2], int levelTrellis[], int type)
  379. {
  380.   const byte *p_scan = &pos_scan[0][0];
  381.   levelDataStruct levelData[16];
  382.   double  lambda_md = 0;
  383.   int kStart=0, kStop=0, noCoeff = 0, estBits;
  384.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  385.   if ((img->type==B_SLICE) && img->nal_reference_idc)
  386.   {
  387.     lambda_md = img->lambda_md[5][img->masterQP];  
  388.   }
  389.   else
  390.   {
  391.     lambda_md = img->lambda_md[img->type][img->masterQP]; 
  392.   }
  393.   noCoeff = init_trellis_data_DC_CABAC(tblock, qp_per, qp_rem, levelscale, leveloffset, p_scan, currMB, &levelData[0], &kStart, &kStop);
  394.   estBits = est_write_and_store_CBP_block_bit(currMB, type);
  395.   est_writeRunLevel_CABAC(levelData, levelTrellis, type, lambda_md, kStart, kStop, noCoeff, estBits);
  396. }