diff mbox series

[FFmpeg-devel,v2,7/8] lavc/vvc_dec: Add hardware decode API

Message ID 20240918071031.1377336-7-fei.w.wang@intel.com
State New
Headers show
Series [FFmpeg-devel,v2,1/8] lavc/vaapi_dec: Create VA parameters dynamically | 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 success Make fate finished

Commit Message

Wang, Fei W Sept. 18, 2024, 7:10 a.m. UTC
From: Fei Wang <fei.w.wang@intel.com>

Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
 libavcodec/vvc/dec.c  | 73 +++++++++++++++++++++++++++++++++++++------
 libavcodec/vvc/dec.h  |  4 +++
 libavcodec/vvc/refs.c |  6 ++++
 3 files changed, 73 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/vvc/dec.c b/libavcodec/vvc/dec.c
index edf2607f50..c9f0e44889 100644
--- a/libavcodec/vvc/dec.c
+++ b/libavcodec/vvc/dec.c
@@ -22,6 +22,8 @@ 
  */
 #include "libavcodec/codec_internal.h"
 #include "libavcodec/decode.h"
+#include "libavcodec/hwaccel_internal.h"
+#include "libavcodec/hwconfig.h"
 #include "libavcodec/profiles.h"
 #include "libavcodec/refstruct.h"
 #include "libavutil/cpu.h"
@@ -610,6 +612,8 @@  static int ref_frame(VVCFrame *dst, const VVCFrame *src)
 
     ff_refstruct_replace(&dst->rpl_tab, src->rpl_tab);
     ff_refstruct_replace(&dst->rpl, src->rpl);
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
     dst->nb_rpl_elems = src->nb_rpl_elems;
 
     dst->poc = src->poc;
@@ -770,17 +774,41 @@  static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc,
     return 0;
 }
 
+static enum AVPixelFormat get_format(AVCodecContext *avctx, const VVCSPS *sps)
+{
+#define HWACCEL_MAX 0
+
+    enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
+
+    switch (sps->pix_fmt) {
+    case AV_PIX_FMT_YUV420P:
+        break;
+    case AV_PIX_FMT_YUV420P10:
+        break;
+    }
+
+    *fmt++ = sps->pix_fmt;
+    *fmt = AV_PIX_FMT_NONE;
+
+    return ff_get_format(avctx, pix_fmts);
+}
+
 static void export_frame_params(VVCContext *s, const VVCFrameContext *fc)
 {
     AVCodecContext *c = s->avctx;
     const VVCSPS *sps = fc->ps.sps;
     const VVCPPS *pps = fc->ps.pps;
 
-    c->pix_fmt      = sps->pix_fmt;
-    c->coded_width  = pps->width;
-    c->coded_height = pps->height;
-    c->width        = pps->width  - ((pps->r->pps_conf_win_left_offset + pps->r->pps_conf_win_right_offset) << sps->hshift[CHROMA]);
-    c->height       = pps->height - ((pps->r->pps_conf_win_top_offset + pps->r->pps_conf_win_bottom_offset) << sps->vshift[CHROMA]);
+    // Reset HW config if pix_fmt/w/h change.
+    if (s->pix_fmt != sps->pix_fmt || c->coded_width != pps->width || c->coded_height != pps->height) {
+        c->coded_width  = pps->width;
+        c->coded_height = pps->height;
+        c->pix_fmt      = get_format(c, sps);
+        s->pix_fmt      = sps->pix_fmt;
+    }
+
+    c->width  = pps->width  - ((pps->r->pps_conf_win_left_offset + pps->r->pps_conf_win_right_offset) << sps->hshift[CHROMA]);
+    c->height = pps->height - ((pps->r->pps_conf_win_top_offset + pps->r->pps_conf_win_bottom_offset) << sps->vshift[CHROMA]);
     c->has_b_frames = sps->r->sps_dpb_params.dpb_max_num_reorder_pics[sps->r->sps_max_sublayers_minus1];
 }
 
