FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffplay_renderer.c
Date: 2024-05-04 02:01:39
Exec Total Coverage
Lines: 0 13 0.0%
Functions: 0 6 0.0%
Branches: 0 0 -%

Line Branch Exec Source
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 #define VK_NO_PROTOTYPES
20 #define VK_ENABLE_BETA_EXTENSIONS
21
22 #include "config.h"
23 #include "ffplay_renderer.h"
24
25 #if (SDL_VERSION_ATLEAST(2, 0, 6) && CONFIG_LIBPLACEBO)
26 /* Get PL_API_VER */
27 #include <libplacebo/config.h>
28 #define HAVE_VULKAN_RENDERER (PL_API_VER >= 278)
29 #else
30 #define HAVE_VULKAN_RENDERER 0
31 #endif
32
33 #if HAVE_VULKAN_RENDERER
34
35 #if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
36 #define VK_USE_PLATFORM_WIN32_KHR
37 #endif
38
39 #include <libplacebo/vulkan.h>
40 #include <libplacebo/utils/frame_queue.h>
41 #include <libplacebo/utils/libav.h>
42 #include <SDL_vulkan.h>
43
44 #include "libavutil/bprint.h"
45 #include "libavutil/mem.h"
46
47 #endif
48
49 struct VkRenderer {
50 const AVClass *class;
51
52 int (*create)(VkRenderer *renderer, SDL_Window *window, AVDictionary *dict);
53
54 int (*get_hw_dev)(VkRenderer *renderer, AVBufferRef **dev);
55
56 int (*display)(VkRenderer *renderer, AVFrame *frame);
57
58 int (*resize)(VkRenderer *renderer, int width, int height);
59
60 void (*destroy)(VkRenderer *renderer);
61 };
62
63 #if HAVE_VULKAN_RENDERER
64
65 typedef struct RendererContext {
66 VkRenderer api;
67
68 // Can be NULL when vulkan instance is created by avutil
69 pl_vk_inst placebo_instance;
70 pl_vulkan placebo_vulkan;
71 pl_swapchain swapchain;
72 VkSurfaceKHR vk_surface;
73 pl_renderer renderer;
74 pl_tex tex[4];
75
76 pl_log vk_log;
77
78 AVBufferRef *hw_device_ref;
79 AVBufferRef *hw_frame_ref;
80 enum AVPixelFormat *transfer_formats;
81 AVHWFramesConstraints *constraints;
82
83 PFN_vkGetInstanceProcAddr get_proc_addr;
84 // This field is a copy from pl_vk_inst->instance or hw_device_ref instance.
85 VkInstance inst;
86
87 AVFrame *vk_frame;
88 } RendererContext;
89
90 static void vk_log_cb(void *log_priv, enum pl_log_level level,
91 const char *msg)
92 {
93 static const int level_map[] = {
94 AV_LOG_QUIET,
95 AV_LOG_FATAL,
96 AV_LOG_ERROR,
97 AV_LOG_WARNING,
98 AV_LOG_INFO,
99 AV_LOG_DEBUG,
100 AV_LOG_TRACE,
101 };
102
103 if (level > 0 && level < FF_ARRAY_ELEMS(level_map))
104 av_log(log_priv, level_map[level], "%s\n", msg);
105 }
106
107 // Should keep sync with optional_device_exts inside hwcontext_vulkan.c
108 static const char *optional_device_exts[] = {
109 /* Misc or required by other extensions */
110 VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME,
111 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
112 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
113 VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME,
114 VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME,
115 VK_EXT_SHADER_ATOMIC_FLOAT_EXTENSION_NAME,
116 VK_KHR_COOPERATIVE_MATRIX_EXTENSION_NAME,
117
118 /* Imports/exports */
119 VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
120 VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
121 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
122 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
123 VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME,
124 #ifdef _WIN32
125 VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
126 VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME,
127 #endif
128
129 /* Video encoding/decoding */
130 VK_KHR_VIDEO_QUEUE_EXTENSION_NAME,
131 VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME,
132 VK_KHR_VIDEO_DECODE_H264_EXTENSION_NAME,
133 VK_KHR_VIDEO_DECODE_H265_EXTENSION_NAME,
134 "VK_MESA_video_decode_av1",
135 };
136
137 static inline int enable_debug(const AVDictionary *opt)
138 {
139 AVDictionaryEntry *entry = av_dict_get(opt, "debug", NULL, 0);
140 int debug = entry && strtol(entry->value, NULL, 10);
141 return debug;
142 }
143
144 static void hwctx_lock_queue(void *priv, uint32_t qf, uint32_t qidx)
145 {
146 AVHWDeviceContext *avhwctx = priv;
147 const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
148 hwctx->lock_queue(avhwctx, qf, qidx);
149 }
150
151 static void hwctx_unlock_queue(void *priv, uint32_t qf, uint32_t qidx)
152 {
153 AVHWDeviceContext *avhwctx = priv;
154 const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
155 hwctx->unlock_queue(avhwctx, qf, qidx);
156 }
157
158 static int add_instance_extension(const char **ext, unsigned num_ext,
159 const AVDictionary *opt,
160 AVDictionary **dict)
161 {
162 const char *inst_ext_key = "instance_extensions";
163 AVDictionaryEntry *entry;
164 AVBPrint buf;
165 char *ext_list = NULL;
166 int ret;
167
168 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
169 for (int i = 0; i < num_ext; i++) {
170 if (i)
171 av_bprintf(&buf, "+%s", ext[i]);
172 else
173 av_bprintf(&buf, "%s", ext[i]);
174 }
175
176 entry = av_dict_get(opt, inst_ext_key, NULL, 0);
177 if (entry && entry->value && entry->value[0]) {
178 if (num_ext)
179 av_bprintf(&buf, "+");
180 av_bprintf(&buf, "%s", entry->value);
181 }
182
183 ret = av_bprint_finalize(&buf, &ext_list);
184 if (ret < 0)
185 return ret;
186 return av_dict_set(dict, inst_ext_key, ext_list, AV_DICT_DONT_STRDUP_VAL);
187 }
188
189 static int add_device_extension(const AVDictionary *opt,
190 AVDictionary **dict)
191 {
192 const char *dev_ext_key = "device_extensions";
193 AVDictionaryEntry *entry;
194 AVBPrint buf;
195 char *ext_list = NULL;
196 int ret;
197
198 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
199 av_bprintf(&buf, "%s", VK_KHR_SWAPCHAIN_EXTENSION_NAME);
200 for (int i = 0; i < pl_vulkan_num_recommended_extensions; i++)
201 av_bprintf(&buf, "+%s", pl_vulkan_recommended_extensions[i]);
202
203 entry = av_dict_get(opt, dev_ext_key, NULL, 0);
204 if (entry && entry->value && entry->value[0])
205 av_bprintf(&buf, "+%s", entry->value);
206
207 ret = av_bprint_finalize(&buf, &ext_list);
208 if (ret < 0)
209 return ret;
210 return av_dict_set(dict, dev_ext_key, ext_list, AV_DICT_DONT_STRDUP_VAL);
211 }
212
213 static const char *select_device(const AVDictionary *opt)
214 {
215 const AVDictionaryEntry *entry;
216
217 entry = av_dict_get(opt, "device", NULL, 0);
218 if (entry)
219 return entry->value;
220 return NULL;
221 }
222
223 static int create_vk_by_hwcontext(VkRenderer *renderer,
224 const char **ext, unsigned num_ext,
225 const AVDictionary *opt)
226 {
227 RendererContext *ctx = (RendererContext *) renderer;
228 AVHWDeviceContext *dev;
229 AVVulkanDeviceContext *hwctx;
230 AVDictionary *dict = NULL;
231 int ret;
232
233 ret = add_instance_extension(ext, num_ext, opt, &dict);
234 if (ret < 0)
235 return ret;
236 ret = add_device_extension(opt, &dict);
237 if (ret) {
238 av_dict_free(&dict);
239 return ret;
240 }
241
242 ret = av_hwdevice_ctx_create(&ctx->hw_device_ref, AV_HWDEVICE_TYPE_VULKAN,
243 select_device(opt), dict, 0);
244 av_dict_free(&dict);
245 if (ret < 0)
246 return ret;
247
248 dev = (AVHWDeviceContext *) ctx->hw_device_ref->data;
249 hwctx = dev->hwctx;
250
251 // There is no way to pass SDL GetInstanceProcAddr to hwdevice.
252 // Check the result and return error if they don't match.
253 if (hwctx->get_proc_addr != SDL_Vulkan_GetVkGetInstanceProcAddr()) {
254 av_log(renderer, AV_LOG_ERROR,
255 "hwdevice and SDL use different get_proc_addr. "
256 "Try -vulkan_params create_by_placebo=1\n");
257 return AVERROR_PATCHWELCOME;
258 }
259
260 ctx->get_proc_addr = hwctx->get_proc_addr;
261 ctx->inst = hwctx->inst;
262 ctx->placebo_vulkan = pl_vulkan_import(ctx->vk_log,
263 pl_vulkan_import_params(
264 .instance = hwctx->inst,
265 .get_proc_addr = hwctx->get_proc_addr,
266 .phys_device = hwctx->phys_dev,
267 .device = hwctx->act_dev,
268 .extensions = hwctx->enabled_dev_extensions,
269 .num_extensions = hwctx->nb_enabled_dev_extensions,
270 .features = &hwctx->device_features,
271 .lock_queue = hwctx_lock_queue,
272 .unlock_queue = hwctx_unlock_queue,
273 .queue_ctx = dev,
274 .queue_graphics = {
275 .index = hwctx->queue_family_index,
276 .count = hwctx->nb_graphics_queues,
277 },
278 .queue_compute = {
279 .index = hwctx->queue_family_comp_index,
280 .count = hwctx->nb_comp_queues,
281 },
282 .queue_transfer = {
283 .index = hwctx->queue_family_tx_index,
284 .count = hwctx->nb_tx_queues,
285 },
286 ));
287 if (!ctx->placebo_vulkan)
288 return AVERROR_EXTERNAL;
289
290 return 0;
291 }
292
293 static void placebo_lock_queue(struct AVHWDeviceContext *dev_ctx,
294 uint32_t queue_family, uint32_t index)
295 {
296 RendererContext *ctx = dev_ctx->user_opaque;
297 pl_vulkan vk = ctx->placebo_vulkan;
298 vk->lock_queue(vk, queue_family, index);
299 }
300
301 static void placebo_unlock_queue(struct AVHWDeviceContext *dev_ctx,
302 uint32_t queue_family,
303 uint32_t index)
304 {
305 RendererContext *ctx = dev_ctx->user_opaque;
306 pl_vulkan vk = ctx->placebo_vulkan;
307 vk->unlock_queue(vk, queue_family, index);
308 }
309
310 static int get_decode_queue(VkRenderer *renderer, int *index, int *count)
311 {
312 RendererContext *ctx = (RendererContext *) renderer;
313 VkQueueFamilyProperties *queue_family_prop = NULL;
314 uint32_t num_queue_family_prop = 0;
315 PFN_vkGetPhysicalDeviceQueueFamilyProperties get_queue_family_prop;
316 PFN_vkGetInstanceProcAddr get_proc_addr = ctx->get_proc_addr;
317
318 *index = -1;
319 *count = 0;
320 get_queue_family_prop = (PFN_vkGetPhysicalDeviceQueueFamilyProperties)
321 get_proc_addr(ctx->placebo_instance->instance,
322 "vkGetPhysicalDeviceQueueFamilyProperties");
323 get_queue_family_prop(ctx->placebo_vulkan->phys_device,
324 &num_queue_family_prop, NULL);
325 if (!num_queue_family_prop)
326 return AVERROR_EXTERNAL;
327
328 queue_family_prop = av_calloc(num_queue_family_prop,
329 sizeof(*queue_family_prop));
330 if (!queue_family_prop)
331 return AVERROR(ENOMEM);
332
333 get_queue_family_prop(ctx->placebo_vulkan->phys_device,
334 &num_queue_family_prop,
335 queue_family_prop);
336
337 for (int i = 0; i < num_queue_family_prop; i++) {
338 if (queue_family_prop[i].queueFlags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) {
339 *index = i;
340 *count = queue_family_prop[i].queueCount;
341 break;
342 }
343 }
344 av_free(queue_family_prop);
345
346 return 0;
347 }
348
349 static int create_vk_by_placebo(VkRenderer *renderer,
350 const char **ext, unsigned num_ext,
351 const AVDictionary *opt)
352 {
353 RendererContext *ctx = (RendererContext *) renderer;
354 AVHWDeviceContext *device_ctx;
355 AVVulkanDeviceContext *vk_dev_ctx;
356 int decode_index;
357 int decode_count;
358 int ret;
359
360 ctx->get_proc_addr = SDL_Vulkan_GetVkGetInstanceProcAddr();
361
362 ctx->placebo_instance = pl_vk_inst_create(ctx->vk_log, pl_vk_inst_params(
363 .get_proc_addr = ctx->get_proc_addr,
364 .debug = enable_debug(opt),
365 .extensions = ext,
366 .num_extensions = num_ext
367 ));
368 if (!ctx->placebo_instance) {
369 return AVERROR_EXTERNAL;
370 }
371 ctx->inst = ctx->placebo_instance->instance;
372
373 ctx->placebo_vulkan = pl_vulkan_create(ctx->vk_log, pl_vulkan_params(
374 .instance = ctx->placebo_instance->instance,
375 .get_proc_addr = ctx->placebo_instance->get_proc_addr,
376 .surface = ctx->vk_surface,
377 .allow_software = false,
378 .opt_extensions = optional_device_exts,
379 .num_opt_extensions = FF_ARRAY_ELEMS(optional_device_exts),
380 .extra_queues = VK_QUEUE_VIDEO_DECODE_BIT_KHR,
381 .device_name = select_device(opt),
382 ));
383 if (!ctx->placebo_vulkan)
384 return AVERROR_EXTERNAL;
385 ctx->hw_device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VULKAN);
386 if (!ctx->hw_device_ref) {
387 return AVERROR(ENOMEM);
388 }
389
390 device_ctx = (AVHWDeviceContext *) ctx->hw_device_ref->data;
391 device_ctx->user_opaque = ctx;
392
393 vk_dev_ctx = device_ctx->hwctx;
394 vk_dev_ctx->lock_queue = placebo_lock_queue,
395 vk_dev_ctx->unlock_queue = placebo_unlock_queue;
396
397 vk_dev_ctx->get_proc_addr = ctx->placebo_instance->get_proc_addr;
398
399 vk_dev_ctx->inst = ctx->placebo_instance->instance;
400 vk_dev_ctx->phys_dev = ctx->placebo_vulkan->phys_device;
401 vk_dev_ctx->act_dev = ctx->placebo_vulkan->device;
402
403 vk_dev_ctx->device_features = *ctx->placebo_vulkan->features;
404
405 vk_dev_ctx->enabled_inst_extensions = ctx->placebo_instance->extensions;
406 vk_dev_ctx->nb_enabled_inst_extensions = ctx->placebo_instance->num_extensions;
407
408 vk_dev_ctx->enabled_dev_extensions = ctx->placebo_vulkan->extensions;
409 vk_dev_ctx->nb_enabled_dev_extensions = ctx->placebo_vulkan->num_extensions;
410
411 vk_dev_ctx->queue_family_index = ctx->placebo_vulkan->queue_graphics.index;
412 vk_dev_ctx->nb_graphics_queues = ctx->placebo_vulkan->queue_graphics.count;
413
414 vk_dev_ctx->queue_family_tx_index = ctx->placebo_vulkan->queue_transfer.index;
415 vk_dev_ctx->nb_tx_queues = ctx->placebo_vulkan->queue_transfer.count;
416
417 vk_dev_ctx->queue_family_comp_index = ctx->placebo_vulkan->queue_compute.index;
418 vk_dev_ctx->nb_comp_queues = ctx->placebo_vulkan->queue_compute.count;
419
420 ret = get_decode_queue(renderer, &decode_index, &decode_count);
421 if (ret < 0)
422 return ret;
423
424 vk_dev_ctx->queue_family_decode_index = decode_index;
425 vk_dev_ctx->nb_decode_queues = decode_count;
426
427 ret = av_hwdevice_ctx_init(ctx->hw_device_ref);
428 if (ret < 0)
429 return ret;
430
431 return 0;
432 }
433
434 static int create(VkRenderer *renderer, SDL_Window *window, AVDictionary *opt)
435 {
436 int ret = 0;
437 unsigned num_ext = 0;
438 const char **ext = NULL;
439 int w, h;
440 struct pl_log_params vk_log_params = {
441 .log_cb = vk_log_cb,
442 .log_level = PL_LOG_DEBUG,
443 .log_priv = renderer,
444 };
445 RendererContext *ctx = (RendererContext *) renderer;
446 AVDictionaryEntry *entry;
447
448 ctx->vk_log = pl_log_create(PL_API_VER, &vk_log_params);
449
450 if (!SDL_Vulkan_GetInstanceExtensions(window, &num_ext, NULL)) {
451 av_log(NULL, AV_LOG_FATAL, "Failed to get vulkan extensions: %s\n",
452 SDL_GetError());
453 return AVERROR_EXTERNAL;
454 }
455
456 ext = av_calloc(num_ext, sizeof(*ext));
457 if (!ext) {
458 ret = AVERROR(ENOMEM);
459 goto out;
460 }
461
462 SDL_Vulkan_GetInstanceExtensions(window, &num_ext, ext);
463
464 entry = av_dict_get(opt, "create_by_placebo", NULL, 0);
465 if (entry && strtol(entry->value, NULL, 10))
466 ret = create_vk_by_placebo(renderer, ext, num_ext, opt);
467 else
468 ret = create_vk_by_hwcontext(renderer, ext, num_ext, opt);
469 if (ret < 0)
470 goto out;
471
472 if (!SDL_Vulkan_CreateSurface(window, ctx->inst, &ctx->vk_surface)) {
473 ret = AVERROR_EXTERNAL;
474 goto out;
475 }
476
477 ctx->swapchain = pl_vulkan_create_swapchain(
478 ctx->placebo_vulkan,
479 pl_vulkan_swapchain_params(
480 .surface = ctx->vk_surface,
481 .present_mode = VK_PRESENT_MODE_FIFO_KHR));
482 if (!ctx->swapchain) {
483 ret = AVERROR_EXTERNAL;
484 goto out;
485 }
486
487 SDL_Vulkan_GetDrawableSize(window, &w, &h);
488 pl_swapchain_resize(ctx->swapchain, &w, &h);
489
490 ctx->renderer = pl_renderer_create(ctx->vk_log, ctx->placebo_vulkan->gpu);
491 if (!ctx->renderer) {
492 ret = AVERROR_EXTERNAL;
493 goto out;
494 }
495
496 ctx->vk_frame = av_frame_alloc();
497 if (!ctx->vk_frame) {
498 ret = AVERROR(ENOMEM);
499 goto out;
500 }
501
502 ret = 0;
503
504 out:
505 av_free(ext);
506 return ret;
507 }
508
509 static int get_hw_dev(VkRenderer *renderer, AVBufferRef **dev)
510 {
511 RendererContext *ctx = (RendererContext *) renderer;
512
513 *dev = ctx->hw_device_ref;
514 return 0;
515 }
516
517 static int create_hw_frame(VkRenderer *renderer, AVFrame *frame)
518 {
519 RendererContext *ctx = (RendererContext *) renderer;
520 AVHWFramesContext *src_hw_frame = (AVHWFramesContext *)
521 frame->hw_frames_ctx->data;
522 AVHWFramesContext *hw_frame;
523 AVVulkanFramesContext *vk_frame_ctx;
524 int ret;
525
526 if (ctx->hw_frame_ref) {
527 hw_frame = (AVHWFramesContext *) ctx->hw_frame_ref->data;
528
529 if (hw_frame->width == frame->width &&
530 hw_frame->height == frame->height &&
531 hw_frame->sw_format == src_hw_frame->sw_format)
532 return 0;
533
534 av_buffer_unref(&ctx->hw_frame_ref);
535 }
536
537 if (!ctx->constraints) {
538 ctx->constraints = av_hwdevice_get_hwframe_constraints(
539 ctx->hw_device_ref, NULL);
540 if (!ctx->constraints)
541 return AVERROR(ENOMEM);
542 }
543
544 // Check constraints and skip create hwframe. Don't take it as error since
545 // we can fallback to memory copy from GPU to CPU.
546 if ((ctx->constraints->max_width &&
547 ctx->constraints->max_width < frame->width) ||
548 (ctx->constraints->max_height &&
549 ctx->constraints->max_height < frame->height) ||
550 (ctx->constraints->min_width &&
551 ctx->constraints->min_width > frame->width) ||
552 (ctx->constraints->min_height &&
553 ctx->constraints->min_height > frame->height))
554 return 0;
555
556 if (ctx->constraints->valid_sw_formats) {
557 enum AVPixelFormat *sw_formats = ctx->constraints->valid_sw_formats;
558 while (*sw_formats != AV_PIX_FMT_NONE) {
559 if (*sw_formats == src_hw_frame->sw_format)
560 break;
561 sw_formats++;
562 }
563 if (*sw_formats == AV_PIX_FMT_NONE)
564 return 0;
565 }
566
567 ctx->hw_frame_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref);
568 if (!ctx->hw_frame_ref)
569 return AVERROR(ENOMEM);
570
571 hw_frame = (AVHWFramesContext *) ctx->hw_frame_ref->data;
572 hw_frame->format = AV_PIX_FMT_VULKAN;
573 hw_frame->sw_format = src_hw_frame->sw_format;
574 hw_frame->width = frame->width;
575 hw_frame->height = frame->height;
576
577 if (frame->format == AV_PIX_FMT_CUDA) {
578 vk_frame_ctx = hw_frame->hwctx;
579 vk_frame_ctx->flags = AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE;
580 }
581
582 ret = av_hwframe_ctx_init(ctx->hw_frame_ref);
583 if (ret < 0) {
584 av_log(renderer, AV_LOG_ERROR, "Create hwframe context failed, %s\n",
585 av_err2str(ret));
586 return ret;
587 }
588
589 av_hwframe_transfer_get_formats(ctx->hw_frame_ref,
590 AV_HWFRAME_TRANSFER_DIRECTION_TO,
591 &ctx->transfer_formats, 0);
592
593 return 0;
594 }
595
596 static inline int check_hw_transfer(RendererContext *ctx, AVFrame *frame)
597 {
598 if (!ctx->hw_frame_ref || !ctx->transfer_formats)
599 return 0;
600
601 for (int i = 0; ctx->transfer_formats[i] != AV_PIX_FMT_NONE; i++)
602 if (ctx->transfer_formats[i] == frame->format)
603 return 1;
604
605 return 0;
606 }
607
608 static inline int move_to_output_frame(RendererContext *ctx, AVFrame *frame)
609 {
610 int ret = av_frame_copy_props(ctx->vk_frame, frame);
611 if (ret < 0)
612 return ret;
613 av_frame_unref(frame);
614 av_frame_move_ref(frame, ctx->vk_frame);
615 return 0;
616 }
617
618 static int map_frame(VkRenderer *renderer, AVFrame *frame, int use_hw_frame)
619 {
620 RendererContext *ctx = (RendererContext *) renderer;
621 int ret;
622
623 if (use_hw_frame && !ctx->hw_frame_ref)
624 return AVERROR(ENOSYS);
625
626 // Try map data first
627 av_frame_unref(ctx->vk_frame);
628 if (use_hw_frame) {
629 ctx->vk_frame->hw_frames_ctx = av_buffer_ref(ctx->hw_frame_ref);
630 ctx->vk_frame->format = AV_PIX_FMT_VULKAN;
631 }
632 ret = av_hwframe_map(ctx->vk_frame, frame, 0);
633 if (!ret)
634 return move_to_output_frame(ctx, frame);
635
636 if (ret != AVERROR(ENOSYS))
637 av_log(NULL, AV_LOG_FATAL, "Map frame failed: %s\n", av_err2str(ret));
638 return ret;
639 }
640
641 static int transfer_frame(VkRenderer *renderer, AVFrame *frame, int use_hw_frame)
642 {
643 RendererContext *ctx = (RendererContext *) renderer;
644 int ret;
645
646 if (use_hw_frame && !check_hw_transfer(ctx, frame))
647 return AVERROR(ENOSYS);
648
649 av_frame_unref(ctx->vk_frame);
650 if (use_hw_frame)
651 av_hwframe_get_buffer(ctx->hw_frame_ref, ctx->vk_frame, 0);
652 ret = av_hwframe_transfer_data(ctx->vk_frame, frame, 1);
653 if (!ret)
654 return move_to_output_frame(ctx, frame);
655
656 if (ret != AVERROR(ENOSYS))
657 av_log(NULL, AV_LOG_FATAL, "Transfer frame failed: %s\n",
658 av_err2str(ret));
659 return ret;
660 }
661
662 static int convert_frame(VkRenderer *renderer, AVFrame *frame)
663 {
664 int ret;
665
666 if (!frame->hw_frames_ctx)
667 return 0;
668
669 if (frame->format == AV_PIX_FMT_VULKAN)
670 return 0;
671
672 ret = create_hw_frame(renderer, frame);
673 if (ret < 0)
674 return ret;
675
676 for (int use_hw = 1; use_hw >=0; use_hw--) {
677 ret = map_frame(renderer, frame, use_hw);
678 if (!ret)
679 return 0;
680 if (ret != AVERROR(ENOSYS))
681 return ret;
682
683 ret = transfer_frame(renderer, frame, use_hw);
684 if (!ret)
685 return 0;
686 if (ret != AVERROR(ENOSYS))
687 return ret;
688 }
689
690 return ret;
691 }
692
693 static int display(VkRenderer *renderer, AVFrame *frame)
694 {
695 struct pl_swapchain_frame swap_frame = {0};
696 struct pl_frame pl_frame = {0};
697 struct pl_frame target = {0};
698 RendererContext *ctx = (RendererContext *) renderer;
699 int ret = 0;
700
701 ret = convert_frame(renderer, frame);
702 if (ret < 0)
703 return ret;
704
705 if (!pl_map_avframe_ex(ctx->placebo_vulkan->gpu, &pl_frame, pl_avframe_params(
706 .frame = frame,
707 .tex = ctx->tex))) {
708 av_log(NULL, AV_LOG_ERROR, "pl_map_avframe_ex failed\n");
709 return AVERROR_EXTERNAL;
710 }
711
712 if (!pl_swapchain_start_frame(ctx->swapchain, &swap_frame)) {
713 av_log(NULL, AV_LOG_ERROR, "start frame failed\n");
714 ret = AVERROR_EXTERNAL;
715 goto out;
716 }
717
718 pl_frame_from_swapchain(&target, &swap_frame);
719 if (!pl_render_image(ctx->renderer, &pl_frame, &target,
720 &pl_render_default_params)) {
721 av_log(NULL, AV_LOG_ERROR, "pl_render_image failed\n");
722 ret = AVERROR_EXTERNAL;
723 goto out;
724 }
725
726 if (!pl_swapchain_submit_frame(ctx->swapchain)) {
727 av_log(NULL, AV_LOG_ERROR, "pl_swapchain_submit_frame failed\n");
728 ret = AVERROR_EXTERNAL;
729 goto out;
730 }
731 pl_swapchain_swap_buffers(ctx->swapchain);
732
733 out:
734 pl_unmap_avframe(ctx->placebo_vulkan->gpu, &pl_frame);
735 return ret;
736 }
737
738 static int resize(VkRenderer *renderer, int width, int height)
739 {
740 RendererContext *ctx = (RendererContext *) renderer;
741
742 if (!pl_swapchain_resize(ctx->swapchain, &width, &height))
743 return AVERROR_EXTERNAL;
744 return 0;
745 }
746
747 static void destroy(VkRenderer *renderer)
748 {
749 RendererContext *ctx = (RendererContext *) renderer;
750 PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR;
751
752 av_frame_free(&ctx->vk_frame);
753 av_freep(&ctx->transfer_formats);
754 av_hwframe_constraints_free(&ctx->constraints);
755 av_buffer_unref(&ctx->hw_frame_ref);
756
757 if (ctx->placebo_vulkan) {
758 for (int i = 0; i < FF_ARRAY_ELEMS(ctx->tex); i++)
759 pl_tex_destroy(ctx->placebo_vulkan->gpu, &ctx->tex[i]);
760 pl_renderer_destroy(&ctx->renderer);
761 pl_swapchain_destroy(&ctx->swapchain);
762 pl_vulkan_destroy(&ctx->placebo_vulkan);
763 }
764
765 if (ctx->vk_surface) {
766 vkDestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)
767 ctx->get_proc_addr(ctx->inst, "vkDestroySurfaceKHR");
768 vkDestroySurfaceKHR(ctx->inst, ctx->vk_surface, NULL);
769 ctx->vk_surface = NULL;
770 }
771
772 av_buffer_unref(&ctx->hw_device_ref);
773 pl_vk_inst_destroy(&ctx->placebo_instance);
774
775 pl_log_destroy(&ctx->vk_log);
776 }
777
778 static const AVClass vulkan_renderer_class = {
779 .class_name = "Vulkan Renderer",
780 .item_name = av_default_item_name,
781 .version = LIBAVUTIL_VERSION_INT,
782 };
783
784 VkRenderer *vk_get_renderer(void)
785 {
786 RendererContext *ctx = av_mallocz(sizeof(*ctx));
787 VkRenderer *renderer;
788
789 if (!ctx)
790 return NULL;
791
792 renderer = &ctx->api;
793 renderer->class = &vulkan_renderer_class;
794 renderer->get_hw_dev = get_hw_dev;
795 renderer->create = create;
796 renderer->display = display;
797 renderer->resize = resize;
798 renderer->destroy = destroy;
799
800 return renderer;
801 }
802
803 #else
804
805 VkRenderer *vk_get_renderer(void)
806 {
807 return NULL;
808 }
809
810 #endif
811
812 int vk_renderer_create(VkRenderer *renderer, SDL_Window *window,
813 AVDictionary *opt)
814 {
815 return renderer->create(renderer, window, opt);
816 }
817
818 int vk_renderer_get_hw_dev(VkRenderer *renderer, AVBufferRef **dev)
819 {
820 return renderer->get_hw_dev(renderer, dev);
821 }
822
823 int vk_renderer_display(VkRenderer *renderer, AVFrame *frame)
824 {
825 return renderer->display(renderer, frame);
826 }
827
828 int vk_renderer_resize(VkRenderer *renderer, int width, int height)
829 {
830 return renderer->resize(renderer, width, height);
831 }
832
833 void vk_renderer_destroy(VkRenderer *renderer)
834 {
835 renderer->destroy(renderer);
836 }
837