FFmpeg
vf_scale_vulkan.c
Go to the documentation of this file.
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 "scale_eval.h"
22 #include "internal.h"
23 #include "colorspace.h"
24 
25 #define CGROUPS (int [3]){ 32, 32, 1 }
26 
27 enum ScalerFunc {
30 
32 };
33 
34 typedef struct ScaleVulkanContext {
36 
41 
42  /* Shader updators, must be in the main filter struct */
43  VkDescriptorImageInfo input_images[3];
44  VkDescriptorImageInfo output_images[3];
45  VkDescriptorBufferInfo params_desc;
46 
50  char *w_expr;
51  char *h_expr;
53 
54 static const char scale_bilinear[] = {
55  C(0, vec4 scale_bilinear(int idx, ivec2 pos, vec2 crop_range, vec2 crop_off))
56  C(0, { )
57  C(1, vec2 npos = (vec2(pos) + 0.5f) / imageSize(output_img[idx]); )
58  C(1, npos *= crop_range; /* Reduce the range */ )
59  C(1, npos += crop_off; /* Offset the start */ )
60  C(1, return texture(input_img[idx], npos); )
61  C(0, } )
62 };
63 
64 static const char rgb2yuv[] = {
65  C(0, vec4 rgb2yuv(vec4 src, int fullrange) )
66  C(0, { )
67  C(1, src *= yuv_matrix; )
68  C(1, if (fullrange == 1) { )
69  C(2, src += vec4(0.0, 0.5, 0.5, 0.0); )
70  C(1, } else { )
71  C(2, src *= vec4(219.0 / 255.0, 224.0 / 255.0, 224.0 / 255.0, 1.0); )
72  C(2, src += vec4(16.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 0.0); )
73  C(1, } )
74  C(1, return src; )
75  C(0, } )
76 };
77 
78 static const char write_nv12[] = {
79  C(0, void write_nv12(vec4 src, ivec2 pos) )
80  C(0, { )
81  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
82  C(1, pos /= ivec2(2); )
83  C(1, imageStore(output_img[1], pos, vec4(src.g, src.b, 0.0, 0.0)); )
84  C(0, } )
85 };
86 
87 static const char write_420[] = {
88  C(0, void write_420(vec4 src, ivec2 pos) )
89  C(0, { )
90  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
91  C(1, pos /= ivec2(2); )
92  C(1, imageStore(output_img[1], pos, vec4(src.g, 0.0, 0.0, 0.0)); )
93  C(1, imageStore(output_img[2], pos, vec4(src.b, 0.0, 0.0, 0.0)); )
94  C(0, } )
95 };
96 
97 static const char write_444[] = {
98  C(0, void write_444(vec4 src, ivec2 pos) )
99  C(0, { )
100  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
101  C(1, imageStore(output_img[1], pos, vec4(src.g, 0.0, 0.0, 0.0)); )
102  C(1, imageStore(output_img[2], pos, vec4(src.b, 0.0, 0.0, 0.0)); )
103  C(0, } )
104 };
105 
107 {
108  int err;
109  VkSampler *sampler;
110  VkFilter sampler_mode;
111  ScaleVulkanContext *s = ctx->priv;
112 
113  int crop_x = in->crop_left;
114  int crop_y = in->crop_top;
115  int crop_w = in->width - (in->crop_left + in->crop_right);
116  int crop_h = in->height - (in->crop_top + in->crop_bottom);
117 
119  s->vkctx.queue_count = GET_QUEUE_COUNT(s->vkctx.hwctx, 0, 1, 0);
120  s->vkctx.cur_queue_idx = rand() % s->vkctx.queue_count;
121 
122  switch (s->scaler) {
123  case F_NEAREST:
124  sampler_mode = VK_FILTER_NEAREST;
125  break;
126  case F_BILINEAR:
127  sampler_mode = VK_FILTER_LINEAR;
128  break;
129  };
130 
131  /* Create a sampler */
132  sampler = ff_vk_init_sampler(ctx, 0, sampler_mode);
133  if (!sampler)
134  return AVERROR_EXTERNAL;
135 
136  s->pl = ff_vk_create_pipeline(ctx);
137  if (!s->pl)
138  return AVERROR(ENOMEM);
139 
140  { /* Create the shader */
141  VulkanDescriptorSetBinding desc_i[2] = {
142  {
143  .name = "input_img",
144  .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
145  .dimensions = 2,
147  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
148  .updater = s->input_images,
149  .samplers = DUP_SAMPLER_ARRAY4(*sampler),
150  },
151  {
152  .name = "output_img",
153  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
154  .mem_layout = ff_vk_shader_rep_fmt(s->vkctx.output_format),
155  .mem_quali = "writeonly",
156  .dimensions = 2,
158  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
159  .updater = s->output_images,
160  },
161  };
162 
163  VulkanDescriptorSetBinding desc_b = {
164  .name = "params",
165  .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
166  .mem_quali = "readonly",
167  .mem_layout = "std430",
168  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
169  .updater = &s->params_desc,
170  .buf_content = "mat4 yuv_matrix;",
171  };
172 
173  SPIRVShader *shd = ff_vk_init_shader(ctx, s->pl, "scale_compute",
174  VK_SHADER_STAGE_COMPUTE_BIT);
175  if (!shd)
176  return AVERROR(ENOMEM);
177 
179 
180  RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, desc_i, 2, 0)); /* set 0 */
181  RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, &desc_b, 1, 0)); /* set 0 */
182 
184 
185  if (s->vkctx.output_format != s->vkctx.input_format) {
186  GLSLD( rgb2yuv );
187  }
188 
189  switch (s->vkctx.output_format) {
190  case AV_PIX_FMT_NV12: GLSLD(write_nv12); break;
191  case AV_PIX_FMT_YUV420P: GLSLD( write_420); break;
192  case AV_PIX_FMT_YUV444P: GLSLD( write_444); break;
193  default: break;
194  }
195 
196  GLSLC(0, void main() );
197  GLSLC(0, { );
198  GLSLC(1, ivec2 size; );
199  GLSLC(1, ivec2 pos = ivec2(gl_GlobalInvocationID.xy); );
200  GLSLF(1, vec2 in_d = vec2(%i, %i); ,in->width, in->height);
201  GLSLF(1, vec2 c_r = vec2(%i, %i) / in_d; ,crop_w, crop_h);
202  GLSLF(1, vec2 c_o = vec2(%i, %i) / in_d; ,crop_x,crop_y);
203  GLSLC(0, );
204 
205  if (s->vkctx.output_format == s->vkctx.input_format) {
206  for (int i = 0; i < desc_i[1].elems; i++) {
207  GLSLF(1, size = imageSize(output_img[%i]); ,i);
208  GLSLC(1, if (IS_WITHIN(pos, size)) { );
209  switch (s->scaler) {
210  case F_NEAREST:
211  case F_BILINEAR:
212  GLSLF(2, vec4 res = scale_bilinear(%i, pos, c_r, c_o); ,i);
213  GLSLF(2, imageStore(output_img[%i], pos, res); ,i);
214  break;
215  };
216  GLSLC(1, } );
217  }
218  } else {
219  GLSLC(1, vec4 res = scale_bilinear(0, pos, c_r, c_o); );
220  GLSLF(1, res = rgb2yuv(res, %i); ,s->out_range == AVCOL_RANGE_JPEG);
221  switch (s->vkctx.output_format) {
222  case AV_PIX_FMT_NV12: GLSLC(1, write_nv12(res, pos); ); break;
223  case AV_PIX_FMT_YUV420P: GLSLC(1, write_420(res, pos); ); break;
224  case AV_PIX_FMT_YUV444P: GLSLC(1, write_444(res, pos); ); break;
225  default: return AVERROR(EINVAL);
226  }
227  }
228 
229  GLSLC(0, } );
230 
231  RET(ff_vk_compile_shader(ctx, shd, "main"));
232  }
233 
236 
237  if (s->vkctx.output_format != s->vkctx.input_format) {
238  const struct LumaCoefficients *lcoeffs;
239  double tmp_mat[3][3];
240 
241  struct {
242  float yuv_matrix[4][4];
243  } *par;
244 
245  lcoeffs = ff_get_luma_coefficients(in->colorspace);
246  if (!lcoeffs) {
247  av_log(ctx, AV_LOG_ERROR, "Unsupported colorspace\n");
248  return AVERROR(EINVAL);
249  }
250 
251  err = ff_vk_create_buf(ctx, &s->params_buf,
252  sizeof(*par),
253  VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
254  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
255  if (err)
256  return err;
257 
258  err = ff_vk_map_buffers(ctx, &s->params_buf, (uint8_t **)&par, 1, 0);
259  if (err)
260  return err;
261 
262  ff_fill_rgb2yuv_table(lcoeffs, tmp_mat);
263 
264  memset(par, 0, sizeof(*par));
265 
266  for (int y = 0; y < 3; y++)
267  for (int x = 0; x < 3; x++)
268  par->yuv_matrix[x][y] = tmp_mat[x][y];
269 
270  par->yuv_matrix[3][3] = 1.0;
271 
272  err = ff_vk_unmap_buffers(ctx, &s->params_buf, 1, 1);
273  if (err)
274  return err;
275 
276  s->params_desc.buffer = s->params_buf.buf;
277  s->params_desc.range = VK_WHOLE_SIZE;
278 
279  ff_vk_update_descriptor_set(ctx, s->pl, 1);
280  }
281 
282  /* Execution context */
283  RET(ff_vk_create_exec_ctx(ctx, &s->exec));
284 
285  s->initialized = 1;
286 
287  return 0;
288 
289 fail:
290  return err;
291 }
292 
293 static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
294 {
295  int err = 0;
296  VkCommandBuffer cmd_buf;
297  ScaleVulkanContext *s = avctx->priv;
298  AVVkFrame *in = (AVVkFrame *)in_f->data[0];
299  AVVkFrame *out = (AVVkFrame *)out_f->data[0];
300  VkImageMemoryBarrier barriers[AV_NUM_DATA_POINTERS*2];
301  int barrier_count = 0;
302 
303  /* Update descriptors and init the exec context */
304  ff_vk_start_exec_recording(avctx, s->exec);
305  cmd_buf = ff_vk_get_exec_buf(avctx, s->exec);
306 
307  for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++) {
308  RET(ff_vk_create_imageview(avctx, s->exec, &s->input_images[i].imageView,
309  in->img[i],
312 
313  s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
314  }
315 
316  for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++) {
317  RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView,
318  out->img[i],
321 
322  s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
323  }
324 
325  ff_vk_update_descriptor_set(avctx, s->pl, 0);
326 
327  for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++) {
328  VkImageMemoryBarrier bar = {
329  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
330  .srcAccessMask = 0,
331  .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
332  .oldLayout = in->layout[i],
333  .newLayout = s->input_images[i].imageLayout,
334  .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
335  .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
336  .image = in->img[i],
337  .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
338  .subresourceRange.levelCount = 1,
339  .subresourceRange.layerCount = 1,
340  };
341 
342  memcpy(&barriers[barrier_count++], &bar, sizeof(VkImageMemoryBarrier));
343 
344  in->layout[i] = bar.newLayout;
345  in->access[i] = bar.dstAccessMask;
346  }
347 
348  for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++) {
349  VkImageMemoryBarrier bar = {
350  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
351  .srcAccessMask = 0,
352  .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
353  .oldLayout = out->layout[i],
354  .newLayout = s->output_images[i].imageLayout,
355  .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
356  .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
357  .image = out->img[i],
358  .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
359  .subresourceRange.levelCount = 1,
360  .subresourceRange.layerCount = 1,
361  };
362 
363  memcpy(&barriers[barrier_count++], &bar, sizeof(VkImageMemoryBarrier));
364 
365  out->layout[i] = bar.newLayout;
366  out->access[i] = bar.dstAccessMask;
367  }
368 
369  vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
370  VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
371  0, NULL, 0, NULL, barrier_count, barriers);
372 
373  ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl);
374 
375  vkCmdDispatch(cmd_buf,
377  FFALIGN(s->vkctx.output_height, CGROUPS[1])/CGROUPS[1], 1);
378 
379  ff_vk_add_exec_dep(avctx, s->exec, in_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
380  ff_vk_add_exec_dep(avctx, s->exec, out_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
381 
382  err = ff_vk_submit_exec_queue(avctx, s->exec);
383  if (err)
384  return err;
385 
386  return err;
387 
388 fail:
389  ff_vk_discard_exec_deps(avctx, s->exec);
390  return err;
391 }
392 
394 {
395  int err;
396  AVFilterContext *ctx = link->dst;
397  ScaleVulkanContext *s = ctx->priv;
398  AVFilterLink *outlink = ctx->outputs[0];
399 
400  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
401  if (!out) {
402  err = AVERROR(ENOMEM);
403  goto fail;
404  }
405 
406  if (!s->initialized)
407  RET(init_filter(ctx, in));
408 
409  RET(process_frames(ctx, out, in));
410 
411  err = av_frame_copy_props(out, in);
412  if (err < 0)
413  goto fail;
414 
416  out->color_range = s->out_range;
417  if (s->vkctx.output_format != s->vkctx.input_format)
419 
420  av_frame_free(&in);
421 
422  return ff_filter_frame(outlink, out);
423 
424 fail:
425  av_frame_free(&in);
426  av_frame_free(&out);
427  return err;
428 }
429 
431 {
432  int err;
433  AVFilterContext *avctx = outlink->src;
434  ScaleVulkanContext *s = avctx->priv;
435  AVFilterLink *inlink = outlink->src->inputs[0];
436 
437  err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
438  &s->vkctx.output_width,
439  &s->vkctx.output_height);
440  if (err < 0)
441  return err;
442 
443  if (s->out_format_string) {
445  if (s->vkctx.output_format == AV_PIX_FMT_NONE) {
446  av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
447  return AVERROR(EINVAL);
448  }
449  } else {
451  }
452 
453  if (s->vkctx.output_format != s->vkctx.input_format) {
455  av_log(avctx, AV_LOG_ERROR, "Unsupported input format for conversion\n");
456  return AVERROR(EINVAL);
457  }
458  if (s->vkctx.output_format != AV_PIX_FMT_NV12 &&
461  av_log(avctx, AV_LOG_ERROR, "Unsupported output format\n");
462  return AVERROR(EINVAL);
463  }
464  } else if (s->out_range != AVCOL_RANGE_UNSPECIFIED) {
465  av_log(avctx, AV_LOG_ERROR, "Cannot change range without converting format\n");
466  return AVERROR(EINVAL);
467  }
468 
469  err = ff_vk_filter_config_output(outlink);
470  if (err < 0)
471  return err;
472 
473  return 0;
474 }
475 
477 {
478  ScaleVulkanContext *s = avctx->priv;
479 
480  ff_vk_filter_uninit(avctx);
481  ff_vk_free_buf(avctx, &s->params_buf);
482 
483  s->initialized = 0;
484 }
485 
486 #define OFFSET(x) offsetof(ScaleVulkanContext, x)
487 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
488 static const AVOption scale_vulkan_options[] = {
489  { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
490  { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
491  { "scaler", "Scaler function", OFFSET(scaler), AV_OPT_TYPE_INT, {.i64 = F_BILINEAR}, 0, F_NB, .flags = FLAGS, "scaler" },
492  { "bilinear", "Bilinear interpolation (fastest)", 0, AV_OPT_TYPE_CONST, {.i64 = F_BILINEAR}, 0, 0, .flags = FLAGS, "scaler" },
493  { "nearest", "Nearest (useful for pixel art)", 0, AV_OPT_TYPE_CONST, {.i64 = F_NEAREST}, 0, 0, .flags = FLAGS, "scaler" },
494  { "format", "Output video format (software format of hardware frames)", OFFSET(out_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
495  { "out_range", "Output colour range (from 0 to 2) (default 0)", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED}, AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_JPEG, .flags = FLAGS, "range" },
496  { "full", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
497  { "limited", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
498  { "jpeg", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
499  { "mpeg", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
500  { "tv", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
501  { "pc", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
502  { NULL },
503 };
504 
505 AVFILTER_DEFINE_CLASS(scale_vulkan);
506 
508  {
509  .name = "default",
510  .type = AVMEDIA_TYPE_VIDEO,
511  .filter_frame = &scale_vulkan_filter_frame,
512  .config_props = &ff_vk_filter_config_input,
513  },
514  { NULL }
515 };
516 
518  {
519  .name = "default",
520  .type = AVMEDIA_TYPE_VIDEO,
521  .config_props = &scale_vulkan_config_output,
522  },
523  { NULL }
524 };
525 
527  .name = "scale_vulkan",
528  .description = NULL_IF_CONFIG_SMALL("Scale Vulkan frames"),
529  .priv_size = sizeof(ScaleVulkanContext),
533  .inputs = scale_vulkan_inputs,
534  .outputs = scale_vulkan_outputs,
535  .priv_class = &scale_vulkan_class,
536  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
537 };
VulkanFilterContext vkctx
#define NULL
Definition: coverity.c:32
int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e, AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
Adds a frame as a queue dependency.
Definition: vulkan.c:439
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:365
VkBuffer buf
Definition: vulkan.h:87
int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl)
Initializes the pipeline layout after all shaders and descriptor sets have been finished.
Definition: vulkan.c:1155
#define AV_NUM_DATA_POINTERS
Definition: frame.h:301
static const AVFilterPad scale_vulkan_outputs[]
int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl, SPIRVShader *shd, VulkanDescriptorSetBinding *desc, int num, int only_print_to_shader)
Adds a descriptor set to the shader and registers them in the pipeline.
Definition: vulkan.c:995
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
void ff_vk_filter_uninit(AVFilterContext *avctx)
Definition: vulkan.c:1392
AVOption.
Definition: opt.h:246
const char * ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)
Gets the glsl format string for a pixel format.
Definition: vulkan.c:792
VkDescriptorImageInfo output_images[3]
const char * name
Definition: vulkan.h:74
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2589
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl)
Initializes a compute pipeline.
Definition: vulkan.c:1258
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
int ff_vk_create_imageview(AVFilterContext *avctx, FFVkExecContext *e, VkImageView *v, VkImage img, VkFormat fmt, const VkComponentMapping map)
Create an imageview.
Definition: vulkan.c:811
int ff_vk_filter_config_output(AVFilterLink *outlink)
Definition: vulkan.c:680
ScalerFunc
int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e)
Begin recording to the command buffer.
Definition: vulkan.c:392
static const char write_444[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
size_t crop_bottom
Definition: frame.h:661
FFVkExecContext * exec
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the format of each image up to the number of planes for a given sw_format.
VkDescriptorBufferInfo params_desc
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
static const char rgb2yuv[]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
uint8_t
VkDescriptorImageInfo input_images[3]
#define av_cold
Definition: attributes.h:82
static const AVOption scale_vulkan_options[]
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
AVOptions.
size_t crop_left
Definition: frame.h:662
#define f(width, name)
Definition: cbs_vp9.c:255
void ff_vk_set_compute_shader_sizes(AVFilterContext *avctx, SPIRVShader *shd, int local_size[3])
Writes the workgroup size for a shader.
Definition: vulkan.c:884
enum AVColorRange out_range
static const char write_420[]
int queue_family_comp_index
Queue family index for compute ops, and the amount of queues enabled.
int ff_vk_filter_init(AVFilterContext *avctx)
Definition: vulkan.c:731
AVFilter ff_vf_scale_vulkan
static const char scale_bilinear[]
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:532
ptrdiff_t size
Definition: opengl_enc.c:100
const struct LumaCoefficients * ff_get_luma_coefficients(enum AVColorSpace csp)
Definition: colorspace.c:128
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int ff_vk_unmap_buffers(AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers, int flush)
Unmaps the buffer from userspace.
Definition: vulkan.c:239
#define src
Definition: vp8dsp.c:254
int ff_vk_filter_query_formats(AVFilterContext *avctx)
General lavfi IO functions.
Definition: vulkan.c:567
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
enum AVPixelFormat input_format
Definition: vulkan.h:176
int width
Definition: frame.h:358
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
Definition: scale_eval.c:57
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:544
unsigned int pos
Definition: spdifenc.c:410
int ff_vk_create_buf(AVFilterContext *avctx, FFVkBuffer *buf, size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Create a VkBuffer with the specified parameters.
Definition: vulkan.c:150
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:555
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
#define fail()
Definition: checkasm.h:123
size_t crop_top
Definition: frame.h:660
VkSampler * ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords, VkFilter filt)
Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
Definition: vulkan.c:744
int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx)
Init an execution context for command recording and queue submission.
Definition: vulkan.c:314
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:558
int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e)
Submits a command buffer to the queue for execution.
Definition: vulkan.c:497
AVFormatContext * ctx
Definition: movenc.c:48
enum ScalerFunc scaler
#define GLSLF(N, S,...)
Definition: vulkan.h:40
#define s(width, name)
Definition: cbs_vp9.c:257
void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf)
Frees a buffer.
Definition: vulkan.c:281
int main(int argc, char *argv[])
#define GLSLC(N, S)
Definition: vulkan.h:38
#define FLAGS
void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e, VulkanPipeline *pl)
Add a command to bind the completed pipeline and its descriptor sets.
Definition: vulkan.c:1293
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:535
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
#define CGROUPS
const VkComponentMapping ff_comp_identity_map
Definition: vulkan.c:44
static const AVFilterPad scale_vulkan_inputs[]
void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs, double rgb2yuv[3][3])
Definition: colorspace.c:141
int ff_vk_compile_shader(AVFilterContext *avctx, SPIRVShader *shd, const char *entrypoint)
Compiles the shader, entrypoint must be set to "main".
Definition: vulkan.c:917
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if the image is any sort of supported RGB.
Definition: vulkan.c:780
SPIRVShader * ff_vk_init_shader(AVFilterContext *avctx, VulkanPipeline *pl, const char *name, VkShaderStageFlags stage)
Inits a shader for a specific pipeline.
Definition: vulkan.c:863
size_t crop_right
Definition: frame.h:663
AVVulkanDeviceContext * hwctx
Definition: vulkan.h:165
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Filter definition.
Definition: avfilter.h:144
VulkanPipeline * ff_vk_create_pipeline(AVFilterContext *avctx)
Inits a pipeline.
Definition: vulkan.c:1253
const char * name
Filter name.
Definition: avfilter.h:148
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 link
enum AVChromaLocation chroma_location
Definition: frame.h:557
VkCommandBuffer ff_vk_get_exec_buf(AVFilterContext *avctx, FFVkExecContext *e)
Gets the command buffer to use for this submission from the exe context.
Definition: vulkan.c:433
#define GET_QUEUE_COUNT(hwctx, graph, comp, tx)
Definition: vulkan.h:53
static const char write_nv12[]
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
void ff_vk_discard_exec_deps(AVFilterContext *avctx, FFVkExecContext *e)
Discards all queue dependencies.
Definition: vulkan.c:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
VulkanPipeline * pl
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
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:534
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
static int scale_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
static void scale_vulkan_uninit(AVFilterContext *avctx)
enum AVPixelFormat output_format
Definition: vulkan.h:175
static int scale_vulkan_config_output(AVFilterLink *outlink)
int ff_vk_map_buffers(AVFilterContext *avctx, FFVkBuffer *buf, uint8_t *mem[], int nb_buffers, int invalidate)
Maps the buffer to userspace.
Definition: vulkan.c:190
AVFILTER_DEFINE_CLASS(scale_vulkan)
#define GLSLD(D)
Definition: vulkan.h:41
int ff_vk_filter_config_input(AVFilterLink *inlink)
Definition: vulkan.c:610
#define OFFSET(x)
An instance of a filter.
Definition: avfilter.h:338
VkImageLayout layout[AV_NUM_DATA_POINTERS]
#define RET(x)
Definition: vulkan.h:46
int height
Definition: frame.h:358
FILE * out
Definition: movenc.c:54
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2477
#define DUP_SAMPLER_ARRAY4(x)
Definition: vulkan.h:64
internal API functions
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
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:659
void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl, int set_id)
Updates a descriptor set via the updaters defined.
Definition: vulkan.c:1135