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) : (s)==0 ? (x) : ((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][4][4];
75 int quant8_mf[2][6][8][8];
77 int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
78 32 - h->param.analyse.i_luma_deadzone[0],
82 for( i = 0; i < 6; i++ )
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) ) )
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];
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) );
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) ) )
106 h->quant4_bias[i] = h->quant4_bias[j];
108 h->quant4_bias[i] = x264_malloc(52*size*sizeof(uint16_t) );
111 for( q = 0; q < 6; q++ )
113 for( i = 0; i < 16; i++ )
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];
119 for( i = 0; i < 64; i++ )
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];
127 for( q = 0; q < 6; q++ )
129 for( i_list = 0; i_list < 4; i_list++ )
130 for( i = 0; i < 16; i++ )
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]);
135 for( i_list = 0; i_list < 2; i_list++ )
136 for( i = 0; i < 64; i++ )
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]);
142 for( q = 0; q < 52; q++ )
144 for( i_list = 0; i_list < 4; i_list++ )
145 for( i = 0; i < 16; i++ )
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 )
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++ )
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 )
166 if( !h->mb.b_lossless && max_qp_err >= h->param.rc.i_qp_min )
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 );
176 void x264_cqm_delete( x264_t *h )
179 for( i = 0; i < 6; i++ )
181 for( j = 0; j < i; j++ )
182 if( h->quant4_mf[i] == h->quant4_mf[j] )
186 x264_free( h-> quant4_mf[i] );
187 x264_free( h->dequant4_mf[i] );
188 x264_free( h->unquant4_mf[i] );
190 for( j = 0; j < i; j++ )
191 if( h->quant4_bias[i] == h->quant4_bias[j] )
194 x264_free( h->quant4_bias[i] );
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 )
205 p = strstr( buf, name );
208 memset( cqm, 16, length );
213 if( *p == 'U' || *p == 'V' )
216 nextvar = strstr( p, "INT" );
218 for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
221 sscanf( p, "%d", &coef );
222 if( i == 0 && coef == 0 )
224 memcpy( cqm, jvt, length );
227 if( coef < 1 || coef > 255 )
229 x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
235 if( (nextvar && p > nextvar) || i != length )
237 x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
244 int x264_cqm_parse_file( x264_t *h, const char *filename )
249 h->param.i_cqm_preset = X264_CQM_CUSTOM;
251 buf = x264_slurp_file( filename );
254 x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
258 while( (p = strchr( buf, '#' )) != NULL )
259 memset( p, ' ', strcspn( p, "\n" ) );
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 );