@@ -824,6 +852,20 @@  static int decode_slice(VVCContext *s, VVCFrameContext *fc, const H2645NAL *nal,
     ret = slice_init_entry_points(sc, fc, nal, unit);
     if (ret < 0)
         return ret;
+
+    if (s->avctx->hwaccel) {
+        if (is_first_slice) {
+            ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
+            if (ret < 0)
+                return ret;
+        }
+
+        ret = FF_HW_CALL(s->avctx, decode_slice,
+                         nal->raw_data, nal->raw_size);
+        if (ret < 0)
+            return ret;
+    }
+
     fc->nb_slices++;
 
     return 0;
@@ -939,17 +981,26 @@  static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output)
 
 static int submit_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *output, int *got_output)
 {
-    int ret = ff_vvc_frame_submit(s, fc);
+    int ret;
 
-    if (ret < 0) {
-        ff_vvc_report_frame_finished(fc->ref);
-        return ret;
+    if (s->avctx->hwaccel) {
+        if (ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame) < 0) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "Hardware accelerator failed to decode picture\n");
+            ff_vvc_unref_frame(fc, fc->ref, ~0);
+            return ret;
+        }
+    } else {
+        if (ret = ff_vvc_frame_submit(s, fc) < 0) {
+            ff_vvc_report_frame_finished(fc->ref);
+            return ret;
+        }
     }
 
     s->nb_frames++;
     s->nb_delayed++;
 
-    if (s->nb_delayed >= s->nb_fcs) {
+    if (s->nb_delayed >= s->nb_fcs || s->avctx->hwaccel) {
         if ((ret = wait_delayed_frame(s, output, got_output)) < 0)
             return ret;
     }
@@ -1095,6 +1146,8 @@  static av_cold int vvc_decode_init(AVCodecContext *avctx)
     GDR_SET_RECOVERED(s);
     ff_thread_once(&init_static_once, init_default_scale_m);
 
+    s->pix_fmt = AV_PIX_FMT_NONE;
+
     return 0;
 }
 
diff --git a/libavcodec/vvc/dec.h b/libavcodec/vvc/dec.h
index d27cf52ca2..776b38b20f 100644
--- a/libavcodec/vvc/dec.h
+++ b/libavcodec/vvc/dec.h
@@ -101,6 +101,8 @@  typedef struct VVCFrame {
      * A combination of VVC_FRAME_FLAG_*
      */
     uint8_t flags;
+
+    void *hwaccel_picture_private; ///< hardware accelerator private data
 } VVCFrame;
 
 typedef struct SliceContext {
@@ -243,6 +245,8 @@  typedef struct VVCContext {
 
     uint64_t nb_frames;     ///< processed frames
     int nb_delayed;         ///< delayed frames
+
+    enum AVPixelFormat pix_fmt; ///< pix format of current frame
 }  VVCContext ;
 
 #endif /* AVCODEC_VVC_DEC_H */
diff --git a/libavcodec/vvc/refs.c b/libavcodec/vvc/refs.c
index 3e5573df29..a41a83631a 100644
--- a/libavcodec/vvc/refs.c
+++ b/libavcodec/vvc/refs.c
@@ -26,6 +26,7 @@ 
 #include "libavutil/thread.h"
 #include "libavcodec/refstruct.h"
 #include "libavcodec/thread.h"
+#include "libavcodec/decode.h"
 
 #include "refs.h"
 
@@ -59,6 +60,7 @@  void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags)
         ff_refstruct_unref(&frame->rpl_tab);
 
         frame->collocated_ref = NULL;
+        ff_refstruct_unref(&frame->hwaccel_picture_private);
     }
 }
 
@@ -153,6 +155,10 @@  static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc)
         if (!frame->progress)
             goto fail;
 
+        ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
+        if (ret < 0)
+            goto fail;
+
         return frame;
 fail:
         ff_vvc_unref_frame(fc, frame, ~0);