1 /*****************************************************************************
2 * quant.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2005-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Christian Heine <sennindemokrit@gmx.net>
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.
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.
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 *****************************************************************************/
27 #include "x86/quant.h"
30 # include "ppc/quant.h"
33 # include "arm/quant.h"
36 #define QUANT_ONE( coef, mf, f ) \
39 (coef) = (f + (coef)) * (mf) >> 16; \
41 (coef) = - ((f - (coef)) * (mf) >> 16); \
45 static int quant_8x8( int16_t dct[64], uint16_t mf[64], uint16_t bias[64] )
48 for( int i = 0; i < 64; i++ )
49 QUANT_ONE( dct[i], mf[i], bias[i] );
53 static int quant_4x4( int16_t dct[16], uint16_t mf[16], uint16_t bias[16] )
56 for( int i = 0; i < 16; i++ )
57 QUANT_ONE( dct[i], mf[i], bias[i] );
61 static int quant_4x4_dc( int16_t dct[16], int mf, int bias )
64 for( int i = 0; i < 16; i++ )
65 QUANT_ONE( dct[i], mf, bias );
69 static int quant_2x2_dc( int16_t dct[4], int mf, int bias )
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 );
79 #define DEQUANT_SHL( x ) \
80 dct[x] = ( dct[x] * dequant_mf[i_mf][x] ) << i_qbits
82 #define DEQUANT_SHR( x ) \
83 dct[x] = ( dct[x] * dequant_mf[i_mf][x] + f ) >> (-i_qbits)
85 static void dequant_4x4( int16_t dct[16], int dequant_mf[6][16], int i_qp )
87 const int i_mf = i_qp%6;
88 const int i_qbits = i_qp/6 - 4;
92 for( int i = 0; i < 16; i++ )
97 const int f = 1 << (-i_qbits-1);
98 for( int i = 0; i < 16; i++ )
103 static void dequant_8x8( int16_t dct[64], int dequant_mf[6][64], int i_qp )
105 const int i_mf = i_qp%6;
106 const int i_qbits = i_qp/6 - 6;
110 for( int i = 0; i < 64; i++ )
115 const int f = 1 << (-i_qbits-1);
116 for( int i = 0; i < 64; i++ )
121 static void dequant_4x4_dc( int16_t dct[16], int dequant_mf[6][16], int i_qp )
123 const int i_qbits = i_qp/6 - 6;
127 const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
128 for( int i = 0; i < 16; i++ )
133 const int i_dmf = dequant_mf[i_qp%6][0];
134 const int f = 1 << (-i_qbits-1);
135 for( int i = 0; i < 16; i++ )
136 dct[i] = ( dct[i] * i_dmf + f ) >> (-i_qbits);
140 static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
142 for( int i = 1; i < size; i++ )
145 int sign = level>>15;
146 level = (level+sign)^sign;
149 dct[i] = level<0 ? 0 : (level^sign)-sign;
154 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
155 * to 0 (low score means set it to null)
156 * Used in inter macroblock (luma and chroma)
157 * luma: for a 8x8 block: if score < 4 -> null
158 * for the complete mb: if score < 6 -> null
159 * chroma: for the complete mb: if score < 7 -> null
162 const uint8_t x264_decimate_table4[16] =
164 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
166 const uint8_t x264_decimate_table8[64] =
168 3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
169 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
170 0,0,0,0,0,0,0,0,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
174 static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *dct, int i_max )
176 const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
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 )
183 if( idx >= 0 && dct[idx] == 0 )
189 if( (unsigned)(dct[idx--] + 1) > 2 )
193 while( idx >= 0 && dct[idx] == 0 )
198 i_score += ds_table[i_run];
204 static int x264_decimate_score15( int16_t *dct )
206 return x264_decimate_score_internal( dct+1, 15 );
208 static int x264_decimate_score16( int16_t *dct )
210 return x264_decimate_score_internal( dct, 16 );
212 static int x264_decimate_score64( int16_t *dct )
214 return x264_decimate_score_internal( dct, 64 );
217 static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
220 for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
221 if( M64( l+i_last-3 ) )
223 while( i_last >= 0 && l[i_last] == 0 )
228 static int x264_coeff_last4( int16_t *l )
230 return x264_coeff_last_internal( l, 4 );
232 static int x264_coeff_last15( int16_t *l )
234 return x264_coeff_last_internal( l, 15 );
236 static int x264_coeff_last16( int16_t *l )
238 return x264_coeff_last_internal( l, 16 );
240 static int x264_coeff_last64( int16_t *l )
242 return x264_coeff_last_internal( l, 64 );
245 #define level_run(num)\
246 static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
248 int i_last = runlevel->last = x264_coeff_last##num(dct);\
253 runlevel->level[i_total] = dct[i_last];\
254 while( --i_last >= 0 && dct[i_last] == 0 )\
256 runlevel->run[i_total++] = r;\
257 } while( i_last >= 0 );\
266 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
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;
273 pf->dequant_4x4 = dequant_4x4;
274 pf->dequant_4x4_dc = dequant_4x4_dc;
275 pf->dequant_8x8 = dequant_8x8;
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;
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;
291 if( cpu&X264_CPU_MMX )
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 )
301 pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
302 pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
304 pf->denoise_dct = x264_denoise_dct_mmx;
308 if( cpu&X264_CPU_MMXEXT )
310 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
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 if( cpu&X264_CPU_SLOW_CTZ )
317 pf->decimate_score15 = x264_decimate_score15_mmxext_slowctz;
318 pf->decimate_score16 = x264_decimate_score16_mmxext_slowctz;
320 pf->decimate_score64 = x264_decimate_score64_mmxext;
321 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
322 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
323 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
324 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
325 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
327 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
328 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
329 if( cpu&X264_CPU_LZCNT )
331 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
332 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
336 if( cpu&X264_CPU_SSE2 )
338 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
339 pf->quant_4x4 = x264_quant_4x4_sse2;
340 pf->quant_8x8 = x264_quant_8x8_sse2;
341 pf->dequant_4x4 = x264_dequant_4x4_sse2;
342 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
343 pf->dequant_8x8 = x264_dequant_8x8_sse2;
344 if( h->param.i_cqm_preset == X264_CQM_FLAT )
346 pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
347 pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
349 pf->denoise_dct = x264_denoise_dct_sse2;
350 pf->decimate_score15 = x264_decimate_score15_sse2;
351 pf->decimate_score16 = x264_decimate_score16_sse2;
352 pf->decimate_score64 = x264_decimate_score64_sse2;
353 if( cpu&X264_CPU_SLOW_CTZ )
355 pf->decimate_score15 = x264_decimate_score15_sse2_slowctz;
356 pf->decimate_score16 = x264_decimate_score16_sse2_slowctz;
358 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
359 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
360 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
361 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
362 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
363 if( cpu&X264_CPU_LZCNT )
365 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
366 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
367 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
368 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
369 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
373 if( cpu&X264_CPU_SSSE3 )
375 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
376 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
377 pf->quant_4x4 = x264_quant_4x4_ssse3;
378 pf->quant_8x8 = x264_quant_8x8_ssse3;
379 pf->denoise_dct = x264_denoise_dct_ssse3;
380 pf->decimate_score15 = x264_decimate_score15_ssse3;
381 pf->decimate_score16 = x264_decimate_score16_ssse3;
382 if( cpu&X264_CPU_SLOW_CTZ )
384 pf->decimate_score15 = x264_decimate_score15_ssse3_slowctz;
385 pf->decimate_score16 = x264_decimate_score16_ssse3_slowctz;
387 pf->decimate_score64 = x264_decimate_score64_ssse3;
390 if( cpu&X264_CPU_SSE4 )
392 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
393 pf->quant_4x4 = x264_quant_4x4_sse4;
394 pf->quant_8x8 = x264_quant_8x8_sse4;
399 if( cpu&X264_CPU_ALTIVEC ) {
400 pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
401 pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
402 pf->quant_4x4 = x264_quant_4x4_altivec;
403 pf->quant_8x8 = x264_quant_8x8_altivec;
405 pf->dequant_4x4 = x264_dequant_4x4_altivec;
406 pf->dequant_8x8 = x264_dequant_8x8_altivec;
411 if( cpu&X264_CPU_ARMV6 )
412 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_arm;
414 if( cpu&X264_CPU_NEON )
416 pf->quant_2x2_dc = x264_quant_2x2_dc_neon;
417 pf->quant_4x4 = x264_quant_4x4_neon;
418 pf->quant_4x4_dc = x264_quant_4x4_dc_neon;
419 pf->quant_8x8 = x264_quant_8x8_neon;
420 pf->dequant_4x4 = x264_dequant_4x4_neon;
421 pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
422 pf->dequant_8x8 = x264_dequant_8x8_neon;
423 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
424 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
425 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
428 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
429 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
430 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
431 pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];