FFmpeg
vf_avgblur_vulkan.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Lynne
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/random_seed.h"
22 #include "libavutil/opt.h"
23 #include "vulkan_filter.h"
24 
25 #include "filters.h"
26 #include "video.h"
27 
28 extern const unsigned char ff_avgblur_comp_spv_data[];
29 extern const unsigned int ff_avgblur_comp_spv_len;
30 
31 typedef struct AvgBlurVulkanContext {
33 
38 
39  /* Push constants / options */
40  struct {
41  float filter_norm[4];
43  uint32_t planes;
44  } opts;
45 
46  int size_x;
47  int size_y;
49 
51 {
52  int err;
53  AvgBlurVulkanContext *s = ctx->priv;
54  FFVulkanContext *vkctx = &s->vkctx;
55  const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
56 
57  s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
58  if (!s->qf) {
59  av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
60  err = AVERROR(ENOTSUP);
61  goto fail;
62  }
63 
64  RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, s->qf->num*4, 0, 0, 0, NULL));
65 
66  ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT,
67  NULL, (uint32_t []) { 32, 1, planes }, 0);
68 
69  ff_vk_shader_add_push_const(&s->shd, 0, sizeof(s->opts),
70  VK_SHADER_STAGE_COMPUTE_BIT);
71 
72  const FFVulkanDescriptorSetBinding desc_set[] = {
73  { /* input_img */
74  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
75  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
76  .elems = planes,
77  },
78  { /* output_img */
79  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
80  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
81  .elems = planes,
82  },
83  };
84  ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc_set, 2, 0, 0);
85 
86  RET(ff_vk_shader_link(vkctx, &s->shd,
88  ff_avgblur_comp_spv_len, "main"));
89 
90  RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
91 
92  s->initialized = 1;
93  s->opts.filter_len[0] = s->size_x - 1;
94  s->opts.filter_len[1] = s->size_y - 1;
95 
96  s->opts.filter_norm[0] = s->opts.filter_len[0]*2 + 1;
97  s->opts.filter_norm[0] = 1.0/(s->opts.filter_norm[0]*s->opts.filter_norm[0]);
98  s->opts.filter_norm[1] = s->opts.filter_norm[0];
99  s->opts.filter_norm[2] = s->opts.filter_norm[0];
100  s->opts.filter_norm[3] = s->opts.filter_norm[0];
101 
102 fail:
103  return err;
104 }
105 
107 {
108  int err;
109  AVFrame *out = NULL;
110  AVFilterContext *ctx = link->dst;
111  AvgBlurVulkanContext *s = ctx->priv;
112  AVFilterLink *outlink = ctx->outputs[0];
113 
114  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
115  if (!out) {
116  err = AVERROR(ENOMEM);
117  goto fail;
118  }
119 
120  if (!s->initialized)
121  RET(init_filter(ctx, in));
122 
123  RET(ff_vk_filter_process_simple(&s->vkctx, &s->e, &s->shd,
124  out, in, VK_NULL_HANDLE,
125  &s->opts, sizeof(s->opts)));
126 
127  err = av_frame_copy_props(out, in);
128  if (err < 0)
129  goto fail;
130 
131  av_frame_free(&in);
132 
133  return ff_filter_frame(outlink, out);
134 
135 fail:
136  av_frame_free(&in);
137  av_frame_free(&out);
138  return err;
139 }
140 
142 {
143  AvgBlurVulkanContext *s = avctx->priv;
144  FFVulkanContext *vkctx = &s->vkctx;
145 
146  ff_vk_exec_pool_free(vkctx, &s->e);
147  ff_vk_shader_free(vkctx, &s->shd);
148 
149  ff_vk_uninit(&s->vkctx);
150 
151  s->initialized = 0;
152 }
153 
154 #define OFFSET(x) offsetof(AvgBlurVulkanContext, x)
155 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
157  { "sizeX", "Set horizontal radius", OFFSET(size_x), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 32, .flags = FLAGS },
158  { "sizeY", "Set vertical radius", OFFSET(size_y), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 32, .flags = FLAGS },
159  { "planes", "Set planes to filter (bitmask)", OFFSET(opts.planes), AV_OPT_TYPE_INT, {.i64 = 0xF}, 0, 0xF, .flags = FLAGS },
160  { NULL },
161 };
162 
163 AVFILTER_DEFINE_CLASS(avgblur_vulkan);
164 
166  {
167  .name = "default",
168  .type = AVMEDIA_TYPE_VIDEO,
169  .filter_frame = &avgblur_vulkan_filter_frame,
170  .config_props = &ff_vk_filter_config_input,
171  },
172 };
173 
175  {
176  .name = "default",
177  .type = AVMEDIA_TYPE_VIDEO,
178  .config_props = &ff_vk_filter_config_output,
179  },
180 };
181 
183  .p.name = "avgblur_vulkan",
184  .p.description = NULL_IF_CONFIG_SMALL("Apply avgblur mask to input video"),
185  .p.priv_class = &avgblur_vulkan_class,
186  .p.flags = AVFILTER_FLAG_HWDEVICE,
187  .priv_size = sizeof(AvgBlurVulkanContext),
193  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
194 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:118
AvgBlurVulkanContext
Definition: vf_avgblur_vulkan.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FLAGS
#define FLAGS
Definition: vf_avgblur_vulkan.c:155
avgblur_vulkan_outputs
static const AVFilterPad avgblur_vulkan_outputs[]
Definition: vf_avgblur_vulkan.c:174
ff_vk_shader_free
void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd)
Free a shader.
Definition: vulkan.c:2789
out
static FILE * out
Definition: movenc.c:55
avgblur_vulkan_inputs
static const AVFilterPad avgblur_vulkan_inputs[]
Definition: vf_avgblur_vulkan.c:165
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1067
RET
#define RET(x)
Definition: vulkan.h:68
ff_vk_filter_process_simple
int ff_vk_filter_process_simple(FFVulkanContext *vkctx, FFVkExecPool *e, FFVulkanShader *shd, AVFrame *out_f, AVFrame *in_f, VkSampler sampler, void *push_src, size_t push_size)
Submit a compute shader with a zero/one input and single out for execution.
Definition: vulkan_filter.c:242
ff_vk_exec_pool_init
int ff_vk_exec_pool_init(FFVulkanContext *s, AVVulkanDeviceQueueFamily *qf, FFVkExecPool *pool, int nb_contexts, int nb_queries, VkQueryType query_type, int query_64bit, const void *query_create_pnext)
Allocates/frees an execution pool.
Definition: vulkan.c:355
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:264
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AvgBlurVulkanContext::shd
FFVulkanShader shd
Definition: vf_avgblur_vulkan.c:37
ff_vk_filter_init
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
Definition: vulkan_filter.c:233
AVOption
AVOption.
Definition: opt.h:429
ff_vk_uninit
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition: vulkan.c:2815
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
avgblur_vulkan_uninit
static void avgblur_vulkan_uninit(AVFilterContext *avctx)
Definition: vf_avgblur_vulkan.c:141
AvgBlurVulkanContext::initialized
int initialized
Definition: vf_avgblur_vulkan.c:34
video.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(avgblur_vulkan)
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:379
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3496
AvgBlurVulkanContext::filter_norm
float filter_norm[4]
Definition: vf_avgblur_vulkan.c:41
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:289
fail
#define fail()
Definition: checkasm.h:218
vulkan_filter.h
avgblur_vulkan_options
static const AVOption avgblur_vulkan_options[]
Definition: vf_avgblur_vulkan.c:156
ff_vk_shader_register_exec
int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, FFVulkanShader *shd)
Register a shader with an exec pool.
Definition: vulkan.c:2582
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:40
FFVulkanDescriptorSetBinding::type
VkDescriptorType type
Definition: vulkan.h:114
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:106
FFFilter
Definition: filters.h:267
ff_avgblur_comp_spv_data
const unsigned char ff_avgblur_comp_spv_data[]
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
ff_vk_exec_pool_free
void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
Definition: vulkan.c:299
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:265
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
AvgBlurVulkanContext::e
FFVkExecPool e
Definition: vf_avgblur_vulkan.c:35
opts
static AVDictionary * opts
Definition: movenc.c:51
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:599
OFFSET
#define OFFSET(x)
Definition: vf_avgblur_vulkan.c:154
ff_vk_filter_config_output
int ff_vk_filter_config_output(AVFilterLink *outlink)
Definition: vulkan_filter.c:209
ff_vk_shader_link
int ff_vk_shader_link(FFVulkanContext *s, FFVulkanShader *shd, const char *spirv, size_t spirv_len, const char *entrypoint)
Link a shader into an executable.
Definition: vulkan.c:2355
AvgBlurVulkanContext::size_y
int size_y
Definition: vf_avgblur_vulkan.c:47
FFVulkanContext
Definition: vulkan.h:312
AvgBlurVulkanContext::filter_len
int32_t filter_len[2]
Definition: vf_avgblur_vulkan.c:42
FF_FILTER_FLAG_HWFRAME_AWARE
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: filters.h:208
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
FFVulkanDescriptorSetBinding
Definition: vulkan.h:112
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:188
FFVulkanShader
Definition: vulkan.h:225
AvgBlurVulkanContext::vkctx
FFVulkanContext vkctx
Definition: vf_avgblur_vulkan.c:32
init_filter
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
Definition: vf_avgblur_vulkan.c:50
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
AvgBlurVulkanContext::planes
uint32_t planes
Definition: vf_avgblur_vulkan.c:43
AvgBlurVulkanContext::size_x
int size_x
Definition: vf_avgblur_vulkan.c:46
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:46
AvgBlurVulkanContext::opts
struct AvgBlurVulkanContext::@383 opts
FFVkExecPool
Definition: vulkan.h:290
ff_vk_shader_add_push_const
int ff_vk_shader_add_push_const(FFVulkanShader *shd, int offset, int size, VkShaderStageFlagBits stage)
Add/update push constants for execution.
Definition: vulkan.c:1479
ff_vk_qf_find
AVVulkanDeviceQueueFamily * ff_vk_qf_find(FFVulkanContext *s, VkQueueFlagBits dev_family, VkVideoCodecOperationFlagBitsKHR vid_ops)
Chooses an appropriate QF.
Definition: vulkan.c:286
ff_vk_shader_add_descriptor_set
int ff_vk_shader_add_descriptor_set(FFVulkanContext *s, FFVulkanShader *shd, const FFVulkanDescriptorSetBinding *desc, int nb, int singular, int print_to_shader_only)
Add descriptor to a shader.
Definition: vulkan.c:2482
random_seed.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
planes
static const struct @554 planes[]
ff_avgblur_comp_spv_len
const unsigned int ff_avgblur_comp_spv_len
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
avgblur_vulkan_filter_frame
static int avgblur_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_avgblur_vulkan.c:106
ff_vk_filter_config_input
int ff_vk_filter_config_input(AVFilterLink *inlink)
Definition: vulkan_filter.c:176
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:271
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVVulkanDeviceQueueFamily
Definition: hwcontext_vulkan.h:33
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:254
ff_vk_shader_load
int ff_vk_shader_load(FFVulkanShader *shd, VkPipelineStageFlags stage, VkSpecializationInfo *spec, uint32_t wg_size[3], uint32_t required_subgroup_size)
Initialize a shader object.
Definition: vulkan.c:2072
AvgBlurVulkanContext::qf
AVVulkanDeviceQueueFamily * qf
Definition: vf_avgblur_vulkan.c:36
ff_vf_avgblur_vulkan
const FFFilter ff_vf_avgblur_vulkan
Definition: vf_avgblur_vulkan.c:182