1 /*****************************************************************************
2  * quant.c: quantization and level-run
3  *****************************************************************************
4  * Copyright (C) 2005-2021 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 HAVE_ALTIVEC
35 #   include "ppc/quant.h"
36 #endif
37 #if HAVE_ARMV6
38 #   include "arm/quant.h"
39 #endif
40 #if HAVE_AARCH64
41 #   include "aarch64/quant.h"
42 #endif
43 #if HAVE_MSA
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 
quant_8x8(dctcoef dct[64],udctcoef mf[64],udctcoef bias[64])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 
quant_4x4(dctcoef dct[16],udctcoef mf[16],udctcoef bias[16])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 
quant_4x4x4(dctcoef dct[4][16],udctcoef mf[16],udctcoef bias[16])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 
quant_4x4_dc(dctcoef dct[16],int mf,int bias)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 
quant_2x2_dc(dctcoef dct[4],int mf,int bias)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 
dequant_4x4(dctcoef dct[16],int dequant_mf[6][16],int i_qp)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 
dequant_8x8(dctcoef dct[64],int dequant_mf[6][64],int i_qp)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 
dequant_4x4_dc(dctcoef dct[16],int dequant_mf[6][16],int i_qp)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 
idct_dequant_2x4_dc(dctcoef dct[8],dctcoef dct4x4[8][16],int dequant_mf[6][16],int i_qp)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 
idct_dequant_2x4_dconly(dctcoef dct[8],int dequant_mf[6][16],int i_qp)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 
optimize_chroma_idct_dequant_2x4(dctcoef out[8],dctcoef dct[8],int dmf)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 
optimize_chroma_idct_dequant_2x2(dctcoef out[4],dctcoef dct[4],int dmf)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 
optimize_chroma_round(dctcoef * ref,dctcoef * dct,int dequant_mf,int chroma422)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 
optimize_chroma_dc_internal(dctcoef * dct,int dequant_mf,int chroma422)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 
optimize_chroma_2x2_dc(dctcoef dct[4],int dequant_mf)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 
optimize_chroma_2x4_dc(dctcoef dct[8],int dequant_mf)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 
denoise_dct(dctcoef * dct,uint32_t * sum,udctcoef * offset,int size)301 static void 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 
decimate_score_internal(dctcoef * dct,int i_max)323 static ALWAYS_INLINE int decimate_score_internal( dctcoef *dct, int i_max )
324 {
325     const uint8_t *ds_table = (i_max == 64) ? x264_decimate_table8 : x264_decimate_table4;
326     int i_score = 0;
327     int idx = i_max - 1;
328 
329     while( idx >= 0 && dct[idx] == 0 )
330         idx--;
331     while( idx >= 0 )
332     {
333         int i_run;
334 
335         if( (unsigned)(dct[idx--] + 1) > 2 )
336             return 9;
337 
338         i_run = 0;
339         while( idx >= 0 && dct[idx] == 0 )
340         {
341             idx--;
342             i_run++;
343         }
344         i_score += ds_table[i_run];
345     }
346 
347     return i_score;
348 }
349 
decimate_score15(dctcoef * dct)350 static int decimate_score15( dctcoef *dct )
351 {
352     return decimate_score_internal( dct+1, 15 );
353 }
decimate_score16(dctcoef * dct)354 static int decimate_score16( dctcoef *dct )
355 {
356     return decimate_score_internal( dct, 16 );
357 }
decimate_score64(dctcoef * dct)358 static int decimate_score64( dctcoef *dct )
359 {
360     return decimate_score_internal( dct, 64 );
361 }
362 
363 #define last(num)\
364 static int coeff_last##num( dctcoef *l )\
365 {\
366     int i_last = num-1;\
367     while( i_last >= 0 && l[i_last] == 0 )\
368         i_last--;\
369     return i_last;\
370 }
371 
372 last(4)
373 last(8)
374 last(15)
375 last(16)
376 last(64)
377 
378 #define level_run(num)\
379 static int coeff_level_run##num( dctcoef *dct, x264_run_level_t *runlevel )\
380 {\
381     int i_last = runlevel->last = coeff_last##num(dct);\
382     int i_total = 0;\
383     int mask = 0;\
384     do\
385     {\
386         runlevel->level[i_total++] = dct[i_last];\
387         mask |= 1 << (i_last);\
388         while( --i_last >= 0 && dct[i_last] == 0 );\
389     } while( i_last >= 0 );\
390     runlevel->mask = mask;\
391     return i_total;\
392 }
393 
394 level_run(4)
395 level_run(8)
396 level_run(15)
397 level_run(16)
398 
399 #if ARCH_X86_64
400 #define INIT_TRELLIS(cpu)\
401     pf->trellis_cabac_4x4 = x264_trellis_cabac_4x4_##cpu;\
402     pf->trellis_cabac_8x8 = x264_trellis_cabac_8x8_##cpu;\
403     pf->trellis_cabac_4x4_psy = x264_trellis_cabac_4x4_psy_##cpu;\
404     pf->trellis_cabac_8x8_psy = x264_trellis_cabac_8x8_psy_##cpu;\
405     pf->trellis_cabac_dc = x264_trellis_cabac_dc_##cpu;\
406     pf->trellis_cabac_chroma_422_dc = x264_trellis_cabac_chroma_422_dc_##cpu;
407 #else
408 #define INIT_TRELLIS(...)
409 #endif
410 
x264_quant_init(x264_t * h,uint32_t cpu,x264_quant_function_t * pf)411 void x264_quant_init( x264_t *h, uint32_t cpu, x264_quant_function_t *pf )
412 {
413     pf->quant_8x8 = quant_8x8;
414     pf->quant_4x4 = quant_4x4;
415     pf->quant_4x4x4 = quant_4x4x4;
416     pf->quant_4x4_dc = quant_4x4_dc;
417     pf->quant_2x2_dc = quant_2x2_dc;
418 
419     pf->dequant_4x4 = dequant_4x4;
420     pf->dequant_4x4_dc = dequant_4x4_dc;
421     pf->dequant_8x8 = dequant_8x8;
422 
423     pf->idct_dequant_2x4_dc = idct_dequant_2x4_dc;
424     pf->idct_dequant_2x4_dconly = idct_dequant_2x4_dconly;
425 
426     pf->optimize_chroma_2x2_dc = optimize_chroma_2x2_dc;
427     pf->optimize_chroma_2x4_dc = optimize_chroma_2x4_dc;
428 
429     pf->denoise_dct = denoise_dct;
430     pf->decimate_score15 = decimate_score15;
431     pf->decimate_score16 = decimate_score16;
432     pf->decimate_score64 = decimate_score64;
433 
434     pf->coeff_last4 = coeff_last4;
435     pf->coeff_last8 = coeff_last8;
436     pf->coeff_last[  DCT_LUMA_AC] = coeff_last15;
437     pf->coeff_last[ DCT_LUMA_4x4] = coeff_last16;
438     pf->coeff_last[ DCT_LUMA_8x8] = coeff_last64;
439     pf->coeff_level_run4 = coeff_level_run4;
440     pf->coeff_level_run8 = coeff_level_run8;
441     pf->coeff_level_run[  DCT_LUMA_AC] = coeff_level_run15;
442     pf->coeff_level_run[ DCT_LUMA_4x4] = coeff_level_run16;
443 
444 #if HIGH_BIT_DEPTH
445 #if HAVE_MMX
446     INIT_TRELLIS( sse2 );
447     if( cpu&X264_CPU_MMX2 )
448     {
449 #if ARCH_X86
450         pf->denoise_dct = x264_denoise_dct_mmx;
451         pf->coeff_last8 = x264_coeff_last8_mmx2;
452         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmx2;
453         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmx2;
454         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmx2;
455         pf->coeff_level_run8 = x264_coeff_level_run8_mmx2;
456         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmx2;
457         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmx2;
458 #endif
459         pf->coeff_last4 = x264_coeff_last4_mmx2;
460         pf->coeff_level_run4 = x264_coeff_level_run4_mmx2;
461     }
462     if( cpu&X264_CPU_SSE2 )
463     {
464         pf->quant_4x4 = x264_quant_4x4_sse2;
465         pf->quant_4x4x4 = x264_quant_4x4x4_sse2;
466         pf->quant_8x8 = x264_quant_8x8_sse2;
467         pf->quant_2x2_dc = x264_quant_2x2_dc_sse2;
468         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
469         pf->dequant_4x4 = x264_dequant_4x4_sse2;
470         pf->dequant_8x8 = x264_dequant_8x8_sse2;
471         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
472         pf->idct_dequant_2x4_dc = x264_idct_dequant_2x4_dc_sse2;
473         pf->idct_dequant_2x4_dconly = x264_idct_dequant_2x4_dconly_sse2;
474         pf->denoise_dct = x264_denoise_dct_sse2;
475         pf->decimate_score15 = x264_decimate_score15_sse2;
476         pf->decimate_score16 = x264_decimate_score16_sse2;
477         pf->decimate_score64 = x264_decimate_score64_sse2;
478         pf->coeff_last8 = x264_coeff_last8_sse2;
479         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
480         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
481         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
482         pf->coeff_level_run8 = x264_coeff_level_run8_sse2;
483         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
484         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
485     }
486     if( cpu&X264_CPU_LZCNT )
487     {
488         pf->coeff_last4 = x264_coeff_last4_lzcnt;
489         pf->coeff_last8 = x264_coeff_last8_lzcnt;
490         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_lzcnt;
491         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_lzcnt;
492         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_lzcnt;
493         pf->coeff_level_run4 = x264_coeff_level_run4_lzcnt;
494         pf->coeff_level_run8 = x264_coeff_level_run8_lzcnt;
495         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_lzcnt;
496         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_lzcnt;
497     }
498     if( cpu&X264_CPU_SSSE3 )
499     {
500         pf->quant_4x4 = x264_quant_4x4_ssse3;
501         pf->quant_4x4x4 = x264_quant_4x4x4_ssse3;
502         pf->quant_8x8 = x264_quant_8x8_ssse3;
503         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
504         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
505         pf->denoise_dct = x264_denoise_dct_ssse3;
506         pf->decimate_score15 = x264_decimate_score15_ssse3;
507         pf->decimate_score16 = x264_decimate_score16_ssse3;
508         pf->decimate_score64 = x264_decimate_score64_ssse3;
509         INIT_TRELLIS( ssse3 );
510     }
511     if( cpu&X264_CPU_SSE4 )
512     {
513         pf->quant_2x2_dc = x264_quant_2x2_dc_sse4;
514         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
515         pf->quant_4x4 = x264_quant_4x4_sse4;
516         pf->quant_4x4x4 = x264_quant_4x4x4_sse4;
517         pf->quant_8x8 = x264_quant_8x8_sse4;
518     }
519     if( cpu&X264_CPU_AVX )
520     {
521         pf->idct_dequant_2x4_dc = x264_idct_dequant_2x4_dc_avx;
522         pf->idct_dequant_2x4_dconly = x264_idct_dequant_2x4_dconly_avx;
523         pf->denoise_dct = x264_denoise_dct_avx;
524     }
525     if( cpu&X264_CPU_XOP )
526     {
527         pf->dequant_4x4_dc = x264_dequant_4x4dc_xop;
528         if( h->param.i_cqm_preset != X264_CQM_FLAT )
529         {
530             pf->dequant_4x4 = x264_dequant_4x4_xop;
531             pf->dequant_8x8 = x264_dequant_8x8_xop;
532         }
533     }
534     if( cpu&X264_CPU_AVX2 )
535     {
536         pf->quant_4x4 = x264_quant_4x4_avx2;
537         pf->quant_4x4_dc = x264_quant_4x4_dc_avx2;
538         pf->quant_8x8 = x264_quant_8x8_avx2;
539         pf->quant_4x4x4 = x264_quant_4x4x4_avx2;
540         pf->dequant_4x4 = x264_dequant_4x4_avx2;
541         pf->dequant_8x8 = x264_dequant_8x8_avx2;
542         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx2;
543         pf->denoise_dct = x264_denoise_dct_avx2;
544         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_avx2;
545     }
546     if( cpu&X264_CPU_AVX512 )
547     {
548         pf->dequant_4x4 = x264_dequant_4x4_avx512;
549         pf->dequant_8x8 = x264_dequant_8x8_avx512;
550         pf->decimate_score15 = x264_decimate_score15_avx512;
551         pf->decimate_score16 = x264_decimate_score16_avx512;
552         pf->decimate_score64 = x264_decimate_score64_avx512;
553         pf->coeff_last4 = x264_coeff_last4_avx512;
554         pf->coeff_last8 = x264_coeff_last8_avx512;
555         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_avx512;
556         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_avx512;
557         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_avx512;
558     }
559 #endif // HAVE_MMX
560 #else // !HIGH_BIT_DEPTH
561 #if HAVE_MMX
562     INIT_TRELLIS( sse2 );
563     if( cpu&X264_CPU_MMX )
564     {
565 #if ARCH_X86
566         pf->dequant_4x4 = x264_dequant_4x4_mmx;
567         pf->dequant_4x4_dc = x264_dequant_4x4dc_mmx2;
568         pf->dequant_8x8 = x264_dequant_8x8_mmx;
569         if( h->param.i_cqm_preset == X264_CQM_FLAT )
570         {
571             pf->dequant_4x4 = x264_dequant_4x4_flat16_mmx;
572             pf->dequant_8x8 = x264_dequant_8x8_flat16_mmx;
573         }
574         pf->denoise_dct = x264_denoise_dct_mmx;
575 #endif
576     }
577 
578     if( cpu&X264_CPU_MMX2 )
579     {
580         pf->quant_2x2_dc = x264_quant_2x2_dc_mmx2;
581 #if ARCH_X86
582         pf->quant_4x4 = x264_quant_4x4_mmx2;
583         pf->quant_8x8 = x264_quant_8x8_mmx2;
584         pf->quant_4x4_dc = x264_quant_4x4_dc_mmx2;
585         pf->coeff_last[  DCT_LUMA_AC] = x264_coeff_last15_mmx2;
586         pf->coeff_last[ DCT_LUMA_4x4] = x264_coeff_last16_mmx2;
587         pf->coeff_last[ DCT_LUMA_8x8] = x264_coeff_last64_mmx2;
588         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_mmx2;
589         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_mmx2;
590 #endif
591         pf->coeff_last4 = x264_coeff_last4_mmx2;
592         pf->coeff_last8 = x264_coeff_last8_mmx2;
593         pf->coeff_level_run4 = x264_coeff_level_run4_mmx2;
594         pf->coeff_level_run8 = x264_coeff_level_run8_mmx2;
595     }
596 
597     if( cpu&X264_CPU_SSE2 )
598     {
599         pf->quant_4x4_dc = x264_quant_4x4_dc_sse2;
600         pf->quant_4x4 = x264_quant_4x4_sse2;
601         pf->quant_4x4x4 = x264_quant_4x4x4_sse2;
602         pf->quant_8x8 = x264_quant_8x8_sse2;
603         pf->dequant_4x4 = x264_dequant_4x4_sse2;
604         pf->dequant_4x4_dc = x264_dequant_4x4dc_sse2;
605         pf->dequant_8x8 = x264_dequant_8x8_sse2;
606         if( h->param.i_cqm_preset == X264_CQM_FLAT )
607         {
608             pf->dequant_4x4 = x264_dequant_4x4_flat16_sse2;
609             pf->dequant_8x8 = x264_dequant_8x8_flat16_sse2;
610         }
611         pf->idct_dequant_2x4_dc = x264_idct_dequant_2x4_dc_sse2;
612         pf->idct_dequant_2x4_dconly = x264_idct_dequant_2x4_dconly_sse2;
613         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_sse2;
614         pf->denoise_dct = x264_denoise_dct_sse2;
615         pf->decimate_score15 = x264_decimate_score15_sse2;
616         pf->decimate_score16 = x264_decimate_score16_sse2;
617         pf->decimate_score64 = x264_decimate_score64_sse2;
618         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_sse2;
619         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_sse2;
620         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_sse2;
621         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_sse2;
622         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_sse2;
623     }
624 
625     if( cpu&X264_CPU_LZCNT )
626     {
627         pf->coeff_last4 = x264_coeff_last4_lzcnt;
628         pf->coeff_last8 = x264_coeff_last8_lzcnt;
629         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_lzcnt;
630         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_lzcnt;
631         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_lzcnt;
632         pf->coeff_level_run4 = x264_coeff_level_run4_lzcnt;
633         pf->coeff_level_run8 = x264_coeff_level_run8_lzcnt;
634         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_lzcnt;
635         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_lzcnt;
636     }
637 
638     if( cpu&X264_CPU_SSSE3 )
639     {
640         pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3;
641         pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3;
642         pf->quant_4x4 = x264_quant_4x4_ssse3;
643         pf->quant_4x4x4 = x264_quant_4x4x4_ssse3;
644         pf->quant_8x8 = x264_quant_8x8_ssse3;
645         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_ssse3;
646         pf->denoise_dct = x264_denoise_dct_ssse3;
647         pf->decimate_score15 = x264_decimate_score15_ssse3;
648         pf->decimate_score16 = x264_decimate_score16_ssse3;
649         pf->decimate_score64 = x264_decimate_score64_ssse3;
650         INIT_TRELLIS( ssse3 );
651         pf->coeff_level_run4 = x264_coeff_level_run4_ssse3;
652         pf->coeff_level_run8 = x264_coeff_level_run8_ssse3;
653         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_ssse3;
654         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_ssse3;
655         if( cpu&X264_CPU_LZCNT )
656         {
657             pf->coeff_level_run4 = x264_coeff_level_run4_ssse3_lzcnt;
658             pf->coeff_level_run8 = x264_coeff_level_run8_ssse3_lzcnt;
659             pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_ssse3_lzcnt;
660             pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_ssse3_lzcnt;
661         }
662     }
663 
664     if( cpu&X264_CPU_SSE4 )
665     {
666         pf->quant_4x4_dc = x264_quant_4x4_dc_sse4;
667         pf->quant_4x4 = x264_quant_4x4_sse4;
668         pf->quant_8x8 = x264_quant_8x8_sse4;
669         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_sse4;
670     }
671 
672     if( cpu&X264_CPU_AVX )
673     {
674         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx;
675         if( h->param.i_cqm_preset != X264_CQM_FLAT )
676         {
677             pf->dequant_4x4 = x264_dequant_4x4_avx;
678             pf->dequant_8x8 = x264_dequant_8x8_avx;
679         }
680         pf->idct_dequant_2x4_dc = x264_idct_dequant_2x4_dc_avx;
681         pf->idct_dequant_2x4_dconly = x264_idct_dequant_2x4_dconly_avx;
682         pf->optimize_chroma_2x2_dc = x264_optimize_chroma_2x2_dc_avx;
683         pf->denoise_dct = x264_denoise_dct_avx;
684     }
685 
686     if( cpu&X264_CPU_XOP )
687     {
688         if( h->param.i_cqm_preset != X264_CQM_FLAT )
689         {
690             pf->dequant_4x4 = x264_dequant_4x4_xop;
691             pf->dequant_8x8 = x264_dequant_8x8_xop;
692         }
693     }
694 
695     if( cpu&X264_CPU_AVX2 )
696     {
697         pf->quant_4x4 = x264_quant_4x4_avx2;
698         pf->quant_4x4_dc = x264_quant_4x4_dc_avx2;
699         pf->quant_8x8 = x264_quant_8x8_avx2;
700         pf->quant_4x4x4 = x264_quant_4x4x4_avx2;
701         pf->dequant_4x4 = x264_dequant_4x4_avx2;
702         pf->dequant_8x8 = x264_dequant_8x8_avx2;
703         pf->dequant_4x4_dc = x264_dequant_4x4dc_avx2;
704         if( h->param.i_cqm_preset == X264_CQM_FLAT )
705         {
706             pf->dequant_4x4 = x264_dequant_4x4_flat16_avx2;
707             pf->dequant_8x8 = x264_dequant_8x8_flat16_avx2;
708         }
709         pf->decimate_score64 = x264_decimate_score64_avx2;
710         pf->denoise_dct = x264_denoise_dct_avx2;
711         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_avx2;
712         pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15_avx2;
713         pf->coeff_level_run[DCT_LUMA_4x4] = x264_coeff_level_run16_avx2;
714     }
715     if( cpu&X264_CPU_AVX512 )
716     {
717         if( h->param.i_cqm_preset == X264_CQM_FLAT )
718             pf->dequant_8x8 = x264_dequant_8x8_flat16_avx512;
719         else
720         {
721             pf->dequant_4x4 = x264_dequant_4x4_avx512;
722             pf->dequant_8x8 = x264_dequant_8x8_avx512;
723         }
724         pf->decimate_score15 = x264_decimate_score15_avx512;
725         pf->decimate_score16 = x264_decimate_score16_avx512;
726         pf->decimate_score64 = x264_decimate_score64_avx512;
727         pf->coeff_last8 = x264_coeff_last8_avx512;
728         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_avx512;
729         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_avx512;
730         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_avx512;
731     }
732 #endif // HAVE_MMX
733 
734 #if HAVE_ALTIVEC
735     if( cpu&X264_CPU_ALTIVEC )
736     {
737         pf->quant_2x2_dc = x264_quant_2x2_dc_altivec;
738         pf->quant_4x4_dc = x264_quant_4x4_dc_altivec;
739         pf->quant_4x4 = x264_quant_4x4_altivec;
740         pf->quant_4x4x4 = x264_quant_4x4x4_altivec;
741         pf->quant_8x8 = x264_quant_8x8_altivec;
742 
743         pf->dequant_4x4 = x264_dequant_4x4_altivec;
744         pf->dequant_8x8 = x264_dequant_8x8_altivec;
745     }
746 #endif
747 
748 #if HAVE_ARMV6
749     if( cpu&X264_CPU_ARMV6 )
750     {
751         pf->coeff_last4 = x264_coeff_last4_arm;
752         pf->coeff_last8 = x264_coeff_last8_arm;
753     }
754 #endif
755 #if HAVE_ARMV6 || HAVE_AARCH64
756     if( cpu&X264_CPU_NEON )
757     {
758         pf->quant_2x2_dc   = x264_quant_2x2_dc_neon;
759         pf->quant_4x4      = x264_quant_4x4_neon;
760         pf->quant_4x4_dc   = x264_quant_4x4_dc_neon;
761         pf->quant_4x4x4    = x264_quant_4x4x4_neon;
762         pf->quant_8x8      = x264_quant_8x8_neon;
763         pf->dequant_4x4    = x264_dequant_4x4_neon;
764         pf->dequant_4x4_dc = x264_dequant_4x4_dc_neon;
765         pf->dequant_8x8    = x264_dequant_8x8_neon;
766         pf->coeff_last[ DCT_LUMA_AC] = x264_coeff_last15_neon;
767         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_neon;
768         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
769         pf->denoise_dct = x264_denoise_dct_neon;
770         pf->decimate_score15 = x264_decimate_score15_neon;
771         pf->decimate_score16 = x264_decimate_score16_neon;
772         pf->decimate_score64 = x264_decimate_score64_neon;
773     }
774 #endif
775 #if HAVE_AARCH64
776     if( cpu&X264_CPU_ARMV8 )
777     {
778         pf->coeff_last4 = x264_coeff_last4_aarch64;
779         pf->coeff_last8 = x264_coeff_last8_aarch64;
780         pf->coeff_level_run4 = x264_coeff_level_run4_aarch64;
781     }
782     if( cpu&X264_CPU_NEON )
783     {
784         pf->coeff_level_run8 = x264_coeff_level_run8_neon;
785         pf->coeff_level_run[  DCT_LUMA_AC] = x264_coeff_level_run15_neon;
786         pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16_neon;
787     }
788 #endif
789 
790 #if HAVE_MSA
791     if( cpu&X264_CPU_MSA )
792     {
793         pf->quant_4x4      = x264_quant_4x4_msa;
794         pf->quant_4x4_dc   = x264_quant_4x4_dc_msa;
795         pf->quant_4x4x4    = x264_quant_4x4x4_msa;
796         pf->quant_8x8      = x264_quant_8x8_msa;
797         pf->dequant_4x4    = x264_dequant_4x4_msa;
798         pf->dequant_4x4_dc = x264_dequant_4x4_dc_msa;
799         pf->dequant_8x8    = x264_dequant_8x8_msa;
800         pf->coeff_last[DCT_LUMA_4x4] = x264_coeff_last16_msa;
801         pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_msa;
802     }
803 #endif
804 #endif // HIGH_BIT_DEPTH
805     pf->coeff_last[DCT_LUMA_DC]     = pf->coeff_last[DCT_CHROMAU_DC]  = pf->coeff_last[DCT_CHROMAV_DC] =
806     pf->coeff_last[DCT_CHROMAU_4x4] = pf->coeff_last[DCT_CHROMAV_4x4] = pf->coeff_last[DCT_LUMA_4x4];
807     pf->coeff_last[DCT_CHROMA_AC]   = pf->coeff_last[DCT_CHROMAU_AC]  =
808     pf->coeff_last[DCT_CHROMAV_AC]  = pf->coeff_last[DCT_LUMA_AC];
809     pf->coeff_last[DCT_CHROMAU_8x8] = pf->coeff_last[DCT_CHROMAV_8x8] = pf->coeff_last[DCT_LUMA_8x8];
810 
811     pf->coeff_level_run[DCT_LUMA_DC]     = pf->coeff_level_run[DCT_CHROMAU_DC]  = pf->coeff_level_run[DCT_CHROMAV_DC] =
812     pf->coeff_level_run[DCT_CHROMAU_4x4] = pf->coeff_level_run[DCT_CHROMAV_4x4] = pf->coeff_level_run[DCT_LUMA_4x4];
813     pf->coeff_level_run[DCT_CHROMA_AC]   = pf->coeff_level_run[DCT_CHROMAU_AC]  =
814     pf->coeff_level_run[DCT_CHROMAV_AC]  = pf->coeff_level_run[DCT_LUMA_AC];
815 }
816