]> git.sesse.net Git - x264/blob - common/quant.c
Much faster CAVLC RDO and bitstream writing
[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 static void dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
143 {
144     const int i_qbits = i_qp/6 - 6;
145     int y;
146
147     if( i_qbits >= 0 )
148     {
149         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
150
151         for( y = 0; y < 4; y++ )
152         {
153             dct[y][0] *= i_dmf;
154             dct[y][1] *= i_dmf;
155             dct[y][2] *= i_dmf;
156             dct[y][3] *= i_dmf;
157         }
158     }
159     else
160     {
161         const int i_dmf = dequant_mf[i_qp%6][0][0];
162         const int f = 1 << (-i_qbits-1);
163
164         for( y = 0; y < 4; y++ )
165         {
166             dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
167             dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
168             dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
169             dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
170         }
171     }
172 }
173
174 static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
175 {
176     int i;
177     for( i=1; i<size; i++ )
178     {
179         int level = dct[i];
180         int sign = level>>15;
181         level = (level+sign)^sign;
182         sum[i] += level;
183         level -= offset[i];
184         dct[i] = level<0 ? 0 : (level^sign)-sign;
185     }
186 }
187
188 /* (ref: JVT-B118)
189  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
190  * to 0 (low score means set it to null)
191  * Used in inter macroblock (luma and chroma)
192  *  luma: for a 8x8 block: if score < 4 -> null
193  *        for the complete mb: if score < 6 -> null
194  *  chroma: for the complete mb: if score < 7 -> null
195  */
196
197 const uint8_t x264_decimate_table4[16] = {
198     3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
199 const uint8_t x264_decimate_table8[64] = {
200     3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
201     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
202     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
203     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
204
205 static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *dct, int i_max )
206 {
207     const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
208     int i_score = 0;
209     int idx = i_max - 1;
210
211     /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned.  idx>=0 instead of 1 works correctly for the same reason */
212     while( idx >= 0 && *(uint32_t*)&dct[idx-1] == 0 )
213         idx -= 2;
214     if( idx >= 0 && dct[idx] == 0 )
215         idx--;
216     while( idx >= 0 )
217     {
218         int i_run;
219
220         if( (unsigned)(dct[idx--] + 1) > 2 )
221             return 9;
222
223         i_run = 0;
224         while( idx >= 0 && dct[idx] == 0 )
225         {
226             idx--;
227             i_run++;
228         }
229         i_score += ds_table[i_run];
230     }
231
232     return i_score;
233 }
234
235 static int x264_decimate_score15( int16_t *dct )
236 {
237     return x264_decimate_score_internal( dct+1, 15 );
238 }
239 static int x264_decimate_score16( int16_t *dct )
240 {
241     return x264_decimate_score_internal( dct, 16 );
242 }
243 static int x264_decimate_score64( int16_t *dct )
244 {
245     return x264_decimate_score_internal( dct, 64 );
246 }
247
248 static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
249 {
250     int i_last;
251     for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
252         if( *(uint64_t*)(l+i_last-3) )
253             break;
254     while( i_last >= 0 && l[i_last] == 0 )
255         i_last--;
256     return i_last;
257 }
258
259 static int x264_coeff_last4( int16_t *l )
260 {
261     return x264_coeff_last_internal( l, 4 );
262 }
263 static int x264_coeff_last15( int16_t *l )
264 {
265     return x264_coeff_last_internal( l, 15 );
266 }
267 static int x264_coeff_last16( int16_t *l )
268 {
269     return x264_coeff_last_internal( l, 16 );
270 }
271 static int x264_coeff_last64( int16_t *l )
272 {
273     return x264_coeff_last_internal( l, 64 );
274 }
275
276 #define level_run(num)\
277 static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
278 {\
279     int i_last = runlevel->last = x264_coeff_last##num(dct);\
280     int i_total = 0;\
281     do\
282     {\
283         int r = 0;\
284         runlevel->level[i_total] = dct[i_last];\
285         while( --i_last >= 0 && dct[i_last] == 0 )\
286             r++;\
287         runlevel->run[i_total++] = r;\
288     } while( i_last >= 0 );\
289     return i_total;\
290 }
291
292 level_run(4)
293 level_run(15)
294 level_run(16)
295
296
297 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
298 {
299     pf->quant_8x8 = quant_8x8;
300     pf->quant_4x4 = quant_4x4;
301     pf->quant_4x4_dc = quant_4x4_dc;
302     pf->quant_2x2_dc = quant_2x2_dc;
303
304     pf->dequant_4x4 = dequant_4x4;
305     pf->dequant_4x4_dc = dequant_4x4_dc;
306     pf->dequant_8x8 = dequant_8x8;
307
308     pf->denoise_dct = x264_denoise_dct;
309     pf->decimate_score15 = x264_decimate_score15;
310     pf->decimate_score16 = x264_decimate_score16;
311     pf->decimate_score64 = x264_decimate_score64;
312
313     pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
314     pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15;
315     pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
316     pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
317     pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
318     pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
319     pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
320
321 #ifdef HAVE_MMX
322     if( cpu&X264_CPU_MMX )
323     {
324 #ifdef ARCH_X86
325         pf->quant_4x4 = x264_quant_4x4_mmx;
326         pf->quant_8x8 = x264_quant_8x8_mmx;
327         pf->dequant_4x4 = x264_dequant_4x4_mmx;
328         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
329         pf->dequant_8x8 = x264_dequant_8x8_mmx;
330         if( h->param.i_cqm_preset == X264_CQM_FLAT )
331         {
332             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
333             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
334         }
335         pf->denoise_dct = x264_denoise_dct_mmx;
336 #endif
337     }
338
339     if( cpu&X264_CPU_MMXEXT )
340     {
341         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
342 #ifdef ARCH_X86
343         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
344         pf->decimate_score15 = x264_decimate_score15_mmxext;
345         pf->decimate_score16 = x264_decimate_score16_mmxext;
346         pf->decimate_score64 = x264_decimate_score64_mmxext;
347         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmxext;
348         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
349         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
350         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
351         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
352 #endif
353         pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
354         pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
355     }
356
357     if( cpu&X264_CPU_SSE2 )
358     {
359         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
360         pf->quant_4x4 = x264_quant_4x4_sse2;
361         pf->quant_8x8 = x264_quant_8x8_sse2;
362         pf->dequant_4x4 = x264_dequant_4x4_sse2;
363         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
364         pf->dequant_8x8 = x264_dequant_8x8_sse2;
365         if( h->param.i_cqm_preset == X264_CQM_FLAT )
366         {
367             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
368             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
369         }
370         pf->denoise_dct = x264_denoise_dct_sse2;
371         pf->decimate_score15 = x264_decimate_score15_sse2;
372         pf->decimate_score16 = x264_decimate_score16_sse2;
373         pf->decimate_score64 = x264_decimate_score64_sse2;
374         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
375         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
376         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
377         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
378         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
379     }
380
381     if( cpu&X264_CPU_SSSE3 )
382     {
383         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
384         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
385         pf->quant_4x4 = x264_quant_4x4_ssse3;
386         pf->quant_8x8 = x264_quant_8x8_ssse3;
387         pf->denoise_dct = x264_denoise_dct_ssse3;
388         pf->decimate_score15 = x264_decimate_score15_ssse3;
389         pf->decimate_score16 = x264_decimate_score16_ssse3;
390         pf->decimate_score64 = x264_decimate_score64_ssse3;
391     }
392 #endif // HAVE_MMX
393
394 #ifdef ARCH_PPC
395     if( cpu&X264_CPU_ALTIVEC ) {
396         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
397         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
398         pf->quant_4x4 = x264_quant_4x4_altivec;
399         pf->quant_8x8 = x264_quant_8x8_altivec;
400
401         pf->dequant_4x4 = x264_dequant_4x4_altivec;
402         pf->dequant_8x8 = x264_dequant_8x8_altivec;
403     }
404 #endif
405     pf->coeff_last[  DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
406     pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
407     pf->coeff_level_run[  DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
408     pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];
409 }