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_LUMA_AC] = x264_coeff_last15_mmxext;
310 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
311 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
312 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
313 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
315 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
316 if( cpu&X264_CPU_LZCNT )
317 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
319 if( cpu&X264_CPU_SSE2 )
321 pf->quant_4x4 = x264_quant_4x4_sse2;
322 pf->quant_8x8 = x264_quant_8x8_sse2;
323 pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
324 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
325 pf->denoise_dct = x264_denoise_dct_sse2;
326 pf->decimate_score15 = x264_decimate_score15_sse2;
327 pf->decimate_score16 = x264_decimate_score16_sse2;
328 pf->decimate_score64 = x264_decimate_score64_sse2;
329 if( cpu&X264_CPU_SLOW_CTZ )
331 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
332 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
334 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
335 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
336 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
337 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
338 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
339 if( cpu&X264_CPU_LZCNT )
341 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
342 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
343 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
344 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
345 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
348 if( cpu&X264_CPU_SSSE3 )
350 pf->quant_4x4 = x264_quant_4x4_ssse3;
351 pf->quant_8x8 = x264_quant_8x8_ssse3;
352 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
353 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
354 pf->denoise_dct = x264_denoise_dct_ssse3;
355 pf->decimate_score15 = x264_decimate_score15_ssse3;
356 pf->decimate_score16 = x264_decimate_score16_ssse3;
357 if( cpu&X264_CPU_SLOW_CTZ )
359 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
360 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
362 pf->decimate_score64 = x264_decimate_score64_ssse3;
364 if( cpu&X264_CPU_SSE4 )
366 pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
367 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
368 pf->quant_4x4 = x264_quant_4x4_sse4;
369 pf->quant_8x8 = x264_quant_8x8_sse4;
372 #else // !HIGH_BIT_DEPTH
374 if( cpu&X264_CPU_MMX )
377 pf->quant_4x4 = x264_quant_4x4_mmx;
378 pf->quant_8x8 = x264_quant_8x8_mmx;
379 pf->dequant_4x4 = x264_dequant_4x4_mmx;
380 pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
381 pf->dequant_8x8 = x264_dequant_8x8_mmx;
382 if( h->param.i_cqm_preset == X264_CQM_FLAT )
384 pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
385 pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
387 pf->denoise_dct = x264_denoise_dct_mmx;
391 if( cpu&X264_CPU_MMXEXT )
393 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
395 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
396 pf->decimate_score15 = x264_decimate_score15_mmxext;
397 pf->decimate_score16 = x264_decimate_score16_mmxext;
398 if( cpu&X264_CPU_SLOW_CTZ )
400 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
401 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
403 pf->decimate_score64 = x264_decimate_score64_mmxext;
404 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
405 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
406 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
407 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
408 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
410 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
411 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
412 if( cpu&X264_CPU_LZCNT )
414 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
415 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
419 if( cpu&X264_CPU_SSE2 )
421 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
422 pf->quant_4x4 = x264_quant_4x4_sse2;
423 pf->quant_8x8 = x264_quant_8x8_sse2;
424 pf->dequant_4x4 = x264_dequant_4x4_sse2;
425 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
426 pf->dequant_8x8 = x264_dequant_8x8_sse2;
427 if( h->param.i_cqm_preset == X264_CQM_FLAT )
429 pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
430 pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
432 pf->denoise_dct = x264_denoise_dct_sse2;
433 pf->decimate_score15 = x264_decimate_score15_sse2;
434 pf->decimate_score16 = x264_decimate_score16_sse2;
435 pf->decimate_score64 = x264_decimate_score64_sse2;
436 if( cpu&X264_CPU_SLOW_CTZ )
438 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
439 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
441 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
442 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
443 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
444 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
445 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
446 if( cpu&X264_CPU_LZCNT )
448 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
449 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
450 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
451 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
452 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
456 if( cpu&X264_CPU_SSSE3 )
458 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
459 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
460 pf->quant_4x4 = x264_quant_4x4_ssse3;
461 pf->quant_8x8 = x264_quant_8x8_ssse3;
462 pf->denoise_dct = x264_denoise_dct_ssse3;
463 pf->decimate_score15 = x264_decimate_score15_ssse3;
464 pf->decimate_score16 = x264_decimate_score16_ssse3;
465 if( cpu&X264_CPU_SLOW_CTZ )
467 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
468 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
470 pf->decimate_score64 = x264_decimate_score64_ssse3;
473 if( cpu&X264_CPU_SSE4 )
475 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
476 pf->quant_4x4 = x264_quant_4x4_sse4;
477 pf->quant_8x8 = x264_quant_8x8_sse4;
482 if( cpu&X264_CPU_ALTIVEC ) {
483 pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
484 pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
485 pf->quant_4x4 = x264_quant_4x4_altivec;
486 pf->quant_8x8 = x264_quant_8x8_altivec;
488 pf->dequant_4x4 = x264_dequant_4x4_altivec;
489 pf->dequant_8x8 = x264_dequant_8x8_altivec;
494 if( cpu&X264_CPU_ARMV6 )
495 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
497 if( cpu&X264_CPU_NEON )
499 pf->quant_2x2_dc = x264_quant_2x2_dc_neon;
500 pf->quant_4x4 = x264_quant_4x4_neon;
501 pf->quant_4x4_dc = x264_quant_4x4_dc_neon;
502 pf->quant_8x8 = x264_quant_8x8_neon;
503 pf->dequant_4x4 = x264_dequant_4x4_neon;
504 pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
505 pf->dequant_8x8 = x264_dequant_8x8_neon;
506 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
507 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
508 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
511 #endif // HIGH_BIT_DEPTH
512 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
513 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
514 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
515 pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];