FFmpeg
vf_chromaber_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_chromaber_comp_spv_data[];
29 extern const unsigned int ff_chromaber_comp_spv_len;
30 
33 
38  VkSampler sampler;
39 
40  /* Push constants / options */
41  struct {
42  float dist[2];
43  uint32_t single_plane;
44  } opts;
46 
48 {
49  int err;
51  FFVulkanContext *vkctx = &s->vkctx;
52  const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
53  FFVulkanShader *shd = &s->shd;
54 
55  /* Normalize options */
56  s->opts.dist[0] = (s->opts.dist[0] / 100.0f) + 1.0f;
57  s->opts.dist[1] = (s->opts.dist[1] / 100.0f) + 1.0f;
58 
59  s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
60  if (!s->qf) {
61  av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
62  err = AVERROR(ENOTSUP);
63  goto fail;
64  }
65 
66  RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, s->qf->num*4, 0, 0, 0, NULL));
67  RET(ff_vk_init_sampler(vkctx, &s->sampler, 0, VK_FILTER_LINEAR));
68 
69  ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
70  (uint32_t []) { 32, 32, 1 }, 0);
71 
72  ff_vk_shader_add_push_const(&s->shd, 0, sizeof(s->opts),
73  VK_SHADER_STAGE_COMPUTE_BIT);
74 
76  { /* input_img */
77  .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
78  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
79  .elems = planes,
80  },
81  { /* output_img */
82  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
83  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
84  .elems = planes,
85  },
86  };
87  ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 2, 0, 0);
88 
89  RET(ff_vk_shader_link(vkctx, shd,
91  ff_chromaber_comp_spv_len, "main"));
92 
93  RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
94 
95  s->opts.single_plane = planes == 1;
96  s->initialized = 1;
97 
98 fail:
99  return err;
100 }
101 
103 {
104  int err;
105  AVFilterContext *ctx = link->dst;
107  AVFilterLink *outlink = ctx->outputs[0];
108 
109  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
110  if (!out) {
111  err = AVERROR(ENOMEM);
112  goto fail;
113  }
114 
115  if (!s->initialized)
116  RET(init_filter(ctx, in));
117 
118  RET(ff_vk_filter_process_simple(&s->vkctx, &s->e, &s->shd, out, in,
119  s->sampler, 1, &s->opts, sizeof(s->opts)));
120 
121  err = av_frame_copy_props(out, in);
122  if (err < 0)
123  goto fail;
124 
125  av_frame_free(&in);
126 
127  return ff_filter_frame(outlink, out);
128 
129 fail:
130  av_frame_free(&in);
131  av_frame_free(&out);
132  return err;
133 }
134 
136 {
138  FFVulkanContext *vkctx = &s->vkctx;
139  FFVulkanFunctions *vk = &vkctx->vkfn;
140 
141  ff_vk_exec_pool_free(vkctx, &s->e);
142  ff_vk_shader_free(vkctx, &s->shd);
143 
144  if (s->sampler)
145  vk->DestroySampler(vkctx->hwctx->act_dev, s->sampler,
146  vkctx->hwctx->alloc);
147 
148  ff_vk_uninit(&s->vkctx);
149 
150  s->initialized = 0;
151 }
152 
153 #define OFFSET(x) offsetof(ChromaticAberrationVulkanContext, x)
154 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
156  { "dist_x", "Set horizontal distortion amount", OFFSET(opts.dist[0]), AV_OPT_TYPE_FLOAT, {.dbl = 0.0f}, -10.0f, 10.0f, .flags = FLAGS },
157  { "dist_y", "Set vertical distortion amount", OFFSET(opts.dist[1]), AV_OPT_TYPE_FLOAT, {.dbl = 0.0f}, -10.0f, 10.0f, .flags = FLAGS },
158  { NULL },
159 };
160 
161 AVFILTER_DEFINE_CLASS(chromaber_vulkan);
162 
164  {
165  .name = "default",
166  .type = AVMEDIA_TYPE_VIDEO,
167  .filter_frame = &chromaber_vulkan_filter_frame,
168  .config_props = &ff_vk_filter_config_input,
169  },
170 };
171 
173  {
174  .name = "default",
175  .type = AVMEDIA_TYPE_VIDEO,
176  .config_props = &ff_vk_filter_config_output,
177  },
178 };
179 
181  .p.name = "chromaber_vulkan",
182  .p.description = NULL_IF_CONFIG_SMALL("Offset chroma of input video (chromatic aberration)"),
183  .p.priv_class = &chromaber_vulkan_class,
184  .p.flags = AVFILTER_FLAG_HWDEVICE,
185  .priv_size = sizeof(ChromaticAberrationVulkanContext),
191  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
192 };
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:89
ff_chromaber_comp_spv_data
const unsigned char ff_chromaber_comp_spv_data[]
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
ff_vk_shader_free
void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd)
Free a shader.
Definition: vulkan.c:2820
out
static FILE * out
Definition: movenc.c:55
ChromaticAberrationVulkanContext::dist
float dist[2]
Definition: vf_chromaber_vulkan.c:42
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_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:357
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
ChromaticAberrationVulkanContext::shd
FFVulkanShader shd
Definition: vf_chromaber_vulkan.c:37
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:434
chromaber_vulkan_uninit
static void chromaber_vulkan_uninit(AVFilterContext *avctx)
Definition: vf_chromaber_vulkan.c:135
ff_vk_filter_init
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
Definition: vulkan_filter.c:233
OFFSET
#define OFFSET(x)
Definition: vf_chromaber_vulkan.c:153
AVOption
AVOption.
Definition: opt.h:429
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:254
filters.h
ff_vk_uninit
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition: vulkan.c:2846
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
video.h
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
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, uint32_t wgc_z, 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
ChromaticAberrationVulkanContext::sampler
VkSampler sampler
Definition: vf_chromaber_vulkan.c:38
AVVulkanDeviceContext::alloc
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
Definition: hwcontext_vulkan.h:63
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:289
fail
#define fail()
Definition: checkasm.h:224
vulkan_filter.h
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:2613
ChromaticAberrationVulkanContext::e
FFVkExecPool e
Definition: vf_chromaber_vulkan.c:35
ChromaticAberrationVulkanContext
Definition: vf_chromaber_vulkan.c:31
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:40
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:111
FFFilter
Definition: filters.h:267
FLAGS
#define FLAGS
Definition: vf_chromaber_vulkan.c:154
s
#define s(width, name)
Definition: cbs_vp9.c:198
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:265
chromaber_vulkan_options
static const AVOption chromaber_vulkan_options[]
Definition: vf_chromaber_vulkan.c:155
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
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
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
ChromaticAberrationVulkanContext::opts
struct ChromaticAberrationVulkanContext::@402 opts
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(chromaber_vulkan)
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:2386
FFVulkanContext
Definition: vulkan.h:312
chromaber_vulkan_outputs
static const AVFilterPad chromaber_vulkan_outputs[]
Definition: vf_chromaber_vulkan.c:172
init_filter
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
Definition: vf_chromaber_vulkan.c:47
f
f
Definition: af_crystalizer.c:122
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
chromaber_vulkan_filter_frame
static int chromaber_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_chromaber_vulkan.c:102
planes
static const struct @585 planes[]
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
ChromaticAberrationVulkanContext::initialized
int initialized
Definition: vf_chromaber_vulkan.c:34
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
ChromaticAberrationVulkanContext::qf
AVVulkanDeviceQueueFamily * qf
Definition: vf_chromaber_vulkan.c:36
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:46
ChromaticAberrationVulkanContext::vkctx
FFVulkanContext vkctx
Definition: vf_chromaber_vulkan.c:32
FFVulkanContext::vkfn
FFVulkanFunctions vkfn
Definition: vulkan.h:316
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:1499
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:264
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:2513
random_seed.h
ff_chromaber_comp_spv_len
const unsigned int ff_chromaber_comp_spv_len
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
desc
const char * desc
Definition: libsvtav1.c:82
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
FFVulkanContext::hwctx
AVVulkanDeviceContext * hwctx
Definition: vulkan.h:349
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:84
ff_vk_init_sampler
int ff_vk_init_sampler(FFVulkanContext *s, VkSampler *sampler, int unnorm_coords, VkFilter filt)
Create a sampler.
Definition: vulkan.c:1510
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVVulkanDeviceQueueFamily
Definition: hwcontext_vulkan.h:33
chromaber_vulkan_inputs
static const AVFilterPad chromaber_vulkan_inputs[]
Definition: vf_chromaber_vulkan.c:163
FFVulkanFunctions
Definition: vulkan_functions.h:275
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:2103
ff_vf_chromaber_vulkan
const FFFilter ff_vf_chromaber_vulkan
Definition: vf_chromaber_vulkan.c:180
ChromaticAberrationVulkanContext::single_plane
uint32_t single_plane
Definition: vf_chromaber_vulkan.c:43