/*****************************************************************************
* set.c: quantization init
*****************************************************************************
- * Copyright (C) 2005-2011 x264 project
+ * Copyright (C) 2005-2016 x264 project
*
* Authors: Loren Merritt <lorenm@u.washington.edu>
*
int def_dequant4[6][16];
int def_dequant8[6][64];
int quant4_mf[4][6][16];
- int quant8_mf[2][6][64];
+ int quant8_mf[4][6][64];
int deadzone[4] = { 32 - h->param.analyse.i_luma_deadzone[1],
32 - h->param.analyse.i_luma_deadzone[0],
32 - 11, 32 - 21 };
int max_qp_err = -1;
int max_chroma_qp_err = -1;
int min_qp_err = QP_MAX+1;
+ int num_8x8_lists = h->sps->i_chroma_format_idc == CHROMA_444 ? 4
+ : h->param.analyse.b_transform_8x8 ? 2 : 0; /* Checkasm may segfault if optimized out by --chroma-format */
- for( int i = 0; i < 6; i++ )
- {
- int size = i<4 ? 16 : 64;
- int j;
- for( j = (i<4 ? 0 : 4); j < i; j++ )
- if( !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
- break;
- if( j < i )
- {
- h-> quant4_mf[i] = h-> quant4_mf[j];
- h->dequant4_mf[i] = h->dequant4_mf[j];
- h->unquant4_mf[i] = h->unquant4_mf[j];
- }
- else
- {
- CHECKED_MALLOC( h-> quant4_mf[i], (QP_MAX+1)*size*sizeof(udctcoef) );
- CHECKED_MALLOC( h->dequant4_mf[i], 6*size*sizeof(int) );
- CHECKED_MALLOC( h->unquant4_mf[i], (QP_MAX+1)*size*sizeof(int) );
- }
-
- for( j = (i<4 ? 0 : 4); j < i; j++ )
- if( deadzone[j&3] == deadzone[i&3] &&
- !memcmp( h->pps->scaling_list[i], h->pps->scaling_list[j], size*sizeof(uint8_t) ) )
- break;
- if( j < i )
- h->quant4_bias[i] = h->quant4_bias[j];
- else
- CHECKED_MALLOC( h->quant4_bias[i], (QP_MAX+1)*size*sizeof(udctcoef) );
+#define CQM_ALLOC( w, count )\
+ for( int i = 0; i < count; i++ )\
+ {\
+ int size = w*w;\
+ int start = w == 8 ? 4 : 0;\
+ int j;\
+ for( j = 0; j < i; j++ )\
+ if( !memcmp( h->pps->scaling_list[i+start], h->pps->scaling_list[j+start], size*sizeof(uint8_t) ) )\
+ break;\
+ if( j < i )\
+ {\
+ h-> quant##w##_mf[i] = h-> quant##w##_mf[j];\
+ h->dequant##w##_mf[i] = h->dequant##w##_mf[j];\
+ h->unquant##w##_mf[i] = h->unquant##w##_mf[j];\
+ }\
+ else\
+ {\
+ CHECKED_MALLOC( h-> quant##w##_mf[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
+ CHECKED_MALLOC( h->dequant##w##_mf[i], 6*size*sizeof(int) );\
+ CHECKED_MALLOC( h->unquant##w##_mf[i], (QP_MAX_SPEC+1)*size*sizeof(int) );\
+ }\
+ for( j = 0; j < i; j++ )\
+ if( deadzone[j] == deadzone[i] &&\
+ !memcmp( h->pps->scaling_list[i+start], h->pps->scaling_list[j+start], size*sizeof(uint8_t) ) )\
+ break;\
+ if( j < i )\
+ {\
+ h->quant##w##_bias[i] = h->quant##w##_bias[j];\
+ h->quant##w##_bias0[i] = h->quant##w##_bias0[j];\
+ }\
+ else\
+ {\
+ CHECKED_MALLOC( h->quant##w##_bias[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
+ CHECKED_MALLOC( h->quant##w##_bias0[i], (QP_MAX_SPEC+1)*size*sizeof(udctcoef) );\
+ }\
}
+ CQM_ALLOC( 4, 4 )
+ CQM_ALLOC( 8, num_8x8_lists )
+
for( int q = 0; q < 6; q++ )
{
for( int i = 0; i < 16; i++ )
h->dequant4_mf[i_list][q][i] = def_dequant4[q][i] * h->pps->scaling_list[i_list][i];
quant4_mf[i_list][q][i] = DIV(def_quant4[q][i] * 16, h->pps->scaling_list[i_list][i]);
}
- for( int i_list = 0; i_list < 2; i_list++ )
+ for( int i_list = 0; i_list < num_8x8_lists; i_list++ )
for( int i = 0; i < 64; i++ )
{
h->dequant8_mf[i_list][q][i] = def_dequant8[q][i] * h->pps->scaling_list[4+i_list][i];
quant8_mf[i_list][q][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
}
}
- for( int q = 0; q < QP_MAX+1; q++ )
+ for( int q = 0; q <= QP_MAX_SPEC; q++ )
{
int j;
for( int i_list = 0; i_list < 4; i_list++ )
}
// round to nearest, unless that would cause the deadzone to be negative
h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
+ h->quant4_bias0[i_list][q][i] = (1<<15)/j;
if( j > 0xffff && q > max_qp_err && (i_list == CQM_4IY || i_list == CQM_4PY) )
max_qp_err = q;
if( j > 0xffff && q > max_chroma_qp_err && (i_list == CQM_4IC || i_list == CQM_4PC) )
max_chroma_qp_err = q;
}
if( h->param.analyse.b_transform_8x8 )
- for( int i_list = 0; i_list < 2; i_list++ )
+ for( int i_list = 0; i_list < num_8x8_lists; i_list++ )
for( int i = 0; i < 64; i++ )
{
h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][i];
continue;
}
h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
- if( j > 0xffff && q > max_qp_err )
+ h->quant8_bias0[i_list][q][i] = (1<<15)/j;
+ if( j > 0xffff && q > max_qp_err && (i_list == CQM_8IY || i_list == CQM_8PY) )
max_qp_err = q;
+ if( j > 0xffff && q > max_chroma_qp_err && (i_list == CQM_8IC || i_list == CQM_8PC) )
+ max_chroma_qp_err = q;
}
}
x264_emms();
CHECKED_MALLOC( h->nr_offset_emergency, sizeof(*h->nr_offset_emergency)*(QP_MAX-QP_MAX_SPEC) );
for( int q = 0; q < QP_MAX - QP_MAX_SPEC; q++ )
- for( int cat = 0; cat <= 2; cat++ )
+ for( int cat = 0; cat < 3 + CHROMA444; cat++ )
{
- int dct8x8 = cat == 1;
+ int dct8x8 = cat&1;
+ if( !h->param.analyse.b_transform_8x8 && dct8x8 )
+ continue;
+
int size = dct8x8 ? 64 : 16;
udctcoef *nr_offset = h->nr_offset_emergency[q][cat];
/* Denoise chroma first (due to h264's chroma QP offset), then luma, then DC. */
continue;
}
- int thresh = i == 0 ? dc_threshold : cat == 2 ? chroma_threshold : luma_threshold;
+ int thresh = i == 0 ? dc_threshold : cat >= 2 ? chroma_threshold : luma_threshold;
if( q < thresh )
{
nr_offset[i] = 0;
if( !h->mb.b_lossless )
{
- while( h->chroma_qp_table[h->param.rc.i_qp_min] <= max_chroma_qp_err )
+ while( h->chroma_qp_table[SPEC_QP(h->param.rc.i_qp_min)] <= max_chroma_qp_err )
h->param.rc.i_qp_min++;
if( min_qp_err <= h->param.rc.i_qp_max )
h->param.rc.i_qp_max = min_qp_err-1;
}
#define CQM_DELETE( n, max )\
- for( int i = 0; i < max; i++ )\
+ for( int i = 0; i < (max); i++ )\
{\
int j;\
for( j = 0; j < i; j++ )\
if( h->quant##n##_bias[i] == h->quant##n##_bias[j] )\
break;\
if( j == i )\
+ {\
x264_free( h->quant##n##_bias[i] );\
+ x264_free( h->quant##n##_bias0[i] );\
+ }\
}
void x264_cqm_delete( x264_t *h )
{
CQM_DELETE( 4, 4 );
- CQM_DELETE( 8, 2 );
+ CQM_DELETE( 8, CHROMA444 ? 4 : 2 );
x264_free( h->nr_offset_emergency );
}
static int x264_cqm_parse_jmlist( x264_t *h, const char *buf, const char *name,
- uint8_t *cqm, const uint8_t *jvt, int length )
+ uint8_t *cqm, const uint8_t *jvt, int length )
{
int i;
memset( p, ' ', strcspn( p, "\n" ) );
b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_LUMA", h->param.cqm_4iy, x264_cqm_jvt4i, 16 );
- b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_LUMA", h->param.cqm_4py, x264_cqm_jvt4p, 16 );
+ b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA4X4_CHROMA", h->param.cqm_4ic, x264_cqm_jvt4i, 16 );
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER4X4_CHROMA", h->param.cqm_4pc, x264_cqm_jvt4p, 16 );
b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_LUMA", h->param.cqm_8iy, x264_cqm_jvt8i, 64 );
b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_LUMA", h->param.cqm_8py, x264_cqm_jvt8p, 64 );
+ if( CHROMA444 )
+ {
+ b_error |= x264_cqm_parse_jmlist( h, buf, "INTRA8X8_CHROMA", h->param.cqm_8ic, x264_cqm_jvt8i, 64 );
+ b_error |= x264_cqm_parse_jmlist( h, buf, "INTER8X8_CHROMA", h->param.cqm_8pc, x264_cqm_jvt8p, 64 );
+ }
x264_free( buf );
return b_error;