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