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.
Showing the changes to libavfilter/avfiltergraph.c (1 of 1 file). Show all
Unified · Split
| @@ -794,6 +794,10 @@ static int reduce_formats_on_filter(AVFilterContext *filter) · 793 unmodified lines | ||
| 794 | 794 | nb_formats, ff_add_format); |
| 795 | 795 | REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats, |
| 796 | 796 | 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); | |
| 797 | 801 | |
| 798 | 802 | /* reduce channel layouts */ |
| 799 | 803 | for (i = 0; i < filter->nb_inputs; i++) { |
| @@ -906,82 +910,6 @@ static void swap_samplerates(AVFilterGraph *graph) · 106 unmodified lines | ||
| 906 | 910 | swap_samplerates_on_filter(graph->filters[i]); |
| 907 | 911 | } |
| 908 | 912 | |
| 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 | − | |
| 985 | 913 | #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER) |
| 986 | 914 | #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT) |
| 987 | 915 | #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 | ||
| 1258 | 1186 | if ((ret = reduce_formats(graph)) < 0) |
| 1259 | 1187 | return ret; |
| 1260 | 1188 | |
| 1261 | − /* for video filters, ensure that the best colorspace metadata is selected */ | |
| 1262 | − swap_color_spaces(graph); | |
| 1263 | − swap_color_ranges(graph); | |
| 1264 | − | |
| 1265 | 1189 | /* for audio filters, ensure the best format, sample rate and channel layout |
| 1266 | 1190 | * is selected */ |
| 1267 | 1191 | swap_sample_fmts(graph); |