diff mbox series

[FFmpeg-devel,v1,2/3] lavc/decode: Add internal surface re-allocate method for hwaccel

Message ID 20220812125545.1229410-2-fei.w.wang@intel.com
State New
Headers show
Series [FFmpeg-devel,v1,1/3] lavc/decode: Add get_hw_config function | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Wang, Fei W Aug. 12, 2022, 12:55 p.m. UTC
From: Linjie Fu <linjie.fu@intel.com>

Add HWACCEL_CAP_INTERNAL_ALLOC flag to indicate hwaccels are able to
re-allocate surface internally through ff_decode_get_hw_frames_ctx.

Signed-off-by: Linjie Fu <linjie.fu@intel.com>
Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
 libavcodec/decode.c   | 36 ++++++++++++++++++++++++++++++++++++
 libavcodec/hwconfig.h |  1 +
 2 files changed, 37 insertions(+)

Comments

Anton Khirnov Aug. 16, 2022, 11:26 a.m. UTC | #1
Quoting Fei Wang (2022-08-12 14:55:44)
> From: Linjie Fu <linjie.fu@intel.com>
> 
> Add HWACCEL_CAP_INTERNAL_ALLOC flag to indicate hwaccels are able to
> re-allocate surface internally through ff_decode_get_hw_frames_ctx.
> 
> Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> ---

The commit message should explain what problem is this trying to solve.
It is very much not obvious to me.
diff mbox series

Patch

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index d66d5a4160..00c9141d91 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1176,6 +1176,33 @@  static const AVCodecHWConfigInternal *get_hw_config(AVCodecContext *avctx, enum
     return hw_config;
 }
 
+static int hwaccel_realloc_surface(AVCodecContext *avctx)
+{
+    const AVCodecHWConfigInternal *hw_config;
+    int ret;
+
+    if (avctx->hw_frames_ctx)
+        av_buffer_unref(&avctx->hw_frames_ctx);
+
+    hw_config = get_hw_config(avctx, avctx->pix_fmt);
+    if (!hw_config)
+        return AV_PIX_FMT_NONE;
+
+    if (avctx->hw_device_ctx &&
+        hw_config->public.methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) {
+        const AVHWDeviceContext *device_ctx =
+                (AVHWDeviceContext*)avctx->hw_device_ctx->data;
+        ret = ff_decode_get_hw_frames_ctx(avctx, device_ctx->type);
+        if (ret < 0) {
+            av_log(avctx, AV_LOG_WARNING, "Failed to re-allocate hwaccel surface internally.\n");
+            return AV_PIX_FMT_NONE;
+        }
+    } else
+        return AV_PIX_FMT_NONE;
+
+    return hw_config->public.pix_fmt;
+}
+
 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
 {
     const AVPixFmtDescriptor *desc;
@@ -1202,6 +1229,15 @@  int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
         return AV_PIX_FMT_NONE;
 
     for (;;) {
+        if (avctx->internal->hwaccel_priv_data &&
+            avctx->hwaccel->caps_internal & HWACCEL_CAP_INTERNAL_ALLOC) {
+            err = hwaccel_realloc_surface(avctx);
+            if (err < 0)
+                av_log(avctx, AV_LOG_WARNING, "Try to re-initialize all.\n");
+            else
+                return err;
+        }
+
         // Remove the previous hwaccel, if there was one.
         hwaccel_uninit(avctx);
 
diff --git a/libavcodec/hwconfig.h b/libavcodec/hwconfig.h
index 721424912c..7405c66c07 100644
--- a/libavcodec/hwconfig.h
+++ b/libavcodec/hwconfig.h
@@ -24,6 +24,7 @@ 
 
 
 #define HWACCEL_CAP_ASYNC_SAFE      (1 << 0)
+#define HWACCEL_CAP_INTERNAL_ALLOC  (1 << 1)
 
 
 typedef struct AVCodecHWConfigInternal {