[FFmpeg-devel] [PATCH 3/3] h264: Run VLC init under pthread_once

Derek Buitenhuis derek.buitenhuis at gmail.com
Wed Oct 7 17:39:47 CEST 2015


This makes the h.264 decoder threadsafe to initialize.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis at gmail.com>
---
 libavcodec/h264.c |  7 ++++++-
 libavcodec/h264.h | 10 ++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 3209c9c..27a1d32 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -657,7 +657,11 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
     if (!avctx->has_b_frames)
         h->low_delay = 1;
 
-    ff_h264_decode_init_vlc();
+    ret = pthread_once(&ff_h264_vlc_init, &ff_h264_decode_init_vlc);
+    if (ret != 0) {
+        av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
+        return AVERROR_UNKNOWN;
+    }
 
     if (avctx->codec_id == AV_CODEC_ID_H264) {
         if (avctx->ticks_per_frame == 1) {
@@ -1993,6 +1997,7 @@ AVCodec ff_h264_decoder = {
     .capabilities          = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
                              AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
                              AV_CODEC_CAP_FRAME_THREADS,
+    .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE,
     .flush                 = flush_dpb,
     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
     .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index 769abda..c5119f9 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -43,6 +43,14 @@
 #include "rectangle.h"
 #include "videodsp.h"
 
+#if HAVE_PTHREADS
+#   include <pthread.h>
+#elif HAVE_OS2THREADS
+#   include "compat/os2threads.h"
+#elif HAVE_W32THREADS
+#   include "compat/w32pthreads.h"
+#endif
+
 #define H264_MAX_PICTURE_COUNT 36
 #define H264_MAX_THREADS       32
 
@@ -931,6 +939,8 @@ int ff_h264_check_intra_pred_mode(const H264Context *h, H264SliceContext *sl,
 void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl);
 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size);
 int ff_h264_decode_init(AVCodecContext *avctx);
+
+static pthread_once_t ff_h264_vlc_init = PTHREAD_ONCE_INIT;
 void ff_h264_decode_init_vlc(void);
 
 /**
-- 
1.8.3.1



More information about the ffmpeg-devel mailing list