From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 7BF384AE97
	for <ffmpegdev@gitmailbox.com>; Mon, 31 Mar 2025 02:39:10 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 19292687CD8;
	Mon, 31 Mar 2025 05:38:09 +0300 (EEST)
Received: from vidala.pars.ee (vidala.pars.ee [116.203.72.101])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0BCE687CAA
 for <ffmpeg-devel@ffmpeg.org>; Mon, 31 Mar 2025 05:37:56 +0300 (EEST)
DKIM-Signature: v=1; a=rsa-sha256; s=202405r; d=lynne.ee; c=relaxed/relaxed;
 h=Message-ID:Date:Subject:To:From; t=1743388674; bh=YVL+5HNYXtY6uwphSNzD8sx
 ESPTs0gbeucDhW+aKeQs=; b=Jm/2F5xL1//f3I62o6uvNo1vQEjeThx4oro+MZcvSO2rPAdtHT
 sD+sijxX9Frz58c4/FgGTLmdFW5P0nhopO/m2SpIkbySQiFjMJ6Atqqc/Z924w7yBGkGr8XbS+c
 HFIHlF2v4epD1qhepfRTJB9JB32Kh5bHtUMQELQyUhIuhZYQKHGP/UFlVM0FrIq5SMZuAHQwrev
 hhamFsyZIsJAMivXwSi5Baev5nIkjx/WwNQWjj9JI6tFdVOecZ+OSqVbOTWu9DucafpNzUkd4HS
 LbDRwr1loELgS9Akvien3optSVKJrMHgN7FolwxaQRnEj3/wqgyueAwOTogNs7Y/z0A==;
DKIM-Signature: v=1; a=ed25519-sha256; s=202405e; d=lynne.ee; c=relaxed/relaxed;
 h=Message-ID:Date:Subject:To:From; t=1743388674; bh=YVL+5HNYXtY6uwphSNzD8sx
 ESPTs0gbeucDhW+aKeQs=; b=wzJaXd9XCFURI0eB6idYGr7EOwlP79GW2lXWzop0UTjf/tw19L
 gvuVjS/2OULuM/OVtT3w+Uhn1K0f/XDZTxAQ==;
From: Lynne <dev@lynne.ee>
To: ffmpeg-devel@ffmpeg.org
Date: Mon, 31 Mar 2025 04:37:37 +0200
Message-ID: <20250331023753.285499-1-dev@lynne.ee>
X-Mailer: git-send-email 2.49.0
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH 1/6] vulkan_decode: generalize handling of
 slice offsets/nb
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Lynne <dev@lynne.ee>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250331023753.285499-1-dev@lynne.ee/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

This allows for the upcoming refactor.
---
 libavcodec/vulkan_av1.c    |  6 +++---
 libavcodec/vulkan_decode.c | 23 ++++++-----------------
 libavcodec/vulkan_decode.h |  5 +++--
 libavcodec/vulkan_ffv1.c   | 22 ++++++++--------------
 libavcodec/vulkan_h264.c   |  9 +++------
 libavcodec/vulkan_hevc.c   |  6 +++---
 6 files changed, 26 insertions(+), 45 deletions(-)

diff --git a/libavcodec/vulkan_av1.c b/libavcodec/vulkan_av1.c
index ccb88004b5..13df24a44b 100644
--- a/libavcodec/vulkan_av1.c
+++ b/libavcodec/vulkan_av1.c
@@ -577,9 +577,7 @@ static int vk_av1_decode_slice(AVCodecContext *avctx,
 
         err = ff_vk_decode_add_slice(avctx, vp,
                                      data + s->tile_group_info[i].tile_offset,
-                                     s->tile_group_info[i].tile_size, 0,
-                                     &ap->av1_pic_info.tileCount,
-                                     &ap->av1_pic_info.pTileOffsets);
+                                     s->tile_group_info[i].tile_size, 0);
         if (err < 0)
             return err;
     }
@@ -616,6 +614,8 @@ static int vk_av1_end_frame(AVCodecContext *avctx)
     }
 #endif
 
+    ap->av1_pic_info.pTileOffsets = vp->slice_offsets;
+    ap->av1_pic_info.tileCount = vp->nb_slices;
     if (!ap->av1_pic_info.tileCount)
         return 0;
 
diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index 93aa0ce5b3..48a206e3c0 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -249,33 +249,23 @@ int ff_vk_decode_prepare_frame_sdr(FFVulkanDecodeContext *dec, AVFrame *pic,
 }
 
 int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
