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