[FFmpeg-devel] [PATCH 1/2] lavc/threadframe: allow decoders to attach buffers to ThreadFrame

Anton Khirnov anton at khirnov.net
Tue Mar 15 12:49:31 EET 2022


This may be useful for synchronizing side data that only becomes
available after ff_thread_finish_setup() is called.
---
 libavcodec/pthread_frame.c | 1 +
 libavcodec/threadframe.h   | 3 +++
 libavcodec/utils.c         | 7 +++++++
 3 files changed, 11 insertions(+)

diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 33b5a2e628..4da3832942 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -1138,6 +1138,7 @@ fail:
 void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
 {
     av_buffer_unref(&f->progress);
+    av_buffer_unref(&f->priv_buf);
     f->owner[0] = f->owner[1] = NULL;
     ff_thread_release_buffer(avctx, f->f);
 }
diff --git a/libavcodec/threadframe.h b/libavcodec/threadframe.h
index dea4dadc6d..c2ddc2969f 100644
--- a/libavcodec/threadframe.h
+++ b/libavcodec/threadframe.h
@@ -30,6 +30,9 @@ typedef struct ThreadFrame {
     // progress->data is an array of 2 ints holding progress for top/bottom
     // fields
     AVBufferRef *progress;
+
+    /* arbitrary user data propagated along with the frame */
+    AVBufferRef *priv_buf;
 } ThreadFrame;
 
 /**
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 066da76e16..cc2c2715b3 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -881,6 +881,12 @@ int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
         return AVERROR(ENOMEM);
     }
 
+    if (src->priv_buf) {
+        dst->priv_buf = av_buffer_ref(src->priv_buf);
+        if (!dst->priv_buf)
+            return AVERROR(ENOMEM);
+    }
+
     return 0;
 }
 
@@ -913,6 +919,7 @@ void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
     f->owner[0] = f->owner[1] = NULL;
     if (f->f)
         av_frame_unref(f->f);
+    av_buffer_unref(&f->priv_buf);
 }
 
 void ff_thread_finish_setup(AVCodecContext *avctx)
-- 
2.34.1



More information about the ffmpeg-devel mailing list