History

avfilter/vf_scale: simplify color matrix parsing logic

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).

Niklas Haas committed 3 years ago

Commit 5d5bb771 parent ea955701 file · +22 −44Browse files

Unified · Split

libavfilter/vf_scale.c+22−44
@@ -139,8 +139,8 @@ typedef struct ScaleContext { · 138 unmodified lines
139139
140140 char *flags_str;
141141
142− char *in_color_matrix;
143− char *out_color_matrix;
142+ int in_color_matrix;
143+ int out_color_matrix;
144144
145145 int in_range;
146146 int in_frame_range;
@@ -410,30 +410,6 @@ static int query_formats(AVFilterContext *ctx) · 263 unmodified lines
410410 return 0;
411411 }
412412
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−
437413 static int scale_eval_dimensions(AVFilterContext *ctx)
438414 {
439415 ScaleContext *scale = ctx->priv;
@@ -554,7 +530,7 @@ static int config_props(AVFilterLink *outlink) · 114 unmodified lines
554530 scale->isws[0] = scale->isws[1] = scale->sws = NULL;
555531 if (inlink0->w == outlink->w &&
556532 inlink0->h == outlink->h &&
557− !scale->out_color_matrix &&
533+ scale->out_color_matrix == AVCOL_SPC_UNSPECIFIED &&
558534 scale->in_range == scale->out_range &&
559535 inlink0->format == outlink->format)
560536 ;
@@ -840,8 +816,8 @@ scale: · 279 unmodified lines
840816
841817 in_range = in->color_range;
842818
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
845821 || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
846822 || in_range != AVCOL_RANGE_UNSPECIFIED
847823 || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
@@ -852,11 +828,13 @@ scale: · 4 unmodified lines
852828 (int **)&table, &out_full,
853829 &brightness, &contrast, &saturation);
854830
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)
860838 table = inv_table;
861839
862840 if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
@@ -1003,16 +981,16 @@ static const AVOption scale_options[] = { · 140 unmodified lines
1003981 { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_BOOL, {.i64 = 0 }, -1, 1, FLAGS },
1004982 { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
1005983 { "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" },
1016994 { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
1017995 { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
1018996 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },

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