-                           const uint8_t *data, size_t size, int add_startcode,
-                           uint32_t *nb_slices, const uint32_t **offsets)
+                           const uint8_t *data, size_t size, int add_startcode)
 {
     FFVulkanDecodeContext *dec = avctx->internal->hwaccel_priv_data;
     FFVulkanDecodeShared *ctx = dec->shared_ctx;
 
     static const uint8_t startcode_prefix[3] = { 0x0, 0x0, 0x1 };
     const size_t startcode_len = add_startcode ? sizeof(startcode_prefix) : 0;
-    const int nb = nb_slices ? *nb_slices : 0;
     uint8_t *slices;
-    uint32_t *slice_off;
     FFVkBuffer *vkbuf;
 
     size_t new_size = vp->slices_size + startcode_len + size +
                       ctx->caps.minBitstreamBufferSizeAlignment;
     new_size = FFALIGN(new_size, ctx->caps.minBitstreamBufferSizeAlignment);
 
-    if (offsets) {
-        slice_off = av_fast_realloc(dec->slice_off, &dec->slice_off_max,
-                                    (nb + 1)*sizeof(slice_off));
-        if (!slice_off)
-            return AVERROR(ENOMEM);
-
-        *offsets = dec->slice_off = slice_off;
-
-        slice_off[nb] = vp->slices_size;
-    }
+    /* Should never happen */
+    if (vp->nb_slices > FF_ARRAY_ELEMS(vp->slice_offsets))
+        return AVERROR(EINVAL);
 
     vkbuf = vp->slices_buf ? (FFVkBuffer *)vp->slices_buf->data : NULL;
     if (!vkbuf || vkbuf->size < new_size) {
@@ -321,9 +311,8 @@ int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
     /* Slice data */
     memcpy(slices + vp->slices_size + startcode_len, data, size);
 
-    if (nb_slices)
-        *nb_slices = nb + 1;
-
+    vp->slice_offsets[vp->nb_slices] = vp->slices_size;
+    vp->nb_slices = vp->nb_slices + 1;
     vp->slices_size += startcode_len + size;
 
     return 0;
diff --git a/libavcodec/vulkan_decode.h b/libavcodec/vulkan_decode.h
index cbd22b3591..4bd755c1f6 100644
--- a/libavcodec/vulkan_decode.h
+++ b/libavcodec/vulkan_decode.h
@@ -110,6 +110,8 @@ typedef struct FFVulkanDecodePicture {
     /* Slice data */
     AVBufferRef                    *slices_buf;
     size_t                          slices_size;
+    uint32_t                        slice_offsets[1024];
+    uint32_t                        nb_slices;
 
     /* Vulkan functions needed for destruction, as no other context is guaranteed to exist */
     PFN_vkWaitSemaphores            wait_semaphores;
@@ -158,8 +160,7 @@ int ff_vk_decode_prepare_frame_sdr(FFVulkanDecodeContext *dec, AVFrame *pic,
  * Add slice data to frame.
  */
 int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
-                           const uint8_t *data, size_t size, int add_startcode,
-                           uint32_t *nb_slices, const uint32_t **offsets);
+                           const uint8_t *data, size_t size, int add_startcode);
 
 /**
  * Decode a frame.
diff --git a/libavcodec/vulkan_ffv1.c b/libavcodec/vulkan_ffv1.c
index 17bfc943d4..7eb51b0dab 100644
--- a/libavcodec/vulkan_ffv1.c
+++ b/libavcodec/vulkan_ffv1.c
@@ -55,8 +55,6 @@ typedef struct FFv1VulkanDecodePicture {
     uint32_t max_context_count;
 
     AVBufferRef *slice_offset_buf;
-    uint32_t    *slice_offset;
-    int          slice_num;
 
     AVBufferRef *slice_status_buf;
     int crc_checked;
@@ -163,8 +161,6 @@ static int vk_ffv1_start_frame(AVCodecContext          *avctx,
     int is_rgb = !(f->colorspace == 0 && sw_format != AV_PIX_FMT_YA8) &&
                  !(sw_format == AV_PIX_FMT_YA8);
 
-    fp->slice_num = 0;
-
     for (int i = 0; i < f->quant_table_count; i++)
         fp->max_context_count = FFMAX(f->context_count[i], fp->max_context_count);
 
@@ -278,22 +274,20 @@ static int vk_ffv1_decode_slice(AVCodecContext *avctx,
 
     if (ctx->s.extensions & FF_VK_EXT_EXTERNAL_HOST_MEMORY) {
         FFVkBuffer *slices_buf = (FFVkBuffer *)vp->slices_buf->data;
-        AV_WN32(slice_offset->mapped_mem + (2*fp->slice_num + 0)*sizeof(uint32_t),
+        AV_WN32(slice_offset->mapped_mem + (2*vp->nb_slices + 0)*sizeof(uint32_t),
                 data - slices_buf->mapped_mem);
-        AV_WN32(slice_offset->mapped_mem + (2*fp->slice_num + 1)*sizeof(uint32_t),
+        AV_WN32(slice_offset->mapped_mem + (2*vp->nb_slices + 1)*sizeof(uint32_t),
                 size);
 
-        fp->slice_num++;
+        vp->nb_slices++;
     } else {
-        int err = ff_vk_decode_add_slice(avctx, vp, data, size, 0,
-                                         &fp->slice_num,
-                                         (const uint32_t **)&fp->slice_offset);
+        int err = ff_vk_decode_add_slice(avctx, vp, data, size, 0);
         if (err < 0)
             return err;
 
-        AV_WN32(slice_offset->mapped_mem + (2*(fp->slice_num - 1) + 0)*sizeof(uint32_t),
-                fp->slice_offset[fp->slice_num - 1]);
-        AV_WN32(slice_offset->mapped_mem + (2*(fp->slice_num - 1) + 1)*sizeof(uint32_t),
+        AV_WN32(slice_offset->mapped_mem + (2*(vp->nb_slices - 1) + 0)*sizeof(uint32_t),
+                vp->slice_offsets[vp->nb_slices - 1]);
+        AV_WN32(slice_offset->mapped_mem + (2*(vp->nb_slices - 1) + 1)*sizeof(uint32_t),
                 size);
     }
 
@@ -1280,7 +1274,7 @@ static void vk_ffv1_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
 
     if (fp->crc_checked) {
         FFVkBuffer *slice_status = (FFVkBuffer *)fp->slice_status_buf->data;
-        for (int i = 0; i < fp->slice_num; i++) {
+        for (int i = 0; i < vp->nb_slices; i++) {
             uint32_t crc_res;
             crc_res = AV_RN32(slice_status->mapped_mem + i*sizeof(uint32_t));
             if (crc_res != 0)
diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index 73aaed7f6a..af37e0800b 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -491,9 +491,7 @@ static int vk_h264_decode_slice(AVCodecContext *avctx,
     H264VulkanDecodePicture *hp = h->cur_pic_ptr->hwaccel_picture_private;
     FFVulkanDecodePicture *vp = &hp->vp;
 
-    int err = ff_vk_decode_add_slice(avctx, vp, data, size, 1,
-                                     &hp->h264_pic_info.sliceCount,
-                                     &hp->h264_pic_info.pSliceOffsets);
+    int err = ff_vk_decode_add_slice(avctx, vp, data, size, 1);
     if (err < 0)
         return err;
 
@@ -541,12 +539,11 @@ static int vk_h264_end_frame(AVCodecContext *avctx)
     }
 #endif
 
+    hp->h264_pic_info.pSliceOffsets = vp->slice_offsets;
+    hp->h264_pic_info.sliceCount = vp->nb_slices;
     if (!hp->h264_pic_info.sliceCount)
         return 0;
 
-    if (!vp->slices_buf)
-        return AVERROR(EINVAL);
-
     if (!dec->session_params &&
         !(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)) {
         int err = vk_h264_create_params(avctx, &dec->session_params);
diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index e4a946ae0d..e95f554756 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -842,9 +842,7 @@ static int vk_hevc_decode_slice(AVCodecContext *avctx,
     HEVCVulkanDecodePicture *hp = h->cur_frame->hwaccel_picture_private;
     FFVulkanDecodePicture *vp = &hp->vp;
 
-    int err = ff_vk_decode_add_slice(avctx, vp, data, size, 1,
-                                     &hp->h265_pic_info.sliceSegmentCount,
-                                     &hp->h265_pic_info.pSliceSegmentOffsets);
+    int err = ff_vk_decode_add_slice(avctx, vp, data, size, 1);
     if (err < 0)
         return err;
 
@@ -898,6 +896,8 @@ static int vk_hevc_end_frame(AVCodecContext *avctx)
     }
 #endif
 
+    hp->h265_pic_info.pSliceSegmentOffsets = vp->slice_offsets;
+    hp->h265_pic_info.sliceSegmentCount = vp->nb_slices;
     if (!hp->h265_pic_info.sliceSegmentCount)
         return 0;
 
-- 
2.49.0
_______________________________________________
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".