[FFmpeg-cvslog] avcodec/mpeg12: Make initializing VLCs thread-safe

Andreas Rheinhardt git at videolan.org
Thu Dec 31 17:53:38 EET 2020


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Mon Nov 23 03:10:36 2020 +0100| [902e16d18356025506e6a084dcfb3c0eacf634da] | committer: Andreas Rheinhardt

avcodec/mpeg12: Make initializing VLCs thread-safe

This automatically makes the eamad, eatqi, ipu and mdec decoders
init-threadsafe; in addition to the actual mpeg[12]video decoders,
of course.

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=902e16d18356025506e6a084dcfb3c0eacf634da
---

 libavcodec/eamad.c     |  1 +
 libavcodec/eatqi.c     |  1 +
 libavcodec/mdec.c      |  1 +
 libavcodec/mpeg12.c    | 15 ++++++++-------
 libavcodec/mpeg12dec.c | 11 +++++++----
 5 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/libavcodec/eamad.c b/libavcodec/eamad.c
index 7f28abbafe..9dccddd6d3 100644
--- a/libavcodec/eamad.c
+++ b/libavcodec/eamad.c
@@ -349,4 +349,5 @@ AVCodec ff_eamad_decoder = {
     .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = AV_CODEC_CAP_DR1,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/eatqi.c b/libavcodec/eatqi.c
index 96536b1a08..456bd3c563 100644
--- a/libavcodec/eatqi.c
+++ b/libavcodec/eatqi.c
@@ -189,4 +189,5 @@ AVCodec ff_eatqi_decoder = {
     .close          = tqi_decode_end,
     .decode         = tqi_decode_frame,
     .capabilities   = AV_CODEC_CAP_DR1,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/mdec.c b/libavcodec/mdec.c
index b16cbb6a79..d0bb1162b6 100644
--- a/libavcodec/mdec.c
+++ b/libavcodec/mdec.c
@@ -258,4 +258,5 @@ AVCodec ff_mdec_decoder = {
     .close            = decode_end,
     .decode           = decode_frame,
     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
+    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index b4ef41e12d..2aaed89920 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -30,6 +30,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/timecode.h"
+#include "libavutil/thread.h"
 
 #include "internal.h"
 #include "avcodec.h"
@@ -132,13 +133,8 @@ VLC ff_mb_ptype_vlc;
 VLC ff_mb_btype_vlc;
 VLC ff_mb_pat_vlc;
 
-av_cold void ff_mpeg12_init_vlcs(void)
+static av_cold void mpeg12_init_vlcs(void)
 {
-    static int done = 0;
-
-    if (!done) {
-        done = 1;
-
         INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
                         ff_mpeg12_vlc_dc_lum_bits, 1, 1,
                         ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
@@ -164,7 +160,12 @@ av_cold void ff_mpeg12_init_vlcs(void)
 
         INIT_2D_VLC_RL(ff_rl_mpeg1, 680, 0);
         INIT_2D_VLC_RL(ff_rl_mpeg2, 674, 0);
-    }
+}
+
+av_cold void ff_mpeg12_init_vlcs(void)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, mpeg12_init_vlcs);
 }
 
 /**
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 6d0e9fc7ed..7d797c259a 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2899,7 +2899,8 @@ AVCodec ff_mpeg1video_decoder = {
     .capabilities          = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                              AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
                              AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal         = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                             FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .flush                 = flush,
     .max_lowres            = 3,
     .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context),
@@ -2932,7 +2933,8 @@ AVCodec ff_mpeg2video_decoder = {
     .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                       AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
                       AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .flush          = flush,
     .max_lowres     = 3,
     .profiles       = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
@@ -2976,7 +2978,8 @@ AVCodec ff_mpegvideo_decoder = {
     .close          = mpeg_decode_end,
     .decode         = mpeg_decode_frame,
     .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .flush          = flush,
     .max_lowres     = 3,
 };
@@ -3139,5 +3142,5 @@ AVCodec ff_ipu_decoder = {
     .decode         = ipu_decode_frame,
     .close          = ipu_decode_end,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };



More information about the ffmpeg-cvslog mailing list