diff -uNrp src/encoder/encoder.c src.new/encoder/encoder.c --- src/encoder/encoder.c 2012-07-19 16:42:32 +0300 +++ src.new/encoder/encoder.c 2012-07-19 16:43:11 +0300 @@ -800,7 +800,7 @@ static int x264_validate_parameters( x26 h->param.analyse.intra &= ~X264_ANALYSE_I8x8; } h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 ); - h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 ); + h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 3 ); 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.i_aq_mode = 0; diff -uNrp src/encoder/ratecontrol.c src.new/encoder/ratecontrol.c --- src/encoder/ratecontrol.c 2012-07-19 11:29:04 +0300 +++ src.new/encoder/ratecontrol.c 2012-07-19 16:44:40 +0300 @@ -301,10 +301,6 @@ static NOINLINE uint32_t x264_ac_energy_ 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. - * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */ - float strength; - float avg_adj = 0.f; /* Initialize frame stats */ for( int i = 0; i < 3; i++ ) { @@ -348,23 +344,30 @@ void x264_adaptive_quant_frame( x264_t * /* Actual adaptive quantization */ else { - if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE ) + /* constants chosen to result in approximately the same overall bitrate as without AQ. + * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */ + float strength; + float avg_adj = 0.f; + float mod_strength = 0.f; + + if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE || h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE_MOD ) { - float bit_depth_correction = powf(1 << (BIT_DEPTH-8), 0.5f); + float bit_depth_correction = 1.f / (1 << (2*(BIT_DEPTH-8))); float avg_adj_pow2 = 0.f; for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ ) for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ ) { uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame ); - float qp_adj = powf( energy + 1, 0.125f ); + float qp_adj = powf( energy * bit_depth_correction + 1, 0.125f ); frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj; avg_adj += qp_adj; avg_adj_pow2 += qp_adj * qp_adj; } avg_adj /= h->mb.i_mb_count; avg_adj_pow2 /= h->mb.i_mb_count; - strength = h->param.rc.f_aq_strength * avg_adj / bit_depth_correction; - avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - (14.f * bit_depth_correction)) / avg_adj; + strength = h->param.rc.f_aq_strength * avg_adj; + avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj; + mod_strength = h->param.rc.f_aq_strength; } else strength = h->param.rc.f_aq_strength * 1.0397f; @@ -374,7 +377,12 @@ 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_AUTOVARIANCE_MOD ) + { + qp_adj = frame->f_qp_offset[mb_xy]; + qp_adj = strength * (qp_adj - avg_adj) + mod_strength * (1.f - 14.f / (qp_adj * qp_adj)); + } + else if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE ) { qp_adj = frame->f_qp_offset[mb_xy]; qp_adj = strength * (qp_adj - avg_adj); diff -uNrp src/x264.c src.new/x264.c --- src/x264.c 2012-07-19 16:42:32 +0300 +++ src.new/x264.c 2012-07-19 16:44:55 +0300 @@ -702,7 +702,8 @@ static void help( x264_param_t *defaults H2( " --aq-mode AQ method [%d]\n" " - 0: Disabled\n" " - 1: Variance AQ (complexity mask)\n" - " - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode ); + " - 2: Auto-variance AQ (experimental)\n" + " - 3: Auto-variance AQ modification\n", defaults->rc.i_aq_mode ); H1( " --aq-strength Reduces blocking and blurring in flat and\n" " textured areas. [%.1f]\n", defaults->rc.f_aq_strength ); H1( " --fade-compensate Allocate more bits to fades [%.1f]\n", defaults->rc.f_fade_compensate ); diff -uNrp src/x264.h src.new/x264.h --- src/x264.h 2012-07-19 16:42:33 +0300 +++ src.new/x264.h 2012-07-19 16:45:07 +0300 @@ -170,6 +170,7 @@ typedef struct #define X264_AQ_NONE 0 #define X264_AQ_VARIANCE 1 #define X264_AQ_AUTOVARIANCE 2 +#define X264_AQ_AUTOVARIANCE_MOD 3 #define X264_B_ADAPT_NONE 0 #define X264_B_ADAPT_FAST 1 #define X264_B_ADAPT_TRELLIS 2