History

avfilter: add a hflip_vulkan filter

The following command is on how to apply hflip_vulkan filter: ffmpeg -init_hw_device vulkan -i input.264 -vf hwupload=extra_hw_frames=16,hflip_vulkan,hwdownload,format=yuv420p output.264

Wu Jianhua committed 4 years ago

Commit d313cb51 parent ff82bd54 files · +281 −0Browse files

Unified · Split

4 files
  1. configure +1 −0
  2. libavfilter/Makefile +1 −0
  3. libavfilter/allfilters.c +1 −0
  4. libavfilter/vf_hflip_vulkan.c +278 −0
configure+1−0
@@ -3616,6 +3616,7 @@ frei0r_filter_deps="frei0r" · 3615 unmodified lines
3616frei0r_src_filter_deps="frei0r"3616frei0r_src_filter_deps="frei0r"
3617fspp_filter_deps="gpl"3617fspp_filter_deps="gpl"
3618gblur_vulkan_filter_deps="vulkan spirv_compiler"3618gblur_vulkan_filter_deps="vulkan spirv_compiler"
3619hflip_vulkan_filter_deps="vulkan spirv_compiler"
3619histeq_filter_deps="gpl"3620histeq_filter_deps="gpl"
3620hqdn3d_filter_deps="gpl"3621hqdn3d_filter_deps="gpl"
3621interlace_filter_deps="gpl"3622interlace_filter_deps="gpl"
libavfilter/Makefile+1−0
@@ -298,6 +298,7 @@ OBJS-$(CONFIG_GREYEDGE_FILTER) += vf_colorconstancy.o · 297 unmodified lines
298OBJS-$(CONFIG_GUIDED_FILTER) += vf_guided.o298OBJS-$(CONFIG_GUIDED_FILTER) += vf_guided.o
299OBJS-$(CONFIG_HALDCLUT_FILTER) += vf_lut3d.o framesync.o299OBJS-$(CONFIG_HALDCLUT_FILTER) += vf_lut3d.o framesync.o
300OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o300OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
301OBJS-$(CONFIG_HFLIP_VULKAN_FILTER) += vf_hflip_vulkan.o vulkan.o
301OBJS-$(CONFIG_HISTEQ_FILTER) += vf_histeq.o302OBJS-$(CONFIG_HISTEQ_FILTER) += vf_histeq.o
302OBJS-$(CONFIG_HISTOGRAM_FILTER) += vf_histogram.o303OBJS-$(CONFIG_HISTOGRAM_FILTER) += vf_histogram.o
303OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o304OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o
libavfilter/allfilters.c+1−0
@@ -283,6 +283,7 @@ extern const AVFilter ff_vf_greyedge; · 282 unmodified lines
283extern const AVFilter ff_vf_guided;283extern const AVFilter ff_vf_guided;
284extern const AVFilter ff_vf_haldclut;284extern const AVFilter ff_vf_haldclut;
285extern const AVFilter ff_vf_hflip;285extern const AVFilter ff_vf_hflip;
286extern const AVFilter ff_vf_hflip_vulkan;
286extern const AVFilter ff_vf_histeq;287extern const AVFilter ff_vf_histeq;
287extern const AVFilter ff_vf_histogram;288extern const AVFilter ff_vf_histogram;
288extern const AVFilter ff_vf_hqdn3d;289extern const AVFilter ff_vf_hqdn3d;
libavfilter/vf_hflip_vulkan.c+278−0
@@ -0,0 +1,278 @@
1/*
2 * copyright (c) 2021 Wu Jianhua <jianhua.wu@intel.com>
3 * This file is part of FFmpeg.
4 *
5 * FFmpeg is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * FFmpeg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with FFmpeg; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "libavutil/random_seed.h"
21#include "libavutil/opt.h"
22#include "vulkan_filter.h"
23#include "internal.h"
24
25#define CGS 32
26
27typedef struct HFlipVulkanContext {
28 FFVulkanContext vkctx;
29 FFVkQueueFamilyCtx qf;
30 FFVkExecContext *exec;
31 FFVulkanPipeline *pl;
32
33 VkDescriptorImageInfo input_images[3];
34 VkDescriptorImageInfo output_images[3];
35
36 int initialized;
37} HFlipVulkanContext;
38
39static av_cold int init_filter(AVFilterContext *avctx, AVFrame *in)
40{
41 int err = 0;
42 FFVkSPIRVShader *shd;
43 HFlipVulkanContext *s = avctx->priv;
44 FFVulkanContext *vkctx = &s->vkctx;
45 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
46
47 FFVulkanDescriptorSetBinding image_descs[] = {
48 {
49 .name = "input_image",
50 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
51 .dimensions = 2,
52 .elems = planes,
53 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
54 .updater = s->input_images,
55 },
56 {
57 .name = "output_image",
58 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
59 .mem_layout = ff_vk_shader_rep_fmt(s->vkctx.output_format),
60 .mem_quali = "writeonly",
61 .dimensions = 2,
62 .elems = planes,
63 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
64 .updater = s->output_images,
65 },
66 };
67
68 image_descs[0].sampler = ff_vk_init_sampler(vkctx, 1, VK_FILTER_LINEAR);
69 if (!image_descs[0].sampler)
70 return AVERROR_EXTERNAL;
71
72 ff_vk_qf_init(vkctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
73
74 {
75 s->pl = ff_vk_create_pipeline(vkctx, &s->qf);
76 if (!s->pl)
77 return AVERROR(ENOMEM);
78
79 shd = ff_vk_init_shader(s->pl, "hflip_compute", image_descs[0].stages);
80 if (!shd)
81 return AVERROR(ENOMEM);
82
83 ff_vk_set_compute_shader_sizes(shd, (int [3]){ CGS, 1, 1 });
84 RET(ff_vk_add_descriptor_set(vkctx, s->pl, shd, image_descs, FF_ARRAY_ELEMS(image_descs), 0));
85
86 GLSLC(0, void main() );
87 GLSLC(0, { );
88 GLSLC(1, ivec2 size; );
89 GLSLC(1, const ivec2 pos = ivec2(gl_GlobalInvocationID.xy); );
90 for (int i = 0; i < planes; i++) {
91 GLSLC(0, );
92 GLSLF(1, size = imageSize(output_image[%i]); ,i);
93 GLSLC(1, if (IS_WITHIN(pos, size)) { );
94 GLSLF(2, vec4 res = texture(input_image[%i], ivec2(size.x - pos.x, pos.y)); ,i);
95 GLSLF(2, imageStore(output_image[%i], pos, res); ,i);
96 GLSLC(1, } );
97 }
98 GLSLC(0, } );
99
100 RET(ff_vk_compile_shader(vkctx, shd, "main"));
101 RET(ff_vk_init_pipeline_layout(vkctx, s->pl));
102 RET(ff_vk_init_compute_pipeline(vkctx, s->pl));
103 }
104
105 RET(ff_vk_create_exec_ctx(vkctx, &s->exec, &s->qf));
106 s->initialized = 1;
107
108fail:
109 return err;
110}
111
112static av_cold void hflip_vulkan_uninit(AVFilterContext *avctx)
113{
114 HFlipVulkanContext *s = avctx->priv;
115 ff_vk_uninit(&s->vkctx);
116
117 s->initialized = 0;
118}
119
120static int process_frames(AVFilterContext *avctx, AVFrame *outframe, AVFrame *inframe)
121{
122 int err = 0;
123 VkCommandBuffer cmd_buf;
124 HFlipVulkanContext *s = avctx->priv;
125 FFVulkanContext *vkctx = &s->vkctx;
126 FFVulkanFunctions *vk = &s->vkctx.vkfn;
127 AVVkFrame *in = (AVVkFrame *)inframe->data[0];
128 AVVkFrame *out = (AVVkFrame *)outframe->data[0];
129 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
130 const VkFormat *input_formats = av_vkfmt_from_pixfmt(s->vkctx.input_format);
131 const VkFormat *output_formats = av_vkfmt_from_pixfmt(s->vkctx.output_format);
132
133 ff_vk_start_exec_recording(vkctx, s->exec);
134 cmd_buf = ff_vk_get_exec_buf(s->exec);
135
136 for (int i = 0; i < planes; i++) {
137 RET(ff_vk_create_imageview(vkctx, s->exec,
138 &s->input_images[i].imageView, in->img[i],
139 input_formats[i],
140 ff_comp_identity_map));
141
142 RET(ff_vk_create_imageview(vkctx, s->exec,
143 &s->output_images[i].imageView, out->img[i],
144 output_formats[i],
145 ff_comp_identity_map));
146
147 s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
148 s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
149 }
150
151 ff_vk_update_descriptor_set(vkctx, s->pl, 0);
152
153 for (int i = 0; i < planes; i++) {
154 VkImageMemoryBarrier barriers[] = {
155 {
156 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
157 .srcAccessMask = 0,
158 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
159 .oldLayout = in->layout[i],
160 .newLayout = s->input_images[i].imageLayout,
161 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
162 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
163 .image = in->img[i],
164 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
165 .subresourceRange.levelCount = 1,
166 .subresourceRange.layerCount = 1,
167 },
168 {
169 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
170 .srcAccessMask = 0,
171 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
172 .oldLayout = out->layout[i],
173 .newLayout = s->output_images[i].imageLayout,
174 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
175 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
176 .image = out->img[i],
177 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
178 .subresourceRange.levelCount = 1,
179 .subresourceRange.layerCount = 1,
180 },
181 };
182
183 vk->CmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
184 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
185 0, NULL, 0, NULL, FF_ARRAY_ELEMS(barriers), barriers);
186
187 in->layout[i] = barriers[0].newLayout;
188 in->access[i] = barriers[0].dstAccessMask;
189
190 out->layout[i] = barriers[1].newLayout;
191 out->access[i] = barriers[1].dstAccessMask;
192 }
193
194 ff_vk_bind_pipeline_exec(vkctx, s->exec, s->pl);
195 vk->CmdDispatch(cmd_buf, FFALIGN(s->vkctx.output_width, CGS)/CGS,
196 s->vkctx.output_height, 1);
197
198 ff_vk_add_exec_dep(vkctx, s->exec, inframe, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
199 ff_vk_add_exec_dep(vkctx, s->exec, outframe, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
200
201 err = ff_vk_submit_exec_queue(vkctx, s->exec);
202 if (err)
203 return err;
204
205 ff_vk_qf_rotate(&s->qf);
206
207 return 0;
208fail:
209 ff_vk_discard_exec_deps(s->exec);
210 return err;
211}
212
213static int hflip_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
214{
215 int err;
216 AVFrame *out = NULL;
217 AVFilterContext *ctx = link->dst;
218 HFlipVulkanContext *s = ctx->priv;
219 AVFilterLink *outlink = ctx->outputs[0];
220
221 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
222 if (!out) {
223 err = AVERROR(ENOMEM);
224 goto fail;
225 }
226
227 if (!s->initialized)
228 RET(init_filter(ctx, in));
229
230 RET(process_frames(ctx, out, in));
231
232 RET(av_frame_copy_props(out, in));
233
234 av_frame_free(&in);
235
236 return ff_filter_frame(outlink, out);
237
238fail:
239 av_frame_free(&in);
240 av_frame_free(&out);
241 return err;
242}
243
244static const AVOption hflip_vulkan_options[] = {
245 { NULL },
246};
247
248AVFILTER_DEFINE_CLASS(hflip_vulkan);
249
250static const AVFilterPad hflip_vulkan_inputs[] = {
251 {
252 .name = "default",
253 .type = AVMEDIA_TYPE_VIDEO,
254 .filter_frame = &hflip_vulkan_filter_frame,
255 .config_props = &ff_vk_filter_config_input,
256 }
257};
258
259static const AVFilterPad hflip_vulkan_outputs[] = {
260 {
261 .name = "default",
262 .type = AVMEDIA_TYPE_VIDEO,
263 .config_props = &ff_vk_filter_config_output,
264 }
265};
266
267const AVFilter ff_vf_hflip_vulkan = {
268 .name = "hflip_vulkan",
269 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video in Vulkan"),
270 .priv_size = sizeof(HFlipVulkanContext),
271 .init = &ff_vk_filter_init,
272 .uninit = &hflip_vulkan_uninit,
273 FILTER_INPUTS(hflip_vulkan_inputs),
274 FILTER_OUTPUTS(hflip_vulkan_outputs),
275 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VULKAN),
276 .priv_class = &hflip_vulkan_class,
277 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
278};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t