[FFmpeg-cvslog] avcodec: Do not lock during open for codecs marked as having threadsafe init

Derek Buitenhuis git at videolan.org
Mon Oct 5 11:33:54 CEST 2015


ffmpeg | branch: master | Derek Buitenhuis <derek.buitenhuis at gmail.com> | Thu Oct  1 15:36:56 2015 +0100| [eae58428bd199f7d4670bf471f56ed204430878e] | committer: Luca Barbato

avcodec: Do not lock during open for codecs marked as having threadsafe init

Signed-off-by: Derek Buitenhuis <derek.buitenhuis at gmail.com>
Signed-off-by: Luca Barbato <lu_zero at gentoo.org>

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

 libavcodec/utils.c |   38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 1721c09..a1aece7 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -845,20 +845,21 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         av_dict_copy(&tmp, *options, 0);
 
     /* If there is a user-supplied mutex locking routine, call it. */
-    if (lockmgr_cb) {
-        if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
-            return -1;
-    }
+    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
+        if (lockmgr_cb) {
+            if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
+                return -1;
+        }
 
-    entangled_thread_counter++;
-    if (entangled_thread_counter != 1 &&
-        !(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Insufficient thread locking. At least %d threads are "
-               "calling avcodec_open2() at the same time right now.\n",
-               entangled_thread_counter);
-        ret = -1;
-        goto end;
+        entangled_thread_counter++;
+        if (entangled_thread_counter != 1) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Insufficient thread locking. At least %d threads are "
+                   "calling avcodec_open2() at the same time right now.\n",
+                   entangled_thread_counter);
+            ret = -1;
+            goto end;
+        }
     }
 
     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
@@ -1085,12 +1086,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
 #endif
     }
 end:
-    entangled_thread_counter--;
+    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
+        entangled_thread_counter--;
 
-    /* Release any user-supplied mutex. */
-    if (lockmgr_cb) {
-        (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
+        /* Release any user-supplied mutex. */
+        if (lockmgr_cb) {
+            (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
+        }
     }
+
     if (options) {
         av_dict_free(options);
         *options = tmp;



More information about the ffmpeg-cvslog mailing list