From f1db6b9d75574ba292c75fdfee3eeda19745f73e Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jmvalin@amazon.com>
Date: Fri, 20 Jan 2023 15:53:26 -0500
Subject: [PATCH] Only decode the DRED frames we need

---
 src/opus_demo.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/opus_demo.c b/src/opus_demo.c
index af5e30f31..54249b5f9 100644
--- a/src/opus_demo.c
+++ b/src/opus_demo.c
@@ -796,7 +796,12 @@ int main(int argc, char *argv[])
             if (run_decoder)
                 run_decoder += lost_count;
             if (!lost && lost_count > 0) {
-                opus_decoder_dred_input(dec, data, len, 100);
+                opus_int32 output_samples=0;
+                int dred_input;
+                opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples));
+                dred_input = lost_count*output_samples*100/sampling_rate;
+                /* Only decode the amount we need to fill in the gap. */
+                opus_decoder_dred_input(dec, data, len, IMIN(100, IMAX(0, dred_input)));
             }
             /* FIXME: Figure out how to trigger the decoder when the last packet of the file is lost. */
             for (fr=0;fr<run_decoder;fr++) {
-- 
GitLab