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

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * quant.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2005-2008 x264 project
  5.  *
  6.  * Authors: Loren Merritt <lorenm@u.washington.edu>
  7.  *          Christian Heine <sennindemokrit@gmx.net>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include "common.h"
  24. #ifdef HAVE_MMX
  25. #include "x86/quant.h"
  26. #endif
  27. #ifdef ARCH_PPC
  28. #   include "ppc/quant.h"
  29. #endif
  30. #define QUANT_ONE( coef, mf, f ) 
  31.     if( (coef) > 0 ) 
  32.         (coef) = (f + (coef)) * (mf) >> 16; 
  33.     else 
  34.         (coef) = - ((f - (coef)) * (mf) >> 16); 
  35. }
  36. static void quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
  37. {
  38.     int i;
  39.     for( i = 0; i < 64; i++ )
  40.         QUANT_ONE( dct[0][i], mf[i], bias[i] );
  41. }
  42. static void quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
  43. {
  44.     int i;
  45.     for( i = 0; i < 16; i++ )
  46.         QUANT_ONE( dct[0][i], mf[i], bias[i] );
  47. }
  48. static void quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
  49. {
  50.     int i;
  51.     for( i = 0; i < 16; i++ )
  52.         QUANT_ONE( dct[0][i], mf, bias );
  53. }
  54. static void quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
  55. {
  56.     QUANT_ONE( dct[0][0], mf, bias );
  57.     QUANT_ONE( dct[0][1], mf, bias );
  58.     QUANT_ONE( dct[0][2], mf, bias );
  59.     QUANT_ONE( dct[0][3], mf, bias );
  60. }
  61. #define DEQUANT_SHL( x ) 
  62.     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
  63. #define DEQUANT_SHR( x ) 
  64.     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
  65. static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
  66. {
  67.     const int i_mf = i_qp%6;
  68.     const int i_qbits = i_qp/6 - 4;
  69.     int y;
  70.     if( i_qbits >= 0 )
  71.     {
  72.         for( y = 0; y < 4; y++ )
  73.         {
  74.             DEQUANT_SHL( 0 );
  75.             DEQUANT_SHL( 1 );
  76.             DEQUANT_SHL( 2 );
  77.             DEQUANT_SHL( 3 );
  78.         }
  79.     }
  80.     else
  81.     {
  82.         const int f = 1 << (-i_qbits-1);
  83.         for( y = 0; y < 4; y++ )
  84.         {
  85.             DEQUANT_SHR( 0 );
  86.             DEQUANT_SHR( 1 );
  87.             DEQUANT_SHR( 2 );
  88.             DEQUANT_SHR( 3 );
  89.         }
  90.     }
  91. }
  92. static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
  93. {
  94.     const int i_mf = i_qp%6;
  95.     const int i_qbits = i_qp/6 - 6;
  96.     int y;
  97.     if( i_qbits >= 0 )
  98.     {
  99.         for( y = 0; y < 8; y++ )
  100.         {
  101.             DEQUANT_SHL( 0 );
  102.             DEQUANT_SHL( 1 );
  103.             DEQUANT_SHL( 2 );
  104.             DEQUANT_SHL( 3 );
  105.             DEQUANT_SHL( 4 );
  106.             DEQUANT_SHL( 5 );
  107.             DEQUANT_SHL( 6 );
  108.             DEQUANT_SHL( 7 );
  109.         }
  110.     }
  111.     else
  112.     {
  113.         const int f = 1 << (-i_qbits-1);
  114.         for( y = 0; y < 8; y++ )
  115.         {
  116.             DEQUANT_SHR( 0 );
  117.             DEQUANT_SHR( 1 );
  118.             DEQUANT_SHR( 2 );
  119.             DEQUANT_SHR( 3 );
  120.             DEQUANT_SHR( 4 );
  121.             DEQUANT_SHR( 5 );
  122.             DEQUANT_SHR( 6 );
  123.             DEQUANT_SHR( 7 );
  124.         }
  125.     }
  126. }
  127. void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
  128. {
  129.     const int i_qbits = i_qp/6 - 5;
  130.     if( i_qbits >= 0 )
  131.     {
  132.         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
  133.         dct[0][0] *= i_dmf;
  134.         dct[0][1] *= i_dmf;
  135.         dct[1][0] *= i_dmf;
  136.         dct[1][1] *= i_dmf;
  137.     }
  138.     else
  139.     {
  140.         const int i_dmf = dequant_mf[i_qp%6][0][0];
  141.         // chroma DC is truncated, not rounded
  142.         dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
  143.         dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
  144.         dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
  145.         dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
  146.     }
  147. }
  148. void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
  149. {
  150.     const int i_qbits = i_qp/6 - 6;
  151.     int y;
  152.     if( i_qbits >= 0 )
  153.     {
  154.         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
  155.         for( y = 0; y < 4; y++ )
  156.         {
  157.             dct[y][0] *= i_dmf;
  158.             dct[y][1] *= i_dmf;
  159.             dct[y][2] *= i_dmf;
  160.             dct[y][3] *= i_dmf;
  161.         }
  162.     }
  163.     else
  164.     {
  165.         const int i_dmf = dequant_mf[i_qp%6][0][0];
  166.         const int f = 1 << (-i_qbits-1);
  167.         for( y = 0; y < 4; y++ )
  168.         {
  169.             dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
  170.             dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
  171.             dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
  172.             dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
  173.         }
  174.     }
  175. }
  176. static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
  177. {
  178.     int i;
  179.     for( i=1; i<size; i++ )
  180.     {
  181.         int level = dct[i];
  182.         int sign = level>>15;
  183.         level = (level+sign)^sign;
  184.         sum[i] += level;
  185.         level -= offset[i];
  186.         dct[i] = level<0 ? 0 : (level^sign)-sign;
  187.     }
  188. }
  189. void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
  190. {
  191.     pf->quant_8x8 = quant_8x8;
  192.     pf->quant_4x4 = quant_4x4;
  193.     pf->quant_4x4_dc = quant_4x4_dc;
  194.     pf->quant_2x2_dc = quant_2x2_dc;
  195.     pf->dequant_4x4 = dequant_4x4;
  196.     pf->dequant_8x8 = dequant_8x8;
  197.     pf->denoise_dct = x264_denoise_dct;
  198. #ifdef HAVE_MMX
  199.     if( cpu&X264_CPU_MMX )
  200.     {
  201. #ifdef ARCH_X86
  202.         pf->quant_4x4 = x264_quant_4x4_mmx;
  203.         pf->quant_8x8 = x264_quant_8x8_mmx;
  204.         pf->dequant_4x4 = x264_dequant_4x4_mmx;
  205.         pf->dequant_8x8 = x264_dequant_8x8_mmx;
  206.         if( h->param.i_cqm_preset == X264_CQM_FLAT )
  207.         {
  208.             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
  209.             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
  210.         }
  211.         pf->denoise_dct = x264_denoise_dct_mmx;
  212. #endif
  213.     }
  214.     if( cpu&X264_CPU_MMXEXT )
  215.     {
  216.         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
  217. #ifdef ARCH_X86
  218.         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
  219. #endif
  220.     }
  221.     if( cpu&X264_CPU_SSE2 )
  222.     {
  223.         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
  224.         pf->quant_4x4 = x264_quant_4x4_sse2;
  225.         pf->quant_8x8 = x264_quant_8x8_sse2;
  226.         pf->dequant_4x4 = x264_dequant_4x4_sse2;
  227.         pf->dequant_8x8 = x264_dequant_8x8_sse2;
  228.         if( h->param.i_cqm_preset == X264_CQM_FLAT )
  229.         {
  230.             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
  231.             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
  232.         }
  233.         pf->denoise_dct = x264_denoise_dct_sse2;
  234.     }
  235.     if( cpu&X264_CPU_SSSE3 )
  236.     {
  237.         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
  238.         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
  239.         pf->quant_4x4 = x264_quant_4x4_ssse3;
  240.         pf->quant_8x8 = x264_quant_8x8_ssse3;
  241.         pf->denoise_dct = x264_denoise_dct_ssse3;
  242.     }
  243. #endif // HAVE_MMX
  244. #ifdef ARCH_PPC
  245.     if( cpu&X264_CPU_ALTIVEC ) {
  246.         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
  247.         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
  248.         pf->quant_4x4 = x264_quant_4x4_altivec;
  249.         pf->quant_8x8 = x264_quant_8x8_altivec;
  250.         pf->dequant_4x4 = x264_dequant_4x4_altivec;
  251.         pf->dequant_8x8 = x264_dequant_8x8_altivec;
  252.     }
  253. #endif
  254. }