[FFmpeg-devel] [PATCH] vulkan_video: remove NIH pooled buffer implementation

Lynne dev at lynne.ee
Thu Jul 18 11:13:29 EEST 2024


The code predates ff_vk_get_pooled_buffer().
---
 libavcodec/vulkan_decode.c | 47 +++++++++++++++-------
 libavcodec/vulkan_decode.h |  2 +
 libavcodec/vulkan_video.c  | 82 --------------------------------------
 libavcodec/vulkan_video.h  |  7 ----
 4 files changed, 34 insertions(+), 104 deletions(-)

diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index d8c75cd0e6..d871d46772 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -260,7 +260,7 @@ int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
     const int nb = *nb_slices;
     uint8_t *slices;
     uint32_t *slice_off;
-    FFVkVideoBuffer *vkbuf;
+    FFVkBuffer *vkbuf;
 
     size_t new_size = vp->slices_size + startcode_len + size +
                       ctx->caps.minBitstreamBufferSizeAlignment;
@@ -274,29 +274,44 @@ int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
     *offsets = dec->slice_off = slice_off;
     slice_off[nb] = vp->slices_size;
 
-    vkbuf = vp->slices_buf ? (FFVkVideoBuffer *)vp->slices_buf->data : NULL;
-    if (!vkbuf || vkbuf->buf.size < new_size) {
+    vkbuf = vp->slices_buf ? (FFVkBuffer *)vp->slices_buf->data : NULL;
+    if (!vkbuf || vkbuf->size < new_size) {
         int err;
         AVBufferRef *new_ref;
-        FFVkVideoBuffer *new_buf;
-        err = ff_vk_video_get_buffer(&ctx->s, &ctx->common, &new_ref,
-                                     VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
-                                     ctx->s.hwfc->create_pnext, new_size);
+        FFVkBuffer *new_buf;
+
+        /* No point in requesting anything smaller. */
+        size_t buf_size = FFMAX(new_size, 1024*1024);
+
+        /* Align buffer to nearest power of two. Makes fragmentation management
+         * easier, and gives us ample headroom. */
+        buf_size--;
+        buf_size |= buf_size >>  1;
+        buf_size |= buf_size >>  2;
+        buf_size |= buf_size >>  4;
+        buf_size |= buf_size >>  8;
+        buf_size |= buf_size >> 16;
+        buf_size++;
+
+        err = ff_vk_get_pooled_buffer(&ctx->s, &ctx->buf_pool, &new_ref,
+                                      VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
+                                      ctx->s.hwfc->create_pnext, buf_size,
+                                      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
         if (err < 0)
             return err;
 
-        new_buf = (FFVkVideoBuffer *)new_ref->data;
+        new_buf = (FFVkBuffer *)new_ref->data;
 
         /* Copy data from the old buffer */
         if (vkbuf) {
-            memcpy(new_buf->mem, vkbuf->mem, vp->slices_size);
+            memcpy(new_buf->mapped_mem, vkbuf->mapped_mem, vp->slices_size);
             av_buffer_unref(&vp->slices_buf);
         }
 
         vp->slices_buf = new_ref;
         vkbuf = new_buf;
     }
-    slices = vkbuf->mem;
+    slices = vkbuf->mapped_mem;
 
     /* Startcode */
     memcpy(slices + vp->slices_size, startcode_prefix, startcode_len);
@@ -347,7 +362,7 @@ int ff_vk_decode_frame(AVCodecContext *avctx,
     int err;
     VkResult ret;
     VkCommandBuffer cmd_buf;
-    FFVkVideoBuffer *sd_buf;
+    FFVkBuffer *sd_buf;
 
     FFVulkanDecodeContext *dec = avctx->internal->hwaccel_priv_data;
     FFVulkanDecodeShared *ctx = dec->shared_ctx;
@@ -400,13 +415,13 @@ int ff_vk_decode_frame(AVCodecContext *avctx,
                    "Result of previous frame decoding: %"PRId64"\n", prev_sub_res);
     }
 
-    sd_buf = (FFVkVideoBuffer *)vp->slices_buf->data;
+    sd_buf = (FFVkBuffer *)vp->slices_buf->data;
 
     /* Flush if needed */
-    if (!(sd_buf->buf.flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
+    if (!(sd_buf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
         VkMappedMemoryRange flush_buf = {
             .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
-            .memory = sd_buf->buf.mem,
+            .memory = sd_buf->mem,
             .offset = 0,
             .size = FFALIGN(vp->slices_size,
                             ctx->s.props.properties.limits.nonCoherentAtomSize),
@@ -420,7 +435,7 @@ int ff_vk_decode_frame(AVCodecContext *avctx,
         }
     }
 
-    vp->decode_info.srcBuffer       = sd_buf->buf.buf;
+    vp->decode_info.srcBuffer       = sd_buf->buf;
     vp->decode_info.srcBufferOffset = 0;
     vp->decode_info.srcBufferRange  = data_size;
 
@@ -621,6 +636,8 @@ static void free_common(FFRefStructOpaque unused, void *obj)
                                              ctx->empty_session_params,
                                              s->hwctx->alloc);
 
+    av_buffer_pool_uninit(&ctx->buf_pool);
+
     ff_vk_video_common_uninit(s, &ctx->common);
 
     if (ctx->yuv_sampler)
diff --git a/libavcodec/vulkan_decode.h b/libavcodec/vulkan_decode.h
index 076af93499..c181277cdc 100644
--- a/libavcodec/vulkan_decode.h
+++ b/libavcodec/vulkan_decode.h
@@ -48,6 +48,8 @@ typedef struct FFVulkanDecodeShared {
     FFVkVideoCommon common;
     FFVkQueueFamilyCtx qf;
 
+    AVBufferPool *buf_pool;
+
     VkVideoCapabilitiesKHR caps;
     VkVideoDecodeCapabilitiesKHR dec_caps;
 
diff --git a/libavcodec/vulkan_video.c b/libavcodec/vulkan_video.c
index 412ae9b61e..f2a15d392e 100644
--- a/libavcodec/vulkan_video.c
+++ b/libavcodec/vulkan_video.c
@@ -177,86 +177,6 @@ int ff_vk_h265_level_to_av(StdVideoH265LevelIdc level)
     }
 }
 
-static void free_data_buf(void *opaque, uint8_t *data)
-{
-    FFVulkanContext *ctx = opaque;
-    FFVkVideoBuffer *buf = (FFVkVideoBuffer *)data;
-    ff_vk_unmap_buffer(ctx, &buf->buf, 0);
-    ff_vk_free_buf(ctx, &buf->buf);
-    av_free(data);
-}
-
-static AVBufferRef *alloc_data_buf(void *opaque, size_t size)
-{
-    AVBufferRef *ref;
-    uint8_t *buf = av_mallocz(size);
-    if (!buf)
-        return NULL;
-
-    ref = av_buffer_create(buf, size, free_data_buf, opaque, 0);
-    if (!ref)
-        av_free(buf);
-    return ref;
-}
-
-int ff_vk_video_get_buffer(FFVulkanContext *ctx, FFVkVideoCommon *s,
-                           AVBufferRef **buf, VkBufferUsageFlags usage,
-                           void *create_pNext, size_t size)
-{
-    int err;
-    AVBufferRef *ref;
-    FFVkVideoBuffer *data;
-
-    if (!s->buf_pool) {
-        s->buf_pool = av_buffer_pool_init2(sizeof(FFVkVideoBuffer), ctx,
-                                           alloc_data_buf, NULL);
-        if (!s->buf_pool)
-            return AVERROR(ENOMEM);
-    }
-
-    *buf = ref = av_buffer_pool_get(s->buf_pool);
-    if (!ref)
-        return AVERROR(ENOMEM);
-
-    data = (FFVkVideoBuffer *)ref->data;
-
-    if (data->buf.size >= size)
-        return 0;
-
-    /* No point in requesting anything smaller. */
-    size = FFMAX(size, 1024*1024);
-
-    /* Align buffer to nearest power of two. Makes fragmentation management
-     * easier, and gives us ample headroom. */
-    size--;
-    size |= size >>  1;
-    size |= size >>  2;
-    size |= size >>  4;
-    size |= size >>  8;
-    size |= size >> 16;
-    size++;
-
-    ff_vk_free_buf(ctx, &data->buf);
-    memset(data, 0, sizeof(FFVkVideoBuffer));
-
-    err = ff_vk_create_buf(ctx, &data->buf, size,
-                           create_pNext, NULL, usage,
-                           VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
-    if (err < 0) {
-        av_buffer_unref(&ref);
-        return err;
-    }
-
-    /* Map the buffer */
-    err = ff_vk_map_buffer(ctx, &data->buf, &data->mem, 0);
-    if (err < 0) {
-        av_buffer_unref(&ref);
-        return err;
-    }
-
-    return 0;
-}
-
 av_cold void ff_vk_video_common_uninit(FFVulkanContext *s,
                                        FFVkVideoCommon *common)
 {
@@ -273,8 +193,6 @@ av_cold void ff_vk_video_common_uninit(FFVulkanContext *s,
             vk->FreeMemory(s->hwctx->act_dev, common->mem[i], s->hwctx->alloc);
 
     av_freep(&common->mem);
-
-    av_buffer_pool_uninit(&common->buf_pool);
 }
 
 av_cold int ff_vk_video_common_init(void *log, FFVulkanContext *s,
diff --git a/libavcodec/vulkan_video.h b/libavcodec/vulkan_video.h
index 01a1de7d9d..1f895c21fa 100644
--- a/libavcodec/vulkan_video.h
+++ b/libavcodec/vulkan_video.h
@@ -32,8 +32,6 @@ typedef struct FFVkVideoSession {
     VkVideoSessionKHR session;
     VkDeviceMemory *mem;
     uint32_t nb_mem;
-
-    AVBufferPool *buf_pool;
 } FFVkVideoCommon;
 
 /**
@@ -63,11 +61,6 @@ VkVideoComponentBitDepthFlagBitsKHR ff_vk_depth_from_av_depth(int depth);
 int ff_vk_h264_level_to_av(StdVideoH264LevelIdc level);
 int ff_vk_h265_level_to_av(StdVideoH265LevelIdc level);
 
-typedef struct FFVkVideoBuffer {
-    FFVkBuffer buf;
-    uint8_t *mem;
-} FFVkVideoBuffer;
-
 /**
  * Get a mapped FFVkPooledBuffer with a specific guaranteed minimum size
  * from a pool.
-- 
2.45.2.753.g447d99e1c3b


More information about the ffmpeg-devel mailing list