From patchwork Tue Aug 16 13:30:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thilo Borgmann X-Patchwork-Id: 37317 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3d0d:b0:8d:a68e:8a0e with SMTP id y13csp1868351pzi; Tue, 16 Aug 2022 06:31:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR4bSOlV5U/U/Cak9EaUt9u+On80jAobraS/mxJWKt1X7ijUkwZjzQVNs/NY0E4qzfxgnx09 X-Received: by 2002:a17:906:9b14:b0:730:984e:a51c with SMTP id eo20-20020a1709069b1400b00730984ea51cmr13120994ejc.435.1660656705549; Tue, 16 Aug 2022 06:31:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660656705; cv=none; d=google.com; s=arc-20160816; b=XcCBiSvLt9HddAkds2BMGdrId4fBoA/KD9VJ6c28PfFPZYRpdR0TN4zE+EdpGExKT2 C6q6GBSyGS5fTk+9LybB5jwLzx5HJjiPkvoloH+ILsvUVIX7I3a+626LdxlpfVqi1fWt IFwhS7P1IDDML/yKADf7LA5IINMOZ6TR8WksJo+q2sGIvxPYsV81ztg4HeaSz3SmBAdR RJ6e9kM/fzbXXZXwau4iuHr2Sx76EGjqlFT2eFh1V+soq/ZkF9iZfs4EkJAIFZMS5pih nsSP90q+MZ3AbPQK2VxvQvUkwRNhtKhCSxJNGdVFpkgO2iPuuu2sXTh7IeWOqhXLlxTd tC8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=uNC77xOIgjgXQsKpTM0vX1cXKn0qm9EqkWl6wH9rZE8=; b=RaJ1uTWLVu85PdSW3yE/PeH9EVdbWcUMOm42tjvqpxtFemwQELd17XPFaqtX1uoBt4 57Sf2Ldndhsc4bOA+CVmPID9jQOJ6/9pUSdIeYBzZ1aDhrV9DmDwqDrVa9U4SRDu+SKT f/scWoEqYyydYqvOcVNj6+Hc6WJtxxr/d9Ru8avPzTKlFLvvTA/YmNKyfmwVqReyBuAK wfTlKkbvQZNoPijWDN5g2dk6kfakfpWpoxSNd0vJK7CXtugHwolHXSJ1Gz0g5lZuWVCJ 6QSwSoY+JjH083vkB0Lxz5JdM4AKUAOjRQc1N6YEMe+u5airrAG3TqThY0maB3jLFDCf 4DgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@mail.de header.s=mailde202009 header.b="ajV/ioMB"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mail.de Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hs28-20020a1709073e9c00b00730bcba7f70si12575928ejc.635.2022.08.16.06.31.44; Tue, 16 Aug 2022 06:31:45 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@mail.de header.s=mailde202009 header.b="ajV/ioMB"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mail.de Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDF6A68B912; Tue, 16 Aug 2022 16:31:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from shout02.mail.de (shout02.mail.de [62.201.172.25]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5A90868B8F3 for ; Tue, 16 Aug 2022 16:30:57 +0300 (EEST) Received: from postfix03.mail.de (postfix03.bt.mail.de [10.0.121.127]) by shout02.mail.de (Postfix) with ESMTP id 54347A0E05 for ; Tue, 16 Aug 2022 15:30:53 +0200 (CEST) Received: from smtp04.mail.de (smtp04.bt.mail.de [10.0.121.214]) by postfix03.mail.de (Postfix) with ESMTP id 37AF780127; Tue, 16 Aug 2022 15:30:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mailde202009; t=1660656653; bh=/gUeW16e89jjuD6RmZb00agceKr48ZS3etvr4g9yDFo=; h=From:To:Cc:Subject:Date:Message-Id:From:To:CC:Subject:Reply-To; b=ajV/ioMBkoFzvonXO0Dn8/rO5hrbxofzBq9uURmt0nSo1U498ZQW85dt776cHaQvg SRDoIb04/dDnwYhhiWPOHxseMIY1trCFlMN9PIug4mFl32GlkvsyoA2SSefE0epB0g VLKPMHieSYLeDXCVifcmfGuVu1HUV2GHSNzYUJePtlvvUMi1pmJqJ9aI8S1FLs2Axe gvUG+dJN9/Vog4jk5RY6o5h51juoKtbzmTAoaNM9Y0hPwEFjaNeQGC6XmUfGlBnZol Vw6Z3WhexZLmmszgx3CcxQqDNLGpkoNuCESSQ/5Cgx8C2vURi4ojebTghmnMxZa5pw ssO6lh8HItUcA== Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp04.mail.de (Postfix) with ESMTPSA id A1D5FC1338; Tue, 16 Aug 2022 15:30:52 +0200 (CEST) From: Thilo Borgmann To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Aug 2022 15:30:46 +0200 Message-Id: <20220816133046.70846-5-thilo.borgmann@mail.de> In-Reply-To: <20220816133046.70846-1-thilo.borgmann@mail.de> References: <20220816133046.70846-1-thilo.borgmann@mail.de> MIME-Version: 1.0 X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 7062 X-purgate-ID: 154282::1660656652-000016C9-67B526D3/0/0 Subject: [FFmpeg-devel] [PATCH 5/5] ffmpeg: Add {h, v}scale argument to display_matrix option to allow for scaling via the display matrix 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: Thilo Borgmann Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: cdQZ7WT+1k5h --- doc/ffmpeg.texi | 4 ++++ fftools/ffmpeg_filter.c | 15 +++++++++++++++ fftools/ffmpeg_opt.c | 10 ++++++++++ libavutil/display.c | 21 +++++++++++++++++++++ libavutil/display.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+) diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index 5d3e3b3052..52cca7a407 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -923,6 +923,10 @@ The @code{-autorotate} logic will be affected. @item hflip=@var{[0,1]} @item vflip=@var{[0,1]} Set a horizontal or vertical flip. +@item hscale=@var{[0,2]} +Set a horizontal scaling by factor of the given floating-point value. +@item vscale=@var{[0,2]} +Set a vertical scaling by factor of the given floating-point value. @end table @item -vn (@emph{input/output}) diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index f9ae76f76d..0759c08687 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -778,9 +778,24 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { int32_t *displaymatrix = ifilter->displaymatrix; double theta; + double hscale = 1.0f; + double vscale = 1.0f; if (!displaymatrix) displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL); + + if (displaymatrix) { + hscale = av_display_hscale_get(displaymatrix); + vscale = av_display_vscale_get(displaymatrix); + + if (hscale != 1.0f || vscale != 1.0f) { + char scale_buf[128]; + snprintf(scale_buf, sizeof(scale_buf), "%f*iw:%f*ih", hscale, vscale); + ret = insert_filter(&last_filter, &pad_idx, "scale", scale_buf); + } + } + + theta = get_rotation(displaymatrix); if (fabs(theta - 90) < 1.0) { diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index f6551621c3..4fae6cbfbf 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -121,6 +121,8 @@ static const char *const opt_name_bits_per_raw_sample[] = {"bits_per_raw_s double rotation; int hflip; int vflip; + double hscale; + double vscale; }; #define OFFSET(x) offsetof(struct display_matrix_s, x) static const AVOption display_matrix_args[] = { @@ -130,6 +132,10 @@ static const char *const opt_name_bits_per_raw_sample[] = {"bits_per_raw_s { .i64 = -1 }, 0, 1, AV_OPT_FLAG_ARGUMENT}, { "vflip", "set vflip", OFFSET(vflip), AV_OPT_TYPE_BOOL, { .i64 = -1 }, 0, 1, AV_OPT_FLAG_ARGUMENT}, + { "hscale", "set scale factor", OFFSET(hscale), AV_OPT_TYPE_DOUBLE, + { .dbl = 1.0f }, 0.0f, 2.0f, AV_OPT_FLAG_ARGUMENT}, + { "vscale", "set scale factor", OFFSET(vscale), AV_OPT_TYPE_DOUBLE, + { .dbl = 1.0f }, 0.0f, 2.0f, AV_OPT_FLAG_ARGUMENT}, { NULL }, }; static const AVClass class_display_matrix_args = { @@ -848,6 +854,8 @@ static void add_display_matrix_to_stream(OptionsContext *o, .rotation = DBL_MAX, .hflip = -1, .vflip = -1, + .hscale = 1.0f, + .vscale = 1.0f, }; AVDictionary *global_args = NULL; @@ -903,6 +911,8 @@ static void add_display_matrix_to_stream(OptionsContext *o, av_display_matrix_flip((int32_t *)buf, hflip_set ? test_args.hflip : 0, vflip_set ? test_args.vflip : 0); + + av_display_matrix_scale((int32_t *)buf, test_args.hscale, test_args.vscale); } diff --git a/libavutil/display.c b/libavutil/display.c index d31061283c..b89763ff48 100644 --- a/libavutil/display.c +++ b/libavutil/display.c @@ -28,9 +28,11 @@ // fixed point to double #define CONV_FP(x) ((double) (x)) / (1 << 16) +#define CONV_FP2(x) ((double) (x)) / (1 << 30) // double to fixed point #define CONV_DB(x) (int32_t) ((x) * (1 << 16)) +#define CONV_DB2(x) (int32_t) ((x) * (1 << 30)) double av_display_rotation_get(const int32_t matrix[9]) { @@ -48,6 +50,17 @@ double av_display_rotation_get(const int32_t matrix[9]) return -rotation; } +double av_display_hscale_get(const int32_t matrix[9]) +{ + return fabs(CONV_FP2(matrix[2])); +} + +double av_display_vscale_get(const int32_t matrix[9]) +{ + return fabs(CONV_FP2(matrix[5])); +} + +#include void av_display_rotation_set(int32_t matrix[9], double angle) { double radians = -angle * M_PI / 180.0f; @@ -60,6 +73,8 @@ void av_display_rotation_set(int32_t matrix[9], double angle) matrix[1] = CONV_DB(-s); matrix[3] = CONV_DB(s); matrix[4] = CONV_DB(c); + matrix[2] = 1 << 30; + matrix[5] = 1 << 30; matrix[8] = 1 << 30; } @@ -72,3 +87,9 @@ void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip) for (i = 0; i < 9; i++) matrix[i] *= flip[i % 3]; } + +void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale) +{ + matrix[2] = CONV_DB2(CONV_FP2(matrix[2]) * hscale); + matrix[5] = CONV_DB2(CONV_FP2(matrix[5]) * vscale); +} diff --git a/libavutil/display.h b/libavutil/display.h index 31d8bef361..b875e1cfdd 100644 --- a/libavutil/display.h +++ b/libavutil/display.h @@ -86,6 +86,26 @@ */ double av_display_rotation_get(const int32_t matrix[9]); +/** + * Extract the horizontal scaling component of the transformation matrix. + * + * @param matrix the transformation matrix + * @return the horizontal scaling by which the transformation matrix scales the frame + * in the horizontal direction. The scaling factor will be in the range + * [0.0, 2.0]. + */ +double av_display_hscale_get(const int32_t matrix[9]); + +/** + * Extract the vertical scaling component of the transformation matrix. + * + * @param matrix the transformation matrix + * @return the vertical scaling by which the transformation matrix scales the frame + * in the vertical direction. The scaling factor will be in the range + * [0.0, 2.0]. + */ +double av_display_vscale_get(const int32_t matrix[9]); + /** * Initialize a transformation matrix describing a pure clockwise * rotation by the specified angle (in degrees). @@ -105,6 +125,14 @@ void av_display_rotation_set(int32_t matrix[9], double angle); */ void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip); +/** + * Scale the input matrix horizontally and/or vertically. + * + * @param matrix an allocated transformation matrix + * @param hscale whether the matrix should be scaled horizontally + * @param vscale whether the matrix should be scaled vertically + */ +void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale); /** * @} * @}