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 #define QUANT_ONE( coef, mf, f ) \
36 (coef) = (f + (coef)) * (mf) >> 16; \
38 (coef) = - ((f - (coef)) * (mf) >> 16); \
42 static int quant_8x8( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
45 for( i = 0; i < 64; i++ )
46 QUANT_ONE( dct[0][i], mf[i], bias[i] );
50 static int quant_4x4( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
53 for( i = 0; i < 16; i++ )
54 QUANT_ONE( dct[0][i], mf[i], bias[i] );
58 static int quant_4x4_dc( int16_t dct[4][4], int mf, int bias )
61 for( i = 0; i < 16; i++ )
62 QUANT_ONE( dct[0][i], mf, bias );
66 static int quant_2x2_dc( int16_t dct[2][2], int mf, int bias )
69 QUANT_ONE( dct[0][0], mf, bias );
70 QUANT_ONE( dct[0][1], mf, bias );
71 QUANT_ONE( dct[0][2], mf, bias );
72 QUANT_ONE( dct[0][3], mf, bias );
76 #define DEQUANT_SHL( x ) \
77 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] ) << i_qbits
79 #define DEQUANT_SHR( x ) \
80 dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][y][x] + f ) >> (-i_qbits)
82 static void dequant_4x4( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
84 const int i_mf = i_qp%6;
85 const int i_qbits = i_qp/6 - 4;
90 for( y = 0; y < 4; y++ )
100 const int f = 1 << (-i_qbits-1);
101 for( y = 0; y < 4; y++ )
111 static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
113 const int i_mf = i_qp%6;
114 const int i_qbits = i_qp/6 - 6;
119 for( y = 0; y < 8; y++ )
133 const int f = 1 << (-i_qbits-1);
134 for( y = 0; y < 8; y++ )
148 static void dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
150 const int i_qbits = i_qp/6 - 6;
155 const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
157 for( y = 0; y < 4; y++ )
167 const int i_dmf = dequant_mf[i_qp%6][0][0];
168 const int f = 1 << (-i_qbits-1);
170 for( y = 0; y < 4; y++ )
172 dct[y][0] = ( dct[y][0] * i_dmf + f ) >> (-i_qbits);
173 dct[y][1] = ( dct[y][1] * i_dmf + f ) >> (-i_qbits);
174 dct[y][2] = ( dct[y][2] * i_dmf + f ) >> (-i_qbits);
175 dct[y][3] = ( dct[y][3] * i_dmf + f ) >> (-i_qbits);
180 static void x264_denoise_dct( int16_t *dct, uint32_t *sum, uint16_t *offset, int size )
183 for( i=1; i<size; i++ )
186 int sign = level>>15;
187 level = (level+sign)^sign;
190 dct[i] = level<0 ? 0 : (level^sign)-sign;
195 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
196 * to 0 (low score means set it to null)
197 * Used in inter macroblock (luma and chroma)
198 * luma: for a 8x8 block: if score < 4 -> null
199 * for the complete mb: if score < 6 -> null
200 * chroma: for the complete mb: if score < 7 -> null
203 const uint8_t x264_decimate_table4[16] = {
204 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
205 const uint8_t x264_decimate_table8[64] = {
206 3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
207 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
208 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
209 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
211 static int ALWAYS_INLINE x264_decimate_score_internal( int16_t *dct, int i_max )
213 const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
217 /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned. idx>=0 instead of 1 works correctly for the same reason */
218 while( idx >= 0 && *(uint32_t*)&dct[idx-1] == 0 )
220 if( idx >= 0 && dct[idx] == 0 )
226 if( (unsigned)(dct[idx--] + 1) > 2 )
230 while( idx >= 0 && dct[idx] == 0 )
235 i_score += ds_table[i_run];
241 static int x264_decimate_score15( int16_t *dct )
243 return x264_decimate_score_internal( dct+1, 15 );
245 static int x264_decimate_score16( int16_t *dct )
247 return x264_decimate_score_internal( dct, 16 );
249 static int x264_decimate_score64( int16_t *dct )
251 return x264_decimate_score_internal( dct, 64 );
254 static int ALWAYS_INLINE x264_coeff_last_internal( int16_t *l, int i_count )
257 for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
258 if( *(uint64_t*)(l+i_last-3) )
260 while( i_last >= 0 && l[i_last] == 0 )
265 static int x264_coeff_last4( int16_t *l )
267 return x264_coeff_last_internal( l, 4 );
269 static int x264_coeff_last15( int16_t *l )
271 return x264_coeff_last_internal( l, 15 );
273 static int x264_coeff_last16( int16_t *l )
275 return x264_coeff_last_internal( l, 16 );
277 static int x264_coeff_last64( int16_t *l )
279 return x264_coeff_last_internal( l, 64 );
282 #define level_run(num)\
283 static int x264_coeff_level_run##num( int16_t *dct, x264_run_level_t *runlevel )\
285 int i_last = runlevel->last = x264_coeff_last##num(dct);\
290 runlevel->level[i_total] = dct[i_last];\
291 while( --i_last >= 0 && dct[i_last] == 0 )\
293 runlevel->run[i_total++] = r;\
294 } while( i_last >= 0 );\
303 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
305 pf->quant_8x8 = quant_8x8;
306 pf->quant_4x4 = quant_4x4;
307 pf->quant_4x4_dc = quant_4x4_dc;
308 pf->quant_2x2_dc = quant_2x2_dc;
310 pf->dequant_4x4 = dequant_4x4;
311 pf->dequant_4x4_dc = dequant_4x4_dc;
312 pf->dequant_8x8 = dequant_8x8;
314 pf->denoise_dct = x264_denoise_dct;
315 pf->decimate_score15 = x264_decimate_score15;
316 pf->decimate_score16 = x264_decimate_score16;
317 pf->decimate_score64 = x264_decimate_score64;
319 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4;
320 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15;
321 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
322 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
323 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4;
324 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15;
325 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
328 if( cpu&X264_CPU_MMX )
331 pf->quant_4x4 = x264_quant_4x4_mmx;
332 pf->quant_8x8 = x264_quant_8x8_mmx;
333 pf->dequant_4x4 = x264_dequant_4x4_mmx;
334 pf->dequant_4x4_dc = x264_dequant_4x4dc_mmxext;
335 pf->dequant_8x8 = x264_dequant_8x8_mmx;
336 if( h->param.i_cqm_preset == X264_CQM_FLAT )
338 pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
339 pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
341 pf->denoise_dct = x264_denoise_dct_mmx;
345 if( cpu&X264_CPU_MMXEXT )
347 pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext;
349 pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext;
350 pf->decimate_score15 = x264_decimate_score15_mmxext;
351 pf->decimate_score16 = x264_decimate_score16_mmxext;
352 pf->decimate_score64 = x264_decimate_score64_mmxext;
353 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_mmxext;
354 pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmxext;
355 pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmxext;
356 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_mmxext;
357 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmxext;
359 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext;
360 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext;
361 if( cpu&X264_CPU_LZCNT )
363 pf->coeff_last[DCT_CHROMA_DC] = x264_coeff_last4_mmxext_lzcnt;
364 pf->coeff_level_run[DCT_CHROMA_DC] = x264_coeff_level_run4_mmxext_lzcnt;
368 if( cpu&X264_CPU_SSE2 )
370 pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
371 pf->quant_4x4 = x264_quant_4x4_sse2;
372 pf->quant_8x8 = x264_quant_8x8_sse2;
373 pf->dequant_4x4 = x264_dequant_4x4_sse2;
374 pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
375 pf->dequant_8x8 = x264_dequant_8x8_sse2;
376 if( h->param.i_cqm_preset == X264_CQM_FLAT )
378 pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
379 pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
381 pf->denoise_dct = x264_denoise_dct_sse2;
382 pf->decimate_score15 = x264_decimate_score15_sse2;
383 pf->decimate_score16 = x264_decimate_score16_sse2;
384 pf->decimate_score64 = x264_decimate_score64_sse2;
385 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
386 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
387 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
388 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
389 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
390 if( cpu&X264_CPU_LZCNT )
392 pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
393 pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
394 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
395 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
396 pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
400 if( cpu&X264_CPU_SSSE3 )
402 pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
403 pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
404 pf->quant_4x4 = x264_quant_4x4_ssse3;
405 pf->quant_8x8 = x264_quant_8x8_ssse3;
406 pf->denoise_dct = x264_denoise_dct_ssse3;
407 pf->decimate_score15 = x264_decimate_score15_ssse3;
408 pf->decimate_score16 = x264_decimate_score16_ssse3;
409 pf->decimate_score64 = x264_decimate_score64_ssse3;
412 if( cpu&X264_CPU_SSE4 )
414 pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
415 pf->quant_4x4 = x264_quant_4x4_sse4;
416 pf->quant_8x8 = x264_quant_8x8_sse4;
421 if( cpu&X264_CPU_ALTIVEC ) {
422 pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
423 pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
424 pf->quant_4x4 = x264_quant_4x4_altivec;
425 pf->quant_8x8 = x264_quant_8x8_altivec;
427 pf->dequant_4x4 = x264_dequant_4x4_altivec;
428 pf->dequant_8x8 = x264_dequant_8x8_altivec;
431 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
432 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
433 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
434 pf->coeff_level_run[DCT_CHROMA_AC] = pf->coeff_level_run[ DCT_LUMA_AC];