1 /*****************************************************************************
2 * set.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2005 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
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.
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.
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 *****************************************************************************/
27 static const int dequant4_scale[6][3] =
36 static const int quant4_scale[6][3] =
38 { 13107, 8066, 5243 },
39 { 11916, 7490, 4660 },
40 { 10082, 6554, 4194 },
46 static const int quant8_scan[16] =
48 0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
50 static const int dequant8_scale[6][6] =
52 { 20, 18, 32, 19, 25, 24 },
53 { 22, 19, 35, 21, 28, 26 },
54 { 26, 23, 42, 24, 33, 31 },
55 { 28, 25, 45, 26, 35, 33 },
56 { 32, 28, 51, 30, 40, 38 },
57 { 36, 32, 58, 34, 46, 43 },
59 static const int quant8_scale[6][6] =
61 { 13107, 11428, 20972, 12222, 16777, 15481 },
62 { 11916, 10826, 19174, 11058, 14980, 14290 },
63 { 10082, 8943, 15978, 9675, 12710, 11985 },
64 { 9362, 8228, 14913, 8931, 11984, 11259 },
65 { 8192, 7346, 13159, 7740, 10486, 9777 },
66 { 7282, 6428, 11570, 6830, 9118, 8640 }
69 void x264_cqm_init( x264_t *h )
71 int def_quant4[6][16];
72 int def_quant8[6][64];
73 int def_dequant4[6][16];
74 int def_dequant8[6][64];
77 for( i = 0; i < 6; i++ )
79 int size = i<4 ? 16 : 64;
80 for( j = (i<4 ? 0 : 4); j < i; j++ )
81 if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
85 h-> quant4_mf[i] = h-> quant4_mf[j];
86 h->dequant4_mf[i] = h->dequant4_mf[j];
87 h->unquant4_mf[i] = h->unquant4_mf[j];
91 h-> quant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
92 h->dequant4_mf[i] = x264_malloc( 6*size*sizeof(int) );
93 h->unquant4_mf[i] = x264_malloc(52*size*sizeof(int) );
97 for( q = 0; q < 6; q++ )
99 for( i = 0; i < 16; i++ )
101 int j = (i&1) + ((i>>2)&1);
102 def_dequant4[q][i] = dequant4_scale[q][j];
103 def_quant4[q][i] = quant4_scale[q][j];
105 for( i = 0; i < 64; i++ )
107 int j = quant8_scan[((i>>1)&12) | (i&3)];
108 def_dequant8[q][i] = dequant8_scale[q][j];
109 def_quant8[q][i] = quant8_scale[q][j];
113 for( q = 0; q < 6; q++ )
115 for( i_list = 0; i_list < 4; i_list++ )
116 for( i = 0; i < 16; i++ )
118 h->dequant4_mf[i_list][q][0][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
119 h-> quant4_mf[i_list][q][0][i] = def_quant4[q][i] * 16 / h->pps->scaling_list[i_list][i];
121 for( i_list = 0; i_list < 2; i_list++ )
122 for( i = 0; i < 64; i++ )
124 h->dequant8_mf[i_list][q][0][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
125 h-> quant8_mf[i_list][q][0][i] = def_quant8[q][i] * 16 / h->pps->scaling_list[4+i_list][i];
128 for( q = 0; q < 52; q++ )
130 for( i_list = 0; i_list < 4; i_list++ )
131 for( i = 0; i < 16; i++ )
132 h->unquant4_mf[i_list][q][i] = (1 << (q/6 + 15 + 8)) / h->quant4_mf[i_list][q%6][0][i];
133 for( i_list = 0; i_list < 2; i_list++ )
134 for( i = 0; i < 64; i++ )
135 h->unquant8_mf[i_list][q][i] = (1 << (q/6 + 16 + 8)) / h->quant8_mf[i_list][q%6][0][i];
139 void x264_cqm_delete( x264_t *h )
142 for( i = 0; i < 6; i++ )
144 for( j = 0; j < i; j++ )
145 if( h->quant4_mf[i] == h->quant4_mf[j] )
149 x264_free( h-> quant4_mf[i] );
150 x264_free( h->dequant4_mf[i] );
151 x264_free( h->unquant4_mf[i] );
156 int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
157 uint8_t *cqm, const uint8_t *jvt, int length )
163 p = strstr( buf, name );
166 memset( cqm, 16, length );
171 if( *p == 'U' || *p == 'V' )
174 nextvar = strstr( p, "INT" );
176 for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
179 sscanf( p, "%d", &coef );
180 if( i == 0 && coef == 0 )
182 memcpy( cqm, jvt, length );
185 if( coef < 1 || coef > 255 )
187 x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
193 if( (nextvar && p > nextvar) || i != length )
195 x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
202 int x264_cqm_parse_file( x264_t *h, const char *filename )
207 h->param.i_cqm_preset = X264_CQM_CUSTOM;
209 buf = x264_slurp_file( filename );
212 x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
216 while( (p = strchr( buf, '#' )) != NULL )
217 memset( p, ' ', strcspn( p, "\n" ) );
219 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA", h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
220 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
221 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA", h->param.cqm_4py, x264_cqm_jvt4p, 16 );
222 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
223 b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA", h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
224 b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA", h->param.cqm_8py, x264_cqm_jvt8p, 64 );