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

James Almer jamrial at gmail.com
Wed Aug 11 01:00:00 EEST 2021


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/pthread_frame.c | 29 +++++++++++++++++++++++++++++
 libavcodec/thread.h        |  2 ++
 libavcodec/utils.c         | 20 ++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 2ff71ca39e..1480269671 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -1162,3 +1162,32 @@ fail:
     }
 #endif
 }
+
+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_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_buffer(dst->owner[0], dst);
+        return ret;
+    }
+
+    return 0;
+}
diff --git a/libavcodec/thread.h b/libavcodec/thread.h
index d7d2ddd8f1..5fef42e16f 100644
--- a/libavcodec/thread.h
+++ b/libavcodec/thread.h
@@ -135,6 +135,8 @@ void ff_thread_release_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);
+
 int ff_thread_init(AVCodecContext *s);
 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx,
         int (*action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr),
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index cfc07cbcb8..cd322ff6a9 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -917,6 +917,26 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
         av_frame_unref(f->f);
 }
 
+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_buffer(dst->owner[0], dst);
+        return ret;
+    }
+
+    return 0;
+}
+
 void ff_thread_finish_setup(AVCodecContext *avctx)
 {
 }
-- 
2.32.0



More information about the ffmpeg-devel mailing list