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
*****************************************************************************/
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))
28
static const int dequant4_scale[6][3] =
37
static const int quant4_scale[6][3] =
39
{ 13107, 8066, 5243 },
40
{ 11916, 7490, 4660 },
41
{ 10082, 6554, 4194 },
47
static const int quant8_scan[16] =
49
0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
51
static const int dequant8_scale[6][6] =
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 },
60
static const int quant8_scale[6][6] =
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 }
70
int x264_cqm_init( x264_t *h )
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];
79
int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
80
32 - h->param.analyse.i_luma_deadzone[0],
84
for( i = 0; i < 6; i++ )
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) ) )
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];
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) );
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) ) )
108
h->quant4_bias[i] = h->quant4_bias[j];
110
h->quant4_bias[i] = x264_malloc(52*size*sizeof(uint16_t) );
113
for( q = 0; q < 6; q++ )
115
for( i = 0; i < 16; i++ )
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];
121
for( i = 0; i < 64; i++ )
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];
129
for( q = 0; q < 6; q++ )
131
for( i_list = 0; i_list < 4; i_list++ )
132
for( i = 0; i < 16; i++ )
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]);
137
for( i_list = 0; i_list < 2; i_list++ )
138
for( i = 0; i < 64; i++ )
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]);
144
for( q = 0; q < 52; q++ )
146
for( i_list = 0; i_list < 4; i_list++ )
147
for( i = 0; i < 16; i++ )
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 )
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++ )
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 )
168
if( !h->mb.b_lossless && max_qp_err >= h->param.rc.i_qp_min )
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 );
178
void x264_cqm_delete( x264_t *h )
181
for( i = 0; i < 6; i++ )
183
for( j = 0; j < i; j++ )
184
if( h->quant4_mf[i] == h->quant4_mf[j] )
188
x264_free( h-> quant4_mf[i] );
189
x264_free( h->dequant4_mf[i] );
190
x264_free( h->unquant4_mf[i] );
192
for( j = 0; j < i; j++ )
193
if( h->quant4_bias[i] == h->quant4_bias[j] )
196
x264_free( h->quant4_bias[i] );
200
int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
201
uint8_t *cqm, const uint8_t *jvt, int length )
207
p = strstr( buf, name );
210
memset( cqm, 16, length );
215
if( *p == 'U' || *p == 'V' )
218
nextvar = strstr( p, "INT" );
220
for( i = 0; i < length && (p = strpbrk( p, " \t\n," )) && (p = strpbrk( p, "0123456789" )); i++ )
223
sscanf( p, "%d", &coef );
224
if( i == 0 && coef == 0 )
226
memcpy( cqm, jvt, length );
229
if( coef < 1 || coef > 255 )
231
x264_log( h, X264_LOG_ERROR, "bad coefficient in list '%s'\n", name );
237
if( (nextvar && p > nextvar) || i != length )
239
x264_log( h, X264_LOG_ERROR, "not enough coefficients in list '%s'\n", name );
246
int x264_cqm_parse_file( x264_t *h, const char *filename )
251
h->param.i_cqm_preset = X264_CQM_CUSTOM;
253
buf = x264_slurp_file( filename );
256
x264_log( h, X264_LOG_ERROR, "can't open file '%s'\n", filename );
260
while( (p = strchr( buf, '#' )) != NULL )
261
memset( p, ' ', strcspn( p, "\n" ) );
263
b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA", h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
264
b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
265
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA", h->param.cqm_4py, x264_cqm_jvt4p, 16 );
266
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
267
b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA", h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
268
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA", h->param.cqm_8py, x264_cqm_jvt8p, 64 );