[FFmpeg-devel] [PATCH 2/3 v2] avcodec/utils: add ff_thread_replace_frame()

James Almer jamrial at gmail.com
Wed Aug 3 19:02:47 EEST 2022


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/pthread_frame.c | 30 ++++++++++++++++++++++++++++++
 libavcodec/threadframe.h   |  3 +++
 libavcodec/utils.c         | 21 +++++++++++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index a54d16fee4..ef447ee699 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -1071,6 +1071,36 @@ int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
     return ret;
 }
 
+int ff_thread_replace_frame(AVCodecContext *avctx, ThreadFrame *dst,
+                            const ThreadFrame *src)
+{
+    int ret;
+#if FF_API_THREAD_SAFE_CALLBACKS
+FF_DISABLE_DEPRECATION_WARNINGS
+    int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
+                          THREAD_SAFE_CALLBACKS(avctx);
+FF_ENABLE_DEPRECATION_WARNINGS
+
+    if (!can_direct_free)
+        ff_thread_release_ext_buffer(avctx, dst);
+#endif
+
+    dst->owner[0] = src->owner[0];
+    dst->owner[1] = src->owner[1];
+
+    ret = av_frame_replace(dst->f, src->f);
+    if (ret < 0)
+        return ret;
+
+    ret = av_buffer_replace(&dst->progress, src->progress);
+    if (ret < 0) {
+        ff_thread_release_ext_buffer(dst->owner[0], dst);
+        return ret;
+    }
+
+    return 0;
+}
+
 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
 {
 #if FF_API_THREAD_SAFE_CALLBACKS
diff --git a/libavcodec/threadframe.h b/libavcodec/threadframe.h
index 100e068e06..a8403c8976 100644
--- a/libavcodec/threadframe.h
+++ b/libavcodec/threadframe.h
@@ -84,4 +84,7 @@ void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f);
 
 int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src);
 
+int ff_thread_replace_frame(AVCodecContext *avctx, ThreadFrame *dst,
+                            const ThreadFrame *src);
+
 #endif
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index e73e3a7d08..44ed19458c 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -928,6 +928,27 @@ int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
     return ff_get_buffer(avctx, f->f, flags);
 }
 
+int ff_thread_replace_frame(AVCodecContext *avctx, , ThreadFrame *dst,
+                            const ThreadFrame *src)
+{
+    int ret;
+
+    dst->owner[0] = src->owner[0];
+    dst->owner[1] = src->owner[1];
+
+    ret = av_frame_replace(dst->f, src->f);
+    if (ret < 0)
+        return ret;
+
+    ret = av_buffer_replace(&dst->progress, src->progress);
+    if (ret < 0) {
+        ff_thread_release_ext_buffer(dst->owner[0], dst);
+        return ret;
+    }
+
+    return 0;
+}
+
 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
 {
     if (f)
-- 
2.37.1



More information about the ffmpeg-devel mailing list