]> git.sesse.net Git - x264/blob - common/quant.c
conflate HAVE_MMXEXT with HAVE_SSE2, since they were never used distinctly.
[x264] / common / quant.c
1 /*****************************************************************************
2  * quant.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2005 x264 project
5  *
6  * Authors: Christian Heine <sennindemokrit@gmx.net>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #include "common.h"
24
25 #ifdef HAVE_MMX
26 #include "i386/quant.h"
27 #endif
28 #ifdef ARCH_PPC
29 #   include "ppc/quant.h"
30 #endif
31
32 #define QUANT_ONE( coef, mf ) \
33 { \
34     if( (coef) > 0 ) \
35         (coef) = ( f + (coef) * (mf) ) >> i_qbits; \
36     else \
37         (coef) = - ( ( f - (coef) * (mf) ) >> i_qbits ); \
38 }
39
40 static void quant_8x8_core( int16_t dct[8][8], int quant_mf[8][8], int i_qbits, int f )
41 {
42     int i;
43     for( i = 0; i < 64; i++ )
44         QUANT_ONE( dct[0][i], quant_mf[0][i] );
45 }
46
47 static void quant_4x4_core( int16_t dct[4][4], int quant_mf[4][4], int i_qbits, int f )
48 {
49     int i;
50     for( i = 0; i < 16; i++ )
51         QUANT_ONE( dct[0][i], quant_mf[0][i] );
52 }
53
54 static void quant_4x4_dc_core( int16_t dct[4][4], int i_quant_mf, int i_qbits, int f )
55 {
56     int i;
57     for( i = 0; i < 16; i++ )
58         QUANT_ONE( dct[0][i], i_quant_mf );
59 }
60
61 static void quant_2x2_dc_core( int16_t dct[2][2], int i_quant_mf, int i_qbits, int f )
62 {
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 );
67 }
68
69 #define DEQUANT_SHL( x ) \
70     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
71
72 #define DEQUANT_SHR( x ) \
73     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
74
75 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
76 {
77     const int i_mf = i_qp%6;
78     const int i_qbits = i_qp/6 - 4;
79     int y;
80
81     if( i_qbits >= 0 )
82     {
83         for( y = 0; y < 4; y++ )
84         {
85             DEQUANT_SHL( 0 );
86             DEQUANT_SHL( 1 );
87             DEQUANT_SHL( 2 );
88             DEQUANT_SHL( 3 );
89         }
90     }
91     else
92     {
93         const int f = 1 << (-i_qbits-1);
94         for( y = 0; y < 4; y++ )
95         {
96             DEQUANT_SHR( 0 );
97             DEQUANT_SHR( 1 );
98             DEQUANT_SHR( 2 );
99             DEQUANT_SHR( 3 );
100         }
101     }
102 }
103
104 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
105 {
106     const int i_mf = i_qp%6;
107     const int i_qbits = i_qp/6 - 6;
108     int y;
109
110     if( i_qbits >= 0 )
111     {
112         for( y = 0; y < 8; y++ )
113         {
114             DEQUANT_SHL( 0 );
115             DEQUANT_SHL( 1 );
116             DEQUANT_SHL( 2 );
117             DEQUANT_SHL( 3 );
118             DEQUANT_SHL( 4 );
119             DEQUANT_SHL( 5 );
120             DEQUANT_SHL( 6 );
121             DEQUANT_SHL( 7 );
122         }
123     }
124     else
125     {
126         const int f = 1 << (-i_qbits-1);
127         for( y = 0; y < 8; y++ )
128         {
129             DEQUANT_SHR( 0 );
130             DEQUANT_SHR( 1 );
131             DEQUANT_SHR( 2 );
132             DEQUANT_SHR( 3 );
133             DEQUANT_SHR( 4 );
134             DEQUANT_SHR( 5 );
135             DEQUANT_SHR( 6 );
136             DEQUANT_SHR( 7 );
137         }
138     }
139 }
140
141 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
142 {
143     const int i_qbits = i_qp/6 - 5;
144
145     if( i_qbits >= 0 )
146     {
147         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
148         dct[0][0] *= i_dmf;
149         dct[0][1] *= i_dmf;
150         dct[1][0] *= i_dmf;
151         dct[1][1] *= i_dmf;
152     }
153     else
154     {
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);
161     }
162 }
163
164 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
165 {
166     const int i_qbits = i_qp/6 - 6;
167     int y;
168
169     if( i_qbits >= 0 )
170     {
171         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
172
173         for( y = 0; y < 4; y++ )
174         {
175             dct[y][0] *= i_dmf;
176             dct[y][1] *= i_dmf;
177             dct[y][2] *= i_dmf;
178             dct[y][3] *= i_dmf;
179         }
180     }
181     else
182     {
183         const int i_dmf = dequant_mf[i_qp%6][0][0];
184         const int f = 1 << (-i_qbits-1);
185
186         for( y = 0; y < 4; y++ )
187         {
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);
192         }
193     }
194 }
195
196 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
197 {
198     int i, j, maxQ8=0, maxQ4=0, maxQdc=0;
199
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;
204
205     pf->dequant_4x4 = dequant_4x4;
206     pf->dequant_8x8 = dequant_8x8;
207
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++ )
211         {
212             int q = h->quant8_mf[j][0][0][i];
213             if( maxQ8 < q )
214                 maxQ8 = q;
215         }
216
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++ )
221         {
222             int q = h->quant4_mf[j][0][0][i];
223             if( maxQ4 < q )
224                 maxQ4 = q;
225             if( maxQdc < q && i%16 == 0 )
226                 maxQdc = q;
227         }
228
229 #ifdef HAVE_MMX
230
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;
235     else
236 #endif
237     if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
238         pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
239     else
240     if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
241         pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
242     else
243     if( cpu&X264_CPU_MMXEXT )
244         pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
245
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;
250     else
251 #endif
252     if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
253         pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
254     else
255     if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
256         pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
257     else
258     if( cpu&X264_CPU_MMXEXT )
259         pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
260
261     /* select quant_XxX_dc based on CPU and maxQdc */
262     if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
263     {
264         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
265         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
266     }
267     else
268     if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
269     {
270         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
271         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
272     }
273     else
274     if( cpu&X264_CPU_MMXEXT )
275     {
276         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
277         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
278     }
279
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;
283 #endif
284
285     if( cpu&X264_CPU_MMX )
286     {
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;
292     }
293 #endif  /* HAVE_MMX */
294     
295 #ifdef ARCH_PPC
296     if( cpu&X264_CPU_ALTIVEC ) {
297         if( maxQ8 < (1<<16) )
298         {
299             pf->quant_8x8_core = x264_quant_8x8_altivec;
300         }
301         if( maxQ4 < (1<<16) )
302         {
303             pf->quant_4x4_core = x264_quant_4x4_altivec;
304         }
305         if( maxQdc < (1<<16) )
306         {
307            pf->quant_4x4_dc_core = x264_quant_4x4_dc_altivec;
308         }
309     }
310 #endif /* ARCH_PPC */
311 }