FFmpeg
Loading...
Searching...
No Matches
vf_libplacebo.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 <math.h>
20
21#include "config_components.h"
22
23#include "libavutil/avassert.h"
24#include "libavutil/avstring.h"
25#include "libavutil/eval.h"
26#include "libavutil/fifo.h"
27#include "libavutil/file.h"
28#include "libavutil/frame.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
32#include "formats.h"
33#include "filters.h"
34#include "video.h"
35#include "vulkan_filter.h"
36#include "scale_eval.h"
37
38#include <libplacebo/options.h>
39#include <libplacebo/renderer.h>
40#include <libplacebo/utils/libav.h>
41#include <libplacebo/utils/frame_queue.h>
42#include <libplacebo/vulkan.h>
43
44#if PL_API_VER >= 386
45#include <libplacebo/compositor.h>
46#else
47typedef const struct pl_scene_t *pl_scene;
48#endif
49
50enum {
64};
65
66enum {
77};
78
79static const char *const var_names[] = {
80 "in_idx", "idx",///< index of input
81 "in_w", "iw", ///< width of the input video frame
82 "in_h", "ih", ///< height of the input video frame
83 "out_w", "ow", ///< width of the output video frame
84 "out_h", "oh", ///< height of the output video frame
85 "crop_w", "cw", ///< evaluated input crop width
86 "crop_h", "ch", ///< evaluated input crop height
87 "pos_w", "pw", ///< evaluated output placement width
88 "pos_h", "ph", ///< evaluated output placement height
89 "a", ///< iw/ih
90 "sar", ///< input pixel aspect ratio
91 "dar", ///< output pixel aspect ratio
92 "hsub", ///< input horizontal subsampling factor
93 "vsub", ///< input vertical subsampling factor
94 "ohsub", ///< output horizontal subsampling factor
95 "ovsub", ///< output vertical subsampling factor
96 "in_t", "t", ///< input frame pts
97 "out_t", "ot", ///< output frame pts
98 "n", ///< number of frame
99 NULL,
100};
101
124
125/* per-input dynamic filter state */
126typedef struct LibplaceboInput {
127 int idx;
128 pl_renderer renderer;
129 pl_queue queue;
130 enum pl_queue_status qstatus;
131 struct pl_frame_mix mix; ///< temporary storage
132 AVFifo *out_pts; ///< timestamps of wanted output frames
136
145
151
152typedef struct LibplaceboContext {
153 /* lavfi vulkan*/
155
156 /* libplacebo */
157 pl_log log;
158 pl_vulkan vulkan;
159 pl_gpu gpu;
160 pl_tex tex[4];
161 struct pl_custom_lut *lut;
162
163 /* dedicated renderer for linear output composition */
164 pl_renderer linear_rr;
166
167 /* input state */
171
172 /* scene graph (libplacebo_scene only) */
175
176 /* settings */
179 uint8_t fillcolor[4];
181 char *w_expr;
182 char *h_expr;
184 AVRational fps; ///< parsed FPS, or 0/0 for "none"
189 // Parsed expressions for input/output crop
195 /* enum pl_lut_type */
214
215 pl_cache cache;
218
220
221 /* pl_render_params */
222 pl_options opts;
223 char *upscaler;
233
234 /* pl_deinterlace_params */
238
239 /* pl_deband_params */
245
246 /* pl_color_adjustment */
248 float contrast;
250 float hue;
251 float gamma;
253
254 /* pl_peak_detect_params */
260
261 /* pl_color_map_params */
265 struct pl_tone_map_constants tone_constants;
270
271 /* pl_dither_params */
275
276 /* pl_cone_params */
277 int cones;
278 float cone_str;
279
280 /* custom shaders */
284 const struct pl_hook *hooks[2];
287
288static inline enum pl_log_level get_log_level(void)
289{
290 int av_lev = av_log_get_level();
291 return av_lev >= AV_LOG_TRACE ? PL_LOG_TRACE :
292 av_lev >= AV_LOG_DEBUG ? PL_LOG_DEBUG :
293 av_lev >= AV_LOG_VERBOSE ? PL_LOG_INFO :
294 av_lev >= AV_LOG_WARNING ? PL_LOG_WARN :
295 av_lev >= AV_LOG_ERROR ? PL_LOG_ERR :
296 av_lev >= AV_LOG_FATAL ? PL_LOG_FATAL :
297 PL_LOG_NONE;
298}
299
300static void pl_av_log(void *log_ctx, enum pl_log_level level, const char *msg)
301{
302 int av_lev;
303
304 switch (level) {
305 case PL_LOG_FATAL: av_lev = AV_LOG_FATAL; break;
306 case PL_LOG_ERR: av_lev = AV_LOG_ERROR; break;
307 case PL_LOG_WARN: av_lev = AV_LOG_WARNING; break;
308 case PL_LOG_INFO: av_lev = AV_LOG_VERBOSE; break;
309 case PL_LOG_DEBUG: av_lev = AV_LOG_DEBUG; break;
310 case PL_LOG_TRACE: av_lev = AV_LOG_TRACE; break;
311 default: return;
312 }
313
314 av_log(log_ctx, av_lev, "%s\n", msg);
315}
316
317static const struct pl_tone_map_function *get_tonemapping_func(int tm) {
318 switch (tm) {
319 case TONE_MAP_AUTO: return &pl_tone_map_auto;
320 case TONE_MAP_CLIP: return &pl_tone_map_clip;
321 case TONE_MAP_ST2094_40: return &pl_tone_map_st2094_40;
322 case TONE_MAP_ST2094_10: return &pl_tone_map_st2094_10;
323 case TONE_MAP_BT2390: return &pl_tone_map_bt2390;
324 case TONE_MAP_BT2446A: return &pl_tone_map_bt2446a;
325 case TONE_MAP_SPLINE: return &pl_tone_map_spline;
326 case TONE_MAP_REINHARD: return &pl_tone_map_reinhard;
327 case TONE_MAP_MOBIUS: return &pl_tone_map_mobius;
328 case TONE_MAP_HABLE: return &pl_tone_map_hable;
329 case TONE_MAP_GAMMA: return &pl_tone_map_gamma;
330 case TONE_MAP_LINEAR: return &pl_tone_map_linear;
331 default: av_assert0(0);
332 }
333}
334
335static void set_gamut_mode(struct pl_color_map_params *p, int gamut_mode)
336{
337 switch (gamut_mode) {
338 case GAMUT_MAP_CLIP: p->gamut_mapping = &pl_gamut_map_clip; return;
339 case GAMUT_MAP_PERCEPTUAL: p->gamut_mapping = &pl_gamut_map_perceptual; return;
340 case GAMUT_MAP_RELATIVE: p->gamut_mapping = &pl_gamut_map_relative; return;
341 case GAMUT_MAP_SATURATION: p->gamut_mapping = &pl_gamut_map_saturation; return;
342 case GAMUT_MAP_ABSOLUTE: p->gamut_mapping = &pl_gamut_map_absolute; return;
343 case GAMUT_MAP_DESATURATE: p->gamut_mapping = &pl_gamut_map_desaturate; return;
344 case GAMUT_MAP_DARKEN: p->gamut_mapping = &pl_gamut_map_darken; return;
345 case GAMUT_MAP_HIGHLIGHT: p->gamut_mapping = &pl_gamut_map_highlight; return;
346 case GAMUT_MAP_LINEAR: p->gamut_mapping = &pl_gamut_map_linear; return;
347 }
348
349 av_assert0(0);
350};
351
352static int find_scaler(AVFilterContext *avctx,
353 const struct pl_filter_config **opt,
354 const char *name, int frame_mixing)
355{
356 const struct pl_filter_preset *preset, *presets_avail;
357 presets_avail = frame_mixing ? pl_frame_mixers : pl_scale_filters;
358
359 if (!strcmp(name, "help")) {
360 av_log(avctx, AV_LOG_INFO, "Available scaler presets:\n");
361 for (preset = presets_avail; preset->name; preset++)
362 av_log(avctx, AV_LOG_INFO, " %s\n", preset->name);
363 return AVERROR_EXIT;
364 }
365
366 for (preset = presets_avail; preset->name; preset++) {
367 if (!strcmp(name, preset->name)) {
368 *opt = preset->filter;
369 return 0;
370 }
371 }
372
373 av_log(avctx, AV_LOG_ERROR, "No such scaler preset '%s'.\n", name);
374 return AVERROR(EINVAL);
375}
376
378{
379 LibplaceboContext *s = avctx->priv;
380 int ret;
381 uint8_t *lutbuf;
382 size_t lutbuf_size;
383
384 if ((ret = av_file_map(s->lut_filename, &lutbuf, &lutbuf_size, 0, s)) < 0) {
385 av_log(avctx, AV_LOG_ERROR,
386 "The LUT file '%s' could not be read: %s\n",
387 s->lut_filename, av_err2str(ret));
388 return ret;
389 }
390
391 s->lut = pl_lut_parse_cube(s->log, lutbuf, lutbuf_size);
392 av_file_unmap(lutbuf, lutbuf_size);
393 if (!s->lut)
394 return AVERROR(EINVAL);
395 return 0;
396}
397
399{
400 int err = 0;
401 LibplaceboContext *s = ctx->priv;
403 pl_options opts = s->opts;
404 int gamut_mode = s->gamut_mode;
405
406 opts->deinterlace_params = *pl_deinterlace_params(
407 .algo = s->deinterlace,
408 .skip_spatial_check = s->skip_spatial_check,
409 );
410
411 opts->deband_params = *pl_deband_params(
412 .iterations = s->deband_iterations,
413 .threshold = s->deband_threshold,
414 .radius = s->deband_radius,
415 .grain = s->deband_grain,
416 );
417
418 opts->sigmoid_params = pl_sigmoid_default_params;
419
420 opts->color_adjustment = (struct pl_color_adjustment) {
421 .brightness = s->brightness,
422 .contrast = s->contrast,
423 .saturation = s->saturation,
424 .hue = s->hue,
425 .gamma = s->gamma,
426 // libplacebo uses a normalized/relative scale for CCT
427 .temperature = (s->temperature - 6500.0) / 3500.0,
428 };
429
430 opts->peak_detect_params = (struct pl_peak_detect_params) {
431 PL_PEAK_DETECT_DEFAULTS
432 .smoothing_period = s->smoothing,
433 .scene_threshold_low = s->scene_low,
434 .scene_threshold_high = s->scene_high,
435 .percentile = s->percentile,
436 };
437
438 struct pl_tone_map_constants tone_constants = s->tone_constants;
439 if (s->tonemapping_param) {
440 switch (s->tonemapping) {
441 case TONE_MAP_ST2094_40:
442 case TONE_MAP_ST2094_10:
443 tone_constants.knee_adaptation = s->tonemapping_param;
444 break;
445 case TONE_MAP_BT2390:
446 tone_constants.knee_offset = s->tonemapping_param;
447 break;
448 case TONE_MAP_AUTO:
449 case TONE_MAP_SPLINE:
450 tone_constants.spline_contrast = s->tonemapping_param;
451 break;
452 case TONE_MAP_REINHARD:
453 tone_constants.reinhard_contrast = s->tonemapping_param;
454 break;
455 case TONE_MAP_MOBIUS:
456 case TONE_MAP_GAMMA:
457 tone_constants.linear_knee = s->tonemapping_param;
458 break;
459 case TONE_MAP_LINEAR:
460 tone_constants.exposure = s->tonemapping_param;
461 break;
462 default:
463 break;
464 }
465 }
466
467 opts->color_map_params = (struct pl_color_map_params) {
468 PL_COLOR_MAP_DEFAULTS
469 .tone_mapping_function = get_tonemapping_func(s->tonemapping),
470 .tone_constants = tone_constants,
471 .inverse_tone_mapping = s->inverse_tonemapping,
472 .lut_size = s->tonemapping_lut_size,
473 .contrast_recovery = s->contrast_recovery,
474 .contrast_smoothness = s->contrast_smoothness,
475 };
476
477 set_gamut_mode(&opts->color_map_params, gamut_mode);
478
479 opts->dither_params = *pl_dither_params(
480 .method = s->dithering,
481 .lut_size = s->dither_lut_size,
482 .temporal = s->dither_temporal,
483 );
484
485 opts->cone_params = *pl_cone_params(
486 .cones = s->cones,
487 .strength = s->cone_str,
488 );
489
490 opts->params = (struct pl_render_params) {
491 PL_RENDER_DEFAULTS
492 .antiringing_strength = s->antiringing,
493 .background_transparency = 1.0f - (float) s->fillcolor[3] / UINT8_MAX,
494 .background_color = {
495 (float) s->fillcolor[0] / UINT8_MAX,
496 (float) s->fillcolor[1] / UINT8_MAX,
497 (float) s->fillcolor[2] / UINT8_MAX,
498 },
499 .corner_rounding = s->corner_rounding,
500
501 .deinterlace_params = &opts->deinterlace_params,
502 .deband_params = s->deband ? &opts->deband_params : NULL,
503 .sigmoid_params = s->sigmoid ? &opts->sigmoid_params : NULL,
504 .color_adjustment = &opts->color_adjustment,
505 .peak_detect_params = s->peakdetect ? &opts->peak_detect_params : NULL,
506 .color_map_params = &opts->color_map_params,
507 .dither_params = s->dithering >= 0 ? &opts->dither_params : NULL,
508 .cone_params = s->cones ? &opts->cone_params : NULL,
509
510 .hooks = s->hooks,
511 .num_hooks = s->num_hooks,
512
513 .skip_anti_aliasing = s->skip_aa,
514 .disable_linear_scaling = s->disable_linear,
515 .disable_builtin_scalers = s->disable_builtin,
516 .force_dither = s->force_dither,
517 .disable_fbos = s->disable_fbos,
518 };
519
520 RET(find_scaler(ctx, &opts->params.upscaler, s->upscaler, 0));
521 RET(find_scaler(ctx, &opts->params.downscaler, s->downscaler, 0));
522 RET(find_scaler(ctx, &opts->params.frame_mixer, s->frame_mixer, 1));
523
524 while ((e = av_dict_get(s->extra_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
525 if (!pl_options_set_str(s->opts, e->key, e->value)) {
526 err = AVERROR(EINVAL);
527 goto fail;
528 }
529 }
530
531 return 0;
532
533fail:
534 return err;
535}
536
537static int parse_shader(AVFilterContext *avctx, const void *shader, size_t len)
538{
539 LibplaceboContext *s = avctx->priv;
540 const struct pl_hook *hook;
541
542 hook = pl_mpv_user_shader_parse(s->gpu, shader, len);
543 if (!hook) {
544 av_log(avctx, AV_LOG_ERROR, "Failed parsing custom shader!\n");
545 return AVERROR(EINVAL);
546 }
547
548 s->hooks[s->num_hooks++] = hook;
549 return update_settings(avctx);
550}
551
552static void libplacebo_uninit(AVFilterContext *avctx);
553static int libplacebo_config_input(AVFilterLink *inlink);
554static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwctx);
555static int libplacebo_setup_gpu(AVFilterContext *avctx);
556
557/* Creates the log, shader cache and output format shared by all filters */
558static int init_common(AVFilterContext *avctx)
559{
560 LibplaceboContext *s = avctx->priv;
561
562 /* Create libplacebo log context */
563 s->log = pl_log_create(PL_API_VER, pl_log_params(
564 .log_level = get_log_level(),
565 .log_cb = pl_av_log,
566 .log_priv = s,
567 ));
568
569 if (!s->log)
570 return AVERROR(ENOMEM);
571
572 if (s->shader_cache && s->shader_cache[0]) {
573 s->cache = pl_cache_create(pl_cache_params(
574 .log = s->log,
575 .get = pl_cache_get_file,
576 .set = pl_cache_set_file,
577 .priv = s->shader_cache,
578 ));
579 if (!s->cache)
580 return AVERROR(ENOMEM);
581 }
582
583 if (s->out_format_string) {
584 s->out_format = av_get_pix_fmt(s->out_format_string);
585 if (s->out_format == AV_PIX_FMT_NONE) {
586 av_log(avctx, AV_LOG_ERROR, "Invalid output format: %s\n",
587 s->out_format_string);
588 return AVERROR(EINVAL);
589 }
590 } else {
591 s->out_format = AV_PIX_FMT_NONE;
592 }
593
594 return 0;
595}
596
597static int append_inputs(AVFilterContext *avctx, int nb_inputs,
598 int (*config_props)(AVFilterLink *link))
599{
600 int err;
601
602 for (int i = 0; i < nb_inputs; i++) {
603 AVFilterPad pad = {
604 .name = av_asprintf("input%d", i),
605 .type = AVMEDIA_TYPE_VIDEO,
606 .config_props = config_props,
607 };
608 if (!pad.name)
609 return AVERROR(ENOMEM);
610 if ((err = ff_append_inpad_free_name(avctx, &pad)) < 0)
611 return err;
612 }
613
614 return 0;
615}
616
617/* Returns the filter's own Vulkan device, if it has one */
619{
620 if (avctx->hw_device_ctx) {
621 const AVHWDeviceContext *avhwctx = (void *) avctx->hw_device_ctx->data;
622 if (avhwctx->type == AV_HWDEVICE_TYPE_VULKAN)
623 return avhwctx->hwctx;
624 }
625 return NULL;
626}
627
629{
630 int err = 0;
631 LibplaceboContext *s = avctx->priv;
632
633 if (s->normalize_sar && s->fit_mode != FIT_FILL) {
634 av_log(avctx, AV_LOG_WARNING, "normalize_sar has no effect when using "
635 "a fit mode other than 'fill'\n");
636 }
637
638 if ((err = init_common(avctx)) < 0) {
639 libplacebo_uninit(avctx);
640 return err;
641 }
642
643 s->opts = pl_options_alloc(s->log);
644 if (!s->opts) {
645 libplacebo_uninit(avctx);
646 return AVERROR(ENOMEM);
647 }
648
649 RET(append_inputs(avctx, s->nb_inputs, &libplacebo_config_input));
650 RET(update_settings(avctx));
651 RET(av_expr_parse(&s->crop_x_pexpr, s->crop_x_expr, var_names,
652 NULL, NULL, NULL, NULL, 0, s));
653 RET(av_expr_parse(&s->crop_y_pexpr, s->crop_y_expr, var_names,
654 NULL, NULL, NULL, NULL, 0, s));
655 RET(av_expr_parse(&s->crop_w_pexpr, s->crop_w_expr, var_names,
656 NULL, NULL, NULL, NULL, 0, s));
657 RET(av_expr_parse(&s->crop_h_pexpr, s->crop_h_expr, var_names,
658 NULL, NULL, NULL, NULL, 0, s));
659 RET(av_expr_parse(&s->pos_x_pexpr, s->pos_x_expr, var_names,
660 NULL, NULL, NULL, NULL, 0, s));
661 RET(av_expr_parse(&s->pos_y_pexpr, s->pos_y_expr, var_names,
662 NULL, NULL, NULL, NULL, 0, s));
663 RET(av_expr_parse(&s->pos_w_pexpr, s->pos_w_expr, var_names,
664 NULL, NULL, NULL, NULL, 0, s));
665 RET(av_expr_parse(&s->pos_h_pexpr, s->pos_h_expr, var_names,
666 NULL, NULL, NULL, NULL, 0, s));
667
668 if (strcmp(s->fps_string, "none") != 0)
669 RET(av_parse_video_rate(&s->fps, s->fps_string));
670
671 /* if we are told to inherit the input link's device, ignore the global
672 * hw_device_ctx even if it would be compatible. This ensure consistent
673 * behaviour when the flag is set. */
674 if (!s->inherit_device) {
675 RET(init_vulkan(avctx, choose_default_device(avctx)));
677 }
678
679 return 0;
680
681fail:
682 return err;
683}
684
685static void lock_queue(void *priv, uint32_t qf, uint32_t qidx)
686{
687 AVHWDeviceContext *avhwctx = priv;
688 const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
689#if FF_API_VULKAN_SYNC_QUEUES
691 hwctx->lock_queue(avhwctx, qf, qidx);
693#endif
694}
695
696static void unlock_queue(void *priv, uint32_t qf, uint32_t qidx)
697{
698 AVHWDeviceContext *avhwctx = priv;
699 const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
700#if FF_API_VULKAN_SYNC_QUEUES
702 hwctx->unlock_queue(avhwctx, qf, qidx);
704#endif
705}
706
707static int input_init(AVFilterContext *avctx, LibplaceboInput *input, int idx)
708{
709 LibplaceboContext *s = avctx->priv;
710
712 if (!input->out_pts)
713 return AVERROR(ENOMEM);
714 input->queue = pl_queue_create(s->gpu);
715 input->renderer = pl_renderer_create(s->log, s->gpu);
716 input->idx = idx;
717
718 return 0;
719}
720
721static void input_uninit(LibplaceboInput *input)
722{
723 pl_renderer_destroy(&input->renderer);
724 pl_queue_destroy(&input->queue);
725 av_fifo_freep2(&input->out_pts);
726}
727
728static int copy_pl_queue(const AVVulkanDeviceContext *hwctx,
730 struct pl_vulkan_queue *pl_qf)
731{
732 pl_qf->index = qf->idx;
733 pl_qf->count = qf->num;
734#if PL_API_VER >= 365
735 pl_qf->flags = hwctx->queue_flags;
736#else
737 if (hwctx->queue_flags != 0)
738 return AVERROR(EINVAL); // prevent undefined behavior
739#endif
740 return 0;
741}
742
743/* Creates or imports the Vulkan device, unless already done */
744static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwctx)
745{
746 int err = 0;
747 LibplaceboContext *s = avctx->priv;
748
749 if (s->gpu)
750 return 0;
751
752 if (hwctx) {
753 struct pl_vulkan_import_params import_params = {
754 .instance = hwctx->inst,
755 .get_proc_addr = hwctx->get_proc_addr,
756 .phys_device = hwctx->phys_dev,
757 .device = hwctx->act_dev,
758 .extensions = hwctx->enabled_dev_extensions,
759 .num_extensions = hwctx->nb_enabled_dev_extensions,
760 .features = &hwctx->device_features,
761 .lock_queue = lock_queue,
762 .unlock_queue = unlock_queue,
763 .queue_ctx = avctx->hw_device_ctx->data,
764 .queue_graphics = { VK_QUEUE_FAMILY_IGNORED },
765 .queue_compute = { VK_QUEUE_FAMILY_IGNORED },
766 .queue_transfer = { VK_QUEUE_FAMILY_IGNORED },
767 /* This is the highest version created by hwcontext_vulkan.c */
768 .max_api_version = VK_API_VERSION_1_3,
769 };
770 for (int i = 0; i < hwctx->nb_qf; i++) {
771 const AVVulkanDeviceQueueFamily *qf = &hwctx->qf[i];
772 if (qf->flags & VK_QUEUE_GRAPHICS_BIT)
773 RET(copy_pl_queue(hwctx, qf, &import_params.queue_graphics));
774 if (qf->flags & VK_QUEUE_COMPUTE_BIT)
775 RET(copy_pl_queue(hwctx, qf, &import_params.queue_compute));
776 if (qf->flags & VK_QUEUE_TRANSFER_BIT)
777 RET(copy_pl_queue(hwctx, qf, &import_params.queue_transfer));
778 }
779
780 /* Import libavfilter vulkan context into libplacebo */
781 s->vulkan = pl_vulkan_import(s->log, &import_params);
782 s->have_hwdevice = 1;
783 } else {
784 s->vulkan = pl_vulkan_create(s->log, pl_vulkan_params(
785 .queue_count = 0, /* enable all queues for parallelization */
786 ));
787 }
788
789 if (!s->vulkan) {
790 av_log(avctx, AV_LOG_ERROR, "Failed %s Vulkan device!\n",
791 hwctx ? "importing" : "creating");
792 err = AVERROR_EXTERNAL;
793 goto fail;
794 }
795
796 s->gpu = s->vulkan->gpu;
797 pl_gpu_set_cache(s->gpu, s->cache);
798 return 0;
799
800fail:
801 return err;
802}
803
804/* Sets up the state depending on the GPU, unless already done */
806{
807 int err = 0;
808 LibplaceboContext *s = avctx->priv;
809 uint8_t *buf = NULL;
810 size_t buf_len;
811
812 if (s->inputs)
813 return 0;
814
815 /* Parse the user shaders, if requested */
816 if (s->shader_bin_len)
817 RET(parse_shader(avctx, s->shader_bin, s->shader_bin_len));
818
819 if (s->shader_path && s->shader_path[0]) {
820 RET(av_file_map(s->shader_path, &buf, &buf_len, 0, s));
821 RET(parse_shader(avctx, buf, buf_len));
822 }
823
824 if (s->lut_filename)
825 RET(parse_custom_lut(avctx));
826
827 /* Initialize inputs */
828 s->inputs = av_calloc(s->nb_inputs, sizeof(*s->inputs));
829 if (!s->inputs)
830 return AVERROR(ENOMEM);
831 for (int i = 0; i < s->nb_inputs; i++)
832 RET(input_init(avctx, &s->inputs[i], i));
833 s->nb_active = s->nb_inputs;
834 s->linear_rr = pl_renderer_create(s->log, s->gpu);
835
836 /* fall through */
837fail:
838 if (buf)
839 av_file_unmap(buf, buf_len);
840 return err;
841}
842
844{
845 LibplaceboContext *s = avctx->priv;
846
847 for (int i = 0; i < FF_ARRAY_ELEMS(s->tex); i++)
848 pl_tex_destroy(s->gpu, &s->tex[i]);
849 for (int i = 0; i < s->num_hooks; i++)
850 pl_mpv_user_shader_destroy(&s->hooks[i]);
851 if (s->inputs) {
852 for (int i = 0; i < s->nb_inputs; i++)
853 input_uninit(&s->inputs[i]);
854 av_freep(&s->inputs);
855 }
856
857 pl_lut_free(&s->lut);
858 pl_cache_destroy(&s->cache);
859 pl_renderer_destroy(&s->linear_rr);
860 pl_tex_destroy(s->gpu, &s->linear_tex);
861 pl_options_free(&s->opts);
862 pl_vulkan_destroy(&s->vulkan);
863 pl_log_destroy(&s->log);
864 ff_vk_uninit(&s->vkctx);
865 s->gpu = NULL;
866
867 av_expr_free(s->crop_x_pexpr);
868 av_expr_free(s->crop_y_pexpr);
869 av_expr_free(s->crop_w_pexpr);
870 av_expr_free(s->crop_h_pexpr);
871 av_expr_free(s->pos_x_pexpr);
872 av_expr_free(s->pos_y_pexpr);
873 av_expr_free(s->pos_w_pexpr);
874 av_expr_free(s->pos_h_pexpr);
875}
876
877static int libplacebo_process_command(AVFilterContext *ctx, const char *cmd,
878 const char *arg, char *res, int res_len,
879 int flags)
880{
881 int err = 0;
882 RET(ff_filter_process_command(ctx, cmd, arg, res, res_len, flags));
884 return 0;
885
886fail:
887 return err;
888}
889
890static const AVFrame *ref_frame(const struct pl_frame_mix *mix)
891{
892 for (int i = 0; i < mix->num_frames; i++) {
893 if (i+1 == mix->num_frames || mix->timestamps[i+1] > 0)
894 return pl_get_mapped_avframe(mix->frames[i]);
895 }
896 return NULL;
897}
898
899static inline double q2d_fallback(AVRational q, const double def)
900{
901 return (q.num && q.den) ? av_q2d(q) : def;
902}
903
905 struct pl_frame *target, double target_pts)
906{
907 FilterLink *outl = ff_filter_link(ctx->outputs[0]);
908 LibplaceboContext *s = ctx->priv;
909 const AVFilterLink *outlink = ctx->outputs[0];
910 const AVFilterLink *inlink = ctx->inputs[in->idx];
911 const AVFrame *ref = ref_frame(&in->mix);
912
913 for (int i = 0; i < in->mix.num_frames; i++) {
914 // Mutate the `pl_frame.crop` fields in-place. This is fine because we
915 // own the entire pl_queue, and hence, the pointed-at frames.
916 struct pl_frame *image = (struct pl_frame *) in->mix.frames[i];
917 const AVFrame *src = pl_get_mapped_avframe(image);
918 double image_pts = TS2T(src->pts, inlink->time_base);
919
920 /* Update dynamic variables */
921 s->var_values[VAR_IN_IDX] = s->var_values[VAR_IDX] = in->idx;
922 s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = inlink->w;
923 s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = inlink->h;
924 s->var_values[VAR_A] = (double) inlink->w / inlink->h;
925 s->var_values[VAR_SAR] = q2d_fallback(inlink->sample_aspect_ratio, 1.0);
926 s->var_values[VAR_IN_T] = s->var_values[VAR_T] = image_pts;
927 s->var_values[VAR_OUT_T] = s->var_values[VAR_OT] = target_pts;
928 s->var_values[VAR_N] = outl->frame_count_out;
929
930 /* Clear these explicitly to avoid leaking previous frames' state */
931 s->var_values[VAR_CROP_W] = s->var_values[VAR_CW] = NAN;
932 s->var_values[VAR_CROP_H] = s->var_values[VAR_CH] = NAN;
933 s->var_values[VAR_POS_W] = s->var_values[VAR_PW] = NAN;
934 s->var_values[VAR_POS_H] = s->var_values[VAR_PH] = NAN;
935
936 /* Compute dimensions first and placement second */
937 s->var_values[VAR_CROP_W] = s->var_values[VAR_CW] =
938 av_expr_eval(s->crop_w_pexpr, s->var_values, NULL);
939 s->var_values[VAR_CROP_H] = s->var_values[VAR_CH] =
940 av_expr_eval(s->crop_h_pexpr, s->var_values, NULL);
941 s->var_values[VAR_CROP_W] = s->var_values[VAR_CW] =
942 av_expr_eval(s->crop_w_pexpr, s->var_values, NULL);
943 s->var_values[VAR_POS_W] = s->var_values[VAR_PW] =
944 av_expr_eval(s->pos_w_pexpr, s->var_values, NULL);
945 s->var_values[VAR_POS_H] = s->var_values[VAR_PH] =
946 av_expr_eval(s->pos_h_pexpr, s->var_values, NULL);
947 s->var_values[VAR_POS_W] = s->var_values[VAR_PW] =
948 av_expr_eval(s->pos_w_pexpr, s->var_values, NULL);
949
950 image->crop.x0 = av_expr_eval(s->crop_x_pexpr, s->var_values, NULL);
951 image->crop.y0 = av_expr_eval(s->crop_y_pexpr, s->var_values, NULL);
952 image->crop.x1 = image->crop.x0 + s->var_values[VAR_CROP_W];
953 image->crop.y1 = image->crop.y0 + s->var_values[VAR_CROP_H];
954
955 const pl_rect2df crop_orig = image->crop;
956 pl_rotation rot_total = PL_ROTATION_360 + image->rotation - target->rotation;
957 if (rot_total % PL_ROTATION_180 == PL_ROTATION_90) {
958 /* Libplacebo expects the input crop relative to the actual frame
959 * dimensions, so un-transpose them here */
960 FFSWAP(float, image->crop.x0, image->crop.y0);
961 FFSWAP(float, image->crop.x1, image->crop.y1);
962 }
963
964 if (src == ref) {
965 /* Only update the target crop once, for the 'reference' frame */
966 target->crop.x0 = av_expr_eval(s->pos_x_pexpr, s->var_values, NULL);
967 target->crop.y0 = av_expr_eval(s->pos_y_pexpr, s->var_values, NULL);
968 target->crop.x1 = target->crop.x0 + s->var_values[VAR_POS_W];
969 target->crop.y1 = target->crop.y0 + s->var_values[VAR_POS_H];
970
971 /* Effective visual crop */
972 double sar_in = q2d_fallback(inlink->sample_aspect_ratio, 1.0);
973 double sar_out = q2d_fallback(outlink->sample_aspect_ratio, 1.0);
974 if (rot_total % PL_ROTATION_180 == PL_ROTATION_90)
975 sar_in = 1.0 / sar_in;
976
977 pl_rect2df fixed = crop_orig;
978 pl_rect2df_stretch(&fixed, sar_in / sar_out, 1.0);
979
980 switch (s->fit_mode) {
981 case FIT_FILL:
982 if (s->normalize_sar)
983 pl_rect2df_aspect_copy(&target->crop, &fixed, s->pad_crop_ratio);
984 break;
985 case FIT_CONTAIN:
986 pl_rect2df_aspect_copy(&target->crop, &fixed, 0.0);
987 break;
988 case FIT_COVER:
989 pl_rect2df_aspect_copy(&target->crop, &fixed, 1.0);
990 break;
991 case FIT_NONE: {
992 const float sx = fabsf(pl_rect_w(fixed)) / pl_rect_w(target->crop);
993 const float sy = fabsf(pl_rect_h(fixed)) / pl_rect_h(target->crop);
994 pl_rect2df_stretch(&target->crop, sx, sy);
995 break;
996 }
997 case FIT_SCALE_DOWN:
998 pl_rect2df_aspect_fit(&target->crop, &fixed, 0.0);
999 }
1000 }
1001 }
1002}
1003
1004static int map_output(LibplaceboContext *s, AVFrame *out, struct pl_frame *target)
1005{
1007 bool ok;
1008
1009 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1010 ok = pl_map_avframe_ex(s->gpu, target, pl_avframe_params(
1011 .frame = out,
1012 .map_dovi = false,
1013 ));
1014 } else {
1015 ok = pl_frame_recreate_from_avframe(s->gpu, target, s->tex, out);
1016 }
1017
1018 return ok ? 0 : AVERROR_EXTERNAL;
1019}
1020
1021static int unmap_output(LibplaceboContext *s, struct pl_frame *target, AVFrame *out)
1022{
1024
1025 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1026 pl_unmap_avframe(s->gpu, target);
1027 } else if (!pl_download_avframe(s->gpu, target, out)) {
1028 return AVERROR_EXTERNAL;
1029 }
1030
1031 return 0;
1032}
1033
1035 AVFrame *out, const AVFrame *ref, bool progressive)
1036{
1037 int err, changed = 0;
1038
1039 if (ref) {
1040 if ((err = av_frame_copy_props(out, ref)) < 0)
1041 return err;
1042
1044 /* Output of dovi reshaping is always BT.2020+PQ, so infer the correct
1045 * output colorspace defaults */
1046 out->color_primaries = AVCOL_PRI_BT2020;
1047 out->color_trc = AVCOL_TRC_SMPTE2084;
1049 }
1050 }
1051
1052 out->width = outlink->w;
1053 out->height = outlink->h;
1054 out->sample_aspect_ratio = outlink->sample_aspect_ratio;
1055 out->colorspace = outlink->colorspace;
1056 out->color_range = outlink->color_range;
1057 out->alpha_mode = outlink->alpha_mode;
1059 out->chroma_location = outlink->chroma_location;
1060 if (s->color_trc >= 0)
1061 out->color_trc = s->color_trc;
1062 if (s->color_primaries >= 0)
1063 out->color_primaries = s->color_primaries;
1064
1065 if (!ref)
1066 return 0;
1067
1068 /* Strip side data if no longer relevant */
1069 if (out->width != ref->width || out->height != ref->height)
1071 if (ref->color_trc != out->color_trc || ref->color_primaries != out->color_primaries)
1073 av_frame_side_data_remove_by_props(&out->side_data, &out->nb_side_data, changed);
1074
1075 if (s->apply_filmgrain)
1077
1078 if (progressive)
1080
1081 return 0;
1082}
1083
1084/* Construct and emit an output frame for a given timestamp */
1086{
1087 int err = 0;
1088 LibplaceboContext *s = ctx->priv;
1089 pl_options opts = s->opts;
1090 AVFilterLink *outlink = ctx->outputs[0];
1091 const double target_pts = TS2T(pts, outlink->time_base);
1092 struct pl_frame target;
1093 const AVFrame *ref = NULL;
1094 AVFrame *out;
1095
1096 /* Count the number of visible inputs, by excluding frames which are fully
1097 * obscured or which have no frames in the mix */
1098 int idx_start = 0, nb_visible = 0;
1099 for (int i = 0; i < s->nb_inputs; i++) {
1100 LibplaceboInput *in = &s->inputs[i];
1101 struct pl_frame dummy;
1102 if (in->qstatus != PL_QUEUE_OK || !in->mix.num_frames)
1103 continue;
1104 const struct pl_frame *cur = pl_frame_mix_nearest(&in->mix);
1105 av_assert1(cur);
1106 update_crops(ctx, in, &dummy, target_pts);
1107 const int x0 = roundf(FFMIN(dummy.crop.x0, dummy.crop.x1)),
1108 y0 = roundf(FFMIN(dummy.crop.y0, dummy.crop.y1)),
1109 x1 = roundf(FFMAX(dummy.crop.x0, dummy.crop.x1)),
1110 y1 = roundf(FFMAX(dummy.crop.y0, dummy.crop.y1));
1111
1112 /* If an opaque frame covers entire the output, disregard all lower layers */
1113 const bool cropped = x0 > 0 || y0 > 0 || x1 < outlink->w || y1 < outlink->h;
1114 if (!cropped && cur->repr.alpha == PL_ALPHA_NONE) {
1115 idx_start = i;
1116 nb_visible = 0;
1117 ref = NULL;
1118 }
1119 /* Use first visible input as overall reference */
1120 if (!ref)
1121 ref = ref_frame(&in->mix);
1122 nb_visible++;
1123 }
1124
1125 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1126 if (!out)
1127 return AVERROR(ENOMEM);
1128
1129 if (!ref)
1130 goto props_done;
1131
1132 RET(set_output_props(s, outlink, out, ref, s->deinterlace));
1133
1134props_done:
1135 out->pts = pts;
1136 if (s->fps.num)
1137 out->duration = 1;
1138
1139 /* Map, render and unmap output frame */
1140 RET(map_output(s, out, &target));
1141
1142 struct pl_frame orig_target = target;
1143 bool use_linear_compositor = false;
1144 if (s->linear_tex && target.color.transfer != PL_COLOR_TRC_LINEAR &&
1145 !s->disable_linear && nb_visible > 1) {
1146 target = (struct pl_frame) {
1147 .num_planes = 1,
1148 .planes[0] = {
1149 .components = 4,
1150 .component_mapping = {0, 1, 2, 3},
1151 .texture = s->linear_tex,
1152 },
1153 .repr = pl_color_repr_rgb,
1154 .color = orig_target.color,
1155 .rotation = orig_target.rotation,
1156 };
1157 target.repr.alpha = PL_ALPHA_PREMULTIPLIED;
1158 target.color.transfer = PL_COLOR_TRC_LINEAR;
1159 use_linear_compositor = true;
1160 }
1161
1162 /* Draw first frame opaque, others with blending */
1163 struct pl_render_params tmp_params = opts->params;
1164 for (int i = 0; i < s->nb_inputs; i++) {
1165 LibplaceboInput *in = &s->inputs[i];
1166 if (!in->renderer)
1167 continue; /* input was already freed */
1168 FilterLink *il = ff_filter_link(ctx->inputs[i]);
1169 FilterLink *ol = ff_filter_link(outlink);
1170 int high_fps = av_cmp_q(il->frame_rate, ol->frame_rate) >= 0;
1171 if (in->qstatus != PL_QUEUE_OK || !in->mix.num_frames || i < idx_start) {
1172 pl_renderer_flush_cache(in->renderer);
1173 continue;
1174 }
1175 tmp_params.skip_caching_single_frame = high_fps;
1176 update_crops(ctx, in, &target, target_pts);
1177 pl_render_image_mix(in->renderer, &in->mix, &target, &tmp_params);
1178
1179 /* Force straight output and set correct blend operator. This is
1180 * required to get correct blending onto YUV target buffers. */
1181 target.repr.alpha = PL_ALPHA_INDEPENDENT;
1182 tmp_params.blend_params = &pl_alpha_overlay;
1183 tmp_params.background = tmp_params.border = PL_CLEAR_SKIP;
1184 }
1185
1186 if (use_linear_compositor) {
1187 /* Blit the linear intermediate image to the output frame */
1188 target.crop = orig_target.crop = (struct pl_rect2df) {0};
1189 target.repr.alpha = PL_ALPHA_PREMULTIPLIED;
1190 pl_render_image(s->linear_rr, &target, &orig_target, &opts->params);
1191 target = orig_target;
1192 } else if (!ref) {
1193 /* Render an empty image to clear the frame to the desired fill color */
1194 pl_render_image(s->linear_rr, NULL, &target, &opts->params);
1195 }
1196
1197 RET(unmap_output(s, &target, out));
1198 return ff_filter_frame(outlink, out);
1199
1200fail:
1202 return err;
1203}
1204
1205static bool map_frame(pl_gpu gpu, pl_tex *tex,
1206 const struct pl_source_frame *src,
1207 struct pl_frame *out)
1208{
1209 AVFrame *avframe = src->frame_data;
1210 LibplaceboContext *s = avframe->opaque;
1211 bool ok = pl_map_avframe_ex(gpu, out, pl_avframe_params(
1212 .frame = avframe,
1213 .tex = tex,
1214 .map_dovi = s->apply_dovi,
1215 ));
1216 out->lut = s->lut;
1217 out->lut_type = s->lut_type;
1218 out->rotation += s->rotation;
1219
1220 if (!s->apply_filmgrain)
1221 out->film_grain.type = PL_FILM_GRAIN_NONE;
1222
1223 av_frame_free(&avframe);
1224 return ok;
1225}
1226
1227static void unmap_frame(pl_gpu gpu, struct pl_frame *frame,
1228 const struct pl_source_frame *src)
1229{
1230 pl_unmap_avframe(gpu, frame);
1231}
1232
1233static void discard_frame(const struct pl_source_frame *src)
1234{
1235 AVFrame *avframe = src->frame_data;
1236 av_frame_free(&avframe);
1237}
1238
1239/* Push an input frame to the queue, returning its first field */
1240static enum pl_field push_frame(LibplaceboContext *s, pl_queue queue,
1241 const AVFilterLink *inlink, AVFrame *in,
1242 bool deinterlace)
1243{
1244 struct pl_source_frame src = {
1245 .pts = TS2T(in->pts, inlink->time_base),
1246 .duration = TS2T(in->duration, inlink->time_base),
1247 .first_field = deinterlace ? pl_field_from_avframe(in) : PL_FIELD_NONE,
1248 .frame_data = in,
1249 .map = map_frame,
1250 .unmap = unmap_frame,
1251 .discard = discard_frame,
1252 };
1253
1254 /* Input without a chroma location is assumed to be sited like the output. */
1256 in->chroma_location = inlink->dst->outputs[0]->chroma_location;
1257
1258 in->opaque = s;
1259 pl_queue_push(queue, &src);
1260 return src.first_field;
1261}
1262
1264{
1265 int ret, status;
1266 LibplaceboContext *s = ctx->priv;
1267 AVFilterLink *outlink = ctx->outputs[0];
1268 AVFilterLink *inlink = ctx->inputs[input->idx];
1269 AVFrame *in;
1270 int64_t pts;
1271
1272 while ((ret = ff_inlink_consume_frame(inlink, &in)) > 0) {
1273 const enum pl_field field = push_frame(s, input->queue, inlink, in,
1274 s->deinterlace);
1275
1276 if (!s->fps.num) {
1277 /* Internally queue an output frame for the same PTS */
1278 pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base);
1279 av_fifo_write(input->out_pts, &pts, 1);
1280
1281 if (s->send_fields && field != PL_FIELD_NONE) {
1282 /* Queue the second field for interlaced content */
1283 pts += av_rescale_q(in->duration, inlink->time_base, outlink->time_base) / 2;
1284 av_fifo_write(input->out_pts, &pts, 1);
1285 }
1286 }
1287 }
1288
1289 if (ret < 0)
1290 return ret;
1291
1292 if (!input->status && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
1293 pts = av_rescale_q_rnd(pts, inlink->time_base, outlink->time_base,
1294 AV_ROUND_UP);
1295 pl_queue_push(input->queue, NULL); /* Signal EOF to pl_queue */
1296 input->status = status;
1297 input->status_pts = pts;
1298 s->nb_active--;
1299 }
1300
1301 return 0;
1302}
1303
1305{
1306 int64_t pts;
1307 while (av_fifo_peek(in->out_pts, &pts, 1, 0) >= 0 && pts <= until)
1308 av_fifo_drain2(in->out_pts, 1);
1309}
1310
1312{
1313 int ret, ok = 0, retry = 0;
1314 LibplaceboContext *s = ctx->priv;
1315 AVFilterLink *outlink = ctx->outputs[0];
1316 FilterLink *outl = ff_filter_link(outlink);
1317 int64_t pts, out_pts;
1318
1320 pl_log_level_update(s->log, get_log_level());
1321
1322 for (int i = 0; i < s->nb_inputs; i++) {
1323 if ((ret = handle_input(ctx, &s->inputs[i])) < 0)
1324 return ret;
1325 }
1326
1327 if (ff_outlink_frame_wanted(outlink)) {
1328 if (s->fps.num) {
1329 out_pts = outl->frame_count_out;
1330 } else {
1331 /* Determine the PTS of the next frame from any active input */
1332 out_pts = INT64_MAX;
1333 for (int i = 0; i < s->nb_inputs; i++) {
1334 LibplaceboInput *in = &s->inputs[i];
1335 if (av_fifo_peek(in->out_pts, &pts, 1, 0) >= 0) {
1336 out_pts = FFMIN(out_pts, pts);
1337 } else if (!in->status) {
1338 ff_inlink_request_frame(ctx->inputs[i]);
1339 retry = true;
1340 }
1341 }
1342
1343 if (retry) /* some inputs are incomplete */
1344 return 0;
1345 }
1346
1347 /* Update all input queues to the chosen out_pts */
1348 for (int i = 0; i < s->nb_inputs; i++) {
1349 LibplaceboInput *in = &s->inputs[i];
1350 FilterLink *l = ff_filter_link(outlink);
1351 if (in->status && out_pts >= in->status_pts) {
1352 /* Free up resources which will never be needed again */
1353 pl_renderer_destroy(&in->renderer);
1354 pl_queue_destroy(&in->queue);
1355 in->qstatus = PL_QUEUE_EOF;
1356 continue;
1357 }
1358
1359 in->qstatus = pl_queue_update(in->queue, &in->mix, pl_queue_params(
1360 .pts = TS2T(out_pts, outlink->time_base),
1361 .radius = pl_frame_mix_radius(&s->opts->params),
1362 .vsync_duration = q2d_fallback(av_inv_q(l->frame_rate), 0.0),
1363 ));
1364
1365 switch (in->qstatus) {
1366 case PL_QUEUE_MORE:
1367 ff_inlink_request_frame(ctx->inputs[i]);
1368 retry = true;
1369 break;
1370 case PL_QUEUE_OK:
1371 ok |= in->mix.num_frames > 0;
1372 break;
1373 case PL_QUEUE_ERR:
1374 return AVERROR_EXTERNAL;
1375 }
1376 }
1377
1378 /* In constant FPS mode, we can also output an empty frame if there is
1379 * a gap in the input timeline and we still have active streams */
1380 ok |= s->fps.num && s->nb_active > 0;
1381
1382 if (retry) {
1383 return 0;
1384 } else if (ok) {
1385 /* Got any valid frame mixes, drain PTS queue and render output */
1386 for (int i = 0; i < s->nb_inputs; i++)
1387 drain_input_pts(&s->inputs[i], out_pts);
1388 return output_frame(ctx, out_pts);
1389 } else if (s->nb_active == 0) {
1390 /* Forward most recent status, but don't let a later EOF mask errors */
1391 int status = s->inputs[0].status;
1392 int64_t status_pts = s->inputs[0].status_pts;
1393 for (int i = 1; i < s->nb_inputs; i++) {
1394 const LibplaceboInput *in = &s->inputs[i];
1395 if (in->status_pts > status_pts) {
1396 status_pts = in->status_pts;
1397 if (in->status != AVERROR_EOF || status == AVERROR_EOF)
1398 status = in->status;
1399 } else if (status == AVERROR_EOF) {
1400 status = in->status;
1401 }
1402 }
1403 ff_outlink_set_status(outlink, status, status_pts);
1404 return 0;
1405 }
1406
1407 return AVERROR_BUG;
1408 }
1409
1410 return FFERROR_NOT_READY;
1411}
1412
1414 AVFilterFormatsConfig **cfg_in,
1415 AVFilterFormatsConfig **cfg_out)
1416{
1417 int err;
1418 const LibplaceboContext *s = ctx->priv;
1419 const AVPixFmtDescriptor *desc = NULL;
1420 AVFilterFormats *infmts = NULL, *outfmts = NULL;
1421
1422 if (!s->gpu) {
1423 /* Device deferred to config_input (inherit_device): we have no GPU yet
1424 * to enumerate software formats against, and this mode requires Vulkan
1425 * input, so advertise Vulkan only. */
1427 if (s->out_format == AV_PIX_FMT_NONE || av_vkfmt_from_pixfmt(s->out_format))
1429 goto done;
1430 }
1431
1432 /* List AV_PIX_FMT_VULKAN first to prefer it when possible */
1433 if (s->have_hwdevice) {
1435 if (s->out_format == AV_PIX_FMT_NONE || av_vkfmt_from_pixfmt(s->out_format))
1437 }
1438
1439 while ((desc = av_pix_fmt_desc_next(desc))) {
1442 continue; /* Handled above */
1443
1444 if (!pl_test_pixfmt(s->gpu, pixfmt))
1445 continue;
1446
1447 RET(ff_add_format(&infmts, pixfmt));
1448
1449 /* Filter for supported output pixel formats */
1450 if (desc->flags & AV_PIX_FMT_FLAG_BE)
1451 continue; /* BE formats are not supported by pl_download_avframe */
1452
1453 /* Mask based on user specified format */
1454 if (pixfmt != s->out_format && s->out_format != AV_PIX_FMT_NONE)
1455 continue;
1456
1457 if (!pl_test_pixfmt_caps(s->gpu, pixfmt, PL_FMT_CAP_RENDERABLE))
1458 continue;
1459
1460 RET(ff_add_format(&outfmts, pixfmt));
1461 }
1462
1463done:
1464
1465 if (!infmts || !outfmts) {
1466 err = AVERROR(EINVAL);
1467 goto fail;
1468 }
1469
1470 for (int i = 0; i < s->nb_inputs; i++) {
1471 if (i > 0) {
1472 /* Duplicate the format list for each subsequent input */
1473 infmts = NULL;
1474 for (int n = 0; n < cfg_in[0]->formats->nb_formats; n++)
1475 RET(ff_add_format(&infmts, cfg_in[0]->formats->formats[n]));
1476 }
1477 RET(ff_formats_ref(infmts, &cfg_in[i]->formats));
1478 RET(ff_formats_ref(ff_all_color_spaces(), &cfg_in[i]->color_spaces));
1479 RET(ff_formats_ref(ff_all_color_ranges(), &cfg_in[i]->color_ranges));
1480 RET(ff_formats_ref(ff_all_alpha_modes(), &cfg_in[i]->alpha_modes));
1481 RET(ff_formats_ref(ff_all_chroma_locations(), &cfg_in[i]->chroma_locations));
1482 }
1483
1484 RET(ff_formats_ref(outfmts, &cfg_out[0]->formats));
1485
1486 outfmts = s->colorspace >= 0 ? ff_make_formats_list_singleton(s->colorspace)
1488 RET(ff_formats_ref(outfmts, &cfg_out[0]->color_spaces));
1489
1490 outfmts = s->color_range >= 0 ? ff_make_formats_list_singleton(s->color_range)
1492 RET(ff_formats_ref(outfmts, &cfg_out[0]->color_ranges));
1493
1494 outfmts = s->alpha_mode >= 0 ? ff_make_formats_list_singleton(s->alpha_mode)
1496 RET(ff_formats_ref(outfmts, &cfg_out[0]->alpha_modes));
1497
1498 outfmts = s->chroma_location >= 0 ? ff_make_formats_list_singleton(s->chroma_location)
1500 RET(ff_formats_ref(outfmts, &cfg_out[0]->chroma_locations));
1501 return 0;
1502
1503fail:
1504 if (infmts && !infmts->refcount)
1505 ff_formats_unref(&infmts);
1506 if (outfmts && !outfmts->refcount)
1507 ff_formats_unref(&outfmts);
1508 return err;
1509}
1510
1511/* Adopt the Vulkan device of the input's hardware frames, unless a device was
1512 * already set up. query_formats advertised Vulkan only, so a non-Vulkan input
1513 * here is a configuration error. */
1514static int inherit_device(AVFilterLink *inlink)
1515{
1516 AVFilterContext *avctx = inlink->dst;
1517 LibplaceboContext *s = avctx->priv;
1518 FilterLink *l = ff_filter_link(inlink);
1519 AVHWFramesContext *hwfc;
1520
1521 if (s->gpu)
1522 return 0;
1523
1524 if (inlink->format != AV_PIX_FMT_VULKAN || !l->hw_frames_ctx) {
1525 av_log(avctx, AV_LOG_ERROR, "inherit_device requires a Vulkan "
1526 "hardware frames context on the input.\n");
1527 return AVERROR(EINVAL);
1528 }
1529 hwfc = (AVHWFramesContext *) l->hw_frames_ctx->data;
1531 avctx->hw_device_ctx = av_buffer_ref(hwfc->device_ref);
1532 if (!avctx->hw_device_ctx)
1533 return AVERROR(ENOMEM);
1534 return 0;
1535}
1536
1538{
1539 LibplaceboContext *s = inlink->dst->priv;
1540
1541 if (inlink->format == AV_PIX_FMT_VULKAN)
1542 return ff_vk_filter_config_input(inlink);
1543
1544 /* Forward this to the vkctx for format selection */
1545 s->vkctx.input_format = inlink->format;
1546
1547 return 0;
1548}
1549
1551{
1552 int err;
1553 AVFilterContext *avctx = inlink->dst;
1554 LibplaceboContext *s = avctx->priv;
1555
1556 if (s->rotation % PL_ROTATION_180 == PL_ROTATION_90) {
1557 /* Swap width and height for 90 degree rotations to make the size and
1558 * scaling calculations work out correctly */
1559 FFSWAP(int, inlink->w, inlink->h);
1560 if (inlink->sample_aspect_ratio.num)
1562 }
1563
1564 if (s->inherit_device)
1565 RET(inherit_device(inlink));
1566 RET(init_vulkan(avctx, choose_default_device(avctx)));
1567 RET(libplacebo_setup_gpu(avctx));
1568 return config_input_vulkan(inlink);
1569
1570fail:
1571 return err;
1572}
1573
1575{
1576 return av_cmp_q(a, b) < 0 ? b : a;
1577}
1578
1580{
1581 int err;
1582 FilterLink *l = ff_filter_link(outlink);
1583 LibplaceboContext *s = outlink->src->priv;
1584 AVHWFramesContext *hwfc;
1586
1587 if (outlink->format != AV_PIX_FMT_VULKAN)
1588 return 0;
1589
1590 s->vkctx.output_width = outlink->w;
1591 s->vkctx.output_height = outlink->h;
1592 /* Default to reusing the input format */
1593 if (s->out_format == AV_PIX_FMT_NONE || s->out_format == AV_PIX_FMT_VULKAN) {
1594 s->vkctx.output_format = s->vkctx.input_format;
1595 } else {
1596 s->vkctx.output_format = s->out_format;
1597 }
1598 if ((err = ff_vk_filter_config_output(outlink)) < 0)
1599 return err;
1600 hwfc = (AVHWFramesContext *)l->hw_frames_ctx->data;
1601 vkfc = hwfc->hwctx;
1602 vkfc->usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1603 return 0;
1604}
1605
1607{
1608 int err;
1609 AVFilterContext *avctx = outlink->src;
1610 LibplaceboContext *s = avctx->priv;
1611 AVFilterLink *inlink = outlink->src->inputs[0];
1612 FilterLink *ol = ff_filter_link(outlink);
1614 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
1615
1616 /* Frame dimensions */
1617 RET(ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
1618 &outlink->w, &outlink->h));
1619
1620 s->reset_sar |= s->normalize_sar || s->nb_inputs > 1;
1621 double sar_in = q2d_fallback(inlink->sample_aspect_ratio, 1.0);
1622
1623 int force_oar = s->force_original_aspect_ratio;
1624 if (!force_oar && s->fit_sense == FIT_CONSTRAINT) {
1625 if (s->fit_mode == FIT_CONTAIN || s->fit_mode == FIT_SCALE_DOWN) {
1626 force_oar = SCALE_FORCE_OAR_DECREASE;
1627 } else if (s->fit_mode == FIT_COVER) {
1628 force_oar = SCALE_FORCE_OAR_INCREASE;
1629 }
1630 }
1631
1632 RET(ff_scale_adjust_dimensions(inlink, &outlink->w, &outlink->h,
1633 force_oar, s->force_divisible_by,
1634 s->reset_sar ? sar_in : 1.0));
1635
1636 if (s->fit_mode == FIT_SCALE_DOWN && s->fit_sense == FIT_CONSTRAINT) {
1637 int w_adj = s->reset_sar ? sar_in * inlink->w : inlink->w;
1638 outlink->w = FFMIN(outlink->w, w_adj);
1639 outlink->h = FFMIN(outlink->h, inlink->h);
1640 }
1641
1642 if (s->nb_inputs > 1 && !s->disable_fbos) {
1643 /* Create a separate renderer and composition texture */
1644 const enum pl_fmt_caps caps = PL_FMT_CAP_BLENDABLE | PL_FMT_CAP_BLITTABLE;
1645 pl_fmt fmt = pl_find_fmt(s->gpu, PL_FMT_FLOAT, 4, 16, 0, caps);
1646 bool ok = !!fmt;
1647 if (ok) {
1648 ok = pl_tex_recreate(s->gpu, &s->linear_tex, pl_tex_params(
1649 .format = fmt,
1650 .w = outlink->w,
1651 .h = outlink->h,
1652 .blit_dst = true,
1653 .renderable = true,
1654 .sampleable = true,
1655 .storable = fmt->caps & PL_FMT_CAP_STORABLE,
1656 ));
1657 }
1658
1659 if (!ok) {
1660 av_log(avctx, AV_LOG_WARNING, "Failed to create a linear texture "
1661 "for compositing multiple inputs, falling back to non-linear "
1662 "blending.\n");
1663 }
1664 }
1665
1666 if (s->reset_sar) {
1667 /* SAR is normalized, or we have multiple inputs, set out to 1:1 */
1668 outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
1669 } else if (inlink->sample_aspect_ratio.num && s->fit_mode == FIT_FILL) {
1670 /* This is consistent with other scale_* filters, which only
1671 * set the outlink SAR to be equal to the scale SAR iff the input SAR
1672 * was set to something nonzero */
1673 const AVRational ar_in = { inlink->w, inlink->h };
1674 const AVRational ar_out = { outlink->w, outlink->h };
1675 const AVRational stretch = av_div_q(ar_in, ar_out);
1676 outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio, stretch);
1677 } else {
1678 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
1679 }
1680
1681 if (s->out_sar.num)
1682 outlink->sample_aspect_ratio = s->out_sar;
1683
1684 /* Frame rate */
1685 if (s->fps.num) {
1686 ol->frame_rate = s->fps;
1687 outlink->time_base = av_inv_q(s->fps);
1688 } else {
1689 FilterLink *il = ff_filter_link(avctx->inputs[0]);
1690 ol->frame_rate = il->frame_rate;
1691 outlink->time_base = avctx->inputs[0]->time_base;
1692 for (int i = 1; i < s->nb_inputs; i++) {
1693 il = ff_filter_link(avctx->inputs[i]);
1694 ol->frame_rate = max_q(ol->frame_rate, il->frame_rate);
1695 outlink->time_base = av_gcd_q(outlink->time_base,
1696 avctx->inputs[i]->time_base,
1698 }
1699
1700 if (s->deinterlace && s->send_fields) {
1701 const AVRational q2 = { 2, 1 };
1702 ol->frame_rate = av_mul_q(ol->frame_rate, q2);
1703 /* Ensure output frame timestamps are divisible by two */
1704 outlink->time_base = av_div_q(outlink->time_base, q2);
1705 }
1706 }
1707
1708 /* Static variables */
1709 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = outlink->w;
1710 s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = outlink->h;
1711 s->var_values[VAR_DAR] = q2d_fallback(outlink->sample_aspect_ratio, 1.0);
1712 s->var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
1713 s->var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
1714 s->var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
1715 s->var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
1716
1717 return config_output_vulkan(outlink);
1718
1719fail:
1720 return err;
1721}
1722
1723#define OFFSET(x) offsetof(LibplaceboContext, x)
1724#define STATIC (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1725#define DYNAMIC (STATIC | AV_OPT_FLAG_RUNTIME_PARAM)
1726
1727#define OPTS_COMMON \
1728 { "format", "Output video format", OFFSET(out_format_string), AV_OPT_TYPE_STRING, .flags = STATIC }, \
1729 { "sar", "Output sample aspect ratio (0 for automatic)", OFFSET(out_sar), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, INT_MAX, STATIC }, \
1730 { "inherit_device", "Inherit the Vulkan device from the input's hardware frames context", \
1731 OFFSET(inherit_device), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags = STATIC }, \
1732 { "shader_cache", "Set shader cache path", OFFSET(shader_cache), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = STATIC }, \
1733 { "apply_filmgrain", "Apply film grain metadata", OFFSET(apply_filmgrain), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DYNAMIC }, \
1734 { "apply_dolbyvision", "Apply Dolby Vision metadata", OFFSET(apply_dovi), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DYNAMIC }, \
1735 \
1736 {"colorspace", "select colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_NB-1, STATIC, .unit = "colorspace"}, \
1737 {"auto", "use the negotiated colorspace", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1738 {"gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1739 {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1740 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1741 {"bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1742 {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1743 {"smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1744 {"ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1745 {"bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1746 {"bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1747 {"ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, STATIC, .unit = "colorspace"}, \
1748 \
1749 {"range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_RANGE_NB-1, STATIC, .unit = "range"}, \
1750 {"auto", "use the negotiated color range", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, STATIC, .unit = "range"}, \
1751 {"unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, STATIC, .unit = "range"}, \
1752 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, STATIC, .unit = "range"}, \
1753 {"limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, STATIC, .unit = "range"}, \
1754 {"tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, STATIC, .unit = "range"}, \
1755 {"mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, STATIC, .unit = "range"}, \
1756 {"full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, STATIC, .unit = "range"}, \
1757 {"pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, STATIC, .unit = "range"}, \
1758 {"jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, STATIC, .unit = "range"}, \
1759 \
1760 {"color_primaries", "select color primaries", OFFSET(color_primaries), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_PRI_EXT_NB-1, DYNAMIC, .unit = "color_primaries"}, \
1761 {"auto", "keep the same color primaries", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1762 {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT709}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1763 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_UNSPECIFIED}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1764 {"bt470m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT470M}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1765 {"bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT470BG}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1766 {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE170M}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1767 {"smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE240M}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1768 {"film", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_FILM}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1769 {"bt2020", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT2020}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1770 {"smpte428", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE428}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1771 {"smpte431", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE431}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1772 {"smpte432", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE432}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1773 {"jedec-p22", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_JEDEC_P22}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1774 {"ebu3213", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_EBU3213}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1775 {"vgamut", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_V_GAMUT}, INT_MIN, INT_MAX, STATIC, .unit = "color_primaries"}, \
1776 \
1777 {"color_trc", "select color transfer", OFFSET(color_trc), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_TRC_EXT_NB-1, DYNAMIC, .unit = "color_trc"}, \
1778 {"auto", "keep the same color transfer", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1779 {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT709}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1780 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_UNSPECIFIED}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1781 {"gamma22", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_GAMMA22}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1782 {"bt470m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_GAMMA22}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1783 {"gamma28", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_GAMMA28}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1784 {"bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_GAMMA28}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1785 {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_SMPTE170M}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1786 {"smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_SMPTE240M}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1787 {"linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_LINEAR}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1788 {"iec61966-2-4", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_IEC61966_2_4}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1789 {"bt1361e", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT1361_ECG}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1790 {"iec61966-2-1", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_IEC61966_2_1}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1791 {"bt2020-10", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT2020_10}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1792 {"bt2020-12", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT2020_12}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1793 {"smpte2084", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_SMPTE2084}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1794 {"arib-std-b67", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_ARIB_STD_B67}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1795 {"vlog", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_V_LOG}, INT_MIN, INT_MAX, STATIC, .unit = "color_trc"}, \
1796 \
1797 {"chroma_location", "select chroma location", OFFSET(chroma_location), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCHROMA_LOC_NB-1, STATIC, .unit = "chroma_location"}, \
1798 {"auto", "use the negotiated chroma location", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, STATIC, .unit = "chroma_location"}, \
1799 {"unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, STATIC, .unit = "chroma_location"}, \
1800 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, STATIC, .unit = "chroma_location"}, \
1801 {"left", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_LEFT}, 0, 0, STATIC, .unit = "chroma_location"}, \
1802 {"center", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_CENTER}, 0, 0, STATIC, .unit = "chroma_location"}, \
1803 {"topleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOPLEFT}, 0, 0, STATIC, .unit = "chroma_location"}, \
1804 {"top", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOP}, 0, 0, STATIC, .unit = "chroma_location"}, \
1805 {"bottomleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOMLEFT}, 0, 0, STATIC, .unit = "chroma_location"}, \
1806 {"bottom", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOM}, 0, 0, STATIC, .unit = "chroma_location"}, \
1807 \
1808 {"alpha_mode", "select alpha moda", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVALPHA_MODE_NB-1, STATIC, .unit = "alpha_mode"}, \
1809 {"auto", "use the negotiated alpha mode", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, STATIC, .unit = "alpha_mode"}, \
1810 {"unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, STATIC, .unit = "alpha_mode"}, \
1811 {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, STATIC, .unit = "alpha_mode"}, \
1812 {"premultiplied", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_PREMULTIPLIED}, 0, 0, STATIC, .unit = "alpha_mode"}, \
1813 {"straight", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_STRAIGHT}, 0, 0, STATIC, .unit = "alpha_mode"},
1814
1816 { "inputs", "Number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags = STATIC },
1817 { "w", "Output video frame width", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = STATIC },
1818 { "h", "Output video frame height", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = STATIC },
1819 { "fps", "Output video frame rate", OFFSET(fps_string), AV_OPT_TYPE_STRING, {.str = "none"}, .flags = STATIC },
1820 { "crop_x", "Input video crop x", OFFSET(crop_x_expr), AV_OPT_TYPE_STRING, {.str = "(iw-cw)/2"}, .flags = DYNAMIC },
1821 { "crop_y", "Input video crop y", OFFSET(crop_y_expr), AV_OPT_TYPE_STRING, {.str = "(ih-ch)/2"}, .flags = DYNAMIC },
1822 { "crop_w", "Input video crop w", OFFSET(crop_w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = DYNAMIC },
1823 { "crop_h", "Input video crop h", OFFSET(crop_h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = DYNAMIC },
1824 { "pos_x", "Output video placement x", OFFSET(pos_x_expr), AV_OPT_TYPE_STRING, {.str = "(ow-pw)/2"}, .flags = DYNAMIC },
1825 { "pos_y", "Output video placement y", OFFSET(pos_y_expr), AV_OPT_TYPE_STRING, {.str = "(oh-ph)/2"}, .flags = DYNAMIC },
1826 { "pos_w", "Output video placement w", OFFSET(pos_w_expr), AV_OPT_TYPE_STRING, {.str = "ow"}, .flags = DYNAMIC },
1827 { "pos_h", "Output video placement h", OFFSET(pos_h_expr), AV_OPT_TYPE_STRING, {.str = "oh"}, .flags = DYNAMIC },
1828 { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, SCALE_FORCE_OAR_NB-1, STATIC, .unit = "force_oar" },
1829 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_DISABLE }, 0, 0, STATIC, .unit = "force_oar" },
1830 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_DECREASE }, 0, 0, STATIC, .unit = "force_oar" },
1831 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_INCREASE }, 0, 0, STATIC, .unit = "force_oar" },
1832 { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 256, STATIC },
1833 { "reset_sar", "force SAR normalization to 1:1 by adjusting pos_x/y/w/h", OFFSET(reset_sar), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, STATIC },
1834 { "normalize_sar", "like reset_sar, but pad/crop instead of stretching the video", OFFSET(normalize_sar), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, STATIC },
1835 { "pad_crop_ratio", "ratio between padding and cropping when normalizing SAR (0=pad, 1=crop)", OFFSET(pad_crop_ratio), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, 1.0, DYNAMIC },
1836 { "fit_mode", "Content fit strategy for placing input layers in the output", OFFSET(fit_mode), AV_OPT_TYPE_INT, {.i64 = FIT_FILL }, 0, FIT_MODE_NB - 1, STATIC, .unit = "fit_mode" },
1837 { "fill", "Stretch content, ignoring aspect ratio", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_FILL }, 0, 0, STATIC, .unit = "fit_mode" },
1838 { "contain", "Stretch content, padding to preserve aspect", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_CONTAIN }, 0, 0, STATIC, .unit = "fit_mode" },
1839 { "cover", "Stretch content, cropping to preserve aspect", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_COVER }, 0, 0, STATIC, .unit = "fit_mode" },
1840 { "none", "Keep input unscaled, padding and cropping as needed", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_NONE }, 0, 0, STATIC, .unit = "fit_mode" },
1841 { "place", "Keep input unscaled, padding and cropping as needed", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_NONE }, 0, 0, STATIC, .unit = "fit_mode" },
1842 { "scale_down", "Downscale only if larger, padding to preserve aspect", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_SCALE_DOWN }, 0, 0, STATIC, .unit = "fit_mode" },
1843 { "fit_sense", "Output size strategy (for the base layer only)", OFFSET(fit_sense), AV_OPT_TYPE_INT, {.i64 = FIT_TARGET }, 0, FIT_SENSE_NB - 1, STATIC, .unit = "fit_sense" },
1844 { "target", "Computed resolution is the exact output size", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_TARGET }, 0, 0, STATIC, .unit = "fit_sense" },
1845 { "constraint", "Computed resolution constrains the output size", 0, AV_OPT_TYPE_CONST, {.i64 = FIT_CONSTRAINT }, 0, 0, STATIC, .unit = "fit_sense" },
1846 { "fillcolor", "Background fill color", OFFSET(fillcolor), AV_OPT_TYPE_COLOR, {.str = "black@0"}, .flags = DYNAMIC },
1847 { "corner_rounding", "Corner rounding radius", OFFSET(corner_rounding), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, .flags = DYNAMIC },
1848 { "lut", "Path to custom LUT file to apply", OFFSET(lut_filename), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = STATIC },
1849 { "lut_type", "Application mode of the custom LUT", OFFSET(lut_type), AV_OPT_TYPE_INT, { .i64 = PL_LUT_UNKNOWN }, 0, PL_LUT_CONVERSION, STATIC, .unit = "lut_type" },
1850 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_UNKNOWN }, 0, 0, STATIC, .unit = "lut_type" },
1851 { "native", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NATIVE }, 0, 0, STATIC, .unit = "lut_type" },
1852 { "normalized", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NORMALIZED }, 0, 0, STATIC, .unit = "lut_type" },
1853 { "conversion", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_CONVERSION }, 0, 0, STATIC, .unit = "lut_type" },
1854
1855 { "extra_opts", "Pass extra libplacebo-specific options using a :-separated list of key=value pairs", OFFSET(extra_opts), AV_OPT_TYPE_DICT, .flags = DYNAMIC },
1856
1858
1859 {"rotate", "rotate the input clockwise", OFFSET(rotation), AV_OPT_TYPE_INT, {.i64=PL_ROTATION_0}, PL_ROTATION_0, PL_ROTATION_360, DYNAMIC, .unit = "rotation"},
1860 {"0", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PL_ROTATION_0}, .flags = STATIC, .unit = "rotation"},
1861 {"90", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PL_ROTATION_90}, .flags = STATIC, .unit = "rotation"},
1862 {"180", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PL_ROTATION_180}, .flags = STATIC, .unit = "rotation"},
1863 {"270", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PL_ROTATION_270}, .flags = STATIC, .unit = "rotation"},
1864 {"360", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PL_ROTATION_360}, .flags = STATIC, .unit = "rotation"},
1865
1866 { "upscaler", "Upscaler function", OFFSET(upscaler), AV_OPT_TYPE_STRING, {.str = "spline36"}, .flags = DYNAMIC },
1867 { "downscaler", "Downscaler function", OFFSET(downscaler), AV_OPT_TYPE_STRING, {.str = "mitchell"}, .flags = DYNAMIC },
1868 { "frame_mixer", "Frame mixing function", OFFSET(frame_mixer), AV_OPT_TYPE_STRING, {.str = "none"}, .flags = DYNAMIC },
1869 { "antiringing", "Antiringing strength (for non-EWA filters)", OFFSET(antiringing), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, DYNAMIC },
1870 { "sigmoid", "Enable sigmoid upscaling", OFFSET(sigmoid), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DYNAMIC },
1871
1872 { "deinterlace", "Deinterlacing mode", OFFSET(deinterlace), AV_OPT_TYPE_INT, {.i64 = PL_DEINTERLACE_WEAVE}, 0, PL_DEINTERLACE_ALGORITHM_COUNT - 1, DYNAMIC, .unit = "deinterlace" },
1873 { "weave", "Weave fields together (no-op)", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DEINTERLACE_WEAVE}, 0, 0, STATIC, .unit = "deinterlace" },
1874 { "bob", "Naive bob deinterlacing", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DEINTERLACE_BOB}, 0, 0, STATIC, .unit = "deinterlace" },
1875 { "yadif", "Yet another deinterlacing filter", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DEINTERLACE_YADIF}, 0, 0, STATIC, .unit = "deinterlace" },
1876#if PL_API_VER >= 353
1877 { "bwdif", "Bob weaver deinterlacing filter", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DEINTERLACE_BWDIF}, 0, 0, STATIC, .unit = "deinterlace" },
1878#endif
1879 { "skip_spatial_check", "Skip yadif spatial check", OFFSET(skip_spatial_check), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1880 { "send_fields", "Output a frame for each field", OFFSET(send_fields), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1881
1882 { "deband", "Enable debanding", OFFSET(deband), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1883 { "deband_iterations", "Deband iterations", OFFSET(deband_iterations), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 16, DYNAMIC },
1884 { "deband_threshold", "Deband threshold", OFFSET(deband_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 4.0}, 0.0, 1024.0, DYNAMIC },
1885 { "deband_radius", "Deband radius", OFFSET(deband_radius), AV_OPT_TYPE_FLOAT, {.dbl = 16.0}, 0.0, 1024.0, DYNAMIC },
1886 { "deband_grain", "Deband grain", OFFSET(deband_grain), AV_OPT_TYPE_FLOAT, {.dbl = 6.0}, 0.0, 1024.0, DYNAMIC },
1887
1888 { "brightness", "Brightness boost", OFFSET(brightness), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -1.0, 1.0, DYNAMIC },
1889 { "contrast", "Contrast gain", OFFSET(contrast), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 16.0, DYNAMIC },
1890 { "saturation", "Saturation gain", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 16.0, DYNAMIC },
1891 { "hue", "Hue shift", OFFSET(hue), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -M_PI, M_PI, DYNAMIC },
1892 { "gamma", "Gamma adjustment", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 16.0, DYNAMIC },
1893 { "temperature", "Color temperature adjustment (kelvin)", OFFSET(temperature), AV_OPT_TYPE_FLOAT, {.dbl = 6500.0}, 1667.0, 25000.0, DYNAMIC },
1894
1895 { "peak_detect", "Enable dynamic peak detection for HDR tone-mapping", OFFSET(peakdetect), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DYNAMIC },
1896 { "smoothing_period", "Peak detection smoothing period", OFFSET(smoothing), AV_OPT_TYPE_FLOAT, {.dbl = 20.0}, 0.0, 1000.0, DYNAMIC },
1897 { "scene_threshold_low", "Scene change low threshold", OFFSET(scene_low), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, -1.0, 100.0, DYNAMIC },
1898 { "scene_threshold_high", "Scene change high threshold", OFFSET(scene_high), AV_OPT_TYPE_FLOAT, {.dbl = 3.0}, -1.0, 100.0, DYNAMIC },
1899 { "percentile", "Peak detection percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl = 99.995}, 0.0, 100.0, DYNAMIC },
1900
1901 { "gamut_mode", "Gamut-mapping mode", OFFSET(gamut_mode), AV_OPT_TYPE_INT, {.i64 = GAMUT_MAP_PERCEPTUAL}, 0, GAMUT_MAP_COUNT - 1, DYNAMIC, .unit = "gamut_mode" },
1902 { "clip", "Hard-clip (RGB per-channel)", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_CLIP}, 0, 0, STATIC, .unit = "gamut_mode" },
1903 { "perceptual", "Colorimetric soft clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_PERCEPTUAL}, 0, 0, STATIC, .unit = "gamut_mode" },
1904 { "relative", "Relative colorimetric clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_RELATIVE}, 0, 0, STATIC, .unit = "gamut_mode" },
1905 { "saturation", "Saturation mapping (RGB -> RGB)", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_SATURATION}, 0, 0, STATIC, .unit = "gamut_mode" },
1906 { "absolute", "Absolute colorimetric clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_ABSOLUTE}, 0, 0, STATIC, .unit = "gamut_mode" },
1907 { "desaturate", "Colorimetrically desaturate colors towards white", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_DESATURATE}, 0, 0, STATIC, .unit = "gamut_mode" },
1908 { "darken", "Colorimetric clip with bias towards darkening image to fit gamut", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_DARKEN}, 0, 0, STATIC, .unit = "gamut_mode" },
1909 { "warn", "Highlight out-of-gamut colors", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_HIGHLIGHT}, 0, 0, STATIC, .unit = "gamut_mode" },
1910 { "linear", "Linearly reduce chromaticity to fit gamut", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_LINEAR}, 0, 0, STATIC, .unit = "gamut_mode" },
1911 { "tonemapping", "Tone-mapping algorithm", OFFSET(tonemapping), AV_OPT_TYPE_INT, {.i64 = TONE_MAP_AUTO}, 0, TONE_MAP_COUNT - 1, DYNAMIC, .unit = "tonemap" },
1912 { "auto", "Automatic selection", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_AUTO}, 0, 0, STATIC, .unit = "tonemap" },
1913 { "clip", "No tone mapping (clip", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_CLIP}, 0, 0, STATIC, .unit = "tonemap" },
1914 { "st2094-40", "SMPTE ST 2094-40", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_ST2094_40}, 0, 0, STATIC, .unit = "tonemap" },
1915 { "st2094-10", "SMPTE ST 2094-10", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_ST2094_10}, 0, 0, STATIC, .unit = "tonemap" },
1916 { "bt.2390", "ITU-R BT.2390 EETF", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_BT2390}, 0, 0, STATIC, .unit = "tonemap" },
1917 { "bt.2446a", "ITU-R BT.2446 Method A", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_BT2446A}, 0, 0, STATIC, .unit = "tonemap" },
1918 { "spline", "Single-pivot polynomial spline", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_SPLINE}, 0, 0, STATIC, .unit = "tonemap" },
1919 { "reinhard", "Reinhard", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_REINHARD}, 0, 0, STATIC, .unit = "tonemap" },
1920 { "mobius", "Mobius", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_MOBIUS}, 0, 0, STATIC, .unit = "tonemap" },
1921 { "hable", "Filmic tone-mapping (Hable)", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_HABLE}, 0, 0, STATIC, .unit = "tonemap" },
1922 { "gamma", "Gamma function with knee", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_GAMMA}, 0, 0, STATIC, .unit = "tonemap" },
1923 { "linear", "Perceptually linear stretch", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_LINEAR}, 0, 0, STATIC, .unit = "tonemap" },
1924 { "tonemapping_param", "Tunable parameter for some tone-mapping functions; use the corresponding tonemapping_* options instead", OFFSET(tonemapping_param), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 100.0, .flags = DYNAMIC | AV_OPT_FLAG_DEPRECATED },
1925 { "tonemapping_knee_adaptation", "Scene-average knee point adaptation", OFFSET(tone_constants.knee_adaptation), AV_OPT_TYPE_FLOAT, {.dbl = 0.4}, 0.0, 1.0, DYNAMIC },
1926 { "tonemapping_knee_minimum", "Minimum knee point in PQ space", OFFSET(tone_constants.knee_minimum), AV_OPT_TYPE_FLOAT, {.dbl = 0.1}, 0.0, 0.5, DYNAMIC },
1927 { "tonemapping_knee_maximum", "Maximum knee point in PQ space", OFFSET(tone_constants.knee_maximum), AV_OPT_TYPE_FLOAT, {.dbl = 0.8}, 0.5, 1.0, DYNAMIC },
1928 { "tonemapping_knee_default", "Fallback knee point without scene average metadata", OFFSET(tone_constants.knee_default), AV_OPT_TYPE_FLOAT, {.dbl = 0.4}, 0.0, 1.0, DYNAMIC },
1929 { "tonemapping_knee_offset", "BT.2390 knee point offset", OFFSET(tone_constants.knee_offset), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.5, 2.0, DYNAMIC },
1930 { "tonemapping_slope_tuning", "Spline slope tuning", OFFSET(tone_constants.slope_tuning), AV_OPT_TYPE_FLOAT, {.dbl = 1.5}, 0.0, 10.0, DYNAMIC },
1931 { "tonemapping_slope_offset", "Spline slope offset", OFFSET(tone_constants.slope_offset), AV_OPT_TYPE_FLOAT, {.dbl = 0.2}, 0.0, 1.0, DYNAMIC },
1932 { "tonemapping_spline_contrast", "Spline curve contrast", OFFSET(tone_constants.spline_contrast), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0.0, 1.5, DYNAMIC },
1933 { "tonemapping_reinhard_contrast", "Reinhard contrast at the display peak", OFFSET(tone_constants.reinhard_contrast), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0.0, 1.0, DYNAMIC },
1934 { "tonemapping_linear_knee", "Linear-light knee point for Mobius and gamma", OFFSET(tone_constants.linear_knee), AV_OPT_TYPE_FLOAT, {.dbl = 0.3}, 0.0, 1.0, DYNAMIC },
1935 { "tonemapping_exposure", "Exposure for linear tone mapping", OFFSET(tone_constants.exposure), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 10.0, DYNAMIC },
1936 { "inverse_tonemapping", "Inverse tone mapping (range expansion)", OFFSET(inverse_tonemapping), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1937 { "tonemapping_lut_size", "Tone-mapping LUT size", OFFSET(tonemapping_lut_size), AV_OPT_TYPE_INT, {.i64 = 256}, 2, 1024, DYNAMIC },
1938 { "contrast_recovery", "HDR contrast recovery strength", OFFSET(contrast_recovery), AV_OPT_TYPE_FLOAT, {.dbl = 0.30}, 0.0, 3.0, DYNAMIC },
1939 { "contrast_smoothness", "HDR contrast recovery smoothness", OFFSET(contrast_smoothness), AV_OPT_TYPE_FLOAT, {.dbl = 3.50}, 1.0, 32.0, DYNAMIC },
1940
1941 { "dithering", "Dither method to use", OFFSET(dithering), AV_OPT_TYPE_INT, {.i64 = PL_DITHER_BLUE_NOISE}, -1, PL_DITHER_METHOD_COUNT - 1, DYNAMIC, .unit = "dither" },
1942 { "none", "Disable dithering", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, STATIC, .unit = "dither" },
1943 { "blue", "Blue noise", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DITHER_BLUE_NOISE}, 0, 0, STATIC, .unit = "dither" },
1944 { "ordered", "Ordered LUT", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DITHER_ORDERED_LUT}, 0, 0, STATIC, .unit = "dither" },
1945 { "ordered_fixed", "Fixed function ordered", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DITHER_ORDERED_FIXED}, 0, 0, STATIC, .unit = "dither" },
1946 { "white", "White noise", 0, AV_OPT_TYPE_CONST, {.i64 = PL_DITHER_WHITE_NOISE}, 0, 0, STATIC, .unit = "dither" },
1947 { "dither_lut_size", "Dithering LUT size", OFFSET(dither_lut_size), AV_OPT_TYPE_INT, {.i64 = 6}, 1, 8, STATIC },
1948 { "dither_temporal", "Enable temporal dithering", OFFSET(dither_temporal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1949
1950 { "cones", "Colorblindness adaptation model", OFFSET(cones), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, PL_CONE_LMS, DYNAMIC, .unit = "cone" },
1951 { "l", "L cone", 0, AV_OPT_TYPE_CONST, {.i64 = PL_CONE_L}, 0, 0, STATIC, .unit = "cone" },
1952 { "m", "M cone", 0, AV_OPT_TYPE_CONST, {.i64 = PL_CONE_M}, 0, 0, STATIC, .unit = "cone" },
1953 { "s", "S cone", 0, AV_OPT_TYPE_CONST, {.i64 = PL_CONE_S}, 0, 0, STATIC, .unit = "cone" },
1954 { "cone-strength", "Colorblindness adaptation strength", OFFSET(cone_str), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 10.0, DYNAMIC },
1955
1956 { "custom_shader_path", "Path to custom user shader (mpv .hook format)", OFFSET(shader_path), AV_OPT_TYPE_STRING, .flags = STATIC },
1957 { "custom_shader_bin", "Custom user shader as binary (mpv .hook format)", OFFSET(shader_bin), AV_OPT_TYPE_BINARY, .flags = STATIC },
1958
1959 /* Performance/quality tradeoff options */
1960 { "skip_aa", "Skip anti-aliasing", OFFSET(skip_aa), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1961 { "disable_linear", "Disable linear scaling", OFFSET(disable_linear), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1962 { "disable_builtin", "Disable built-in scalers", OFFSET(disable_builtin), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1963 { "force_dither", "Force dithering", OFFSET(force_dither), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1964 { "disable_fbos", "Force-disable FBOs", OFFSET(disable_fbos), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC },
1965 { NULL },
1966};
1967
1969
1971 {
1972 .name = "default",
1973 .type = AVMEDIA_TYPE_VIDEO,
1974 .config_props = &libplacebo_config_output,
1975 },
1976};
1977
1979 .p.name = "libplacebo",
1980 .p.description = NULL_IF_CONFIG_SMALL("Apply various GPU filters from libplacebo"),
1981 .p.priv_class = &libplacebo_class,
1983 .priv_size = sizeof(LibplaceboContext),
1991};
1992
1993#if CONFIG_LIBPLACEBO_SCENE_FILTER
1994
1995static int scene_setup_gpu(AVFilterContext *avctx)
1996{
1997 LibplaceboContext *s = avctx->priv;
1998 pl_compositor comp = s->scene->comp;
1999 if (comp->inputs[0]->renderer)
2000 return 0; /* already initialized */
2001
2002 if (!pl_compositor_init(comp, s->gpu))
2003 return AVERROR_EXTERNAL;
2004
2005 return 0;
2006}
2007
2008static int scene_config_input(AVFilterLink *inlink)
2009{
2010 int err;
2011 AVFilterContext *avctx = inlink->dst;
2012 LibplaceboContext *s = avctx->priv;
2013
2014 if (s->inherit_device)
2015 RET(inherit_device(inlink));
2016 RET(init_vulkan(avctx, choose_default_device(avctx)));
2017 RET(scene_setup_gpu(avctx));
2018 return config_input_vulkan(inlink);
2019
2020fail:
2021 return err;
2022}
2023
2024static int scene_init(AVFilterContext *avctx)
2025{
2026 int err;
2027 LibplaceboContext *s = avctx->priv;
2028 pl_compositor comp;
2029
2030 if (!s->scene_json || !s->w_expr || !s->h_expr || !s->fps_string) {
2031 av_log(avctx, AV_LOG_ERROR, "The 'scene', 'w', 'h' and 'fps' options "
2032 "are required\n");
2033 return AVERROR(EINVAL);
2034 }
2035
2036 RET(av_parse_video_rate(&s->fps, s->fps_string));
2037 RET(init_common(avctx));
2038
2039 s->scene = pl_scene_create_json(s->log, s->scene_json, strlen(s->scene_json));
2040 if (!s->scene) {
2041 av_log(avctx, AV_LOG_ERROR, "Failed parsing scene description\n");
2042 return AVERROR(EINVAL);
2043 }
2044
2045 comp = s->scene->comp;
2046 s->nb_inputs = comp->num_inputs;
2047 if (!s->nb_inputs) {
2048 av_log(avctx, AV_LOG_ERROR, "Scene contains no inputs\n");
2049 return AVERROR(EINVAL);
2050 }
2051
2052 /* Output color properties not set on the filter default to the scene's */
2053 if (s->colorspace < 0 && comp->repr.sys)
2054 s->colorspace = pl_system_to_av(comp->repr.sys);
2055 if (s->color_range < 0 && comp->repr.levels)
2056 s->color_range = pl_levels_to_av(comp->repr.levels);
2057 if (s->alpha_mode < 0 && comp->repr.alpha)
2058 s->alpha_mode = pl_alpha_to_av(comp->repr.alpha);
2059 if (s->chroma_location < 0 && comp->chroma_location)
2060 s->chroma_location = pl_chroma_to_av(comp->chroma_location);
2061 if (s->color_primaries < 0 && comp->color.primaries)
2062 s->color_primaries = pl_primaries_to_av(comp->color.primaries);
2063 if (s->color_trc < 0 && comp->color.transfer)
2064 s->color_trc = pl_transfer_to_av(comp->color.transfer);
2065
2066 RET(append_inputs(avctx, s->nb_inputs, &scene_config_input));
2067
2068 if (!s->inherit_device) {
2069 RET(init_vulkan(avctx, choose_default_device(avctx)));
2070 RET(scene_setup_gpu(avctx));
2071 } /* else deferred to scene_config_input() */
2072
2073 return 0;
2074
2075fail:
2076 return err;
2077}
2078
2079static void scene_uninit(AVFilterContext *avctx)
2080{
2081 LibplaceboContext *s = avctx->priv;
2082 pl_scene_destroy(&s->scene);
2083 libplacebo_uninit(avctx);
2084}
2085
2086static int scene_eval_dim(AVFilterContext *avctx, const char *expr, int *out)
2087{
2088 double res;
2089 int err = av_expr_parse_and_eval(&res, expr, NULL, NULL, NULL, NULL,
2090 NULL, NULL, NULL, 0, avctx);
2091 if (err < 0 || res < 1 || res > INT_MAX) {
2092 av_log(avctx, AV_LOG_ERROR, "Invalid output dimension: %s\n", expr);
2093 return err < 0 ? err : AVERROR(EINVAL);
2094 }
2095
2096 *out = lrint(res);
2097 return 0;
2098}
2099
2100static int scene_config_output(AVFilterLink *outlink)
2101{
2102 int err;
2103 AVFilterContext *avctx = outlink->src;
2104 LibplaceboContext *s = avctx->priv;
2105 FilterLink *l = ff_filter_link(outlink);
2106
2107 RET(scene_eval_dim(avctx, s->w_expr, &outlink->w));
2108 RET(scene_eval_dim(avctx, s->h_expr, &outlink->h));
2109 outlink->sample_aspect_ratio = s->out_sar.num ? s->out_sar : (AVRational){ 1, 1 };
2110 outlink->time_base = av_inv_q(s->fps);
2111 l->frame_rate = s->fps;
2112
2113 RET(config_output_vulkan(outlink));
2114 return 0;
2115
2116fail:
2117 return err;
2118}
2119
2120static int scene_handle_input(AVFilterContext *ctx, int idx)
2121{
2122 int ret, status;
2123 LibplaceboContext *s = ctx->priv;
2124 AVFilterLink *inlink = ctx->inputs[idx];
2125 pl_input input = s->scene->comp->inputs[idx];
2126 const bool deinterlace = input->params && input->params->deinterlace_params;
2127 AVFrame *in;
2128 int64_t pts;
2129
2130 while ((ret = ff_inlink_consume_frame(inlink, &in)) > 0)
2131 push_frame(s, input->queue, inlink, in, deinterlace);
2132
2133 if (ret < 0)
2134 return ret;
2135
2136 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
2137 pl_queue_set_status(input->queue, status == AVERROR_EOF ? PL_QUEUE_EOF : PL_QUEUE_ERR);
2138 input->user_data = (void *) (intptr_t) status;
2139 }
2140
2141 return 0;
2142}
2143
2144static int scene_output_frame(AVFilterContext *ctx, int64_t pts,
2145 const struct pl_queue_params *qparams,
2146 const struct pl_compositor_status *st)
2147{
2148 int err = 0;
2149 bool ok;
2150 LibplaceboContext *s = ctx->priv;
2151 AVFilterLink *outlink = ctx->outputs[0];
2152 const AVFrame *ref = st->base ? pl_get_mapped_avframe(st->base) : NULL;
2153 struct pl_frame target;
2154 AVFrame *out;
2155
2156 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2157 if (!out)
2158 return AVERROR(ENOMEM);
2159
2160 RET(set_output_props(s, outlink, out, ref, true)); /* always progressive */
2161 out->pts = pts;
2162 out->duration = 1;
2163
2164 RET(map_output(s, out, &target));
2165 ok = pl_compositor_render(s->scene->comp, &target, qparams, NULL);
2166 RET(unmap_output(s, &target, out));
2167 if (!ok) {
2168 err = AVERROR_EXTERNAL;
2169 goto fail;
2170 }
2171 return ff_filter_frame(outlink, out);
2172
2173fail:
2175 return err;
2176}
2177
2178static int scene_activate(AVFilterContext *ctx)
2179{
2180 int ret, retry = 0;
2181 LibplaceboContext *s = ctx->priv;
2182 AVFilterLink *outlink = ctx->outputs[0];
2183 FilterLink *outl = ff_filter_link(outlink);
2184 pl_compositor comp = s->scene->comp;
2185
2187 pl_log_level_update(s->log, get_log_level());
2188
2189 for (int i = 0; i < s->nb_inputs; i++) {
2190 if ((ret = scene_handle_input(ctx, i)) < 0)
2191 return ret;
2192 }
2193
2194 if (!ff_outlink_frame_wanted(outlink))
2195 return FFERROR_NOT_READY;
2196
2197 const int64_t out_pts = outl->frame_count_out;
2198 const struct pl_queue_params qparams = *pl_queue_params(
2199 .pts = TS2T(out_pts, outlink->time_base),
2200 .vsync_duration = av_q2d(outlink->time_base),
2201 );
2202
2203 pl_scene_update(s->scene, qparams.pts);
2204
2205 struct pl_compositor_status st;
2206 st = pl_compositor_update(comp, &qparams, pl_canvas_size(
2207 .width = outlink->w,
2208 .height = outlink->h,
2209 .pixel_aspect_ratio = av_q2d(outlink->sample_aspect_ratio),
2210 ));
2211
2212 for (int i = 0; i < s->nb_inputs; i++) {
2213 AVFilterLink *inlink = ctx->inputs[i];
2214 switch (st.input_status[i].status) {
2215 case PL_QUEUE_MORE:
2216 if (!ff_outlink_get_status(inlink)) {
2218 retry = 1;
2219 }
2220 break;
2221 case PL_QUEUE_EOF:
2222 /* Also closes inputs that reached their stop_pts early */
2224 break;
2225 case PL_QUEUE_ERR: {
2226 /* Forward the error of the failed input */
2227 const int status = (intptr_t) comp->inputs[i]->user_data;
2228 if (!status || status == AVERROR_EOF)
2229 return AVERROR_EXTERNAL;
2230 ff_outlink_set_status(outlink, status, out_pts);
2231 return 0;
2232 }
2233 default:
2234 break;
2235 }
2236 }
2237
2238 if (retry)
2239 return 0;
2240
2241 if (st.status == PL_QUEUE_EOF) {
2242 ff_outlink_set_status(outlink, AVERROR_EOF, out_pts);
2243 return 0;
2244 }
2245
2246 return scene_output_frame(ctx, out_pts, &qparams, &st);
2247}
2248
2249static const AVOption libplacebo_scene_options[] = {
2250 { "scene", "JSON scene graph description", OFFSET(scene_json), AV_OPT_TYPE_STRING, .flags = STATIC },
2251 { "w", "Output video frame width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = STATIC },
2252 { "h", "Output video frame height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = STATIC },
2253 { "fps", "Output video frame rate", OFFSET(fps_string), AV_OPT_TYPE_STRING, .flags = STATIC },
2255 { NULL },
2256};
2257
2258AVFILTER_DEFINE_CLASS(libplacebo_scene);
2259
2260static const AVFilterPad libplacebo_scene_outputs[] = {
2261 {
2262 .name = "default",
2263 .type = AVMEDIA_TYPE_VIDEO,
2264 .config_props = &scene_config_output,
2265 },
2266};
2267
2269 .p.name = "libplacebo_scene",
2270 .p.description = NULL_IF_CONFIG_SMALL("Composite inputs according to a libplacebo scene graph"),
2271 .p.priv_class = &libplacebo_scene_class,
2273 .priv_size = sizeof(LibplaceboContext),
2274 .init = &scene_init,
2275 .uninit = &scene_uninit,
2276 .activate = &scene_activate,
2278 FILTER_OUTPUTS(libplacebo_scene_outputs),
2281};
2282
2283#endif /* CONFIG_LIBPLACEBO_SCENE_FILTER */
@ VAR_CH
Definition aeval.c:49
@ VAR_T
Definition aeval.c:53
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static const char *const format[]
Definition af_aiir.c:445
const FFFilter ff_vf_libplacebo_scene
const FFFilter ff_vf_libplacebo
static FILE * out
static AVFormatContext * ctx
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition avfilter.c:132
void ff_inlink_set_status(AVFilterLink *link, int status)
Set the status on an input link.
Definition avfilter.c:1657
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition avfilter.c:1673
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1474
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1715
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:917
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1527
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1648
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
@ VAR_VSUB
Definition boxblur.c:42
@ VAR_CW
Definition boxblur.c:39
@ VAR_HSUB
Definition boxblur.c:41
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static IPT saturation(const CmsCtx *ctx, IPT ipt)
Definition cms.c:559
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabsf(float a)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition eval.c:368
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition eval.c:824
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition eval.c:839
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition eval.c:735
simple arithmetic expression evaluator
@ VAR_IW
Definition f_select.c:147
@ VAR_IH
Definition f_select.c:146
int ret
Definition ffplay.c:391
SDL_atomic_t done
Definition ffplay.c:392
static int dummy
Definition ffplay.c:4427
A generic FIFO API.
Misc file utilities.
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:776
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition formats.c:706
AVFilterFormats * ff_make_formats_list_singleton(int fmt)
Equivalent to ff_make_format_list({const int[]}{ fmt, -1 })
Definition formats.c:605
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:580
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to,...
Definition formats.c:815
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition formats.c:722
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition formats.c:733
AVFilterFormats * ff_all_chroma_locations(void)
Construct an AVFilterFormats representing all possible chroma locations.
Definition formats.c:744
reference-counted frame API
#define fail
Definition test.h:479
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
Definition opt.h:385
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition opt.h:285
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:193
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition avfilter.h:161
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition dict.h:75
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition fifo.h:63
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition fifo.c:255
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
void av_fifo_drain2(AVFifo *f, size_t size)
Discard the specified amount of data from an AVFifo.
Definition fifo.c:266
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:702
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:707
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:662
void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
Remove and free all side data instances of the given type.
Definition frame.c:728
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, int props)
Remove and free all side data instances that match any of the given side data properties.
Definition side_data.c:124
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
@ AV_SIDE_DATA_PROP_SIZE_DEPENDENT
Side data depends on the video dimensions.
Definition frame.h:361
@ AV_SIDE_DATA_PROP_COLOR_DEPENDENT
Side data depends on the video color space.
Definition frame.h:368
@ AV_FRAME_DATA_DOVI_METADATA
Parsed Dolby Vision metadata, suitable for passing to a software implementation.
Definition frame.h:208
@ AV_FRAME_DATA_FILM_GRAIN_PARAMS
Film grain parameters for a frame, described by AVFilmGrainParams.
Definition frame.h:188
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition log.h:204
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_log_get_level(void)
Get the current log level.
Definition log.c:477
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def)
Return the best rational so that a and b are multiple of it.
Definition rational.c:188
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition rational.c:88
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_UP
Round toward +infinity.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
int a
@ AV_HWDEVICE_TYPE_VULKAN
Definition hwcontext.h:39
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the optimal per-plane Vulkan format for a given sw_format, one for each plane.
#define b
Definition input.c:43
enum AVPixelFormat pixfmt
Definition kmsgrab.c:367
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int mix(int c0, int c1)
Definition 4xm.c:717
#define fixed(width, name, value)
Definition cbs_apv.c:75
static void log_cb(cmsContext ctx, cmsUInt32Number error, const char *str)
Definition fflcms2.c:24
const char * arg
Definition jacosubdec.c:66
static int get_log_level(int level)
Definition lcevcdec.c:404
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define TS2T(ts, tb)
Definition filters.h:498
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:655
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS
Definition filters.h:222
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:678
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:256
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:175
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:114
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition internal.h:71
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition internal.h:72
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition vulkan.c:2800
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define NAN
#define M_PI
Definition mathematics.h:67
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
var_name
Definition noise.c:46
@ VAR_N
Definition noise.c:47
@ VAR_VARS_NB
Definition noise.c:59
static const char *const var_names[]
Definition noise.c:30
AVOptions.
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition parseutils.c:181
misc parsing utilities
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition pixdesc.c:3503
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition pixdesc.c:3491
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3416
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition pixdesc.h:116
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:812
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition pixfmt.h:658
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition pixfmt.h:694
const char * name
Definition qsvenc.c:142
@ VAR_OUT_H
Definition scale_eval.c:47
@ VAR_SAR
Definition scale_eval.c:49
@ VAR_OH
Definition scale_eval.c:47
@ VAR_IN_W
Definition scale_eval.c:44
@ VAR_OW
Definition scale_eval.c:46
@ VAR_OHSUB
Definition scale_eval.c:53
@ VAR_A
Definition scale_eval.c:48
@ VAR_OUT_W
Definition scale_eval.c:46
@ VAR_OVSUB
Definition scale_eval.c:54
@ VAR_IN_H
Definition scale_eval.c:45
@ VAR_DAR
Definition scale_eval.c:50
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by, double w_adj)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition scale_eval.c:123
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:58
@ SCALE_FORCE_OAR_DISABLE
Definition scale_eval.h:25
@ SCALE_FORCE_OAR_NB
Definition scale_eval.h:28
@ SCALE_FORCE_OAR_INCREASE
Definition scale_eval.h:27
@ SCALE_FORCE_OAR_DECREASE
Definition scale_eval.h:26
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
uint8_t * data
The data buffer.
Definition buffer.h:90
Definition eval.c:171
Definition fifo.c:35
An instance of a filter.
Definition avfilter.h:279
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:287
void * priv
private data for use by the filter
Definition avfilter.h:294
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in.
Definition avfilter.h:342
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:291
Lists of formats / etc.
Definition avfilter.h:120
AVFilterFormats * formats
List of supported formats (pixel or sample).
Definition avfilter.h:125
A list of supported formats for one end of a filter link.
Definition formats.h:64
unsigned refcount
number of references to this list
Definition formats.h:68
unsigned nb_formats
number of formats
Definition formats.h:65
A filter pad used for either input or output.
Definition filters.h:40
const char * name
Pad name.
Definition filters.h:46
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:581
enum AVChromaLocation chroma_location
Definition frame.h:746
void * opaque
Frame owner's private data.
Definition frame.h:617
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:830
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition hwcontext.h:75
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition hwcontext.h:153
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
VkPhysicalDevice phys_dev
Physical device.
AVVulkanDeviceQueueFamily qf[64]
Queue families used.
const char *const * enabled_dev_extensions
Enabled device extensions.
VkDevice act_dev
Active device.
VkInstance inst
Vulkan instance.
VkPhysicalDeviceFeatures2 device_features
This structure should be set to the set of features that present and enabled during device creation.
VkDeviceQueueCreateFlags queue_flags
PFN_vkGetInstanceProcAddr get_proc_addr
Pointer to a vkGetInstanceProcAddr loading function.
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
VkImageUsageFlagBits usage
Defines extra usage of output frames.
struct pl_custom_lut * lut
double var_values[VAR_VARS_NB]
AVDictionary * extra_opts
enum AVPixelFormat out_format
AVRational fps
parsed FPS, or 0/0 for "none"
pl_renderer linear_rr
LibplaceboInput * inputs
struct pl_tone_map_constants tone_constants
const struct pl_hook * hooks[2]
FFVulkanContext vkctx
struct pl_frame_mix mix
temporary storage
enum pl_queue_status qstatus
pl_renderer renderer
AVFifo * out_pts
timestamps of wanted output frames
Definition dct.c:58
Definition format.c:914
uint8_t level
Definition svq3.c:208
#define lrint
Definition tablegen.h:53
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
#define width
Definition dsp.h:99
AVThreadMessageQueue * queue
Definition swscale.c:141
static int64_t pts
preset
Definition vf_curves.c:47
static float sigmoid(float x)
static int libplacebo_config_output(AVFilterLink *outlink)
@ VAR_CROP_H
@ VAR_POS_W
@ VAR_CROP_W
@ VAR_IN_IDX
@ VAR_POS_H
@ VAR_PH
@ VAR_IDX
@ VAR_OT
@ VAR_IN_T
@ VAR_VARS_NB
@ VAR_PW
@ VAR_OUT_T
static int update_settings(AVFilterContext *ctx)
static const AVVulkanDeviceContext * choose_default_device(const AVFilterContext *avctx)
static int find_scaler(AVFilterContext *avctx, const struct pl_filter_config **opt, const char *name, int frame_mixing)
#define STATIC
static const AVOption libplacebo_options[]
#define DYNAMIC
static void pl_av_log(void *log_ctx, enum pl_log_level level, const char *msg)
static int libplacebo_setup_gpu(AVFilterContext *avctx)
static int parse_shader(AVFilterContext *avctx, const void *shader, size_t len)
static int init_common(AVFilterContext *avctx)
static int map_output(LibplaceboContext *s, AVFrame *out, struct pl_frame *target)
static enum pl_field push_frame(LibplaceboContext *s, pl_queue queue, const AVFilterLink *inlink, AVFrame *in, bool deinterlace)
static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwctx)
static const AVFilterPad libplacebo_outputs[]
static void lock_queue(void *priv, uint32_t qf, uint32_t qidx)
static void libplacebo_uninit(AVFilterContext *avctx)
static int handle_input(AVFilterContext *ctx, LibplaceboInput *input)
static int output_frame(AVFilterContext *ctx, int64_t pts)
static void unlock_queue(void *priv, uint32_t qf, uint32_t qidx)
static void unmap_frame(pl_gpu gpu, struct pl_frame *frame, const struct pl_source_frame *src)
static int unmap_output(LibplaceboContext *s, struct pl_frame *target, AVFrame *out)
static void set_gamut_mode(struct pl_color_map_params *p, int gamut_mode)
static int libplacebo_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
static void drain_input_pts(LibplaceboInput *in, int64_t until)
static int libplacebo_init(AVFilterContext *avctx)
@ GAMUT_MAP_LINEAR
@ GAMUT_MAP_ABSOLUTE
@ GAMUT_MAP_DESATURATE
@ GAMUT_MAP_COUNT
@ GAMUT_MAP_DARKEN
@ GAMUT_MAP_CLIP
@ GAMUT_MAP_PERCEPTUAL
@ GAMUT_MAP_RELATIVE
@ GAMUT_MAP_SATURATION
@ GAMUT_MAP_HIGHLIGHT
#define OPTS_COMMON
static int parse_custom_lut(AVFilterContext *avctx)
static int set_output_props(LibplaceboContext *s, const AVFilterLink *outlink, AVFrame *out, const AVFrame *ref, bool progressive)
static const AVFrame * ref_frame(const struct pl_frame_mix *mix)
static bool map_frame(pl_gpu gpu, pl_tex *tex, const struct pl_source_frame *src, struct pl_frame *out)
fit_sense
@ FIT_TARGET
@ FIT_CONSTRAINT
@ FIT_SENSE_NB
static void discard_frame(const struct pl_source_frame *src)
static int libplacebo_config_input(AVFilterLink *inlink)
static int inherit_device(AVFilterLink *inlink)
static AVRational max_q(AVRational a, AVRational b)
static int copy_pl_queue(const AVVulkanDeviceContext *hwctx, const AVVulkanDeviceQueueFamily *qf, struct pl_vulkan_queue *pl_qf)
static int input_init(AVFilterContext *avctx, LibplaceboInput *input, int idx)
static const struct pl_tone_map_function * get_tonemapping_func(int tm)
#define OFFSET(x)
fit_mode
@ FIT_SCALE_DOWN
@ FIT_COVER
@ FIT_FILL
@ FIT_CONTAIN
@ FIT_NONE
@ FIT_MODE_NB
static int config_output_vulkan(AVFilterLink *outlink)
static int libplacebo_query_format(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static double q2d_fallback(AVRational q, const double def)
static void update_crops(AVFilterContext *ctx, LibplaceboInput *in, struct pl_frame *target, double target_pts)
const struct pl_scene_t * pl_scene
static int append_inputs(AVFilterContext *avctx, int nb_inputs, int(*config_props)(AVFilterLink *link))
static int libplacebo_activate(AVFilterContext *ctx)
static int config_input_vulkan(AVFilterLink *inlink)
static enum pl_log_level get_log_level(void)
static void input_uninit(LibplaceboInput *input)
@ TONE_MAP_BT2390
@ TONE_MAP_BT2446A
@ TONE_MAP_SPLINE
@ TONE_MAP_ST2094_40
@ TONE_MAP_LINEAR
@ TONE_MAP_HABLE
@ TONE_MAP_AUTO
@ TONE_MAP_CLIP
@ TONE_MAP_GAMMA
@ TONE_MAP_ST2094_10
@ TONE_MAP_COUNT
@ TONE_MAP_REINHARD
@ TONE_MAP_MOBIUS
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:120
int len
#define RET(x)
Definition vulkan.h:37
int ff_vk_filter_config_input(AVFilterLink *inlink)
int ff_vk_filter_config_output(AVFilterLink *outlink)