https://git.videolan.org/?p=ffmpeg.git;a=commit;h=8fe2fec45321d1b82f98f9725ad9118cefcfa58d
https://git.videolan.org/?p=ffmpeg.git;a=commit;h=e5b5dd66535f444451e0fee59247b224d866f334

From 8fe2fec45321d1b82f98f9725ad9118cefcfa58d Mon Sep 17 00:00:00 2001
From: Christopher Degawa <ccom@randomderp.com>
Date: Sun, 20 Nov 2022 13:07:36 -0600
Subject: [PATCH] avcodec/libsvtav1: only set max_buf_sz if both bitrate and
 rc_buf_sz is set

maximum_buffer_size_ms should only be set if both are specified or if
the user sets it through -svtav1-params buf-sz=val

Signed-off-by: Christopher Degawa <ccom@randomderp.com>
--- a/libavcodec/libsvtav1.c
+++ b/libavcodec/libsvtav1.c
@@ -179,7 +179,8 @@ static int config_enc_params(EbSvtAv1EncConfiguration *param,
         param->min_qp_allowed       = avctx->qmin;
     }
     param->max_bit_rate             = avctx->rc_max_rate;
-    param->maximum_buffer_size_ms   = avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
+    if (avctx->bit_rate && avctx->rc_buffer_size)
+        param->maximum_buffer_size_ms = avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
 
     if (svt_enc->crf > 0) {
         param->qp                   = svt_enc->crf;
-- 
2.30.2

From e5b5dd66535f444451e0fee59247b224d866f334 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Jan=20Ekstr=C3=B6m?= <jeebjp@gmail.com>
Date: Thu, 9 Mar 2023 20:27:06 +0200
Subject: [PATCH] avcodec/libsvtav1: use larger of bit rate and max rate for
 buffer size

Generally if maxrate is set, the calculation should be maxrate over
bufsize. This additionally enables CRF + maxrate & bufsize usage.

In order to keep negative values from enabling zero to be treated
as larger and causing a division by zero, check that one of the
variables is larger than zero.
--- a/libavcodec/libsvtav1.c
+++ b/libavcodec/libsvtav1.c
@@ -179,8 +179,10 @@ static int config_enc_params(EbSvtAv1EncConfiguration *param,
         param->min_qp_allowed       = avctx->qmin;
     }
     param->max_bit_rate             = avctx->rc_max_rate;
-    if (avctx->bit_rate && avctx->rc_buffer_size)
-        param->maximum_buffer_size_ms = avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
+    if ((avctx->bit_rate > 0 || avctx->rc_max_rate > 0) && avctx->rc_buffer_size)
+        param->maximum_buffer_size_ms =
+            avctx->rc_buffer_size * 1000LL /
+            FFMAX(avctx->bit_rate, avctx->rc_max_rate);
 
     if (svt_enc->crf > 0) {
         param->qp                   = svt_enc->crf;
@@ -297,7 +299,8 @@ static int config_enc_params(EbSvtAv1EncConfiguration *param,
     avctx->bit_rate       = param->rate_control_mode > 0 ?
                             param->target_bit_rate : 0;
     avctx->rc_max_rate    = param->max_bit_rate;
-    avctx->rc_buffer_size = param->maximum_buffer_size_ms * avctx->bit_rate / 1000LL;
+    avctx->rc_buffer_size = param->maximum_buffer_size_ms *
+                            FFMAX(avctx->bit_rate, avctx->rc_max_rate) / 1000LL;
 
     if (avctx->bit_rate || avctx->rc_max_rate || avctx->rc_buffer_size) {
         AVCPBProperties *cpb_props = ff_add_cpb_side_data(avctx);
-- 
2.30.2
