[FFmpeg-devel] [PATCH v2] vulkan_h264: fix long-term ref handling

2023-10-13 Thread Benjamin Cheng via ffmpeg-devel
h->long_ref isn't guaranteed to be contiguously filled. Use the approach
from both vaapi_h264 and vdpau_h264 which goes through the 16 frames in
h->long_ref to find the LTR entries.

Fixes MR2_MW_A.264 from JVT-AVC_V1.

---
 libavcodec/vulkan_h264.c | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index 32ef32d640..1bbef7b0e8 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -406,10 +406,14 @@ static int vk_h264_start_frame(AVCodecContext  
*avctx,
 }
 
 /* Fill in long-term refs */
-for (int r = 0, i = h->short_ref_count; i < h->short_ref_count + 
h->long_ref_count; i++, r++) {
+for (int r = 0, i = h->short_ref_count; r < H264_MAX_DPB_FRAMES &&
+ i < h->short_ref_count + h->long_ref_count; r++) {
+if (!h->long_ref[r])
+continue;
+
 dpb_slot_index = 0;
-for (unsigned slot = 0; slot < H264_MAX_PICTURE_COUNT; slot++) {
-if (h->long_ref[i] == &h->DPB[slot]) {
+for (unsigned slot = 0; slot < 16; slot++) {
+if (h->long_ref[r] == &h->DPB[slot]) {
 dpb_slot_index = slot;
 break;
 }
@@ -422,6 +426,7 @@ static int vk_h264_start_frame(AVCodecContext  
*avctx,
 dpb_slot_index);
 if (err < 0)
 return err;
+i++;
 }
 
 hp->h264pic = (StdVideoDecodeH264PictureInfo) {
-- 
2.42.0

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] libavcodec/dxva2.c: fix dxva2 does not support H264 baseline profile

2023-10-14 Thread Benjamin Cheng via ffmpeg-devel
On Fri Oct 13, 2023 at 11:59 PM EDT, xyz1001 wrote:
> dxva2 fail to init when decode h264 with baseline profile becase 
> `prof_h264_high` does not contains `AV_PROFILE_H264_BASELINE` and 
> `dxva_check_codec_compatibility` will return error

prof_h264_high uses either DXVA2_ModeH264_E or DXVA2_ModeH264_F, which
only supports up to H.264 High, and H.264 Baseline has features that are
not in High. You have to use a different DXVA profile for Baseline.

> ---
>  libavcodec/dxva2.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/libavcodec/dxva2.c b/libavcodec/dxva2.c
> index d7bc587562..e6b83f89cc 100644
> --- a/libavcodec/dxva2.c
> +++ b/libavcodec/dxva2.c
> @@ -61,7 +61,8 @@ typedef struct dxva_mode {
>  static const int prof_mpeg2_main[]   = {AV_PROFILE_MPEG2_SIMPLE,
>  AV_PROFILE_MPEG2_MAIN,
>  AV_PROFILE_UNKNOWN};
> -static const int prof_h264_high[]= {AV_PROFILE_H264_CONSTRAINED_BASELINE,
> +static const int prof_h264_high[]= {AV_PROFILE_H264_BASELINE,
> +AV_PROFILE_H264_CONSTRAINED_BASELINE,
>  AV_PROFILE_H264_MAIN,
>  AV_PROFILE_H264_HIGH,
>  AV_PROFILE_UNKNOWN};

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v4 2/2] lavc/vulkan_av1: port to the new stable API

2024-02-12 Thread Benjamin Cheng via ffmpeg-devel
On Sun Feb 11, 2024 at 10:15 AM EST, Lynne wrote:
> From: Lynne 
> Date: Fri, 19 Jan 2024 10:49:02 +1000
> Subject: [PATCH v4 2/2] lavc/vulkan_av1: port to the new stable API
> 
> Most of this patch was written by Dave Airlie ,
> with some additions by me.
> ---
>  configure |   4 +-
>  libavcodec/Makefile   |   3 +-
>  libavcodec/av1.h  |   2 +
>  libavcodec/vulkan_av1.c   | 477 ++
>  libavcodec/vulkan_decode.c|  24 +-
>  libavcodec/vulkan_decode.h|   2 +-
>  libavcodec/vulkan_video.c |   2 +-
>  libavcodec/vulkan_video.h |   2 -
>  libavcodec/vulkan_video_codec_av1std.h| 403 ---
>  libavcodec/vulkan_video_codec_av1std_decode.h |  36 --
>  libavutil/hwcontext_vulkan.c  |   2 +-
>  libavutil/vulkan_functions.h  |   2 +-
>  libavutil/vulkan_loader.h |   2 +-
>  13 files changed, 277 insertions(+), 684 deletions(-)
>  delete mode 100644 libavcodec/vulkan_video_codec_av1std.h
>  delete mode 100644 libavcodec/vulkan_video_codec_av1std_decode.h
> 
> diff --git a/configure b/configure
> index f72533b7d2..9a3a451115 100755
> --- a/configure
> +++ b/configure
> @@ -7225,8 +7225,8 @@ enabled crystalhd && check_lib crystalhd "stdint.h 
> libcrystalhd/libcrystalhd_if.
>   "in maintaining it."
>  
>  if enabled vulkan; then
> -check_pkg_config_header_only vulkan "vulkan >= 1.3.255" 
> "vulkan/vulkan.h" "defined VK_VERSION_1_3" ||
> -check_cpp_condition vulkan "vulkan/vulkan.h" 
> "defined(VK_VERSION_1_4) || (defined(VK_VERSION_1_3) && VK_HEADER_VERSION >= 
> 255)"
> +check_pkg_config_header_only vulkan "vulkan >= 1.3.277" 
> "vulkan/vulkan.h" "defined VK_VERSION_1_3" ||
> +check_cpp_condition vulkan "vulkan/vulkan.h" 
> "defined(VK_VERSION_1_4) || (defined(VK_VERSION_1_3) && VK_HEADER_VERSION >= 
> 277)"
>  fi
>  
>  if disabled vulkan; then
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 470d7cb9b1..581415f3b7 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1262,7 +1262,6 @@ SKIPHEADERS+= %_tablegen.h  
> \
>aacenc_quantization.h \
>aacenc_quantization_misc.h\
>bitstream_template.h  \
> -  vulkan_video_codec_av1std.h   \
>$(ARCH)/vpx_arith.h  \
>  
>  SKIPHEADERS-$(CONFIG_AMF)  += amfenc.h
> @@ -1285,7 +1284,7 @@ SKIPHEADERS-$(CONFIG_XVMC) += xvmc.h
>  SKIPHEADERS-$(CONFIG_VAAPI)+= vaapi_decode.h vaapi_hevc.h 
> vaapi_encode.h
>  SKIPHEADERS-$(CONFIG_VDPAU)+= vdpau.h vdpau_internal.h
>  SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX) += videotoolbox.h vt_internal.h
> -SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> vulkan_decode.h vulkan_video_codec_av1std_decode.h
> +SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> vulkan_decode.h
>  SKIPHEADERS-$(CONFIG_V4L2_M2M) += v4l2_buffers.h v4l2_context.h 
> v4l2_m2m.h
>  SKIPHEADERS-$(CONFIG_ZLIB) += zlib_wrapper.h
>  
> diff --git a/libavcodec/av1.h b/libavcodec/av1.h
> index 8704bc41c1..18d5fa9e7f 100644
> --- a/libavcodec/av1.h
> +++ b/libavcodec/av1.h
> @@ -121,6 +121,8 @@ enum {
>  AV1_DIV_LUT_NUM   = 257,
>  
>  AV1_MAX_LOOP_FILTER = 63,
> +
> +AV1_RESTORATION_TILESIZE_MAX = 256,
>  };
>  
>  
> diff --git a/libavcodec/vulkan_av1.c b/libavcodec/vulkan_av1.c
> index 9730e4b08d..641ee3e4bf 100644
> --- a/libavcodec/vulkan_av1.c
> +++ b/libavcodec/vulkan_av1.c
> @@ -31,22 +31,36 @@ const VkExtensionProperties ff_vk_dec_av1_ext = {
>  typedef struct AV1VulkanDecodePicture {
>  FFVulkanDecodePicture   vp;
>  
> -/* Workaround for a spec issue.
> - *Can be removed once no longer needed, and threading can be enabled. */
> +/* TODO: investigate if this can be removed to make decoding completely
> + * independent. */
>  FFVulkanDecodeContext  *dec;
>  
> -StdVideoAV1MESATiletiles[MAX_TILES];
> -StdVideoAV1MESATileListtile_list;
> -const uint32_t*tile_offsets;
> +uint32_t tile_count;
> +uint32_t tile_sizes[MAX_TILES];
> +const uint32_t *tile_offsets;
>  
>  /* Current picture */
> -VkVideoDecodeAV1DpbSlotInfoMESAvkav1_ref;
> -StdVideoAV1MESAFrameHeader av1_frame_header;
> -VkVideoDecodeAV1PictureInfoMESAav1_pic_info;
> +StdVideoDecodeAV1ReferenceInfo std_ref;
> +VkVideoDecodeAV1DpbSlotInfoKHR vkav1_ref;
> +uint16_t width_in_sbs_minus1[64];
> +uint16_t height_in_sbs_minus1[64];
> +uint1

[FFmpeg-devel] [PATCH] vulkan_h264: send scaling lists in zig-zag order

2023-08-21 Thread Benjamin Cheng via ffmpeg-devel
h264_ps turns the scaling lists into matrices with a raster scan order,
but Vulkan wants the scaling lists as originally defined.
---
 libavcodec/vulkan_h264.c | 18 ++
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index f38bb9057b..9fe0d194f5 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -145,12 +145,13 @@ static void set_sps(const SPS *sps,
 };
 
 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
-memcpy(vksps_scaling->ScalingList4x4[i], sps->scaling_matrix4[i],
-   STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS * 
sizeof(**sps->scaling_matrix4));
+for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS; j++)
+vksps_scaling->ScalingList4x4[i][j] = 
sps->scaling_matrix4[i][ff_zigzag_scan[j]];
 
 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; i++)
-memcpy(vksps_scaling->ScalingList8x8[i], 
sps->scaling_matrix8[h264_scaling_list8_order[i]],
-   STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS * 
sizeof(**sps->scaling_matrix8));
+for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS; j++)
+vksps_scaling->ScalingList8x8[i][j] =
+
sps->scaling_matrix8[h264_scaling_list8_order[i]][ff_zigzag_direct[j]];
 
 *vksps_vui_header = (StdVideoH264HrdParameters) {
 .cpb_cnt_minus1 = sps->cpb_cnt - 1,
@@ -250,12 +251,13 @@ static void set_pps(const PPS *pps, const SPS *sps,
 };
 
 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
-memcpy(vkpps_scaling->ScalingList4x4[i], pps->scaling_matrix4[i],
-   STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS * 
sizeof(**pps->scaling_matrix4));
+for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS; j++)
+vkpps_scaling->ScalingList4x4[i][j] = 
pps->scaling_matrix4[i][ff_zigzag_scan[j]];
 
 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; i++)
