From patchwork Sat Oct 15 10:25:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thilo Borgmann X-Patchwork-Id: 38742 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp722713pzb; Sat, 15 Oct 2022 03:26:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6cqeLWsTOmCycrxkFPH2Ipi+AD0VQjQHdt/HA4jswk+FTM7GmsGKjUWki8q777QERNX+WQ X-Received: by 2002:a17:906:478d:b0:78d:ec4d:e2e0 with SMTP id cw13-20020a170906478d00b0078dec4de2e0mr1557212ejc.34.1665829560860; Sat, 15 Oct 2022 03:26:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665829560; cv=none; d=google.com; s=arc-20160816; b=vcqiqlvd88o8ksIqeTuI31ftRb7VNrDiZPssC8WKNJQU8vmQHhfpPZLM+AItqf5NFi Al1xtx6929zqw7/DcpqUDzqoDNIXupE83OE3fsqOMhqmHWCzOzMiDpRf1g0asNDke+U+ GbCQv55PG3uLawNomMBodUfJ9A4kd2XX/PFpaOQbHTtZOJ+xpM07ylIwOoltVTeCvPev /Hd3nds8gCw3gIVblfp2SF1RCcWHCuIr34AWPBPUSIK6/rlyCW2wqgfJPCxO7vdBqelY GESuvb2E6cpVMhtZrAZnsKpN0zM9XUhD+omGjU+tLR5Fcj58lps3Yk0D80Ivdr+L9E2a 3E6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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=yLVuSVDLsUeYb1dfAA+FKCyqHeH/0T8LJQQhUBtzaRs=; b=vIUXX7Cd0n40Lgi1QpoYo/fQAR9370d5QrUQlJValCru8czIpU8PKl/AWjmDkX7jB2 76Zrlk4WqM4FeHp5KgTbM9bWFG59hv515Np7Boq/l6hi4bbvOj7QRPnM+ig7jj185Cd6 TTgPIM04y2A+a0QK1j7KQOA+TbDVa1oiTovGXopwpnNW6xNPsPLLmu7Vaf7wSvTQaUXJ 4a6WKQdY5CIFPpUiRsgaYPewNIUIOzRZVxxb2REIZG25AgYjw0/L1Ebk8VzSFLSESe1U R+NNP22/1ZNrMI7DC3SlrIU0kxdK8cfjwUj1qseD5JpUAyuxzk7JoEWzZ9TfWJ/o+scO lb0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@mail.de header.s=mailde202009 header.b=xc4GxuKi; 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 gn20-20020a1709070d1400b0078d514ace6bsi5187457ejc.282.2022.10.15.03.26.00; Sat, 15 Oct 2022 03:26:00 -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=xc4GxuKi; 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 A5A1A6800AA; Sat, 15 Oct 2022 13:25:47 +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 20DC268BBB0 for ; Sat, 15 Oct 2022 13:25:40 +0300 (EEST) Received: from postfix03.mail.de (postfix03.bt.mail.de [10.0.121.127]) by shout02.mail.de (Postfix) with ESMTP id 48B85A0C2D for ; Sat, 15 Oct 2022 12:25:39 +0200 (CEST) Received: from smtp02.mail.de (smtp02.bt.mail.de [10.0.121.212]) by postfix03.mail.de (Postfix) with ESMTP id 302238014B for ; Sat, 15 Oct 2022 12:25:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mailde202009; t=1665829539; bh=kv8Nqw1JRr3gDMNxsXhP6dnFuG/2sVRKuw4vozVcwhE=; h=From:To:Subject:Date:Message-Id:From:To:CC:Subject:Reply-To; b=xc4GxuKi+n1y8eI4K4DggneRU43NErux/+cJOsLh0Qe8/VTxQ7s48mL+u6yG/FfFn tQpmPcyveYhTdUI8pC+N7U66F+Slq1nyZ+go/YebsLRGdGJ48Bg1+eQX3qHm8h2q4M AgYrnmYEfKcBLHVJIFlhTtZWcwyo/UWdJ7Fx7PTlTi3ymUqSCLZnOYVrQEI1Ond4Fs 62R432JbOWek7wH+/iQ8tQVAob19sXWxRsocm2Fwdx2NX4RXttZrYadsDD3tZFncDn lRV0BPnSXMnnw9D6gN2ErKJ13Wql+G4TbSrG8yBuhCkfF+V10wVUnrEiWnoXBtAG+5 ArlqSWtoi3hKg== 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 smtp02.mail.de (Postfix) with ESMTPSA id F1798A0559 for ; Sat, 15 Oct 2022 12:25:38 +0200 (CEST) From: Thilo Borgmann To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Oct 2022 12:25:36 +0200 Message-Id: <20221015102537.96246-2-thilo.borgmann@mail.de> In-Reply-To: <20221015102537.96246-1-thilo.borgmann@mail.de> References: <20221015102537.96246-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: 8764 X-purgate-ID: 154282::1665829539-DAFF88E0-F3E443CE/0/0 Subject: [FFmpeg-devel] [PATCH v7 1/2] ffmpeg: Add display_{rotation, hflip, vflip} options 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Dg8W0/vRI00e From: Jan Ekström This enables overriding the rotation as well as horizontal/vertical flip state of a specific video stream on the input side. Additionally, switch the singular test that was utilizing the rotation metadata to instead override the input display rotation, thus leading to the same result. --- doc/ffmpeg.texi | 26 +++++++++++++++++++ fftools/ffmpeg.h | 6 +++++ fftools/ffmpeg_opt.c | 50 +++++++++++++++++++++++++++++++++++++ tests/fate/filter-video.mak | 2 +- 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index e9020b30d5..0367930a3b 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -912,6 +912,32 @@ If used together with @option{-vcodec copy}, it will affect the aspect ratio stored at container level, but not the aspect ratio stored in encoded frames, if it exists. +@item -display_rotation[:@var{stream_specifier}] @var{rotation} (@emph{input,per-stream}) +Set video rotation metadata. + +@var{rotation} is a decimal number specifying the amount in degree by +which the video should be rotated counter-clockwise before being +displayed. + +This option overrides the rotation/display transform metadata stored in +the file, if any. When the video is being transcoded (rather than +copied) and @code{-autorotate} is enabled, the video will be rotated at +the filtering stage. Otherwise, the metadata will be written into the +output file if the muxer supports it. + +If the @code{-display_hflip} and/or @code{-display_vflip} options are +given, they are applied after the rotation specified by this option. + +@item -display_hflip[:@var{stream_specifier}] (@emph{input,per-stream}) +Set whether on display the image should be horizontally flipped. + +See the @code{-display_rotation} option for more details. + +@item -display_vflip[:@var{stream_specifier}] (@emph{input,per-stream}) +Set whether on display the image should be vertically flipped. + +See the @code{-display_rotation} option for more details. + @item -vn (@emph{input/output}) As an input option, blocks all video streams of a file from being filtered or being automatically selected or mapped for any output. See @code{-discard} diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9c908e7465..684c51e524 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -194,6 +194,12 @@ typedef struct OptionsContext { int nb_force_fps; SpecifierOpt *frame_aspect_ratios; int nb_frame_aspect_ratios; + SpecifierOpt *display_rotations; + int nb_display_rotations; + SpecifierOpt *display_hflips; + int nb_display_hflips; + SpecifierOpt *display_vflips; + int nb_display_vflips; SpecifierOpt *rc_overrides; int nb_rc_overrides; SpecifierOpt *intra_matrices; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index fbc8638eed..f83900eb60 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -20,6 +20,7 @@ #include "config.h" +#include #include #if HAVE_SYS_RESOURCE_H @@ -45,6 +46,7 @@ #include "libavutil/avutil.h" #include "libavutil/bprint.h" #include "libavutil/channel_layout.h" +#include "libavutil/display.h" #include "libavutil/getenv_utf8.h" #include "libavutil/intreadwrite.h" #include "libavutil/fifo.h" @@ -86,6 +88,9 @@ static const char *const opt_name_forced_key_frames[] = {"forced_key_fra static const char *const opt_name_fps_mode[] = {"fps_mode", NULL}; static const char *const opt_name_force_fps[] = {"force_fps", NULL}; static const char *const opt_name_frame_aspect_ratios[] = {"aspect", NULL}; +static const char *const opt_name_display_rotations[] = {"display_rotation", NULL}; +static const char *const opt_name_display_hflips[] = {"display_hflip", NULL}; +static const char *const opt_name_display_vflips[] = {"display_vflip", NULL}; static const char *const opt_name_rc_overrides[] = {"rc_override", NULL}; static const char *const opt_name_intra_matrices[] = {"intra_matrix", NULL}; static const char *const opt_name_inter_matrices[] = {"inter_matrix", NULL}; @@ -800,6 +805,39 @@ static int opt_recording_timestamp(void *optctx, const char *opt, const char *ar return 0; } +static void add_display_matrix_to_stream(OptionsContext *o, + AVFormatContext *ctx, AVStream *st) +{ + double rotation = DBL_MAX; + int hflip = -1, vflip = -1; + int hflip_set = 0, vflip_set = 0, rotation_set = 0; + uint8_t *buf = NULL; + + MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st); + MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st); + MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st); + + rotation_set = rotation != DBL_MAX; + hflip_set = hflip != -1; + vflip_set = vflip != -1; + + if (!rotation_set && !hflip_set && !vflip_set) + return; + + if (!(buf = av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, + sizeof(int32_t) * 9))) { + av_log(NULL, AV_LOG_FATAL, "Failed to generate a display matrix!\n"); + exit_program(1); + } + + av_display_rotation_set((int32_t *)buf, + rotation_set ? -(rotation) : + -0.0f); + av_display_matrix_flip((int32_t *)buf, + hflip_set ? hflip : 0, + vflip_set ? vflip : 0); +} + static const AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder) { const AVCodecDescriptor *desc; @@ -934,6 +972,8 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) } if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + add_display_matrix_to_stream(o, ic, st); + MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st); MATCH_PER_STREAM_OPT(hwaccel_output_formats, str, hwaccel_output_format, ic, st); @@ -4019,6 +4059,16 @@ const OptionDef options[] = { { "pix_fmt", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_pix_fmts) }, "set pixel format", "format" }, + { "display_rotation", OPT_VIDEO | HAS_ARG | OPT_DOUBLE | OPT_SPEC | + OPT_INPUT, { .off = OFFSET(display_rotations) }, + "set pure counter-clockwise rotation in degrees for stream(s)", + "angle" }, + { "display_hflip", OPT_VIDEO | OPT_BOOL | OPT_SPEC | OPT_INPUT, { .off = OFFSET(display_hflips) }, + "set display horizontal flip for stream(s) " + "(overrides any display rotation if it is not set)"}, + { "display_vflip", OPT_VIDEO | OPT_BOOL | OPT_SPEC | OPT_INPUT, { .off = OFFSET(display_vflips) }, + "set display vertical flip for stream(s) " + "(overrides any display rotation if it is not set)"}, { "vn", OPT_VIDEO | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,{ .off = OFFSET(video_disable) }, "disable video" }, { "rc_override", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC | diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak index 372c70bba7..f3c27ed1c8 100644 --- a/tests/fate/filter-video.mak +++ b/tests/fate/filter-video.mak @@ -691,7 +691,7 @@ fate-filter-metadata-avf-aphase-meter-out-of-phase: SRC = $(TARGET_SAMPLES)/filt fate-filter-metadata-avf-aphase-meter-out-of-phase: CMD = run $(FILTER_METADATA_COMMAND) "amovie='$(SRC)',aphasemeter=video=0" FATE_FILTER_SAMPLES-$(call TRANSCODE, RAWVIDEO H264, MOV, ARESAMPLE_FILTER AAC_FIXED_DECODER) += fate-filter-meta-4560-rotate0 -fate-filter-meta-4560-rotate0: CMD = transcode mov $(TARGET_SAMPLES)/filter/sample-in-issue-505.mov mov "-c copy -metadata:s:v:0 rotate=0" "-af aresample" "" "" "-flags +bitexact -c:a aac_fixed" +fate-filter-meta-4560-rotate0: CMD = transcode "mov -display_rotation:v:0 0" $(TARGET_SAMPLES)/filter/sample-in-issue-505.mov mov "-c copy" "-af aresample" "" "" "-flags +bitexact -c:a aac_fixed" FATE_FILTER_CMP_METADATA-$(CONFIG_BLOCKDETECT_FILTER) += fate-filter-refcmp-blockdetect-yuv fate-filter-refcmp-blockdetect-yuv: CMD = cmp_metadata blockdetect yuv420p 0.015