]> git.sesse.net Git - x264/blob - common/quant.c
2x faster quant. 2% overall.
[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, f ) \
33 { \
34     if( (coef) > 0 ) \
35         (coef) = (f + (coef)) * (mf) >> 16; \
36     else \
37         (coef) = - ((f - (coef)) * (mf) >> 16); \
38 }
39
40 static void quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
41 {
42     int i;
43     for( i = 0; i < 64; i++ )
44         QUANT_ONE( dct[0][i], mf[i], bias[i] );
45 }
46
47 static void quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
48 {
49     int i;
50     for( i = 0; i < 16; i++ )
51         QUANT_ONE( dct[0][i], mf[i], bias[i] );
52 }
53
54 static void quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
55 {
56     int i;
57     for( i = 0; i < 16; i++ )
58         QUANT_ONE( dct[0][i], mf, bias );
59 }
60
61 static void quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
62 {
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 );
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     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;
202
203     pf->dequant_4x4 = dequant_4x4;
204     pf->dequant_8x8 = dequant_8x8;
205
206 #ifdef HAVE_MMX
207     if( cpu&X264_CPU_MMX )
208     {
209 #ifdef ARCH_X86
210         pf->quant_4x4 = x264_quant_4x4_mmx;
211         pf->quant_8x8 = x264_quant_8x8_mmx;
212 #endif
213         pf->dequant_4x4 = x264_dequant_4x4_mmx;
214         pf->dequant_8x8 = x264_dequant_8x8_mmx;
215     }
216
217     if( cpu&X264_CPU_MMXEXT )
218     {
219         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
220 #ifdef ARCH_X86
221         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
222 #endif
223     }
224
225     if( cpu&X264_CPU_SSE2 )
226     {
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     }
231 #endif
232
233 #ifdef HAVE_SSE3
234     if( cpu&X264_CPU_SSSE3 )
235     {
236         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
237         pf->quant_4x4 = x264_quant_4x4_ssse3;
238         pf->quant_8x8 = x264_quant_8x8_ssse3;
239     }
240 #endif
241 }