No need to write a custom string parser when we can just use an integer option with preset values. The various bits of fallback logic are wholly redundant with equivalent logic already inside sws_getCoefficients. Note: I disallowed setting 'out_color_matrix=auto', because this does not do anything meaningful in the current code (just hard-codes AVCOL_SPC_BT470BG fallback).
Unified · Split
| @@ -139,8 +139,8 @@ typedef struct ScaleContext { · 138 unmodified lines | ||
| 139 | 139 | |
| 140 | 140 | char *flags_str; |
| 141 | 141 | |
| 142 | − char *in_color_matrix; | |
| 143 | − char *out_color_matrix; | |
| 142 | + int in_color_matrix; | |
| 143 | + int out_color_matrix; | |
| 144 | 144 | |
| 145 | 145 | int in_range; |
| 146 | 146 | int in_frame_range; |
| @@ -410,30 +410,6 @@ static int query_formats(AVFilterContext *ctx) · 263 unmodified lines | ||
| 410 | 410 | return 0; |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | −static const int *parse_yuv_type(const char *s, enum AVColorSpace colorspace) | |
| 414 | −{ | |
| 415 | − if (!s) | |
| 416 | − s = "bt601"; | |
| 417 | − | |
| 418 | − if (s && strstr(s, "bt709")) { | |
| 419 | − colorspace = AVCOL_SPC_BT709; | |
| 420 | − } else if (s && strstr(s, "fcc")) { | |
| 421 | − colorspace = AVCOL_SPC_FCC; | |
| 422 | − } else if (s && strstr(s, "smpte240m")) { | |
| 423 | − colorspace = AVCOL_SPC_SMPTE240M; | |
| 424 | − } else if (s && (strstr(s, "bt601") || strstr(s, "bt470") || strstr(s, "smpte170m"))) { | |
| 425 | − colorspace = AVCOL_SPC_BT470BG; | |
| 426 | − } else if (s && strstr(s, "bt2020")) { | |
| 427 | − colorspace = AVCOL_SPC_BT2020_NCL; | |
| 428 | − } | |
| 429 | − | |
| 430 | − if (colorspace < 1 || colorspace > 10 || colorspace == 8) { | |
| 431 | − colorspace = AVCOL_SPC_BT470BG; | |
| 432 | − } | |
| 433 | − | |
| 434 | − return sws_getCoefficients(colorspace); | |
| 435 | −} | |
| 436 | − | |
| 437 | 413 | static int scale_eval_dimensions(AVFilterContext *ctx) |
| 438 | 414 | { |
| 439 | 415 | ScaleContext *scale = ctx->priv; |
| @@ -554,7 +530,7 @@ static int config_props(AVFilterLink *outlink) · 114 unmodified lines | ||
| 554 | 530 | scale->isws[0] = scale->isws[1] = scale->sws = NULL; |
| 555 | 531 | if (inlink0->w == outlink->w && |
| 556 | 532 | inlink0->h == outlink->h && |
| 557 | − !scale->out_color_matrix && | |
| 533 | + scale->out_color_matrix == AVCOL_SPC_UNSPECIFIED && | |
| 558 | 534 | scale->in_range == scale->out_range && |
| 559 | 535 | inlink0->format == outlink->format) |
| 560 | 536 | ; |
| @@ -840,8 +816,8 @@ scale: · 279 unmodified lines | ||
| 840 | 816 | |
| 841 | 817 | in_range = in->color_range; |
| 842 | 818 | |
| 843 | − if ( scale->in_color_matrix | |
| 844 | − || scale->out_color_matrix | |
| 819 | + if ( scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED | |
| 820 | + || scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED | |
| 845 | 821 | || scale-> in_range != AVCOL_RANGE_UNSPECIFIED |
| 846 | 822 | || in_range != AVCOL_RANGE_UNSPECIFIED |
| 847 | 823 | || scale->out_range != AVCOL_RANGE_UNSPECIFIED) { |
| @@ -852,11 +828,13 @@ scale: · 4 unmodified lines | ||
| 852 | 828 | (int **)&table, &out_full, |
| 853 | 829 | &brightness, &contrast, &saturation); |
| 854 | 830 | |
| 855 | − if (scale->in_color_matrix) | |
| 856 | − inv_table = parse_yuv_type(scale->in_color_matrix, in->colorspace); | |
| 857 | − if (scale->out_color_matrix) | |
| 858 | − table = parse_yuv_type(scale->out_color_matrix, AVCOL_SPC_UNSPECIFIED); | |
| 859 | − else if (scale->in_color_matrix) | |
| 831 | + if (scale->in_color_matrix == -1 /* auto */) | |
| 832 | + inv_table = sws_getCoefficients(in->colorspace); | |
| 833 | + else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED) | |
| 834 | + inv_table = sws_getCoefficients(scale->in_color_matrix); | |
| 835 | + if (scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED) | |
| 836 | + table = sws_getCoefficients(scale->out_color_matrix); | |
| 837 | + else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED) | |
| 860 | 838 | table = inv_table; |
| 861 | 839 | |
| 862 | 840 | if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED) |
| @@ -1003,16 +981,16 @@ static const AVOption scale_options[] = { · 140 unmodified lines | ||
| 1003 | 981 | { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_BOOL, {.i64 = 0 }, -1, 1, FLAGS }, |
| 1004 | 982 | { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS }, |
| 1005 | 983 | { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS }, |
| 1006 | − { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_STRING, { .str = "auto" }, .flags = FLAGS, "color" }, | |
| 1007 | − { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS, "color"}, | |
| 1008 | − { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .str = "auto" }, 0, 0, FLAGS, "color" }, | |
| 1009 | − { "bt601", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt601" }, 0, 0, FLAGS, "color" }, | |
| 1010 | − { "bt470", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt470" }, 0, 0, FLAGS, "color" }, | |
| 1011 | − { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, { .str = "smpte170m" }, 0, 0, FLAGS, "color" }, | |
| 1012 | − { "bt709", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt709" }, 0, 0, FLAGS, "color" }, | |
| 1013 | − { "fcc", NULL, 0, AV_OPT_TYPE_CONST, { .str = "fcc" }, 0, 0, FLAGS, "color" }, | |
| 1014 | − { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, { .str = "smpte240m" }, 0, 0, FLAGS, "color" }, | |
| 1015 | − { "bt2020", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt2020" }, 0, 0, FLAGS, "color" }, | |
| 984 | + { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AVCOL_SPC_NB-1, .flags = FLAGS, "color" }, | |
| 985 | + { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, AVCOL_SPC_NB-1, .flags = FLAGS, "color"}, | |
| 986 | + { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, FLAGS, "color" }, | |
| 987 | + { "bt601", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, "color" }, | |
| 988 | + { "bt470", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, "color" }, | |
| 989 | + { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, "color" }, | |
| 990 | + { "bt709", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT709 }, 0, 0, FLAGS, "color" }, | |
| 991 | + { "fcc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_FCC }, 0, 0, FLAGS, "color" }, | |
| 992 | + { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE240M }, 0, 0, FLAGS, "color" }, | |
| 993 | + { "bt2020", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT2020_NCL }, 0, 0, FLAGS, "color" }, | |
| 1016 | 994 | { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" }, |
| 1017 | 995 | { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" }, |
| 1018 | 996 | { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" }, |