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/random_seed.h"
20 #include "libavutil/opt.h"
21 #include "vulkan_filter.h"
22 #include "scale_eval.h"
23 #include "internal.h"
24 #include "colorspace.h"
25 
26 #define CGROUPS (int [3]){ 32, 32, 1 }
27 
28 enum ScalerFunc {
31 
33 };
34 
35 typedef struct ScaleVulkanContext {
37 
42 
43  /* Shader updators, must be in the main filter struct */
44  VkDescriptorImageInfo input_images[3];
45  VkDescriptorImageInfo output_images[3];
46  VkDescriptorBufferInfo params_desc;
47 
49  char *w_expr;
50  char *h_expr;
51 
54 
57 
58 static const char scale_bilinear[] = {
59  C(0, vec4 scale_bilinear(int idx, ivec2 pos, vec2 crop_range, vec2 crop_off))
60  C(0, { )
61  C(1, vec2 npos = (vec2(pos) + 0.5f) / imageSize(output_img[idx]); )
62  C(1, npos *= crop_range; /* Reduce the range */ )
63  C(1, npos += crop_off; /* Offset the start */ )
64  C(1, return texture(input_img[idx], npos); )
65  C(0, } )
66 };
67 
68 static const char rgb2yuv[] = {
69  C(0, vec4 rgb2yuv(vec4 src, int fullrange) )
70  C(0, { )
71  C(1, src *= yuv_matrix; )
72  C(1, if (fullrange == 1) { )
73  C(2, src += vec4(0.0, 0.5, 0.5, 0.0); )
74  C(1, } else { )
75  C(2, src *= vec4(219.0 / 255.0, 224.0 / 255.0, 224.0 / 255.0, 1.0); )
76  C(2, src += vec4(16.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 0.0); )
77  C(1, } )
78  C(1, return src; )
79  C(0, } )
80 };
81 
82 static const char write_nv12[] = {
83  C(0, void write_nv12(vec4 src, ivec2 pos) )
84  C(0, { )
85  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
86  C(1, pos /= ivec2(2); )
87  C(1, imageStore(output_img[1], pos, vec4(src.g, src.b, 0.0, 0.0)); )
88  C(0, } )
89 };
90 
91 static const char write_420[] = {
92  C(0, void write_420(vec4 src, ivec2 pos) )
93  C(0, { )
94  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
95  C(1, pos /= ivec2(2); )
96  C(1, imageStore(output_img[1], pos, vec4(src.g, 0.0, 0.0, 0.0)); )
97  C(1, imageStore(output_img[2], pos, vec4(src.b, 0.0, 0.0, 0.0)); )
98  C(0, } )
99 };
100 
101 static const char write_444[] = {
102  C(0, void write_444(vec4 src, ivec2 pos) )
103  C(0, { )
104  C(1, imageStore(output_img[0], pos, vec4(src.r, 0.0, 0.0, 0.0)); )
105  C(1, imageStore(output_img[1], pos, vec4(src.g, 0.0, 0.0, 0.0)); )
106  C(1, imageStore(output_img[2], pos, vec4(src.b, 0.0, 0.0, 0.0)); )
107  C(0, } )
108 };
109 
111 {
112  int err;
113  FFVkSampler *sampler;
114  VkFilter sampler_mode;
115  ScaleVulkanContext *s = ctx->priv;
116  FFVulkanContext *vkctx = &s->vkctx;
117 
118  int crop_x = in->crop_left;
119  int crop_y = in->crop_top;
120  int crop_w = in->width - (in->crop_left + in->crop_right);
121  int crop_h = in->height - (in->crop_top + in->crop_bottom);
122  int in_planes = av_pix_fmt_count_planes(s->vkctx.input_format);
123 
124  ff_vk_qf_init(vkctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
125 
126  switch (s->scaler) {
127  case F_NEAREST:
128  sampler_mode = VK_FILTER_NEAREST;
129  break;
130  case F_BILINEAR:
131  sampler_mode = VK_FILTER_LINEAR;
132  break;
133  };
134 
135  /* Create a sampler */
136  sampler = ff_vk_init_sampler(vkctx, 0, sampler_mode);
137  if (!sampler)
138  return AVERROR_EXTERNAL;
139 
140  s->pl = ff_vk_create_pipeline(vkctx, &s->qf);
141  if (!s->pl)
142  return AVERROR(ENOMEM);
143 
144  { /* Create the shader */
145  FFVulkanDescriptorSetBinding desc_i[2] = {
146  {
147  .name = "input_img",
148  .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
149  .dimensions = 2,
150  .elems = in_planes,
151  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
152  .updater = s->input_images,
153  .sampler = sampler,
154  },
155  {
156  .name = "output_img",
157  .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
158  .mem_layout = ff_vk_shader_rep_fmt(s->vkctx.output_format),
159  .mem_quali = "writeonly",
160  .dimensions = 2,
161  .elems = av_pix_fmt_count_planes(s->vkctx.output_format),
162  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
163  .updater = s->output_images,
164  },
165  };
166 
168  .name = "params",
169  .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
170  .mem_quali = "readonly",
171  .mem_layout = "std430",
172  .stages = VK_SHADER_STAGE_COMPUTE_BIT,
173  .updater = &s->params_desc,
174  .buf_content = "mat4 yuv_matrix;",
175  };
176 
177  FFVkSPIRVShader *shd = ff_vk_init_shader(s->pl, "scale_compute",
178  VK_SHADER_STAGE_COMPUTE_BIT);
179  if (!shd)
180  return AVERROR(ENOMEM);
181 
183 
184  RET(ff_vk_add_descriptor_set(vkctx, s->pl, shd, desc_i, FF_ARRAY_ELEMS(desc_i), 0)); /* set 0 */
185  RET(ff_vk_add_descriptor_set(vkctx, s->pl, shd, &desc_b, 1, 0)); /* set 1 */
186 
188 
189  if (s->vkctx.output_format != s->vkctx.input_format) {
190  GLSLD( rgb2yuv );
191  }
192 
193  switch (s->vkctx.output_format) {
194  case AV_PIX_FMT_NV12: GLSLD(write_nv12); break;
195  case AV_PIX_FMT_YUV420P: GLSLD( write_420); break;
196  case AV_PIX_FMT_YUV444P: GLSLD( write_444); break;
197  default: break;
198  }
199 
200  GLSLC(0, void main() );
201  GLSLC(0, { );
202  GLSLC(1, ivec2 size; );
203  GLSLC(1, ivec2 pos = ivec2(gl_GlobalInvocationID.xy); );
204  GLSLF(1, vec2 in_d = vec2(%i, %i); ,in->width, in->height);
205  GLSLF(1, vec2 c_r = vec2(%i, %i) / in_d; ,crop_w, crop_h);
206  GLSLF(1, vec2 c_o = vec2(%i, %i) / in_d; ,crop_x,crop_y);
207  GLSLC(0, );
208 
209  if (s->vkctx.output_format == s->vkctx.input_format) {
210  for (int i = 0; i < desc_i[1].elems; i++) {
211  GLSLF(1, size = imageSize(output_img[%i]); ,i);
212  GLSLC(1, if (IS_WITHIN(pos, size)) { );
213  switch (s->scaler) {
214  case F_NEAREST:
215  case F_BILINEAR:
216  GLSLF(2, vec4 res = scale_bilinear(%i, pos, c_r, c_o); ,i);
217  GLSLF(2, imageStore(output_img[%i], pos, res); ,i);
218  break;
219  };
220  GLSLC(1, } );
221  }
222  } else {
223  GLSLC(1, vec4 res = scale_bilinear(0, pos, c_r, c_o); );
224  GLSLF(1, res = rgb2yuv(res, %i); ,s->out_range == AVCOL_RANGE_JPEG);
225  switch (s->vkctx.output_format) {
226  case AV_PIX_FMT_NV12: GLSLC(1, write_nv12(res, pos); ); break;
227  case AV_PIX_FMT_YUV420P: GLSLC(1, write_420(res, pos); ); break;
228  case AV_PIX_FMT_YUV444P: GLSLC(1, write_444(res, pos); ); break;
229  default: return AVERROR(EINVAL);
230  }
231  }
232 
233  GLSLC(0, } );
234 
235  RET(ff_vk_compile_shader(vkctx, shd, "main"));
236  }
237 
238  RET(ff_vk_init_pipeline_layout(vkctx, s->pl));
239  RET(ff_vk_init_compute_pipeline(vkctx, s->pl));
240 
241  if (s->vkctx.output_format != s->vkctx.input_format) {
242  const struct LumaCoefficients *lcoeffs;
243  double tmp_mat[3][3];
244 
245  struct {
246  float yuv_matrix[4][4];
247  } *par;
248 
249  lcoeffs = ff_get_luma_coefficients(in->colorspace);
250  if (!lcoeffs) {
251  av_log(ctx, AV_LOG_ERROR, "Unsupported colorspace\n");
252  return AVERROR(EINVAL);
253  }
254 
255  err = ff_vk_create_buf(vkctx, &s->params_buf,
256  sizeof(*par),
257  VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
258  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
259  if (err)
260  return err;
261 
262  err = ff_vk_map_buffers(vkctx, &s->params_buf, (uint8_t **)&par, 1, 0);
263  if (err)
264  return err;
265 
266  ff_fill_rgb2yuv_table(lcoeffs, tmp_mat);
267 
268  memset(par, 0, sizeof(*par));
269 
270  for (int y = 0; y < 3; y++)
271  for (int x = 0; x < 3; x++)
272  par->yuv_matrix[x][y] = tmp_mat[x][y];
273 
274  par->yuv_matrix[3][3] = 1.0;
275 
276  err = ff_vk_unmap_buffers(vkctx, &s->params_buf, 1, 1);
277  if (err)
278  return err;
279 
280  s->params_desc.buffer = s->params_buf.buf;
281  s->params_desc.range = VK_WHOLE_SIZE;
282 
283  ff_vk_update_descriptor_set(vkctx, s->pl, 1);
284  }
285 
286  /* Execution context */
287  RET(ff_vk_create_exec_ctx(vkctx, &s->exec, &s->qf));
288 
289  s->initialized = 1;
290 
291  return 0;
292 
293 fail:
294  return err;
295 }
296 
297 static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
298 {
299  int err = 0;
300  VkCommandBuffer cmd_buf;
301  ScaleVulkanContext *s = avctx->priv;
302  FFVulkanContext *vkctx = &s->vkctx;
303  FFVulkanFunctions *vk = &vkctx->vkfn;
304  AVVkFrame *in = (AVVkFrame *)in_f->data[0];
305  AVVkFrame *out = (AVVkFrame *)out_f->data[0];
306  VkImageMemoryBarrier barriers[AV_NUM_DATA_POINTERS*2];
307  int barrier_count = 0;
308  const int planes = av_pix_fmt_count_planes(s->vkctx.input_format);
309  const VkFormat *input_formats = av_vkfmt_from_pixfmt(s->vkctx.input_format);
310  const VkFormat *output_formats = av_vkfmt_from_pixfmt(s->vkctx.output_format);
311 
312  /* Update descriptors and init the exec context */
313  ff_vk_start_exec_recording(vkctx, s->exec);
314  cmd_buf = ff_vk_get_exec_buf(s->exec);
315 
316  for (int i = 0; i < planes; i++) {
317  RET(ff_vk_create_imageview(vkctx, s->exec,
318  &s->input_images[i].imageView, in->img[i],
319  input_formats[i],
321 
322  RET(ff_vk_create_imageview(vkctx, s->exec,
323  &s->output_images[i].imageView, out->img[i],
324  output_formats[i],
326 
327  s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
328  s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
329  }
330 
331  ff_vk_update_descriptor_set(vkctx, s->pl, 0);
332 
333  for (int i = 0; i < planes; i++) {
334  VkImageMemoryBarrier bar = {
335  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
336  .srcAccessMask = 0,
337  .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
338  .oldLayout = in->layout[i],
339  .newLayout = s->input_images[i].imageLayout,
340  .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
341  .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
342  .image = in->img[i],
343  .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
344  .subresourceRange.levelCount = 1,
345  .subresourceRange.layerCount = 1,
346  };
347 
348  memcpy(&barriers[barrier_count++], &bar, sizeof(VkImageMemoryBarrier));
349 
350  in->layout[i] = bar.newLayout;
351  in->access[i] = bar.dstAccessMask;
352  }
353 
354  for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++) {
355  VkImageMemoryBarrier bar = {
356  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
357  .srcAccessMask = 0,
358  .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
359  .oldLayout = out->layout[i],
360  .newLayout = s->output_images[i].imageLayout,
361  .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
362  .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
363  .image = out->img[i],
364  .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
365  .subresourceRange.levelCount = 1,
366  .subresourceRange.layerCount = 1,
367  };
368 
369  memcpy(&barriers[barrier_count++], &bar, sizeof(VkImageMemoryBarrier));
370 
371  out->layout[i] = bar.newLayout;
372  out->access[i] = bar.dstAccessMask;
373  }
374 
375  vk->CmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
376  VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
377  0, NULL, 0, NULL, barrier_count, barriers);
378 
379  ff_vk_bind_pipeline_exec(vkctx, s->exec, s->pl);
380 
381  vk->CmdDispatch(cmd_buf,
382  FFALIGN(vkctx->output_width, CGROUPS[0])/CGROUPS[0],
383  FFALIGN(vkctx->output_height, CGROUPS[1])/CGROUPS[1], 1);
384 
385  ff_vk_add_exec_dep(vkctx, s->exec, in_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
386  ff_vk_add_exec_dep(vkctx, s->exec, out_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
387 
388  err = ff_vk_submit_exec_queue(vkctx, s->exec);
389  if (err)
390  return err;
391 
392  ff_vk_qf_rotate(&s->qf);
393 
394  return err;
395 
396 fail:
397  ff_vk_discard_exec_deps(s->exec);
398  return err;
399 }
400 
402 {
403  int err;
404  AVFilterContext *ctx = link->dst;
405  ScaleVulkanContext *s = ctx->priv;
406  AVFilterLink *outlink = ctx->outputs[0];
407 
408  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
409  if (!out) {
410  err = AVERROR(ENOMEM);
411  goto fail;
412  }
413 
414  if (!s->initialized)
415  RET(init_filter(ctx, in));
416 
417  RET(process_frames(ctx, out, in));
418 
419  err = av_frame_copy_props(out, in);
420  if (err < 0)
421  goto fail;
422 
423  if (s->out_range != AVCOL_RANGE_UNSPECIFIED)
424  out->color_range = s->out_range;
425  if (s->vkctx.output_format != s->vkctx.input_format)
426  out->chroma_location = AVCHROMA_LOC_TOPLEFT;
427 
428  av_frame_free(&in);
429 
430  return ff_filter_frame(outlink, out);
431 
432 fail:
433  av_frame_free(&in);
434  av_frame_free(&out);
435  return err;
436 }
437 
439 {
440  int err;
441  AVFilterContext *avctx = outlink->src;
442  ScaleVulkanContext *s = avctx->priv;
443  FFVulkanContext *vkctx = &s->vkctx;
444  AVFilterLink *inlink = outlink->src->inputs[0];
445 
446  err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
447  &vkctx->output_width,
448  &vkctx->output_height);
449  if (err < 0)
450  return err;
451 
452  if (s->out_format_string) {
453  s->vkctx.output_format = av_get_pix_fmt(s->out_format_string);
454  if (s->vkctx.output_format == AV_PIX_FMT_NONE) {
455  av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
456  return AVERROR(EINVAL);
457  }
458  } else {
459  s->vkctx.output_format = s->vkctx.input_format;
460  }
461 
462  if (s->vkctx.output_format != s->vkctx.input_format) {
463  if (!ff_vk_mt_is_np_rgb(s->vkctx.input_format)) {
464  av_log(avctx, AV_LOG_ERROR, "Unsupported input format for conversion\n");
465  return AVERROR(EINVAL);
466  }
467  if (s->vkctx.output_format != AV_PIX_FMT_NV12 &&
468  s->vkctx.output_format != AV_PIX_FMT_YUV420P &&
469  s->vkctx.output_format != AV_PIX_FMT_YUV444P) {
470  av_log(avctx, AV_LOG_ERROR, "Unsupported output format\n");
471  return AVERROR(EINVAL);
472  }
473  } else if (s->out_range != AVCOL_RANGE_UNSPECIFIED) {
474  av_log(avctx, AV_LOG_ERROR, "Cannot change range without converting format\n");
475  return AVERROR(EINVAL);
476  }
477 
478  err = ff_vk_filter_config_output(outlink);
479  if (err < 0)
480  return err;
481 
482  return 0;
483 }
484 
486 {
487  ScaleVulkanContext *s = avctx->priv;
488 
489  ff_vk_free_buf(&s->vkctx, &s->params_buf);
490  ff_vk_uninit(&s->vkctx);
491 
492  s->initialized = 0;
493 }
494 
495 #define OFFSET(x) offsetof(ScaleVulkanContext, x)
496 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
497 static const AVOption scale_vulkan_options[] = {
498  { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
499  { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
500  { "scaler", "Scaler function", OFFSET(scaler), AV_OPT_TYPE_INT, {.i64 = F_BILINEAR}, 0, F_NB, .flags = FLAGS, "scaler" },
501  { "bilinear", "Bilinear interpolation (fastest)", 0, AV_OPT_TYPE_CONST, {.i64 = F_BILINEAR}, 0, 0, .flags = FLAGS, "scaler" },
502  { "nearest", "Nearest (useful for pixel art)", 0, AV_OPT_TYPE_CONST, {.i64 = F_NEAREST}, 0, 0, .flags = FLAGS, "scaler" },
503  { "format", "Output video format (software format of hardware frames)", OFFSET(out_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
504  { "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" },
505  { "full", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
506  { "limited", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
507  { "jpeg", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
508  { "mpeg", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
509  { "tv", "Limited range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
510  { "pc", "Full range", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
511  { NULL },
512 };
513 
514 AVFILTER_DEFINE_CLASS(scale_vulkan);
515 
517  {
518  .name = "default",
519  .type = AVMEDIA_TYPE_VIDEO,
520  .filter_frame = &scale_vulkan_filter_frame,
521  .config_props = &ff_vk_filter_config_input,
522  },
523 };
524 
526  {
527  .name = "default",
528  .type = AVMEDIA_TYPE_VIDEO,
529  .config_props = &scale_vulkan_config_output,
530  },
531 };
532 
534  .name = "scale_vulkan",
535  .description = NULL_IF_CONFIG_SMALL("Scale Vulkan frames"),
536  .priv_size = sizeof(ScaleVulkanContext),
542  .priv_class = &scale_vulkan_class,
543  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
544 };
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:98
process_frames
static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
Definition: vf_scale_vulkan.c:297
FFVulkanContext::output_height
int output_height
Definition: vulkan.h:208
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_comp_identity_map
const VkComponentMapping ff_comp_identity_map
Definition: vulkan.c:51
out
FILE * out
Definition: movenc.c:54
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: internal.h:371
ff_vk_create_buf
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Create a VkBuffer with the specified parameters.
Definition: vulkan.c:193
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
ff_vk_bind_pipeline_exec
void ff_vk_bind_pipeline_exec(FFVulkanContext *s, FFVkExecContext *e, FFVulkanPipeline *pl)
Add a command to bind the completed pipeline and its descriptor sets.
Definition: vulkan.c:1263
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:577
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
OFFSET
#define OFFSET(x)
Definition: vf_scale_vulkan.c:495
AVFrame::width
int width
Definition: frame.h:389
ff_vk_filter_init
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
Definition: vulkan_filter.c:184
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:597
scale_vulkan_outputs
static const AVFilterPad scale_vulkan_outputs[]
Definition: vf_scale_vulkan.c:525
AVOption
AVOption.
Definition: opt.h:247
scale_vulkan_options
static const AVOption scale_vulkan_options[]
Definition: vf_scale_vulkan.c:497
LumaCoefficients
Definition: colorspace.h:28
ff_vk_compile_shader
int ff_vk_compile_shader(FFVulkanContext *s, FFVkSPIRVShader *shd, const char *entrypoint)
Compiles the shader, entrypoint must be set to "main".
Definition: vulkan.c:848
rgb2yuv
static const char rgb2yuv[]
Definition: vf_scale_vulkan.c:68
write_nv12
static const char write_nv12[]
Definition: vf_scale_vulkan.c:82
ff_scale_eval_dimensions
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
ff_vk_uninit
void ff_vk_uninit(FFVulkanContext *s)
Frees the main Vulkan context.
Definition: vulkan.c:1378
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
write_420
static const char write_420[]
Definition: vf_scale_vulkan.c:91
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:346
FFVkSampler
Definition: vulkan.h:74
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(scale_vulkan)
ScalerFunc
ScalerFunc
Definition: vf_scale_vulkan.c:28
ff_vk_add_exec_dep
int ff_vk_add_exec_dep(FFVulkanContext *s, FFVkExecContext *e, AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
Adds a frame as a queue dependency.
Definition: vulkan.c:509
init
static int init
Definition: av_tx.c:47
ff_vk_get_exec_buf
VkCommandBuffer ff_vk_get_exec_buf(FFVkExecContext *e)
Gets the command buffer to use for this submission from the exe context.
Definition: vulkan.c:504
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2700
AVVkFrame::img
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Definition: hwcontext_vulkan.h:217
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:417
fail
#define fail()
Definition: checkasm.h:127
vulkan_filter.h
colorspace.h
ScaleVulkanContext::exec
FFVkExecContext * exec
Definition: vf_scale_vulkan.c:39
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
ff_get_luma_coefficients
const struct LumaCoefficients * ff_get_luma_coefficients(enum AVColorSpace csp)
Definition: colorspace.c:128
C
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
Definition: writing_filters.txt:58
ff_vk_qf_init
void ff_vk_qf_init(FFVulkanContext *s, FFVkQueueFamilyCtx *qf, VkQueueFlagBits dev_family, int nb_queues)
Initialize a queue family with a specific number of queues.
Definition: vulkan.c:96
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
ff_vk_create_imageview
int ff_vk_create_imageview(FFVulkanContext *s, FFVkExecContext *e, VkImageView *v, VkImage img, VkFormat fmt, const VkComponentMapping map)
Create an imageview.
Definition: vulkan.c:742
FFVulkanDescriptorSetBinding::elems
uint32_t elems
Definition: vulkan.h:85
FFVulkanContext::output_width
int output_width
Definition: vulkan.h:207
F_NEAREST
@ F_NEAREST
Definition: vf_scale_vulkan.c:30
s
#define s(width, name)
Definition: cbs_vp9.c:257
F_BILINEAR
@ F_BILINEAR
Definition: vf_scale_vulkan.c:29
ScaleVulkanContext::pl
FFVulkanPipeline * pl
Definition: vf_scale_vulkan.c:40
ff_fill_rgb2yuv_table
void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs, double rgb2yuv[3][3])
Definition: colorspace.c:141
scale_vulkan_filter_frame
static int scale_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_scale_vulkan.c:401
ff_vk_init_shader
FFVkSPIRVShader * ff_vk_init_shader(FFVulkanPipeline *pl, const char *name, VkShaderStageFlags stage)
Inits a shader for a specific pipeline.
Definition: vulkan.c:795
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFrame::crop_right
size_t crop_right
Definition: frame.h:667
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
f
#define f(width, name)
Definition: cbs_vp9.c:255
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
link
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
Definition: filter_design.txt:23
ff_vk_update_descriptor_set
void ff_vk_update_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, int set_id)
Updates a descriptor set via the updaters defined.
Definition: vulkan.c:1079
planes
static const struct @321 planes[]
ff_vk_start_exec_recording
int ff_vk_start_exec_recording(FFVulkanContext *s, FFVkExecContext *e)
Begin recording to the command buffer.
Definition: vulkan.c:463
av_vkfmt_from_pixfmt
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.
Definition: hwcontext_vulkan.c:238
main
int main(int argc, char *argv[])
Definition: avio_list_dir.c:112
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:537
ScaleVulkanContext::params_buf
FFVkBuffer params_buf
Definition: vf_scale_vulkan.c:41
GLSLD
#define GLSLD(D)
Definition: vulkan.h:47
scale_vulkan_uninit
static void scale_vulkan_uninit(AVFilterContext *avctx)
Definition: vf_scale_vulkan.c:485
ScaleVulkanContext::h_expr
char * h_expr
Definition: vf_scale_vulkan.c:50
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:620
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:410
ff_vk_create_exec_ctx
int ff_vk_create_exec_ctx(FFVulkanContext *s, FFVkExecContext **ctx, FFVkQueueFamilyCtx *qf)
Init an execution context for command recording and queue submission.
Definition: vulkan.c:385
src
#define src
Definition: vp8dsp.c:255
ff_vk_filter_config_output
int ff_vk_filter_config_output(AVFilterLink *outlink)
Definition: vulkan_filter.c:133
ff_vk_qf_rotate
void ff_vk_qf_rotate(FFVkQueueFamilyCtx *qf)
Rotate through the queues in a queue family.
Definition: vulkan.c:132
FFVulkanContext
Definition: vulkan.h:188
ScaleVulkanContext::out_range
enum AVColorRange out_range
Definition: vf_scale_vulkan.c:53
F_NB
@ F_NB
Definition: vf_scale_vulkan.c:32
FLAGS
#define FLAGS
Definition: vf_scale_vulkan.c:496
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:563
ScaleVulkanContext::vkctx
FFVulkanContext vkctx
Definition: vf_scale_vulkan.c:36
FFVulkanPipeline
Definition: vulkan.h:104
AVFrame::crop_bottom
size_t crop_bottom
Definition: frame.h:665
ff_vk_discard_exec_deps
void ff_vk_discard_exec_deps(FFVkExecContext *e)
Discards all queue dependencies.
Definition: vulkan.c:447
AVFrame::crop_left
size_t crop_left
Definition: frame.h:666
ff_vk_unmap_buffers
int ff_vk_unmap_buffers(FFVulkanContext *s, FFVkBuffer *buf, int nb_buffers, int flush)
Unmaps the buffer from userspace.
Definition: vulkan.c:307
AVVkFrame::access
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
Definition: hwcontext_vulkan.h:240
FFVulkanDescriptorSetBinding
Definition: vulkan.h:78
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:117
AVVkFrame
Definition: hwcontext_vulkan.h:213
ff_vk_init_pipeline_layout
int ff_vk_init_pipeline_layout(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes the pipeline layout after all shaders and descriptor sets have been finished.
Definition: vulkan.c:1115
size
int size
Definition: twinvq_data.h:10344
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:318
FFVkQueueFamilyCtx
Definition: vulkan.h:97
scale_eval.h
ff_vk_submit_exec_queue
int ff_vk_submit_exec_queue(FFVulkanContext *s, FFVkExecContext *e)
Submits a command buffer to the queue for execution.
Definition: vulkan.c:590
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
FFVkExecContext
Definition: vulkan.h:154
FFVulkanDescriptorSetBinding::name
const char * name
Definition: vulkan.h:79
ff_vk_mt_is_np_rgb
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:709
ScaleVulkanContext::input_images
VkDescriptorImageInfo input_images[3]
Definition: vf_scale_vulkan.c:44
internal.h
ScaleVulkanContext::scaler
enum ScalerFunc scaler
Definition: vf_scale_vulkan.c:52
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: internal.h:181
ff_vf_scale_vulkan
const AVFilter ff_vf_scale_vulkan
Definition: vf_scale_vulkan.c:533
ScaleVulkanContext::w_expr
char * w_expr
Definition: vf_scale_vulkan.c:49
ff_vk_init_compute_pipeline
int ff_vk_init_compute_pipeline(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes a compute pipeline.
Definition: vulkan.c:1228
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
scale_bilinear
static const char scale_bilinear[]
Definition: vf_scale_vulkan.c:58
write_444
static const char write_444[]
Definition: vf_scale_vulkan.c:101
ff_vk_shader_rep_fmt
const char * ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)
Gets the glsl format string for a pixel format.
Definition: vulkan.c:721
scale_vulkan_inputs
static const AVFilterPad scale_vulkan_inputs[]
Definition: vf_scale_vulkan.c:516
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
GLSLF
#define GLSLF(N, S,...)
Definition: vulkan.h:46
ff_vk_create_pipeline
FFVulkanPipeline * ff_vk_create_pipeline(FFVulkanContext *s, FFVkQueueFamilyCtx *qf)
Inits a pipeline.
Definition: vulkan.c:1219
ff_vk_free_buf
void ff_vk_free_buf(FFVulkanContext *s, FFVkBuffer *buf)
Frees a buffer.
Definition: vulkan.c:349
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:580
AVFilter
Filter definition.
Definition: avfilter.h:165
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
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
FFVulkanContext::vkfn
FFVulkanFunctions vkfn
Definition: vulkan.h:191
pos
unsigned int pos
Definition: spdifenc.c:412
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2592
ff_vk_add_descriptor_set
int ff_vk_add_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, FFVkSPIRVShader *shd, FFVulkanDescriptorSetBinding *desc, int num, int only_print_to_shader)
Adds a descriptor set to the shader and registers them in the pipeline.
Definition: vulkan.c:922
AVFrame::height
int height
Definition: frame.h:389
random_seed.h
ScaleVulkanContext::initialized
int initialized
Definition: vf_scale_vulkan.c:55
FFVkSPIRVShader
Definition: vulkan.h:58
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
CGROUPS
#define CGROUPS
Definition: vf_scale_vulkan.c:26
ScaleVulkanContext::output_images
VkDescriptorImageInfo output_images[3]
Definition: vf_scale_vulkan.c:45
ff_vk_init_sampler
FFVkSampler * ff_vk_init_sampler(FFVulkanContext *s, int unnorm_coords, VkFilter filt)
Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
Definition: vulkan.c:670
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
ScaleVulkanContext
Definition: vf_scale_vulkan.c:35
GLSLC
#define GLSLC(N, S)
Definition: vulkan.h:44
ff_vk_filter_config_input
int ff_vk_filter_config_input(AVFilterLink *inlink)
Definition: vulkan_filter.c:52
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVVkFrame::layout
VkImageLayout layout[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:241
ff_vk_map_buffers
int ff_vk_map_buffers(FFVulkanContext *s, FFVkBuffer *buf, uint8_t *mem[], int nb_buffers, int invalidate)
Maps the buffer to userspace.
Definition: vulkan.c:258
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
AVFrame::crop_top
size_t crop_top
Definition: frame.h:664
ff_vk_set_compute_shader_sizes
void ff_vk_set_compute_shader_sizes(FFVkSPIRVShader *shd, int local_size[3])
Writes the workgroup size for a shader.
Definition: vulkan.c:816
FFVkBuffer
Definition: vulkan.h:91
scale_vulkan_config_output
static int scale_vulkan_config_output(AVFilterLink *outlink)
Definition: vf_scale_vulkan.c:438
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
uninit
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:282
init_filter
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
Definition: vf_scale_vulkan.c:110
ScaleVulkanContext::qf
FFVkQueueFamilyCtx qf
Definition: vf_scale_vulkan.c:38
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:562
ScaleVulkanContext::params_desc
VkDescriptorBufferInfo params_desc
Definition: vf_scale_vulkan.c:46
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
RET
#define RET(x)
Definition: vulkan.h:52
FFVulkanFunctions
Definition: vulkan_functions.h:175
ScaleVulkanContext::out_format_string
char * out_format_string
Definition: vf_scale_vulkan.c:48