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