* [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
@ 2022-09-02 20:59 Anton Khirnov
2022-09-02 21:12 ` Andreas Rheinhardt
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Anton Khirnov @ 2022-09-02 20:59 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Steve Lhomme
This state is not refcounted, so make sure it always has a well-defined
owner.
---
Steve, could you please test this?
---
libavcodec/pthread_frame.c | 37 ++++++++++++++++++++++++++++++++-----
1 file changed, 32 insertions(+), 5 deletions(-)
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 08a6f98898..9b44e2e698 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -148,6 +148,10 @@ typedef struct FrameThreadContext {
* Set for the first N packets, where N is the number of threads.
* While it is set, ff_thread_en/decode_frame won't return any results.
*/
+
+ const AVHWAccel *stash_hwaccel;
+ void *stash_hwaccel_context;
+ void *stash_hwaccel_priv;
} FrameThreadContext;
#if FF_API_THREAD_SAFE_CALLBACKS
@@ -228,9 +232,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
ff_thread_finish_setup(avctx);
if (p->hwaccel_serializing) {
+ /* wipe hwaccel state to avoid stale pointers lying around;
+ * the state was transferred to FrameThreadContext in
+ * ff_thread_finish_setup(), so nothing is leaked */
+ avctx->hwaccel = NULL;
+ avctx->hwaccel_context = NULL;
+ avctx->internal->hwaccel_priv_data = NULL;
+
p->hwaccel_serializing = 0;
pthread_mutex_unlock(&p->parent->hwaccel_mutex);
}
+ av_assert0(!avctx->hwaccel);
if (p->async_serializing) {
p->async_serializing = 0;
@@ -294,9 +306,6 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src,
dst->color_range = src->color_range;
dst->chroma_sample_location = src->chroma_sample_location;
- dst->hwaccel = src->hwaccel;
- dst->hwaccel_context = src->hwaccel_context;
-
dst->sample_rate = src->sample_rate;
dst->sample_fmt = src->sample_fmt;
#if FF_API_OLD_CHANNEL_LAYOUT
@@ -309,8 +318,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (err < 0)
return err;
- dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
-
if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
(dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
av_buffer_unref(&dst->hw_frames_ctx);
@@ -450,6 +457,12 @@ static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
pthread_mutex_unlock(&p->mutex);
return err;
}
+
+ /* transfer hwaccel state stashed from previous thread, if any */
+ av_assert0(!p->avctx->hwaccel);
+ FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
+ FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
+ FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
}
av_packet_unref(p->avpkt);
@@ -655,6 +668,13 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
async_lock(p->parent);
}
+ /* save hwaccel state for passing to the next thread;
+ * this is done here so that this worker thread can wipe its own hwaccel
+ * state after decoding, without requiring synchronization */
+ p->parent->stash_hwaccel = avctx->hwaccel;
+ p->parent->stash_hwaccel_context = avctx->hwaccel_context;
+ p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
+
pthread_mutex_lock(&p->progress_mutex);
if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
@@ -761,6 +781,13 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
av_freep(&fctx->threads);
ff_pthread_free(fctx, thread_ctx_offsets);
+ /* if we have stashed hwaccel state, move it to the user-facing context,
+ * so it will be freed in avcodec_close() */
+ av_assert0(!avctx->hwaccel);
+ FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
+ FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
+ FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
+
av_freep(&avctx->internal->thread_ctx);
}
--
2.35.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-02 20:59 [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
@ 2022-09-02 21:12 ` Andreas Rheinhardt
2022-09-03 18:25 ` Anton Khirnov
2022-09-05 5:42 ` [FFmpeg-devel] [PATCH] " Steve Lhomme
2 siblings, 0 replies; 11+ messages in thread
From: Andreas Rheinhardt @ 2022-09-02 21:12 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> This state is not refcounted, so make sure it always has a well-defined
> owner.
> ---
> Steve, could you please test this?
> ---
> libavcodec/pthread_frame.c | 37 ++++++++++++++++++++++++++++++++-----
> 1 file changed, 32 insertions(+), 5 deletions(-)
>
> diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
> index 08a6f98898..9b44e2e698 100644
> --- a/libavcodec/pthread_frame.c
> +++ b/libavcodec/pthread_frame.c
> @@ -148,6 +148,10 @@ typedef struct FrameThreadContext {
> * Set for the first N packets, where N is the number of threads.
> * While it is set, ff_thread_en/decode_frame won't return any results.
> */
> +
> + const AVHWAccel *stash_hwaccel;
> + void *stash_hwaccel_context;
> + void *stash_hwaccel_priv;
> } FrameThreadContext;
>
> #if FF_API_THREAD_SAFE_CALLBACKS
> @@ -228,9 +232,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
> ff_thread_finish_setup(avctx);
>
> if (p->hwaccel_serializing) {
> + /* wipe hwaccel state to avoid stale pointers lying around;
> + * the state was transferred to FrameThreadContext in
> + * ff_thread_finish_setup(), so nothing is leaked */
> + avctx->hwaccel = NULL;
> + avctx->hwaccel_context = NULL;
> + avctx->internal->hwaccel_priv_data = NULL;
> +
> p->hwaccel_serializing = 0;
> pthread_mutex_unlock(&p->parent->hwaccel_mutex);
> }
> + av_assert0(!avctx->hwaccel);
>
> if (p->async_serializing) {
> p->async_serializing = 0;
> @@ -294,9 +306,6 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src,
> dst->color_range = src->color_range;
> dst->chroma_sample_location = src->chroma_sample_location;
>
> - dst->hwaccel = src->hwaccel;
> - dst->hwaccel_context = src->hwaccel_context;
> -
> dst->sample_rate = src->sample_rate;
> dst->sample_fmt = src->sample_fmt;
> #if FF_API_OLD_CHANNEL_LAYOUT
> @@ -309,8 +318,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
> if (err < 0)
> return err;
>
> - dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
> -
> if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
> (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
> av_buffer_unref(&dst->hw_frames_ctx);
> @@ -450,6 +457,12 @@ static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
> pthread_mutex_unlock(&p->mutex);
> return err;
> }
> +
> + /* transfer hwaccel state stashed from previous thread, if any */
> + av_assert0(!p->avctx->hwaccel);
> + FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
> + FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
> + FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
> }
>
> av_packet_unref(p->avpkt);
> @@ -655,6 +668,13 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
> async_lock(p->parent);
> }
>
> + /* save hwaccel state for passing to the next thread;
> + * this is done here so that this worker thread can wipe its own hwaccel
> + * state after decoding, without requiring synchronization */
> + p->parent->stash_hwaccel = avctx->hwaccel;
> + p->parent->stash_hwaccel_context = avctx->hwaccel_context;
> + p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
> +
> pthread_mutex_lock(&p->progress_mutex);
> if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
> av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
> @@ -761,6 +781,13 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
> av_freep(&fctx->threads);
> ff_pthread_free(fctx, thread_ctx_offsets);
>
> + /* if we have stashed hwaccel state, move it to the user-facing context,
> + * so it will be freed in avcodec_close() */
> + av_assert0(!avctx->hwaccel);
> + FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
> + FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
> + FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
> +
> av_freep(&avctx->internal->thread_ctx);
> }
>
Does this allow to revert 091341f2ab5bd35ca1a2aae90503adc74f8d3523?
- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-02 20:59 [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
2022-09-02 21:12 ` Andreas Rheinhardt
@ 2022-09-03 18:25 ` Anton Khirnov
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 1/2] lavc: fix and extend AVCodecContext.hwaccel_context doxy Anton Khirnov
2022-09-05 5:42 ` [FFmpeg-devel] [PATCH] " Steve Lhomme
2 siblings, 1 reply; 11+ messages in thread
From: Anton Khirnov @ 2022-09-03 18:25 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Quoting Andreas Rheinhardt (2022-09-02 23:12:12)
> Does this allow to revert 091341f2ab5bd35ca1a2aae90503adc74f8d3523?
Yes, just tested that with the commandline from
091341f2ab5bd35ca1a2aae90503adc74f8d3523, asan
- reports no issues with current master
- reports heap-use-after-free if the commit is reverted without my patch
- reports no issues if the commit is reverted with my patch
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH 1/2] lavc: fix and extend AVCodecContext.hwaccel_context doxy
2022-09-03 18:25 ` Anton Khirnov
@ 2022-09-03 18:39 ` Anton Khirnov
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
0 siblings, 1 reply; 11+ messages in thread
From: Anton Khirnov @ 2022-09-03 18:39 UTC (permalink / raw)
To: ffmpeg-devel
Mention:
- that it is legacy and optional (every hwaccel that uses it can also
work with hwcontext, though some optional information can only be
signalled throught hwaccel_context)
- that it can be used for encoders (only qsvenc currently)
- ownership and lifetime
---
libavcodec/avcodec.h | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 65c8535359..7db5d1b1c5 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1388,13 +1388,26 @@ typedef struct AVCodecContext {
const struct AVHWAccel *hwaccel;
/**
- * Hardware accelerator context.
- * For some hardware accelerators, a global context needs to be
- * provided by the user. In that case, this holds display-dependent
- * data FFmpeg cannot instantiate itself. Please refer to the
- * FFmpeg HW accelerator documentation to know how to fill this.
- * - encoding: unused
- * - decoding: Set by user
+ * Legacy hardware accelerator context.
+ *
+ * For some hardware acceleration methods, the caller may use this field to
+ * signal hwaccel-specific data to the codec. The struct pointed to by this
+ * pointer is hwaccel-dependent and defined in the respective header. Please
+ * refer to the FFmpeg HW accelerator documentation to know how to fill
+ * this.
+ *
+ * In most cases this field is optional - the necessary information may also
+ * be provided to libavcodec through @ref hw_frames_ctx or @ref
+ * hw_device_ctx (see avcodec_get_hw_config()). However, in some cases it
+ * may be the only method of signalling some (optional) information.
+ *
+ * The struct and its contents are owned by the caller.
+ *
+ * - encoding: May be set by the caller before avcodec_open2(). Must remain
+ * valid until avcodec_free_context().
+ * - decoding: May be set by the caller in the get_format() callback.
+ * Must remain valid until the next get_format() call,
+ * or avcodec_free_context() (whichever comes first).
*/
void *hwaccel_context;
--
2.35.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 1/2] lavc: fix and extend AVCodecContext.hwaccel_context doxy Anton Khirnov
@ 2022-09-03 18:39 ` Anton Khirnov
2022-09-12 2:02 ` Wang Bin
0 siblings, 1 reply; 11+ messages in thread
From: Anton Khirnov @ 2022-09-03 18:39 UTC (permalink / raw)
To: ffmpeg-devel
This state is not refcounted, so make sure it always has a well-defined
owner.
Remove the block added in 091341f2ab5bd35ca1a2aae90503adc74f8d3523, as
this commit also solves that issue in a more general way.
---
libavcodec/pthread_frame.c | 47 ++++++++++++++++++++++++++++----------
1 file changed, 35 insertions(+), 12 deletions(-)
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 08a6f98898..066269621d 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -148,6 +148,12 @@ typedef struct FrameThreadContext {
* Set for the first N packets, where N is the number of threads.
* While it is set, ff_thread_en/decode_frame won't return any results.
*/
+
+ /* hwaccel state is temporarily stored here in order to transfer its ownership
+ * to the next decoding thread without the need for extra synchronization */
+ const AVHWAccel *stash_hwaccel;
+ void *stash_hwaccel_context;
+ void *stash_hwaccel_priv;
} FrameThreadContext;
#if FF_API_THREAD_SAFE_CALLBACKS
@@ -228,9 +234,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
ff_thread_finish_setup(avctx);
if (p->hwaccel_serializing) {
+ /* wipe hwaccel state to avoid stale pointers lying around;
+ * the state was transferred to FrameThreadContext in
+ * ff_thread_finish_setup(), so nothing is leaked */
+ avctx->hwaccel = NULL;
+ avctx->hwaccel_context = NULL;
+ avctx->internal->hwaccel_priv_data = NULL;
+
p->hwaccel_serializing = 0;
pthread_mutex_unlock(&p->parent->hwaccel_mutex);
}
+ av_assert0(!avctx->hwaccel);
if (p->async_serializing) {
p->async_serializing = 0;
@@ -294,9 +308,6 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src,
dst->color_range = src->color_range;
dst->chroma_sample_location = src->chroma_sample_location;
- dst->hwaccel = src->hwaccel;
- dst->hwaccel_context = src->hwaccel_context;
-
dst->sample_rate = src->sample_rate;
dst->sample_fmt = src->sample_fmt;
#if FF_API_OLD_CHANNEL_LAYOUT
@@ -309,8 +320,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (err < 0)
return err;
- dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
-
if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
(dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
av_buffer_unref(&dst->hw_frames_ctx);
@@ -450,6 +459,12 @@ static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
pthread_mutex_unlock(&p->mutex);
return err;
}
+
+ /* transfer hwaccel state stashed from previous thread, if any */
+ av_assert0(!p->avctx->hwaccel);
+ FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
+ FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
+ FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
}
av_packet_unref(p->avpkt);
@@ -655,6 +670,14 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
async_lock(p->parent);
}
+ /* save hwaccel state for passing to the next thread;
+ * this is done here so that this worker thread can wipe its own hwaccel
+ * state after decoding, without requiring synchronization */
+ av_assert0(!p->parent->stash_hwaccel);
+ p->parent->stash_hwaccel = avctx->hwaccel;
+ p->parent->stash_hwaccel_context = avctx->hwaccel_context;
+ p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
+
pthread_mutex_lock(&p->progress_mutex);
if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
@@ -708,13 +731,6 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
park_frame_worker_threads(fctx, thread_count);
- if (fctx->prev_thread && avctx->internal->hwaccel_priv_data !=
- fctx->prev_thread->avctx->internal->hwaccel_priv_data) {
- if (update_context_from_thread(avctx, fctx->prev_thread->avctx, 1) < 0) {
- av_log(avctx, AV_LOG_ERROR, "Failed to update user thread.\n");
- }
- }
-
for (i = 0; i < thread_count; i++) {
PerThreadContext *p = &fctx->threads[i];
AVCodecContext *ctx = p->avctx;
@@ -761,6 +777,13 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
av_freep(&fctx->threads);
ff_pthread_free(fctx, thread_ctx_offsets);
+ /* if we have stashed hwaccel state, move it to the user-facing context,
+ * so it will be freed in avcodec_close() */
+ av_assert0(!avctx->hwaccel);
+ FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
+ FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
+ FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
+
av_freep(&avctx->internal->thread_ctx);
}
--
2.35.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-02 20:59 [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
2022-09-02 21:12 ` Andreas Rheinhardt
2022-09-03 18:25 ` Anton Khirnov
@ 2022-09-05 5:42 ` Steve Lhomme
2022-09-05 5:53 ` Anton Khirnov
2022-09-05 19:36 ` Michael Niedermayer
2 siblings, 2 replies; 11+ messages in thread
From: Steve Lhomme @ 2022-09-05 5:42 UTC (permalink / raw)
To: ffmpeg-devel
Hi Anton,
On 2022-09-02 22:59, Anton Khirnov wrote:
> This state is not refcounted, so make sure it always has a well-defined
> owner.
> ---
> Steve, could you please test this?
I can confirm it doesn't leak the context and plays correctly. It also
doesn't crash ;)
> ---
> libavcodec/pthread_frame.c | 37 ++++++++++++++++++++++++++++++++-----
> 1 file changed, 32 insertions(+), 5 deletions(-)
>
> diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
> index 08a6f98898..9b44e2e698 100644
> --- a/libavcodec/pthread_frame.c
> +++ b/libavcodec/pthread_frame.c
> @@ -148,6 +148,10 @@ typedef struct FrameThreadContext {
> * Set for the first N packets, where N is the number of threads.
> * While it is set, ff_thread_en/decode_frame won't return any results.
> */
> +
> + const AVHWAccel *stash_hwaccel;
> + void *stash_hwaccel_context;
> + void *stash_hwaccel_priv;
> } FrameThreadContext;
>
> #if FF_API_THREAD_SAFE_CALLBACKS
> @@ -228,9 +232,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
> ff_thread_finish_setup(avctx);
>
> if (p->hwaccel_serializing) {
> + /* wipe hwaccel state to avoid stale pointers lying around;
> + * the state was transferred to FrameThreadContext in
> + * ff_thread_finish_setup(), so nothing is leaked */
> + avctx->hwaccel = NULL;
> + avctx->hwaccel_context = NULL;
> + avctx->internal->hwaccel_priv_data = NULL;
> +
> p->hwaccel_serializing = 0;
> pthread_mutex_unlock(&p->parent->hwaccel_mutex);
> }
> + av_assert0(!avctx->hwaccel);
>
> if (p->async_serializing) {
> p->async_serializing = 0;
> @@ -294,9 +306,6 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src,
> dst->color_range = src->color_range;
> dst->chroma_sample_location = src->chroma_sample_location;
>
> - dst->hwaccel = src->hwaccel;
> - dst->hwaccel_context = src->hwaccel_context;
> -
> dst->sample_rate = src->sample_rate;
> dst->sample_fmt = src->sample_fmt;
> #if FF_API_OLD_CHANNEL_LAYOUT
> @@ -309,8 +318,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
> if (err < 0)
> return err;
>
> - dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
> -
> if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
> (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
> av_buffer_unref(&dst->hw_frames_ctx);
> @@ -450,6 +457,12 @@ static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
> pthread_mutex_unlock(&p->mutex);
> return err;
> }
> +
> + /* transfer hwaccel state stashed from previous thread, if any */
> + av_assert0(!p->avctx->hwaccel);
> + FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
> + FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
> + FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
> }
>
> av_packet_unref(p->avpkt);
> @@ -655,6 +668,13 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
> async_lock(p->parent);
> }
>
> + /* save hwaccel state for passing to the next thread;
> + * this is done here so that this worker thread can wipe its own hwaccel
> + * state after decoding, without requiring synchronization */
> + p->parent->stash_hwaccel = avctx->hwaccel;
> + p->parent->stash_hwaccel_context = avctx->hwaccel_context;
> + p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
> +
> pthread_mutex_lock(&p->progress_mutex);
> if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
> av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
> @@ -761,6 +781,13 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
> av_freep(&fctx->threads);
> ff_pthread_free(fctx, thread_ctx_offsets);
>
> + /* if we have stashed hwaccel state, move it to the user-facing context,
> + * so it will be freed in avcodec_close() */
> + av_assert0(!avctx->hwaccel);
> + FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
> + FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
> + FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
> +
> av_freep(&avctx->internal->thread_ctx);
> }
>
> --
> 2.35.1
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-05 5:42 ` [FFmpeg-devel] [PATCH] " Steve Lhomme
@ 2022-09-05 5:53 ` Anton Khirnov
2022-09-05 19:36 ` Michael Niedermayer
1 sibling, 0 replies; 11+ messages in thread
From: Anton Khirnov @ 2022-09-05 5:53 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Quoting Steve Lhomme (2022-09-05 07:42:17)
> Hi Anton,
>
> On 2022-09-02 22:59, Anton Khirnov wrote:
> > This state is not refcounted, so make sure it always has a well-defined
> > owner.
> > ---
> > Steve, could you please test this?
>
> I can confirm it doesn't leak the context and plays correctly. It also
> doesn't crash ;)
Awesome, thank you very much for testing.
Will push tomorrow to master and 5.1, if nobody has further comments.
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-05 5:42 ` [FFmpeg-devel] [PATCH] " Steve Lhomme
2022-09-05 5:53 ` Anton Khirnov
@ 2022-09-05 19:36 ` Michael Niedermayer
1 sibling, 0 replies; 11+ messages in thread
From: Michael Niedermayer @ 2022-09-05 19:36 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 648 bytes --]
On Mon, Sep 05, 2022 at 07:42:17AM +0200, Steve Lhomme wrote:
> Hi Anton,
>
> On 2022-09-02 22:59, Anton Khirnov wrote:
> > This state is not refcounted, so make sure it always has a well-defined
> > owner.
> > ---
> > Steve, could you please test this?
>
> I can confirm it doesn't leak the context and plays correctly. It also
> doesn't crash ;)
just wanted to say a big thanks to both you and anton for working on
this !
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Elect your leaders based on what they did after the last election, not
based on what they say before an election.
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
@ 2022-09-12 2:02 ` Wang Bin
2022-09-12 6:59 ` Wang Bin
0 siblings, 1 reply; 11+ messages in thread
From: Wang Bin @ 2022-09-12 2:02 UTC (permalink / raw)
To: FFmpeg development discussions and patches
>
>
> av_packet_unref(p->avpkt);
> @@ -655,6 +670,14 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
> async_lock(p->parent);
> }
>
> + /* save hwaccel state for passing to the next thread;
> + * this is done here so that this worker thread can wipe its own
> hwaccel
> + * state after decoding, without requiring synchronization */
> + av_assert0(!p->parent->stash_hwaccel);
> + p->parent->stash_hwaccel = avctx->hwaccel;
> + p->parent->stash_hwaccel_context = avctx->hwaccel_context;
> + p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
>
Assertion failure when seeking. Step to reproduce:
./ffmpeg -stream_loop -1 -an -hwaccel vaapi -i test.mp4 -f null >/dev/null
Regards
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-12 2:02 ` Wang Bin
@ 2022-09-12 6:59 ` Wang Bin
2022-09-15 8:12 ` Marvin Scholz
0 siblings, 1 reply; 11+ messages in thread
From: Wang Bin @ 2022-09-12 6:59 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Wang Bin <wbsecg1@gmail.com> 于2022年9月12日周一 10:02写道:
>
>> av_packet_unref(p->avpkt);
>> @@ -655,6 +670,14 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
>> async_lock(p->parent);
>> }
>>
>> + /* save hwaccel state for passing to the next thread;
>> + * this is done here so that this worker thread can wipe its own
>> hwaccel
>> + * state after decoding, without requiring synchronization */
>> + av_assert0(!p->parent->stash_hwaccel);
>> + p->parent->stash_hwaccel = avctx->hwaccel;
>> + p->parent->stash_hwaccel_context = avctx->hwaccel_context;
>> + p->parent->stash_hwaccel_priv =
>> avctx->internal->hwaccel_priv_data;
>>
>
> Assertion failure when seeking. Step to reproduce:
> ./ffmpeg -stream_loop -1 -an -hwaccel vaapi -i test.mp4 -f null >/dev/null
>
> Regards
>
>
The change breaks hwaccel on all platforms. videotoolbox has another crash.
Regards
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads
2022-09-12 6:59 ` Wang Bin
@ 2022-09-15 8:12 ` Marvin Scholz
0 siblings, 0 replies; 11+ messages in thread
From: Marvin Scholz @ 2022-09-15 8:12 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On 12 Sep 2022, at 8:59, Wang Bin wrote:
> Wang Bin <wbsecg1@gmail.com> 于2022年9月12日周一 10:02写道:
>
>>
>>> av_packet_unref(p->avpkt);
>>> @@ -655,6 +670,14 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
>>> async_lock(p->parent);
>>> }
>>>
>>> + /* save hwaccel state for passing to the next thread;
>>> + * this is done here so that this worker thread can wipe its own
>>> hwaccel
>>> + * state after decoding, without requiring synchronization */
>>> + av_assert0(!p->parent->stash_hwaccel);
>>> + p->parent->stash_hwaccel = avctx->hwaccel;
>>> + p->parent->stash_hwaccel_context = avctx->hwaccel_context;
>>> + p->parent->stash_hwaccel_priv =
>>> avctx->internal->hwaccel_priv_data;
>>>
>>
>> Assertion failure when seeking. Step to reproduce:
>> ./ffmpeg -stream_loop -1 -an -hwaccel vaapi -i test.mp4 -f null >/dev/null
>>
>> Regards
>>
>>
>
> The change breaks hwaccel on all platforms. videotoolbox has another crash.
>
Can confirm this breaks videotoolbox, to reproduce simply do:
./ffmpeg -v debug -hwaccel videotoolbox -i ~/Downloads/HDR\ Mix\ 8k\ Dolby\ Vision-sLs6yYYDpTw.webm -f null /dev/null
Trace:
* thread #30, name = 'com.apple.coremedia.rootQueue.47', queue = 'vtdecoder-callback-queue-0x10331f520', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
frame #0: 0x0000000100d376b8 ffmpeg_g`videotoolbox_decoder_callback(opaque=0x0000000108c079c0, sourceFrameRefCon=0x0000000000000000, status=0, flags=0, image_buffer=0x0000600003300000, pts=CMTime @ 0x0000700005db5a00, duration=0 seconds) at videotoolbox.c:696:16 [opt]
693 AVCodecContext *avctx = opaque;
694 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
695
-> 696 if (vtctx->frame) {
697 CVPixelBufferRelease(vtctx->frame);
698 vtctx->frame = NULL;
699 }
Target 0: (ffmpeg_g) stopped.
The hwaccel_priv_data is NULL here. It works fine when using -threads 1.
> Regards
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2022-09-15 8:12 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-02 20:59 [FFmpeg-devel] [PATCH] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
2022-09-02 21:12 ` Andreas Rheinhardt
2022-09-03 18:25 ` Anton Khirnov
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 1/2] lavc: fix and extend AVCodecContext.hwaccel_context doxy Anton Khirnov
2022-09-03 18:39 ` [FFmpeg-devel] [PATCH 2/2] lavc/pthread_frame: avoid leaving stale hwaccel state in worker threads Anton Khirnov
2022-09-12 2:02 ` Wang Bin
2022-09-12 6:59 ` Wang Bin
2022-09-15 8:12 ` Marvin Scholz
2022-09-05 5:42 ` [FFmpeg-devel] [PATCH] " Steve Lhomme
2022-09-05 5:53 ` Anton Khirnov
2022-09-05 19:36 ` Michael Niedermayer
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git