]> git.sesse.net Git - x264/blob - common/quant.c
Update file headers throughout x264
[x264] / common / quant.c
1 /*****************************************************************************
2  * quant.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2005-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Christian Heine <sennindemokrit@gmx.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "common.h"
25
26 #ifdef HAVE_MMX
27 #include "x86/quant.h"
28 #endif
29 #ifdef ARCH_PPC
30 #   include "ppc/quant.h"
31 #endif
32
33 #define QUANT_ONE( coef, mf, f ) \
34 { \
35     if( (coef) > 0 ) \
36         (coef) = (f + (coef)) * (mf) >> 16; \
37     else \
38         (coef) = - ((f - (coef)) * (mf) >> 16); \
39 }
40
41 static void quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
42 {
43     int i;
44     for( i = 0; i < 64; i++ )
45         QUANT_ONE( dct[0][i], mf[i], bias[i] );
46 }
47
48 static void quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
49 {
50     int i;
51     for( i = 0; i < 16; i++ )
52         QUANT_ONE( dct[0][i], mf[i], bias[i] );
53 }
54
55 static void quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
56 {
57     int i;
58     for( i = 0; i < 16; i++ )
59         QUANT_ONE( dct[0][i], mf, bias );
60 }
61
62 static void quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
63 {
64     QUANT_ONE( dct[0][0], mf, bias );
65     QUANT_ONE( dct[0][1], mf, bias );
66     QUANT_ONE( dct[0][2], mf, bias );
67     QUANT_ONE( dct[0][3], mf, bias );
68 }
69
70 #define DEQUANT_SHL( x ) \
71     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
72
73 #define DEQUANT_SHR( x ) \
74     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
75
76 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
77 {
78     const int i_mf = i_qp%6;
79     const int i_qbits = i_qp/6 - 4;
80     int y;
81
82     if( i_qbits >= 0 )
83     {
84         for( y = 0; y < 4; y++ )
85         {
86             DEQUANT_SHL( 0 );
87             DEQUANT_SHL( 1 );
88             DEQUANT_SHL( 2 );
89             DEQUANT_SHL( 3 );
90         }
91     }
92     else
93     {
94         const int f = 1 << (-i_qbits-1);
95         for( y = 0; y < 4; y++ )
96         {
97             DEQUANT_SHR( 0 );
98             DEQUANT_SHR( 1 );
99             DEQUANT_SHR( 2 );
100             DEQUANT_SHR( 3 );
101         }
102     }
103 }
104
105 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
106 {
107     const int i_mf = i_qp%6;
108     const int i_qbits = i_qp/6 - 6;
109     int y;
110
111     if( i_qbits >= 0 )
112     {
113         for( y = 0; y < 8; y++ )
114         {
115             DEQUANT_SHL( 0 );
116             DEQUANT_SHL( 1 );
117             DEQUANT_SHL( 2 );
118             DEQUANT_SHL( 3 );
119             DEQUANT_SHL( 4 );
120             DEQUANT_SHL( 5 );
121             DEQUANT_SHL( 6 );
122             DEQUANT_SHL( 7 );
123         }
124     }
125     else
126     {
127         const int f = 1 << (-i_qbits-1);
128         for( y = 0; y < 8; y++ )
129         {
130             DEQUANT_SHR( 0 );
131             DEQUANT_SHR( 1 );
132             DEQUANT_SHR( 2 );
133             DEQUANT_SHR( 3 );
134             DEQUANT_SHR( 4 );
135             DEQUANT_SHR( 5 );
136             DEQUANT_SHR( 6 );
137             DEQUANT_SHR( 7 );
138         }
139     }
140 }
141
142 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
143 {
144     const int i_qbits = i_qp/6 - 5;
145
146     if( i_qbits >= 0 )
147     {
148         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
149         dct[0][0] *= i_dmf;
150         dct[0][1] *= i_dmf;
151         dct[1][0] *= i_dmf;
152         dct[1][1] *= i_dmf;
153     }
154     else
155     {
156         const int i_dmf = dequant_mf[i_qp%6][0][0];
157         // chroma DC is truncated, not rounded
158         dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
159         dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
160         dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
161         dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
162     }
163 }
164
165 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
166 {
167     const int i_qbits = i_qp/6 - 6;
168     int y;
169
170     if( i_qbits >= 0 )
171     {
172         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
173
174         for( y = 0; y < 4; y++ )
175         {
176             dct[y][0] *= i_dmf;
177             dct[y][1] *= i_dmf;
178             dct[y][2] *= i_dmf;
179             dct[y][3] *= i_dmf;
180         }
181     }
182     else
183     {
184         const int i_dmf = dequant_mf[i_qp%6][0][0];
185         const int f = 1 << (-i_qbits-1);
186
187         for( y = 0; y < 4; y++ )
188         {
189             dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
190             dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
191             dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
192             dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
193         }
194     }
195 }
196
197 void x264_denoise_dct_core( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
198 {
199     int i;
200     for( i=1; i<size; i++ )
201     {
202         int level = dct[i];
203         int sign = level>>15;
204         level = (level+sign)^sign;
205         sum[i] += level;
206         level -= offset[i];
207         dct[i] = level<0 ? 0 : (level^sign)-sign;
208     }
209 }
210
211 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
212 {
213     pf->quant_8x8 = quant_8x8;
214     pf->quant_4x4 = quant_4x4;
215     pf->quant_4x4_dc = quant_4x4_dc;
216     pf->quant_2x2_dc = quant_2x2_dc;
217
218     pf->dequant_4x4 = dequant_4x4;
219     pf->dequant_8x8 = dequant_8x8;
220
221     pf->denoise_dct_core = x264_denoise_dct_core;
222
223 #ifdef HAVE_MMX
224     if( cpu&X264_CPU_MMX )
225     {
226 #ifdef ARCH_X86
227         pf->quant_4x4 = x264_quant_4x4_mmx;
228         pf->quant_8x8 = x264_quant_8x8_mmx;
229         pf->dequant_4x4 = x264_dequant_4x4_mmx;
230         pf->dequant_8x8 = x264_dequant_8x8_mmx;
231         if( h->param.i_cqm_preset == X264_CQM_FLAT )
232         {
233             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
234             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
235         }
236         pf->denoise_dct_core = x264_denoise_dct_core_mmx;
237 #endif
238     }
239
240     if( cpu&X264_CPU_MMXEXT )
241     {
242         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
243 #ifdef ARCH_X86
244         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
245 #endif
246     }
247
248     if( cpu&X264_CPU_SSE2 )
249     {
250         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
251         pf->quant_4x4 = x264_quant_4x4_sse2;
252         pf->quant_8x8 = x264_quant_8x8_sse2;
253         pf->dequant_4x4 = x264_dequant_4x4_sse2;
254         pf->dequant_8x8 = x264_dequant_8x8_sse2;
255         if( h->param.i_cqm_preset == X264_CQM_FLAT )
256         {
257             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
258             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
259         }
260         pf->denoise_dct_core = x264_denoise_dct_core_sse2;
261     }
262
263     if( cpu&X264_CPU_SSSE3 )
264     {
265         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
266         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
267         pf->quant_4x4 = x264_quant_4x4_ssse3;
268         pf->quant_8x8 = x264_quant_8x8_ssse3;
269         pf->denoise_dct_core = x264_denoise_dct_core_ssse3;
270     }
271 #endif // HAVE_MMX
272
273 #ifdef ARCH_PPC
274     if( cpu&X264_CPU_ALTIVEC ) {
275         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
276         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
277         pf->quant_4x4 = x264_quant_4x4_altivec;
278         pf->quant_8x8 = x264_quant_8x8_altivec;
279
280         pf->dequant_4x4 = x264_dequant_4x4_altivec;
281         pf->dequant_8x8 = x264_dequant_8x8_altivec;
282     }
283 #endif
284 }