]> git.sesse.net Git - x264/blob - common/set.c
More small speed tweaks to macroblock.c
[x264] / common / set.c
1 /*****************************************************************************
2  * set.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2005-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 #include "common.h"
22
23 #define SHIFT(x,s) ((s)<0 ? (x)<<-(s) : (s)==0 ? (x) : ((x)+(1<<((s)-1)))>>(s))
24 #define DIV(n,d) (((n) + ((d)>>1)) / (d))
25
26 static const int dequant4_scale[6][3] =
27 {
28     { 10, 13, 16 },
29     { 11, 14, 18 },
30     { 13, 16, 20 },
31     { 14, 18, 23 },
32     { 16, 20, 25 },
33     { 18, 23, 29 }
34 };
35 static const int quant4_scale[6][3] =
36 {
37     { 13107, 8066, 5243 },
38     { 11916, 7490, 4660 },
39     { 10082, 6554, 4194 },
40     {  9362, 5825, 3647 },
41     {  8192, 5243, 3355 },
42     {  7282, 4559, 2893 },
43 };
44
45 static const int quant8_scan[16] =
46 {
47     0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
48 };
49 static const int dequant8_scale[6][6] =
50 {
51     { 20, 18, 32, 19, 25, 24 },
52     { 22, 19, 35, 21, 28, 26 },
53     { 26, 23, 42, 24, 33, 31 },
54     { 28, 25, 45, 26, 35, 33 },
55     { 32, 28, 51, 30, 40, 38 },
56     { 36, 32, 58, 34, 46, 43 },
57 };
58 static const int quant8_scale[6][6] =
59 {
60     { 13107, 11428, 20972, 12222, 16777, 15481 },
61     { 11916, 10826, 19174, 11058, 14980, 14290 },
62     { 10082,  8943, 15978,  9675, 12710, 11985 },
63     {  9362,  8228, 14913,  8931, 11984, 11259 },
64     {  8192,  7346, 13159,  7740, 10486,  9777 },
65     {  7282,  6428, 11570,  6830,  9118,  8640 }
66 };
67
68 int x264_cqm_init( x264_t *h )
69 {
70     int def_quant4[6][16];
71     int def_quant8[6][64];
72     int def_dequant4[6][16];
73     int def_dequant8[6][64];
74     int quant4_mf[4][6][4][4];
75     int quant8_mf[2][6][8][8];
76     int q, i, j, i_list;
77     int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
78                         32 - h->param.analyse.i_luma_deadzone[0],
79                         32 - 11, 32 - 21 };
80     int max_qp_err = -1;
81
82     for( i = 0; i < 6; i++ )
83     {
84         int size = i<4 ? 16 : 64;
85         for( j = (i<4 ? 0 : 4); j < i; j++ )
86             if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
87                 break;
88         if( j < i )
89         {
90             h->  quant4_mf[i] = h->  quant4_mf[j];
91             h->dequant4_mf[i] = h->dequant4_mf[j];
92             h->unquant4_mf[i] = h->unquant4_mf[j];
93         }
94         else
95         {
96             h->  quant4_mf[i] = x264_malloc(52*size*sizeof(uint16_t) );
97             h->dequant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
98             h->unquant4_mf[i] = x264_malloc(52*size*sizeof(int) );
99         }
100
101         for( j = (i<4 ? 0 : 4); j < i; j++ )
102             if( deadzone[j&3] == deadzone[i&3] &&
103                 !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
104                 break;
105         if( j < i )
106             h->quant4_bias[i] = h->quant4_bias[j];
107         else
108             h->quant4_bias[i] = x264_malloc(52*size*sizeof(uint16_t) );
109     }
110
111     for( q = 0; q < 6; q++ )
112     {
113         for( i = 0; i < 16; i++ )
114         {
115             int j = (i&1) + ((i>>2)&1);
116             def_dequant4[q][i] = dequant4_scale[q][j];
117             def_quant4[q][i]   =   quant4_scale[q][j];
118         }
119         for( i = 0; i < 64; i++ )
120         {
121             int j = quant8_scan[((i>>1)&12) | (i&3)];
122             def_dequant8[q][i] = dequant8_scale[q][j];
123             def_quant8[q][i]   =   quant8_scale[q][j];
124         }
125     }
126
127     for( q = 0; q < 6; q++ )
128     {
129         for( i_list = 0; i_list < 4; i_list++ )
130             for( i = 0; i < 16; i++ )
131             {
132                 h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
133                      quant4_mf[i_list][q][0][i] = DIV(def_quant4[q][i] * 16, h->pps->scaling_list[i_list][i]);
134             }
135         for( i_list = 0; i_list < 2; i_list++ )
136             for( i = 0; i < 64; i++ )
137             {
138                 h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
139                      quant8_mf[i_list][q][0][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
140             }
141     }
142     for( q = 0; q < 52; q++ )
143     {
144         for( i_list = 0; i_list < 4; i_list++ )
145             for( i = 0; i < 16; i++ )
146             {
147                 h->unquant4_mf[i_list][q][i] = (1ULL << (q/6 + 15 + 8)) / quant4_mf[i_list][q%6][0][i];
148                 h->  quant4_mf[i_list][q][i] = j = SHIFT(quant4_mf[i_list][q%6][0][i], q/6 - 1);
149                 // round to nearest, unless that would cause the deadzone to be negative
150                 h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
151                 if( j > 0xffff && q > max_qp_err )
152                     max_qp_err = q;
153             }
154         if( h->param.analyse.b_transform_8x8 )
155         for( i_list = 0; i_list < 2; i_list++ )
156             for( i = 0; i < 64; i++ )
157             {
158                 h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][0][i];
159                 h->  quant8_mf[i_list][q][i] = j = SHIFT(quant8_mf[i_list][q%6][0][i], q/6);
160                 h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
161                 if( j > 0xffff && q > max_qp_err )
162                     max_qp_err = q;
163             }
164     }
165
166     if( !h->mb.b_lossless && max_qp_err >= h->param.rc.i_qp_min )
167     {
168         x264_log( h, X264_LOG_ERROR, "Quantization overflow.\n" );
169         x264_log( h, X264_LOG_ERROR, "Your CQM is incompatible with QP < %d, but min QP is set to %d\n",
170                   max_qp_err+1, h->param.rc.i_qp_min );
171         return -1;
172     }
173     return 0;
174 }
175
176 void x264_cqm_delete( x264_t *h )
177 {
178     int i, j;
179     for( i = 0; i < 6; i++ )
180     {
181         for( j = 0; j < i; j++ )
182             if( h->quant4_mf[i] == h->quant4_mf[j] )
183                 break;
184         if( j == i )
185         {
186             x264_free( h->  quant4_mf[i] );
187             x264_free( h->dequant4_mf[i] );
188             x264_free( h->unquant4_mf[i] );
189         }
190         for( j = 0; j < i; j++ )
191             if( h->quant4_bias[i] == h->quant4_bias[j] )
192                 break;
193         if( j == i )
194             x264_free( h->quant4_bias[i] );
195     }
196 }
197
198 static int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
199                            uint8_t *cqm, const uint8_t *jvt, int length )
200 {
201     char *p;
202     char *nextvar;
203     int i;
204
205     p = strstr( buf, name );
206     if( !p )
207     {
208         memset( cqm, 16, length );
209         return 0;
210     }
211
212     p += strlen( name );
213     if( *p == 'U' || *p == 'V' )
214         p++;
215
216     nextvar = strstr( p, "INT" );
217
218     for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
219     {
220         int coef = -1;
221         sscanf( p, "%d", &coef );
222         if( i == 0 && coef == 0 )
223         {
224             memcpy( cqm, jvt, length );
225             return 0;
226         }
227         if( coef < 1 || coef > 255 )
228         {
229             x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
230             return -1;
231         }
232         cqm[i] = coef;
233     }
234
235     if( (nextvar && p > nextvar) || i != length )
236     {
237         x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
238         return -1;
239     }
240
241     return 0;
242 }
243
244 int x264_cqm_parse_file( x264_t *h, const char *filename )
245 {
246     char *buf, *p;
247     int b_error = 0;
248
249     h->param.i_cqm_preset = X264_CQM_CUSTOM;
250
251     buf = x264_slurp_file( filename );
252     if( !buf )
253     {
254         x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
255         return -1;
256     }
257
258     while( (p = strchr( buf, '#' )) != NULL )
259         memset( p, ' ', strcspn( p, "\n" ) );
260
261     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA",   h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
262     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
263     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA",   h->param.cqm_4py, x264_cqm_jvt4p, 16 );
264     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
265     b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA",   h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
266     b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA",   h->param.cqm_8py, x264_cqm_jvt8p, 64 );
267
268     x264_free( buf );
269     return b_error;
270 }
271