FFmpeg
Loading...
Searching...
No Matches
vf_flip_vulkan.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2021 Wu Jianhua <jianhua.wu@intel.com>
3 * Copyright (c) Lynne
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/opt.h"
23#include "vulkan_filter.h"
24
25#include "filters.h"
26#include "video.h"
27
28extern const unsigned char ff_flip_comp_spv_data[];
29extern const unsigned int ff_flip_comp_spv_len;
30
36
45
47{
48 int err = 0;
49 FlipVulkanContext *s = ctx->priv;
50 FFVulkanContext *vkctx = &s->vkctx;
51 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
52 FFVulkanShader *shd = &s->shd;
53
54 s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
55 if (!s->qf) {
56 av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
57 err = AVERROR(ENOTSUP);
58 goto fail;
59 }
60
61 RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, FF_VK_DEFAULT_EXEC_CONTEXTS, 0, 0, 0, NULL));
62
63 ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
64 (uint32_t []) { 32, 8, planes }, 0);
65
66 ff_vk_shader_add_push_const(&s->shd, 0, sizeof(int),
67 VK_SHADER_STAGE_COMPUTE_BIT);
68
70 { /* input_img */
71 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
72 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
73 .elems = planes,
74 },
75 { /* output_img */
76 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
77 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
78 .elems = planes,
79 },
80 };
81 ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 2, 0);
82
83 RET(ff_vk_shader_link(vkctx, shd,
85 ff_flip_comp_spv_len, "main"));
86
87 RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
88
89 s->initialized = 1;
90
91fail:
92 return err;
93}
94
96{
97 FlipVulkanContext *s = avctx->priv;
98 FFVulkanContext *vkctx = &s->vkctx;
99
100 ff_vk_exec_pool_free(vkctx, &s->e);
101 ff_vk_shader_free(vkctx, &s->shd);
102
103 ff_vk_uninit(&s->vkctx);
104
105 s->initialized = 0;
106}
107
108static int filter_frame(AVFilterLink *link, AVFrame *in, enum FlipType type)
109{
110 int err;
111 AVFrame *out = NULL;
112 AVFilterContext *ctx = link->dst;
113 FlipVulkanContext *s = ctx->priv;
114 AVFilterLink *outlink = ctx->outputs[0];
115
116 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
117 if (!out) {
118 err = AVERROR(ENOMEM);
119 goto fail;
120 }
121
122 if (!s->initialized)
123 RET(init_filter(ctx, in));
124
125 RET(ff_vk_filter_process_simple(&s->vkctx, &s->e, &s->shd, out, in,
126 VK_NULL_HANDLE, 1, &type, sizeof(int)));
127
129
130 av_frame_free(&in);
131
132 return ff_filter_frame(outlink, out);
133
134fail:
135 av_frame_free(&in);
137 return err;
138}
139
141{
142 return filter_frame(link, in, FLIP_HORIZONTAL);
143}
144
146{
147 return filter_frame(link, in, FLIP_VERTICAL);
148}
149
151{
152 return filter_frame(link, in, FLIP_BOTH);
153}
154
156 {
157 .name = "default",
158 .type = AVMEDIA_TYPE_VIDEO,
159 .config_props = &ff_vk_filter_config_output,
160 }
161};
162
164 { NULL },
165};
166
168
170 {
171 .name = "default",
172 .type = AVMEDIA_TYPE_VIDEO,
173 .filter_frame = &hflip_vulkan_filter_frame,
174 .config_props = &ff_vk_filter_config_input,
175 }
176};
177
179 .p.name = "hflip_vulkan",
180 .p.description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video in Vulkan"),
181 .p.priv_class = &hflip_vulkan_class,
182 .priv_size = sizeof(FlipVulkanContext),
188 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
189};
190
192 { NULL },
193};
194
196
198 {
199 .name = "default",
200 .type = AVMEDIA_TYPE_VIDEO,
201 .filter_frame = &vflip_vulkan_filter_frame,
202 .config_props = &ff_vk_filter_config_input,
203 }
204};
205
207 .p.name = "vflip_vulkan",
208 .p.description = NULL_IF_CONFIG_SMALL("Vertically flip the input video in Vulkan"),
209 .p.priv_class = &vflip_vulkan_class,
210 .priv_size = sizeof(FlipVulkanContext),
216 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
217};
218
220 { NULL },
221};
222
224
226 {
227 .name = "default",
228 .type = AVMEDIA_TYPE_VIDEO,
229 .filter_frame = &flip_vulkan_filter_frame,
230 .config_props = &ff_vk_filter_config_input,
231 }
232};
233
235 .p.name = "flip_vulkan",
236 .p.description = NULL_IF_CONFIG_SMALL("Flip both horizontally and vertically"),
237 .p.priv_class = &flip_vulkan_class,
238 .p.flags = AVFILTER_FLAG_HWDEVICE,
239 .priv_size = sizeof(FlipVulkanContext),
245 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
246};
const FFFilter ff_vf_hflip_vulkan
const FFFilter ff_vf_flip_vulkan
const FFFilter ff_vf_vflip_vulkan
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ FLIP_HORIZONTAL
@ FLIP_VERTICAL
cl_device_type type
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
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:2070
void ff_vk_shader_add_descriptor_set(FFVulkanContext *s, FFVulkanShader *shd, const FFVulkanDescriptorSetBinding *desc, int nb, int singular)
Add descriptor to a shader.
Definition vulkan.c:2373
void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
Definition vulkan.c:310
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
int ff_vk_shader_add_push_const(FFVulkanShader *shd, int offset, int size, VkShaderStageFlagBits stage)
Add/update push constants for execution.
Definition vulkan.c:1443
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition vulkan.c:2638
void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd)
Free a shader.
Definition vulkan.c:2614
int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, FFVulkanShader *shd)
Register a shader with an exec pool.
Definition vulkan.c:2407
AVVulkanDeviceQueueFamily * ff_vk_qf_find(FFVulkanContext *s, VkQueueFlagBits dev_family, VkVideoCodecOperationFlagBitsKHR vid_ops)
Chooses an appropriate QF.
Definition vulkan.c:297
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:2267
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
FFVulkanShader shd
AVVulkanDeviceQueueFamily * qf
FFVulkanContext vkctx
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int flip_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
static const AVOption vflip_vulkan_options[]
static const AVFilterPad flip_vulkan_inputs[]
static int filter_frame(AVFilterLink *link, AVFrame *in, enum FlipType type)
static const AVFilterPad vflip_vulkan_inputs[]
static const AVOption flip_vulkan_options[]
FlipType
@ FLIP_BOTH
const unsigned int ff_flip_comp_spv_len
static int hflip_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
static av_cold void flip_vulkan_uninit(AVFilterContext *avctx)
static int vflip_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
static const AVFilterPad hflip_vulkan_inputs[]
const unsigned char ff_flip_comp_spv_data[]
static const AVFilterPad flip_vulkan_outputs[]
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
static const AVOption hflip_vulkan_options[]
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
#define FF_VK_DEFAULT_EXEC_CONTEXTS
Definition vulkan.h:121
#define RET(x)
Definition vulkan.h:37
int ff_vk_filter_config_input(AVFilterLink *inlink)
int ff_vk_filter_config_output(AVFilterLink *outlink)
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.
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.