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 );
66 #define DEQUANT_SHL( x ) \
67 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
69 #define DEQUANT_SHR( x ) \
70 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
72 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
74 const int i_mf = i_qp%6;
75 const int i_qbits = i_qp/6 - 4;
80 for( y = 0; y < 4; y++ )
90 const int f = 1 << (-i_qbits-1);
91 for( y = 0; y < 4; y++ )
101 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
103 const int i_mf = i_qp%6;
104 const int i_qbits = i_qp/6 - 6;
109 for( y = 0; y < 8; y++ )
123 const int f = 1 << (-i_qbits-1);
124 for( y = 0; y < 8; y++ )
138 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
140 const int i_qbits = i_qp/6 - 5;
144 const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
152 const int i_dmf = dequant_mf[i_qp%6][0][0];
153 // chroma DC is truncated, not rounded
154 dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
155 dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
156 dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
157 dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
161 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
163 const int i_qbits = i_qp/6 - 6;
168 const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
170 for( y = 0; y < 4; y++ )
180 const int i_dmf = dequant_mf[i_qp%6][0][0];
181 const int f = 1 << (-i_qbits-1);
183 for( y = 0; y < 4; y++ )
185 dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
186 dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
187 dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
188 dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
193 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
195 int i, maxQ8=0, maxQ4=0, maxQdc=0;
197 pf->quant_8x8_core = quant_8x8_core;
198 pf->quant_4x4_core = quant_4x4_core;
199 pf->quant_4x4_dc_core = quant_4x4_dc_core;
200 pf->quant_2x2_dc_core = quant_2x2_dc_core;
202 pf->dequant_4x4 = dequant_4x4;
203 pf->dequant_8x8 = dequant_8x8;
207 /* determine the biggest coeffient in all quant8_mf tables */
208 for( i = 0; i < 2*6*8*8; i++ )
210 int q = h->quant8_mf[0][0][0][i];
215 /* determine the biggest coeffient in all quant4_mf tables ( maxQ4 )
216 and the biggest DC coefficient if all quant4_mf tables ( maxQdc ) */
217 for( i = 0; i < 4*6*4*4; i++ )
219 int q = h->quant4_mf[0][0][0][i];
222 if( maxQdc < q && i%16 == 0 )
226 /* select quant_8x8 based on CPU and maxQ8 */
227 if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
228 pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
230 if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
231 pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
233 if( cpu&X264_CPU_MMXEXT )
234 pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
236 /* select quant_4x4 based on CPU and maxQ4 */
237 if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
238 pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
240 if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
241 pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
243 if( cpu&X264_CPU_MMXEXT )
244 pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
246 /* select quant_XxX_dc based on CPU and maxQdc */
247 if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
249 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
250 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
253 if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
255 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
256 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
259 if( cpu&X264_CPU_MMXEXT )
261 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
262 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
265 if( cpu&X264_CPU_MMX )
267 /* dequant is not subject to the above CQM-dependent overflow issues,
268 * as long as the inputs are in the range generable by dct+quant.
269 * that is not guaranteed by the standard, but is true within x264 */
270 pf->dequant_4x4 = x264_dequant_4x4_mmx;
271 pf->dequant_8x8 = x264_dequant_8x8_mmx;
273 #endif /* HAVE_MMXEXT */