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 "x86/quant.h"
29 # include "ppc/quant.h"
32 #define QUANT_ONE( coef, mf, f ) \
35 (coef) = (f + (coef)) * (mf) >> 16; \
37 (coef) = - ((f - (coef)) * (mf) >> 16); \
40 static void quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
43 for( i = 0; i < 64; i++ )
44 QUANT_ONE( dct[0][i], mf[i], bias[i] );
47 static void quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
50 for( i = 0; i < 16; i++ )
51 QUANT_ONE( dct[0][i], mf[i], bias[i] );
54 static void quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
57 for( i = 0; i < 16; i++ )
58 QUANT_ONE( dct[0][i], mf, bias );
61 static void quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
63 QUANT_ONE( dct[0][0], mf, bias );
64 QUANT_ONE( dct[0][1], mf, bias );
65 QUANT_ONE( dct[0][2], mf, bias );
66 QUANT_ONE( dct[0][3], mf, bias );
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 pf->quant_8x8 = quant_8x8;
199 pf->quant_4x4 = quant_4x4;
200 pf->quant_4x4_dc = quant_4x4_dc;
201 pf->quant_2x2_dc = quant_2x2_dc;
203 pf->dequant_4x4 = dequant_4x4;
204 pf->dequant_8x8 = dequant_8x8;
207 if( cpu&X264_CPU_MMX )
210 pf->quant_4x4 = x264_quant_4x4_mmx;
211 pf->quant_8x8 = x264_quant_8x8_mmx;
212 pf->dequant_4x4 = x264_dequant_4x4_mmx;
213 pf->dequant_8x8 = x264_dequant_8x8_mmx;
217 if( cpu&X264_CPU_MMXEXT )
219 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
221 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
225 if( cpu&X264_CPU_SSE2 )
227 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
228 pf->quant_4x4 = x264_quant_4x4_sse2;
229 pf->quant_8x8 = x264_quant_8x8_sse2;
230 pf->dequant_4x4 = x264_dequant_4x4_sse2;
231 pf->dequant_8x8 = x264_dequant_8x8_sse2;
236 if( cpu&X264_CPU_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;
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;
251 pf->dequant_4x4 = x264_dequant_4x4_altivec;
252 pf->dequant_8x8 = x264_dequant_8x8_altivec;