History

avfilter: add a transpose_vulkan filter

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

Wu Jianhua committed 4 years ago

Commit 58816f61 parent ceeff7a4 files · +319 −0Browse files

Showing the changes to libavfilter (3 of 4 files). Show all

Unified · Split

3 files
  1. libavfilter/Makefile +1 −0
  2. libavfilter/allfilters.c +1 −0
  3. libavfilter/vf_transpose_vulkan.c +316 −0
libavfilter/Makefile+1−0
@@ -483,6 +483,7 @@ OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o · 482 unmodified lines
483483 OBJS-$(CONFIG_TRANSPOSE_NPP_FILTER) += vf_transpose_npp.o
484484 OBJS-$(CONFIG_TRANSPOSE_OPENCL_FILTER) += vf_transpose_opencl.o opencl.o opencl/transpose.o
485485 OBJS-$(CONFIG_TRANSPOSE_VAAPI_FILTER) += vf_transpose_vaapi.o vaapi_vpp.o
486+OBJS-$(CONFIG_TRANSPOSE_VULKAN_FILTER) += vf_transpose_vulkan.o vulkan.o vulkan_filter.o
486487 OBJS-$(CONFIG_TRIM_FILTER) += trim.o
487488 OBJS-$(CONFIG_UNPREMULTIPLY_FILTER) += vf_premultiply.o framesync.o
488489 OBJS-$(CONFIG_UNSHARP_FILTER) += vf_unsharp.o
libavfilter/allfilters.c+1−0
@@ -462,6 +462,7 @@ extern const AVFilter ff_vf_transpose; · 461 unmodified lines
462462 extern const AVFilter ff_vf_transpose_npp;
463463 extern const AVFilter ff_vf_transpose_opencl;
464464 extern const AVFilter ff_vf_transpose_vaapi;
465+extern const AVFilter ff_vf_transpose_vulkan;
465466 extern const AVFilter ff_vf_trim;
466467 extern const AVFilter ff_vf_unpremultiply;
467468 extern const AVFilter ff_vf_unsharp;
libavfilter/vf_transpose_vulkan.c+316−0
@@ -0,0 +1,316 @@
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+
27+typedef struct TransposeVulkanContext {
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+} TransposeVulkanContext;
38+
39+static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
40+{
41+ int err = 0;
42+ FFVkSPIRVShader *shd;
43+ TransposeVulkanContext *s = ctx->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_images",
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_images",
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, "transpose_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_images[%i]); ,i);
93+ GLSLC(1, if (IS_WITHIN(pos, size)) { );
94+ GLSLF(2, vec4 res = texture(input_images[%i], pos.yx); ,i);
95+ GLSLF(2, imageStore(output_images[%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+
108+fail:
109+ return err;
110+}
111+
112+static int process_frames(AVFilterContext *avctx, AVFrame *outframe, AVFrame *inframe)
113+{
114+ int err = 0;
115+ VkCommandBuffer cmd_buf;
116+ TransposeVulkanContext *s = avctx->priv;
117+ FFVulkanContext *vkctx = &s->vkctx;
118+ FFVulkanFunctions *vk = &s->vkctx.vkfn;
119+ const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
120+
121+ AVVkFrame *in = (AVVkFrame *)inframe->data[0];
122+ AVVkFrame *out = (AVVkFrame *)outframe->data[0];
123+
124+ const VkFormat *input_formats = av_vkfmt_from_pixfmt(s->vkctx.input_format);
125+ const VkFormat *output_formats = av_vkfmt_from_pixfmt(s->vkctx.output_format);
126+
127+ ff_vk_start_exec_recording(vkctx, s->exec);
128+ cmd_buf = ff_vk_get_exec_buf(s->exec);
129+
130+ for (int i = 0; i < planes; i++) {
131+ RET(ff_vk_create_imageview(vkctx, s->exec,
132+ &s->input_images[i].imageView, in->img[i],
133+ input_formats[i],
134+ ff_comp_identity_map));
135+
136+ RET(ff_vk_create_imageview(vkctx, s->exec,
137+ &s->output_images[i].imageView, out->img[i],
138+ output_formats[i],
139+ ff_comp_identity_map));
140+
141+ s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
142+ s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
143+ }
144+
145+ ff_vk_update_descriptor_set(vkctx, s->pl, 0);
146+
147+ for (int i = 0; i < planes; i++) {
148+ VkImageMemoryBarrier barriers[] = {
149+ {
150+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
151+ .srcAccessMask = 0,
152+ .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
153+ .oldLayout = in->layout[i],
154+ .newLayout = s->input_images[i].imageLayout,
155+ .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
156+ .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
157+ .image = in->img[i],
158+ .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
159+ .subresourceRange.levelCount = 1,
160+ .subresourceRange.layerCount = 1,
161+ },
162+ {
163+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
164+ .srcAccessMask = 0,
165+ .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
166+ .oldLayout = out->layout[i],
167+ .newLayout = s->output_images[i].imageLayout,
168+ .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
169+ .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
170+ .image = out->img[i],
171+ .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
172+ .subresourceRange.levelCount = 1,
173+ .subresourceRange.layerCount = 1,
174+ },
175+ };
176+
177+ vk->CmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
178+ VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
179+ 0, NULL, 0, NULL, FF_ARRAY_ELEMS(barriers), barriers);
180+
181+ in->layout[i] = barriers[0].newLayout;
182+ in->access[i] = barriers[0].dstAccessMask;
183+
184+ out->layout[i] = barriers[1].newLayout;
185+ out->access[i] = barriers[1].dstAccessMask;
186+ }
187+
188+ ff_vk_bind_pipeline_exec(vkctx, s->exec, s->pl);
189+ vk->CmdDispatch(cmd_buf, FFALIGN(s->vkctx.output_width, CGS)/CGS,
190+ s->vkctx.output_height, 1);
191+
192+ ff_vk_add_exec_dep(vkctx, s->exec, inframe, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
193+ ff_vk_add_exec_dep(vkctx, s->exec, outframe, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
194+
195+ err = ff_vk_submit_exec_queue(vkctx, s->exec);
196+ if (err)
197+ return err;
198+
199+ ff_vk_qf_rotate(&s->qf);
200+
201+ return 0;
202+
203+fail:
204+ ff_vk_discard_exec_deps(s->exec);
205+ return err;
206+}
207+
208+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
209+{
210+ int err;
211+ AVFrame *out = NULL;
212+ AVFilterContext *ctx = inlink->dst;
213+ TransposeVulkanContext *s = ctx->priv;
214+ AVFilterLink *outlink = ctx->outputs[0];
215+
216+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
217+ if (!out) {
218+ err = AVERROR(ENOMEM);
219+ goto fail;
220+ }
221+
222+ if (!s->initialized)
223+ RET(init_filter(ctx, in));
224+
225+ RET(process_frames(ctx, out, in));
226+
227+ RET(av_frame_copy_props(out, in));
228+
229+ if (in->sample_aspect_ratio.num)
230+ out->sample_aspect_ratio = in->sample_aspect_ratio;
231+ else {
232+ out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
233+ out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
234+ }
235+
236+ av_frame_free(&in);
237+
238+ return ff_filter_frame(outlink, out);
239+
240+fail:
241+ av_frame_free(&in);
242+ av_frame_free(&out);
243+ return err;
244+}
245+
246+static av_cold void transpose_vulkan_uninit(AVFilterContext *avctx)
247+{
248+ TransposeVulkanContext *s = avctx->priv;
249+ ff_vk_uninit(&s->vkctx);
250+
251+ s->initialized = 0;
252+}
253+
254+static int config_props_output(AVFilterLink *outlink)
255+{
256+ int err = 0;
257+ AVFilterContext *avctx = outlink->src;
258+ TransposeVulkanContext *s = avctx->priv;
259+ FFVulkanContext *vkctx = &s->vkctx;
260+ AVFilterLink *inlink = avctx->inputs[0];
261+
262+ vkctx->output_width = inlink->h;
263+ vkctx->output_height = inlink->w;
264+
265+ RET(ff_vk_filter_config_output(outlink));
266+
267+ outlink->w = inlink->h;
268+ outlink->h = inlink->w;
269+
270+ if (inlink->sample_aspect_ratio.num)
271+ outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
272+ inlink->sample_aspect_ratio);
273+ else
274+ outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
275+
276+ err = 0;
277+
278+fail:
279+ return err;
280+}
281+
282+static const AVOption transpose_vulkan_options[] = {
283+ { NULL }
284+};
285+
286+AVFILTER_DEFINE_CLASS(transpose_vulkan);
287+
288+static const AVFilterPad transpose_vulkan_inputs[] = {
289+ {
290+ .name = "default",
291+ .type = AVMEDIA_TYPE_VIDEO,
292+ .filter_frame = &filter_frame,
293+ .config_props = &ff_vk_filter_config_input,
294+ }
295+};
296+
297+static const AVFilterPad transpose_vulkan_outputs[] = {
298+ {
299+ .name = "default",
300+ .type = AVMEDIA_TYPE_VIDEO,
301+ .config_props = &config_props_output,
302+ }
303+};
304+
305+const AVFilter ff_vf_transpose_vulkan = {
306+ .name = "transpose_vulkan",
307+ .description = NULL_IF_CONFIG_SMALL("Transpose Vulkan Filter"),
308+ .priv_size = sizeof(TransposeVulkanContext),
309+ .init = &ff_vk_filter_init,
310+ .uninit = &transpose_vulkan_uninit,
311+ FILTER_INPUTS(transpose_vulkan_inputs),
312+ FILTER_OUTPUTS(transpose_vulkan_outputs),
313+ FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VULKAN),
314+ .priv_class = &transpose_vulkan_class,
315+ .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
316+};

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