1 /*****************************************************************************
2  * quant.c: quantization and level-run
3  *****************************************************************************
4  * Copyright (C) 2005-2014 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Fiona Glaser <fiona@x264.com>
8  *          Christian Heine <sennindemokrit@gmx.net>
9  *          Henrik Gramner <henrik@gramner.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
24  *
25  * This program is also available under a commercial proprietary license.
26  * For more information, contact us at licensing@x264.com.
27  *****************************************************************************/
28 
29 #include "common.h"
30 
31 #if HAVE_MMX
32 #include "x86/quant.h"
33 #endif
34 #if ARCH_PPC
35 #   include "ppc/quant.h"
36 #endif
37 #if ARCH_ARM
38 #   include "arm/quant.h"
39 #endif
40 #if ARCH_AARCH64
41 #   include "aarch64/quant.h"
42 #endif
43 
44 #define QUANT_ONE( coef, mf, f ) \
45 { \
46     if( (coef) > 0 ) \
47         (coef) = (f + (coef)) * (mf) >> 16; \
48     else \
49         (coef) = - ((f - (coef)) * (mf) >> 16); \
50     nz |= (coef); \
51 }
52 
quant_8x8(dctcoef dct[64],udctcoef mf[64],udctcoef bias[64])53 static int quant_8x8( dctcoef dct[64], udctcoef mf[64], udctcoef bias[64] )
54 {
55     int nz = 0;
56 	int i;
57 
58 	for( i = 0; i < 64; i++ )
59         QUANT_ONE( dct[i], mf[i], bias[i] );
60     return !!nz;
61 }
62 
quant_4x4(dctcoef dct[16],udctcoef mf[16],udctcoef bias[16])63 static int quant_4x4( dctcoef dct[16], udctcoef mf[16], udctcoef bias[16] )
64 {
65     int nz = 0;
66 	int i;
67 
68 	for( i = 0; i < 16; i++ )
69         QUANT_ONE( dct[i], mf[i], bias[i] );
70     return !!nz;
71 }
72 
quant_4x4x4(dctcoef dct[4][16],udctcoef mf[16],udctcoef bias[16])73 static int quant_4x4x4( dctcoef dct[4][16], udctcoef mf[16], udctcoef bias[16] )
74 {
75     int nza = 0;
76 	int j;
77 
78 	for( j = 0; j < 4; j++ )
79     {
80         int nz = 0;
81 		int i;
82 
83 		for( i = 0; i < 16; i++ )
84             QUANT_ONE( dct[j][i], mf[i], bias[i] );
85         nza |= (!!nz)<<j;
86     }
87     return nza;
88 }
89 
quant_4x4_dc(dctcoef dct[16],int mf,int bias)90 static int quant_4x4_dc( dctcoef dct[16], int mf, int bias )
91 {
92     int nz = 0;
93     int i;
94 
95 	for( i = 0; i < 16; i++ )
96         QUANT_ONE( dct[i], mf, bias );
97     return !!nz;
98 }
99 
quant_2x2_dc(dctcoef dct[4],int mf,int bias)100 static int quant_2x2_dc( dctcoef dct[4], int mf, int bias )
101 {
102     int nz = 0;
103     QUANT_ONE( dct[0], mf, bias );
104     QUANT_ONE( dct[1], mf, bias );
105     QUANT_ONE( dct[2], mf, bias );
106     QUANT_ONE( dct[3], mf, bias );
107     return !!nz;
108 }
109 
110 #define DEQUANT_SHL( x ) \
111     dct[x] = ( dct[x] * dequant_mf[i_mf][x] ) << i_qbits
112 
113 #define DEQUANT_SHR( x ) \
114     dct[x] = ( dct[x] * dequant_mf[i_mf][x] + f ) >> (-i_qbits)
115 
dequant_4x4(dctcoef dct[16],int dequant_mf[6][16],int i_qp)116 static void dequant_4x4( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
117 {
118     const int i_mf = i_qp%6;
119     const int i_qbits = i_qp/6 - 4;
120 
121     if( i_qbits >= 0 )
122     {
123 		int i;
124 		for( i = 0; i < 16; i++ )
125             DEQUANT_SHL( i );
126     }
127     else
128     {
129         const int f = 1 << (-i_qbits-1);
130 		int i;
131 
132 		for( i = 0; i < 16; i++ )
133             DEQUANT_SHR( i );
134     }
135 }
136 
dequant_8x8(dctcoef dct[64],int dequant_mf[6][64],int i_qp)137 static void dequant_8x8( dctcoef dct[64], int dequant_mf[6][64], int i_qp )
138 {
139     const int i_mf = i_qp%6;
140     const int i_qbits = i_qp/6 - 6;
141 
142     if( i_qbits >= 0 )
143     {
144 		int i;
145 
146 		for( i = 0; i < 64; i++ )
147             DEQUANT_SHL( i );
148     }
149     else
150     {
151         const int f = 1 << (-i_qbits-1);
152 		int i;
153 
154 		for( i = 0; i < 64; i++ )
155             DEQUANT_SHR( i );
156     }
157 }
158 
dequant_4x4_dc(dctcoef dct[16],int dequant_mf[6][16],int i_qp)159 static void dequant_4x4_dc( dctcoef dct[16], int dequant_mf[6][16], int i_qp )
160 {
161     const int i_qbits = i_qp/6 - 6;
162 
163     if( i_qbits >= 0 )
164     {
165         const int i_dmf = dequant_mf[i_qp%6][0] << i_qbits;
166 		int i;
167 
168 		for( i = 0; i < 16; i++ )
169             dct[i] *= i_dmf;
170     }
171     else
172     {
173         const int i_dmf = dequant_mf[i_qp%6][0];
174         const int f = 1 << (-i_qbits-1);
175 		int i;
176 
177 		for( i = 0; i < 16; i++ )
178             dct[i] = ( dct[i] * i_dmf + f ) >> (-i_qbits);
179     }
180 }
181 
182 #define IDCT_DEQUANT_2X4_START \
183     int a0 = dct[0] + dct[1]; \
184     int a1 = dct[2] + dct[3]; \
185     int a2 = dct[4] + dct[5]; \
186     int a3 = dct[6] + dct[7]; \
187     int a4 = dct[0] - dct[1]; \
188     int a5 = dct[2] - dct[3]; \
189     int a6 = dct[4] - dct[5]; \
190     int a7 = dct[6] - dct[7]; \
191     int b0 = a0 + a1; \
192     int b1 = a2 + a3; \
193     int b2 = a4 + a5; \
194     int b3 = a6 + a7; \
195     int b4 = a0 - a1; \
196     int b5 = a2 - a3; \
197     int b6 = a4 - a5; \
198     int b7 = a6 - a7;
199 
idct_dequant_2x4_dc(dctcoef dct[8],dctcoef dct4x4[8][16],int dequant_mf[6][16],int i_qp)200 static void idct_dequant_2x4_dc( dctcoef dct[8], dctcoef dct4x4[8][16], int dequant_mf[6][16], int i_qp )
201 {
202     IDCT_DEQUANT_2X4_START
203     int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
204     dct4x4[0][0] = ((b0 + b1) * dmf + 32) >> 6;
205     dct4x4[1][0] = ((b2 + b3) * dmf + 32) >> 6;
206     dct4x4[2][0] = ((b0 - b1) * dmf + 32) >> 6;
207     dct4x4[3][0] = ((b2 - b3) * dmf + 32) >> 6;
208     dct4x4[4][0] = ((b4 - b5) * dmf + 32) >> 6;
209     dct4x4[5][0] = ((b6 - b7) * dmf + 32) >> 6;
210     dct4x4[6][0] = ((b4 + b5) * dmf + 32) >> 6;
211     dct4x4[7][0] = ((b6 + b7) * dmf + 32) >> 6;
212 }
213 
idct_dequant_2x4_dconly(dctcoef dct[8],int dequant_mf[6][16],int i_qp)214 static void idct_dequant_2x4_dconly( dctcoef dct[8], int dequant_mf[6][16], int i_qp )
215 {
216     IDCT_DEQUANT_2X4_START
217     int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
218     dct[0] = ((b0 + b1) * dmf + 32) >> 6;
219     dct[1] = ((b2 + b3) * dmf + 32) >> 6;
220     dct[2] = ((b0 - b1) * dmf + 32) >> 6;
221     dct[3] = ((b2 - b3) * dmf + 32) >> 6;
222     dct[4] = ((b4 - b5) * dmf + 32) >> 6;
223     dct[5] = ((b6 - b7) * dmf + 32) >> 6;
224     dct[6] = ((b4 + b5) * dmf + 32) >> 6;
225     dct[7] = ((b6 + b7) * dmf + 32) >> 6;
226 }
227 
optimize_chroma_idct_dequant_2x4(dctcoef out[8],dctcoef dct[8],int dmf)228 static ALWAYS_INLINE void optimize_chroma_idct_dequant_2x4( dctcoef out[8], dctcoef dct[8], int dmf )
229 {
230     IDCT_DEQUANT_2X4_START
231     out[0] = ((b0 + b1) * dmf + 2080) >> 6; /* 2080 = 32 + (32<<6) */
232     out[1] = ((b2 + b3) * dmf + 2080) >> 6;
233     out[2] = ((b0 - b1) * dmf + 2080) >> 6;
234     out[3] = ((b2 - b3) * dmf + 2080) >> 6;
235     out[4] = ((b4 - b5) * dmf + 2080) >> 6;
236     out[5] = ((b6 - b7) * dmf + 2080) >> 6;
237     out[6] = ((b4 + b5) * dmf + 2080) >> 6;
238     out[7] = ((b6 + b7) * dmf + 2080) >> 6;
239 }
240 #undef IDCT_DEQUANT_2X4_START
241 
optimize_chroma_idct_dequant_2x2(dctcoef out[4],dctcoef dct[4],int dmf)242 static ALWAYS_INLINE void optimize_chroma_idct_dequant_2x2( dctcoef out[4], dctcoef dct[4], int dmf )
243 {
244     int d0 = dct[0] + dct[1];
245     int d1 = dct[2] + dct[3];
246     int d2 = dct[0] - dct[1];
247     int d3 = dct[2] - dct[3];
248     out[0] = ((d0 + d1) * dmf >> 5) + 32;
249     out[1] = ((d0 - d1) * dmf >> 5) + 32;
250     out[2] = ((d2 + d3) * dmf >> 5) + 32;
251     out[3] = ((d2 - d3) * dmf >> 5) + 32;
252 }
253 
optimize_chroma_round(dctcoef * ref,dctcoef * dct,int dequant_mf,int chroma422)254 static ALWAYS_INLINE int optimize_chroma_round( dctcoef *ref, dctcoef *dct, int dequant_mf, int chroma422 )
255 {
256     dctcoef out[8];
257     int sum;
258 	int i;
259 
260     if( chroma422 )
261         optimize_chroma_idct_dequant_2x4( out, dct, dequant_mf );
262     else
263         optimize_chroma_idct_dequant_2x2( out, dct, dequant_mf );
264 
265     sum = 0;
266     for( i = 0; i < (chroma422?8:4); i++ )
267         sum |= ref[i] ^ out[i];
268     return sum >> 6;
269 }
270 
optimize_chroma_dc_internal(dctcoef * dct,int dequant_mf,int chroma422)271 static ALWAYS_INLINE int optimize_chroma_dc_internal( dctcoef *dct, int dequant_mf, int chroma422 )
272 {
273     /* dequant_mf = h->dequant4_mf[CQM_4IC + b_inter][i_qp%6][0] << i_qp/6, max 32*64 */
274     dctcoef dct_orig[8];
275     int coeff, nz;
276     int sum;
277 	int i;
278 
279     if( chroma422 )
280         optimize_chroma_idct_dequant_2x4( dct_orig, dct, dequant_mf );
281     else
282         optimize_chroma_idct_dequant_2x2( dct_orig, dct, dequant_mf );
283 
284     /* If the DC coefficients already round to zero, terminate early. */
285     sum = 0;
286     for( i = 0; i < (chroma422?8:4); i++ )
287         sum |= dct_orig[i];
288     if( !(sum >> 6) )
289         return 0;
290 
291     /* Start with the highest frequency coefficient... is this the best option? */
292     for( nz = 0, coeff = (chroma422?7:3); coeff >= 0; coeff-- )
293     {
294         int level = dct[coeff];
295         int sign = level>>31 | 1; /* dct[coeff] < 0 ? -1 : 1 */
296 
297         while( level )
298         {
299             dct[coeff] = level - sign;
300             if( optimize_chroma_round( dct_orig, dct, dequant_mf, chroma422 ) )
301             {
302                 nz = 1;
303                 dct[coeff] = level;
304                 break;
305             }
306             level -= sign;
307         }
308     }
309 
310     return nz;
311 }
312 
optimize_chroma_2x2_dc(dctcoef dct[4],int dequant_mf)313 static int optimize_chroma_2x2_dc( dctcoef dct[4], int dequant_mf )
314 {
315     return optimize_chroma_dc_internal( dct, dequant_mf, 0 );
316 }
317 
optimize_chroma_2x4_dc(dctcoef dct[8],int dequant_mf)318 static int optimize_chroma_2x4_dc( dctcoef dct[8], int dequant_mf )
319 {
320     return optimize_chroma_dc_internal( dct, dequant_mf, 1 );
321 }
322 
x264_denoise_dct(dctcoef * dct,uint32_t * sum,udctcoef * offset,int size)323 static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, udctcoef *offset, int size )
324 {
325 	int i;
326 
327 	for( i = 0; i < size; i++ )
328     {
329         int level = dct[i];
330         int sign = level>>31;
331         level = (level+sign)^sign;
332         sum[i] += level;
333         level -= offset[i];
334         dct[i] = level<0 ? 0 : (level^sign)-sign;
335     }
336 }
337 
338 /* (ref: JVT-B118)
339  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
340  * to 0 (low score means set it to null)
341  * Used in inter macroblock (luma and chroma)
342  *  luma: for a 8x8 block: if score < 4 -> null
343  *        for the complete mb: if score < 6 -> null
344  *  chroma: for the complete mb: if score < 7 -> null
345  */
346 
347 const uint8_t x264_decimate_table4[16] =
348 {
349     3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0
350 };
351 const uint8_t x264_decimate_table8[64] =
352 {
353     3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
354     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
355     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
356     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
357 };
358 
x264_decimate_score_internal(dctcoef * dct,int i_max)359 static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
360 {
361     const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
362     int i_score = 0;
363     int idx = i_max - 1;
364 
365     while( idx >= 0 && dct[idx] == 0 )
366         idx--;
367     while( idx >= 0 )
368     {
369         int i_run;
370 
371         if( (unsigned)(dct[idx--] + 1) > 2 )
372             return 9;
373 
374         i_run = 0;
375         while( idx >= 0 && dct[idx] == 0 )
376         {
377             idx--;
378             i_run++;
379         }
380         i_score += ds_table[i_run];
381     }
382 
383     return i_score;
384 }
385 
x264_decimate_score15(dctcoef * dct)386 static int x264_decimate_score15( dctcoef *dct )
387 {
388     return x264_decimate_score_internal( dct+1, 15 );
389 }
x264_decimate_score16(dctcoef * dct)390 static int x264_decimate_score16( dctcoef *dct )
391 {
392     return x264_decimate_score_internal( dct, 16 );
393 }
x264_decimate_score64(dctcoef * dct)394 static int x264_decimate_score64( dctcoef *dct )
395 {
396     return x264_decimate_score_internal( dct, 64 );
397 }
398 
399 #define last(num)\
400 static int x264_coeff_last##num( dctcoef *l )\
401 {\
402     int i_last = num-1;\
403     while( i_last >= 0 && l[i_last] == 0 )\
404         i_last--;\
405     return i_last;\
406 }
407 
408 last(4)
409 last(8)
410 last(15)
411 last(16)
412 last(64)
413 
414 #define level_run(num)\
415 static int x264_coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
416 {\
417     int i_last = runlevel->last = x264_coeff_last##num(dct);\
418     int i_total = 0;\
419     int mask = 0;\
420     do\
421     {\
422         runlevel->level[i_total++] = dct[i_last];\
423         mask |= 1 << (i_last);\
424         while( --i_last >= 0 && dct[i_last] == 0 );\
425     } while( i_last >= 0 );\
426     runlevel->mask = mask;\
427     return i_total;\
428 }
429 
430 level_run(4)
431 level_run(8)
432 level_run(15)
433 level_run(16)
434 
435 #if ARCH_X86_64
436 #define INIT_TRELLIS(cpu)\
437     pf->trellis_cabac_4x4 = x264_trellis_cabac_4x4_##cpu;\
438     pf->trellis_cabac_8x8 = x264_trellis_cabac_8x8_##cpu;\
439     pf->trellis_cabac_4x4_psy = x264_trellis_cabac_4x4_psy_##cpu;\
440     pf->trellis_cabac_8x8_psy = x264_trellis_cabac_8x8_psy_##cpu;\
441     pf->trellis_cabac_dc = x264_trellis_cabac_dc_##cpu;\
442     pf->trellis_cabac_chroma_422_dc = x264_trellis_cabac_chroma_422_dc_##cpu;
443 #else
444 #  define INIT_TRELLIS(...)
445 #endif
446 
x264_quant_init(x264_t * h,int cpu,x264_quant_function_t * pf)447 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
448 {
449     pf->quant_8x8 = quant_8x8;
450     pf->quant_4x4 = quant_4x4;
451     pf->quant_4x4x4 = quant_4x4x4;
452     pf->quant_4x4_dc = quant_4x4_dc;
453     pf->quant_2x2_dc = quant_2x2_dc;
454 
455     pf->dequant_4x4 = dequant_4x4;
456     pf->dequant_4x4_dc = dequant_4x4_dc;
457     pf->dequant_8x8 = dequant_8x8;
458 
459     pf->idct_dequant_2x4_dc = idct_dequant_2x4_dc;
460     pf->idct_dequant_2x4_dconly = idct_dequant_2x4_dconly;
461 
462     pf->optimize_chroma_2x2_dc = optimize_chroma_2x2_dc;
463     pf->optimize_chroma_2x4_dc = optimize_chroma_2x4_dc;
464 
465     pf->denoise_dct = x264_denoise_dct;
466     pf->decimate_score15 = x264_decimate_score15;
467     pf->decimate_score16 = x264_decimate_score16;
468     pf->decimate_score64 = x264_decimate_score64;
469 
470     pf->coeff_last4 = x264_coeff_last4;
471     pf->coeff_last8 = x264_coeff_last8;
472     pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15;
473     pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16;
474     pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64;
475     pf->coeff_level_run4 = x264_coeff_level_run4;
476     pf->coeff_level_run8 = x264_coeff_level_run8;
477     pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15;
478     pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
479 
480 #if HIGH_BIT_DEPTH
481 #if HAVE_MMX
482     INIT_TRELLIS( sse2 );
483     if( cpu&X264_CPU_MMX2 )
484     {
485 #if ARCH_X86
486         pf->denoise_dct = x264_denoise_dct_mmx;
487         pf->decimate_score15 = x264_decimate_score15_mmx2;
488         pf->decimate_score16 = x264_decimate_score16_mmx2;
489         pf->decimate_score64 = x264_decimate_score64_mmx2;
490         pf->coeff_last8 = x264_coeff_last8_mmx2;
491         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmx2;
492         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmx2;
493         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmx2;
494         pf->coeff_level_run8 = x264_coeff_level_run8_mmx2;
495         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmx2;
496         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmx2;
497 #endif
498         pf->coeff_last4 = x264_coeff_last4_mmx2;
499         pf->coeff_level_run4 = x264_coeff_level_run4_mmx2;
500         if( cpu&X264_CPU_LZCNT )
501             pf->coeff_level_run4 = x264_coeff_level_run4_mmx2_lzcnt;
502     }
503     if( cpu&X264_CPU_SSE2 )
504     {
505         pf->quant_4x4 = x264_quant_4x4_sse2;
506         pf->quant_4x4x4 = x264_quant_4x4x4_sse2;
507         pf->quant_8x8 = x264_quant_8x8_sse2;
508         pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
509         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
510         pf->dequant_4x4 = x264_dequant_4x4_sse2;
511         pf->dequant_8x8 = x264_dequant_8x8_sse2;
512         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
513         pf->denoise_dct = x264_denoise_dct_sse2;
514         pf->decimate_score15 = x264_decimate_score15_sse2;
515         pf->decimate_score16 = x264_decimate_score16_sse2;
516         pf->decimate_score64 = x264_decimate_score64_sse2;
517         pf->coeff_last8 = x264_coeff_last8_sse2;
518         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
519         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
520         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
521         pf->coeff_level_run8 = x264_coeff_level_run8_sse2;
522         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
523         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
524         if( cpu&X264_CPU_LZCNT )
525         {
526             pf->coeff_last4 = x264_coeff_last4_mmx2_lzcnt;
527             pf->coeff_last8 = x264_coeff_last8_sse2_lzcnt;
528             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
529             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
530             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
531             pf->coeff_level_run8 = x264_coeff_level_run8_sse2_lzcnt;
532             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
533             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
534         }
535     }
536     if( cpu&X264_CPU_SSSE3 )
537     {
538         pf->quant_4x4 = x264_quant_4x4_ssse3;
539         pf->quant_4x4x4 = x264_quant_4x4x4_ssse3;
540         pf->quant_8x8 = x264_quant_8x8_ssse3;
541         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
542         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
543         pf->denoise_dct = x264_denoise_dct_ssse3;
544         pf->decimate_score15 = x264_decimate_score15_ssse3;
545         pf->decimate_score16 = x264_decimate_score16_ssse3;
546         pf->decimate_score64 = x264_decimate_score64_ssse3;
547         INIT_TRELLIS( ssse3 );
548     }
549     if( cpu&X264_CPU_SSE4 )
550     {
551         pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
552         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
553         pf->quant_4x4 = x264_quant_4x4_sse4;
554         pf->quant_4x4x4 = x264_quant_4x4x4_sse4;
555         pf->quant_8x8 = x264_quant_8x8_sse4;
556     }
557     if( cpu&X264_CPU_AVX )
558     {
559         pf->denoise_dct = x264_denoise_dct_avx;
560     }
561     if( cpu&X264_CPU_XOP )
562     {
563         pf->dequant_4x4_dc = x264_dequant_4x4dc_xop;
564         if( h->param.i_cqm_preset != X264_CQM_FLAT )
565         {
566             pf->dequant_4x4 = x264_dequant_4x4_xop;
567             pf->dequant_8x8 = x264_dequant_8x8_xop;
568         }
569     }
570     if( cpu&X264_CPU_AVX2 )
571     {
572         pf->quant_4x4 = x264_quant_4x4_avx2;
573         pf->quant_4x4_dc = x264_quant_4x4_dc_avx2;
574         pf->quant_8x8 = x264_quant_8x8_avx2;
575         pf->quant_4x4x4 = x264_quant_4x4x4_avx2;
576         pf->dequant_4x4 = x264_dequant_4x4_avx2;
577         pf->dequant_8x8 = x264_dequant_8x8_avx2;
578         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx2;
579         pf->denoise_dct = x264_denoise_dct_avx2;
580     }
581 #endif // HAVE_MMX
582 #else // !HIGH_BIT_DEPTH
583 #if HAVE_MMX
584     INIT_TRELLIS( sse2 );
585     if( cpu&X264_CPU_MMX )
586     {
587 #if ARCH_X86
588         pf->quant_4x4 = x264_quant_4x4_mmx;
589         pf->quant_8x8 = x264_quant_8x8_mmx;
590         pf->dequant_4x4 = x264_dequant_4x4_mmx;
591         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmx2;
592         pf->dequant_8x8 = x264_dequant_8x8_mmx;
593         if( h->param.i_cqm_preset == X264_CQM_FLAT )
594         {
595             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
596             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
597         }
598         pf->denoise_dct = x264_denoise_dct_mmx;
599 #endif
600     }
601 
602     if( cpu&X264_CPU_MMX2 )
603     {
604         pf->quant_2x2_dc = x264_quant_2x2_dc_mmx2;
605 #if ARCH_X86
606         pf->quant_4x4_dc = x264_quant_4x4_dc_mmx2;
607         pf->decimate_score15 = x264_decimate_score15_mmx2;
608         pf->decimate_score16 = x264_decimate_score16_mmx2;
609         pf->decimate_score64 = x264_decimate_score64_mmx2;
610         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmx2;
611         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmx2;
612         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmx2;
613         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmx2;
614         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmx2;
615 #endif
616         pf->coeff_last4 = x264_coeff_last4_mmx2;
617         pf->coeff_last8 = x264_coeff_last8_mmx2;
618         pf->coeff_level_run4 = x264_coeff_level_run4_mmx2;
619         pf->coeff_level_run8 = x264_coeff_level_run8_mmx2;
620         if( cpu&X264_CPU_LZCNT )
621         {
622             pf->coeff_last4 = x264_coeff_last4_mmx2_lzcnt;
623             pf->coeff_last8 = x264_coeff_last8_mmx2_lzcnt;
624             pf->coeff_level_run4 = x264_coeff_level_run4_mmx2_lzcnt;
625             pf->coeff_level_run8 = x264_coeff_level_run8_mmx2_lzcnt;
626         }
627     }
628 
629     if( cpu&X264_CPU_SSE2 )
630     {
631         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
632         pf->quant_4x4 = x264_quant_4x4_sse2;
633         pf->quant_4x4x4 = x264_quant_4x4x4_sse2;
634         pf->quant_8x8 = x264_quant_8x8_sse2;
635         pf->dequant_4x4 = x264_dequant_4x4_sse2;
636         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
637         pf->dequant_8x8 = x264_dequant_8x8_sse2;
638         if( h->param.i_cqm_preset == X264_CQM_FLAT )
639         {
640             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
641             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
642         }
643         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_sse2;
644         pf->denoise_dct = x264_denoise_dct_sse2;
645         pf->decimate_score15 = x264_decimate_score15_sse2;
646         pf->decimate_score16 = x264_decimate_score16_sse2;
647         pf->decimate_score64 = x264_decimate_score64_sse2;
648         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
649         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
650         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
651         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
652         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
653         if( cpu&X264_CPU_LZCNT )
654         {
655             pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2_lzcnt;
656             pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2_lzcnt;
657             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2_lzcnt;
658             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2_lzcnt;
659             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2_lzcnt;
660         }
661     }
662 
663     if( cpu&X264_CPU_SSSE3 )
664     {
665         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
666         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
667         pf->quant_4x4 = x264_quant_4x4_ssse3;
668         pf->quant_4x4x4 = x264_quant_4x4x4_ssse3;
669         pf->quant_8x8 = x264_quant_8x8_ssse3;
670         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_ssse3;
671         pf->denoise_dct = x264_denoise_dct_ssse3;
672         pf->decimate_score15 = x264_decimate_score15_ssse3;
673         pf->decimate_score16 = x264_decimate_score16_ssse3;
674         pf->decimate_score64 = x264_decimate_score64_ssse3;
675         INIT_TRELLIS( ssse3 );
676         pf->coeff_level_run4 = x264_coeff_level_run4_ssse3;
677         pf->coeff_level_run8 = x264_coeff_level_run8_ssse3;
678         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_ssse3;
679         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_ssse3;
680         if( cpu&X264_CPU_LZCNT )
681         {
682             pf->coeff_level_run4 = x264_coeff_level_run4_ssse3;
683             pf->coeff_level_run8 = x264_coeff_level_run8_ssse3;
684             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_ssse3_lzcnt;
685             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_ssse3_lzcnt;
686         }
687     }
688 
689     if( cpu&X264_CPU_SSE4 )
690     {
691         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
692         pf->quant_4x4 = x264_quant_4x4_sse4;
693         pf->quant_8x8 = x264_quant_8x8_sse4;
694         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_sse4;
695     }
696 
697     if( cpu&X264_CPU_AVX )
698     {
699         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx;
700         if( h->param.i_cqm_preset != X264_CQM_FLAT )
701         {
702             pf->dequant_4x4 = x264_dequant_4x4_avx;
703             pf->dequant_8x8 = x264_dequant_8x8_avx;
704         }
705         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_avx;
706         pf->denoise_dct = x264_denoise_dct_avx;
707     }
708 
709     if( cpu&X264_CPU_XOP )
710     {
711         if( h->param.i_cqm_preset != X264_CQM_FLAT )
712         {
713             pf->dequant_4x4 = x264_dequant_4x4_xop;
714             pf->dequant_8x8 = x264_dequant_8x8_xop;
715         }
716     }
717 
718     if( cpu&X264_CPU_AVX2 )
719     {
720         pf->quant_4x4 = x264_quant_4x4_avx2;
721         pf->quant_4x4_dc = x264_quant_4x4_dc_avx2;
722         pf->quant_8x8 = x264_quant_8x8_avx2;
723         pf->quant_4x4x4 = x264_quant_4x4x4_avx2;
724         pf->dequant_4x4 = x264_dequant_4x4_avx2;
725         pf->dequant_8x8 = x264_dequant_8x8_avx2;
726         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx2;
727         if( h->param.i_cqm_preset == X264_CQM_FLAT )
728         {
729             pf->dequant_4x4 = x264_dequant_4x4_flat16_avx2;
730             pf->dequant_8x8 = x264_dequant_8x8_flat16_avx2;
731         }
732         pf->decimate_score64 = x264_decimate_score64_avx2;
733         pf->denoise_dct = x264_denoise_dct_avx2;
734         if( cpu&X264_CPU_LZCNT )
735         {
736             pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_avx2_lzcnt;
737             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_avx2_lzcnt;
738             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_avx2_lzcnt;
739         }
740     }
741 #endif // HAVE_MMX
742 
743 #if HAVE_ALTIVEC
744     if( cpu&X264_CPU_ALTIVEC ) {
745         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
746         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
747         pf->quant_4x4 = x264_quant_4x4_altivec;
748         pf->quant_8x8 = x264_quant_8x8_altivec;
749 
750         pf->dequant_4x4 = x264_dequant_4x4_altivec;
751         pf->dequant_8x8 = x264_dequant_8x8_altivec;
752     }
753 #endif
754 
755 #if HAVE_ARMV6
756     if( cpu&X264_CPU_ARMV6 )
757     {
758         pf->coeff_last4 = x264_coeff_last4_arm;
759         pf->coeff_last8 = x264_coeff_last8_arm;
760     }
761 #endif
762 #if HAVE_ARMV6 || ARCH_AARCH64
763     if( cpu&X264_CPU_NEON )
764     {
765         pf->quant_2x2_dc   = x264_quant_2x2_dc_neon;
766         pf->quant_4x4      = x264_quant_4x4_neon;
767         pf->quant_4x4_dc   = x264_quant_4x4_dc_neon;
768         pf->quant_4x4x4    = x264_quant_4x4x4_neon;
769         pf->quant_8x8      = x264_quant_8x8_neon;
770         pf->dequant_4x4    = x264_dequant_4x4_neon;
771         pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
772         pf->dequant_8x8    = x264_dequant_8x8_neon;
773         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
774         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
775         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
776     }
777 #endif
778 #if ARCH_AARCH64
779     if( cpu&X264_CPU_ARMV8 )
780     {
781         pf->coeff_last4 = x264_coeff_last4_aarch64;
782         pf->coeff_last8 = x264_coeff_last8_aarch64;
783     }
784 #endif
785 #endif // HIGH_BIT_DEPTH
786     pf->coeff_last[DCT_LUMA_DC]     = pf->coeff_last[DCT_CHROMAU_DC]  = pf->coeff_last[DCT_CHROMAV_DC] =
787     pf->coeff_last[DCT_CHROMAU_4x4] = pf->coeff_last[DCT_CHROMAV_4x4] = pf->coeff_last[DCT_LUMA_4x4];
788     pf->coeff_last[DCT_CHROMA_AC]   = pf->coeff_last[DCT_CHROMAU_AC]  =
789     pf->coeff_last[DCT_CHROMAV_AC]  = pf->coeff_last[DCT_LUMA_AC];
790     pf->coeff_last[DCT_CHROMAU_8x8] = pf->coeff_last[DCT_CHROMAV_8x8] = pf->coeff_last[DCT_LUMA_8x8];
791 
792     pf->coeff_level_run[DCT_LUMA_DC]     = pf->coeff_level_run[DCT_CHROMAU_DC]  = pf->coeff_level_run[DCT_CHROMAV_DC] =
793     pf->coeff_level_run[DCT_CHROMAU_4x4] = pf->coeff_level_run[DCT_CHROMAV_4x4] = pf->coeff_level_run[DCT_LUMA_4x4];
794     pf->coeff_level_run[DCT_CHROMA_AC]   = pf->coeff_level_run[DCT_CHROMAU_AC]  =
795     pf->coeff_level_run[DCT_CHROMAV_AC]  = pf->coeff_level_run[DCT_LUMA_AC];
796 }
797