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