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

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * quant.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 x264 project
  5.  *
  6.  * Authors: Christian Heine <sennindemokrit@gmx.net>
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  21.  *****************************************************************************/
  22. #include "common.h"
  23. #ifdef HAVE_MMXEXT
  24. #include "i386/quant.h"
  25. #endif
  26. #define QUANT_ONE( coef, mf ) 
  27.     if( (coef) > 0 ) 
  28.         (coef) = ( f + (coef) * (mf) ) >> i_qbits; 
  29.     else 
  30.         (coef) = - ( ( f - (coef) * (mf) ) >> i_qbits ); 
  31. }
  32. static void quant_8x8_core( int16_t dct[8][8], int quant_mf[8][8], int i_qbits, int f )
  33. {
  34.     int i;
  35.     for( i = 0; i < 64; i++ )
  36.         QUANT_ONE( dct[0][i], quant_mf[0][i] );
  37. }
  38. static void quant_4x4_core( int16_t dct[4][4], int quant_mf[4][4], int i_qbits, int f )
  39. {
  40.     int i;
  41.     for( i = 0; i < 16; i++ )
  42.         QUANT_ONE( dct[0][i], quant_mf[0][i] );
  43. }
  44. static void quant_4x4_dc_core( int16_t dct[4][4], int i_quant_mf, int i_qbits, int f )
  45. {
  46.     int i;
  47.     for( i = 0; i < 16; i++ )
  48.         QUANT_ONE( dct[0][i], i_quant_mf );
  49. }
  50. static void quant_2x2_dc_core( int16_t dct[2][2], int i_quant_mf, int i_qbits, int f )
  51. {
  52.     QUANT_ONE( dct[0][0], i_quant_mf );
  53.     QUANT_ONE( dct[0][1], i_quant_mf );
  54.     QUANT_ONE( dct[0][2], i_quant_mf );
  55.     QUANT_ONE( dct[0][3], i_quant_mf );
  56. }
  57. #define DEQUANT_SHL( x ) 
  58.     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
  59. #define DEQUANT_SHR( x ) 
  60.     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
  61. static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
  62. {
  63.     const int i_mf = i_qp%6;
  64.     const int i_qbits = i_qp/6 - 4;
  65.     int y;
  66.     if( i_qbits >= 0 )
  67.     {
  68.         for( y = 0; y < 4; y++ )
  69.         {
  70.             DEQUANT_SHL( 0 );
  71.             DEQUANT_SHL( 1 );
  72.             DEQUANT_SHL( 2 );
  73.             DEQUANT_SHL( 3 );
  74.         }
  75.     }
  76.     else
  77.     {
  78.         const int f = 1 << (-i_qbits-1);
  79.         for( y = 0; y < 4; y++ )
  80.         {
  81.             DEQUANT_SHR( 0 );
  82.             DEQUANT_SHR( 1 );
  83.             DEQUANT_SHR( 2 );
  84.             DEQUANT_SHR( 3 );
  85.         }
  86.     }
  87. }
  88. static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
  89. {
  90.     const int i_mf = i_qp%6;
  91.     const int i_qbits = i_qp/6 - 6;
  92.     int y;
  93.     if( i_qbits >= 0 )
  94.     {
  95.         for( y = 0; y < 8; y++ )
  96.         {
  97.             DEQUANT_SHL( 0 );
  98.             DEQUANT_SHL( 1 );
  99.             DEQUANT_SHL( 2 );
  100.             DEQUANT_SHL( 3 );
  101.             DEQUANT_SHL( 4 );
  102.             DEQUANT_SHL( 5 );
  103.             DEQUANT_SHL( 6 );
  104.             DEQUANT_SHL( 7 );
  105.         }
  106.     }
  107.     else
  108.     {
  109.         const int f = 1 << (-i_qbits-1);
  110.         for( y = 0; y < 8; y++ )
  111.         {
  112.             DEQUANT_SHR( 0 );
  113.             DEQUANT_SHR( 1 );
  114.             DEQUANT_SHR( 2 );
  115.             DEQUANT_SHR( 3 );
  116.             DEQUANT_SHR( 4 );
  117.             DEQUANT_SHR( 5 );
  118.             DEQUANT_SHR( 6 );
  119.             DEQUANT_SHR( 7 );
  120.         }
  121.     }
  122. }
  123. void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
  124. {
  125.     const int i_qbits = i_qp/6 - 5;
  126.     if( i_qbits >= 0 )
  127.     {
  128.         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
  129.         dct[0][0] *= i_dmf;
  130.         dct[0][1] *= i_dmf;
  131.         dct[1][0] *= i_dmf;
  132.         dct[1][1] *= i_dmf;
  133.     }
  134.     else
  135.     {
  136.         const int i_dmf = dequant_mf[i_qp%6][0][0];
  137.         // chroma DC is truncated, not rounded
  138.         dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
  139.         dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
  140.         dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
  141.         dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
  142.     }
  143. }
  144. void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
  145. {
  146.     const int i_qbits = i_qp/6 - 6;
  147.     int y;
  148.     if( i_qbits >= 0 )
  149.     {
  150.         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
  151.         for( y = 0; y < 4; y++ )
  152.         {
  153.             dct[y][0] *= i_dmf;
  154.             dct[y][1] *= i_dmf;
  155.             dct[y][2] *= i_dmf;
  156.             dct[y][3] *= i_dmf;
  157.         }
  158.     }
  159.     else
  160.     {
  161.         const int i_dmf = dequant_mf[i_qp%6][0][0];
  162.         const int f = 1 << (-i_qbits-1);
  163.         for( y = 0; y < 4; y++ )
  164.         {
  165.             dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
  166.             dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
  167.             dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
  168.             dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
  169.         }
  170.     }
  171. }
  172. void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
  173. {
  174.     int i, maxQ8=0, maxQ4=0, maxQdc=0;
  175.     pf->quant_8x8_core = quant_8x8_core;
  176.     pf->quant_4x4_core = quant_4x4_core;
  177.     pf->quant_4x4_dc_core = quant_4x4_dc_core;
  178.     pf->quant_2x2_dc_core = quant_2x2_dc_core;
  179.     pf->dequant_4x4 = dequant_4x4;
  180.     pf->dequant_8x8 = dequant_8x8;
  181. #ifdef HAVE_MMXEXT
  182.     /* determine the biggest coeffient in all quant8_mf tables */
  183.     for( i = 0; i < 2*6*8*8; i++ )
  184.     {
  185.         int q = h->quant8_mf[0][0][0][i];
  186.         if( maxQ8 < q )
  187.             maxQ8 = q;
  188.     }
  189.     /* determine the biggest coeffient in all quant4_mf tables ( maxQ4 )
  190.        and the biggest DC coefficient if all quant4_mf tables ( maxQdc ) */
  191.     for( i = 0; i < 4*6*4*4; i++ )
  192.     {
  193.         int q = h->quant4_mf[0][0][0][i];
  194.         if( maxQ4 < q )
  195.             maxQ4 = q;
  196.         if( maxQdc < q && i%16 == 0 )
  197.             maxQdc = q;
  198.     }
  199.     /* select quant_8x8 based on CPU and maxQ8 */
  200.     if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
  201.         pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
  202.     else
  203.     if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
  204.         pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
  205.     else
  206.     if( cpu&X264_CPU_MMXEXT )
  207.         pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
  208.     /* select quant_4x4 based on CPU and maxQ4 */
  209.     if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
  210.         pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
  211.     else
  212.     if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
  213.         pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
  214.     else
  215.     if( cpu&X264_CPU_MMXEXT )
  216.         pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
  217.     /* select quant_XxX_dc based on CPU and maxQdc */
  218.     if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
  219.     {
  220.         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
  221.         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
  222.     }
  223.     else
  224.     if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
  225.     {
  226.         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
  227.         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
  228.     }
  229.     else
  230.     if( cpu&X264_CPU_MMXEXT )
  231.     {
  232.         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
  233.         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
  234.     }
  235.     if( cpu&X264_CPU_MMX )
  236.     {
  237.         /* dequant is not subject to the above CQM-dependent overflow issues,
  238.          * as long as the inputs are in the range generable by dct+quant.
  239.          * that is not guaranteed by the standard, but is true within x264 */
  240.         pf->dequant_4x4 = x264_dequant_4x4_mmx;
  241.         pf->dequant_8x8 = x264_dequant_8x8_mmx;
  242.     }
  243. #endif  /* HAVE_MMXEXT */
  244. }