]> git.sesse.net Git - x264/blob - common/quant.c
Merges Guillaume Poirier's AltiVec changes:
[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_MMXEXT
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_MMXEXT
230
231     /* select quant_8x8 based on CPU and maxQ8 */
232     if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
233         pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
234     else
235     if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
236         pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
237     else
238     if( cpu&X264_CPU_MMXEXT )
239         pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
240
241     /* select quant_4x4 based on CPU and maxQ4 */
242     if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
243         pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
244     else
245     if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
246         pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
247     else
248     if( cpu&X264_CPU_MMXEXT )
249         pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
250
251     /* select quant_XxX_dc based on CPU and maxQdc */
252     if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
253     {
254         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
255         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
256     }
257     else
258     if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
259     {
260         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
261         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
262     }
263     else
264     if( cpu&X264_CPU_MMXEXT )
265     {
266         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
267         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
268     }
269
270     if( cpu&X264_CPU_MMX )
271     {
272         /* dequant is not subject to the above CQM-dependent overflow issues,
273          * as long as the inputs are in the range generable by dct+quant.
274          * that is not guaranteed by the standard, but is true within x264 */
275         pf->dequant_4x4 = x264_dequant_4x4_mmx;
276         pf->dequant_8x8 = x264_dequant_8x8_mmx;
277     }
278 #endif  /* HAVE_MMXEXT */
279     
280 #ifdef ARCH_PPC
281     if( cpu&X264_CPU_ALTIVEC ) {
282         if( maxQ8 < (1<<16) )
283         {
284             pf->quant_8x8_core = x264_quant_8x8_altivec;
285         }
286         if( maxQ4 < (1<<16) )
287         {
288             pf->quant_4x4_core = x264_quant_4x4_altivec;
289         }
290         if( maxQdc < (1<<16) )
291         {
292            pf->quant_4x4_dc_core = x264_quant_4x4_dc_altivec;
293         }
294     }
295 #endif /* ARCH_PPC */
296 }