History

avfilter: properly reduce YUV colorspace format lists

Doing this with REDUCE_FORMATS() instead of swap_color_*() is not only shorter, but more importantly comes with the benefit of being done inside a loop, allowing us to correctly propagate complex graphs involving multiple conversion filters (e.g. -vf scale,zscale). The latter family of swapping functions is only used to settle the best *remaining* entry if no exact match was found, and as such was never the correct solution to YUV colorspaces, which only care about exact matches.

Niklas Haas committed 2 years ago

Commit b89ee261 parent 189c32f1 file · +4 −80Browse files

Showing the changes to libavfilter/avfiltergraph.c (1 of 1 file). Show all

Unified · Split

libavfilter/avfiltergraph.c+4−80
@@ -794,6 +794,10 @@ static int reduce_formats_on_filter(AVFilterContext *filter) · 793 unmodified lines
794794 nb_formats, ff_add_format);
795795 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
796796 nb_formats, ff_add_format);
797+ REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
798+ nb_formats, ff_add_format);
799+ REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
800+ nb_formats, ff_add_format);
797801
798802 /* reduce channel layouts */
799803 for (i = 0; i < filter->nb_inputs; i++) {
@@ -906,82 +910,6 @@ static void swap_samplerates(AVFilterGraph *graph) · 106 unmodified lines
906910 swap_samplerates_on_filter(graph->filters[i]);
907911 }
908912
909−static void swap_color_spaces_on_filter(AVFilterContext *filter)
910−{
911− AVFilterLink *link = NULL;
912− enum AVColorSpace csp;
913− int i;
914−
915− for (i = 0; i < filter->nb_inputs; i++) {
916− link = filter->inputs[i];
917− if (link->type == AVMEDIA_TYPE_VIDEO &&
918− link->outcfg.color_spaces->nb_formats == 1)
919− break;
920− }
921− if (i == filter->nb_inputs)
922− return;
923−
924− csp = link->outcfg.color_spaces->formats[0];
925−
926− for (i = 0; i < filter->nb_outputs; i++) {
927− AVFilterLink *outlink = filter->outputs[i];
928− if (outlink->type != AVMEDIA_TYPE_VIDEO)
929− continue;
930− /* there is no meaningful 'score' between different yuv matrices,
931− * so just prioritize an exact match if it exists */
932− for (int j = 0; j < outlink->incfg.color_spaces->nb_formats; j++) {
933− if (csp == outlink->incfg.color_spaces->formats[j]) {
934− FFSWAP(int, outlink->incfg.color_spaces->formats[0],
935− outlink->incfg.color_spaces->formats[j]);
936− break;
937− }
938− }
939− }
940−}
941−
942−static void swap_color_spaces(AVFilterGraph *graph)
943−{
944− for (int i = 0; i < graph->nb_filters; i++)
945− swap_color_spaces_on_filter(graph->filters[i]);
946−}
947−
948−static void swap_color_ranges_on_filter(AVFilterContext *filter)
949−{
950− AVFilterLink *link = NULL;
951− enum AVColorRange range;
952− int i;
953−
954− for (i = 0; i < filter->nb_inputs; i++) {
955− link = filter->inputs[i];
956− if (link->type == AVMEDIA_TYPE_VIDEO &&
957− link->outcfg.color_ranges->nb_formats == 1)
958− break;
959− }
960− if (i == filter->nb_inputs)
961− return;
962−
963− range = link->outcfg.color_ranges->formats[0];
964−
965− for (i = 0; i < filter->nb_outputs; i++) {
966− AVFilterLink *outlink = filter->outputs[i];
967− if (outlink->type != AVMEDIA_TYPE_VIDEO)
968− continue;
969− for (int j = 0; j < outlink->incfg.color_ranges->nb_formats; j++) {
970− if (range == outlink->incfg.color_ranges->formats[j]) {
971− FFSWAP(int, outlink->incfg.color_ranges->formats[0],
972− outlink->incfg.color_ranges->formats[j]);
973− break;
974− }
975− }
976− }
977−}
978−
979−static void swap_color_ranges(AVFilterGraph *graph)
980−{
981− for (int i = 0; i < graph->nb_filters; i++)
982− swap_color_ranges_on_filter(graph->filters[i]);
983−}
984−
985913 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
986914 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
987915 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
@@ -1258,10 +1186,6 @@ static int graph_config_formats(AVFilterGraph *graph, void *log_ctx) · 270 unmodified lines
12581186 if ((ret = reduce_formats(graph)) < 0)
12591187 return ret;
12601188
1261− /* for video filters, ensure that the best colorspace metadata is selected */
1262− swap_color_spaces(graph);
1263− swap_color_ranges(graph);
1264−
12651189 /* for audio filters, ensure the best format, sample rate and channel layout
12661190 * is selected */
12671191 swap_sample_fmts(graph);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t