]> git.sesse.net Git - x264/blob - common/quant.c
Disable Altivec and VIS optimizations when --disable-asm is specified
[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 #ifdef 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( int16_t dct[64], uint16_t mf[64], uint16_t bias[64] )
46 {
47     int i, nz = 0;
48     for( i = 0; i < 64; i++ )
49         QUANT_ONE( dct[i], mf[i], bias[i] );
50     return !!nz;
51 }
52
53 static int quant_4x4( int16_t dct[16], uint16_t mf[16], uint16_t bias[16] )
54 {
55     int i, nz = 0;
56     for( 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( int16_t dct[16], int mf, int bias )
62 {
63     int i, nz = 0;
64     for( i = 0; i < 16; i++ )
65         QUANT_ONE( dct[i], mf, bias );
66     return !!nz;
67 }
68
69 static int quant_2x2_dc( int16_t 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( int16_t 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     int i;
90
91     if( i_qbits >= 0 )
92     {
93         for( i = 0; i < 16; i++ )
94             DEQUANT_SHL( i );
95     }
96     else
97     {
98         const int f = 1 << (-i_qbits-1);
99         for( i = 0; i < 16; i++ )
100             DEQUANT_SHR( i );
101     }
102 }
103
104 static void dequant_8x8( int16_t dct[64], int dequant_mf[6][64], int i_qp )
105 {
106     const int i_mf = i_qp%6;
107     const int i_qbits = i_qp/6 - 6;
108     int i;
109
110     if( i_qbits >= 0 )
111     {
112         for( i = 0; i < 64; i++ )
113             DEQUANT_SHL( i );
114     }
115     else
116     {
117         const int f = 1 << (-i_qbits-1);
118         for( i = 0; i < 64; i++ )
119             DEQUANT_SHR( i );
120     }
121 }
122
123 static void dequant_4x4_dc( int16_t dct[16], int dequant_mf[6][16], int i_qp )
124 {
125     const int i_qbits = i_qp/6 - 6;
126     int i;
127
128     if( i_qbits >= 0 )
129     {
130         const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
131         for( i = 0; i < 16; i++ )
132             dct[i] *= i_dmf;
133     }
134     else
135     {
136         const int i_dmf = dequant_mf[i_qp%6][0];
137         const int f = 1 << (-i_qbits-1);
138         for( i = 0; i < 16; i++ )
139             dct[i] = ( dct[i] * i_dmf + f ) >> (-i_qbits);
140     }
141 }
142
143 static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
144 {
145     int i;
146     for( i=1; i<size; i++ )
147     {
148         int level = dct[i];
149         int sign = level>>15;
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     3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
168 const uint8_t x264_decimate_table8[64] = {
169     3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
170     1,1,1,1,1,1,1,1,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     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
173
174 static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *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     /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned.  idx>=0 instead of 1 works correctly for the same reason */
181     while( idx >= 0 && M32( &dct[idx-1] ) == 0 )
182         idx -= 2;
183     if( idx >= 0 && dct[idx] == 0 )
184         idx--;
185     while( idx >= 0 )
186     {
187         int i_run;
188
189         if( (unsigned)(dct[idx--] + 1) > 2 )
190             return 9;
191
192         i_run = 0;
193         while( idx >= 0 && dct[idx] == 0 )
194         {
195             idx--;
196             i_run++;
197         }
198         i_score += ds_table[i_run];
199     }
200
201     return i_score;
202 }
203
204 static int x264_decimate_score15( int16_t *dct )
205 {
206     return x264_decimate_score_internal( dct+1, 15 );
207 }
208 static int x264_decimate_score16( int16_t *dct )
209 {
210     return x264_decimate_score_internal( dct, 16 );
211 }
212 static int x264_decimate_score64( int16_t *dct )
213 {
214     return x264_decimate_score_internal( dct, 64 );
215 }
216
217 static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
218 {
219     int i_last;
220     for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
221         if( M64( l+i_last-3 ) )
222             break;
223     while( i_last >= 0 && l[i_last] == 0 )
224         i_last--;
225     return i_last;
226 }
227
228 static int x264_coeff_last4( int16_t *l )
229 {
230     return x264_coeff_last_internal( l, 4 );
231 }
232 static int x264_coeff_last15( int16_t *l )
233 {
234     return x264_coeff_last_internal( l, 15 );
235 }
236 static int x264_coeff_last16( int16_t *l )
237 {
238     return x264_coeff_last_internal( l, 16 );
239 }
240 static int x264_coeff_last64( int16_t *l )
241 {
242     return x264_coeff_last_internal( l, 64 );
243 }
244
245 #define level_run(num)\
246 static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
247 {\
248     int i_last = runlevel->last = x264_coeff_last##num(dct);\
249     int i_total = 0;\
250     do\
251     {\
252         int r = 0;\
253         runlevel->level[i_total] = dct[i_last];\
254         while( --i_last >= 0 && dct[i_last] == 0 )\
255             r++;\
256         runlevel->run[i_total++] = r;\
257     } while( i_last >= 0 );\
258     return i_total;\
259 }
260
261 level_run(4)
262 level_run(15)
263 level_run(16)
264
265
266 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
267 {
268     pf->quant_8x8 = quant_8x8;
269     pf->quant_4x4 = quant_4x4;
270     pf->quant_4x4_dc = quant_4x4_dc;
271     pf->quant_2x2_dc = quant_2x2_dc;
272
273     pf->dequant_4x4 = dequant_4x4;
274     pf->dequant_4x4_dc = dequant_4x4_dc;
275     pf->dequant_8x8 = dequant_8x8;
276
277     pf->denoise_dct = x264_denoise_dct;
278     pf->decimate_score15 = x264_decimate_score15;
279     pf->decimate_score16 = x264_decimate_score16;
280     pf->decimate_score64 = x264_decimate_score64;
281
282     pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
283     pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15;
284     pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
285     pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
286     pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
287     pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
288     pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
289
290 #ifdef HAVE_MMX
291     if( cpu&X264_CPU_MMX )
292     {
293 #ifdef ARCH_X86
294         pf->quant_4x4 = x264_quant_4x4_mmx;
295         pf->quant_8x8 = x264_quant_8x8_mmx;
296         pf->dequant_4x4 = x264_dequant_4x4_mmx;
297         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
298         pf->dequant_8x8 = x264_dequant_8x8_mmx;
299         if( h->param.i_cqm_preset == X264_CQM_FLAT )
300         {
301             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
302             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
303         }
304         pf->denoise_dct = x264_denoise_dct_mmx;
305 #endif
306     }
307
308     if( cpu&X264_CPU_MMXEXT )
309     {
310         pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
311 #ifdef ARCH_X86
312         pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
313         pf->decimate_score15 = x264_decimate_score15_mmxext;
314         pf->decimate_score16 = x264_decimate_score16_mmxext;
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         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
349         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
350         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
351         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
352         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
353         if( cpu&X264_CPU_LZCNT )
354         {
355             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
356             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
357             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
358             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
359             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
360         }
361     }
362
363     if( cpu&X264_CPU_SSSE3 )
364     {
365         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
366         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
367         pf->quant_4x4 = x264_quant_4x4_ssse3;
368         pf->quant_8x8 = x264_quant_8x8_ssse3;
369         pf->denoise_dct = x264_denoise_dct_ssse3;
370         pf->decimate_score15 = x264_decimate_score15_ssse3;
371         pf->decimate_score16 = x264_decimate_score16_ssse3;
372         pf->decimate_score64 = x264_decimate_score64_ssse3;
373     }
374
375     if( cpu&X264_CPU_SSE4 )
376     {
377         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
378         pf->quant_4x4 = x264_quant_4x4_sse4;
379         pf->quant_8x8 = x264_quant_8x8_sse4;
380     }
381 #endif // HAVE_MMX
382
383 #ifdef HAVE_ALTIVEC
384     if( cpu&X264_CPU_ALTIVEC ) {
385         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
386         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
387         pf->quant_4x4 = x264_quant_4x4_altivec;
388         pf->quant_8x8 = x264_quant_8x8_altivec;
389
390         pf->dequant_4x4 = x264_dequant_4x4_altivec;
391         pf->dequant_8x8 = x264_dequant_8x8_altivec;
392     }
393 #endif
394
395 #ifdef HAVE_ARMV6
396     if( cpu&X264_CPU_ARMV6 )
397         pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
398
399     if( cpu&X264_CPU_NEON )
400     {
401         pf->quant_2x2_dc   = x264_quant_2x2_dc_neon;
402         pf->quant_4x4      = x264_quant_4x4_neon;
403         pf->quant_4x4_dc   = x264_quant_4x4_dc_neon;
404         pf->quant_8x8      = x264_quant_8x8_neon;
405         pf->dequant_4x4    = x264_dequant_4x4_neon;
406         pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
407         pf->dequant_8x8    = x264_dequant_8x8_neon;
408         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
409         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
410         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
411     }
412 #endif
413     pf->coeff_last[  DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
414     pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
415     pf->coeff_level_run[  DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
416     pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];
417 }