[vlc-devel] [PATCH 6/6] avcodec: use decoder_UpdateVideoFrameLatency()

Thomas Guillem thomas at gllm.fr
Thu Sep 26 16:38:14 CEST 2019


And fallback to single-threaded mode in case of failure.

As said in cfb320a71bec8d13e51f327f9d799f1541c2b686, failure will happen if the
video decoder is started more than 80ms after the audio output (or more
depending on input pts_delay). This won't happen in most cases since video is
generally started just after or just before the audio.
---
 modules/codec/avcodec/video.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/modules/codec/avcodec/video.c b/modules/codec/avcodec/video.c
index 467ff6c1f9..b323f21526 100644
--- a/modules/codec/avcodec/video.c
+++ b/modules/codec/avcodec/video.c
@@ -477,6 +477,8 @@ static int OpenVideoCodec( decoder_t *p_dec )
         ctx->active_thread_type = FF_THREAD_SLICE;
     }
 
+    for (;;)
+    {
         int ret = ffmpeg_OpenCodec( p_dec, ctx, codec );
         if( ret < 0 )
             return ret;
@@ -484,9 +486,29 @@ static int OpenVideoCodec( decoder_t *p_dec )
         switch( ctx->active_thread_type )
         {
             case FF_THREAD_FRAME:
+            {
+                int dec_ret =
+                    decoder_UpdateVideoFrameLatency(p_dec, ctx->thread_count);
+                if (dec_ret != 0)
+                {
+                    /* Corner case: too late to setup the video latency (the
+                     * audio is likely already started), try again with 1
+                     * thread. */
+
+                    msg_Warn(p_dec, "could not enable frame thread mode with %d"
+                             "threads: the audio output can't cope with this new frame latency",
+                             ctx->thread_count);
+                    msg_Warn(p_dec, "fallback to single thread mode");
+
+                    avcodec_close(ctx);
+                    ctx->thread_count = 1;
+                    ctx->active_thread_type = 0;
+                    continue;
+                }
                 msg_Dbg( p_dec, "using frame thread mode with %d threads",
                          ctx->thread_count );
                 break;
+            }
             case FF_THREAD_SLICE:
                 msg_Dbg( p_dec, "using slice thread mode with %d threads",
                          ctx->thread_count );
@@ -500,7 +522,9 @@ static int OpenVideoCodec( decoder_t *p_dec )
                           ctx->thread_count );
                 break;
         }
-        return 0;
+        return ret;
+    }
+    vlc_assert_unreachable();
 }
 
 /*****************************************************************************
-- 
2.20.1



More information about the vlc-devel mailing list