1 /*****************************************************************************
2 * quant.c: quantization and level-run
3 *****************************************************************************
4 * Copyright (C) 2005-2010 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Fiona Glaser <fiona@x264.com>
8 * Christian Heine <sennindemokrit@gmx.net>
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.
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.
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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
31 #include "x86/quant.h"
34 # include "ppc/quant.h"
37 # include "arm/quant.h"
40 #define QUANT_ONE( coef, mf, f ) \
43 (coef) = (f + (coef)) * (mf) >> 16; \
45 (coef) = - ((f - (coef)) * (mf) >> 16); \
49 static int quant_8x8( dctcoef dct[64], udctcoef mf[64], udctcoef bias[64] )
52 for( int i = 0; i < 64; i++ )
53 QUANT_ONE( dct[i], mf[i], bias[i] );
57 static int quant_4x4( dctcoef dct[16], udctcoef mf[16], udctcoef bias[16] )
60 for( int i = 0; i < 16; i++ )
61 QUANT_ONE( dct[i], mf[i], bias[i] );
65 static int quant_4x4_dc( dctcoef dct[16], int mf, int bias )
68 for( int i = 0; i < 16; i++ )
69 QUANT_ONE( dct[i], mf, bias );
73 static int quant_2x2_dc( dctcoef dct[4], int mf, int bias )
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 );
83 #define DEQUANT_SHL( x ) \
84 dct[x] = ( dct[x] * dequant_mf[i_mf][x] ) << i_qbits
86 #define DEQUANT_SHR( x ) \
87 dct[x] = ( dct[x] * dequant_mf[i_mf][x] + f ) >> (-i_qbits)
89 static void dequant_4x4( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
91 const int i_mf = i_qp%6;
92 const int i_qbits = i_qp/6 - 4;
96 for( int i = 0; i < 16; i++ )
101 const int f = 1 << (-i_qbits-1);
102 for( int i = 0; i < 16; i++ )
107 static void dequant_8x8( dctcoef dct[64], int dequant_mf[6][64], int i_qp )
109 const int i_mf = i_qp%6;
110 const int i_qbits = i_qp/6 - 6;
114 for( int i = 0; i < 64; i++ )
119 const int f = 1 << (-i_qbits-1);
120 for( int i = 0; i < 64; i++ )
125 static void dequant_4x4_dc( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
127 const int i_qbits = i_qp/6 - 6;
131 const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
132 for( int i = 0; i < 16; i++ )
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);
144 static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, udctcoef *offset, int size )
146 for( int i = 1; i < size; i++ )
149 int sign = level>>31;
150 level = (level+sign)^sign;
153 dct[i] = level<0 ? 0 : (level^sign)-sign;
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
166 const uint8_t x264_decimate_table4[16] =
168 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
170 const uint8_t x264_decimate_table8[64] =
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
178 static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
180 const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
184 while( idx >= 0 && dct[idx] == 0 )
190 if( (unsigned)(dct[idx--] + 1) > 2 )
194 while( idx >= 0 && dct[idx] == 0 )
199 i_score += ds_table[i_run];
205 static int x264_decimate_score15( dctcoef *dct )
207 return x264_decimate_score_internal( dct+1, 15 );
209 static int x264_decimate_score16( dctcoef *dct )
211 return x264_decimate_score_internal( dct, 16 );
213 static int x264_decimate_score64( dctcoef *dct )
215 return x264_decimate_score_internal( dct, 64 );
218 static int ALWAYS_INLINE x264_coeff_last_internal( dctcoef *l, int i_count )
220 int i_last = i_count-1;
221 while( i_last >= 0 && l[i_last] == 0 )
226 static int x264_coeff_last4( dctcoef *l )
228 return x264_coeff_last_internal( l, 4 );
230 static int x264_coeff_last15( dctcoef *l )
232 return x264_coeff_last_internal( l, 15 );
234 static int x264_coeff_last16( dctcoef *l )
236 return x264_coeff_last_internal( l, 16 );
238 static int x264_coeff_last64( dctcoef *l )
240 return x264_coeff_last_internal( l, 64 );
243 #define level_run(num)\
244 static int x264_coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
246 int i_last = runlevel->last = x264_coeff_last##num(dct);\
251 runlevel->level[i_total] = dct[i_last];\
252 while( --i_last >= 0 && dct[i_last] == 0 )\
254 runlevel->run[i_total++] = r;\
255 } while( i_last >= 0 );\
264 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
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;
271 pf->dequant_4x4 = dequant_4x4;
272 pf->dequant_4x4_dc = dequant_4x4_dc;
273 pf->dequant_8x8 = dequant_8x8;
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;
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;
290 if( cpu&X264_CPU_MMX )
292 pf->quant_4x4 = x264_quant_4x4_mmx;
293 pf->quant_8x8 = x264_quant_8x8_mmx;
295 if( cpu&X264_CPU_MMXEXT )
297 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
298 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
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 )
305 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
306 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
308 pf->decimate_score64 = x264_decimate_score64_mmxext;
309 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
310 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
311 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
312 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
313 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
314 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
316 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
317 if( cpu&X264_CPU_LZCNT )
318 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
320 if( cpu&X264_CPU_SSE2 )
322 pf->quant_4x4 = x264_quant_4x4_sse2;
323 pf->quant_8x8 = x264_quant_8x8_sse2;
324 pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
325 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
326 pf->dequant_4x4 = x264_dequant_4x4_sse2;
327 pf->dequant_8x8 = x264_dequant_8x8_sse2;
328 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
329 pf->denoise_dct = x264_denoise_dct_sse2;
330 pf->decimate_score15 = x264_decimate_score15_sse2;
331 pf->decimate_score16 = x264_decimate_score16_sse2;
332 pf->decimate_score64 = x264_decimate_score64_sse2;
333 if( cpu&X264_CPU_SLOW_CTZ )
335 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
336 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
338 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
339 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
340 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
341 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
342 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
343 if( cpu&X264_CPU_LZCNT )
345 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
346 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
347 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
348 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
349 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
350 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
353 if( cpu&X264_CPU_SSSE3 )
355 pf->quant_4x4 = x264_quant_4x4_ssse3;
356 pf->quant_8x8 = x264_quant_8x8_ssse3;
357 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
358 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
359 pf->denoise_dct = x264_denoise_dct_ssse3;
360 pf->decimate_score15 = x264_decimate_score15_ssse3;
361 pf->decimate_score16 = x264_decimate_score16_ssse3;
362 if( cpu&X264_CPU_SLOW_CTZ )
364 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
365 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
367 pf->decimate_score64 = x264_decimate_score64_ssse3;
369 if( cpu&X264_CPU_SSE4 )
371 pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
372 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
373 pf->quant_4x4 = x264_quant_4x4_sse4;
374 pf->quant_8x8 = x264_quant_8x8_sse4;
377 #else // !HIGH_BIT_DEPTH
379 if( cpu&X264_CPU_MMX )
382 pf->quant_4x4 = x264_quant_4x4_mmx;
383 pf->quant_8x8 = x264_quant_8x8_mmx;
384 pf->dequant_4x4 = x264_dequant_4x4_mmx;
385 pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
386 pf->dequant_8x8 = x264_dequant_8x8_mmx;
387 if( h->param.i_cqm_preset == X264_CQM_FLAT )
389 pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
390 pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
392 pf->denoise_dct = x264_denoise_dct_mmx;
396 if( cpu&X264_CPU_MMXEXT )
398 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
400 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
401 pf->decimate_score15 = x264_decimate_score15_mmxext;
402 pf->decimate_score16 = x264_decimate_score16_mmxext;
403 if( cpu&X264_CPU_SLOW_CTZ )
405 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
406 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
408 pf->decimate_score64 = x264_decimate_score64_mmxext;
409 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
410 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
411 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
412 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
413 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
415 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
416 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
417 if( cpu&X264_CPU_LZCNT )
419 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
420 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
424 if( cpu&X264_CPU_SSE2 )
426 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
427 pf->quant_4x4 = x264_quant_4x4_sse2;
428 pf->quant_8x8 = x264_quant_8x8_sse2;
429 pf->dequant_4x4 = x264_dequant_4x4_sse2;
430 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
431 pf->dequant_8x8 = x264_dequant_8x8_sse2;
432 if( h->param.i_cqm_preset == X264_CQM_FLAT )
434 pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
435 pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
437 pf->denoise_dct = x264_denoise_dct_sse2;
438 pf->decimate_score15 = x264_decimate_score15_sse2;
439 pf->decimate_score16 = x264_decimate_score16_sse2;
440 pf->decimate_score64 = x264_decimate_score64_sse2;
441 if( cpu&X264_CPU_SLOW_CTZ )
443 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
444 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
446 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
447 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
448 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
449 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
450 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
451 if( cpu&X264_CPU_LZCNT )
453 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
454 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
455 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
456 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
457 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
461 if( cpu&X264_CPU_SSSE3 )
463 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
464 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
465 pf->quant_4x4 = x264_quant_4x4_ssse3;
466 pf->quant_8x8 = x264_quant_8x8_ssse3;
467 pf->denoise_dct = x264_denoise_dct_ssse3;
468 pf->decimate_score15 = x264_decimate_score15_ssse3;
469 pf->decimate_score16 = x264_decimate_score16_ssse3;
470 if( cpu&X264_CPU_SLOW_CTZ )
472 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
473 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
475 pf->decimate_score64 = x264_decimate_score64_ssse3;
478 if( cpu&X264_CPU_SSE4 )
480 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
481 pf->quant_4x4 = x264_quant_4x4_sse4;
482 pf->quant_8x8 = x264_quant_8x8_sse4;
487 if( cpu&X264_CPU_ALTIVEC ) {
488 pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
489 pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
490 pf->quant_4x4 = x264_quant_4x4_altivec;
491 pf->quant_8x8 = x264_quant_8x8_altivec;
493 pf->dequant_4x4 = x264_dequant_4x4_altivec;
494 pf->dequant_8x8 = x264_dequant_8x8_altivec;
499 if( cpu&X264_CPU_ARMV6 )
500 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
502 if( cpu&X264_CPU_NEON )
504 pf->quant_2x2_dc = x264_quant_2x2_dc_neon;
505 pf->quant_4x4 = x264_quant_4x4_neon;
506 pf->quant_4x4_dc = x264_quant_4x4_dc_neon;
507 pf->quant_8x8 = x264_quant_8x8_neon;
508 pf->dequant_4x4 = x264_dequant_4x4_neon;
509 pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
510 pf->dequant_8x8 = x264_dequant_8x8_neon;
511 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
512 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
513 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
516 #endif // HIGH_BIT_DEPTH
517 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
518 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
519 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
520 pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];