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
143 int out_color_matrix;143 int out_color_matrix;
144 144
145 int in_range;145 int in_range;
146 int in_frame_range;
147 int out_range;146 int out_range;
148 147
149 int out_h_chr_pos;148 int out_h_chr_pos;
@@ -292,6 +291,18 @@ static av_cold int preinit(AVFilterContext *ctx) · 142 unmodified lines
292 return 0;291 return 0;
293}292}
294 293
294static 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
295static av_cold int init(AVFilterContext *ctx)306static av_cold int init(AVFilterContext *ctx)
296{307{
297 ScaleContext *scale = ctx->priv;308 ScaleContext *scale = ctx->priv;
@@ -332,6 +343,19 @@ static av_cold int init(AVFilterContext *ctx) · 34 unmodified lines
332 if (ret < 0)343 if (ret < 0)
333 return ret;344 return ret;
334 345
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
335 av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",359 av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
336 scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);360 scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
337 361
@@ -356,8 +380,6 @@ static av_cold int init(AVFilterContext *ctx) · 18 unmodified lines
356 if (!threads)380 if (!threads)
357 av_opt_set_int(scale->sws_opts, "threads", ff_filter_get_nb_threads(ctx), 0);381 av_opt_set_int(scale->sws_opts, "threads", ff_filter_get_nb_threads(ctx), 0);
358 382
359 scale->in_frame_range = AVCOL_RANGE_UNSPECIFIED;
360
361 return 0;383 return 0;
362}384}
363 385
@@ -376,6 +398,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 12 unmodified lines
376 398
377static int query_formats(AVFilterContext *ctx)399static int query_formats(AVFilterContext *ctx)
378{400{
401 ScaleContext *scale = ctx->priv;
379 AVFilterFormats *formats;402 AVFilterFormats *formats;
380 const AVPixFmtDescriptor *desc;403 const AVPixFmtDescriptor *desc;
381 enum AVPixelFormat pix_fmt;404 enum AVPixelFormat pix_fmt;
@@ -407,6 +430,28 @@ static int query_formats(AVFilterContext *ctx) · 25 unmodified lines
407 if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->incfg.formats)) < 0)430 if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->incfg.formats)) < 0)
408 return ret;431 return ret;
409 432
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
410 return 0;455 return 0;
411}456}
412 457
@@ -497,6 +542,7 @@ static int config_props(AVFilterLink *outlink) · 84 unmodified lines
497 const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(outfmt);542 const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(outfmt);
498 ScaleContext *scale = ctx->priv;543 ScaleContext *scale = ctx->priv;
499 uint8_t *flags_val = NULL;544 uint8_t *flags_val = NULL;
545 int in_range, in_colorspace;
500 int ret;546 int ret;
501 547
502 if ((ret = scale_eval_dimensions(ctx)) < 0)548 if ((ret = scale_eval_dimensions(ctx)) < 0)
@@ -521,6 +567,14 @@ static int config_props(AVFilterLink *outlink) · 18 unmodified lines
521 if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;567 if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
522 scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL;568 scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL;
523 569
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
524 if (scale->sws)578 if (scale->sws)
525 sws_freeContext(scale->sws);579 sws_freeContext(scale->sws);
526 if (scale->isws[0])580 if (scale->isws[0])
@@ -530,8 +584,8 @@ static int config_props(AVFilterLink *outlink) · 3 unmodified lines
530 scale->isws[0] = scale->isws[1] = scale->sws = NULL;584 scale->isws[0] = scale->isws[1] = scale->sws = NULL;
531 if (inlink0->w == outlink->w &&585 if (inlink0->w == outlink->w &&
532 inlink0->h == outlink->h &&586 inlink0->h == outlink->h &&
533 scale->out_color_matrix == AVCOL_SPC_UNSPECIFIED &&587 in_range == outlink->color_range &&
534 scale->in_range == scale->out_range &&588 in_colorspace == outlink->colorspace &&
535 inlink0->format == outlink->format)589 inlink0->format == outlink->format)
536 ;590 ;
537 else {591 else {
@@ -540,6 +594,8 @@ static int config_props(AVFilterLink *outlink) · 2 unmodified lines
540 594
541 for (i = 0; i < 3; i++) {595 for (i = 0; i < 3; i++) {
542 int in_v_chr_pos = scale->in_v_chr_pos, out_v_chr_pos = scale->out_v_chr_pos;596 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;
543 struct SwsContext *const s = sws_alloc_context();599 struct SwsContext *const s = sws_alloc_context();
544 if (!s)600 if (!s)
545 return AVERROR(ENOMEM);601 return AVERROR(ENOMEM);
@@ -555,15 +611,12 @@ static int config_props(AVFilterLink *outlink) · 9 unmodified lines
555 av_opt_set_int(s, "dstw", outlink->w, 0);611 av_opt_set_int(s, "dstw", outlink->w, 0);
556 av_opt_set_int(s, "dsth", outlink->h >> !!i, 0);612 av_opt_set_int(s, "dsth", outlink->h >> !!i, 0);
557 av_opt_set_int(s, "dst_format", outfmt, 0);613 av_opt_set_int(s, "dst_format", outfmt, 0);
558 if (scale->in_range != AVCOL_RANGE_UNSPECIFIED)614 if (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)
562 av_opt_set_int(s, "src_range",615 av_opt_set_int(s, "src_range",
563 scale->in_frame_range == AVCOL_RANGE_JPEG, 0);616 in_range == AVCOL_RANGE_JPEG, 0);
564 if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)617 if (outlink->color_range != AVCOL_RANGE_UNSPECIFIED)
565 av_opt_set_int(s, "dst_range",618 av_opt_set_int(s, "dst_range",
566 scale->out_range == AVCOL_RANGE_JPEG, 0);619 outlink->color_range == AVCOL_RANGE_JPEG, 0);
567 620
568 /* Override chroma location default settings to have the correct621 /* Override chroma location default settings to have the correct
569 * chroma positions. MPEG chroma positions are used by convention.622 * chroma positions. MPEG chroma positions are used by convention.
@@ -584,6 +637,24 @@ static int config_props(AVFilterLink *outlink) · 14 unmodified lines
584 637
585 if ((ret = sws_init_context(s, NULL, NULL)) < 0)638 if ((ret = sws_init_context(s, NULL, NULL)) < 0)
586 return ret;639 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
587 if (!scale->interlaced)658 if (!scale->interlaced)
588 break;659 break;
589 }660 }
@@ -597,10 +668,12 @@ static int config_props(AVFilterLink *outlink) · 7 unmodified lines
597 if (scale->sws)668 if (scale->sws)
598 av_opt_get(scale->sws, "sws_flags", 0, &flags_val);669 av_opt_get(scale->sws, "sws_flags", 0, &flags_val);
599 670
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",
601 inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),672 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),
602 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,674 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
603 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),675 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),
604 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,677 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
605 flags_val);678 flags_val);
606 av_freep(&flags_val);679 av_freep(&flags_val);
@@ -620,6 +693,8 @@ static int config_props_ref(AVFilterLink *outlink) · 13 unmodified lines
620 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;693 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
621 outlink->time_base = inlink->time_base;694 outlink->time_base = inlink->time_base;
622 outlink->frame_rate = inlink->frame_rate;695 outlink->frame_rate = inlink->frame_rate;
696 outlink->colorspace = inlink->colorspace;
697 outlink->color_range = inlink->color_range;
623 698
624 return 0;699 return 0;
625}700}
@@ -693,7 +768,6 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 67 unmodified lines
693 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);768 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
694 char buf[32];769 char buf[32];
695 int ret;770 int ret;
696 int in_range;
697 int frame_changed;771 int frame_changed;
698 772
699 *frame_out = NULL;773 *frame_out = NULL;
@@ -704,14 +778,9 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 4 unmodified lines
704 in->height != link->h ||778 in->height != link->h ||
705 in->format != link->format ||779 in->format != link->format ||
706 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||780 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
707 in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;781 in->sample_aspect_ratio.num != link->sample_aspect_ratio.num ||
708 782 in->colorspace != link->colorspace ||
709 if (in->color_range != AVCOL_RANGE_UNSPECIFIED &&783 in->color_range != link->color_range;
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 }
715 784
716 if (scale->eval_mode == EVAL_MODE_FRAME || frame_changed) {785 if (scale->eval_mode == EVAL_MODE_FRAME || frame_changed) {
717 unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };786 unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };
@@ -768,9 +837,11 @@ FF_ENABLE_DEPRECATION_WARNINGS · 50 unmodified lines
768#endif837#endif
769 }838 }
770 839
771 link->dst->inputs[0]->format = in->format;840 link->dst->inputs[0]->format = in->format;
772 link->dst->inputs[0]->w = in->width;841 link->dst->inputs[0]->w = in->width;
773 link->dst->inputs[0]->h = in->height;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;
774 845
775 link->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;846 link->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
776 link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;847 link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
@@ -798,69 +869,12 @@ scale: · 21 unmodified lines
798 av_frame_copy_props(out, in);869 av_frame_copy_props(out, in);
799 out->width = outlink->w;870 out->width = outlink->w;
800 out->height = outlink->h;871 out->height = outlink->h;
872 out->color_range = outlink->color_range;
873 out->colorspace = outlink->colorspace;
801 874
802 if (scale->output_is_pal)875 if (scale->output_is_pal)
803 avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);876 avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
804 877
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
864 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,878 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
865 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,879 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
866 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,880 (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
905 in->height != link->h ||919 in->height != link->h ||
906 in->format != link->format ||920 in->format != link->format ||
907 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||921 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;
909 925
910 if (frame_changed) {926 if (frame_changed) {
911 link->format = in->format;927 link->format = in->format;
@@ -913,6 +929,8 @@ static int filter_frame_ref(AVFilterLink *link, AVFrame *in) · 1 unmodified line
913 link->h = in->height;929 link->h = in->height;
914 link->sample_aspect_ratio.num = in->sample_aspect_ratio.num;930 link->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
915 link->sample_aspect_ratio.den = in->sample_aspect_ratio.den;931 link->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
932 link->colorspace = in->colorspace;
933 link->color_range = in->color_range;
916 934
917 config_props_ref(outlink);935 config_props_ref(outlink);
918 }936 }

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