[FFmpeg-devel] [PATCH] avcodec: add avcodec_reset_hw_frames_parameters()

Thomas Guillem thomas at gllm.fr
Thu Nov 28 17:57:32 EET 2024


usage example:

    AVBufferRef *hwframes_ref;
    int ret = avcodec_get_hw_frames_parameters(ctx, hwdev_ref, hwfmt,
                                               &hwframes_ref);
...
    ret = av_hwframe_ctx_init(hwframes_ref);
    if (ret < 0)
    {
        av_buffer_unref(&hwframes_ref);
        avcodec_reset_hw_frames_parameters(ctx);
        return -1;
    }

If avcodec_reset_hw_frames_parameters() is not called, it can lead to the
following assert:

Assertion p_dst->hwaccel_threadsafe || (!dst->hwaccel && !dst->internal->hwaccel_priv_data) failed at src/libavcodec/pthread_frame.c:426
---
 libavcodec/avcodec.h | 15 +++++++++++++++
 libavcodec/decode.c  |  5 +++++
 2 files changed, 20 insertions(+)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 77ca8dee1f..dfea991db9 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2701,6 +2701,21 @@ int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
                                      enum AVPixelFormat hw_pix_fmt,
                                      AVBufferRef **out_frames_ref);
 
+/**
+ * Reset AVCodecContext HW frames internals
+ *
+ * This function can be called after a successful call to
+ * avcodec_get_hw_frames_parameters() in order to reset the HW decoding state.
+ *
+ * It can be used from get_format, to fallback to software decoding after a
+ * failure, not necessarily linked to avcodec_get_hw_frames_parameters()
+ *
+ * @param avctx The context which is currently calling get_format, and which
+ *              implicitly contains all state needed for filling the returned
+ *              AVHWFramesContext properly.
+ */
+void avcodec_reset_hw_frames_parameters(AVCodecContext *avctx);
+
 enum AVCodecConfig {
     AV_CODEC_CONFIG_PIX_FORMAT,     ///< AVPixelFormat, terminated by AV_PIX_FMT_NONE
     AV_CODEC_CONFIG_FRAME_RATE,     ///< AVRational, terminated by {0, 0}
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index d1e10223f2..5e608076e5 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1227,6 +1227,11 @@ int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
     return ret;
 }
 
+void avcodec_reset_hw_frames_parameters(AVCodecContext *avctx)
+{
+    ff_hwaccel_uninit(avctx);
+}
+
 static int hwaccel_init(AVCodecContext *avctx,
                         const FFHWAccel *hwaccel)
 {
-- 
2.45.2



More information about the ffmpeg-devel mailing list