[FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Thu May 15 14:08:12 EEST 2025


Niklas Haas:
> From: Niklas Haas <git at haasn.dev>
> 
> Useful to speed up shader compilation. May significantly lower startup
> times, in particular with large or complex shaders.
> 
> Sponsored-by: nxtedition
> ---
>  doc/filters.texi            |  5 +++++
>  libavfilter/vf_libplacebo.c | 29 +++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 679b71f290..66cb3e6c20 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -16307,6 +16307,11 @@ as a list of @var{key}=@var{value} pairs separated by ':'. The following example
>  shows how to configure a custom filter kernel ("EWA LanczosSharp") and use it
>  to double the input image resolution:
>  
> + at item shader_cache_dir
> +If set to the path of a directory that exists, libplacebo will store and use
> +cached shader objects in this directory. This cache is not cleaned up
> +automatically.
> +
>  @example
>  -vf "libplacebo=w=iw*2:h=ih*2:extra_opts='upscaler=custom\:upscaler_preset=ewa_lanczos\:upscaler_blur=0.9812505644269356'"
>  @end example
> diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
> index 86e1f43dea..ca7d9e253a 100644
> --- a/libavfilter/vf_libplacebo.c
> +++ b/libavfilter/vf_libplacebo.c
> @@ -195,6 +195,11 @@ typedef struct LibplaceboContext {
>      int color_trc;
>      AVDictionary *extra_opts;
>  
> +#if PL_API_VER >= 351
> +    pl_cache cache;
> +    char *shader_cache_dir;
> +#endif
> +
>      int have_hwdevice;
>  
>      /* pl_render_params */
> @@ -522,6 +527,21 @@ static int libplacebo_init(AVFilterContext *avctx)
>          return AVERROR(ENOMEM);
>      }
>  
> +#if PL_API_VER >= 351
> +    if (s->shader_cache_dir && s->shader_cache_dir[0]) {
> +        s->cache = pl_cache_create(pl_cache_params(
> +            .log  = s->log,
> +            .get  = pl_cache_get_dir,
> +            .set  = pl_cache_set_dir,
> +            .priv = s->shader_cache_dir,
> +        ));
> +        if (!s->cache) {
> +            libplacebo_uninit(avctx);
> +            return AVERROR(ENOMEM);
> +        }
> +    }
> +#endif
> +
>      if (s->out_format_string) {
>          s->out_format = av_get_pix_fmt(s->out_format_string);
>          if (s->out_format == AV_PIX_FMT_NONE) {
> @@ -676,6 +696,9 @@ static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwct
>      }
>  
>      s->gpu = s->vulkan->gpu;
> +#if PL_API_VER >= 351
> +    pl_gpu_set_cache(s->gpu, s->cache);
> +#endif
>  
>      /* Parse the user shaders, if requested */
>      if (s->shader_bin_len)
> @@ -714,6 +737,9 @@ static void libplacebo_uninit(AVFilterContext *avctx)
>          av_freep(&s->inputs);
>      }
>  
> +#if PL_API_VER >= 351
> +    pl_cache_destroy(&s->cache);
> +#endif
>      pl_options_free(&s->opts);
>      pl_vulkan_destroy(&s->vulkan);
>      pl_log_destroy(&s->log);
> @@ -1328,6 +1354,9 @@ static const AVOption libplacebo_options[] = {
>      { "fillcolor", "Background fill color", OFFSET(fillcolor), AV_OPT_TYPE_COLOR, {.str = "black at 0"}, .flags = DYNAMIC },
>      { "corner_rounding", "Corner rounding radius", OFFSET(corner_rounding), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, .flags = DYNAMIC },
>      { "extra_opts", "Pass extra libplacebo-specific options using a :-separated list of key=value pairs", OFFSET(extra_opts), AV_OPT_TYPE_DICT, .flags = DYNAMIC },
> +#if PL_API_VER >= 351
> +    { "shader_cache_dir",  "Set shader cache directory", OFFSET(shader_cache_dir), AV_OPT_TYPE_STRING, {.str=""}, .flags = STATIC },

Why do you set the default to "" instead of NULL? This gives an
allocation even when the option is never set and makes the check for
whether it is set unnecessarily complicated.

> +#endif
>  
>      {"colorspace", "select colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_NB-1, DYNAMIC, .unit = "colorspace"},
>      {"auto", "keep the same colorspace",  0, AV_OPT_TYPE_CONST, {.i64=-1},                          INT_MIN, INT_MAX, STATIC, .unit = "colorspace"},



More information about the ffmpeg-devel mailing list