From c83e625e0bb576f900713130035307f93b10a4cb Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Thu, 4 Jul 2024 12:52:08 +0200 Subject: [PATCH] avfilter/vf_setparams: allow setting chroma location Shockingly, there isn't currently _any_ filter for overriding this. Signed-off-by: Paul B Mahol --- doc/filters.texi | 17 +++++++++++++++++ libavfilter/avfilter.c | 6 +++--- libavfilter/avfilter.h | 5 ----- libavfilter/avfiltergraph.c | 2 +- libavfilter/filters.h | 5 +++++ libavfilter/vf_setparams.c | 19 ++++++++++++++++--- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index a206b81ea9..464491770b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -21686,6 +21686,23 @@ Keep the same colorspace property (default). @item chroma-derived-c @item ictcp @end table + +@item chroma_location +Set the chroma sample location. +Available values are: + +@table @samp +@item auto +Keep the same chroma location (default). + +@item unspecified, unknown +@item left +@item center +@item topleft +@item top +@item bottomleft +@item bottom +@end table @end table @section sharpen_npp diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 055db01e02..43d7c9f4ab 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -185,7 +185,7 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad, link->srcpad = &src->output_pads[srcpad]; link->dstpad = &dst->input_pads[dstpad]; link->type = src->output_pads[srcpad].type; - link->graph = src->graph; + li->l.graph = src->graph; av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1); link->format = -1; link->colorspace = AVCOL_SPC_UNSPECIFIED; @@ -231,8 +231,8 @@ static void update_link_current_pts(FilterLinkInternal *li, int64_t pts) li->l.current_pts = pts; li->l.current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q); /* TODO use duration */ - if (link->graph && li->age_index >= 0) - ff_avfilter_graph_update_heap(link->graph, li); + if (li->l.graph && li->age_index >= 0) + ff_avfilter_graph_update_heap(li->l.graph, li); } void ff_filter_set_ready(AVFilterContext *filter, unsigned priority) diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h index f285c1d429..95b0207ed6 100644 --- a/libavfilter/avfilter.h +++ b/libavfilter/avfilter.h @@ -595,11 +595,6 @@ struct AVFilterLink { * Lists of supported formats / etc. supported by the output filter. */ AVFilterFormatsConfig outcfg; - - /** - * Graph the filter belongs to. - */ - struct AVFilterGraph *graph; }; /** diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index 2bc7076aa9..fbdccf879e 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -105,7 +105,7 @@ void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter filter->graph = NULL; for (j = 0; jnb_outputs; j++) if (filter->outputs[j]) - filter->outputs[j]->graph = NULL; + ff_filter_link(filter->outputs[j])->graph = NULL; return; } diff --git a/libavfilter/filters.h b/libavfilter/filters.h index 36164c171e..ab16047e12 100644 --- a/libavfilter/filters.h +++ b/libavfilter/filters.h @@ -42,6 +42,11 @@ typedef struct FilterLink { AVFilterLink pub; + /** + * Graph the filter belongs to. + */ + struct AVFilterGraph *graph; + /** * Current timestamp of the link, as defined by the most recent * frame(s), in link time_base units. diff --git a/libavfilter/vf_setparams.c b/libavfilter/vf_setparams.c index 9705dcdc35..5b4ef8f882 100644 --- a/libavfilter/vf_setparams.c +++ b/libavfilter/vf_setparams.c @@ -41,6 +41,7 @@ typedef struct SetParamsContext { int color_primaries; int color_trc; int colorspace; + int chroma_location; } SetParamsContext; #define OFFSET(x) offsetof(SetParamsContext, x) @@ -119,6 +120,17 @@ static const AVOption setparams_options[] = { {"chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, 0, 0, FLAGS, .unit = "colorspace"}, {"ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, 0, 0, FLAGS, .unit = "colorspace"}, {"ipt-c2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_IPT_C2}, 0, 0, FLAGS, .unit = "colorspace"}, + + {"chroma_location", "select chroma sample location", OFFSET(chroma_location), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCHROMA_LOC_NB-1, FLAGS, .unit = "chroma_location"}, + {"auto", "keep the same chroma location", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"left", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_LEFT}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"center", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_CENTER}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"topleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOPLEFT}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"top", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOP}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"bottomleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOMLEFT}, 0, 0, FLAGS, .unit = "chroma_location"}, + {"bottom", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOM}, 0, 0, FLAGS, .unit = "chroma_location"}, {NULL} }; @@ -174,17 +186,18 @@ FF_ENABLE_DEPRECATION_WARNINGS frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST; } - /* set range */ + /* set straightforward parameters */ if (s->color_range >= 0) frame->color_range = s->color_range; - - /* set color prim, trc, space */ if (s->color_primaries >= 0) frame->color_primaries = s->color_primaries; if (s->color_trc >= 0) frame->color_trc = s->color_trc; if (s->colorspace >= 0) frame->colorspace = s->colorspace; + if (s->chroma_location >= 0) + frame->chroma_location = s->chroma_location; + return ff_filter_frame(ctx->outputs[0], frame); }