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 # include "ppc/quant.h"
32 #define QUANT_ONE( coef, mf ) \
35 (coef) = ( f + (coef) * (mf) ) >> i_qbits; \
37 (coef) = - ( ( f - (coef) * (mf) ) >> i_qbits ); \
40 static void quant_8x8_core( int16_t dct[8][8], int quant_mf[8][8], int i_qbits, int f )
43 for( i = 0; i < 64; i++ )
44 QUANT_ONE( dct[0][i], quant_mf[0][i] );
47 static void quant_4x4_core( int16_t dct[4][4], int quant_mf[4][4], int i_qbits, int f )
50 for( i = 0; i < 16; i++ )
51 QUANT_ONE( dct[0][i], quant_mf[0][i] );
54 static void quant_4x4_dc_core( int16_t dct[4][4], int i_quant_mf, int i_qbits, int f )
57 for( i = 0; i < 16; i++ )
58 QUANT_ONE( dct[0][i], i_quant_mf );
61 static void quant_2x2_dc_core( int16_t dct[2][2], int i_quant_mf, int i_qbits, int f )
63 QUANT_ONE( dct[0][0], i_quant_mf );
64 QUANT_ONE( dct[0][1], i_quant_mf );
65 QUANT_ONE( dct[0][2], i_quant_mf );
66 QUANT_ONE( dct[0][3], i_quant_mf );
69 #define DEQUANT_SHL( x ) \
70 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
72 #define DEQUANT_SHR( x ) \
73 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
75 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
77 const int i_mf = i_qp%6;
78 const int i_qbits = i_qp/6 - 4;
83 for( y = 0; y < 4; y++ )
93 const int f = 1 << (-i_qbits-1);
94 for( y = 0; y < 4; y++ )
104 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
106 const int i_mf = i_qp%6;
107 const int i_qbits = i_qp/6 - 6;
112 for( y = 0; y < 8; y++ )
126 const int f = 1 << (-i_qbits-1);
127 for( y = 0; y < 8; y++ )
141 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
143 const int i_qbits = i_qp/6 - 5;
147 const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
155 const int i_dmf = dequant_mf[i_qp%6][0][0];
156 // chroma DC is truncated, not rounded
157 dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
158 dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
159 dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
160 dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
164 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
166 const int i_qbits = i_qp/6 - 6;
171 const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
173 for( y = 0; y < 4; y++ )
183 const int i_dmf = dequant_mf[i_qp%6][0][0];
184 const int f = 1 << (-i_qbits-1);
186 for( y = 0; y < 4; y++ )
188 dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
189 dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
190 dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
191 dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
196 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
198 int i, j, maxQ8=0, maxQ4=0, maxQdc=0;
200 pf->quant_8x8_core = quant_8x8_core;
201 pf->quant_4x4_core = quant_4x4_core;
202 pf->quant_4x4_dc_core = quant_4x4_dc_core;
203 pf->quant_2x2_dc_core = quant_2x2_dc_core;
205 pf->dequant_4x4 = dequant_4x4;
206 pf->dequant_8x8 = dequant_8x8;
208 /* determine the biggest coefficient in all quant8_mf tables */
209 for( j = 0; j < 2; j++ )
210 for( i = 0; i < 6*8*8; i++ )
212 int q = h->quant8_mf[j][0][0][i];
217 /* determine the biggest coefficient in all quant4_mf tables ( maxQ4 )
218 and the biggest DC coefficient if all quant4_mf tables ( maxQdc ) */
219 for( j = 0; j < 4; j++ )
220 for( i = 0; i < 6*4*4; i++ )
222 int q = h->quant4_mf[j][0][0][i];
225 if( maxQdc < q && i%16 == 0 )
231 /* select quant_8x8 based on CPU and maxQ8 */
232 #if defined(ARCH_X86_64) && defined(HAVE_SSE3)
233 if( maxQ8 < (1<<15) && cpu&X264_CPU_SSSE3 )
234 pf->quant_8x8_core = x264_quant_8x8_core15_ssse3;
237 if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
238 pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
240 if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
241 pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
243 if( cpu&X264_CPU_MMXEXT )
244 pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
246 /* select quant_4x4 based on CPU and maxQ4 */
247 #if defined(ARCH_X86_64) && defined(HAVE_SSE3)
248 if( maxQ4 < (1<<15) && cpu&X264_CPU_SSSE3 )
249 pf->quant_4x4_core = x264_quant_4x4_core15_ssse3;
252 if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
253 pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
255 if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
256 pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
258 if( cpu&X264_CPU_MMXEXT )
259 pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
261 /* select quant_XxX_dc based on CPU and maxQdc */
262 if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
264 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
265 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
268 if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
270 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
271 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
274 if( cpu&X264_CPU_MMXEXT )
276 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
277 pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
280 #if defined(ARCH_X86_64) && defined(HAVE_SSE3)
281 if( maxQdc < (1<<15) && cpu&X264_CPU_SSSE3 )
282 pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_ssse3;
285 if( cpu&X264_CPU_MMX )
287 /* dequant is not subject to the above CQM-dependent overflow issues,
288 * as long as the inputs are in the range generable by dct+quant.
289 * that is not guaranteed by the standard, but is true within x264 */
290 pf->dequant_4x4 = x264_dequant_4x4_mmx;
291 pf->dequant_8x8 = x264_dequant_8x8_mmx;
293 #endif /* HAVE_MMX */
296 if( cpu&X264_CPU_ALTIVEC ) {
297 if( maxQ8 < (1<<16) )
299 pf->quant_8x8_core = x264_quant_8x8_altivec;
301 if( maxQ4 < (1<<16) )
303 pf->quant_4x4_core = x264_quant_4x4_altivec;
305 if( maxQdc < (1<<16) )
307 pf->quant_4x4_dc_core = x264_quant_4x4_dc_altivec;
310 #endif /* ARCH_PPC */