[FFmpeg-cvslog] avcodec/msmpeg4enc: Make msmpeg4 encoders init-threadsafe

Andreas Rheinhardt git at videolan.org
Tue Apr 27 02:30:12 EEST 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Thu Dec 10 07:26:31 2020 +0100| [f4d16c3865b478b3619021bbf5b9827f99a8f710] | committer: Andreas Rheinhardt

avcodec/msmpeg4enc: Make msmpeg4 encoders init-threadsafe

Reviewed-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f4d16c3865b478b3619021bbf5b9827f99a8f710
---

 libavcodec/mpegvideo_enc.c |  6 +++---
 libavcodec/msmpeg4enc.c    | 33 +++++++++++++++++++--------------
 libavcodec/wmv2enc.c       |  2 +-
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 0d86c0a2cd..d4ddd41035 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -4795,7 +4795,7 @@ AVCodec ff_msmpeg4v2_encoder = {
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .priv_class     = &msmpeg4v2_class,
 };
@@ -4816,7 +4816,7 @@ AVCodec ff_msmpeg4v3_encoder = {
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .priv_class     = &msmpeg4v3_class,
 };
@@ -4837,7 +4837,7 @@ AVCodec ff_wmv1_encoder = {
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .priv_class     = &wmv1_class,
 };
diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c
index e22e8107ad..73a5ea09dd 100644
--- a/libavcodec/msmpeg4enc.c
+++ b/libavcodec/msmpeg4enc.c
@@ -32,6 +32,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avutil.h"
+#include "libavutil/thread.h"
 #include "mpegvideo.h"
 #include "h263.h"
 #include "internal.h"
@@ -72,7 +73,9 @@ void ff_msmpeg4_code012(PutBitContext *pb, int n)
     }
 }
 
-static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
+static int get_size_of_code(const RLTable *rl, int last, int run,
+                            int level, int intra)
+{
     int size=0;
     int code;
     int run_diff= intra ? 0 : 1;
@@ -113,21 +116,10 @@ static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run,
     return size;
 }
 
-av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
+static av_cold void msmpeg4_encode_init_static(void)
 {
-    static int init_done=0;
     int i;
-
-    ff_msmpeg4_common_init(s);
-    if(s->msmpeg4_version>=4){
-        s->min_qcoeff= -255;
-        s->max_qcoeff=  255;
-    }
-
-    if (!init_done) {
         static uint16_t mv_index_tables[2][4096];
-        /* init various encoding tables */
-        init_done = 1;
         init_mv_table(&ff_mv_tables[0], mv_index_tables[0]);
         init_mv_table(&ff_mv_tables[1], mv_index_tables[1]);
 
@@ -138,12 +130,25 @@ av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
                 for(run=0; run<=MAX_RUN; run++){
                     int last;
                     for(last=0; last<2; last++){
-                        rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[  i], last, run, level, 0);
+                        rl_length[i][level][run][last] = get_size_of_code(&ff_rl_table[i], last, run, level, 0);
                     }
                 }
             }
         }
+}
+
+av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+
+    ff_msmpeg4_common_init(s);
+    if (s->msmpeg4_version >= 4) {
+        s->min_qcoeff = -255;
+        s->max_qcoeff =  255;
     }
+
+    /* init various encoding tables */
+    ff_thread_once(&init_static_once, msmpeg4_encode_init_static);
 }
 
 static void find_best_tables(MpegEncContext * s)
diff --git a/libavcodec/wmv2enc.c b/libavcodec/wmv2enc.c
index 312dbc0834..781873a4b3 100644
--- a/libavcodec/wmv2enc.c
+++ b/libavcodec/wmv2enc.c
@@ -231,7 +231,7 @@ AVCodec ff_wmv2_encoder = {
     .init           = wmv2_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                      AV_PIX_FMT_NONE },
 };



More information about the ffmpeg-cvslog mailing list