Go to the documentation of this file.
26 #define GBLUR_MAX_KERNEL_SIZE 127
53 C(0,
void gblur(
const ivec2
pos,
const int index) )
55 C(1, vec4 sum = texture(input_image[
index],
pos) * kernel[0]; )
57 C(1,
for(
int i = 1;
i < kernel.length();
i++) { )
58 C(2, sum += texture(input_image[
index],
pos + vec2(
i, 0.0)) * kernel[
i]; )
59 C(2, sum += texture(input_image[
index],
pos - vec2(
i, 0.0)) * kernel[
i]; )
62 C(1, imageStore(output_image[
index],
pos, sum); )
67 C(0,
void gblur(
const ivec2
pos,
const int index) )
69 C(1, vec4 sum = texture(input_image[
index],
pos) * kernel[0]; )
71 C(1,
for(
int i = 1;
i < kernel.length();
i++) { )
72 C(2, sum += texture(input_image[
index],
pos + vec2(0.0,
i)) * kernel[
i]; )
73 C(2, sum += texture(input_image[
index],
pos - vec2(0.0,
i)) * kernel[
i]; )
76 C(1, imageStore(output_image[
index],
pos, sum); )
80 static inline float gaussian(
float sigma,
float x)
82 return 1.0 / (sqrt(2.0 *
M_PI) * sigma) *
83 exp(-(x * x) / (2.0 * sigma * sigma));
88 return (
b -
a) * (1.0 / 6.0) * ((
gaussian(sigma,
a) +
98 for (x = 0; x < kernel_size; x++) {
103 sum += kernel[x] * 2.0;
107 for (x = 0; x < kernel_size; x++) {
114 if (!(
s->size & 1)) {
119 s->sigmaV =
s->sigma;
121 s->kernel_size = (
s->size >> 1) + 1;
129 uint8_t *kernel_mapped;
136 .
name =
"input_image",
137 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
140 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
143 .name =
"output_image",
144 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
146 .mem_quali =
"writeonly",
149 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
155 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
156 .mem_quali =
"readonly",
157 .mem_layout =
"std430",
158 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
164 if (!image_descs[0].sampler)
169 kernel_def =
av_asprintf(
"float kernel[%i];",
s->kernel_size);
178 image_descs[0].
updater =
s->input_images;
179 image_descs[1].
updater =
s->tmp_images;
180 buf_desc.
updater = &
s->params_desc_hor;
202 GLSLC(1,
const ivec2
pos = ivec2(gl_GlobalInvocationID.xy); );
207 if (
s->planes & (1 <<
i)) {
210 GLSLF(2, vec4 res = texture(input_image[%
i],
pos); ,
i);
211 GLSLF(2, imageStore(output_image[%
i],
pos, res); ,
i);
223 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
230 s->params_desc_hor.buffer =
s->params_buf_hor.buf;
231 s->params_desc_hor.range = VK_WHOLE_SIZE;
237 image_descs[0].
updater =
s->tmp_images;
238 image_descs[1].
updater =
s->output_images;
239 buf_desc.
updater = &
s->params_desc_ver;
261 GLSLC(1,
const ivec2
pos = ivec2(gl_GlobalInvocationID.xy); );
266 if (
s->planes & (1 <<
i)) {
269 GLSLF(2, vec4 res = texture(input_image[%
i],
pos); ,
i);
270 GLSLF(2, imageStore(output_image[%
i],
pos, res); ,
i);
282 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
289 s->params_desc_ver.buffer =
s->params_buf_ver.buf;
290 s->params_desc_ver.range = VK_WHOLE_SIZE;
320 VkCommandBuffer cmd_buf;
322 const VkFormat *input_formats =
NULL;
323 const VkFormat *output_formats =
NULL;
353 s->input_images[
i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
354 s->tmp_images[
i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
355 s->output_images[
i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
362 VkImageMemoryBarrier barriers[] = {
364 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
366 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
368 .newLayout =
s->input_images[
i].imageLayout,
369 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
370 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
372 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
373 .subresourceRange.levelCount = 1,
374 .subresourceRange.layerCount = 1,
377 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
379 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT,
380 .oldLayout =
tmp->layout[
i],
381 .newLayout =
s->tmp_images[
i].imageLayout,
382 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
383 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
384 .image =
tmp->img[
i],
385 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
386 .subresourceRange.levelCount = 1,
387 .subresourceRange.layerCount = 1,
390 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
392 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
393 .oldLayout =
out->layout[
i],
394 .newLayout =
s->output_images[
i].imageLayout,
395 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
396 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
397 .image =
out->img[
i],
398 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
399 .subresourceRange.levelCount = 1,
400 .subresourceRange.layerCount = 1,
404 vk->CmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
405 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
408 in->
layout[
i] = barriers[0].newLayout;
409 in->
access[
i] = barriers[0].dstAccessMask;
411 tmp->layout[
i] = barriers[1].newLayout;
412 tmp->access[
i] = barriers[1].dstAccessMask;
414 out->layout[
i] = barriers[2].newLayout;
415 out->access[
i] = barriers[2].dstAccessMask;
457 if (!
s->initialized) {
482 #define OFFSET(x) offsetof(GBlurVulkanContext, x)
483 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
512 .
name =
"gblur_vulkan",
520 .priv_class = &gblur_vulkan_class,
VkDescriptorImageInfo input_images[3]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_LOG_WARNING
Something somehow does not look correct.
static const char gblur_horizontal[]
AVFILTER_DEFINE_CLASS(gblur_vulkan)
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
const VkComponentMapping ff_comp_identity_map
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Create a VkBuffer with the specified parameters.
static float gaussian_simpson_integration(float sigma, float a, float b)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
void ff_vk_bind_pipeline_exec(FFVulkanContext *s, FFVkExecContext *e, FFVulkanPipeline *pl)
Add a command to bind the completed pipeline and its descriptor sets.
char * av_asprintf(const char *fmt,...)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
VkShaderStageFlags stages
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
int ff_vk_compile_shader(FFVulkanContext *s, FFVkSPIRVShader *shd, const char *entrypoint)
Compiles the shader, entrypoint must be set to "main".
FFVulkanPipeline * pl_hor
FFVulkanPipeline * pl_ver
void ff_vk_uninit(FFVulkanContext *s)
Frees the main Vulkan context.
const char * name
Filter name.
A link between two filters.
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
static const AVFilterPad gblur_vulkan_inputs[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void init_gaussian_kernel(float *kernel, float sigma, float kernel_size)
int ff_vk_add_exec_dep(FFVulkanContext *s, FFVkExecContext *e, AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
Adds a frame as a queue dependency.
VkCommandBuffer ff_vk_get_exec_buf(FFVkExecContext *e)
Gets the command buffer to use for this submission from the exe context.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
VkDescriptorImageInfo output_images[3]
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
void * priv
private data for use by the filter
FFVkBuffer params_buf_ver
static const AVFilterPad gblur_vulkan_outputs[]
A filter pad used for either input or output.
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
void ff_vk_qf_init(FFVulkanContext *s, FFVkQueueFamilyCtx *qf, VkQueueFlagBits dev_family, int nb_queues)
Initialize a queue family with a specific number of queues.
#define FF_ARRAY_ELEMS(a)
int ff_vk_create_imageview(FFVulkanContext *s, FFVkExecContext *e, VkImageView *v, VkImage img, VkFormat fmt, const VkComponentMapping map)
Create an imageview.
VkDescriptorBufferInfo params_desc_hor
FFVkSPIRVShader * ff_vk_init_shader(FFVulkanPipeline *pl, const char *name, VkShaderStageFlags stage)
Inits a shader for a specific pipeline.
static const AVOption gblur_vulkan_options[]
VkDescriptorImageInfo tmp_images[3]
#define FILTER_INPUTS(array)
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
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
void ff_vk_update_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, int set_id)
Updates a descriptor set via the updaters defined.
static const struct @321 planes[]
int ff_vk_start_exec_recording(FFVulkanContext *s, FFVkExecContext *e)
Begin recording to the command buffer.
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the format of each image up to the number of planes for a given sw_format.
int main(int argc, char *argv[])
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
int ff_vk_create_exec_ctx(FFVulkanContext *s, FFVkExecContext **ctx, FFVkQueueFamilyCtx *qf)
Init an execution context for command recording and queue submission.
int ff_vk_filter_config_output(AVFilterLink *outlink)
void ff_vk_qf_rotate(FFVkQueueFamilyCtx *qf)
Rotate through the queues in a queue family.
#define GBLUR_MAX_KERNEL_SIZE
void ff_vk_discard_exec_deps(FFVkExecContext *e)
Discards all queue dependencies.
int ff_vk_unmap_buffers(FFVulkanContext *s, FFVkBuffer *buf, int nb_buffers, int flush)
Unmaps the buffer from userspace.
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
VkDescriptorBufferInfo params_desc_ver
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_vk_init_pipeline_layout(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes the pipeline layout after all shaders and descriptor sets have been finished.
static const char gblur_vertical[]
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static av_cold void gblur_vulkan_uninit(AVFilterContext *avctx)
int ff_vk_submit_exec_queue(FFVulkanContext *s, FFVkExecContext *e)
Submits a command buffer to the queue for execution.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
int ff_vk_init_compute_pipeline(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes a compute pipeline.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
const char * ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)
Gets the glsl format string for a pixel format.
const char * name
Pad name.
FFVulkanPipeline * ff_vk_create_pipeline(FFVulkanContext *s, FFVkQueueFamilyCtx *qf)
Inits a pipeline.
void ff_vk_free_buf(FFVulkanContext *s, FFVkBuffer *buf)
Frees a buffer.
static av_cold void init_gaussian_params(GBlurVulkanContext *s)
static int gblur_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
int ff_vk_add_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, FFVkSPIRVShader *shd, FFVulkanDescriptorSetBinding *desc, int num, int only_print_to_shader)
Adds a descriptor set to the shader and registers them in the pipeline.
int h
agreed upon image height
const AVFilter ff_vf_gblur_vulkan
FFVkSampler * ff_vk_init_sampler(FFVulkanContext *s, int unnorm_coords, VkFilter filt)
Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
int ff_vk_filter_config_input(AVFilterLink *inlink)
VkImageLayout layout[AV_NUM_DATA_POINTERS]
int ff_vk_map_buffers(FFVulkanContext *s, FFVkBuffer *buf, uint8_t *mem[], int nb_buffers, int invalidate)
Maps the buffer to userspace.
static int process_frames(AVFilterContext *avctx, AVFrame *outframe, AVFrame *inframe)
#define FILTER_OUTPUTS(array)
void ff_vk_set_compute_shader_sizes(FFVkSPIRVShader *shd, int local_size[3])
Writes the workgroup size for a shader.
static av_cold int uninit(AVCodecContext *avctx)
FFVkBuffer params_buf_hor
static float gaussian(float sigma, float x)