[FFmpeg-devel] [PATCH v2 063/162] avcodec/mobiclip: Make decoder init-threadsafe

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 20 09:19:36 EET 2020


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mobiclip.c | 47 +++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/libavcodec/mobiclip.c b/libavcodec/mobiclip.c
index d188e1cd57..ce9bcf6afb 100644
--- a/libavcodec/mobiclip.c
+++ b/libavcodec/mobiclip.c
@@ -23,6 +23,7 @@
 #include <inttypes.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/thread.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
@@ -274,8 +275,32 @@ typedef struct MobiClipContext {
 static VLC rl_vlc[2];
 static VLC mv_vlc[2][16];
 
+static av_cold void mobiclip_init_static(void)
+{
+    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[0], MOBI_RL_VLC_BITS, 104,
+                                 bits0, sizeof(*bits0),
+                                 syms0, sizeof(*syms0), sizeof(*syms0),
+                                 0, 0, 1 << MOBI_RL_VLC_BITS);
+    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[1], MOBI_RL_VLC_BITS, 104,
+                                 bits0, sizeof(*bits0),
+                                 syms1, sizeof(*syms1), sizeof(*syms1),
+                                 0, 0, 1 << MOBI_RL_VLC_BITS);
+    for (int i = 0; i < 2; i++) {
+        static VLC_TYPE vlc_buf[2 * 16 << MOBI_MV_VLC_BITS][2];
+        for (int j = 0; j < 16; j++) {
+            mv_vlc[i][j].table           = &vlc_buf[(16 * i + j) << MOBI_MV_VLC_BITS];
+            mv_vlc[i][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
+            ff_init_vlc_from_lengths(&mv_vlc[i][j], MOBI_MV_VLC_BITS, mv_len[j],
+                                     mv_bits[i][j], sizeof(*mv_bits[i][j]),
+                                     mv_syms[i][j], sizeof(*mv_syms[i][j]), sizeof(*mv_syms[i][j]),
+                                     0, INIT_VLC_USE_NEW_STATIC, NULL);
+        }
+    }
+}
+
 static av_cold int mobiclip_init(AVCodecContext *avctx)
 {
+    static AVOnce init_static_once = AV_ONCE_INIT;
     MobiClipContext *s = avctx->priv_data;
 
     if (avctx->width & 15 || avctx->height & 15) {
@@ -298,25 +323,7 @@ static av_cold int mobiclip_init(AVCodecContext *avctx)
             return AVERROR(ENOMEM);
     }
 
-    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[0], MOBI_RL_VLC_BITS, 104,
-                                 bits0, sizeof(*bits0),
-                           syms0,  sizeof(*syms0),  sizeof(*syms0),
-                                 0, 0, 1 << MOBI_RL_VLC_BITS);
-    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[1], MOBI_RL_VLC_BITS, 104,
-                                 bits0, sizeof(*bits0),
-                           syms1,  sizeof(*syms1),  sizeof(*syms1),
-                                 0, 0, 1 << MOBI_RL_VLC_BITS);
-    for (int i = 0; i < 2; i++) {
-        static VLC_TYPE vlc_buf[2 * 16 << MOBI_MV_VLC_BITS][2];
-        for (int j = 0; j < 16; j++) {
-            mv_vlc[i][j].table           = &vlc_buf[(16 * i + j) << MOBI_MV_VLC_BITS];
-            mv_vlc[i][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
-            ff_init_vlc_from_lengths(&mv_vlc[i][j], MOBI_MV_VLC_BITS, mv_len[j],
-                                     mv_bits[i][j], sizeof(*mv_bits[i][j]),
-                                     mv_syms[i][j], sizeof(*mv_syms[i][j]), sizeof(*mv_syms[i][j]),
-                                     0, INIT_VLC_USE_NEW_STATIC, NULL);
-        }
-    }
+    ff_thread_once(&init_static_once, mobiclip_init_static);
 
     return 0;
 }
@@ -1341,5 +1348,5 @@ AVCodec ff_mobiclip_decoder = {
     .flush          = mobiclip_flush,
     .close          = mobiclip_close,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
 };
-- 
2.25.1



More information about the ffmpeg-devel mailing list