From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 13C4F4F1E6 for ; Mon, 16 Jun 2025 18:05:48 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 01A0268DDB4; Mon, 16 Jun 2025 21:05:36 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id ADCB568D823 for ; Mon, 16 Jun 2025 21:05:32 +0300 (EEST) Received: from haasn.dev (unknown [10.30.1.1]) by haasn.dev (Postfix) with UTF8SMTP id 7CF4541926; Mon, 16 Jun 2025 20:05:32 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Mon, 16 Jun 2025 20:05:29 +0200 Message-ID: <20250616180530.140914-2-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250616180530.140914-1-ffmpeg@haasn.xyz> References: <20250616180530.140914-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avfilter/vf_libplacebo: correctly update SAR to reflect scaled result X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas This aligns the behavior of vf_libplacebo with other filters in the vf_scale family, that forward any change in the SAR as a result of changing the output resolution to the output frame / link, and updates the ff_scale_adjust_dimensions() call to continue working as intended. The new behavior reflects the documentation of vf_libplacebo, which described this behavior despite that not being the way the filter worked up to this point. As an aside, also fixes a bug where the AVFrame SAR was inconsistent with the AVFilterLink SAR when the s->nb_inputs > 1 condition was met. --- libavfilter/vf_libplacebo.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c index c7a1b15f92..4775673127 100644 --- a/libavfilter/vf_libplacebo.c +++ b/libavfilter/vf_libplacebo.c @@ -186,6 +186,7 @@ typedef struct LibplaceboContext { float corner_rounding; int force_original_aspect_ratio; int force_divisible_by; + int reset_sar; int normalize_sar; int apply_filmgrain; int apply_dovi; @@ -911,6 +912,15 @@ static int output_frame(AVFilterContext *ctx, int64_t pts) if (s->apply_filmgrain) av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS); + if (s->reset_sar) { + out->sample_aspect_ratio = ref->sample_aspect_ratio; + } else { + const AVRational ar_ref = { ref->width, ref->height }; + const AVRational ar_out = { out->width, out->height }; + const AVRational stretch = av_div_q(ar_ref, ar_out); + out->sample_aspect_ratio = av_mul_q(ref->sample_aspect_ratio, stretch); + } + /* Map, render and unmap output frame */ if (outdesc->flags & AV_PIX_FMT_FLAG_HWACCEL) { ok = pl_map_avframe_ex(s->gpu, &target, pl_avframe_params( @@ -1270,19 +1280,28 @@ static int libplacebo_config_output(AVFilterLink *outlink) RET(ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink, &outlink->w, &outlink->h)); + s->reset_sar = s->normalize_sar || s->nb_inputs > 1; + double sar_in = inlink->sample_aspect_ratio.num ? + av_q2d(inlink->sample_aspect_ratio) : 1.0; + ff_scale_adjust_dimensions(inlink, &outlink->w, &outlink->h, s->force_original_aspect_ratio, - s->force_divisible_by, 1.f); + s->force_divisible_by, + s->reset_sar ? sar_in : 1.0); - if (s->normalize_sar || s->nb_inputs > 1) { + if (s->reset_sar) { /* SAR is normalized, or we have multiple inputs, set out to 1:1 */ outlink->sample_aspect_ratio = (AVRational){ 1, 1 }; - } else { + } else if (inlink->sample_aspect_ratio.num) { /* This is consistent with other scale_* filters, which only * set the outlink SAR to be equal to the scale SAR iff the input SAR * was set to something nonzero */ - if (inlink->sample_aspect_ratio.num) - outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + const AVRational ar_in = { inlink->w, inlink->h }; + const AVRational ar_out = { outlink->w, outlink->h }; + const AVRational stretch = av_div_q(ar_in, ar_out); + outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio, stretch); + } else { + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; } /* Frame rate */ -- 2.49.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".