diff mbox series

[FFmpeg-devel,34/39] avcodec/msmpeg4enc: Make msmpeg4 encoders init-threadsafe

Message ID 20201210111657.2276739-35-andreas.rheinhardt@gmail.com
State New
Headers show
Series Make mpegvideo encoders init-threadsafe
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 10, 2020, 11:16 a.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mpegvideo_enc.c |  6 +++---
 libavcodec/msmpeg4enc.c    | 33 +++++++++++++++++++--------------
 libavcodec/wmv2enc.c       |  2 +-
 3 files changed, 23 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 8f4876b0ca..e3f217c22d 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -4836,7 +4836,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,
 };
@@ -4857,7 +4857,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,
 };
@@ -4878,7 +4878,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 6d9911c2fb..3fd4d38820 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 },
 };