]> git.sesse.net Git - x264/blob - common/dct.h
CAVLC optimizations
[x264] / common / dct.h
1 /*****************************************************************************
2  * dct.h: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2004-2008 Loren Merritt <lorenm@u.washington.edu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19  *****************************************************************************/
20
21 #ifndef X264_DCT_H
22 #define X264_DCT_H
23
24 /* the inverse of the scaling factors introduced by 8x8 fdct */
25 #define W(i) (i==0 ? FIX8(1.0000) :\
26               i==1 ? FIX8(0.8859) :\
27               i==2 ? FIX8(1.6000) :\
28               i==3 ? FIX8(0.9415) :\
29               i==4 ? FIX8(1.2651) :\
30               i==5 ? FIX8(1.1910) :0)
31 static const uint16_t x264_dct8_weight_tab[64] = {
32     W(0), W(3), W(4), W(3),  W(0), W(3), W(4), W(3),
33     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
34     W(4), W(5), W(2), W(5),  W(4), W(5), W(2), W(5),
35     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
36
37     W(0), W(3), W(4), W(3),  W(0), W(3), W(4), W(3),
38     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
39     W(4), W(5), W(2), W(5),  W(4), W(5), W(2), W(5),
40     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1)
41 };
42 #undef W
43
44 #define W(i) (i==0 ? FIX8(1.76777) :\
45               i==1 ? FIX8(1.11803) :\
46               i==2 ? FIX8(0.70711) :0)
47 static const uint16_t x264_dct4_weight_tab[16] = {
48     W(0), W(1), W(0), W(1),
49     W(1), W(2), W(1), W(2),
50     W(0), W(1), W(0), W(1),
51     W(1), W(2), W(1), W(2)
52 };
53 #undef W
54
55 /* inverse squared */
56 #define W(i) (i==0 ? FIX8(3.125) :\
57               i==1 ? FIX8(1.25) :\
58               i==2 ? FIX8(0.5) :0)
59 static const uint16_t x264_dct4_weight2_tab[16] = {
60     W(0), W(1), W(0), W(1),
61     W(1), W(2), W(1), W(2),
62     W(0), W(1), W(0), W(1),
63     W(1), W(2), W(1), W(2)
64 };
65 #undef W
66
67 #define W(i) (i==0 ? FIX8(1.00000) :\
68               i==1 ? FIX8(0.78487) :\
69               i==2 ? FIX8(2.56132) :\
70               i==3 ? FIX8(0.88637) :\
71               i==4 ? FIX8(1.60040) :\
72               i==5 ? FIX8(1.41850) :0)
73 static const uint16_t x264_dct8_weight2_tab[64] = {
74     W(0), W(3), W(4), W(3),  W(0), W(3), W(4), W(3),
75     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
76     W(4), W(5), W(2), W(5),  W(4), W(5), W(2), W(5),
77     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
78
79     W(0), W(3), W(4), W(3),  W(0), W(3), W(4), W(3),
80     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1),
81     W(4), W(5), W(2), W(5),  W(4), W(5), W(2), W(5),
82     W(3), W(1), W(5), W(1),  W(3), W(1), W(5), W(1)
83 };
84 #undef W
85
86 extern int x264_dct4_weight2_zigzag[2][16]; // [2] = {frame, field}
87 extern int x264_dct8_weight2_zigzag[2][64];
88
89 typedef struct
90 {
91     // pix1  stride = FENC_STRIDE
92     // pix2  stride = FDEC_STRIDE
93     // p_dst stride = FDEC_STRIDE
94     void (*sub4x4_dct)   ( int16_t dct[4][4], uint8_t *pix1, uint8_t *pix2 );
95     void (*add4x4_idct)  ( uint8_t *p_dst, int16_t dct[4][4] );
96
97     void (*sub8x8_dct)   ( int16_t dct[4][4][4], uint8_t *pix1, uint8_t *pix2 );
98     void (*add8x8_idct)  ( uint8_t *p_dst, int16_t dct[4][4][4] );
99     void (*add8x8_idct_dc) ( uint8_t *p_dst, int16_t dct[2][2] );
100
101     void (*sub16x16_dct) ( int16_t dct[16][4][4], uint8_t *pix1, uint8_t *pix2 );
102     void (*add16x16_idct)( uint8_t *p_dst, int16_t dct[16][4][4] );
103     void (*add16x16_idct_dc) ( uint8_t *p_dst, int16_t dct[4][4] );
104
105     void (*sub8x8_dct8)  ( int16_t dct[8][8], uint8_t *pix1, uint8_t *pix2 );
106     void (*add8x8_idct8) ( uint8_t *p_dst, int16_t dct[8][8] );
107
108     void (*sub16x16_dct8) ( int16_t dct[4][8][8], uint8_t *pix1, uint8_t *pix2 );
109     void (*add16x16_idct8)( uint8_t *p_dst, int16_t dct[4][8][8] );
110
111     void (*dct4x4dc) ( int16_t d[4][4] );
112     void (*idct4x4dc)( int16_t d[4][4] );
113
114 } x264_dct_function_t;
115
116 typedef struct
117 {
118     void (*scan_8x8)( int16_t level[64], int16_t dct[8][8] );
119     void (*scan_4x4)( int16_t level[16], int16_t dct[4][4] );
120     void (*sub_8x8)( int16_t level[64], const uint8_t *p_src, uint8_t *p_dst );
121     void (*sub_4x4)( int16_t level[16], const uint8_t *p_src, uint8_t *p_dst );
122     void (*interleave_8x8_cavlc)( int16_t *dst, int16_t *src, uint8_t *nnz );
123
124 } x264_zigzag_function_t;
125
126 void x264_dct_init( int cpu, x264_dct_function_t *dctf );
127 void x264_dct_init_weights( void );
128 void x264_zigzag_init( int cpu, x264_zigzag_function_t *pf, int b_interlaced );
129
130 #endif