From d38ddd7270ffaea705981b6a48086778850d3c96 Mon Sep 17 00:00:00 2001
From: Steve Lhomme <robux4@ycbcr.xyz>
Date: Mon, 21 Mar 2022 15:53:52 +0100
Subject: [PATCH] dav1d: limit the number of extra frames needed by the decoder

The i_extra_picture_buffers is used to add pictures to the pool that the core
will allocate. dav1d is actually using n_threads frames. And the core is
allocating 10 frames per default for AV1. So we need to add the missing ones.

(cherry picked from commit a32031dc0f5f32083fc54a21397bce732742ccbe) (rebased)
rebased:
- the code dav1d 1.0.0 in 3.0 uses different max versions

Signed-off-by: Steve Lhomme <robux4@ycbcr.xyz>
---
 modules/codec/dav1d.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/modules/codec/dav1d.c b/modules/codec/dav1d.c
index cfabbc27cb3..8a439ce4ff4 100644
--- a/modules/codec/dav1d.c
+++ b/modules/codec/dav1d.c
@@ -304,7 +304,28 @@ static int OpenDecoder(vlc_object_t *p_this)
     p_sys->s.n_threads = var_InheritInteger(p_this, "dav1d-thread-frames");
     if (p_sys->s.n_threads == 0)
         p_sys->s.n_threads = (i_core_count < 16) ? i_core_count : 16;
-#else
+
+#if DAV1D_API_VERSION_MAJOR > 6 || DAV1D_API_VERSION_MINOR >= 7
+    // after dav1d 1.0.0
+    p_sys->s.max_frame_delay = dav1d_get_frame_delay( &p_sys->s );
+#else // 1.0.0
+    // corresponds to c->n_fc when max_frame_delay is 0 in dav1d 1.0.0
+    static const uint8_t fc_lut[49] = {
+        1,                                     /*     1 */
+        2, 2, 2,                               /*  2- 4 */
+        3, 3, 3, 3, 3,                         /*  5- 9 */
+        4, 4, 4, 4, 4, 4, 4,                   /* 10-16 */
+        5, 5, 5, 5, 5, 5, 5, 5, 5,             /* 17-25 */
+        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,       /* 26-36 */
+        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 37-49 */
+    };
+    if (p_sys->s.n_threads >= 50)
+        p_sys->s.max_frame_delay = 8;
+    else
+        p_sys->s.max_frame_delay = fc_lut[p_sys->s.n_threads - 1];
+#endif
+
+#else // before dav1d 1.0.0
     p_sys->s.n_tile_threads = var_InheritInteger(p_this, "dav1d-thread-tiles");
     if (p_sys->s.n_tile_threads == 0)
         p_sys->s.n_tile_threads =
@@ -329,7 +350,7 @@ static int OpenDecoder(vlc_object_t *p_this)
     msg_Dbg(p_this, "Using dav1d version %s with %d threads",
             dav1d_version(), p_sys->s.n_threads);
 
-    dec->i_extra_picture_buffers = (p_sys->s.n_threads - 1);
+    dec->i_extra_picture_buffers = p_sys->s.max_frame_delay;
 #else
     msg_Dbg(p_this, "Using dav1d version %s with %d/%d frame/tile threads",
             dav1d_version(), p_sys->s.n_frame_threads, p_sys->s.n_tile_threads);
-- 
GitLab

