]> git.sesse.net Git - x264/blob - common/quant.c
Faster 8x8dct+CAVLC interleave
[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     nz |= (coef); \
40 }
41
42 static int quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
43 {
44     int i, nz = 0;
45     for( i = 0; i < 64; i++ )
46         QUANT_ONE( dct[0][i], mf[i], bias[i] );
47     return !!nz;
48 }
49
50 static int quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
51 {
52     int i, nz = 0;
53     for( i = 0; i < 16; i++ )
54         QUANT_ONE( dct[0][i], mf[i], bias[i] );
55     return !!nz;
56 }
57
58 static int quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
59 {
60     int i, nz = 0;
61     for( i = 0; i < 16; i++ )
62         QUANT_ONE( dct[0][i], mf, bias );
63     return !!nz;
64 }
65
66 static int quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
67 {
68     int nz = 0;
69     QUANT_ONE( dct[0][0], mf, bias );
70     QUANT_ONE( dct[0][1], mf, bias );
71     QUANT_ONE( dct[0][2], mf, bias );
72     QUANT_ONE( dct[0][3], mf, bias );
73     return !!nz;
74 }
75
76 #define DEQUANT_SHL( x ) \
77     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
78
79 #define DEQUANT_SHR( x ) \
80     dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
81
82 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
83 {
84     const int i_mf = i_qp%6;
85     const int i_qbits = i_qp/6 - 4;
86     int y;
87
88     if( i_qbits >= 0 )
89     {
90         for( y = 0; y < 4; y++ )
91         {
92             DEQUANT_SHL( 0 );
93             DEQUANT_SHL( 1 );
94             DEQUANT_SHL( 2 );
95             DEQUANT_SHL( 3 );
96         }
97     }
98     else
99     {
100         const int f = 1 << (-i_qbits-1);
101         for( y = 0; y < 4; y++ )
102         {
103             DEQUANT_SHR( 0 );
104             DEQUANT_SHR( 1 );
105             DEQUANT_SHR( 2 );
106             DEQUANT_SHR( 3 );
107         }
108     }
109 }
110
111 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
112 {
113     const int i_mf = i_qp%6;
114     const int i_qbits = i_qp/6 - 6;
115     int y;
116
117     if( i_qbits >= 0 )
118     {
119         for( y = 0; y < 8; y++ )
120         {
121             DEQUANT_SHL( 0 );
122             DEQUANT_SHL( 1 );
123             DEQUANT_SHL( 2 );
124             DEQUANT_SHL( 3 );
125             DEQUANT_SHL( 4 );
126             DEQUANT_SHL( 5 );
127             DEQUANT_SHL( 6 );
128             DEQUANT_SHL( 7 );
129         }
130     }
131     else
132     {
133         const int f = 1 << (-i_qbits-1);
134         for( y = 0; y < 8; y++ )
135         {
136             DEQUANT_SHR( 0 );
137             DEQUANT_SHR( 1 );
138             DEQUANT_SHR( 2 );
139             DEQUANT_SHR( 3 );
140             DEQUANT_SHR( 4 );
141             DEQUANT_SHR( 5 );
142             DEQUANT_SHR( 6 );
143             DEQUANT_SHR( 7 );
144         }
145     }
146 }
147
148 static void dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
149 {
150     const int i_qbits = i_qp/6 - 6;
151     int y;
152
153     if( i_qbits >= 0 )
154     {
155         const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
156
157         for( y = 0; y < 4; y++ )
158         {
159             dct[y][0] *= i_dmf;
160             dct[y][1] *= i_dmf;
161             dct[y][2] *= i_dmf;
162             dct[y][3] *= i_dmf;
163         }
164     }
165     else
166     {
167         const int i_dmf = dequant_mf[i_qp%6][0][0];
168         const int f = 1 << (-i_qbits-1);
169
170         for( y = 0; y < 4; y++ )
171         {
172             dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
173             dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
174             dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
175             dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
176         }
177     }
178 }
179
180 static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
181 {
182     int i;
183     for( i=1; i<size; i++ )
184     {
185         int level = dct[i];
186         int sign = level>>15;
187         level = (level+sign)^sign;
188         sum[i] += level;
189         level -= offset[i];
190         dct[i] = level<0 ? 0 : (level^sign)-sign;
191     }
192 }
193
194 /* (ref: JVT-B118)
195  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
196  * to 0 (low score means set it to null)
197  * Used in inter macroblock (luma and chroma)
198  *  luma: for a 8x8 block: if score < 4 -> null
199  *        for the complete mb: if score < 6 -> null
200  *  chroma: for the complete mb: if score < 7 -> null
201  */
202
203 const uint8_t x264_decimate_table4[16] = {
204     3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
205 const uint8_t x264_decimate_table8[64] = {
206     3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
207     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
208     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
209     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
210
211 static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *dct, int i_max )
212 {
213     const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
214     int i_score = 0;
215     int idx = i_max - 1;
216
217     /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned.  idx>=0 instead of 1 works correctly for the same reason */
218     while( idx >= 0 && *(uint32_t*)&dct[idx-1] == 0 )
219         idx -= 2;
220     if( idx >= 0 && dct[idx] == 0 )
221         idx--;
222     while( idx >= 0 )
223     {
224         int i_run;
225
226         if( (unsigned)(dct[idx--] + 1) > 2 )
227             return 9;
228
229         i_run = 0;
230         while( idx >= 0 && dct[idx] == 0 )
231         {
232             idx--;
233             i_run++;
234         }
235         i_score += ds_table[i_run];
236     }
237
238     return i_score;
239 }
240
241 static int x264_decimate_score15( int16_t *dct )
242 {
243     return x264_decimate_score_internal( dct+1, 15 );
244 }
245 static int x264_decimate_score16( int16_t *dct )
246 {
247     return x264_decimate_score_internal( dct, 16 );
248 }
249 static int x264_decimate_score64( int16_t *dct )
250 {
251     return x264_decimate_score_internal( dct, 64 );
252 }
253
254 static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
255 {
256     int i_last;
257     for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
258         if( *(uint64_t*)(l+i_last-3) )
259             break;
260     while( i_last >= 0 && l[i_last] == 0 )
261         i_last--;
262     return i_last;
263 }
264
265 static int x264_coeff_last4( int16_t *l )
266 {
267     return x264_coeff_last_internal( l, 4 );
268 }
269 static int x264_coeff_last15( int16_t *l )
270 {
271     return x264_coeff_last_internal( l, 15 );
272 }
273 static int x264_coeff_last16( int16_t *l )
274 {
275     return x264_coeff_last_internal( l, 16 );
276 }
277 static int x264_coeff_last64( int16_t *l )
278 {
279     return x264_coeff_last_internal( l, 64 );
280 }
281
282 #define level_run(num)\
283 static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
284 {\
285     int i_last = runlevel->last = x264_coeff_last##num(dct);\
286     int i_total = 0;\
287     do\
288     {\
289         int r = 0;\
290         runlevel->level[i_total] = dct[i_last];\
291         while( --i_last >= 0 && dct[i_last] == 0 )\
292             r++;\
293         runlevel->run[i_total++] = r;\
294     } while( i_last >= 0 );\
295     return i_total;\
296 }
297
298 level_run(4)
299 level_run(15)
300 level_run(16)
301
302
303 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
304 {
305     pf->quant_8x8 = quant_8x8;
306     pf->quant_4x4 = quant_4x4;
307     pf->quant_4x4_dc = quant_4x4_dc;
308     pf->quant_2x2_dc = quant_2x2_dc;
309
310     pf->dequant_4x4 = dequant_4x4;
311     pf->dequant_4x4_dc = dequant_4x4_dc;
312     pf->dequant_8x8 = dequant_8x8;
313
314     pf->denoise_dct = x264_denoise_dct;
315     pf->decimate_score15 = x264_decimate_score15;
316     pf->decimate_score16 = x264_decimate_score16;
317     pf->decimate_score64 = x264_decimate_score64;
318
319     pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
320     pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15;
321     pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
322     pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
323     pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
324     pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
325     pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
326
327 #ifdef HAVE_MMX
328     if( cpu&X264_CPU_MMX )
329     {
330 #ifdef ARCH_X86
331         pf->quant_4x4 = x264_quant_4x4_mmx;
332         pf->quant_8x8 = x264_quant_8x8_mmx;
333         pf->dequant_4x4 = x264_dequant_4x4_mmx;
334         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
335         pf->dequant_8x8 = x264_dequant_8x8_mmx;
336         if( h->param.i_cqm_preset == X264_CQM_FLAT )
337         {
338             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
339             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
340         }
341         pf->denoise_dct = x264_denoise_dct_mmx;
342 #endif
343     }
344
345     if( cpu&X264_CPU_MMXEXT )
346     {
347         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
348 #ifdef ARCH_X86
349         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
350         pf->decimate_score15 = x264_decimate_score15_mmxext;
351         pf->decimate_score16 = x264_decimate_score16_mmxext;
352         pf->decimate_score64 = x264_decimate_score64_mmxext;
353         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmxext;
354         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
355         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
356         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
357         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
358 #endif
359         pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
360         pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
361         if( cpu&X264_CPU_LZCNT )
362         {
363             pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
364             pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
365         }
366     }
367
368     if( cpu&X264_CPU_SSE2 )
369     {
370         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
371         pf->quant_4x4 = x264_quant_4x4_sse2;
372         pf->quant_8x8 = x264_quant_8x8_sse2;
373         pf->dequant_4x4 = x264_dequant_4x4_sse2;
374         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
375         pf->dequant_8x8 = x264_dequant_8x8_sse2;
376         if( h->param.i_cqm_preset == X264_CQM_FLAT )
377         {
378             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
379             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
380         }
381         pf->denoise_dct = x264_denoise_dct_sse2;
382         pf->decimate_score15 = x264_decimate_score15_sse2;
383         pf->decimate_score16 = x264_decimate_score16_sse2;
384         pf->decimate_score64 = x264_decimate_score64_sse2;
385         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
386         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
387         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
388         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
389         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
390         if( cpu&X264_CPU_LZCNT )
391         {
392             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
393             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
394             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
395             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
396             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
397         }
398     }
399
400     if( cpu&X264_CPU_SSSE3 )
401     {
402         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
403         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
404         pf->quant_4x4 = x264_quant_4x4_ssse3;
405         pf->quant_8x8 = x264_quant_8x8_ssse3;
406         pf->denoise_dct = x264_denoise_dct_ssse3;
407         pf->decimate_score15 = x264_decimate_score15_ssse3;
408         pf->decimate_score16 = x264_decimate_score16_ssse3;
409         pf->decimate_score64 = x264_decimate_score64_ssse3;
410     }
411
412     if( cpu&X264_CPU_SSE4 )
413     {
414         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
415         pf->quant_4x4 = x264_quant_4x4_sse4;
416         pf->quant_8x8 = x264_quant_8x8_sse4;
417     }
418 #endif // HAVE_MMX
419
420 #ifdef ARCH_PPC
421     if( cpu&X264_CPU_ALTIVEC ) {
422         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
423         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
424         pf->quant_4x4 = x264_quant_4x4_altivec;
425         pf->quant_8x8 = x264_quant_8x8_altivec;
426
427         pf->dequant_4x4 = x264_dequant_4x4_altivec;
428         pf->dequant_8x8 = x264_dequant_8x8_altivec;
429     }
430 #endif
431     pf->coeff_last[  DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
432     pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
433     pf->coeff_level_run[  DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
434     pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];
435 }