From: Lynne via ffmpeg-devel <ffmpeg-devel@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Lynne <dev@lynne.ee> Subject: [FFmpeg-devel] [PATCH] vulkan_video: remove NIH pooled buffer implementation Date: Thu, 18 Jul 2024 10:13:29 +0200 Message-ID: <20240718081337.1374330-1-dev@lynne.ee> (raw) 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 _______________________________________________ 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".
reply other threads:[~2024-07-18 8:13 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240718081337.1374330-1-dev@lynne.ee \ --to=ffmpeg-devel@ffmpeg.org \ --cc=dev@lynne.ee \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git