History

avcodec/pthread_frame: Add API to share RefStruct refs just once

This is useful when the lifetime of the object to be shared is the whole decoding process as it allows to avoid having to sync them every time in update_thread_context.

Andreas Rheinhardt committed 3 years ago

Commit 9dc8f881 parent 267a7633 files · +57 −0Browse files

Showing the changes to libavcodec (3 of 3 files). Show all

Unified · Split

3 files
  1. libavcodec/decode.c +7 −0
  2. libavcodec/pthread_frame.c +20 −0
  3. libavcodec/thread.h +30 −0
libavcodec/decode.c+7−0
@@ -1747,6 +1747,13 @@ void ff_progress_frame_await(const ProgressFrame *f, int n) · 1746 unmodified lines
17471747 ff_thread_progress_await(&f->progress->progress, n);
17481748 }
17491749
1750+#if !HAVE_THREADS
1751+enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1752+{
1753+ return FF_THREAD_NO_FRAME_THREADING;
1754+}
1755+#endif /* !HAVE_THREADS */
1756+
17501757 static av_cold int progress_frame_pool_init_cb(FFRefStructOpaque opaque, void *obj)
17511758 {
17521759 const AVCodecContext *avctx = opaque.nc;
libavcodec/pthread_frame.c+20−0
@@ -1013,3 +1013,23 @@ void ff_thread_release_ext_buffer(ThreadFrame *f) · 1012 unmodified lines
10131013 if (f->f)
10141014 av_frame_unref(f->f);
10151015 }
1016+
1017+enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1018+{
1019+ PerThreadContext *p;
1020+ const void *ref;
1021+
1022+ if (!avctx->internal->is_copy)
1023+ return avctx->active_thread_type & FF_THREAD_FRAME ?
1024+ FF_THREAD_IS_FIRST_THREAD : FF_THREAD_NO_FRAME_THREADING;
1025+
1026+ p = avctx->internal->thread_ctx;
1027+
1028+ av_assert1(memcpy(&ref, (char*)avctx->priv_data + offset, sizeof(ref)) && ref == NULL);
1029+
1030+ memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
1031+ av_assert1(ref);
1032+ ff_refstruct_replace((char*)avctx->priv_data + offset, ref);
1033+
1034+ return FF_THREAD_IS_COPY;
1035+}
libavcodec/thread.h+30−0
@@ -84,4 +84,34 @@ int ff_slice_thread_init_progress(AVCodecContext *avctx); · 83 unmodified lines
8484 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n);
8585 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift);
8686
87+enum ThreadingStatus {
88+ FF_THREAD_IS_COPY,
89+ FF_THREAD_IS_FIRST_THREAD,
90+ FF_THREAD_NO_FRAME_THREADING,
91+};
92+
93+/**
94+ * Allows to synchronize objects whose lifetime is the whole decoding
95+ * process among all frame threads.
96+ *
97+ * When called from a non-copy thread, do nothing.
98+ * When called from another thread, place a new RefStruct reference
99+ * at the given offset in the calling thread's private data from
100+ * the RefStruct reference in the private data of the first decoding thread.
101+ * The first thread must have a valid RefStruct reference at the given
102+ * offset in its private data; the calling thread must not have
103+ * a reference at this offset in its private data (must be NULL).
104+ *
105+ * @param avctx an AVCodecContext
106+ * @param offset offset of the RefStruct reference in avctx's private data
107+ *
108+ * @retval FF_THREAD_IS_COPY if frame-threading is in use and the
109+ * calling thread is a copy; in this case, the RefStruct reference
110+ * will be set.
111+ * @retval FF_THREAD_IS_MAIN_THREAD if frame-threading is in use
112+ * and the calling thread is the main thread.
113+ * @retval FF_THREAD_NO_FRAME_THREADING if frame-threading is not in use.
114+ */
115+enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset);
116+
87117 #endif /* AVCODEC_THREAD_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t