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