diff mbox series

[FFmpeg-devel,29/39] avcodec/mpeg4videoenc: Make encoder init-threadsafe

Message ID 20201210111657.2276739-30-andreas.rheinhardt@gmail.com
State Accepted
Commit 929107c75156af9a403882248032a301380c635e
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/mpeg4videoenc.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

Comments

Anton Khirnov April 17, 2021, 1:59 p.m. UTC | #1
Quoting Andreas Rheinhardt (2020-12-10 12:16:47)
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/mpeg4videoenc.c | 26 ++++++++++++++------------
>  1 file changed, 14 insertions(+), 12 deletions(-)
> 

Looks ok
diff mbox series

Patch

diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index ca1c0dfd9c..8b98a1d507 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -23,6 +23,7 @@ 
 #include "libavutil/attributes.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
+#include "libavutil/thread.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
 #include "h263.h"
@@ -1269,11 +1270,21 @@  static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
     }
 }
 
+static av_cold void mpeg4_encode_init_static(void)
+{
+    init_uni_dc_tab();
+
+    ff_mpeg4_init_rl_intra();
+
+    init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
+    init_uni_mpeg4_rl_tab(&ff_h263_rl_inter,  uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
+}
+
 static av_cold int encode_init(AVCodecContext *avctx)
 {
+    static AVOnce init_static_once = AV_ONCE_INIT;
     MpegEncContext *s = avctx->priv_data;
     int ret;
-    static int done = 0;
 
     if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
         av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
@@ -1283,16 +1294,7 @@  static av_cold int encode_init(AVCodecContext *avctx)
     if ((ret = ff_mpv_encode_init(avctx)) < 0)
         return ret;
 
-    if (!done) {
-        done = 1;
-
-        init_uni_dc_tab();
-
-        ff_mpeg4_init_rl_intra();
-
-        init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
-        init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
-    }
+    ff_thread_once(&init_static_once, mpeg4_encode_init_static);
 
     s->min_qcoeff               = -2048;
     s->max_qcoeff               = 2047;
@@ -1399,6 +1401,6 @@  AVCodec ff_mpeg4_encoder = {
     .close          = ff_mpv_encode_end,
     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .priv_class     = &mpeg4enc_class,
 };