]> git.sesse.net Git - x264/blob - common/quant.c
x86 asm for some high-bit-depth coefficient functions
[x264] / common / quant.c
1 /*****************************************************************************
2  * quant.c: quantization and level-run
3  *****************************************************************************
4  * Copyright (C) 2005-2010 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Fiona Glaser <fiona@x264.com>
8  *          Christian Heine <sennindemokrit@gmx.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29
30 #if HAVE_MMX
31 #include "x86/quant.h"
32 #endif
33 #if ARCH_PPC
34 #   include "ppc/quant.h"
35 #endif
36 #if ARCH_ARM
37 #   include "arm/quant.h"
38 #endif
39
40 #define QUANT_ONE( coef, mf, f ) \
41 { \
42     if( (coef) > 0 ) \
43         (coef) = (f + (coef)) * (mf) >> 16; \
44     else \
45         (coef) = - ((f - (coef)) * (mf) >> 16); \
46     nz |= (coef); \
47 }
48
49 static int quant_8x8( dctcoef dct[64], udctcoef mf[64], udctcoef bias[64] )
50 {
51     int nz = 0;
52     for( int i = 0; i < 64; i++ )
53         QUANT_ONE( dct[i], mf[i], bias[i] );
54     return !!nz;
55 }
56
57 static int quant_4x4( dctcoef dct[16], udctcoef mf[16], udctcoef bias[16] )
58 {
59     int nz = 0;
60     for( int i = 0; i < 16; i++ )
61         QUANT_ONE( dct[i], mf[i], bias[i] );
62     return !!nz;
63 }
64
65 static int quant_4x4_dc( dctcoef dct[16], int mf, int bias )
66 {
67     int nz = 0;
68     for( int i = 0; i < 16; i++ )
69         QUANT_ONE( dct[i], mf, bias );
70     return !!nz;
71 }
72
73 static int quant_2x2_dc( dctcoef dct[4], int mf, int bias )
74 {
75     int nz = 0;
76     QUANT_ONE( dct[0], mf, bias );
77     QUANT_ONE( dct[1], mf, bias );
78     QUANT_ONE( dct[2], mf, bias );
79     QUANT_ONE( dct[3], mf, bias );
80     return !!nz;
81 }
82
83 #define DEQUANT_SHL( x ) \
84     dct[x] = ( dct[x] * dequant_mf[i_mf][x] ) << i_qbits
85
86 #define DEQUANT_SHR( x ) \
87     dct[x] = ( dct[x] * dequant_mf[i_mf][x] + f ) >> (-i_qbits)
88
89 static void dequant_4x4( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
90 {
91     const int i_mf = i_qp%6;
92     const int i_qbits = i_qp/6 - 4;
93
94     if( i_qbits >= 0 )
95     {
96         for( int i = 0; i < 16; i++ )
97             DEQUANT_SHL( i );
98     }
99     else
100     {
101         const int f = 1 << (-i_qbits-1);
102         for( int i = 0; i < 16; i++ )
103             DEQUANT_SHR( i );
104     }
105 }
106
107 static void dequant_8x8( dctcoef dct[64], int dequant_mf[6][64], int i_qp )
108 {
109     const int i_mf = i_qp%6;
110     const int i_qbits = i_qp/6 - 6;
111
112     if( i_qbits >= 0 )
113     {
114         for( int i = 0; i < 64; i++ )
115             DEQUANT_SHL( i );
116     }
117     else
118     {
119         const int f = 1 << (-i_qbits-1);
120         for( int i = 0; i < 64; i++ )
121             DEQUANT_SHR( i );
122     }
123 }
124
125 static void dequant_4x4_dc( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
126 {
127     const int i_qbits = i_qp/6 - 6;
128
129     if( i_qbits >= 0 )
130     {
131         const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
132         for( int i = 0; i < 16; i++ )
133             dct[i] *= i_dmf;
134     }
135     else
136     {
137         const int i_dmf = dequant_mf[i_qp%6][0];
138         const int f = 1 << (-i_qbits-1);
139         for( int i = 0; i < 16; i++ )
140             dct[i] = ( dct[i] * i_dmf + f ) >> (-i_qbits);
141     }
142 }
143
144 static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, udctcoef *offset, int size )
145 {
146     for( int i = 1; i < size; i++ )
147     {
148         int level = dct[i];
149         int sign = level>>31;
150         level = (level+sign)^sign;
151         sum[i] += level;
152         level -= offset[i];
153         dct[i] = level<0 ? 0 : (level^sign)-sign;
154     }
155 }
156
157 /* (ref: JVT-B118)
158  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
159  * to 0 (low score means set it to null)
160  * Used in inter macroblock (luma and chroma)
161  *  luma: for a 8x8 block: if score < 4 -> null
162  *        for the complete mb: if score < 6 -> null
163  *  chroma: for the complete mb: if score < 7 -> null
164  */
165
166 const uint8_t x264_decimate_table4[16] =
167 {
168     3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
169 };
170 const uint8_t x264_decimate_table8[64] =
171 {
172     3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
173     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
174     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
175     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
176 };
177
178 static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
179 {
180     const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
181     int i_score = 0;
182     int idx = i_max - 1;
183
184     while( idx >= 0 && dct[idx] == 0 )
185         idx--;
186     while( idx >= 0 )
187     {
188         int i_run;
189
190         if( (unsigned)(dct[idx--] + 1) > 2 )
191             return 9;
192
193         i_run = 0;
194         while( idx >= 0 && dct[idx] == 0 )
195         {
196             idx--;
197             i_run++;
198         }
199         i_score += ds_table[i_run];
200     }
201
202     return i_score;
203 }
204
205 static int x264_decimate_score15( dctcoef *dct )
206 {
207     return x264_decimate_score_internal( dct+1, 15 );
208 }
209 static int x264_decimate_score16( dctcoef *dct )
210 {
211     return x264_decimate_score_internal( dct, 16 );
212 }
213 static int x264_decimate_score64( dctcoef *dct )
214 {
215     return x264_decimate_score_internal( dct, 64 );
216 }
217
218 static int ALWAYS_INLINE x264_coeff_last_internal( dctcoef *l, int i_count )
219 {
220     int i_last = i_count-1;
221     while( i_last >= 0 && l[i_last] == 0 )
222         i_last--;
223     return i_last;
224 }
225
226 static int x264_coeff_last4( dctcoef *l )
227 {
228     return x264_coeff_last_internal( l, 4 );
229 }
230 static int x264_coeff_last15( dctcoef *l )
231 {
232     return x264_coeff_last_internal( l, 15 );
233 }
234 static int x264_coeff_last16( dctcoef *l )
235 {
236     return x264_coeff_last_internal( l, 16 );
237 }
238 static int x264_coeff_last64( dctcoef *l )
239 {
240     return x264_coeff_last_internal( l, 64 );
241 }
242
243 #define level_run(num)\
244 static int x264_coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
245 {\
246     int i_last = runlevel->last = x264_coeff_last##num(dct);\
247     int i_total = 0;\
248     do\
249     {\
250         int r = 0;\
251         runlevel->level[i_total] = dct[i_last];\
252         while( --i_last >= 0 && dct[i_last] == 0 )\
253             r++;\
254         runlevel->run[i_total++] = r;\
255     } while( i_last >= 0 );\
256     return i_total;\
257 }
258
259 level_run(4)
260 level_run(15)
261 level_run(16)
262
263
264 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
265 {
266     pf->quant_8x8 = quant_8x8;
267     pf->quant_4x4 = quant_4x4;
268     pf->quant_4x4_dc = quant_4x4_dc;
269     pf->quant_2x2_dc = quant_2x2_dc;
270
271     pf->dequant_4x4 = dequant_4x4;
272     pf->dequant_4x4_dc = dequant_4x4_dc;
273     pf->dequant_8x8 = dequant_8x8;
274
275     pf->denoise_dct = x264_denoise_dct;
276     pf->decimate_score15 = x264_decimate_score15;
277     pf->decimate_score16 = x264_decimate_score16;
278     pf->decimate_score64 = x264_decimate_score64;
279
280     pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
281     pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15;
282     pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
283     pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
284     pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
285     pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
286     pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
287
288 #if X264_HIGH_BIT_DEPTH
289 #if HAVE_MMX
290     if( cpu&X264_CPU_MMX )
291     {
292         pf->quant_4x4 = x264_quant_4x4_mmx;
293         pf->quant_8x8 = x264_quant_8x8_mmx;
294     }
295     if( cpu&X264_CPU_MMXEXT )
296     {
297         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
298         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
299 #if ARCH_X86
300         pf->denoise_dct = x264_denoise_dct_mmx;
301         pf->decimate_score15 = x264_decimate_score15_mmxext;
302         pf->decimate_score16 = x264_decimate_score16_mmxext;
303         if( cpu&X264_CPU_SLOW_CTZ )
304         {
305             pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
306             pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
307         }
308         pf->decimate_score64 = x264_decimate_score64_mmxext;
309         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmxext;
310         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
311         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
312         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
313         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
314 #endif
315         pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
316         if( cpu&X264_CPU_LZCNT )
317             pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
318     }
319     if( cpu&X264_CPU_SSE2 )
320     {
321         pf->quant_4x4 = x264_quant_4x4_sse2;
322         pf->quant_8x8 = x264_quant_8x8_sse2;
323         pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
324         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
325         pf->denoise_dct = x264_denoise_dct_sse2;
326         pf->decimate_score15 = x264_decimate_score15_sse2;
327         pf->decimate_score16 = x264_decimate_score16_sse2;
328         pf->decimate_score64 = x264_decimate_score64_sse2;
329         if( cpu&X264_CPU_SLOW_CTZ )
330         {
331             pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
332             pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
333         }
334         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
335         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
336         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
337         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
338         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
339         if( cpu&X264_CPU_LZCNT )
340         {
341             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
342             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
343             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
344             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
345             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
346         }
347     }
348     if( cpu&X264_CPU_SSSE3 )
349     {
350         pf->quant_4x4 = x264_quant_4x4_ssse3;
351         pf->quant_8x8 = x264_quant_8x8_ssse3;
352         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
353         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
354         pf->denoise_dct = x264_denoise_dct_ssse3;
355         pf->decimate_score15 = x264_decimate_score15_ssse3;
356         pf->decimate_score16 = x264_decimate_score16_ssse3;
357         if( cpu&X264_CPU_SLOW_CTZ )
358         {
359             pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
360             pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
361         }
362         pf->decimate_score64 = x264_decimate_score64_ssse3;
363     }
364     if( cpu&X264_CPU_SSE4 )
365     {
366         pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
367         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
368         pf->quant_4x4 = x264_quant_4x4_sse4;
369         pf->quant_8x8 = x264_quant_8x8_sse4;
370     }
371 #endif // HAVE_MMX
372 #else // !X264_HIGH_BIT_DEPTH
373 #if HAVE_MMX
374     if( cpu&X264_CPU_MMX )
375     {
376 #if ARCH_X86
377         pf->quant_4x4 = x264_quant_4x4_mmx;
378         pf->quant_8x8 = x264_quant_8x8_mmx;
379         pf->dequant_4x4 = x264_dequant_4x4_mmx;
380         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
381         pf->dequant_8x8 = x264_dequant_8x8_mmx;
382         if( h->param.i_cqm_preset == X264_CQM_FLAT )
383         {
384             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
385             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
386         }
387         pf->denoise_dct = x264_denoise_dct_mmx;
388 #endif
389     }
390
391     if( cpu&X264_CPU_MMXEXT )
392     {
393         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
394 #if ARCH_X86
395         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
396         pf->decimate_score15 = x264_decimate_score15_mmxext;
397         pf->decimate_score16 = x264_decimate_score16_mmxext;
398         if( cpu&X264_CPU_SLOW_CTZ )
399         {
400             pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
401             pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
402         }
403         pf->decimate_score64 = x264_decimate_score64_mmxext;
404         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmxext;
405         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
406         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
407         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
408         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
409 #endif
410         pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
411         pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
412         if( cpu&X264_CPU_LZCNT )
413         {
414             pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
415             pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
416         }
417     }
418
419     if( cpu&X264_CPU_SSE2 )
420     {
421         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
422         pf->quant_4x4 = x264_quant_4x4_sse2;
423         pf->quant_8x8 = x264_quant_8x8_sse2;
424         pf->dequant_4x4 = x264_dequant_4x4_sse2;
425         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
426         pf->dequant_8x8 = x264_dequant_8x8_sse2;
427         if( h->param.i_cqm_preset == X264_CQM_FLAT )
428         {
429             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
430             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
431         }
432         pf->denoise_dct = x264_denoise_dct_sse2;
433         pf->decimate_score15 = x264_decimate_score15_sse2;
434         pf->decimate_score16 = x264_decimate_score16_sse2;
435         pf->decimate_score64 = x264_decimate_score64_sse2;
436         if( cpu&X264_CPU_SLOW_CTZ )
437         {
438             pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
439             pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
440         }
441         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
442         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
443         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
444         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
445         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
446         if( cpu&X264_CPU_LZCNT )
447         {
448             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
449             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
450             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
451             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
452             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
453         }
454     }
455
456     if( cpu&X264_CPU_SSSE3 )
457     {
458         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
459         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
460         pf->quant_4x4 = x264_quant_4x4_ssse3;
461         pf->quant_8x8 = x264_quant_8x8_ssse3;
462         pf->denoise_dct = x264_denoise_dct_ssse3;
463         pf->decimate_score15 = x264_decimate_score15_ssse3;
464         pf->decimate_score16 = x264_decimate_score16_ssse3;
465         if( cpu&X264_CPU_SLOW_CTZ )
466         {
467             pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
468             pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
469         }
470         pf->decimate_score64 = x264_decimate_score64_ssse3;
471     }
472
473     if( cpu&X264_CPU_SSE4 )
474     {
475         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
476         pf->quant_4x4 = x264_quant_4x4_sse4;
477         pf->quant_8x8 = x264_quant_8x8_sse4;
478     }
479 #endif // HAVE_MMX
480
481 #if HAVE_ALTIVEC
482     if( cpu&X264_CPU_ALTIVEC ) {
483         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
484         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
485         pf->quant_4x4 = x264_quant_4x4_altivec;
486         pf->quant_8x8 = x264_quant_8x8_altivec;
487
488         pf->dequant_4x4 = x264_dequant_4x4_altivec;
489         pf->dequant_8x8 = x264_dequant_8x8_altivec;
490     }
491 #endif
492
493 #if HAVE_ARMV6
494     if( cpu&X264_CPU_ARMV6 )
495         pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
496
497     if( cpu&X264_CPU_NEON )
498     {
499         pf->quant_2x2_dc   = x264_quant_2x2_dc_neon;
500         pf->quant_4x4      = x264_quant_4x4_neon;
501         pf->quant_4x4_dc   = x264_quant_4x4_dc_neon;
502         pf->quant_8x8      = x264_quant_8x8_neon;
503         pf->dequant_4x4    = x264_dequant_4x4_neon;
504         pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
505         pf->dequant_8x8    = x264_dequant_8x8_neon;
506         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
507         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
508         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
509     }
510 #endif
511 #endif // X264_HIGH_BIT_DEPTH
512     pf->coeff_last[  DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
513     pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
514     pf->coeff_level_run[  DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
515     pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];
516 }