[FFmpeg-devel] [RFC/PATCH] avcodec: deprecate thread_safe_callbacks

Anton Khirnov anton at khirnov.net
Fri May 22 17:04:41 EEST 2020


They add considerable complexity to frame-threading implementation,
which includes an unavoidably leaking error path, while the advantages
of this option to the users are highly dubious.

It should be always possible and desirable for the callers to make their
get_buffer2() implementation thread-safe, so deprecate this option.
---
The patch is incomplete and not ready to be pushed, I am mainly
gathering opinions. Is anyone aware of a valid use case for
thread_safe_callbacks?
---
 libavcodec/avcodec.h |  7 +++++++
 libavcodec/utils.c   | 13 +++++++++++++
 libavcodec/version.h |  3 +++
 3 files changed, 23 insertions(+)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index f10b7a06ec..2dec0d8ca0 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1934,6 +1934,7 @@ typedef struct AVCodecContext {
      */
     int active_thread_type;
 
+#if FF_API_THREAD_SAFE_CALLBACKS
     /**
      * Set by the client if its custom get_buffer() callback can be called
      * synchronously from another thread, which allows faster multithreaded decoding.
@@ -1941,8 +1942,14 @@ typedef struct AVCodecContext {
      * Ignored if the default get_buffer() is used.
      * - encoding: Set by user.
      * - decoding: Set by user.
+     *
+     * @deprecated the custom get_buffer2() callback should always be
+     *   thread-safe. Thread-unsafe get_buffer2() implementations will be
+     *   invalid once this field is removed.
      */
+    attribute_deprecated
     int thread_safe_callbacks;
+#endif
 
     /**
      * The codec may call this to execute several independent things.
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 3255679550..10d5e552c2 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -673,6 +673,19 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         goto free_and_end;
     }
 
+#if FF_API_THREAD_SAFE_CALLBACKS
+FF_DISABLE_DEPRECATION_WARNINGS
+    if ((avctx->thread_type & FF_THREAD_FRAME) &&
+        avctx->get_buffer2 != avcodec_default_get_buffer2 &&
+        !avctx->thread_safe_callbacks) {
+        av_log(avctx, AV_LOG_WARNING, "Requested frame threading with a "
+               "custom get_buffer2() implementation which is not marked as "
+               "thread safe. This is not supported anymore, make your "
+               "callback thread-safe.\n");
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
     avctx->codec = codec;
     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
         avctx->codec_id == AV_CODEC_ID_NONE) {
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 9fc637313d..5b4dfea579 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -144,6 +144,9 @@
 #ifndef FF_API_UNUSED_CODEC_CAPS
 #define FF_API_UNUSED_CODEC_CAPS   (LIBAVCODEC_VERSION_MAJOR < 59)
 #endif
+#ifndef FF_API_THREAD_SAFE_CALLBACKS
+#define FF_API_THREAD_SAFE_CALLBACKS (LIBAVCODEC_VERSION_MAJOR < 59)
+#endif
 
 
 #endif /* AVCODEC_VERSION_H */
-- 
2.26.2



More information about the ffmpeg-devel mailing list