]> git.sesse.net Git - x264/blob - common/quant.c
faster mmx quant 15bit, and add 16bit version. total speedup: ~0.3%
[x264] / common / quant.c
1 /*****************************************************************************
2  * quant.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2005 x264 project
5  *
6  * Authors: Christian Heine <sennindemokrit@gmx.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #include "common.h"
24
25 #ifdef HAVE_MMXEXT
26 #include "i386/quant.h"
27 #endif
28
29 #define QUANT_ONE( coef, mf ) \
30 { \
31     if( (coef) > 0 ) \
32         (coef) = ( f + (coef) * (mf) ) >> i_qbits; \
33     else \
34         (coef) = - ( ( f - (coef) * (mf) ) >> i_qbits ); \
35 }
36
37 static void quant_8x8_core( int16_t dct[8][8], int quant_mf[8][8], int i_qbits, int f )
38 {
39     int i;
40     for( i = 0; i < 64; i++ )
41         QUANT_ONE( dct[0][i], quant_mf[0][i] );
42 }
43
44 static void quant_4x4_core( int16_t dct[4][4], int quant_mf[4][4], int i_qbits, int f )
45 {
46     int i;
47     for( i = 0; i < 16; i++ )
48         QUANT_ONE( dct[0][i], quant_mf[0][i] );
49 }
50
51 static void quant_4x4_dc_core( int16_t dct[4][4], int i_quant_mf, int i_qbits, int f )
52 {
53     int i;
54     for( i = 0; i < 16; i++ )
55         QUANT_ONE( dct[0][i], i_quant_mf );
56 }
57
58 static void quant_2x2_dc_core( int16_t dct[2][2], int i_quant_mf, int i_qbits, int f )
59 {
60     QUANT_ONE( dct[0][0], i_quant_mf );
61     QUANT_ONE( dct[0][1], i_quant_mf );
62     QUANT_ONE( dct[0][2], i_quant_mf );
63     QUANT_ONE( dct[0][3], i_quant_mf );
64 }
65
66
67 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
68 {
69     int i, maxQ8=0, maxQ4=0, maxQdc=0;
70
71     pf->quant_8x8_core = quant_8x8_core;
72     pf->quant_4x4_core = quant_4x4_core;
73     pf->quant_4x4_dc_core = quant_4x4_dc_core;
74     pf->quant_2x2_dc_core = quant_2x2_dc_core;
75
76 #ifdef HAVE_MMXEXT
77
78     /* determine the biggest coeffient in all quant8_mf tables */
79     for( i = 0; i < 2*6*8*8; i++ )
80     {
81         int q = h->quant8_mf[0][0][0][i];
82         if( maxQ8 < q )
83             maxQ8 = q;
84     }
85
86     /* determine the biggest coeffient in all quant4_mf tables ( maxQ4 )
87        and the biggest DC coefficient if all quant4_mf tables ( maxQdc ) */
88     for( i = 0; i < 4*6*4*4; i++ )
89     {
90         int q = h->quant4_mf[0][0][0][i];
91         if( maxQ4 < q )
92             maxQ4 = q;
93         if( maxQdc < q && i%16 == 0 )
94             maxQdc = q;
95     }
96
97     /* select quant_8x8 based on CPU and maxQ8 */
98     if( maxQ8 < (1<<15) && cpu&X264_CPU_MMX )
99         pf->quant_8x8_core = x264_quant_8x8_core15_mmx;
100     else
101     if( maxQ8 < (1<<16) && cpu&X264_CPU_MMXEXT )
102         pf->quant_8x8_core = x264_quant_8x8_core16_mmxext;
103     else
104     if( cpu&X264_CPU_MMXEXT )
105         pf->quant_8x8_core = x264_quant_8x8_core32_mmxext;
106
107     /* select quant_4x4 based on CPU and maxQ4 */
108     if( maxQ4 < (1<<15) && cpu&X264_CPU_MMX )
109         pf->quant_4x4_core = x264_quant_4x4_core15_mmx;
110     else
111     if( maxQ4 < (1<<16) && cpu&X264_CPU_MMXEXT )
112         pf->quant_4x4_core = x264_quant_4x4_core16_mmxext;
113     else
114     if( cpu&X264_CPU_MMXEXT )
115         pf->quant_4x4_core = x264_quant_4x4_core32_mmxext;
116
117     /* select quant_XxX_dc based on CPU and maxQdc */
118     if( maxQdc < (1<<16) && cpu&X264_CPU_MMXEXT )
119     {
120         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core16_mmxext;
121         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core16_mmxext;
122     }
123     else
124     if( maxQdc < (1<<15) && cpu&X264_CPU_MMX )
125     {
126         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core15_mmx;
127         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core15_mmx;
128     }
129     else
130     if( cpu&X264_CPU_MMXEXT )
131     {
132         pf->quant_4x4_dc_core = x264_quant_4x4_dc_core32_mmxext;
133         pf->quant_2x2_dc_core = x264_quant_2x2_dc_core32_mmxext;
134     }
135
136 #endif  /* HAVE_MMXEXT */
137 }