diff mbox series

[FFmpeg-devel,v5,2/3] ffmpeg: Add display_{rotation, hflip, vflip, scale} options

Message ID 20220925161506.80724-3-thilo.borgmann@mail.de
State New
Headers show
Series ffmpeg: Add display_{rotation, hflip, vflip, scale} options | expand

Commit Message

Thilo Borgmann Sept. 25, 2022, 4:15 p.m. UTC
From: Jan Ekström <jeebjp@gmail.com>

This enables overriding the scale, 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             | 36 ++++++++++++++++++++
 fftools/ffmpeg.h            |  8 +++++
 fftools/ffmpeg_filter.c     | 12 +++++++
 fftools/ffmpeg_opt.c        | 65 +++++++++++++++++++++++++++++++++++++
 tests/fate/filter-video.mak |  2 +-
 5 files changed, 122 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 42440d93b4..066aa463b2 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -912,6 +912,42 @@  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 the video display rotation in degrees specified by @var{rotation}.
+
+@var{rotation} is a floating point number that describes a pure
+counter-clockwise rotation in degrees.
+
+When setting this, @code{-autorotate} logic will be affected.
+For additional parameters affecting display matrix side data into which this
+information is saved, see @code{-display_hflip}, @code{-display_vflip}
+and @code{-display_scale}.
+
+These options work as a unit, so if only one of them is set, then the display
+matrix will be overridden to that specific value with the rest being set to
+default values. When applied the order of transformations is scale, flip and rotate.
+
+If unset, the default value if a display matrix is being defined is a rotation
+of zero degrees.
+
+@item -display_hflip[:@var{stream_specifier}] (@emph{input,per-stream})
+Set whether on display the image should be horizontally flipped.
+
+If unset, the default value if a display matrix is being defined is that there
+is no additional horizontal flip. See @code{-display_rotation}.
+
+@item -display_vflip[:@var{stream_specifier}] (@emph{input,per-stream})
+Set whether on display the image should be vertically flipped.
+
+If unset, the default value if a display matrix is being defined is that there
+is no additional vertical flip. See @code{-display_rotation}.
+
+@item -display_scale[:@var{stream_specifier}] (@emph{input,per-stream})
+Set whether on display the image should be scaled.
+
+If unset, the default value if a display matrix is being defined is that there
+is no additional scale. See @code{-display_rotation}.
+
 @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 ede0b2bd96..44a838d29b 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -193,6 +193,14 @@  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 *display_scales;
+    int        nb_display_scales;
     SpecifierOpt *rc_overrides;
     int        nb_rc_overrides;
     SpecifierOpt *intra_matrices;
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 7a5308425d..09e1e0a863 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -779,9 +779,21 @@  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 scale;
 
         if (!displaymatrix)
             displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
+
+        if (displaymatrix) {
+            scale = av_display_scale_get(displaymatrix);
+
+            if (scale != 1.0f) {
+                char scale_buf[128];
+                snprintf(scale_buf, sizeof(scale_buf), "%f*iw:%f*ih", scale, scale);
+                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 5febe319e4..52709ca133 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -20,6 +20,7 @@ 
 
 #include "config.h"
 
+#include <float.h>
 #include <stdint.h>
 
 #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,10 @@  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_display_scales[]            = {"display_scale", 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};
@@ -801,6 +807,49 @@  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, scale = DBL_MAX;
+    int hflip = -1, vflip = -1;
+    int hflip_set = 0, vflip_set = 0, rotation_set = 0, scale_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);
+    MATCH_PER_STREAM_OPT(display_scales,    dbl, scale,    ctx, st);
+
+    rotation_set = rotation != DBL_MAX;
+    hflip_set    = hflip != -1;
+    vflip_set    = vflip != -1;
+    scale_set    = scale != DBL_MAX;
+
+    if (!rotation_set && !hflip_set && !vflip_set && !scale_set)
+        return;
+
+    if (scale <= 0.0f) {
+        av_log(NULL, AV_LOG_FATAL, "Invalid scale factor '%f'\n", scale);
+        exit_program(1);
+    }
+
+    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);
+
+    av_display_matrix_scale((int32_t *)buf,
+                            scale_set ? scale : 1.0f);
+}
+
 static const AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
 {
     const AVCodecDescriptor *desc;
@@ -935,6 +984,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);
@@ -4021,6 +4072,20 @@  const OptionDef options[] = {
     { "aspect",       OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
                       OPT_OUTPUT,                                                { .off = OFFSET(frame_aspect_ratios) },
         "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
+    { "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)"},
+    { "display_scale", OPT_VIDEO | HAS_ARG | OPT_DOUBLE | OPT_SPEC | OPT_INPUT,  { .off = OFFSET(display_scales) },
+        "set display scale for stream(s) "
+        "(overrides any display rotation if it is not set)",
+        "scale" },
     { "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" },
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