-memcpy(vkpps_scaling->ScalingList8x8[i], 
pps->scaling_matrix8[h264_scaling_list8_order[i]],
-   STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS * 
sizeof(**pps->scaling_matrix8));
+for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS; j++)
+vkpps_scaling->ScalingList8x8[i][j] =
+
pps->scaling_matrix8[h264_scaling_list8_order[i]][ff_zigzag_direct[j]];
 
 *vkpps = (StdVideoH264PictureParameterSet) {
 .seq_parameter_set_id = pps->sps_id,
-- 
2.41.0

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


[FFmpeg-devel] [PATCH] hwcontext_vulkan: guard unistd.h include

2023-09-22 Thread Benjamin Cheng via ffmpeg-devel
win32 typically doesn't have unistd.h, so always including it will break
MSVC builds. The usage of those POSIX functions are already guarded by
_WIN32, so use that to guard unistd.h include as well.
---
 libavutil/hwcontext_vulkan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index c676f4fc57..6c573abb8b 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -27,10 +27,10 @@
 #include "compat/w32dlfcn.h"
 #else
 #include 
+#include 
 #endif
 
 #include "thread.h"
-#include 
 
 #include "config.h"
 #include "pixdesc.h"
-- 
2.42.0

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


[FFmpeg-devel] [PATCH] vulkan_h264: fix long-term ref handling

2023-09-22 Thread Benjamin Cheng via ffmpeg-devel
h->long_ref isn't guaranteed to be contiguously filled. Use the approach
from both vaapi_h264 and vdpau_h264 which goes through the 16 frames in
h->long_ref to find the LTR entries.

Fixes MR2_MW_A.264 from JVT-AVC_V1.
---
 libavcodec/vulkan_h264.c | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index 32ef32d640..4135188e7a 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -406,10 +406,14 @@ static int vk_h264_start_frame(AVCodecContext  
*avctx,
 }
 
 /* Fill in long-term refs */
-for (int r = 0, i = h->short_ref_count; i < h->short_ref_count + 
h->long_ref_count; i++, r++) {
+for (int r = 0, i = h->short_ref_count; r < H264_MAX_DPB_FRAMES &&
+ i < h->short_ref_count + h->long_ref_count; r++) {
+   if (!h->long_ref[r])
+   continue;
+
 dpb_slot_index = 0;
-for (unsigned slot = 0; slot < H264_MAX_PICTURE_COUNT; slot++) {
-if (h->long_ref[i] == &h->DPB[slot]) {
+for (unsigned slot = 0; slot < 16; slot++) {
+if (h->long_ref[r] == &h->DPB[slot]) {
 dpb_slot_index = slot;
 break;
 }
@@ -422,6 +426,7 @@ static int vk_h264_start_frame(AVCodecContext  
*avctx,
 dpb_slot_index);
 if (err < 0)
 return err;
+   i++;
 }
 
 hp->h264pic = (StdVideoDecodeH264PictureInfo) {
-- 
2.42.0

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


[FFmpeg-devel] [PATCH] vulkan_hevc: handle non-contiguous SPS/PPS/VPS ids

2023-09-30 Thread Benjamin Cheng via ffmpeg-devel
Some clips (i.e. SLIST_B_Sony_9) will use PPS 0 and 8, before PPS 1-7.
vulkan_hevc expects {sps,pps,vps}_list to be filled in order, which
causes PPS 8 to not be added to the Vulkan session params when it is
being used by a picture.

This removes the expectation that these lists are filled in order. The
indicies into vps_list are saved since there are multiple usages of it.

This also fixes a bug with some clips (i.e. PPS_A_qualcomm_7) which use
all 64 available PPS slots, causing the old loop to think there are more
than 64 PPS-es.
---
 libavcodec/vulkan_hevc.c | 47 +++-
 1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index 52f223ceb2..a8265acf32 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -70,6 +70,7 @@ typedef struct HEVCHeaderSet {
 
 static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
  int nb_vps,
+ const int 
vps_list_idx[HEVC_MAX_VPS_COUNT],
  AVBufferRef * const 
vps_list[HEVC_MAX_VPS_COUNT])
 {
 uint8_t *data_ptr;
@@ -77,7 +78,7 @@ static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
 
 size_t buf_size = sizeof(HEVCHeaderSet) + nb_vps*sizeof(HEVCHeaderVPS);
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps = (const HEVCVPS *)vps_list[i]->data;
+const HEVCVPS *vps = (const HEVCVPS *)vps_list[vps_list_idx[i]]->data;
 buf_size += sizeof(HEVCHeaderVPSSet)*vps->vps_num_hrd_parameters;
 }
 
@@ -96,7 +97,7 @@ static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
 hdr->hvps = (HEVCHeaderVPS *)(data_ptr + sizeof(HEVCHeaderSet));
 data_ptr += sizeof(HEVCHeaderSet) + nb_vps*sizeof(HEVCHeaderVPS);
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps = (const HEVCVPS *)vps_list[i]->data;
+const HEVCVPS *vps = (const HEVCVPS *)vps_list[vps_list_idx[i]]->data;
 hdr->hvps[i].sls = (HEVCHeaderVPSSet *)data_ptr;
 data_ptr += sizeof(HEVCHeaderVPSSet)*vps->vps_num_hrd_parameters;
 }
@@ -655,13 +656,15 @@ static int vk_hevc_create_params(AVCodecContext *avctx, 
AVBufferRef **buf)
 .videoSessionParametersTemplate = NULL,
 };
 
-int nb_vps = 0;
 HEVCHeaderSet *hdr;
+int nb_vps = 0;
+int vps_list_idx[HEVC_MAX_VPS_COUNT];
 
-for (int i = 0; h->ps.vps_list[i]; i++)
-nb_vps++;
+for (int i = 0; i < HEVC_MAX_VPS_COUNT; i++)
+if (h->ps.vps_list[i])
+vps_list_idx[nb_vps++] = i;
 
-err = alloc_hevc_header_structs(dec, nb_vps, h->ps.vps_list);
+err = alloc_hevc_header_structs(dec, nb_vps, vps_list_idx, h->ps.vps_list);
 if (err < 0)
 return err;
 
@@ -672,29 +675,33 @@ static int vk_hevc_create_params(AVCodecContext *avctx, 
AVBufferRef **buf)
 h265_params_info.pStdVPSs = hdr->vps;
 
 /* SPS list */
-for (int i = 0; h->ps.sps_list[i]; i++) {
-const HEVCSPS *sps_l = (const HEVCSPS *)h->ps.sps_list[i]->data;
-set_sps(sps_l, i, &hdr->hsps[i].scaling, &hdr->hsps[i].vui_header,
-&hdr->hsps[i].vui, &hdr->sps[i], hdr->hsps[i].nal_hdr,
-hdr->hsps[i].vcl_hdr, &hdr->hsps[i].ptl, &hdr->hsps[i].dpbm,
-&hdr->hsps[i].pal, hdr->hsps[i].str, &hdr->hsps[i].ltr);
-h265_params_info.stdSPSCount++;
+for (int i = 0; i < HEVC_MAX_SPS_COUNT; i++) {
+if (h->ps.sps_list[i]) {
+const HEVCSPS *sps_l = (const HEVCSPS *)h->ps.sps_list[i]->data;
+int idx = h265_params_info.stdSPSCount++;
+set_sps(sps_l, i, &hdr->hsps[idx].scaling, 
&hdr->hsps[idx].vui_header,
+&hdr->hsps[idx].vui, &hdr->sps[idx], 
hdr->hsps[idx].nal_hdr,
+hdr->hsps[idx].vcl_hdr, &hdr->hsps[idx].ptl, 
&hdr->hsps[idx].dpbm,
+&hdr->hsps[idx].pal, hdr->hsps[idx].str, 
&hdr->hsps[idx].ltr);
+}
 }
 
 /* PPS list */
-for (int i = 0; h->ps.pps_list[i]; i++) {
-const HEVCPPS *pps_l = (const HEVCPPS *)h->ps.pps_list[i]->data;
-const HEVCSPS *sps_l = (const HEVCSPS 
*)h->ps.sps_list[pps_l->sps_id]->data;
-set_pps(pps_l, sps_l, &hdr->hpps[i].scaling, &hdr->pps[i], 
&hdr->hpps[i].pal);
-h265_params_info.stdPPSCount++;
+for (int i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
+if (h->ps.pps_list[i]) {
+const HEVCPPS *pps_l = (const HEVCPPS *)h->ps.pps_list[i]->data;
+const HEVCSPS *sps_l = (const HEVCSPS 
*)h->ps.sps_list[pps_l->sps_id]->data;
+int idx = h265_params_info.stdPPSCount++;
+set_pps(pps_l, sps_l, &hdr->hpps[idx].scaling,
+&hdr->pps[idx], &hdr->hpps[idx].pal);
+}
 }
 
 /* VPS list */
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps_l = (const HEVCVPS *)h->ps.vps_list[i]->data;
+const HEVCVPS *vps_l = (

[FFmpeg-devel] [PATCH v2] vulkan_hevc: handle non-contiguous SPS/PPS/VPS ids

2023-09-30 Thread Benjamin Cheng via ffmpeg-devel
Some clips (i.e. SLIST_B_Sony_9) will use PPS 0 and 8, before PPS 1-7.
vulkan_hevc expects {sps,pps,vps}_list to be filled in order, which
causes PPS 8 to not be added to the Vulkan session params when it is
being used by a picture.

This removes the expectation that these lists are filled in order. The
indicies into vps_list are saved since there are multiple usages of it.

This also fixes a bug with some clips (i.e. PPS_A_qualcomm_7) which use
all 64 available PPS slots, causing the old loop to think there are more
than 64 PPS-es.
---
 libavcodec/vulkan_hevc.c | 46 +++-
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index 52f223ceb2..9685ec14c8 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -70,6 +70,7 @@ typedef struct HEVCHeaderSet {
 
 static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
  int nb_vps,
+ const int 
vps_list_idx[HEVC_MAX_VPS_COUNT],
  AVBufferRef * const 
vps_list[HEVC_MAX_VPS_COUNT])
 {
 uint8_t *data_ptr;
@@ -77,7 +78,7 @@ static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
 
 size_t buf_size = sizeof(HEVCHeaderSet) + nb_vps*sizeof(HEVCHeaderVPS);
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps = (const HEVCVPS *)vps_list[i]->data;
+const HEVCVPS *vps = (const HEVCVPS *)vps_list[vps_list_idx[i]]->data;
 buf_size += sizeof(HEVCHeaderVPSSet)*vps->vps_num_hrd_parameters;
 }
 
@@ -96,7 +97,7 @@ static int alloc_hevc_header_structs(FFVulkanDecodeContext *s,
 hdr->hvps = (HEVCHeaderVPS *)(data_ptr + sizeof(HEVCHeaderSet));
 data_ptr += sizeof(HEVCHeaderSet) + nb_vps*sizeof(HEVCHeaderVPS);
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps = (const HEVCVPS *)vps_list[i]->data;
+const HEVCVPS *vps = (const HEVCVPS *)vps_list[vps_list_idx[i]]->data;
 hdr->hvps[i].sls = (HEVCHeaderVPSSet *)data_ptr;
 data_ptr += sizeof(HEVCHeaderVPSSet)*vps->vps_num_hrd_parameters;
 }
@@ -655,13 +656,15 @@ static int vk_hevc_create_params(AVCodecContext *avctx, 
AVBufferRef **buf)
 .videoSessionParametersTemplate = NULL,
 };
 
-int nb_vps = 0;
 HEVCHeaderSet *hdr;
+int nb_vps = 0;
+int vps_list_idx[HEVC_MAX_VPS_COUNT];
 
-for (int i = 0; h->ps.vps_list[i]; i++)
-nb_vps++;
+for (int i = 0; i < HEVC_MAX_VPS_COUNT; i++)
+if (h->ps.vps_list[i])
+vps_list_idx[nb_vps++] = i;
 
-err = alloc_hevc_header_structs(dec, nb_vps, h->ps.vps_list);
+err = alloc_hevc_header_structs(dec, nb_vps, vps_list_idx, h->ps.vps_list);
 if (err < 0)
 return err;
 
@@ -672,26 +675,31 @@ static int vk_hevc_create_params(AVCodecContext *avctx, 
AVBufferRef **buf)
 h265_params_info.pStdVPSs = hdr->vps;
 
 /* SPS list */
-for (int i = 0; h->ps.sps_list[i]; i++) {
-const HEVCSPS *sps_l = (const HEVCSPS *)h->ps.sps_list[i]->data;
-set_sps(sps_l, i, &hdr->hsps[i].scaling, &hdr->hsps[i].vui_header,
-&hdr->hsps[i].vui, &hdr->sps[i], hdr->hsps[i].nal_hdr,
-hdr->hsps[i].vcl_hdr, &hdr->hsps[i].ptl, &hdr->hsps[i].dpbm,
-&hdr->hsps[i].pal, hdr->hsps[i].str, &hdr->hsps[i].ltr);
-h265_params_info.stdSPSCount++;
+for (int i = 0; i < HEVC_MAX_SPS_COUNT; i++) {
+if (h->ps.sps_list[i]) {
+const HEVCSPS *sps_l = (const HEVCSPS *)h->ps.sps_list[i]->data;
+int idx = h265_params_info.stdSPSCount++;
+set_sps(sps_l, i, &hdr->hsps[idx].scaling, 
&hdr->hsps[idx].vui_header,
+&hdr->hsps[idx].vui, &hdr->sps[idx], 
hdr->hsps[idx].nal_hdr,
+hdr->hsps[idx].vcl_hdr, &hdr->hsps[idx].ptl, 
&hdr->hsps[idx].dpbm,
+&hdr->hsps[idx].pal, hdr->hsps[idx].str, 
&hdr->hsps[idx].ltr);
+}
 }
 
 /* PPS list */
-for (int i = 0; h->ps.pps_list[i]; i++) {
-const HEVCPPS *pps_l = (const HEVCPPS *)h->ps.pps_list[i]->data;
-const HEVCSPS *sps_l = (const HEVCSPS 
*)h->ps.sps_list[pps_l->sps_id]->data;
-set_pps(pps_l, sps_l, &hdr->hpps[i].scaling, &hdr->pps[i], 
&hdr->hpps[i].pal);
-h265_params_info.stdPPSCount++;
+for (int i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
+if (h->ps.pps_list[i]) {
+const HEVCPPS *pps_l = (const HEVCPPS *)h->ps.pps_list[i]->data;
+const HEVCSPS *sps_l = (const HEVCSPS 
*)h->ps.sps_list[pps_l->sps_id]->data;
+int idx = h265_params_info.stdPPSCount++;
+set_pps(pps_l, sps_l, &hdr->hpps[idx].scaling,
+&hdr->pps[idx], &hdr->hpps[idx].pal);
+}
 }
 
 /* VPS list */
 for (int i = 0; i < nb_vps; i++) {
-const HEVCVPS *vps_l = (const HEVCVPS *)h->ps.vps_list[i]->data;
+const HEVCVPS *vps_l = (

Re: [FFmpeg-devel] [PATCH 1/2] lavc: add Vulkan video encoding base code

2024-09-10 Thread Benjamin Cheng via ffmpeg-devel
On Mon Sep 9, 2024 at 6:37 AM EDT, Lynne via ffmpeg-devel wrote:
> This commit adds the common Vulkan video encoding framework.
> It makes full use of the asynchronous features of our new common
> hardware encoding code, and of Vulkan.
> The code is able to handle anything from H264 to AV1 and MJPEG.
> ---
>  configure  |   2 +
>  libavcodec/Makefile|   2 +-
>  libavcodec/vulkan_encode.c | 979 +
>  libavcodec/vulkan_encode.h | 243 +
>  4 files changed, 1225 insertions(+), 1 deletion(-)
>  create mode 100644 libavcodec/vulkan_encode.c
>  create mode 100644 libavcodec/vulkan_encode.h
>
> diff --git a/configure b/configure
> index a8e67d230c..6cfb736a86 100755
> --- a/configure
> +++ b/configure
> @@ -2638,6 +2638,7 @@ CONFIG_EXTRA="
>  vp3dsp
>  vp56dsp
>  vp8dsp
> +vulkan_encode
>  wma_freqs
>  wmv2dsp
>  "
> @@ -3299,6 +3300,7 @@ qsvdec_select="qsv"
>  qsvenc_select="qsv"
>  qsvvpp_select="qsv"
>  vaapi_encode_deps="vaapi"
> +vulkan_encode_deps="vulkan"
>  v4l2_m2m_deps="linux_videodev2_h sem_timedwait"
>  
>  bilateral_cuda_filter_deps="ffnvcodec"
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 27ef4638ce..ff6a3c4efc 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1282,7 +1282,7 @@ SKIPHEADERS-$(CONFIG_QSVENC)   += qsvenc.h
>  SKIPHEADERS-$(CONFIG_VAAPI)+= vaapi_decode.h vaapi_hevc.h 
> vaapi_encode.h
>  SKIPHEADERS-$(CONFIG_VDPAU)+= vdpau.h vdpau_internal.h
>  SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX) += videotoolbox.h vt_internal.h
> -SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> vulkan_decode.h
> +SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> vulkan_encode.h vulkan_decode.h
>  SKIPHEADERS-$(CONFIG_V4L2_M2M) += v4l2_buffers.h v4l2_context.h 
> v4l2_m2m.h
>  SKIPHEADERS-$(CONFIG_ZLIB) += zlib_wrapper.h
>  
> diff --git a/libavcodec/vulkan_encode.c b/libavcodec/vulkan_encode.c
> new file mode 100644
> index 00..5e87d4c073
> --- /dev/null
> +++ b/libavcodec/vulkan_encode.c
> @@ -0,0 +1,979 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + */
> +
> +#include "libavutil/mem.h"
> +#include "libavutil/avassert.h"
> +#include "vulkan_encode.h"
> +#include "config.h"
> +
> +#include "libavutil/vulkan_loader.h"
> +
> +const AVCodecHWConfigInternal *const ff_vulkan_encode_hw_configs[] = {
> +HW_CONFIG_ENCODER_FRAMES(VULKAN, VULKAN),
> +NULL,
> +};
> +
> +av_cold void ff_vulkan_encode_uninit(FFVulkanEncodeContext *ctx)
> +{
> +FFVulkanContext *s = &ctx->s;
> +FFVulkanFunctions *vk = &s->vkfn;
> +
> +/* Wait on and free execution pool */
> +ff_vk_exec_pool_free(s, &ctx->enc_pool);
> +
> +/* Destroy the session params */
> +if (ctx->session_params)
> +vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev,
> + ctx->session_params,
> + s->hwctx->alloc);
> +
> +ff_hw_base_encode_close(&ctx->base);
> +
> +av_buffer_pool_uninit(&ctx->buf_pool);
> +
> +ff_vk_video_common_uninit(s, &ctx->common);
> +
> +ff_vk_uninit(s);
> +}
> +
> +static int vulkan_encode_init(AVCodecContext *avctx, FFHWBaseEncodePicture 
> *pic)
> +{
> +int err;
> +FFVulkanEncodeContext *ctx = avctx->priv_data;
> +FFVulkanEncodePicture *vp = pic->priv;
> +
> +AVFrame *f = pic->input_image;
> +AVHWFramesContext *hwfc = (AVHWFramesContext *)f->hw_frames_ctx->data;
> +AVVulkanFramesContext *vkfc = hwfc->hwctx;
> +AVVkFrame *vkf = (AVVkFrame *)f->data[0];
> +
> +if (ctx->codec->picture_priv_data_size > 0) {
> +pic->codec_priv = av_mallocz(ctx->codec->picture_priv_data_size);
> +if (!pic->codec_priv)
> +return AVERROR(ENOMEM);
> +}
> +
> +/* Input image view */
> +err = ff_vk_create_view(&ctx->s, &ctx->common,
> +&vp->in.view, &vp->in.aspect,
> +vkf, vkfc->format[0], 0);
> +if (err < 0)
> +return err;
> +
> +/* Reference view */
> +if (!ctx->common.layered_dpb) {
> +AVFrame *rf = pic-

Re: [FFmpeg-devel] [PATCH 2/2] lavc: add h264_vulkan hardware encoder

2024-09-10 Thread Benjamin Cheng via ffmpeg-devel
On Mon Sep 9, 2024 at 6:37 AM EDT, Lynne via ffmpeg-devel wrote:
> This commit adds the first Vulkan hardware encoder.
>
> Currently, P, and **B**-frames are supported. This marks the
> first implementation to support both.
>
> The encoder has feature-parity with VAAPI.
> ---
>  configure   |1 +
>  libavcodec/Makefile |3 +
>  libavcodec/allcodecs.c  |1 +
>  libavcodec/vulkan_encode_h264.c | 1687 +++
>  4 files changed, 1692 insertions(+)
>  create mode 100644 libavcodec/vulkan_encode_h264.c
>
> diff --git a/configure b/configure
> index 6cfb736a86..ebb8be73ad 100755
> --- a/configure
> +++ b/configure
> @@ -3366,6 +3366,7 @@ h264_qsv_encoder_select="atsc_a53 qsvenc"
>  h264_rkmpp_decoder_deps="rkmpp"
>  h264_rkmpp_decoder_select="h264_mp4toannexb_bsf"
>  h264_vaapi_encoder_select="atsc_a53 cbs_h264 vaapi_encode"
> +h264_vulkan_encoder_select="cbs_h264 vulkan_encode"
>  h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m"
>  h264_v4l2m2m_decoder_select="h264_mp4toannexb_bsf"
>  h264_v4l2m2m_encoder_deps="v4l2_m2m h264_v4l2_m2m"
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index ff6a3c4efc..d4eefa28a2 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -429,6 +429,9 @@ OBJS-$(CONFIG_H264_VAAPI_ENCODER)  += 
> vaapi_encode_h264.o h264_levels.o \
>  OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
>  OBJS-$(CONFIG_H264_V4L2M2M_DECODER)+= v4l2_m2m_dec.o
>  OBJS-$(CONFIG_H264_V4L2M2M_ENCODER)+= v4l2_m2m_enc.o
> +OBJS-$(CONFIG_H264_VULKAN_ENCODER) += vulkan_encode.o 
> vulkan_encode_h264.o \
> +  hw_base_encode.o 
> hw_base_encode_h264.o \
> +  h264_levels.o h2645data.o
>  OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o
>  OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o
>  OBJS-$(CONFIG_HCA_DECODER) += hcadec.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index d773ac36c2..cfd929b81f 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -850,6 +850,7 @@ extern const FFCodec ff_h264_qsv_encoder;
>  extern const FFCodec ff_h264_v4l2m2m_encoder;
>  extern const FFCodec ff_h264_vaapi_encoder;
>  extern const FFCodec ff_h264_videotoolbox_encoder;
> +extern const FFCodec ff_h264_vulkan_encoder;
>  extern const FFCodec ff_hevc_amf_encoder;
>  extern const FFCodec ff_hevc_cuvid_decoder;
>  extern const FFCodec ff_hevc_d3d12va_encoder;
> diff --git a/libavcodec/vulkan_encode_h264.c b/libavcodec/vulkan_encode_h264.c
> new file mode 100644
> index 00..529ac8aed2
> --- /dev/null
> +++ b/libavcodec/vulkan_encode_h264.c
> @@ -0,0 +1,1687 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + */
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/mem.h"
> +
> +#include "cbs.h"
> +#include "cbs_h264.h"
> +#include "atsc_a53.h"
> +
> +#include "h264_levels.h"
> +#include "h2645data.h"
> +#include "codec_internal.h"
> +#include "version.h"
> +#include "hw_base_encode_h264.h"
> +
> +#include "vulkan_encode.h"
> +
> +enum UnitElems {
> +UNIT_AUD= 1 << 0,
> +UNIT_SEI_TIMING = 1 << 1,
> +UNIT_SEI_IDENTIFIER = 1 << 2,
> +UNIT_SEI_RECOVERY   = 1 << 3,
> +UNIT_SEI_A53_CC = 1 << 4,
> +};
> +
> +const FFVulkanEncodeDescriptor ff_vk_enc_h264_desc = {
> +.codec_id = AV_CODEC_ID_H264,
> +.encode_extension = FF_VK_EXT_VIDEO_ENCODE_H264,
> +.encode_op= VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR,
> +.ext_props = {
> +.extensionName = 
> VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_EXTENSION_NAME,
> +.specVersion   = VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_SPEC_VERSION,
> +},
> +};
> +
> +/* Random (version 4) ISO 11578 UUID. */
> +static const uint8_t vulkan_encode_h264_sei_identifier_uuid[16] = {
> +0x03, 0xfd, 0xf2, 0x0a, 0x5d, 0x4c, 0x05, 0x48,
> +0x20, 0x98, 0xca, 0x6b, 0x0c, 0x95, 0x30, 0x1c,
> +};
> +
> +typedef struct VulkanEncodeH264Picture {
> +int frame_num;
> +int64_t last_idr_frame;
> +uint16_t idr_pic_id;
> +int primary_pic_type;
> +int slice_type;
> +int pic_order_cnt;
> +
> 

Re: [FFmpeg-devel] [PATCH 1/2] lavc: add Vulkan video encoding base code

2024-09-12 Thread Benjamin Cheng via ffmpeg-devel
On Wed Sep 11, 2024 at 12:03 AM EDT, Lynne wrote:
> On 10/09/2024 15:29, Benjamin Cheng wrote:
> > On Mon Sep 9, 2024 at 6:37 AM EDT, Lynne via ffmpeg-devel wrote:
> >> This commit adds the common Vulkan video encoding framework.
> >> It makes full use of the asynchronous features of our new common
> >> hardware encoding code, and of Vulkan.
> >> The code is able to handle anything from H264 to AV1 and MJPEG.
> >> ---
> >>   configure  |   2 +
> >>   libavcodec/Makefile|   2 +-
> >>   libavcodec/vulkan_encode.c | 979 +
> >>   libavcodec/vulkan_encode.h | 243 +
> >>   4 files changed, 1225 insertions(+), 1 deletion(-)
> >>   create mode 100644 libavcodec/vulkan_encode.c
> >>   create mode 100644 libavcodec/vulkan_encode.h
> >>
> >> diff --git a/configure b/configure
> >> index a8e67d230c..6cfb736a86 100755
> >> --- a/configure
> >> +++ b/configure
> >> @@ -2638,6 +2638,7 @@ CONFIG_EXTRA="
> >>   vp3dsp
> >>   vp56dsp
> >>   vp8dsp
> >> +vulkan_encode
> >>   wma_freqs
> >>   wmv2dsp
> >>   "
> >> @@ -3299,6 +3300,7 @@ qsvdec_select="qsv"
> >>   qsvenc_select="qsv"
> >>   qsvvpp_select="qsv"
> >>   vaapi_encode_deps="vaapi"
> >> +vulkan_encode_deps="vulkan"
> >>   v4l2_m2m_deps="linux_videodev2_h sem_timedwait"
> >>   
> >>   bilateral_cuda_filter_deps="ffnvcodec"
> >> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> >> index 27ef4638ce..ff6a3c4efc 100644
> >> --- a/libavcodec/Makefile
> >> +++ b/libavcodec/Makefile
> >> @@ -1282,7 +1282,7 @@ SKIPHEADERS-$(CONFIG_QSVENC)   += qsvenc.h
> >>   SKIPHEADERS-$(CONFIG_VAAPI)+= vaapi_decode.h vaapi_hevc.h 
> >> vaapi_encode.h
> >>   SKIPHEADERS-$(CONFIG_VDPAU)+= vdpau.h vdpau_internal.h
> >>   SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX) += videotoolbox.h vt_internal.h
> >> -SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> >> vulkan_decode.h
> >> +SKIPHEADERS-$(CONFIG_VULKAN)   += vulkan.h vulkan_video.h 
> >> vulkan_encode.h vulkan_decode.h
> >>   SKIPHEADERS-$(CONFIG_V4L2_M2M) += v4l2_buffers.h v4l2_context.h 
> >> v4l2_m2m.h
> >>   SKIPHEADERS-$(CONFIG_ZLIB) += zlib_wrapper.h
> >>   
> >> diff --git a/libavcodec/vulkan_encode.c b/libavcodec/vulkan_encode.c
> >> new file mode 100644
> >> index 00..5e87d4c073
> >> --- /dev/null
> >> +++ b/libavcodec/vulkan_encode.c
> >> @@ -0,0 +1,979 @@
> >> +/*
> >> + * This file is part of FFmpeg.
> >> + *
> >> + * FFmpeg is free software; you can redistribute it and/or
> >> + * modify it under the terms of the GNU Lesser General Public
> >> + * License as published by the Free Software Foundation; either
> >> + * version 2.1 of the License, or (at your option) any later version.
> >> + *
> >> + * FFmpeg is distributed in the hope that it will be useful,
> >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> >> + * Lesser General Public License for more details.
> >> + *
> >> + * You should have received a copy of the GNU Lesser General Public
> >> + * License along with FFmpeg; if not, write to the Free Software
> >> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
> >> 02110-1301 USA
> >> + */
> >> +
> >> +#include "libavutil/mem.h"
> >> +#include "libavutil/avassert.h"
> >> +#include "vulkan_encode.h"
> >> +#include "config.h"
> >> +
> >> +#include "libavutil/vulkan_loader.h"
> >> +
> >> +const AVCodecHWConfigInternal *const ff_vulkan_encode_hw_configs[] = {
> >> +HW_CONFIG_ENCODER_FRAMES(VULKAN, VULKAN),
> >> +NULL,
> >> +};
> >> +
> >> +av_cold void ff_vulkan_encode_uninit(FFVulkanEncodeContext *ctx)
> >> +{
> >> +FFVulkanContext *s = &ctx->s;
> >> +FFVulkanFunctions *vk = &s->vkfn;
> >> +
> >> +/* Wait on and free execution pool */
> >> +ff_vk_exec_pool_free(s, &ctx->enc_pool);
> >> +
> >> +/* Destroy the session params */
> >> +if (ctx->session_params)
> >> +vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev,
> >> + ctx->session_params,
> >> + s->hwctx->alloc);
> >> +
> >> +ff_hw_base_encode_close(&ctx->base);
> >> +
> >> +av_buffer_pool_uninit(&ctx->buf_pool);
> >> +
> >> +ff_vk_video_common_uninit(s, &ctx->common);
> >> +
> >> +ff_vk_uninit(s);
> >> +}
> >> +
> >> +static int vulkan_encode_init(AVCodecContext *avctx, 
> >> FFHWBaseEncodePicture *pic)
> >> +{
> >> +int err;
> >> +FFVulkanEncodeContext *ctx = avctx->priv_data;
> >> +FFVulkanEncodePicture *vp = pic->priv;
> >> +
> >> +AVFrame *f = pic->input_image;
> >> +AVHWFramesContext *hwfc = (AVHWFramesContext *)f->hw_frames_ctx->data;
> >> +AVVulkanFramesContext *vkfc = hwfc->hwctx;
> >> +AVVkFrame *vkf = (AVVkFrame *)f->data[0];
> >> +
> >> +if (ctx->codec->picture_priv_data_size > 0) {
> >> +

[FFmpeg-devel] [PATCH] vulkan_decode: remove yuv sampler for DPB images

2024-10-11 Thread Benjamin Cheng via ffmpeg-devel
The YCbCr sampler is only required for multi-plane images if the image
is created with SAMPLED usage, and the image view is created with
aspectMask = COLOR. For DPB images, it is expected that some
implementations will have opaque DPBs (i.e. they do not support
SAMPLED/TRANSFER/STORAGE usages).

Since ffmpeg doesn't use DPB images in a shader anyways, the SAMPLED
usage was not necessary.

A run with this change did not introduce any new validation errors on
RADV.
---
 libavcodec/vulkan_decode.c | 3 +--
 libavcodec/vulkan_video.c  | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index 7d7295e05e..12472b5cb4 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -1191,8 +1191,7 @@ int ff_vk_decode_init(AVCodecContext *avctx)

VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
 dpb_hwfc->format[0]= s->hwfc->format[0];
 dpb_hwfc->tiling   = VK_IMAGE_TILING_OPTIMAL;
-dpb_hwfc->usage= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR |
- VK_IMAGE_USAGE_SAMPLED_BIT; /* Shuts 
validator up. */
+dpb_hwfc->usage= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
 
 if (ctx->common.layered_dpb)
 dpb_hwfc->nb_layers = ctx->caps.maxDpbSlots;
diff --git a/libavcodec/vulkan_video.c b/libavcodec/vulkan_video.c
index 3a04d60d68..2acbbfa05e 100644
--- a/libavcodec/vulkan_video.c
+++ b/libavcodec/vulkan_video.c
@@ -292,7 +292,7 @@ int ff_vk_create_view(FFVulkanContext *s, FFVkVideoCommon 
*common,
 };
 VkImageViewCreateInfo img_view_create_info = {
 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
-.pNext = &yuv_sampler_info,
+.pNext = common->layered_dpb && is_dpb ? NULL : &yuv_sampler_info,
 .viewType = common->layered_dpb && is_dpb ?
 VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D,
 .format = vkf,
-- 
2.47.0

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avcodec/amfenc: DX12 Reference-only feature support

2025-01-30 Thread Benjamin Cheng via ffmpeg-devel

On 2025-01-30 9:09 a.m., Araz Iusubov wrote:

The Reference-Only feature in DirectX 12 is a memory optimization
technique designed for video decoding scenarios.
This feature requires that reference resources must be allocated with
the D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY resource flag.
Reference textures must also be separated from output textures.
This feature is not supported in the current version of ffmpeg.
Since AMD GPU uses this feature in Direct 12 decoder,
ffmpeg does not support AMD GPU Direct 12 decoding.
To properly support the Reference-Only feature,
two parallel resource pools must be configured and managed:
General Resource Pool:
Contains resources used for output decoded frames.
Defined in AVHWFramesContext and manages the final decoded textures.
Reference-Only Resource Pool:
Intended for storing reference frame resources.
Resources created with the
D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY flag
are allocated to AVBufferPool.

---
  libavcodec/d3d12va_decode.c   | 58 ---
  libavutil/hwcontext_d3d12va.c | 65 ---
  2 files changed, 115 insertions(+), 8 deletions(-)


This patch only affects d3d12va, why is the commit message amfenc?



diff --git a/libavcodec/d3d12va_decode.c b/libavcodec/d3d12va_decode.c
index 3b8978635e..8916f94d10 100644
--- a/libavcodec/d3d12va_decode.c
+++ b/libavcodec/d3d12va_decode.c
@@ -51,11 +51,19 @@ unsigned ff_d3d12va_get_surface_index(const AVCodecContext 
*avctx,
D3D12VADecodeContext *ctx, const 
AVFrame *frame,
int curr)
  {
+AVHWFramesContext  *frames_ctx   = D3D12VA_FRAMES_CONTEXT(avctx);
+AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx;
+
  AVD3D12VAFrame *f;
  ID3D12Resource *res;
  unsigned i;
  
-f = (AVD3D12VAFrame *)frame->data[0];

+if (frames_hwctx->flags & D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY) 
{
+f = (AVD3D12VAFrame*)frame->data[1];
+} else {
+f = (AVD3D12VAFrame*)frame->data[0];
+}
+
  if (!f)
  goto fail;
  
@@ -250,6 +258,11 @@ static int d3d12va_create_decoder(AVCodecContext *avctx)

  return AVERROR_PATCHWELCOME;
  }
  
+if (feature.ConfigurationFlags & D3D12_VIDEO_DECODE_CONFIGURATION_FLAG_REFERENCE_ONLY_ALLOCATIONS_REQUIRED) {

+frames_hwctx->flags |= 
(D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | 
D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
+av_log(avctx, AV_LOG_INFO, "Reference-Only Allocations are required for 
this configuration.\n");
+}
+
  desc = (D3D12_VIDEO_DECODER_DESC) {
  .NodeMask = 0,
  .Configuration = ctx->cfg,
@@ -440,8 +453,19 @@ int ff_d3d12va_common_end_frame(AVCodecContext *avctx, 
AVFrame *frame,
  D3D12VADecodeContext   *ctx   = D3D12VA_DECODE_CONTEXT(avctx);
  ID3D12Resource *buffer= NULL;
  ID3D12CommandAllocator *command_allocator = NULL;
-AVD3D12VAFrame *f = (AVD3D12VAFrame 
*)frame->data[0];
-ID3D12Resource *resource  = (ID3D12Resource *)f->texture;
+AVHWFramesContext  *frames_ctx= D3D12VA_FRAMES_CONTEXT(avctx);
+AVD3D12VAFramesContext *frames_hwctx  = frames_ctx->hwctx;
+AVD3D12VAFrame *f = NULL;
+AVD3D12VAFrame *output_data   = NULL;
+
+if (frames_hwctx->flags & D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY) 
{
+f   = (AVD3D12VAFrame*)frame->data[1];
+output_data = (AVD3D12VAFrame*)frame->data[0];
+} else {
+f   = (AVD3D12VAFrame*)frame->data[0];
+}
+
+ID3D12Resource* resource = (ID3D12Resource*)f->texture;
  
  ID3D12VideoDecodeCommandList *cmd_list = ctx->command_list;

  D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
@@ -469,6 +493,14 @@ int ff_d3d12va_common_end_frame(AVCodecContext *avctx, 
AVFrame *frame,
  .pOutputTexture2D= resource,
  };
  
+if (frames_hwctx->flags & D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY) {

+output_args.pOutputTexture2D = output_data->texture;
+
+output_args.ConversionArguments.Enable   = 1;
+output_args.ConversionArguments.pReferenceTexture2D  = resource;
+output_args.ConversionArguments.ReferenceSubresource = 0;
+}
+
  UINT num_barrier = 1;
  barriers[0] = (D3D12_RESOURCE_BARRIER) {
  .Type  = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
@@ -481,6 +513,20 @@ int ff_d3d12va_common_end_frame(AVCodecContext *avctx, 
AVFrame *frame,
  },
  };
  
+if (frames_hwctx->flags & D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY) {

+barriers[1] = (D3D12_RESOURCE_BARRIER) {
+.Type  = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
+.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
+.Transition = {
+.pResource   = output_data->texture

[FFmpeg-devel] [PATCH] avcodec/hw_base_encode: restrict size of next_prev

2024-12-21 Thread Benjamin Cheng via ffmpeg-devel
Some drivers are more strict about the size of the reference lists given
(i.e. VAOn12 [1]). The next_prev list is used to handle multiple "L0"
references in AV1 encode. Restrict the size of next_prev based on the
value of ref_l0 when the GOP structure is initialized.

[1] https://github.com/intel/cartwheel-ffmpeg/issues/278
---
 libavcodec/hw_base_encode.c | 7 +--
 libavcodec/hw_base_encode.h | 4 
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 4d8bf4fe71..2478e08414 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -190,12 +190,12 @@ static void 
hw_base_encode_add_next_prev(FFHWBaseEncodeContext *ctx,
 return;
 }
 
-if (ctx->nb_next_prev < MAX_PICTURE_REFERENCES) {
+if (ctx->nb_next_prev < ctx->ref_l0) {
 ctx->next_prev[ctx->nb_next_prev++] = pic;
 ++pic->ref_count[0];
 } else {
 --ctx->next_prev[0]->ref_count[0];
-for (i = 0; i < MAX_PICTURE_REFERENCES - 1; i++)
+for (i = 0; i < ctx->ref_l0 - 1; i++)
 ctx->next_prev[i] = ctx->next_prev[i + 1];
 ctx->next_prev[i] = pic;
 ++pic->ref_count[0];
@@ -662,6 +662,9 @@ int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext 
*ctx, AVCodecContext *av
   uint32_t ref_l0, uint32_t ref_l1,
   int flags, int prediction_pre_only)
 {
+   ctx->ref_l0 = min(ref_l0, MAX_PICTURE_REFERENCES);
+   ctx->ref_l1 = min(ref_l1, MAX_PICTURE_REFERENCES);
+
 if (flags & FF_HW_FLAG_INTRA_ONLY || avctx->gop_size <= 1) {
 av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n");
 ctx->gop_size = 1;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index e30b1e60ad..e768579722 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -193,6 +193,10 @@ typedef struct FFHWBaseEncodeContext {
 int end_of_stream;
 int p_to_gpb;
 
+// The number of L0/L1 references supported by the driver.
+int ref_l0;
+int ref_l1;
+
 // Whether the driver supports ROI at all.
 int roi_allowed;
 
-- 
2.47.1

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2] avcodec/hw_base_encode: restrict size of next_prev

2024-12-21 Thread Benjamin Cheng via ffmpeg-devel
Some drivers are more strict about the size of the reference lists given
(i.e. VAOn12 [1]). The next_prev list is used to handle multiple "L0"
references in AV1 encode. Restrict the size of next_prev based on the
value of ref_l0 when the GOP structure is initialized.

[1] https://github.com/intel/cartwheel-ffmpeg/issues/278

v2: fix indentation issues
---
 libavcodec/hw_base_encode.c | 7 +--
 libavcodec/hw_base_encode.h | 4 
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 4d8bf4fe71..b790091d7d 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -190,12 +190,12 @@ static void 
hw_base_encode_add_next_prev(FFHWBaseEncodeContext *ctx,
 return;
 }
 
-if (ctx->nb_next_prev < MAX_PICTURE_REFERENCES) {
+if (ctx->nb_next_prev < ctx->ref_l0) {
 ctx->next_prev[ctx->nb_next_prev++] = pic;
 ++pic->ref_count[0];
 } else {
 --ctx->next_prev[0]->ref_count[0];
-for (i = 0; i < MAX_PICTURE_REFERENCES - 1; i++)
+for (i = 0; i < ctx->ref_l0 - 1; i++)
 ctx->next_prev[i] = ctx->next_prev[i + 1];
 ctx->next_prev[i] = pic;
 ++pic->ref_count[0];
@@ -662,6 +662,9 @@ int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext 
*ctx, AVCodecContext *av
   uint32_t ref_l0, uint32_t ref_l1,
   int flags, int prediction_pre_only)
 {
+ctx->ref_l0 = FFMIN(ref_l0, MAX_PICTURE_REFERENCES);
+ctx->ref_l1 = FFMIN(ref_l1, MAX_PICTURE_REFERENCES);
+
 if (flags & FF_HW_FLAG_INTRA_ONLY || avctx->gop_size <= 1) {
 av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n");
 ctx->gop_size = 1;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index e30b1e60ad..e768579722 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -193,6 +193,10 @@ typedef struct FFHWBaseEncodeContext {
 int end_of_stream;
 int p_to_gpb;
 
+// The number of L0/L1 references supported by the driver.
+int ref_l0;
+int ref_l1;
+
 // Whether the driver supports ROI at all.
 int roi_allowed;
 
-- 
2.47.1

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avutil/vulkan_glslang: windows build improvements

2025-01-17 Thread Benjamin Cheng via ffmpeg-devel

On 2025-01-15 7:38 p.m., Koushik Dutta wrote:

For my use case I ended up creating a custom pkgconfig file. My
platform and compiler is windows and msvc.

I was unable to get the existing configure to work correctly with this
environment and opted for pkgconfig as the hook for my environment. Is
there a better way?


Usually Windows users would install the Vulkan SDK which also ships glslang.

Maybe checking if the directory $VULKAN_SDK/Include/glslang exists, and 
$VULKAN_SDK/Lib/glslang.lib exists could work?




Koush

On Tue, Jan 7, 2025 at 3:13 AM Lynne  wrote:




On 07/01/2025 14:46, Koushik Dutta wrote:

Searches pkg-config for glslang libraries.
Use libavutil thread.h wrapper rather than directly including pthread.h.

Signed-off-by: Koushik Dutta 
---
   configure  | 1 +
   libavutil/vulkan_glslang.c | 2 +-
   2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index b09e9d0436..9d7381de9c 100755
--- a/configure
+++ b/configure
@@ -6931,6 +6931,7 @@ enabled libharfbuzz   && require_pkg_config 
libharfbuzz harfbuzz hb.h hb_buf
   enabled libglslang && { check_lib spirv_compiler 
glslang/Include/glslang_c_interface.h glslang_initialize_process \
   -lglslang -lMachineIndependent -lGenericCodeGen \
   -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt 
-lSPIRV-Tools -lpthread -lstdc++ -lm ||
+check_pkg_config spirv_compiler glslang 
"glslang/Include/glslang_c_interface.h glslang/build_info.h" 
glslang_initialize_process ||


This shouldn't work because glslang is a c++ lib, check_pkg_config uses
the c compiler and linker, and no pkg config file for glslang I've ever
seen has worked well.
We used to have a pkg-config check with some hacks, but it kept breaking
on debian.


   require spirv_compiler 
glslang/Include/glslang_c_interface.h glslang_initialize_process \
   -lglslang -lMachineIndependent -lOSDependent 
-lHLSL -lOGLCompiler -lGenericCodeGen \
   -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt 
-lSPIRV-Tools -lpthread -lstdc++ -lm ; }
diff --git a/libavutil/vulkan_glslang.c b/libavutil/vulkan_glslang.c
index d116b35ec5..9a985a930f 100644
--- a/libavutil/vulkan_glslang.c
+++ b/libavutil/vulkan_glslang.c
@@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
USA
*/

-#include 
+#include "thread.h"


This is fine.



   #include 
   #include 


___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".

___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".


___
ffmpeg-devel mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".