1 /*****************************************************************************
2 * quant.c: quantization and level-run
3 *****************************************************************************
4 * Copyright (C) 2005-2011 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 ALWAYS_INLINE void idct_dequant_2x2_dconly( dctcoef out[4], dctcoef dct[4], int dequant_mf )
146 int d0 = dct[0] + dct[1];
147 int d1 = dct[2] + dct[3];
148 int d2 = dct[0] - dct[1];
149 int d3 = dct[2] - dct[3];
150 out[0] = (d0 + d1) * dequant_mf >> 5;
151 out[1] = (d0 - d1) * dequant_mf >> 5;
152 out[2] = (d2 + d3) * dequant_mf >> 5;
153 out[3] = (d2 - d3) * dequant_mf >> 5;
156 static ALWAYS_INLINE int idct_dequant_round_2x2_dc( dctcoef ref[4], dctcoef dct[4], int dequant_mf )
159 idct_dequant_2x2_dconly( out, dct, dequant_mf );
160 return ((ref[0] ^ (out[0]+32))
161 | (ref[1] ^ (out[1]+32))
162 | (ref[2] ^ (out[2]+32))
163 | (ref[3] ^ (out[3]+32))) >> 6;
166 static int optimize_chroma_dc( dctcoef dct[4], int dequant_mf )
168 /* dequant_mf = h->dequant4_mf[CQM_4IC + b_inter][i_qp%6][0] << i_qp/6, max 32*64 */
172 idct_dequant_2x2_dconly( dct_orig, dct, dequant_mf );
178 /* If the DC coefficients already round to zero, terminate early. */
179 if( !((dct_orig[0]|dct_orig[1]|dct_orig[2]|dct_orig[3])>>6) )
182 /* Start with the highest frequency coefficient... is this the best option? */
183 for( nz = 0, coeff = 3; coeff >= 0; coeff-- )
185 int level = dct[coeff];
186 int sign = level>>31 | 1; /* dct2x2[coeff] < 0 ? -1 : 1 */
190 dct[coeff] = level - sign;
191 if( idct_dequant_round_2x2_dc( dct_orig, dct, dequant_mf ) )
204 static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, udctcoef *offset, int size )
206 for( int i = 0; i < size; i++ )
209 int sign = level>>31;
210 level = (level+sign)^sign;
213 dct[i] = level<0 ? 0 : (level^sign)-sign;
218 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
219 * to 0 (low score means set it to null)
220 * Used in inter macroblock (luma and chroma)
221 * luma: for a 8x8 block: if score < 4 -> null
222 * for the complete mb: if score < 6 -> null
223 * chroma: for the complete mb: if score < 7 -> null
226 const uint8_t x264_decimate_table4[16] =
228 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
230 const uint8_t x264_decimate_table8[64] =
232 3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
233 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
234 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
235 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
238 static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
240 const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
244 while( idx >= 0 && dct[idx] == 0 )
250 if( (unsigned)(dct[idx--] + 1) > 2 )
254 while( idx >= 0 && dct[idx] == 0 )
259 i_score += ds_table[i_run];
265 static int x264_decimate_score15( dctcoef *dct )
267 return x264_decimate_score_internal( dct+1, 15 );
269 static int x264_decimate_score16( dctcoef *dct )
271 return x264_decimate_score_internal( dct, 16 );
273 static int x264_decimate_score64( dctcoef *dct )
275 return x264_decimate_score_internal( dct, 64 );
278 static int ALWAYS_INLINE x264_coeff_last_internal( dctcoef *l, int i_count )
280 int i_last = i_count-1;
281 while( i_last >= 0 && l[i_last] == 0 )
286 static int x264_coeff_last4( dctcoef *l )
288 return x264_coeff_last_internal( l, 4 );
290 static int x264_coeff_last15( dctcoef *l )
292 return x264_coeff_last_internal( l, 15 );
294 static int x264_coeff_last16( dctcoef *l )
296 return x264_coeff_last_internal( l, 16 );
298 static int x264_coeff_last64( dctcoef *l )
300 return x264_coeff_last_internal( l, 64 );
303 #define level_run(num)\
304 static int x264_coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
306 int i_last = runlevel->last = x264_coeff_last##num(dct);\
311 runlevel->level[i_total] = dct[i_last];\
312 while( --i_last >= 0 && dct[i_last] == 0 )\
314 runlevel->run[i_total++] = r;\
315 } while( i_last >= 0 );\
324 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
326 pf->quant_8x8 = quant_8x8;
327 pf->quant_4x4 = quant_4x4;
328 pf->quant_4x4_dc = quant_4x4_dc;
329 pf->quant_2x2_dc = quant_2x2_dc;
331 pf->dequant_4x4 = dequant_4x4;
332 pf->dequant_4x4_dc = dequant_4x4_dc;
333 pf->dequant_8x8 = dequant_8x8;
335 pf->optimize_chroma_dc = optimize_chroma_dc;
337 pf->denoise_dct = x264_denoise_dct;
338 pf->decimate_score15 = x264_decimate_score15;
339 pf->decimate_score16 = x264_decimate_score16;
340 pf->decimate_score64 = x264_decimate_score64;
342 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
343 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15;
344 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
345 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
346 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
347 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15;
348 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
352 if( cpu&X264_CPU_MMXEXT )
355 pf->denoise_dct = x264_denoise_dct_mmx;
356 pf->decimate_score15 = x264_decimate_score15_mmxext;
357 pf->decimate_score16 = x264_decimate_score16_mmxext;
358 if( cpu&X264_CPU_SLOW_CTZ )
360 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
361 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
363 pf->decimate_score64 = x264_decimate_score64_mmxext;
364 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
365 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
366 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
367 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
368 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
369 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
371 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
372 if( cpu&X264_CPU_LZCNT )
373 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
375 if( cpu&X264_CPU_SSE2 )
377 pf->quant_4x4 = x264_quant_4x4_sse2;
378 pf->quant_8x8 = x264_quant_8x8_sse2;
379 pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
380 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
381 pf->dequant_4x4 = x264_dequant_4x4_sse2;
382 pf->dequant_8x8 = x264_dequant_8x8_sse2;
383 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
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 if( cpu&X264_CPU_SLOW_CTZ )
390 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
391 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
393 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
394 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
395 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
396 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
397 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
398 if( cpu&X264_CPU_LZCNT )
400 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
401 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
402 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
403 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
404 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
405 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
408 if( cpu&X264_CPU_SSSE3 )
410 pf->quant_4x4 = x264_quant_4x4_ssse3;
411 pf->quant_8x8 = x264_quant_8x8_ssse3;
412 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
413 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
414 pf->denoise_dct = x264_denoise_dct_ssse3;
415 pf->decimate_score15 = x264_decimate_score15_ssse3;
416 pf->decimate_score16 = x264_decimate_score16_ssse3;
417 if( cpu&X264_CPU_SLOW_CTZ )
419 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
420 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
422 pf->decimate_score64 = x264_decimate_score64_ssse3;
424 if( cpu&X264_CPU_SSE4 )
426 pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
427 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
428 pf->quant_4x4 = x264_quant_4x4_sse4;
429 pf->quant_8x8 = x264_quant_8x8_sse4;
432 #else // !HIGH_BIT_DEPTH
434 if( cpu&X264_CPU_MMX )
437 pf->quant_4x4 = x264_quant_4x4_mmx;
438 pf->quant_8x8 = x264_quant_8x8_mmx;
439 pf->dequant_4x4 = x264_dequant_4x4_mmx;
440 pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
441 pf->dequant_8x8 = x264_dequant_8x8_mmx;
442 if( h->param.i_cqm_preset == X264_CQM_FLAT )
444 pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
445 pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
447 pf->denoise_dct = x264_denoise_dct_mmx;
451 if( cpu&X264_CPU_MMXEXT )
453 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
455 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
456 pf->decimate_score15 = x264_decimate_score15_mmxext;
457 pf->decimate_score16 = x264_decimate_score16_mmxext;
458 if( cpu&X264_CPU_SLOW_CTZ )
460 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
461 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
463 pf->decimate_score64 = x264_decimate_score64_mmxext;
464 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
465 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
466 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
467 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
468 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
470 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
471 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
472 if( cpu&X264_CPU_LZCNT )
474 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
475 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
479 if( cpu&X264_CPU_SSE2 )
481 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
482 pf->quant_4x4 = x264_quant_4x4_sse2;
483 pf->quant_8x8 = x264_quant_8x8_sse2;
484 pf->dequant_4x4 = x264_dequant_4x4_sse2;
485 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
486 pf->dequant_8x8 = x264_dequant_8x8_sse2;
487 if( h->param.i_cqm_preset == X264_CQM_FLAT )
489 pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
490 pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
492 pf->optimize_chroma_dc = x264_optimize_chroma_dc_sse2;
493 pf->denoise_dct = x264_denoise_dct_sse2;
494 pf->decimate_score15 = x264_decimate_score15_sse2;
495 pf->decimate_score16 = x264_decimate_score16_sse2;
496 pf->decimate_score64 = x264_decimate_score64_sse2;
497 if( cpu&X264_CPU_SLOW_CTZ )
499 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
500 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
502 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
503 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
504 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
505 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
506 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
507 if( cpu&X264_CPU_LZCNT )
509 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
510 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
511 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
512 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
513 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
517 if( cpu&X264_CPU_SSSE3 )
519 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
520 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
521 pf->quant_4x4 = x264_quant_4x4_ssse3;
522 pf->quant_8x8 = x264_quant_8x8_ssse3;
523 pf->optimize_chroma_dc = x264_optimize_chroma_dc_ssse3;
524 pf->denoise_dct = x264_denoise_dct_ssse3;
525 pf->decimate_score15 = x264_decimate_score15_ssse3;
526 pf->decimate_score16 = x264_decimate_score16_ssse3;
527 if( cpu&X264_CPU_SLOW_CTZ )
529 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
530 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
532 pf->decimate_score64 = x264_decimate_score64_ssse3;
535 if( cpu&X264_CPU_SSE4 )
537 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
538 pf->quant_4x4 = x264_quant_4x4_sse4;
539 pf->quant_8x8 = x264_quant_8x8_sse4;
540 pf->optimize_chroma_dc = x264_optimize_chroma_dc_sse4;
543 if( cpu&X264_CPU_AVX )
545 pf->dequant_4x4 = x264_dequant_4x4_avx;
546 pf->dequant_8x8 = x264_dequant_8x8_avx;
547 pf->dequant_4x4_dc = x264_dequant_4x4dc_avx;
548 pf->optimize_chroma_dc = x264_optimize_chroma_dc_avx;
549 pf->denoise_dct = x264_denoise_dct_avx;
554 if( cpu&X264_CPU_ALTIVEC ) {
555 pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
556 pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
557 pf->quant_4x4 = x264_quant_4x4_altivec;
558 pf->quant_8x8 = x264_quant_8x8_altivec;
560 pf->dequant_4x4 = x264_dequant_4x4_altivec;
561 pf->dequant_8x8 = x264_dequant_8x8_altivec;
566 if( cpu&X264_CPU_ARMV6 )
567 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
569 if( cpu&X264_CPU_NEON )
571 pf->quant_2x2_dc = x264_quant_2x2_dc_neon;
572 pf->quant_4x4 = x264_quant_4x4_neon;
573 pf->quant_4x4_dc = x264_quant_4x4_dc_neon;
574 pf->quant_8x8 = x264_quant_8x8_neon;
575 pf->dequant_4x4 = x264_dequant_4x4_neon;
576 pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
577 pf->dequant_8x8 = x264_dequant_8x8_neon;
578 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
579 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
580 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
583 #endif // HIGH_BIT_DEPTH
584 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
585 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
586 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
587 pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];