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
25
26 #define CGROUPS (int [3]){ 32, 32, 1 }
27
31
33 };
34
37
42
43 /* Shader updators, must be in the main filter struct */
47
51
54
57
61 C(1, vec2 npos = (vec2(
pos) + 0.5
f) / 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); )
66 };
67
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); )
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); )
80 };
81
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)); )
89 };
90
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)); )
99 };
100
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)); )
108 };
109
111 {
112 int err;
114 VkFilter sampler_mode;
117
123
125
128 sampler_mode = VK_FILTER_NEAREST;
129 break;
131 sampler_mode = VK_FILTER_LINEAR;
132 break;
133 };
134
135 /* Create a sampler */
137 if (!sampler)
139
143
144 { /* Create the shader */
146 {
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,
159 .mem_quali = "writeonly",
160 .dimensions = 2,
162 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
163 .updater =
s->output_images,
164 },
165 };
166
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
178 VK_SHADER_STAGE_COMPUTE_BIT);
179 if (!shd)
181
183
186
188
189 if (
s->vkctx.output_format !=
s->vkctx.input_format) {
191 }
192
193 switch (
s->vkctx.output_format) {
197 default: break;
198 }
199
203 GLSLC(1, ivec2
pos = ivec2(gl_GlobalInvocationID.xy); );
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);
208
209 if (
s->vkctx.output_format ==
s->vkctx.input_format) {
210 for (
int i = 0;
i < desc_i[1].
elems;
i++) {
217 GLSLF(2, imageStore(output_img[%
i],
pos, res); ,
i);
218 break;
219 };
221 }
222 } else {
225 switch (
s->vkctx.output_format) {
229 default:
return AVERROR(EINVAL);
230 }
231 }
232
234
236 }
237
240
241 if (
s->vkctx.output_format !=
s->vkctx.input_format) {
243 double tmp_mat[3][3];
244
245 struct {
246 float yuv_matrix[4][4];
247 } *par;
248
250 if (!lcoeffs) {
253 }
254
256 sizeof(*par),
257 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
258 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
259 if (err)
260 return err;
261
263 if (err)
264 return err;
265
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
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
284 }
285
286 /* Execution context */
288
290
291 return 0;
292
294 return err;
295 }
296
298 {
299 int err = 0;
300 VkCommandBuffer cmd_buf;
307 int barrier_count = 0;
311
312 /* Update descriptors and init the exec context */
315
318 &
s->input_images[
i].imageView, in->
img[
i],
321
323 &
s->output_images[
i].imageView,
out->img[
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
332
334 VkImageMemoryBarrier bar = {
335 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
336 .srcAccessMask = 0,
337 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
339 .newLayout =
s->input_images[
i].imageLayout,
340 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
341 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
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
351 in->
access[
i] = bar.dstAccessMask;
352 }
353
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
380
381 vk->CmdDispatch(cmd_buf,
384
387
389 if (err)
390 return err;
391
393
394 return err;
395
398 return err;
399 }
400
402 {
403 int err;
407
412 }
413
416
418
420 if (err < 0)
422
424 out->color_range =
s->out_range;
425 if (
s->vkctx.output_format !=
s->vkctx.input_format)
427
429
431
435 return err;
436 }
437
439 {
440 int err;
445
449 if (err < 0)
450 return err;
451
452 if (
s->out_format_string) {
457 }
458 } else {
459 s->vkctx.output_format =
s->vkctx.input_format;
460 }
461
462 if (
s->vkctx.output_format !=
s->vkctx.input_format) {
466 }
472 }
476 }
477
479 if (err < 0)
480 return err;
481
482 return 0;
483 }
484
486 {
488
491
493 }
494
495 #define OFFSET(x) offsetof(ScaleVulkanContext, x)
496 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
512 };
513
515
517 {
522 },
523 };
524
526 {
530 },
531 };
532
534 .
name =
"scale_vulkan",
542 .priv_class = &scale_vulkan_class,
544 };