1 /*****************************************************************************
2 * quant.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2005 x264 project
6 * Authors: Christian Heine <sennindemokrit@gmx.net>
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.
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.
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 *****************************************************************************/
26 #include "i386/quant.h"
29 #define QUANT_ONE( coef, mf ) \
32 (coef) = ( f + (coef) * (mf) ) >> i_qbits; \
34 (coef) = - ( ( f - (coef) * (mf) ) >> i_qbits ); \
37 static void quant_8x8_core( int16_t dct[8][8], int quant_mf[8][8], int i_qbits, int f )
40 for( i = 0; i < 64; i++ )
41 QUANT_ONE( dct[0][i], quant_mf[0][i] );
44 static void quant_4x4_core( int16_t dct[4][4], int quant_mf[4][4], int i_qbits, int f )
47 for( i = 0; i < 16; i++ )
48 QUANT_ONE( dct[0][i], quant_mf[0][i] );
51 static void quant_4x4_dc_core( int16_t dct[4][4], int i_quant_mf, int i_qbits, int f )
54 for( i = 0; i < 16; i++ )
55 QUANT_ONE( dct[0][i], i_quant_mf );
58 static void quant_2x2_dc_core( int16_t dct[2][2], int i_quant_mf, int i_qbits, int f )
60 QUANT_ONE( dct[0][0], i_quant_mf );
61 QUANT_ONE( dct[0][1], i_quant_mf );
62 QUANT_ONE( dct[0][2], i_quant_mf );
63 QUANT_ONE( dct[0][3], i_quant_mf );
67 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
69 int i, maxQ8=0, maxQ4=0, maxQdc=0;
71 pf->quant_8x8_core = quant_8x8_core;
72 pf->quant_4x4_core = quant_4x4_core;
73 pf->quant_4x4_dc_core = quant_4x4_dc_core;
74 pf->quant_2x2_dc_core = quant_2x2_dc_core;
78 /* determine the biggest coeffient in all quant8_mf tables */
79 for( i = 0; i < 2*6*8*8; i++ )
81 int q = h->quant8_mf[0][0][0][i];
86 /* determine the biggest coeffient in all quant4_mf tables ( maxQ4 )
87 and the biggest DC coefficient if all quant4_mf tables ( maxQdc ) */
88 for( i = 0; i < 4*6*4*4; i++ )
90 int q = h->quant4_mf[0][0][0][i];
93 if( maxQdc < q && i%16 == 0 )
97 /* select quant_8x8 based on CPU and maxQ8 */
98 if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
99 pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
101 if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
102 pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
104 if( cpu&X264_CPU_MMXEXT )
105 pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
107 /* select quant_4x4 based on CPU and maxQ4 */
108 if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
109 pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
111 if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
112 pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
114 if( cpu&X264_CPU_MMXEXT )
115 pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
117 /* select quant_XxX_dc based on CPU and maxQdc */
118 if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
120 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
121 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
124 if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
126 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
127 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
130 if( cpu&X264_CPU_MMXEXT )
132 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
133 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
136 #endif /* HAVE_MMXEXT */