History

vf_scale: use colorspace negotiation API

This filter will always accept any input format, even if the user sets a specific in_range/in_color_matrix. This is to preserve status quo with current behavior, where passing a specific in_color_matrix merely overrides the incoming frames' attributes. (Use `vf_format` to force a specific input range) Because changing colorspace and color_range now requires reconfiguring the link, we can lift sws_setColorspaceDetails out of scale_frame and into config_props. (This will also get re-called if the input frame properties change)

Niklas Haas committed 3 years ago

Commit 45e09a31 parent 9613ba91 file · +103 −85Browse files

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

Unified · Split

libavfilter/vf_scale.c+103−85
@@ -143,7 +143,6 @@ typedef struct ScaleContext { · 142 unmodified lines
143143 int out_color_matrix;
144144
145145 int in_range;
146− int in_frame_range;
147146 int out_range;
148147
149148 int out_h_chr_pos;
@@ -292,6 +291,18 @@ static av_cold int preinit(AVFilterContext *ctx) · 142 unmodified lines
292291 return 0;
293292 }
294293
294+static const int sws_colorspaces[] = {
295+ AVCOL_SPC_UNSPECIFIED,
296+ AVCOL_SPC_RGB,
297+ AVCOL_SPC_BT709,
298+ AVCOL_SPC_BT470BG,
299+ AVCOL_SPC_SMPTE170M,
300+ AVCOL_SPC_FCC,
301+ AVCOL_SPC_SMPTE240M,
302+ AVCOL_SPC_BT2020_NCL,
303+ -1
304+};
305+
295306 static av_cold int init(AVFilterContext *ctx)
296307 {
297308 ScaleContext *scale = ctx->priv;
@@ -332,6 +343,19 @@ static av_cold int init(AVFilterContext *ctx) · 34 unmodified lines
332343 if (ret < 0)
333344 return ret;
334345
346+ if (scale->in_color_matrix != -1 &&
347+ !ff_fmt_is_in(scale->in_color_matrix, sws_colorspaces)) {
348+ av_log(ctx, AV_LOG_ERROR, "Unsupported input color matrix '%s'\n",
349+ av_color_space_name(scale->in_color_matrix));
350+ return AVERROR(EINVAL);
351+ }
352+
353+ if (!ff_fmt_is_in(scale->out_color_matrix, sws_colorspaces)) {
354+ av_log(ctx, AV_LOG_ERROR, "Unsupported output color matrix '%s'\n",
355+ av_color_space_name(scale->out_color_matrix));
356+ return AVERROR(EINVAL);
357+ }
358+
335359 av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
336360 scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
337361
@@ -356,8 +380,6 @@ static av_cold int init(AVFilterContext *ctx) · 18 unmodified lines
356380 if (!threads)
357381 av_opt_set_int(scale->sws_opts, "threads", ff_filter_get_nb_threads(ctx), 0);
358382
359− scale->in_frame_range = AVCOL_RANGE_UNSPECIFIED;
360−
361383 return 0;
362384 }
363385
@@ -376,6 +398,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 12 unmodified lines
376398
377399 static int query_formats(AVFilterContext *ctx)
378400 {
401+ ScaleContext *scale = ctx->priv;
379402 AVFilterFormats *formats;
380403 const AVPixFmtDescriptor *desc;
381404 enum AVPixelFormat pix_fmt;
@@ -407,6 +430,28 @@ static int query_formats(AVFilterContext *ctx) · 25 unmodified lines
407430 if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->incfg.formats)) < 0)
408431 return ret;
409432
433+ /* accept all supported inputs, even if user overrides their properties */
434+ if ((ret = ff_formats_ref(ff_make_format_list(sws_colorspaces),
435+ &ctx->inputs[0]->outcfg.color_spaces)) < 0)
436+ return ret;
437+
438+ if ((ret = ff_formats_ref(ff_all_color_ranges(),
439+ &ctx->inputs[0]->outcfg.color_ranges)) < 0)
440+ return ret;
441+
442+ /* propagate output properties if overridden */
443+ formats = scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED
444+ ? ff_make_formats_list_singleton(scale->out_color_matrix)
445+ : ff_make_format_list(sws_colorspaces);
446+ if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->incfg.color_spaces)) < 0)
447+ return ret;
448+
449+ formats = scale->out_range != AVCOL_RANGE_UNSPECIFIED
450+ ? ff_make_formats_list_singleton(scale->out_range)
451+ : ff_all_color_ranges();
452+ if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->incfg.color_ranges)) < 0)
453+ return ret;
454+
410455 return 0;
411456 }
412457
@@ -497,6 +542,7 @@ static int config_props(AVFilterLink *outlink) · 84 unmodified lines
497542 const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(outfmt);
498543 ScaleContext *scale = ctx->priv;
499544 uint8_t *flags_val = NULL;
545+ int in_range, in_colorspace;
500546 int ret;
501547
502548 if ((ret = scale_eval_dimensions(ctx)) < 0)
@@ -521,6 +567,14 @@ static int config_props(AVFilterLink *outlink) · 18 unmodified lines
521567 if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
522568 scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL;
523569
570+ in_range = scale->in_range;
571+ if (in_range == AVCOL_RANGE_UNSPECIFIED)
572+ in_range = inlink0->color_range;
573+
574+ in_colorspace = scale->in_color_matrix;
575+ if (in_colorspace == -1 /* auto */)
576+ in_colorspace = inlink0->colorspace;
577+
524578 if (scale->sws)
525579 sws_freeContext(scale->sws);
526580 if (scale->isws[0])
@@ -530,8 +584,8 @@ static int config_props(AVFilterLink *outlink) · 3 unmodified lines
530584 scale->isws[0] = scale->isws[1] = scale->sws = NULL;
531585 if (inlink0->w == outlink->w &&
532586 inlink0->h == outlink->h &&
533− scale->out_color_matrix == AVCOL_SPC_UNSPECIFIED &&
534− scale->in_range == scale->out_range &&
587+ in_range == outlink->color_range &&
588+ in_colorspace == outlink->colorspace &&
535589 inlink0->format == outlink->format)
536590 ;
537591 else {
@@ -540,6 +594,8 @@ static int config_props(AVFilterLink *outlink) · 2 unmodified lines
540594
541595 for (i = 0; i < 3; i++) {
542596 int in_v_chr_pos = scale->in_v_chr_pos, out_v_chr_pos = scale->out_v_chr_pos;
597+ int in_full, out_full, brightness, contrast, saturation;
598+ const int *inv_table, *table;
543599 struct SwsContext *const s = sws_alloc_context();
544600 if (!s)
545601 return AVERROR(ENOMEM);
@@ -555,15 +611,12 @@ static int config_props(AVFilterLink *outlink) · 9 unmodified lines
555611 av_opt_set_int(s, "dstw", outlink->w, 0);
556612 av_opt_set_int(s, "dsth", outlink->h >> !!i, 0);
557613 av_opt_set_int(s, "dst_format", outfmt, 0);
558− if (scale->in_range != AVCOL_RANGE_UNSPECIFIED)
559− av_opt_set_int(s, "src_range",
560− scale->in_range == AVCOL_RANGE_JPEG, 0);
561− else if (scale->in_frame_range != AVCOL_RANGE_UNSPECIFIED)
614+ if (in_range != AVCOL_RANGE_UNSPECIFIED)
562615 av_opt_set_int(s, "src_range",
563− scale->in_frame_range == AVCOL_RANGE_JPEG, 0);
564− if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
616+ in_range == AVCOL_RANGE_JPEG, 0);
617+ if (outlink->color_range != AVCOL_RANGE_UNSPECIFIED)
565618 av_opt_set_int(s, "dst_range",
566− scale->out_range == AVCOL_RANGE_JPEG, 0);
619+ outlink->color_range == AVCOL_RANGE_JPEG, 0);
567620
568621 /* Override chroma location default settings to have the correct
569622 * chroma positions. MPEG chroma positions are used by convention.
@@ -584,6 +637,24 @@ static int config_props(AVFilterLink *outlink) · 14 unmodified lines
584637
585638 if ((ret = sws_init_context(s, NULL, NULL)) < 0)
586639 return ret;
640+
641+ sws_getColorspaceDetails(s, (int **)&inv_table, &in_full,
642+ (int **)&table, &out_full,
643+ &brightness, &contrast, &saturation);
644+
645+ if (scale->in_color_matrix == -1 /* auto */)
646+ inv_table = sws_getCoefficients(inlink0->colorspace);
647+ else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED)
648+ inv_table = sws_getCoefficients(scale->in_color_matrix);
649+ if (outlink->colorspace != AVCOL_SPC_UNSPECIFIED)
650+ table = sws_getCoefficients(outlink->colorspace);
651+ else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED)
652+ table = inv_table;
653+
654+ sws_setColorspaceDetails(s, inv_table, in_full,
655+ table, out_full,
656+ brightness, contrast, saturation);
657+
587658 if (!scale->interlaced)
588659 break;
589660 }
@@ -597,10 +668,12 @@ static int config_props(AVFilterLink *outlink) · 7 unmodified lines
597668 if (scale->sws)
598669 av_opt_get(scale->sws, "sws_flags", 0, &flags_val);
599670
600− av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:%s\n",
671+ av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s csp:%s range:%s sar:%d/%d -> w:%d h:%d fmt:%s csp:%s range:%s sar:%d/%d flags:%s\n",
601672 inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
673+ av_color_space_name(inlink->colorspace), av_color_range_name(inlink->color_range),
602674 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
603675 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
676+ av_color_space_name(outlink->colorspace), av_color_range_name(outlink->color_range),
604677 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
605678 flags_val);
606679 av_freep(&flags_val);
@@ -620,6 +693,8 @@ static int config_props_ref(AVFilterLink *outlink) · 13 unmodified lines
620693 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
621694 outlink->time_base = inlink->time_base;
622695 outlink->frame_rate = inlink->frame_rate;
696+ outlink->colorspace = inlink->colorspace;
697+ outlink->color_range = inlink->color_range;
623698
624699 return 0;
625700 }
@@ -693,7 +768,6 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 67 unmodified lines
693768 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
694769 char buf[32];
695770 int ret;
696− int in_range;
697771 int frame_changed;
698772
699773 *frame_out = NULL;
@@ -704,14 +778,9 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 4 unmodified lines
704778 in->height != link->h ||
705779 in->format != link->format ||
706780 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
707− in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
708−
709− if (in->color_range != AVCOL_RANGE_UNSPECIFIED &&
710− scale->in_range == AVCOL_RANGE_UNSPECIFIED &&
711− in->color_range != scale->in_frame_range) {
712− scale->in_frame_range = in->color_range;
713− frame_changed = 1;
714− }
781+ in->sample_aspect_ratio.num != link->sample_aspect_ratio.num ||
782+ in->colorspace != link->colorspace ||
783+ in->color_range != link->color_range;
715784
716785 if (scale->eval_mode == EVAL_MODE_FRAME || frame_changed) {
717786 unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };
@@ -768,9 +837,11 @@ FF_ENABLE_DEPRECATION_WARNINGS · 50 unmodified lines
768837 #endif
769838 }
770839
771− link->dst->inputs[0]->format = in->format;
772− link->dst->inputs[0]->w = in->width;
773− link->dst->inputs[0]->h = in->height;
840+ link->dst->inputs[0]->format = in->format;
841+ link->dst->inputs[0]->w = in->width;
842+ link->dst->inputs[0]->h = in->height;
843+ link->dst->inputs[0]->colorspace = in->colorspace;
844+ link->dst->inputs[0]->color_range = in->color_range;
774845
775846 link->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
776847 link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
@@ -798,69 +869,12 @@ scale: · 21 unmodified lines
798869 av_frame_copy_props(out, in);
799870 out->width = outlink->w;
800871 out->height = outlink->h;
872+ out->color_range = outlink->color_range;
873+ out->colorspace = outlink->colorspace;
801874
802875 if (scale->output_is_pal)
803876 avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
804877
805− in_range = in->color_range;
806−
807− if ( scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED
808− || scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED
809− || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
810− || in_range != AVCOL_RANGE_UNSPECIFIED
811− || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
812− int in_full, out_full, brightness, contrast, saturation;
813− const int *inv_table, *table;
814−
815− sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full,
816− (int **)&table, &out_full,
817− &brightness, &contrast, &saturation);
818−
819− if (scale->in_color_matrix == -1 /* auto */)
820− inv_table = sws_getCoefficients(in->colorspace);
821− else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED)
822− inv_table = sws_getCoefficients(scale->in_color_matrix);
823− if (scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED)
824− table = sws_getCoefficients(scale->out_color_matrix);
825− else if (scale->in_color_matrix != AVCOL_SPC_UNSPECIFIED)
826− table = inv_table;
827−
828− if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
829− in_full = (scale-> in_range == AVCOL_RANGE_JPEG);
830− else if (in_range != AVCOL_RANGE_UNSPECIFIED)
831− in_full = (in_range == AVCOL_RANGE_JPEG);
832− if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
833− out_full = (scale->out_range == AVCOL_RANGE_JPEG);
834−
835− sws_setColorspaceDetails(scale->sws, inv_table, in_full,
836− table, out_full,
837− brightness, contrast, saturation);
838− if (scale->isws[0])
839− sws_setColorspaceDetails(scale->isws[0], inv_table, in_full,
840− table, out_full,
841− brightness, contrast, saturation);
842− if (scale->isws[1])
843− sws_setColorspaceDetails(scale->isws[1], inv_table, in_full,
844− table, out_full,
845− brightness, contrast, saturation);
846−
847− out->color_range = out_full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
848− if (scale->out_color_matrix != AVCOL_SPC_UNSPECIFIED)
849− out->colorspace = scale->out_color_matrix;
850− }
851−
852− // Sanity checks:
853− // 1. If the output is RGB, set the matrix coefficients to RGB.
854− // 2. If the output is not RGB and we've got the RGB/XYZ (identity)
855− // matrix configured, unset the matrix.
856− // In theory these should be in swscale itself as the AVFrame
857− // based API gets in, so that not every swscale API user has
858− // to go through duplicating such sanity checks.
859− if (av_pix_fmt_desc_get(out->format)->flags & AV_PIX_FMT_FLAG_RGB)
860− out->colorspace = AVCOL_SPC_RGB;
861− else if (out->colorspace == AVCOL_SPC_RGB)
862− out->colorspace = AVCOL_SPC_UNSPECIFIED;
863−
864878 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
865879 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
866880 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
@@ -905,7 +919,9 @@ static int filter_frame_ref(AVFilterLink *link, AVFrame *in) · 38 unmodified lines
905919 in->height != link->h ||
906920 in->format != link->format ||
907921 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
908− in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
922+ in->sample_aspect_ratio.num != link->sample_aspect_ratio.num ||
923+ in->colorspace != link->colorspace ||
924+ in->color_range != link->color_range;
909925
910926 if (frame_changed) {
911927 link->format = in->format;
@@ -913,6 +929,8 @@ static int filter_frame_ref(AVFilterLink *link, AVFrame *in) · 1 unmodified line
913929 link->h = in->height;
914930 link->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
915931 link->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
932+ link->colorspace = in->colorspace;
933+ link->color_range = in->color_range;
916934
917935 config_props_ref(outlink);
918936 }

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