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