From b778271d53928445964cfe801d7461e6db7a67fa Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jmvalin@amazon.com>
Date: Sat, 16 Dec 2023 13:01:20 -0500
Subject: [PATCH] divide max payload too

---
 src/opus_encoder.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/opus_encoder.c b/src/opus_encoder.c
index ba0f7d752..3e35922ef 100644
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -1617,8 +1617,9 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
           frame_to_celt = to_celt && i==nb_frames-1;
           frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
 
-          curr_max = 3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size);
+          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
 #ifdef ENABLE_DRED
+          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
           if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
 #endif
           curr_max = IMIN(max_len_sum-tot_size, curr_max);
-- 
GitLab