From ec49e0eb8dca680e10ad3916ea34aaaca45a539b Mon Sep 17 00:00:00 2001
From: bUd <daoko@protonmail.com>
Date: Wed, 12 Apr 2023 09:23:57 +0200
Subject: [PATCH] Add AQ bias strength parameter

---
 common/base.c         | 9 ++++++++-
 encoder/ratecontrol.c | 2 +-
 x264.c                | 2 ++
 x264.h                | 1 +
 4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/common/base.c b/common/base.c
index d302fed1..f4e10312 100644
--- a/common/base.c
+++ b/common/base.c
@@ -414,6 +414,7 @@ REALIGN_STACK void x264_param_default( x264_param_t *param )
     param->rc.f_pb_factor = 1.3;
     param->rc.i_aq_mode = X264_AQ_VARIANCE;
     param->rc.f_aq_strength = 1.0;
+    param->rc.f_aq_bias_strength = 1.0;
     param->rc.i_lookahead = 40;
 
     param->rc.b_stat_write = 0;
@@ -1346,6 +1347,8 @@ REALIGN_STACK int x264_param_parse( x264_param_t *p, const char *name, const cha
         p->rc.i_aq_mode = atoi(value);
     OPT("aq-strength")
         p->rc.f_aq_strength = atof(value);
+    OPT("aq-strength")
+        p->rc.f_aq_bias_strength = atof(value);
     OPT("pass")
     {
         int pass = x264_clip3( atoi(value), 0, 3 );
@@ -1556,7 +1559,11 @@ char *x264_param2string( x264_param_t *p, int b_res )
             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, ":%.2f", p->rc.f_aq_strength );
+                if( p->rc.i_aq_mode == X264_AQ_AUTOVARIANCE_BIASED )
+                    s += sprintf( s, ":%.2f", p->rc.f_aq_bias_strength );
+            }
         if( p->rc.psz_zones )
             s += sprintf( s, " zones=%s", p->rc.psz_zones );
         else if( p->rc.i_zones )
diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
index ff8f27f5..e3a590b7 100644
--- a/encoder/ratecontrol.c
+++ b/encoder/ratecontrol.c
@@ -368,7 +368,7 @@ void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_off
             avg_adj_pow2 /= h->mb.i_mb_count;
             strength = h->param.rc.f_aq_strength * avg_adj;
             avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj;
-            bias_strength = h->param.rc.f_aq_strength;
+            bias_strength = h->param.rc.f_aq_strength * h->param.rc.f_aq_bias_strength;
         }
         else
             strength = h->param.rc.f_aq_strength * 1.0397f;
diff --git a/x264.c b/x264.c
index 5a8cb363..39ef0c8c 100644
--- a/x264.c
+++ b/x264.c
@@ -751,6 +751,7 @@ static void help( x264_param_t *defaults, int longhelp )
         "                                  - 3: Auto-variance AQ with bias to dark scenes\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-bias-strength <float> Adjust the bias to darks strength in AQ mode 3 [%.1f]\n", defaults->rc.f_aq_bias_strength );
     H1( "\n" );
     H0( "  -p, --pass <integer>        Enable multipass ratecontrol\n"
         "                                  - 1: First pass, creates stats file\n"
@@ -1092,6 +1093,7 @@ static struct option long_options[] =
     { "no-dct-decimate",      no_argument,       NULL, 0 },
     { "aq-strength",          required_argument, NULL, 0 },
     { "aq-mode",              required_argument, NULL, 0 },
+    { "aq-bias-strength",     required_argument, NULL, 0 },
     { "deadzone-inter",       required_argument, NULL, 0 },
     { "deadzone-intra",       required_argument, NULL, 0 },
     { "level",                required_argument, NULL, 0 },
diff --git a/x264.h b/x264.h
index b8619d4b..8119ed6e 100644
--- a/x264.h
+++ b/x264.h
@@ -454,6 +454,7 @@ typedef struct x264_param_t
 
         int         i_aq_mode;      /* psy adaptive QP. (X264_AQ_*) */
         float       f_aq_strength;
+        float       f_aq_bias_strength; /* Fine-tune AQ mode 3 dark bias. */
         int         b_mb_tree;      /* Macroblock-tree ratecontrol. */
         int         i_lookahead;
 
-- 
GitLab