]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_avgblur_vulkan.c
lavfi: add an avgblur_vulkan filter
[ffmpeg] / libavfilter / vf_avgblur_vulkan.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/opt.h"
20 #include "vulkan.h"
21 #include "internal.h"
22
23 #define CGS 32
24
25 typedef struct AvgBlurVulkanContext {
26     VulkanFilterContext vkctx;
27
28     int initialized;
29     FFVkExecContext *exec;
30     VulkanPipeline *pl_hor;
31     VulkanPipeline *pl_ver;
32
33     /* Shader updators, must be in the main filter struct */
34     VkDescriptorImageInfo input_images[3];
35     VkDescriptorImageInfo tmp_images[3];
36     VkDescriptorImageInfo output_images[3];
37
38     int size_x;
39     int size_y;
40     int planes;
41 } AvgBlurVulkanContext;
42
43 static const char blur_kernel[] = {
44     C(0, shared vec4 cache[DIR(gl_WorkGroupSize) + FILTER_RADIUS*2 + 1];           )
45     C(0,                                                                           )
46     C(0, void distort(const ivec2 pos, const int idx)                              )
47     C(0, {                                                                         )
48     C(1,     const uint cp = DIR(gl_LocalInvocationID) + FILTER_RADIUS;            )
49     C(0,                                                                           )
50     C(1,     cache[cp] = texture(input_img[idx], pos);                             )
51     C(0,                                                                           )
52     C(1,     const ivec2 loc_l = pos - INC(FILTER_RADIUS);                         )
53     C(1,     cache[cp - FILTER_RADIUS] = texture(input_img[idx], loc_l);           )
54     C(0,                                                                           )
55     C(1,     const ivec2 loc_h = pos + INC(DIR(gl_WorkGroupSize));                 )
56     C(1,     cache[cp + DIR(gl_WorkGroupSize)] = texture(input_img[idx], loc_h);   )
57     C(0,                                                                           )
58     C(1,     barrier();                                                            )
59     C(0,                                                                           )
60     C(1,     vec4 sum = vec4(0);                                                   )
61     C(1,     for (int p = -FILTER_RADIUS; p <= FILTER_RADIUS; p++)                 )
62     C(2,         sum += cache[cp + p];                                             )
63     C(0,                                                                           )
64     C(1,     sum /= vec4(FILTER_RADIUS*2 + 1);                                     )
65     C(1,     imageStore(output_img[idx], pos, sum);                                )
66     C(0, }                                                                         )
67 };
68
69 static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
70 {
71     int err;
72     SPIRVShader *shd;
73     AvgBlurVulkanContext *s = ctx->priv;
74     const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
75     VkSampler *sampler = ff_vk_init_sampler(ctx, 1, VK_FILTER_LINEAR);
76
77     VulkanDescriptorSetBinding desc_i[2] = {
78         {
79             .name       = "input_img",
80             .type       = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
81             .dimensions = 2,
82             .elems      = planes,
83             .stages     = VK_SHADER_STAGE_COMPUTE_BIT,
84             .samplers   = DUP_SAMPLER_ARRAY4(*sampler),
85         },
86         {
87             .name       = "output_img",
88             .type       = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
89             .mem_layout = ff_vk_shader_rep_fmt(s->vkctx.output_format),
90             .mem_quali  = "writeonly",
91             .dimensions = 2,
92             .elems      = planes,
93             .stages     = VK_SHADER_STAGE_COMPUTE_BIT,
94         },
95     };
96
97     if (!sampler)
98         return AVERROR_EXTERNAL;
99
100     { /* Create shader for the horizontal pass */
101         desc_i[0].updater = s->input_images;
102         desc_i[1].updater = s->tmp_images;
103
104         s->pl_hor = ff_vk_create_pipeline(ctx);
105         if (!s->pl_hor)
106             return AVERROR(ENOMEM);
107
108         shd = ff_vk_init_shader(ctx, s->pl_hor, "avgblur_compute_hor",
109                                 VK_SHADER_STAGE_COMPUTE_BIT);
110
111         ff_vk_set_compute_shader_sizes(ctx, shd, (int [3]){ CGS, 1, 1 });
112
113         RET(ff_vk_add_descriptor_set(ctx, s->pl_hor, shd, desc_i, 2, 0));
114
115         GLSLF(0, #define FILTER_RADIUS (%i)                     ,s->size_x - 1);
116         GLSLC(0, #define INC(x) (ivec2(x, 0))                                 );
117         GLSLC(0, #define DIR(var) (var.x)                                     );
118         GLSLD(   blur_kernel                                                  );
119         GLSLC(0, void main()                                                  );
120         GLSLC(0, {                                                            );
121         GLSLC(1,     ivec2 size;                                              );
122         GLSLC(1,     const ivec2 pos = ivec2(gl_GlobalInvocationID.xy);       );
123         for (int i = 0; i < planes; i++) {
124             GLSLC(0,                                                          );
125             GLSLF(1,  size = imageSize(output_img[%i]);                     ,i);
126             GLSLC(1,  if (IS_WITHIN(pos, size)) {                             );
127             if (s->planes & (1 << i)) {
128                 GLSLF(2, distort(pos, %i);                                  ,i);
129             } else {
130                 GLSLF(2, vec4 res = texture(input_img[%i], pos);            ,i);
131                 GLSLF(2, imageStore(output_img[%i], pos, res);              ,i);
132             }
133             GLSLC(1, }                                                        );
134         }
135         GLSLC(0, }                                                            );
136
137         RET(ff_vk_compile_shader(ctx, shd, "main"));
138
139         RET(ff_vk_init_pipeline_layout(ctx, s->pl_hor));
140         RET(ff_vk_init_compute_pipeline(ctx, s->pl_hor));
141     }
142
143     { /* Create shader for the vertical pass */
144         desc_i[0].updater = s->tmp_images;
145         desc_i[1].updater = s->output_images;
146
147         s->pl_ver = ff_vk_create_pipeline(ctx);
148         if (!s->pl_ver)
149             return AVERROR(ENOMEM);
150
151         shd = ff_vk_init_shader(ctx, s->pl_ver, "avgblur_compute_ver",
152                                 VK_SHADER_STAGE_COMPUTE_BIT);
153
154         ff_vk_set_compute_shader_sizes(ctx, shd, (int [3]){ 1, CGS, 1 });
155
156         RET(ff_vk_add_descriptor_set(ctx, s->pl_ver, shd, desc_i, 2, 0));
157
158         GLSLF(0, #define FILTER_RADIUS (%i)                     ,s->size_y - 1);
159         GLSLC(0, #define INC(x) (ivec2(0, x))                                 );
160         GLSLC(0, #define DIR(var) (var.y)                                     );
161         GLSLD(   blur_kernel                                                  );
162         GLSLC(0, void main()                                                  );
163         GLSLC(0, {                                                            );
164         GLSLC(1,     ivec2 size;                                              );
165         GLSLC(1,     const ivec2 pos = ivec2(gl_GlobalInvocationID.xy);       );
166         for (int i = 0; i < planes; i++) {
167             GLSLC(0,                                                          );
168             GLSLF(1,  size = imageSize(output_img[%i]);                     ,i);
169             GLSLC(1,  if (IS_WITHIN(pos, size)) {                             );
170             if (s->planes & (1 << i)) {
171                 GLSLF(2, distort(pos, %i);                                  ,i);
172             } else {
173                 GLSLF(2, vec4 res = texture(input_img[%i], pos);            ,i);
174                 GLSLF(2, imageStore(output_img[%i], pos, res);              ,i);
175             }
176             GLSLC(1, }                                                        );
177         }
178         GLSLC(0, }                                                            );
179
180         RET(ff_vk_compile_shader(ctx, shd, "main"));
181
182         RET(ff_vk_init_pipeline_layout(ctx, s->pl_ver));
183         RET(ff_vk_init_compute_pipeline(ctx, s->pl_ver));
184     }
185
186     /* Execution context */
187     RET(ff_vk_create_exec_ctx(ctx, &s->exec,
188                               s->vkctx.hwctx->queue_family_comp_index));
189
190     s->initialized = 1;
191
192     return 0;
193
194 fail:
195     return err;
196 }
197
198 static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f, AVFrame *in_f)
199 {
200     int err;
201     AvgBlurVulkanContext *s = avctx->priv;
202     AVVkFrame *in = (AVVkFrame *)in_f->data[0];
203     AVVkFrame *tmp = (AVVkFrame *)tmp_f->data[0];
204     AVVkFrame *out = (AVVkFrame *)out_f->data[0];
205     int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
206
207     for (int i = 0; i < planes; i++) {
208         RET(ff_vk_create_imageview(avctx, &s->input_images[i].imageView, in->img[i],
209                                    av_vkfmt_from_pixfmt(s->vkctx.input_format)[i],
210                                    ff_comp_identity_map));
211
212         RET(ff_vk_create_imageview(avctx, &s->tmp_images[i].imageView, tmp->img[i],
213                                    av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
214                                    ff_comp_identity_map));
215
216         RET(ff_vk_create_imageview(avctx, &s->output_images[i].imageView, out->img[i],
217                                    av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
218                                    ff_comp_identity_map));
219
220         s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
221         s->tmp_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
222         s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
223     }
224
225     ff_vk_update_descriptor_set(avctx, s->pl_hor, 0);
226     ff_vk_update_descriptor_set(avctx, s->pl_ver, 0);
227
228     ff_vk_start_exec_recording(avctx, s->exec);
229
230     for (int i = 0; i < planes; i++) {
231         VkImageMemoryBarrier bar[] = {
232             {
233                 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
234                 .srcAccessMask = 0,
235                 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
236                 .oldLayout = in->layout[i],
237                 .newLayout = s->input_images[i].imageLayout,
238                 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
239                 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
240                 .image = in->img[i],
241                 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
242                 .subresourceRange.levelCount = 1,
243                 .subresourceRange.layerCount = 1,
244             },
245             {
246                 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
247                 .srcAccessMask = 0,
248                 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT,
249                 .oldLayout = tmp->layout[i],
250                 .newLayout = s->tmp_images[i].imageLayout,
251                 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
252                 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
253                 .image = tmp->img[i],
254                 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
255                 .subresourceRange.levelCount = 1,
256                 .subresourceRange.layerCount = 1,
257             },
258             {
259                 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
260                 .srcAccessMask = 0,
261                 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
262                 .oldLayout = out->layout[i],
263                 .newLayout = s->output_images[i].imageLayout,
264                 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
265                 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
266                 .image = out->img[i],
267                 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
268                 .subresourceRange.levelCount = 1,
269                 .subresourceRange.layerCount = 1,
270             },
271         };
272
273         vkCmdPipelineBarrier(s->exec->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
274                              VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
275                              0, NULL, 0, NULL, FF_ARRAY_ELEMS(bar), bar);
276
277         in->layout[i]  = bar[0].newLayout;
278         in->access[i]  = bar[0].dstAccessMask;
279
280         tmp->layout[i] = bar[1].newLayout;
281         tmp->access[i] = bar[1].dstAccessMask;
282
283         out->layout[i] = bar[2].newLayout;
284         out->access[i] = bar[2].dstAccessMask;
285     }
286
287     ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl_hor);
288
289     vkCmdDispatch(s->exec->buf, FFALIGN(s->vkctx.output_width, CGS)/CGS,
290                   s->vkctx.output_height, 1);
291
292     ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl_ver);
293
294     vkCmdDispatch(s->exec->buf, s->vkctx.output_width,
295                   FFALIGN(s->vkctx.output_height, CGS)/CGS, 1);
296
297     ff_vk_add_exec_dep(avctx, s->exec, in_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
298     ff_vk_add_exec_dep(avctx, s->exec, out_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
299
300     err = ff_vk_submit_exec_queue(avctx, s->exec);
301     if (err)
302         return err;
303
304 fail:
305
306     for (int i = 0; i < planes; i++) {
307         ff_vk_destroy_imageview(avctx, &s->input_images[i].imageView);
308         ff_vk_destroy_imageview(avctx, &s->tmp_images[i].imageView);
309         ff_vk_destroy_imageview(avctx, &s->output_images[i].imageView);
310     }
311
312     return err;
313 }
314
315 static int avgblur_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
316 {
317     int err;
318     AVFrame *tmp = NULL, *out = NULL;
319     AVFilterContext *ctx = link->dst;
320     AvgBlurVulkanContext *s = ctx->priv;
321     AVFilterLink *outlink = ctx->outputs[0];
322
323     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
324     if (!out) {
325         err = AVERROR(ENOMEM);
326         goto fail;
327     }
328
329     tmp = ff_get_video_buffer(outlink, outlink->w, outlink->h);
330     if (!out) {
331         err = AVERROR(ENOMEM);
332         goto fail;
333     }
334
335     if (!s->initialized)
336         RET(init_filter(ctx, in));
337
338     RET(process_frames(ctx, out, tmp, in));
339
340     err = av_frame_copy_props(out, in);
341     if (err < 0)
342         goto fail;
343
344     av_frame_free(&in);
345     av_frame_free(&tmp);
346
347     return ff_filter_frame(outlink, out);
348
349 fail:
350     av_frame_free(&in);
351     av_frame_free(&tmp);
352     av_frame_free(&out);
353     return err;
354 }
355
356 static void avgblur_vulkan_uninit(AVFilterContext *avctx)
357 {
358     AvgBlurVulkanContext *s = avctx->priv;
359
360     ff_vk_filter_uninit(avctx);
361
362     s->initialized = 0;
363 }
364
365 #define OFFSET(x) offsetof(AvgBlurVulkanContext, x)
366 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
367 static const AVOption avgblur_vulkan_options[] = {
368     { "sizeX",  "Set horizontal radius", OFFSET(size_x), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 32, .flags = FLAGS },
369     { "planes", "Set planes to filter (bitmask)", OFFSET(planes), AV_OPT_TYPE_INT, {.i64 = 0xF}, 0, 0xF, .flags = FLAGS },
370     { "sizeY",  "Set vertical radius", OFFSET(size_y), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 32, .flags = FLAGS },
371     { NULL },
372 };
373
374 AVFILTER_DEFINE_CLASS(avgblur_vulkan);
375
376 static const AVFilterPad avgblur_vulkan_inputs[] = {
377     {
378         .name         = "default",
379         .type         = AVMEDIA_TYPE_VIDEO,
380         .filter_frame = &avgblur_vulkan_filter_frame,
381         .config_props = &ff_vk_filter_config_input,
382     },
383     { NULL }
384 };
385
386 static const AVFilterPad avgblur_vulkan_outputs[] = {
387     {
388         .name = "default",
389         .type = AVMEDIA_TYPE_VIDEO,
390         .config_props = &ff_vk_filter_config_output,
391     },
392     { NULL }
393 };
394
395 AVFilter ff_vf_avgblur_vulkan = {
396     .name           = "avgblur_vulkan",
397     .description    = NULL_IF_CONFIG_SMALL("Apply avgblur mask to input video"),
398     .priv_size      = sizeof(AvgBlurVulkanContext),
399     .init           = &ff_vk_filter_init,
400     .uninit         = &avgblur_vulkan_uninit,
401     .query_formats  = &ff_vk_filter_query_formats,
402     .inputs         = avgblur_vulkan_inputs,
403     .outputs        = avgblur_vulkan_outputs,
404     .priv_class     = &avgblur_vulkan_class,
405     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
406 };