1 /*****************************************************************************
2 * set.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2005-2008 Loren Merritt <lorenm@u.washington.edu>
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.
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.
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 *****************************************************************************/
23 #define SHIFT(x,s) ((s)<=0 ? (x)<<-(s) : ((x)+(1<<((s)-1)))>>(s))
24 #define DIV(n,d) (((n) + ((d)>>1)) / (d))
26 static const int dequant4_scale[6][3] =
35 static const int quant4_scale[6][3] =
37 { 13107, 8066, 5243 },
38 { 11916, 7490, 4660 },
39 { 10082, 6554, 4194 },
45 static const int quant8_scan[16] =
47 0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
49 static const int dequant8_scale[6][6] =
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 },
58 static const int quant8_scale[6][6] =
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 }
68 int x264_cqm_init( x264_t *h )
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][16];
75 int quant8_mf[2][6][64];
77 int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
78 32 - h->param.analyse.i_luma_deadzone[0],
81 int max_chroma_qp_err = -1;
83 for( i = 0; i < 6; i++ )
85 int size = i<4 ? 16 : 64;
86 for( j = (i<4 ? 0 : 4); j < i; j++ )
87 if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
91 h-> quant4_mf[i] = h-> quant4_mf[j];
92 h->dequant4_mf[i] = h->dequant4_mf[j];
93 h->unquant4_mf[i] = h->unquant4_mf[j];
97 CHECKED_MALLOC( h-> quant4_mf[i], 52*size*sizeof(uint16_t) );
98 CHECKED_MALLOC( h->dequant4_mf[i], 6*size*sizeof(int) );
99 CHECKED_MALLOC( h->unquant4_mf[i], 52*size*sizeof(int) );
102 for( j = (i<4 ? 0 : 4); j < i; j++ )
103 if( deadzone[j&3] == deadzone[i&3] &&
104 !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
107 h->quant4_bias[i] = h->quant4_bias[j];
109 CHECKED_MALLOC( h->quant4_bias[i], 52*size*sizeof(uint16_t) );
112 for( q = 0; q < 6; q++ )
114 for( i = 0; i < 16; i++ )
116 int j = (i&1) + ((i>>2)&1);
117 def_dequant4[q][i] = dequant4_scale[q][j];
118 def_quant4[q][i] = quant4_scale[q][j];
120 for( i = 0; i < 64; i++ )
122 int j = quant8_scan[((i>>1)&12) | (i&3)];
123 def_dequant8[q][i] = dequant8_scale[q][j];
124 def_quant8[q][i] = quant8_scale[q][j];
128 for( q = 0; q < 6; q++ )
130 for( i_list = 0; i_list < 4; i_list++ )
131 for( i = 0; i < 16; i++ )
133 h->dequant4_mf[i_list][q][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
134 quant4_mf[i_list][q][i] = DIV(def_quant4[q][i] * 16, h->pps->scaling_list[i_list][i]);
136 for( i_list = 0; i_list < 2; i_list++ )
137 for( i = 0; i < 64; i++ )
139 h->dequant8_mf[i_list][q][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
140 quant8_mf[i_list][q][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
143 for( q = 0; q < 52; q++ )
145 for( i_list = 0; i_list < 4; i_list++ )
146 for( i = 0; i < 16; i++ )
148 h->unquant4_mf[i_list][q][i] = (1ULL << (q/6 + 15 + 8)) / quant4_mf[i_list][q%6][i];
149 h-> quant4_mf[i_list][q][i] = j = SHIFT(quant4_mf[i_list][q%6][i], q/6 - 1);
150 // round to nearest, unless that would cause the deadzone to be negative
151 h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
152 if( j > 0xffff && q > max_qp_err && (i_list == CQM_4IY || i_list == CQM_4PY) )
154 if( j > 0xffff && q > max_chroma_qp_err && (i_list == CQM_4IC || i_list == CQM_4PC) )
155 max_chroma_qp_err = q;
157 if( h->param.analyse.b_transform_8x8 )
158 for( i_list = 0; i_list < 2; i_list++ )
159 for( i = 0; i < 64; i++ )
161 h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][i];
162 h-> quant8_mf[i_list][q][i] = j = SHIFT(quant8_mf[i_list][q%6][i], q/6);
163 h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
164 if( j > 0xffff && q > max_qp_err )
169 if( !h->mb.b_lossless && max_qp_err >= h->param.rc.i_qp_min )
171 x264_log( h, X264_LOG_ERROR, "Quantization overflow. Your CQM is incompatible with QP < %d,\n", max_qp_err+1 );
172 x264_log( h, X264_LOG_ERROR, "but min QP is set to %d.\n", h->param.rc.i_qp_min );
175 if( !h->mb.b_lossless && max_chroma_qp_err >= h->chroma_qp_table[h->param.rc.i_qp_min] )
177 x264_log( h, X264_LOG_ERROR, "Quantization overflow. Your CQM is incompatible with QP < %d,\n", max_chroma_qp_err+1 );
178 x264_log( h, X264_LOG_ERROR, "but min chroma QP is implied to be %d.\n", h->chroma_qp_table[h->param.rc.i_qp_min] );
183 x264_cqm_delete( h );
187 #define CQM_DELETE( n, max )\
188 for( i = 0; i < max; i++ )\
190 for( j = 0; j < i; j++ )\
191 if( h->quant##n##_mf[i] == h->quant##n##_mf[j] )\
195 x264_free( h-> quant##n##_mf[i] );\
196 x264_free( h->dequant##n##_mf[i] );\
197 x264_free( h->unquant##n##_mf[i] );\
199 for( j = 0; j < i; j++ )\
200 if( h->quant##n##_bias[i] == h->quant##n##_bias[j] )\
203 x264_free( h->quant##n##_bias[i] );\
206 void x264_cqm_delete( x264_t *h )
213 static int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
214 uint8_t *cqm, const uint8_t *jvt, int length )
220 p = strstr( buf, name );
223 memset( cqm, 16, length );
228 if( *p == 'U' || *p == 'V' )
231 nextvar = strstr( p, "INT" );
233 for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
236 sscanf( p, "%d", &coef );
237 if( i == 0 && coef == 0 )
239 memcpy( cqm, jvt, length );
242 if( coef < 1 || coef > 255 )
244 x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
250 if( (nextvar && p > nextvar) || i != length )
252 x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
259 int x264_cqm_parse_file( x264_t *h, const char *filename )
264 h->param.i_cqm_preset = X264_CQM_CUSTOM;
266 buf = x264_slurp_file( filename );
269 x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
273 while( (p = strchr( buf, '#' )) != NULL )
274 memset( p, ' ', strcspn( p, "\n" ) );
276 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA", h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
277 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
278 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA", h->param.cqm_4py, x264_cqm_jvt4p, 16 );
279 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
280 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA", h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
281 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA", h->param.cqm_8py, x264_cqm_jvt8p, 64 );