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
261
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
273
274 s->params_desc.buffer =
s->params_buf.buf;
275 s->params_desc.range = VK_WHOLE_SIZE;
276
278 }
279
280 /* Execution context */
282
284
285 return 0;
286
288 return err;
289 }
290
292 {
293 int err = 0;
294 VkCommandBuffer cmd_buf;
301 int barrier_count = 0;
305
306 /* Update descriptors and init the exec context */
309
312 &
s->input_images[
i].imageView, in->
img[
i],
315
317 &
s->output_images[
i].imageView,
out->img[
i],
320
321 s->input_images[
i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
322 s->output_images[
i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
323 }
324
326
328 VkImageMemoryBarrier bar = {
329 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
330 .srcAccessMask = 0,
331 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
333 .newLayout =
s->input_images[
i].imageLayout,
334 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
335 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
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
345 in->
access[
i] = bar.dstAccessMask;
346 }
347
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 vk->CmdPipelineBarrier(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
374
375 vk->CmdDispatch(cmd_buf,
378
381
383 if (err)
384 return err;
385
387
388 return err;
389
392 return err;
393 }
394
396 {
397 int err;
401
406 }
407
410
412
414 if (err < 0)
416
418 out->color_range =
s->out_range;
419 if (
s->vkctx.output_format !=
s->vkctx.input_format)
421
423
425
429 return err;
430 }
431
433 {
434 int err;
439
443 if (err < 0)
444 return err;
445
446 if (
s->out_format_string) {
451 }
452 } else {
453 s->vkctx.output_format =
s->vkctx.input_format;
454 }
455
456 if (
s->vkctx.output_format !=
s->vkctx.input_format) {
460 }
466 }
470 }
471
473 }
474
476 {
478
481
483 }
484
485 #define OFFSET(x) offsetof(ScaleVulkanContext, x)
486 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
502 };
503
505
507 {
512 },
513 };
514
516 {
520 },
521 };
522
524 .
name =
"scale_vulkan",
532 .priv_class = &scale_vulkan_class,
534 };