FFmpeg
vf_transpose_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 "transpose.h"
27 #include "video.h"
28 
29 extern const unsigned char ff_transpose_comp_spv_data[];
30 extern const unsigned int ff_transpose_comp_spv_len;
31 
32 typedef struct TransposeVulkanContext {
34 
39 
40  int dir;
43 
45 {
46  int err;
47  TransposeVulkanContext *s = ctx->priv;
48  FFVulkanContext *vkctx = &s->vkctx;
49  const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
50 
51  s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
52  if (!s->qf) {
53  av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
54  err = AVERROR(ENOTSUP);
55  goto fail;
56  }
57 
58  RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, s->qf->num*4, 0, 0, 0, NULL));
59 
60  ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
61  (uint32_t []) { 32, 1, planes }, 0);
62 
63  ff_vk_shader_add_push_const(&s->shd, 0, sizeof(int),
64  VK_SHADER_STAGE_COMPUTE_BIT);
65 
67  { /* input_img */
68  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
69  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
70  .elems = planes,
71  },
72  { /* output_img */
73  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
74  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
75  .elems = planes,
76  },
77  };
78  ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 2, 0, 0);
79 
80  RET(ff_vk_shader_link(vkctx, &s->shd,
82  ff_transpose_comp_spv_len, "main"));
83 
84  RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
85 
86  s->initialized = 1;
87 
88 fail:
89  return err;
90 }
91 
93 {
94  int err;
95  AVFrame *out = NULL;
96  AVFilterContext *ctx = inlink->dst;
97  TransposeVulkanContext *s = ctx->priv;
98  AVFilterLink *outlink = ctx->outputs[0];
99 
100  if (s->passthrough)
101  return ff_filter_frame(outlink, in);
102 
103  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
104  if (!out) {
105  err = AVERROR(ENOMEM);
106  goto fail;
107  }
108 
109  if (!s->initialized)
110  RET(init_filter(ctx, in));
111 
112  RET(ff_vk_filter_process_simple(&s->vkctx, &s->e, &s->shd, out, in,
113  VK_NULL_HANDLE, 1, &s->dir, sizeof(int)));
114 
116 
117  if (in->sample_aspect_ratio.num)
118  out->sample_aspect_ratio = in->sample_aspect_ratio;
119  else {
120  out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
121  out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
122  }
123 
124  av_frame_free(&in);
125 
126  return ff_filter_frame(outlink, out);
127 
128 fail:
129  av_frame_free(&in);
130  av_frame_free(&out);
131  return err;
132 }
133 
135 {
136  TransposeVulkanContext *s = avctx->priv;
137  FFVulkanContext *vkctx = &s->vkctx;
138 
139  ff_vk_exec_pool_free(vkctx, &s->e);
140  ff_vk_shader_free(vkctx, &s->shd);
141 
142  ff_vk_uninit(&s->vkctx);
143 
144  s->initialized = 0;
145 }
146 
147 static int config_props_output(AVFilterLink *outlink)
148 {
149  FilterLink *outl = ff_filter_link(outlink);
150  AVFilterContext *avctx = outlink->src;
151  TransposeVulkanContext *s = avctx->priv;
152  FFVulkanContext *vkctx = &s->vkctx;
153  AVFilterLink *inlink = avctx->inputs[0];
155 
156  if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
157  (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
158  av_log(avctx, AV_LOG_VERBOSE,
159  "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
160  inlink->w, inlink->h, inlink->w, inlink->h);
162  return outl->hw_frames_ctx ? 0 : AVERROR(ENOMEM);
163  } else {
164  s->passthrough = TRANSPOSE_PT_TYPE_NONE;
165  }
166 
167  vkctx->output_width = inlink->h;
168  vkctx->output_height = inlink->w;
169 
170  if (inlink->sample_aspect_ratio.num)
171  outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
172  inlink->sample_aspect_ratio);
173  else
174  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
175 
176  return ff_vk_filter_config_output(outlink);
177 }
178 
179 #define OFFSET(x) offsetof(TransposeVulkanContext, x)
180 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | \
181  AV_OPT_FLAG_RUNTIME_PARAM)
182 
184  { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, .unit = "dir" },
185  { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
186  { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
187  { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
188  { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
189 
190  { "passthrough", "do not apply transposition if the input matches the specified geometry",
191  OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, .unit = "passthrough" },
192  { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
193  { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
194  { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
195 
196  { NULL }
197 };
198 
199 AVFILTER_DEFINE_CLASS(transpose_vulkan);
200 
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
205  .filter_frame = &filter_frame,
206  .config_props = &ff_vk_filter_config_input,
207  }
208 };
209 
211  {
212  .name = "default",
213  .type = AVMEDIA_TYPE_VIDEO,
214  .config_props = &config_props_output,
215  }
216 };
217 
219  .p.name = "transpose_vulkan",
220  .p.description = NULL_IF_CONFIG_SMALL("Transpose Vulkan Filter"),
221  .p.priv_class = &transpose_vulkan_class,
222  .p.flags = AVFILTER_FLAG_HWDEVICE,
223  .priv_size = sizeof(TransposeVulkanContext),
229  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
230 };
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
FFVulkanContext::output_height
int output_height
Definition: vulkan.h:361
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:2810
ff_transpose_comp_spv_data
const unsigned char ff_transpose_comp_spv_data[]
out
static FILE * out
Definition: movenc.c:55
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
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
transpose_vulkan_uninit
static av_cold void transpose_vulkan_uninit(AVFilterContext *avctx)
Definition: vf_transpose_vulkan.c:134
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
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
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
TransposeVulkanContext::vkctx
FFVulkanContext vkctx
Definition: vf_transpose_vulkan.c:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:434
ff_vk_filter_init
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
Definition: vulkan_filter.c:233
AVOption
AVOption.
Definition: opt.h:429
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:254
filters.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
TRANSPOSE_CLOCK_FLIP
@ TRANSPOSE_CLOCK_FLIP
Definition: transpose.h:34
ff_vk_uninit
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition: vulkan.c:2836
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
TransposeVulkanContext::initialized
int initialized
Definition: vf_transpose_vulkan.c:35
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
TRANSPOSE_CCLOCK
@ TRANSPOSE_CCLOCK
Definition: transpose.h:33
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
transpose_vulkan_outputs
static const AVFilterPad transpose_vulkan_outputs[]
Definition: vf_transpose_vulkan.c:210
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
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_transpose_vulkan.c:92
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:2603
OFFSET
#define OFFSET(x)
Definition: vf_transpose_vulkan.c:179
TransposeVulkanContext
Definition: vf_transpose_vulkan.c:32
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:40
TransposeVulkanContext::shd
FFVulkanShader shd
Definition: vf_transpose_vulkan.c:38
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
FFVulkanContext::output_width
int output_width
Definition: vulkan.h:360
s
#define s(width, name)
Definition: cbs_vp9.c:198
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:265
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:199
TransposeVulkanContext::dir
int dir
Definition: vf_transpose_vulkan.c:40
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
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
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
transpose_vulkan_options
static const AVOption transpose_vulkan_options[]
Definition: vf_transpose_vulkan.c:183
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:282
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:2376
FFVulkanContext
Definition: vulkan.h:312
TransposeVulkanContext::e
FFVkExecPool e
Definition: vf_transpose_vulkan.c:36
config_props_output
static int config_props_output(AVFilterLink *outlink)
Definition: vf_transpose_vulkan.c:147
TRANSPOSE_PT_TYPE_PORTRAIT
@ TRANSPOSE_PT_TYPE_PORTRAIT
Definition: transpose.h:27
ff_transpose_comp_spv_len
const unsigned int ff_transpose_comp_spv_len
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(transpose_vulkan)
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
TRANSPOSE_PT_TYPE_NONE
@ TRANSPOSE_PT_TYPE_NONE
Definition: transpose.h:25
FFVulkanShader
Definition: vulkan.h:225
planes
static const struct @585 planes[]
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
TRANSPOSE_CLOCK
@ TRANSPOSE_CLOCK
Definition: transpose.h:32
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:46
transpose_vulkan_inputs
static const AVFilterPad transpose_vulkan_inputs[]
Definition: vf_transpose_vulkan.c:201
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:1489
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:264
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:531
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:2503
TRANSPOSE_CCLOCK_FLIP
@ TRANSPOSE_CCLOCK_FLIP
Definition: transpose.h:31
TransposeVulkanContext::passthrough
int passthrough
Definition: vf_transpose_vulkan.c:41
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
init_filter
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
Definition: vf_transpose_vulkan.c:44
transpose.h
FLAGS
#define FLAGS
Definition: vf_transpose_vulkan.c:180
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
TRANSPOSE_PT_TYPE_LANDSCAPE
@ TRANSPOSE_PT_TYPE_LANDSCAPE
Definition: transpose.h:26
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
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVVulkanDeviceQueueFamily
Definition: hwcontext_vulkan.h:33
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
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:2093
TransposeVulkanContext::qf
AVVulkanDeviceQueueFamily * qf
Definition: vf_transpose_vulkan.c:37
ff_vf_transpose_vulkan
const FFFilter ff_vf_transpose_vulkan
Definition: vf_transpose_vulkan.c:218