vf_libplacebo: add support for specifying a LUT for the input

This makes it possible to apply Adobe .cube files to inputs.
This commit is contained in:
Lynne 2025-07-10 23:26:16 +09:00
parent 7838648be2
commit d3288013ab
No known key found for this signature in database
GPG key ID: A2FEA5F03F034464
2 changed files with 66 additions and 0 deletions

View file

@ -159,6 +159,7 @@ typedef struct LibplaceboContext {
pl_vulkan vulkan;
pl_gpu gpu;
pl_tex tex[4];
struct pl_custom_lut *lut;
/* dedicated renderer for linear output composition */
pl_renderer linear_rr;
@ -188,6 +189,8 @@ typedef struct LibplaceboContext {
AVExpr *pos_x_pexpr, *pos_y_pexpr, *pos_w_pexpr, *pos_h_pexpr;
float pad_crop_ratio;
float corner_rounding;
char *lut_filename;
enum pl_lut_type lut_type;
int force_original_aspect_ratio;
int force_divisible_by;
int reset_sar;
@ -375,6 +378,26 @@ static int find_scaler(AVFilterContext *avctx,
return AVERROR(EINVAL);
}
static int parse_custom_lut(LibplaceboContext *s)
{
int ret;
uint8_t *lutbuf;
size_t lutbuf_size;
if ((ret = av_file_map(s->lut_filename, &lutbuf, &lutbuf_size, 0, s)) < 0) {
av_log(s, AV_LOG_ERROR,
"The LUT file '%s' could not be read: %s\n",
s->lut_filename, av_err2str(ret));
return ret;
}
s->lut = pl_lut_parse_cube(s->log, lutbuf, lutbuf_size);
av_file_unmap(lutbuf, lutbuf_size);
if (!s->lut)
return AVERROR(EINVAL);
return 0;
}
static int update_settings(AVFilterContext *ctx)
{
int err = 0;
@ -733,6 +756,9 @@ static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwct
RET(parse_shader(avctx, buf, buf_len));
}
if (s->lut_filename)
RET(parse_custom_lut(s));
/* Initialize inputs */
s->inputs = av_calloc(s->nb_inputs, sizeof(*s->inputs));
if (!s->inputs)
@ -762,6 +788,7 @@ static void libplacebo_uninit(AVFilterContext *avctx)
av_freep(&s->inputs);
}
pl_lut_free(&s->lut);
#if PL_API_VER >= 351
pl_cache_destroy(&s->cache);
#endif
@ -1033,6 +1060,8 @@ static bool map_frame(pl_gpu gpu, pl_tex *tex,
.tex = tex,
.map_dovi = s->apply_dovi,
));
out->lut = s->lut;
out->lut_type = s->lut_type;
if (!s->apply_filmgrain)
out->film_grain.type = PL_FILM_GRAIN_NONE;
@ -1457,6 +1486,13 @@ static const AVOption libplacebo_options[] = {
{ "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 },
{ "fillcolor", "Background fill color", OFFSET(fillcolor), AV_OPT_TYPE_COLOR, {.str = "black@0"}, .flags = DYNAMIC },
{ "corner_rounding", "Corner rounding radius", OFFSET(corner_rounding), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, .flags = DYNAMIC },
{ "lut", "Path to custom LUT file to apply", OFFSET(lut_filename), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = STATIC },
{ "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" },
{ "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_UNKNOWN }, 0, 0, STATIC, .unit = "lut_type" },
{ "native", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NATIVE }, 0, 0, STATIC, .unit = "lut_type" },
{ "normalized", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NORMALIZED }, 0, 0, STATIC, .unit = "lut_type" },
{ "conversion", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_CONVERSION }, 0, 0, STATIC, .unit = "lut_type" },
{ "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", "Set shader cache path", OFFSET(shader_cache), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = STATIC },