Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff -uNrp a/Makefile b/Makefile
- --- a/Makefile 2011-08-25 10:27:19 +0800
- +++ b/Makefile 2011-08-25 10:28:58 +0800
- @@ -221,7 +221,7 @@ OPT0 = --crf 30 -b1 -m1 -r1 --me dia --n
- OPT1 = --crf 16 -b2 -m3 -r3 --me hex --no-8x8dct --direct spatial --no-dct-decimate -t0 --slice-max-mbs 50
- OPT2 = --crf 26 -b4 -m5 -r2 --me hex --cqm jvt --nr 100 --psnr --no-mixed-refs --b-adapt 2 --slice-max-size 1500
- OPT3 = --crf 18 -b3 -m9 -r5 --me umh -t1 -A all --b-pyramid normal --direct auto --no-fast-pskip --no-mbtree
- -OPT4 = --crf 22 -b3 -m7 -r4 --me esa -t2 -A all --psy-rd 1.0:1.0 --slices 4 --fgo 8 --fade-compensate 0.5
- +OPT4 = --crf 22 -b3 -m7 -r4 --me esa -t2 -A all --psy-rd 1.0:1.0 --slices 4 --fgo 8 --fade-compensate 0.5 --aq2-strength 0.5
- OPT5 = --frames 50 --crf 24 -b3 -m10 -r3 --me tesa -t2
- OPT6 = --frames 50 -q0 -m9 -r2 --me hex -Aall
- OPT7 = --frames 50 -q0 -m2 -r1 --me hex --no-cabac
- diff -uNrp a/common/common.c b/common/common.c
- --- a/common/common.c 2011-08-25 10:27:19 +0800
- +++ b/common/common.c 2011-08-25 10:32:33 +0800
- @@ -106,8 +106,15 @@ void x264_param_default( x264_param_t *p
- param->rc.i_qp_step = 4;
- param->rc.f_ip_factor = 1.4;
- param->rc.f_pb_factor = 1.3;
- - param->rc.i_aq_mode = X264_AQ_VARIANCE;
- + param->rc.i_aq_mode = X264_AQ_MIX;
- param->rc.f_aq_strength = 1.0;
- + param->rc.f_aq_sensitivity = 10;
- + param->rc.f_aq_ifactor = 1.0;
- + param->rc.f_aq_pfactor = 1.0;
- + param->rc.f_aq_bfactor = 1.0;
- + param->rc.b_aq2 = 0;
- + param->rc.f_aq2_strength = 0.0;
- + param->rc.f_aq2_sensitivity = 15.0;
- param->rc.i_lookahead = 40;
- param->rc.b_stat_write = 0;
- @@ -359,7 +366,7 @@ static int x264_param_apply_tune( x264_p
- else if( !strncasecmp( s, "ssim", 4 ) )
- {
- if( psy_tuning_used++ ) goto psy_failure;
- - param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
- + param->rc.i_aq_mode = X264_AQ_AUTOMIX;
- param->analyse.b_psy = 0;
- }
- else if( !strncasecmp( s, "fastdecode", 10 ) )
- @@ -973,6 +980,21 @@ int x264_param_parse( x264_param_t *p, c
- p->rc.i_aq_mode = atoi(value);
- OPT("aq-strength")
- p->rc.f_aq_strength = atof(value);
- + OPT("aq-sensitivity")
- + p->rc.f_aq_sensitivity = atof(value);
- + OPT("aq-ifactor")
- + p->rc.f_aq_ifactor = atof(value);
- + OPT("aq-pfactor")
- + p->rc.f_aq_pfactor = atof(value);
- + OPT("aq-bfactor")
- + p->rc.f_aq_bfactor = atof(value);
- + OPT("aq2-strength")
- + {
- + p->rc.f_aq2_strength = atof(value);
- + p->rc.b_aq2 = 1;
- + }
- + OPT("aq2-sensitivity")
- + p->rc.f_aq2_sensitivity = atof(value);
- OPT("fgo")
- p->analyse.i_fgo = atoi(value);
- OPT("fade-compensate")
- @@ -1412,7 +1434,19 @@ char *x264_param2string( x264_param_t *p
- s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
- s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
- if( p->rc.i_aq_mode )
- - s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
- + {
- + s += sprintf( s, " aq-strength=%.2f", p->rc.f_aq_strength );
- + s += sprintf( s, " aq-sensitivity=%.2f", p->rc.f_aq_sensitivity );
- + s += sprintf( s, " aq-ifactor=%.2f", p->rc.f_aq_ifactor );
- + s += sprintf( s, " aq-pfactor=%.2f", p->rc.f_aq_pfactor );
- + s += sprintf( s, " aq-bfactor=%.2f", p->rc.f_aq_bfactor );
- + }
- + s += sprintf( s, " aq2=%d", p->rc.b_aq2 );
- + if( p->rc.b_aq2 )
- + {
- + s += sprintf( s, " aq2-strength=%.2f", p->rc.f_aq2_strength );
- + s += sprintf( s, " aq2-sensitivity=%.2f", p->rc.f_aq2_sensitivity );
- + }
- if( p->rc.psz_zones )
- s += sprintf( s, " zones=%s", p->rc.psz_zones );
- else if( p->rc.i_zones )
- diff -uNrp a/common/frame.c b/common/frame.c
- --- a/common/frame.c 2011-08-25 10:24:33 +0800
- +++ b/common/frame.c 2011-08-25 10:33:33 +0800
- @@ -232,6 +232,7 @@ static x264_frame_t *x264_frame_new( x26
- {
- CHECKED_MALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
- CHECKED_MALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
- + CHECKED_MALLOC( frame->f_aq_energy, h->mb.i_mb_count * sizeof(float) );
- if( h->frames.b_have_lowres )
- /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
- CHECKED_MALLOCZERO( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
- @@ -278,6 +279,7 @@ void x264_frame_delete( x264_frame_t *fr
- x264_free( frame->lowres_costs[j][i] );
- x264_free( frame->f_qp_offset );
- x264_free( frame->f_qp_offset_aq );
- + x264_free( frame->f_aq_energy );
- x264_free( frame->i_inv_qscale_factor );
- x264_free( frame->i_row_bits );
- x264_free( frame->f_row_qp );
- diff -uNrp a/common/frame.h b/common/frame.h
- --- a/common/frame.h 2011-08-25 10:24:33 +0800
- +++ b/common/frame.h 2011-08-25 10:34:02 +0800
- @@ -125,6 +125,7 @@ typedef struct x264_frame
- float *f_row_qscale;
- float *f_qp_offset;
- float *f_qp_offset_aq;
- + float *f_aq_energy;
- int b_intra_calculated;
- uint16_t *i_intra_cost;
- uint16_t *i_propagate_cost;
- diff -uNrp a/common/pixel.c b/common/pixel.c
- --- a/common/pixel.c 2011-08-25 10:27:18 +0800
- +++ b/common/pixel.c 2011-08-25 10:35:24 +0800
- @@ -70,6 +70,23 @@ PIXEL_SAD_C( x264_pixel_sad_8x4, 8,
- PIXEL_SAD_C( x264_pixel_sad_4x8, 4, 8 )
- PIXEL_SAD_C( x264_pixel_sad_4x4, 4, 4 )
- +static int x264_pixel_count_8x8( uint8_t *pix, int i_pix, uint32_t threshold )
- +{
- + int sum = 0;
- + for( int y = 0; y < 8; y++, pix += i_pix )
- + for( int x = 0; x < 8; x++ )
- + sum += pix[x] > (uint8_t)threshold;
- + return sum;
- +}
- +
- +static int x264_pixel_count_16x16( uint8_t *pix, int i_pix, uint32_t threshold )
- +{
- + int sum = 0;
- + for( int y = 0; y < 16; y++, pix += i_pix )
- + for( int x = 0; x < 16; x++ )
- + sum += pix[x] > (uint8_t)threshold;
- + return sum;
- +}
- /****************************************************************************
- * pixel_ssd_WxH
- @@ -849,6 +866,8 @@ void x264_pixel_init( int cpu, x264_pixe
- pixf->ssim_end4 = ssim_end4;
- pixf->var2_8x8 = pixel_var2_8x8;
- pixf->vsad = pixel_vsad;
- + pixf->count_8x8 = x264_pixel_count_8x8;
- + pixf->count_16x16 = x264_pixel_count_16x16;
- pixf->intra_sad_x3_4x4 = x264_intra_sad_x3_4x4;
- pixf->intra_satd_x3_4x4 = x264_intra_satd_x3_4x4;
- diff -uNrp a/common/pixel.h b/common/pixel.h
- --- a/common/pixel.h 2011-08-25 10:27:18 +0800
- +++ b/common/pixel.h 2011-08-25 10:35:55 +0800
- @@ -109,6 +109,9 @@ typedef struct
- int (*ads[7])( int enc_dc[4], uint16_t *sums, int delta,
- uint16_t *cost_mvx, int16_t *mvs, int width, int thresh );
- + int (*count_8x8) ( uint8_t *pix, int i_pix, uint32_t threshold );
- + int (*count_16x16)( uint8_t *pix, int i_pix, uint32_t threshold );
- +
- /* calculate satd or sad of V, H, and DC modes.
- * may be NULL, in which case just use pred+satd instead. */
- void (*intra_mbcmp_x3_16x16)( pixel *fenc, pixel *fdec , int res[3] );
- diff -uNrp a/encoder/encoder.c b/encoder/encoder.c
- --- a/encoder/encoder.c 2011-08-25 10:27:19 +0800
- +++ b/encoder/encoder.c 2011-08-25 10:37:42 +0800
- @@ -585,6 +585,7 @@ static int x264_validate_parameters( x26
- h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, QP_MAX );
- h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, QP_MAX );
- h->param.rc.i_aq_mode = 0;
- + h->param.rc.b_aq2 = 0;
- h->param.rc.b_mb_tree = 0;
- h->param.rc.i_bitrate = 0;
- }
- @@ -773,9 +774,15 @@ static int x264_validate_parameters( x26
- h->param.rc.f_fade_compensate = 0;
- h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 );
- - h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 );
- - if( h->param.rc.f_aq_strength == 0 )
- + h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, -3, 3 );
- + h->param.rc.b_aq2 = h->param.rc.b_aq2 && h->param.rc.f_aq2_strength > 0;
- + if( h->param.rc.f_aq_strength == 0 && (h->param.rc.i_aq_mode > 0 ? !h->param.rc.b_aq2 : 1) )
- h->param.rc.i_aq_mode = 0;
- + if( h->param.rc.f_aq_sensitivity < 0 )
- + h->param.rc.f_aq_sensitivity = 0;
- + h->param.rc.f_aq_ifactor = x264_clip3f( h->param.rc.f_aq_ifactor, -10, 10 );
- + h->param.rc.f_aq_pfactor = x264_clip3f( h->param.rc.f_aq_pfactor, -10, 10 );
- + h->param.rc.f_aq_bfactor = x264_clip3f( h->param.rc.f_aq_bfactor, -10, 10 );
- if( h->param.i_log_level < X264_LOG_INFO && (!h->param.psz_log_file || h->param.i_log_file_level < X264_LOG_INFO) )
- {
- diff -uNrp a/encoder/ratecontrol.c b/encoder/ratecontrol.c
- --- a/encoder/ratecontrol.c 2011-08-25 10:27:19 +0800
- +++ b/encoder/ratecontrol.c 2011-08-25 10:49:11 +0800
- @@ -280,6 +280,15 @@ static NOINLINE uint32_t x264_ac_energy_
- return var;
- }
- +static int x264_sum_dctq( int16_t dct[64] )
- +{
- + int t = 0;
- + int16_t *p = &dct[0];
- + for( int i = 1; i < 64; i++ )
- + t += abs(p[i]) * x264_dct8_weight_tab[i];
- + return t;
- +}
- +
- void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_offsets )
- {
- /* constants chosen to result in approximately the same overall bitrate as without AQ.
- @@ -315,6 +324,7 @@ void x264_adaptive_quant_frame( x264_t *
- for( int mb_xy = 0; mb_xy < h->mb.i_mb_count; mb_xy++ )
- frame->i_inv_qscale_factor[mb_xy] = 256;
- }
- + memset( frame->f_aq_energy, 0, h->mb.i_mb_count * sizeof(float) );
- }
- /* Need variance data for weighted prediction */
- if( h->param.analyse.i_weighted_pred )
- @@ -329,7 +339,7 @@ void x264_adaptive_quant_frame( x264_t *
- /* Actual adaptive quantization */
- else
- {
- - if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
- + if( h->param.rc.i_aq_mode == X264_AQ_AUTOMIX )
- {
- float bit_depth_correction = powf(1 << (BIT_DEPTH-8), 0.5f);
- float avg_adj_pow2 = 0.f;
- @@ -355,7 +365,7 @@ void x264_adaptive_quant_frame( x264_t *
- {
- float qp_adj;
- int mb_xy = mb_x + mb_y*h->mb.i_mb_stride;
- - if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
- + if( h->param.rc.i_aq_mode == X264_AQ_AUTOMIX )
- {
- qp_adj = frame->f_qp_offset[mb_xy];
- qp_adj = strength * (qp_adj - avg_adj);
- @@ -363,7 +373,9 @@ void x264_adaptive_quant_frame( x264_t *
- else
- {
- uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
- - qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - (14.427f + 2*(BIT_DEPTH-8)));
- + float f_aq_energy = x264_log2( X264_MAX(energy, 1) ) - (4.427f + 2*(BIT_DEPTH-8));
- + qp_adj = strength * (f_aq_energy - h->param.rc.f_aq_sensitivity);
- + frame->f_aq_energy[mb_xy] = f_aq_energy;
- }
- if( quant_offsets )
- qp_adj += quant_offsets[mb_xy];
- @@ -420,6 +432,8 @@ int x264_macroblock_tree_read( x264_t *h
- frame->i_inv_qscale_factor[i] = x264_exp2fix8(frame->f_qp_offset[i]);
- }
- rc->qpbuf_pos--;
- +
- + memset( frame->f_aq_energy, 0, h->mb.i_mb_count * sizeof(float) );
- }
- else
- x264_stack_align( x264_adaptive_quant_frame, h, frame, quant_offsets );
- @@ -1521,19 +1535,82 @@ int x264_ratecontrol_qp( x264_t *h )
- return x264_clip3( h->rc->qpm + 0.5f, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
- }
- +static NOINLINE float x264_haali_adaptive_quant( x264_t *h, float *_energy )
- +{
- + ALIGNED_16( static uint8_t zero[FDEC_STRIDE*8] );
- + ALIGNED_16( int16_t dct[64] );
- + float fc, qp_adj;
- + int total = 0;
- + int new_qp;
- +
- + if( h->mb.i_qp <= 10 ) /* AQ is probably not needed at such low QP */
- + return 0;
- +
- + if( h->pixf.sad[PIXEL_16x16](h->mb.pic.p_fenc[0], FENC_STRIDE, zero, 16) > 64*16*16 ) /* light places? */
- + {
- + int (*count_func)( uint8_t *, int, uint32_t ) = CHROMA444 ? h->pixf.count_16x16 : h->pixf.count_8x8;
- + if( count_func(h->mb.pic.p_fenc[1], FENC_STRIDE, 0x81818181) < 40 /* not enough "blue" pixels? */ ||
- + count_func(h->mb.pic.p_fenc[2], FENC_STRIDE, 0x87878787) > 24 /* too many "red" pixels? */ )
- + {
- + x264_emms();
- + return 0;
- + }
- + }
- +
- + for( int i = 0; i < 4; i++ )
- + {
- + h->dctf.sub8x8_dct8( dct, h->mb.pic.p_fenc[0] + (i&1)*8 + (i>>1)*FENC_STRIDE, zero );
- + total += x264_sum_dctq( dct );
- + }
- +
- + x264_emms();
- +
- + if( total == 0 ) /* no AC coefficients, nothing to do */
- + return 0;
- +
- + *_energy = fc = expf( -5e-13 * total * total );
- +
- + /* the function is chosen such that it stays close to 0 in almost all
- + * range of 0..1, and rapidly goes up to 1 near 1.0 */
- + qp_adj = h->rc->qpm * h->param.rc.f_aq2_strength / pow( 2 - fc, h->param.rc.f_aq2_sensitivity );
- +
- + /* don't adjust by more than this amount */
- + qp_adj = X264_MIN( qp_adj, h->rc->qpm / 2.f );
- +
- + new_qp = x264_clip3( h->rc->qpm - qp_adj, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
- +
- + return new_qp - h->rc->qpm;
- +}
- +
- int x264_ratecontrol_mb_qp( x264_t *h )
- {
- x264_emms();
- float qp = h->rc->qpm;
- - if( h->param.rc.i_aq_mode )
- + if( !h->param.rc.i_aq_mode )
- + return x264_clip3( qp + 0.5f, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
- +
- + /* MB-tree currently doesn't adjust quantizers in unreferenced frames. */
- + float qp_offset = h->fdec->b_kept_as_ref ? h->fenc->f_qp_offset[h->mb.i_mb_xy] : h->fenc->f_qp_offset_aq[h->mb.i_mb_xy];
- +
- + float haq_energy = 0.f;
- + if( h->param.rc.b_aq2 )
- {
- - /* MB-tree currently doesn't adjust quantizers in unreferenced frames. */
- - float qp_offset = h->fdec->b_kept_as_ref ? h->fenc->f_qp_offset[h->mb.i_mb_xy] : h->fenc->f_qp_offset_aq[h->mb.i_mb_xy];
- - /* Scale AQ's effect towards zero in emergency mode. */
- - if( qp > QP_MAX_SPEC )
- - qp_offset *= (QP_MAX - qp) / (QP_MAX - QP_MAX_SPEC);
- - qp += qp_offset;
- + float haq_offset = x264_haali_adaptive_quant( h, &haq_energy );
- + if( qp_offset >= 0.f )
- + qp_offset += haq_offset;
- + else
- + qp_offset = X264_MIN( qp_offset, haq_offset );
- }
- + qp_offset *= h->sh.i_type == SLICE_TYPE_I ? h->param.rc.f_aq_ifactor
- + : h->sh.i_type == SLICE_TYPE_P ? h->param.rc.f_aq_pfactor
- + : h->sh.i_type == SLICE_TYPE_B ? h->param.rc.f_aq_bfactor
- + : 1.f;
- +
- + /* Scale AQ's effect towards zero in emergency mode. */
- + if( qp > QP_MAX_SPEC )
- + qp_offset *= (QP_MAX - qp) / (QP_MAX - QP_MAX_SPEC);
- + qp += qp_offset;
- +
- return x264_clip3( qp + 0.5f, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
- }
- diff -uNrp a/x264.c b/x264.c
- --- a/x264.c 2011-08-25 10:27:19 +0800
- +++ b/x264.c 2011-08-25 10:44:53 +0800
- @@ -694,6 +694,19 @@ static void help( x264_param_t *defaults
- " - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode );
- H1( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
- " textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
- + H1( " --aq-sensitivity <float> \"Center\" of AQ curve. [%.1f]\n"
- + " - 5: most QPs are raised\n"
- + " - 10: good general-use sensitivity\n"
- + " - 15: most QPs are lowered\n", defaults->rc.f_aq_sensitivity );
- + H2( " --aq-ifactor <float> AQ strength factor of I-frames [%.2f]\n", defaults->rc.f_aq_ifactor );
- + H2( " --aq-pfactor <float> AQ strength factor of P-frames [%.2f]\n", defaults->rc.f_aq_pfactor );
- + H2( " --aq-bfactor <float> AQ strength factor of B-frames [%.2f]\n", defaults->rc.f_aq_bfactor );
- + H1( " --aq2-strength <float> Use 2nd AQ (Haali's AQ) algorithm for support. [%.1f]\n"
- + " 0.0: no 2nd AQ\n"
- + " 1.1: strong 2nd AQ\n", defaults->rc.f_aq2_strength );
- + H1( " --aq2-sensitivity <float> \"Flatness\" threshold to trigger 2nd AQ [%.1f]\n"
- + " 5: applies to almost all blocks\n"
- + " 22: only flat blocks\n", defaults->rc.f_aq2_sensitivity );
- H1( " --fade-compensate <float> Allocate more bits to fades [%.1f]\n", defaults->rc.f_fade_compensate );
- H2( " Approximate sane range: 0.0 - 1.0\n" );
- H1( "\n" );
- @@ -1062,7 +1075,13 @@ static struct option long_options[] =
- { "no-fast-pskip", no_argument, NULL, 0 },
- { "no-dct-decimate", no_argument, NULL, 0 },
- { "aq-strength", required_argument, NULL, 0 },
- + { "aq-sensitivity", required_argument, NULL, 0 },
- + { "aq-ifactor", required_argument, NULL, 0 },
- + { "aq-pfactor", required_argument, NULL, 0 },
- + { "aq-bfactor", required_argument, NULL, 0 },
- { "aq-mode", required_argument, NULL, 0 },
- + { "aq2-strength", required_argument, NULL, 0 },
- + { "aq2-sensitivity", required_argument, NULL, 0 },
- { "fgo", required_argument, NULL, 0 },
- { "fade-compensate", required_argument, NULL, 0 },
- { "deadzone-inter", required_argument, NULL, 0 },
- diff -uNrp a/x264.h b/x264.h
- --- a/x264.h 2011-08-25 10:27:19 +0800
- +++ b/x264.h 2011-08-25 10:46:39 +0800
- @@ -42,6 +42,7 @@
- #include "x264_config.h"
- #define X264_BUILD 116
- +#define X264_SUB_BUILD 1 // MixAQ
- /* x264_t:
- * opaque handler for encoder */
- @@ -150,8 +151,8 @@ typedef struct
- #define X264_RC_ABR 2
- #define X264_QP_AUTO 0
- #define X264_AQ_NONE 0
- -#define X264_AQ_VARIANCE 1
- -#define X264_AQ_AUTOVARIANCE 2
- +#define X264_AQ_MIX 1
- +#define X264_AQ_AUTOMIX 2
- #define X264_B_ADAPT_NONE 0
- #define X264_B_ADAPT_FAST 1
- #define X264_B_ADAPT_TRELLIS 2
- @@ -388,6 +389,13 @@ typedef struct x264_param_t
- int i_aq_mode; /* psy adaptive QP. (X264_AQ_*) */
- float f_aq_strength;
- + float f_aq_sensitivity;
- + float f_aq_ifactor;
- + float f_aq_pfactor;
- + float f_aq_bfactor;
- + int b_aq2; /* psy 2nd adaptive QP */
- + float f_aq2_strength;
- + float f_aq2_sensitivity;
- float f_fade_compensate; /* Give more bits to fades. */
- int b_mb_tree; /* Macroblock-tree ratecontrol. */
- int i_lookahead;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement