From patchwork Sun Sep 20 10:36:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jan_Ekstr=C3=B6m?= X-Patchwork-Id: 22524 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9B68944B705 for ; Sun, 20 Sep 2020 13:37:05 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 771C368B6D7; Sun, 20 Sep 2020 13:37:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f65.google.com (mail-lf1-f65.google.com [209.85.167.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7FCC468B521 for ; Sun, 20 Sep 2020 13:36:59 +0300 (EEST) Received: by mail-lf1-f65.google.com with SMTP id y2so10861363lfy.10 for ; Sun, 20 Sep 2020 03:36:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Ar0x/ZUtyueSgsV/Tlk0bpy1Aspbi1a0GNC8hLjvu+A=; b=sy5oYisbPa2k/rmYBf0CnJ9MYQYWhiyYHnDjTOwBjnT2LsHKQb7AwUGYfFRuubKoKX VxGPSxaL2f0HbE2J4gnpys7DvlWe2zwXDx9rL2MKdDpHn/BF9IxlSB+w2VUrmSuJUr0c P6nZ4BheC+B5caFwmmFThOeZynmtPlz12cUdnU2PXFlntzvrXNgj9hdJpFRiS2tl5DA5 X+PKrHE4uZhu92mLZSclYJv/Ze7+Xv7AEJ0WvNiGFj4DVLNwRnGMEj/Fe/R2m2HX4vQ4 WinuMbl0zLzr6tCI6gaX4rpdmu8cQlY+uLE7dfHE5/M2XeaiI0PzkhcB+dzRGWoeuj4b UI2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ar0x/ZUtyueSgsV/Tlk0bpy1Aspbi1a0GNC8hLjvu+A=; b=uKACzzV9UMCe2IscNmMwe1Z1AnKB6ruCvHGBT+Wqqj4Q1uaFvFOwfMeRTUKomMfhZV 9hffgmHq+qn3g3rpZXV4ISSPuqCqsA5W4lwk7bsExVXTusjzYOm+3obTKrrv5a5JxrOi s+3cut/PjX+L7rj/lNToa917hm2p37CjJpu2ZxtoYksDEeuO1wVzC51w3AePGSLoyMZR xTqWjAtDZo75RLAQv6bwjZudHS9MlBUcUlRxVbAglmIX6yLLONpS8K38D2N3Vnkj4tX/ tb7lcH26eZazD6ZIVWbSIdiP6r2nLZpSooFpR09f4RXVRF3q0QwTcPIYapDsJycG/rvL lkaQ== X-Gm-Message-State: AOAM533QR3DJ5xXdT6maihR++AojprxpOAjsuhqAbMuueQzcJkkGIkp7 k/vSzzdkb7Y+6JR+cUwc9xkMvxY+zCY= X-Google-Smtp-Source: ABdhPJzkfs4Kr/dfuGtsD+XdnRYfDCD6Hny7ivQTpdYpXgFbJxqB5DQQaEbPm/0KDsc2J+C7dpJaDw== X-Received: by 2002:a19:f245:: with SMTP id d5mr13284239lfk.97.1600598218493; Sun, 20 Sep 2020 03:36:58 -0700 (PDT) Received: from localhost.localdomain (91-159-194-103.elisa-laajakaista.fi. [91.159.194.103]) by smtp.gmail.com with ESMTPSA id z6sm1782539lfq.297.2020.09.20.03.36.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Sep 2020 03:36:57 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Sep 2020 13:36:56 +0300 Message-Id: <20200920103656.24728-1-jeebjp@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200916201848.262474-1-jeebjp@gmail.com> References: <20200916201848.262474-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avfilter/vf_scale: translate and verify swscale internal range flag X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This value - while it looks like the actual range of the content - is nothing but the internal value of swscale. Thus, if we have RGB content, translate the value to 1 which is what at least this filter expects for RGB. Swscale will ignore this value when set. Additionally, after calling sws_setColorspaceDetails double-check the configured internal flag for the color range. Warn if this is different to the requested value. Finally, utilize the translated configured output value as the output AVFrame's color_range. --- libavfilter/vf_scale.c | 44 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 58eee96744..da8ce399cf 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -647,6 +647,8 @@ static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, s out,out_stride); } +// swscale's internal range flag is 0 for RGB, which we have to override +#define NORMALIZE_SWS_RANGE(format_flags, sws_range) (((format_flags) & AV_PIX_FMT_FLAG_RGB) ? 1 : (sws_range)) static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) { AVFilterContext *ctx = link->dst; @@ -750,11 +752,19 @@ scale: || in_range != AVCOL_RANGE_UNSPECIFIED || scale->out_range != AVCOL_RANGE_UNSPECIFIED) { int in_full, out_full, brightness, contrast, saturation; + int configured_in_full_range_flag, configured_out_full_range_flag; const int *inv_table, *table; + const AVPixFmtDescriptor *in_desc = av_pix_fmt_desc_get(in->format); + const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(out->format); + av_assert0(in_desc && out_desc); sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full, (int **)&table, &out_full, &brightness, &contrast, &saturation); + // translate the internal range flags according to this + // filter's expectations for RGB. + in_full = NORMALIZE_SWS_RANGE(in_desc->flags, in_full); + out_full = NORMALIZE_SWS_RANGE(out_desc->flags, out_full); if (scale->in_color_matrix) inv_table = parse_yuv_type(scale->in_color_matrix, in->colorspace); @@ -782,7 +792,39 @@ scale: table, out_full, brightness, contrast, saturation); - out->color_range = out_full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; + // double-check what was actually just configured, + // since swscale can silently ignore the color range + // value in sws_setColorspaceDetails. + sws_getColorspaceDetails(scale->sws, (int **)&inv_table, + &configured_in_full_range_flag, + (int **)&table, + &configured_out_full_range_flag, + &brightness, &contrast, &saturation); + + // translate the actually configured internal range flags according + // to this filter's expectations for RGB. + configured_in_full_range_flag = \ + NORMALIZE_SWS_RANGE(in_desc->flags, + configured_in_full_range_flag); + configured_out_full_range_flag = \ + NORMALIZE_SWS_RANGE(out_desc->flags, + configured_out_full_range_flag); + + if (in_full != configured_in_full_range_flag || + out_full != configured_out_full_range_flag) { + av_log(ctx, AV_LOG_WARNING, + "swscale overrode set input/output range value as it " + "considered it an invalid configuration! " + "(input: requested: %s, configured: %s), " + "(output: requested: %s, configured: %s)!\n", + in_full ? "full" : "limited", + configured_in_full_range_flag ? "full" : "limited", + out_full ? "full" : "limited", + configured_out_full_range_flag ? "full" : "limited"); + } + + out->color_range = configured_out_full_range_flag ? + AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; } av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,