diff mbox series

[FFmpeg-devel,1/2] ffmpeg: add display_{rotation, hflip, vflip} options

Message ID 20220518180454.53301-1-jeebjp@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/2] ffmpeg: add display_{rotation, hflip, vflip} options | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Jan Ekström May 18, 2022, 6:04 p.m. UTC
This enables overriding the rotation as well as horizontal/vertical
flip state of a specific video stream on either the input or output
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             | 29 +++++++++++++++++++++
 fftools/ffmpeg.h            |  6 +++++
 fftools/ffmpeg_opt.c        | 52 +++++++++++++++++++++++++++++++++++++
 tests/fate/filter-video.mak |  2 +-
 4 files changed, 88 insertions(+), 1 deletion(-)

Comments

Gyan Doshi May 19, 2022, 4:21 a.m. UTC | #1
On 2022-05-18 11:34 pm, Jan Ekström wrote:
> This enables overriding the rotation as well as horizontal/vertical
> flip state of a specific video stream on either the input or output
> side.

Since rotation, flip and scale are stored in a single data structure, 
how about a single option i.e. -display "rotate=90,flip=hv,scale=2"

Regards,
Gyan

>
> 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             | 29 +++++++++++++++++++++
>   fftools/ffmpeg.h            |  6 +++++
>   fftools/ffmpeg_opt.c        | 52 +++++++++++++++++++++++++++++++++++++
>   tests/fate/filter-video.mak |  2 +-
>   4 files changed, 88 insertions(+), 1 deletion(-)
>
> diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
> index 0d7e1a479d..f44d863f02 100644
> --- a/doc/ffmpeg.texi
> +++ b/doc/ffmpeg.texi
> @@ -897,6 +897,35 @@ 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/output,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 for input streams, @code{-autorotate} logic will be affected.
> +For additional parameters affecting display matrix side data into which this
> +information is saved, see @code{-display_hflip} and @code{-display_vflip}.
> +
> +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.
> +
> +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/output,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/output,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 -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 9f0c093e34..0da01b0668 100644
> --- a/fftools/ffmpeg.h
> +++ b/fftools/ffmpeg.h
> @@ -180,6 +180,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 47e8b9b7bd..0d1c84d6df 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
> @@ -43,6 +44,7 @@
>   #include "libavutil/avutil.h"
>   #include "libavutil/bprint.h"
>   #include "libavutil/channel_layout.h"
> +#include "libavutil/display.h"
>   #include "libavutil/intreadwrite.h"
>   #include "libavutil/fifo.h"
>   #include "libavutil/mathematics.h"
> @@ -82,6 +84,9 @@ static const char *const opt_name_qscale[]                    = {"q", "qscale",
>   static const char *const opt_name_forced_key_frames[]         = {"forced_key_frames", 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};
> @@ -739,6 +744,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 display_rotation = DBL_MAX;
> +    int hflip = -1, vflip = -1,
> +        hflip_set = 0, vflip_set = 0, display_rotation_set = 0;
> +    uint8_t *buf = NULL;
> +
> +    MATCH_PER_STREAM_OPT(display_rotations, dbl, display_rotation, ctx, st);
> +    MATCH_PER_STREAM_OPT(display_hflips,    i,   hflip,            ctx, st);
> +    MATCH_PER_STREAM_OPT(display_vflips,    i,   vflip,            ctx, st);
> +
> +    display_rotation_set = display_rotation != DBL_MAX;
> +    hflip_set            = hflip != -1;
> +    vflip_set            = vflip != -1;
> +
> +    if (!display_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,
> +                            display_rotation_set ? -display_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;
> @@ -893,6 +931,8 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
>               ist->top_field_first = -1;
>               MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
>   
> +            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);
> @@ -1752,6 +1792,8 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
>           ost->frame_aspect_ratio = q;
>       }
>   
> +    add_display_matrix_to_stream(o, oc, st);
> +
>       MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
>       MATCH_PER_STREAM_OPT(filters,        str, ost->filters,        oc, st);
>   
> @@ -3731,6 +3773,16 @@ 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 |
> +                          OPT_OUTPUT,                                            { .off = OFFSET(display_rotations) },
> +        "set pure counter-clockwise rotation in degrees for stream(s)",
> +        "rotation" },
> +    { "display_hflip", OPT_VIDEO | OPT_BOOL | OPT_SPEC | OPT_INPUT | OPT_OUTPUT, { .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 | OPT_OUTPUT, { .off = OFFSET(display_vflips) },
> +        "set display vertical flip for stream(s) "
> +        "(overrides any display rotation if it is not set)"},
>       { "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 86408dd58c..ce6ae255b4 100644
> --- a/tests/fate/filter-video.mak
> +++ b/tests/fate/filter-video.mak
> @@ -856,7 +856,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"
>   
>   REFCMP_DEPS = FFMPEG LAVFI_INDEV TESTSRC2_FILTER AVGBLUR_FILTER METADATA_FILTER
>
Jan Ekström May 19, 2022, 6:27 a.m. UTC | #2
On Thu, May 19, 2022, 07:21 Gyan Doshi <ffmpeg@gyani.pro> wrote:

>
>
> On 2022-05-18 11:34 pm, Jan Ekström wrote:
> > This enables overriding the rotation as well as horizontal/vertical
> > flip state of a specific video stream on either the input or output
> > side.
>
> Since rotation, flip and scale are stored in a single data structure,
> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>

I did think about that, and I even implemented AVDict based options for
ffmpeg.c in a branch. But then pretty much got tired of lack of AVOption
automation (f.ex. for the flip flagging etc), and decided that since these
are relatively basic and simple, the non-generic option for this could be
just three options in the initial submission.

Jan
Jan Ekström May 26, 2022, 11:31 a.m. UTC | #3
On Thu, May 19, 2022 at 9:27 AM Jan Ekström <jeebjp@gmail.com> wrote:
>
> On Thu, May 19, 2022, 07:21 Gyan Doshi <ffmpeg@gyani.pro> wrote:
>>
>>
>>
>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>> > This enables overriding the rotation as well as horizontal/vertical
>> > flip state of a specific video stream on either the input or output
>> > side.
>>
>> Since rotation, flip and scale are stored in a single data structure,
>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>
>
> I did think about that, and I even implemented AVDict based options for ffmpeg.c in a branch. But then pretty much got tired of lack of AVOption automation (f.ex. for the flip flagging etc), and decided that since these are relatively basic and simple, the non-generic option for this could be just three options in the initial submission.

In the end I did implement this with a single dict-based thing in a
branch but never got to posting it to this thread:
https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts

So for the positive: Now it's all in a single option so it's clear
that it's defining a single structure.
And the negative: Needs a struct definition, struct, AVOption list,
AVClass and actually if you already made a dict out of the options
before, as the functions will free the original after usage and
generate a new one, it destroys the life time expectations of the dict
and thus it is just simpler to copy the dict when entering the
function (I think there is an arguments string based API which might
or might not actually be simpler for this case).

So yea, not sure if I prefer this version.

Jan
Thilo Borgmann July 17, 2022, 3:19 p.m. UTC | #4
Hi,

Am 26.05.22 um 13:31 schrieb Jan Ekström:
> On Thu, May 19, 2022 at 9:27 AM Jan Ekström <jeebjp@gmail.com> wrote:
>>
>> On Thu, May 19, 2022, 07:21 Gyan Doshi <ffmpeg@gyani.pro> wrote:
>>>
>>>
>>>
>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>> This enables overriding the rotation as well as horizontal/vertical
>>>> flip state of a specific video stream on either the input or output
>>>> side.
>>>
>>> Since rotation, flip and scale are stored in a single data structure,
>>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>>
>>
>> I did think about that, and I even implemented AVDict based options for ffmpeg.c in a branch. But then pretty much got tired of lack of AVOption automation (f.ex. for the flip flagging etc), and decided that since these are relatively basic and simple, the non-generic option for this could be just three options in the initial submission.
> 
> In the end I did implement this with a single dict-based thing in a
> branch but never got to posting it to this thread:
> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
> 
> So for the positive: Now it's all in a single option so it's clear
> that it's defining a single structure.
> And the negative: Needs a struct definition, struct, AVOption list,
> AVClass and actually if you already made a dict out of the options
> before, as the functions will free the original after usage and
> generate a new one, it destroys the life time expectations of the dict
> and thus it is just simpler to copy the dict when entering the
> function (I think there is an arguments string based API which might
> or might not actually be simpler for this case).
> 
> So yea, not sure if I prefer this version.

Ping.

Did this stall for a reason? How can we iterate on it?

-Thilo
Gyan Doshi July 17, 2022, 4:26 p.m. UTC | #5
On 2022-07-17 08:49 pm, Thilo Borgmann wrote:
> Hi,
>
> Am 26.05.22 um 13:31 schrieb Jan Ekström:
>> On Thu, May 19, 2022 at 9:27 AM Jan Ekström <jeebjp@gmail.com> wrote:
>>>
>>> On Thu, May 19, 2022, 07:21 Gyan Doshi <ffmpeg@gyani.pro> wrote:
>>>>
>>>>
>>>>
>>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>>> This enables overriding the rotation as well as horizontal/vertical
>>>>> flip state of a specific video stream on either the input or output
>>>>> side.
>>>>
>>>> Since rotation, flip and scale are stored in a single data structure,
>>>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>>>
>>>
>>> I did think about that, and I even implemented AVDict based options 
>>> for ffmpeg.c in a branch. But then pretty much got tired of lack of 
>>> AVOption automation (f.ex. for the flip flagging etc), and decided 
>>> that since these are relatively basic and simple, the non-generic 
>>> option for this could be just three options in the initial submission.
>>
>> In the end I did implement this with a single dict-based thing in a
>> branch but never got to posting it to this thread:
>> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts 
>>
>>
>> So for the positive: Now it's all in a single option so it's clear
>> that it's defining a single structure.
>> And the negative: Needs a struct definition, struct, AVOption list,
>> AVClass and actually if you already made a dict out of the options
>> before, as the functions will free the original after usage and
>> generate a new one, it destroys the life time expectations of the dict
>> and thus it is just simpler to copy the dict when entering the
>> function (I think there is an arguments string based API which might
>> or might not actually be simpler for this case).
>>
>> So yea, not sure if I prefer this version.
>
> Ping.
>
> Did this stall for a reason? How can we iterate on it?

Since the rotation/flip/scale hints are stored in a single data 
structure, I prefer a single user option to set those values.

Regards,
Gyan
Thilo Borgmann Aug. 2, 2022, 4:48 p.m. UTC | #6
Hi,

>>>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>>>> This enables overriding the rotation as well as horizontal/vertical
>>>>>> flip state of a specific video stream on either the input or output
>>>>>> side.
>>>>>
>>>>> Since rotation, flip and scale are stored in a single data structure,
>>>>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>>>>
>>>>
>>>> I did think about that, and I even implemented AVDict based options for ffmpeg.c in a branch. But then pretty much got tired of lack of AVOption automation (f.ex. for the flip flagging etc), and decided that since these are relatively basic and simple, the non-generic option for this could be just three options in the initial submission.
>>>
>>> In the end I did implement this with a single dict-based thing in a
>>> branch but never got to posting it to this thread:
>>> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
>>>
>>> So for the positive: Now it's all in a single option so it's clear
>>> that it's defining a single structure.
>>> And the negative: Needs a struct definition, struct, AVOption list,
>>> AVClass and actually if you already made a dict out of the options
>>> before, as the functions will free the original after usage and
>>> generate a new one, it destroys the life time expectations of the dict
>>> and thus it is just simpler to copy the dict when entering the
>>> function (I think there is an arguments string based API which might
>>> or might not actually be simpler for this case).
>>>
>>> So yea, not sure if I prefer this version.
>>
>> Ping.
>>
>> Did this stall for a reason? How can we iterate on it?
> 
> Since the rotation/flip/scale hints are stored in a single data structure, I prefer a single user option to set those values.

attached patch allows for printing the arguments of the new -display_rotation (or any) option.

I wrote this in jeeb's github branch [1] where it applies on-top. Didn't test with FFmpeg/HEAD as this still needs cleanup anyways.
Never touched this whole options thing before, I guess there's lot to complain about...

-Thilo

[1] https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
From 4fae59de38c93a4cdd079535cc3631f9ccadced1 Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann@mail.de>
Date: Tue, 2 Aug 2022 18:39:18 +0200
Subject: [PATCH] ffmpeg: Allow printing of option arguments in help output

---
 fftools/cmdutils.c   |   5 ++
 fftools/cmdutils.h   |   1 +
 fftools/ffmpeg_opt.c |  56 ++++++------
 libavutil/opt.c      | 205 +++++++++++++++++++++++++++++++++++++++++++
 libavutil/opt.h      |   7 ++
 5 files changed, 247 insertions(+), 27 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 9c475acf7f..73362e5af2 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -169,6 +169,11 @@ void show_help_options(const OptionDef *options, const char *msg, int req_flags,
             av_strlcat(buf, po->argname, sizeof(buf));
         }
         printf("-%-17s  %s\n", buf, po->help);
+
+        if (po->args) {
+            const AVClass *p = po->args;
+            av_arg_show(&p, NULL);
+        }
     }
     printf("\n");
 }
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 6a519c6546..df90cc6958 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -166,6 +166,7 @@ typedef struct OptionDef {
     } u;
     const char *help;
     const char *argname;
+    const AVClass *args;
 } OptionDef;
 
 /**
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 8dffe7c225..d8db0f0681 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -114,6 +114,32 @@ static const char *const opt_name_time_bases[]                = {"time_base", NU
 static const char *const opt_name_enc_time_bases[]            = {"enc_time_base", NULL};
 static const char *const opt_name_bits_per_raw_sample[]       = {"bits_per_raw_sample", NULL};
 
+// XXX this should probably go into a seperate file <name>_args.c and #included here
+    struct display_matrix_s {
+        const AVClass *class;
+        double  rotation;
+        int     hflip;
+        int     vflip;
+    };
+#define OFFSET(x) offsetof(struct display_matrix_s, x)
+    static const AVOption display_matrix_args[] = {
+        { "rotation", "set rotation", OFFSET(rotation), AV_OPT_TYPE_DOUBLE,
+            { .dbl = DBL_MAX }, -(DBL_MAX), DBL_MAX - 1.0f, AV_OPT_FLAG_EXPORT},
+        { "hflip",    "set hflip", OFFSET(hflip),    AV_OPT_TYPE_BOOL,
+            { .i64 = -1 }, 0, 1, AV_OPT_FLAG_EXPORT},
+        { "vflip",    "set vflip", OFFSET(vflip),    AV_OPT_TYPE_BOOL,
+            { .i64 = -1 }, 0, 1, AV_OPT_FLAG_EXPORT},
+        { NULL },
+    };
+    static const AVClass class_display_matrix_args = {
+        .class_name = "display_matrix_args",
+        .item_name  = av_default_item_name,
+        .option     = display_matrix_args,
+        .version    = LIBAVUTIL_VERSION_INT,
+    };
+#undef OFFSET
+// XXX
+
 #define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
 {\
     char namestr[128] = "";\
@@ -755,39 +781,15 @@ static int opt_recording_timestamp(void *optctx, const char *opt, const char *ar
 static void add_display_matrix_to_stream(OptionsContext *o,
                                          AVFormatContext *ctx, AVStream *st)
 {
-    struct DisplayMatrixOpts {
-        const AVClass *class;
-        double  rotation;
-        int     hflip;
-        int     vflip;
-    };
     int hflip_set = 0, vflip_set = 0, display_rotation_set = 0;
     uint8_t *buf = NULL;
 
-#define OFFSET(x) offsetof(struct DisplayMatrixOpts, x)
-    static const AVOption opts[] = {
-        { "rotation", NULL, OFFSET(rotation), AV_OPT_TYPE_DOUBLE,
-            { .dbl = DBL_MAX }, -(DBL_MAX), DBL_MAX - 1.0f},
-        { "hflip",    NULL, OFFSET(hflip),    AV_OPT_TYPE_BOOL,
-            { .i64 = -1 }, 0, 1},
-        { "vflip",    NULL, OFFSET(vflip),    AV_OPT_TYPE_BOOL,
-            { .i64 = -1 }, 0, 1},
-        { NULL },
-    };
-    static const AVClass class = {
-        .class_name = "ffmpeg",
-        .item_name  = av_default_item_name,
-        .option     = opts,
-        .version    = LIBAVUTIL_VERSION_INT,
-    };
-    const AVClass *pclass = &class;
-    static struct DisplayMatrixOpts test_args = {
-        .class    = &class,
+    static struct display_matrix_s test_args = {
+        .class    = &class_display_matrix_args,
         .rotation = DBL_MAX,
         .hflip    = -1,
         .vflip    = -1,
     };
-#undef OFFSET
 
     AVDictionary *global_args = NULL;
     AVDictionary *local_args  = NULL;
@@ -3854,7 +3856,7 @@ const OptionDef options[] = {
                         OPT_OUTPUT,                                              { .off = OFFSET(display_matrixes) },
         "define a display matrix with rotation and/or horizontal/vertical "
         "flip for stream(s)",
-        "arguments" },
+        "arguments", &class_display_matrix_args },
     { "display_rotation", OPT_VIDEO | HAS_ARG | OPT_DOUBLE | OPT_SPEC | OPT_INPUT |
                           OPT_OUTPUT,                                            { .off = OFFSET(display_rotations) },
         "set pure counter-clockwise rotation in degrees for stream(s)",
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 8ffb10449b..428da2319f 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -1443,6 +1443,201 @@ FF_ENABLE_DEPRECATION_WARNINGS
     }
 }
 
+static void arg_list(void *obj, void *av_log_obj, const char *unit, enum AVOptionType parent_type)
+{
+    const AVOption *opt = NULL;
+    AVOptionRanges *r;
+    int i;
+
+    while ((opt = av_opt_next(obj, opt))) {
+        /* Don't print CONST's on level one.
+         * Don't print anything but CONST's on level two.
+         * Only print items from the requested unit.
+         */
+        if (!unit && opt->type == AV_OPT_TYPE_CONST)
+            continue;
+        else if (unit && opt->type != AV_OPT_TYPE_CONST)
+            continue;
+        else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
+            continue;
+        else if (unit && opt->type == AV_OPT_TYPE_CONST)
+            av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", opt->name);
+        else
+            av_log(av_log_obj, AV_LOG_INFO, "   %-17s ", opt->name);
+
+        switch (opt->type) {
+            case AV_OPT_TYPE_FLAGS:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
+                break;
+            case AV_OPT_TYPE_INT:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
+                break;
+            case AV_OPT_TYPE_INT64:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
+                break;
+            case AV_OPT_TYPE_UINT64:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
+                break;
+            case AV_OPT_TYPE_DOUBLE:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
+                break;
+            case AV_OPT_TYPE_FLOAT:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
+                break;
+            case AV_OPT_TYPE_STRING:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
+                break;
+            case AV_OPT_TYPE_RATIONAL:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
+                break;
+            case AV_OPT_TYPE_BINARY:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
+                break;
+            case AV_OPT_TYPE_DICT:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
+                break;
+            case AV_OPT_TYPE_IMAGE_SIZE:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
+                break;
+            case AV_OPT_TYPE_VIDEO_RATE:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
+                break;
+            case AV_OPT_TYPE_PIXEL_FMT:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
+                break;
+            case AV_OPT_TYPE_SAMPLE_FMT:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
+                break;
+            case AV_OPT_TYPE_DURATION:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
+                break;
+            case AV_OPT_TYPE_COLOR:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
+                break;
+            case AV_OPT_TYPE_CHLAYOUT:
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+            case AV_OPT_TYPE_CHANNEL_LAYOUT:
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
+                break;
+            case AV_OPT_TYPE_BOOL:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
+                break;
+            case AV_OPT_TYPE_CONST:
+                if (parent_type == AV_OPT_TYPE_INT)
+                    av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
+                else
+                    av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
+                break;
+            default:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
+                break;
+        }
+
+        if (opt->help)
+            av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
+
+        if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
+            switch (opt->type) {
+            case AV_OPT_TYPE_INT:
+            case AV_OPT_TYPE_INT64:
+            case AV_OPT_TYPE_UINT64:
+            case AV_OPT_TYPE_DOUBLE:
+            case AV_OPT_TYPE_FLOAT:
+            case AV_OPT_TYPE_RATIONAL:
+                for (i = 0; i < r->nb_ranges; i++) {
+                    av_log(av_log_obj, AV_LOG_INFO, " (from ");
+                    log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
+                    av_log(av_log_obj, AV_LOG_INFO, " to ");
+                    log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
+                    av_log(av_log_obj, AV_LOG_INFO, ")");
+                }
+                break;
+            }
+            av_opt_freep_ranges(&r);
+        }
+
+        if (opt->type != AV_OPT_TYPE_CONST  &&
+            opt->type != AV_OPT_TYPE_BINARY &&
+                !((opt->type == AV_OPT_TYPE_COLOR      ||
+                   opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
+                   opt->type == AV_OPT_TYPE_STRING     ||
+                   opt->type == AV_OPT_TYPE_DICT       ||
+                   opt->type == AV_OPT_TYPE_CHLAYOUT   ||
+                   opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
+                  !opt->default_val.str)) {
+            av_log(av_log_obj, AV_LOG_INFO, " (default ");
+            switch (opt->type) {
+            case AV_OPT_TYPE_BOOL:
+                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
+                break;
+            case AV_OPT_TYPE_FLAGS: {
+                char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
+                if (def_flags) {
+                    av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
+                    av_freep(&def_flags);
+                } else {
+                    av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
+                }
+                break;
+            }
+            case AV_OPT_TYPE_DURATION: {
+                char buf[25];
+                format_duration(buf, sizeof(buf), opt->default_val.i64);
+                av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
+                break;
+            }
+            case AV_OPT_TYPE_INT:
+            case AV_OPT_TYPE_UINT64:
+            case AV_OPT_TYPE_INT64: {
+                const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
+                if (def_const)
+                    av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
+                else
+                    log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
+                break;
+            }
+            case AV_OPT_TYPE_DOUBLE:
+            case AV_OPT_TYPE_FLOAT:
+                log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
+                break;
+            case AV_OPT_TYPE_RATIONAL: {
+                AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
+                av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
+                break;
+            case AV_OPT_TYPE_PIXEL_FMT:
+                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
+                break;
+            case AV_OPT_TYPE_SAMPLE_FMT:
+                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
+                break;
+            case AV_OPT_TYPE_COLOR:
+            case AV_OPT_TYPE_IMAGE_SIZE:
+            case AV_OPT_TYPE_STRING:
+            case AV_OPT_TYPE_DICT:
+            case AV_OPT_TYPE_VIDEO_RATE:
+            case AV_OPT_TYPE_CHLAYOUT:
+                av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
+                break;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+            case AV_OPT_TYPE_CHANNEL_LAYOUT:
+                av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
+                break;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+            }
+            av_log(av_log_obj, AV_LOG_INFO, ")");
+        }
+
+        av_log(av_log_obj, AV_LOG_INFO, "\n");
+        if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
+            arg_list(obj, av_log_obj, opt->unit, opt->type);
+    }
+}
+
 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
 {
     if (!obj)
@@ -1455,6 +1650,16 @@ int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
     return 0;
 }
 
+int av_arg_show(void *obj, void *av_log_obj)
+{
+    if (!obj)
+        return -1;
+
+    arg_list(obj, av_log_obj, NULL, -1);
+
+    return 0;
+}
+
 void av_opt_set_defaults(void *s)
 {
     av_opt_set_defaults2(s, 0, 0);
diff --git a/libavutil/opt.h b/libavutil/opt.h
index 461b5d3b6b..7bd560782f 100644
--- a/libavutil/opt.h
+++ b/libavutil/opt.h
@@ -386,6 +386,13 @@ typedef struct AVOptionRanges {
  */
 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags);
 
+/**
+ * Show the obj arguments.
+ *
+ * @param av_log_obj log context to use for showing the options
+ */
+int av_arg_show(void *obj, void *av_log_obj);
+
 /**
  * Set the values of all AVOption fields to their default values.
  *
Thilo Borgmann Aug. 2, 2022, 4:49 p.m. UTC | #7
Am 02.08.22 um 18:48 schrieb Thilo Borgmann:
> Hi,
> 
>>>>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>>>>> This enables overriding the rotation as well as horizontal/vertical
>>>>>>> flip state of a specific video stream on either the input or output
>>>>>>> side.
>>>>>>
>>>>>> Since rotation, flip and scale are stored in a single data structure,
>>>>>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>>>>>
>>>>>
>>>>> I did think about that, and I even implemented AVDict based options for ffmpeg.c in a branch. But then pretty much got tired of lack of AVOption automation (f.ex. for the flip flagging etc), and decided that since these are relatively basic and simple, the non-generic option for this could be just three options in the initial submission.
>>>>
>>>> In the end I did implement this with a single dict-based thing in a
>>>> branch but never got to posting it to this thread:
>>>> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
>>>>
>>>> So for the positive: Now it's all in a single option so it's clear
>>>> that it's defining a single structure.
>>>> And the negative: Needs a struct definition, struct, AVOption list,
>>>> AVClass and actually if you already made a dict out of the options
>>>> before, as the functions will free the original after usage and
>>>> generate a new one, it destroys the life time expectations of the dict
>>>> and thus it is just simpler to copy the dict when entering the
>>>> function (I think there is an arguments string based API which might
>>>> or might not actually be simpler for this case).
>>>>
>>>> So yea, not sure if I prefer this version.
>>>
>>> Ping.
>>>
>>> Did this stall for a reason? How can we iterate on it?
>>
>> Since the rotation/flip/scale hints are stored in a single data structure, I prefer a single user option to set those values.
> 
> attached patch allows for printing the arguments of the new -display_rotation (or any) option.
> 
> I wrote this in jeeb's github branch [1] where it applies on-top. Didn't test with FFmpeg/HEAD as this still needs cleanup anyways.
> Never touched this whole options thing before, I guess there's lot to complain about...

Produced output looks like:

-display_matrix arguments  define a display matrix with rotation and/or horizontal/vertical flip for stream(s)
    rotation          <double>      set rotation (from -DBL_MAX to DBL_MAX) (default DBL_MAX)
    hflip             <boolean>     set hflip (default auto)
    vflip             <boolean>     set vflip (default auto)


> -Thilo
> 
> [1] https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
> 
> _______________________________________________
> 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".
Andreas Rheinhardt Aug. 2, 2022, 5:10 p.m. UTC | #8
Thilo Borgmann:
> Hi,
> 
>>>>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>>>>> This enables overriding the rotation as well as horizontal/vertical
>>>>>>> flip state of a specific video stream on either the input or output
>>>>>>> side.
>>>>>>
>>>>>> Since rotation, flip and scale are stored in a single data structure,
>>>>>> how about a single option i.e. -display "rotate=90,flip=hv,scale=2"
>>>>>
>>>>>
>>>>> I did think about that, and I even implemented AVDict based options
>>>>> for ffmpeg.c in a branch. But then pretty much got tired of lack of
>>>>> AVOption automation (f.ex. for the flip flagging etc), and decided
>>>>> that since these are relatively basic and simple, the non-generic
>>>>> option for this could be just three options in the initial submission.
>>>>
>>>> In the end I did implement this with a single dict-based thing in a
>>>> branch but never got to posting it to this thread:
>>>> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
>>>>
>>>>
>>>> So for the positive: Now it's all in a single option so it's clear
>>>> that it's defining a single structure.
>>>> And the negative: Needs a struct definition, struct, AVOption list,
>>>> AVClass and actually if you already made a dict out of the options
>>>> before, as the functions will free the original after usage and
>>>> generate a new one, it destroys the life time expectations of the dict
>>>> and thus it is just simpler to copy the dict when entering the
>>>> function (I think there is an arguments string based API which might
>>>> or might not actually be simpler for this case).
>>>>
>>>> So yea, not sure if I prefer this version.
>>>
>>> Ping.
>>>
>>> Did this stall for a reason? How can we iterate on it?
>>
>> Since the rotation/flip/scale hints are stored in a single data
>> structure, I prefer a single user option to set those values.
> 
> attached patch allows for printing the arguments of the new
> -display_rotation (or any) option.
> 
> I wrote this in jeeb's github branch [1] where it applies on-top. Didn't
> test with FFmpeg/HEAD as this still needs cleanup anyways.
> Never touched this whole options thing before, I guess there's lot to
> complain about...
> 

Indeed. I hope you don't expect us to apply this clone of opt_list (the
only difference I found were that you are not printing the AV_OPT_FLAG_*
values).

> diff --git a/libavutil/opt.c b/libavutil/opt.c
> index 8ffb10449b..428da2319f 100644
> --- a/libavutil/opt.c
> +++ b/libavutil/opt.c
> @@ -1443,6 +1443,201 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      }
>  }
>  
> +static void arg_list(void *obj, void *av_log_obj, const char *unit, enum AVOptionType parent_type)
> +{
> +    const AVOption *opt = NULL;
> +    AVOptionRanges *r;
> +    int i;
> +
> +    while ((opt = av_opt_next(obj, opt))) {
> +        /* Don't print CONST's on level one.
> +         * Don't print anything but CONST's on level two.
> +         * Only print items from the requested unit.
> +         */
> +        if (!unit && opt->type == AV_OPT_TYPE_CONST)
> +            continue;
> +        else if (unit && opt->type != AV_OPT_TYPE_CONST)
> +            continue;
> +        else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
> +            continue;
> +        else if (unit && opt->type == AV_OPT_TYPE_CONST)
> +            av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", opt->name);
> +        else
> +            av_log(av_log_obj, AV_LOG_INFO, "   %-17s ", opt->name);
> +
> +        switch (opt->type) {
> +            case AV_OPT_TYPE_FLAGS:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
> +                break;
> +            case AV_OPT_TYPE_INT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
> +                break;
> +            case AV_OPT_TYPE_INT64:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
> +                break;
> +            case AV_OPT_TYPE_UINT64:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
> +                break;
> +            case AV_OPT_TYPE_DOUBLE:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
> +                break;
> +            case AV_OPT_TYPE_FLOAT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
> +                break;
> +            case AV_OPT_TYPE_STRING:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
> +                break;
> +            case AV_OPT_TYPE_RATIONAL:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
> +                break;
> +            case AV_OPT_TYPE_BINARY:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
> +                break;
> +            case AV_OPT_TYPE_DICT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
> +                break;
> +            case AV_OPT_TYPE_IMAGE_SIZE:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
> +                break;
> +            case AV_OPT_TYPE_VIDEO_RATE:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
> +                break;
> +            case AV_OPT_TYPE_PIXEL_FMT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
> +                break;
> +            case AV_OPT_TYPE_SAMPLE_FMT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
> +                break;
> +            case AV_OPT_TYPE_DURATION:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
> +                break;
> +            case AV_OPT_TYPE_COLOR:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
> +                break;
> +            case AV_OPT_TYPE_CHLAYOUT:
> +#if FF_API_OLD_CHANNEL_LAYOUT
> +FF_DISABLE_DEPRECATION_WARNINGS
> +            case AV_OPT_TYPE_CHANNEL_LAYOUT:
> +FF_ENABLE_DEPRECATION_WARNINGS
> +#endif
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
> +                break;
> +            case AV_OPT_TYPE_BOOL:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
> +                break;
> +            case AV_OPT_TYPE_CONST:
> +                if (parent_type == AV_OPT_TYPE_INT)
> +                    av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
> +                else
> +                    av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
> +                break;
> +            default:
> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
> +                break;
> +        }
> +
> +        if (opt->help)
> +            av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
> +
> +        if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
> +            switch (opt->type) {
> +            case AV_OPT_TYPE_INT:
> +            case AV_OPT_TYPE_INT64:
> +            case AV_OPT_TYPE_UINT64:
> +            case AV_OPT_TYPE_DOUBLE:
> +            case AV_OPT_TYPE_FLOAT:
> +            case AV_OPT_TYPE_RATIONAL:
> +                for (i = 0; i < r->nb_ranges; i++) {
> +                    av_log(av_log_obj, AV_LOG_INFO, " (from ");
> +                    log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
> +                    av_log(av_log_obj, AV_LOG_INFO, " to ");
> +                    log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
> +                    av_log(av_log_obj, AV_LOG_INFO, ")");
> +                }
> +                break;
> +            }
> +            av_opt_freep_ranges(&r);
> +        }
> +
> +        if (opt->type != AV_OPT_TYPE_CONST  &&
> +            opt->type != AV_OPT_TYPE_BINARY &&
> +                !((opt->type == AV_OPT_TYPE_COLOR      ||
> +                   opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
> +                   opt->type == AV_OPT_TYPE_STRING     ||
> +                   opt->type == AV_OPT_TYPE_DICT       ||
> +                   opt->type == AV_OPT_TYPE_CHLAYOUT   ||
> +                   opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
> +                  !opt->default_val.str)) {
> +            av_log(av_log_obj, AV_LOG_INFO, " (default ");
> +            switch (opt->type) {
> +            case AV_OPT_TYPE_BOOL:
> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
> +                break;
> +            case AV_OPT_TYPE_FLAGS: {
> +                char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
> +                if (def_flags) {
> +                    av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
> +                    av_freep(&def_flags);
> +                } else {
> +                    av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
> +                }
> +                break;
> +            }
> +            case AV_OPT_TYPE_DURATION: {
> +                char buf[25];
> +                format_duration(buf, sizeof(buf), opt->default_val.i64);
> +                av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
> +                break;
> +            }
> +            case AV_OPT_TYPE_INT:
> +            case AV_OPT_TYPE_UINT64:
> +            case AV_OPT_TYPE_INT64: {
> +                const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
> +                if (def_const)
> +                    av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
> +                else
> +                    log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
> +                break;
> +            }
> +            case AV_OPT_TYPE_DOUBLE:
> +            case AV_OPT_TYPE_FLOAT:
> +                log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
> +                break;
> +            case AV_OPT_TYPE_RATIONAL: {
> +                AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
> +                av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
> +                break;
> +            case AV_OPT_TYPE_PIXEL_FMT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
> +                break;
> +            case AV_OPT_TYPE_SAMPLE_FMT:
> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
> +                break;
> +            case AV_OPT_TYPE_COLOR:
> +            case AV_OPT_TYPE_IMAGE_SIZE:
> +            case AV_OPT_TYPE_STRING:
> +            case AV_OPT_TYPE_DICT:
> +            case AV_OPT_TYPE_VIDEO_RATE:
> +            case AV_OPT_TYPE_CHLAYOUT:
> +                av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
> +                break;
> +#if FF_API_OLD_CHANNEL_LAYOUT
> +FF_DISABLE_DEPRECATION_WARNINGS
> +            case AV_OPT_TYPE_CHANNEL_LAYOUT:
> +                av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
> +                break;
> +FF_ENABLE_DEPRECATION_WARNINGS
> +#endif
> +            }
> +            av_log(av_log_obj, AV_LOG_INFO, ")");
> +        }
> +
> +        av_log(av_log_obj, AV_LOG_INFO, "\n");
> +        if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
> +            arg_list(obj, av_log_obj, opt->unit, opt->type);
> +    }
> +}
> +
Thilo Borgmann Aug. 2, 2022, 5:21 p.m. UTC | #9
On 2 Aug 2022, at 19:10, Andreas Rheinhardt wrote:

> Thilo Borgmann:
>> Hi,
>>
>>>>>>> On 2022-05-18 11:34 pm, Jan Ekström wrote:
>>>>>>>> This enables overriding the rotation as well as 
>>>>>>>> horizontal/vertical
>>>>>>>> flip state of a specific video stream on either the input or 
>>>>>>>> output
>>>>>>>> side.
>>>>>>>
>>>>>>> Since rotation, flip and scale are stored in a single data 
>>>>>>> structure,
>>>>>>> how about a single option i.e. -display 
>>>>>>> "rotate=90,flip=hv,scale=2"
>>>>>>
>>>>>>
>>>>>> I did think about that, and I even implemented AVDict based 
>>>>>> options
>>>>>> for ffmpeg.c in a branch. But then pretty much got tired of lack 
>>>>>> of
>>>>>> AVOption automation (f.ex. for the flip flagging etc), and 
>>>>>> decided
>>>>>> that since these are relatively basic and simple, the non-generic
>>>>>> option for this could be just three options in the initial 
>>>>>> submission.
>>>>>
>>>>> In the end I did implement this with a single dict-based thing in 
>>>>> a
>>>>> branch but never got to posting it to this thread:
>>>>> https://github.com/jeeb/ffmpeg/commits/ffmpeg_c_display_matrix_with_dicts
>>>>>
>>>>>
>>>>> So for the positive: Now it's all in a single option so it's clear
>>>>> that it's defining a single structure.
>>>>> And the negative: Needs a struct definition, struct, AVOption 
>>>>> list,
>>>>> AVClass and actually if you already made a dict out of the options
>>>>> before, as the functions will free the original after usage and
>>>>> generate a new one, it destroys the life time expectations of the 
>>>>> dict
>>>>> and thus it is just simpler to copy the dict when entering the
>>>>> function (I think there is an arguments string based API which 
>>>>> might
>>>>> or might not actually be simpler for this case).
>>>>>
>>>>> So yea, not sure if I prefer this version.
>>>>
>>>> Ping.
>>>>
>>>> Did this stall for a reason? How can we iterate on it?
>>>
>>> Since the rotation/flip/scale hints are stored in a single data
>>> structure, I prefer a single user option to set those values.
>>
>> attached patch allows for printing the arguments of the new
>> -display_rotation (or any) option.
>>
>> I wrote this in jeeb's github branch [1] where it applies on-top. 
>> Didn't
>> test with FFmpeg/HEAD as this still needs cleanup anyways.
>> Never touched this whole options thing before, I guess there's lot to
>> complain about...
>>
>
> Indeed. I hope you don't expect us to apply this clone of opt_list 
> (the
> only difference I found were that you are not printing the 
> AV_OPT_FLAG_*
> values).

I guess not, more a proof of concept.
More differences are that it does not prefix the arguments with a 
„-“
and it does not print a header like „<name> AVContext arguments“ (or 
something like that)

Can either be complexified to have these controlled by more args or 
split into several sub functions…?

-Thilo


>
>> diff --git a/libavutil/opt.c b/libavutil/opt.c
>> index 8ffb10449b..428da2319f 100644
>> --- a/libavutil/opt.c
>> +++ b/libavutil/opt.c
>> @@ -1443,6 +1443,201 @@ FF_ENABLE_DEPRECATION_WARNINGS
>>      }
>>  }
>>
>> +static void arg_list(void *obj, void *av_log_obj, const char *unit, 
>> enum AVOptionType parent_type)
>> +{
>> +    const AVOption *opt = NULL;
>> +    AVOptionRanges *r;
>> +    int i;
>> +
>> +    while ((opt = av_opt_next(obj, opt))) {
>> +        /* Don't print CONST's on level one.
>> +         * Don't print anything but CONST's on level two.
>> +         * Only print items from the requested unit.
>> +         */
>> +        if (!unit && opt->type == AV_OPT_TYPE_CONST)
>> +            continue;
>> +        else if (unit && opt->type != AV_OPT_TYPE_CONST)
>> +            continue;
>> +        else if (unit && opt->type == AV_OPT_TYPE_CONST && 
>> strcmp(unit, opt->unit))
>> +            continue;
>> +        else if (unit && opt->type == AV_OPT_TYPE_CONST)
>> +            av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", 
>> opt->name);
>> +        else
>> +            av_log(av_log_obj, AV_LOG_INFO, "   %-17s ", opt->name);
>> +
>> +        switch (opt->type) {
>> +            case AV_OPT_TYPE_FLAGS:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<flags>");
>> +                break;
>> +            case AV_OPT_TYPE_INT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
>> +                break;
>> +            case AV_OPT_TYPE_INT64:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<int64>");
>> +                break;
>> +            case AV_OPT_TYPE_UINT64:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<uint64>");
>> +                break;
>> +            case AV_OPT_TYPE_DOUBLE:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<double>");
>> +                break;
>> +            case AV_OPT_TYPE_FLOAT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<float>");
>> +                break;
>> +            case AV_OPT_TYPE_STRING:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<string>");
>> +                break;
>> +            case AV_OPT_TYPE_RATIONAL:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<rational>");
>> +                break;
>> +            case AV_OPT_TYPE_BINARY:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<binary>");
>> +                break;
>> +            case AV_OPT_TYPE_DICT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<dictionary>");
>> +                break;
>> +            case AV_OPT_TYPE_IMAGE_SIZE:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<image_size>");
>> +                break;
>> +            case AV_OPT_TYPE_VIDEO_RATE:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<video_rate>");
>> +                break;
>> +            case AV_OPT_TYPE_PIXEL_FMT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<pix_fmt>");
>> +                break;
>> +            case AV_OPT_TYPE_SAMPLE_FMT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<sample_fmt>");
>> +                break;
>> +            case AV_OPT_TYPE_DURATION:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<duration>");
>> +                break;
>> +            case AV_OPT_TYPE_COLOR:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<color>");
>> +                break;
>> +            case AV_OPT_TYPE_CHLAYOUT:
>> +#if FF_API_OLD_CHANNEL_LAYOUT
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> +            case AV_OPT_TYPE_CHANNEL_LAYOUT:
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<channel_layout>");
>> +                break;
>> +            case AV_OPT_TYPE_BOOL:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", 
>> "<boolean>");
>> +                break;
>> +            case AV_OPT_TYPE_CONST:
>> +                if (parent_type == AV_OPT_TYPE_INT)
>> +                    av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", 
>> opt->default_val.i64);
>> +                else
>> +                    av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
>> +                break;
>> +            default:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
>> +                break;
>> +        }
>> +
>> +        if (opt->help)
>> +            av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
>> +
>> +        if (av_opt_query_ranges(&r, obj, opt->name, 
>> AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
>> +            switch (opt->type) {
>> +            case AV_OPT_TYPE_INT:
>> +            case AV_OPT_TYPE_INT64:
>> +            case AV_OPT_TYPE_UINT64:
>> +            case AV_OPT_TYPE_DOUBLE:
>> +            case AV_OPT_TYPE_FLOAT:
>> +            case AV_OPT_TYPE_RATIONAL:
>> +                for (i = 0; i < r->nb_ranges; i++) {
>> +                    av_log(av_log_obj, AV_LOG_INFO, " (from ");
>> +                    log_value(av_log_obj, AV_LOG_INFO, 
>> r->range[i]->value_min);
>> +                    av_log(av_log_obj, AV_LOG_INFO, " to ");
>> +                    log_value(av_log_obj, AV_LOG_INFO, 
>> r->range[i]->value_max);
>> +                    av_log(av_log_obj, AV_LOG_INFO, ")");
>> +                }
>> +                break;
>> +            }
>> +            av_opt_freep_ranges(&r);
>> +        }
>> +
>> +        if (opt->type != AV_OPT_TYPE_CONST  &&
>> +            opt->type != AV_OPT_TYPE_BINARY &&
>> +                !((opt->type == AV_OPT_TYPE_COLOR      ||
>> +                   opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
>> +                   opt->type == AV_OPT_TYPE_STRING     ||
>> +                   opt->type == AV_OPT_TYPE_DICT       ||
>> +                   opt->type == AV_OPT_TYPE_CHLAYOUT   ||
>> +                   opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
>> +                  !opt->default_val.str)) {
>> +            av_log(av_log_obj, AV_LOG_INFO, " (default ");
>> +            switch (opt->type) {
>> +            case AV_OPT_TYPE_BOOL:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char 
>> *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
>> +                break;
>> +            case AV_OPT_TYPE_FLAGS: {
>> +                char *def_flags = get_opt_flags_string(obj, 
>> opt->unit, opt->default_val.i64);
>> +                if (def_flags) {
>> +                    av_log(av_log_obj, AV_LOG_INFO, "%s", 
>> def_flags);
>> +                    av_freep(&def_flags);
>> +                } else {
>> +                    av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, 
>> opt->default_val.i64);
>> +                }
>> +                break;
>> +            }
>> +            case AV_OPT_TYPE_DURATION: {
>> +                char buf[25];
>> +                format_duration(buf, sizeof(buf), 
>> opt->default_val.i64);
>> +                av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
>> +                break;
>> +            }
>> +            case AV_OPT_TYPE_INT:
>> +            case AV_OPT_TYPE_UINT64:
>> +            case AV_OPT_TYPE_INT64: {
>> +                const char *def_const = get_opt_const_name(obj, 
>> opt->unit, opt->default_val.i64);
>> +                if (def_const)
>> +                    av_log(av_log_obj, AV_LOG_INFO, "%s", 
>> def_const);
>> +                else
>> +                    log_int_value(av_log_obj, AV_LOG_INFO, 
>> opt->default_val.i64);
>> +                break;
>> +            }
>> +            case AV_OPT_TYPE_DOUBLE:
>> +            case AV_OPT_TYPE_FLOAT:
>> +                log_value(av_log_obj, AV_LOG_INFO, 
>> opt->default_val.dbl);
>> +                break;
>> +            case AV_OPT_TYPE_RATIONAL: {
>> +                AVRational q = av_d2q(opt->default_val.dbl, 
>> INT_MAX);
>> +                av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, 
>> q.den); }
>> +                break;
>> +            case AV_OPT_TYPE_PIXEL_FMT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char 
>> *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
>> +                break;
>> +            case AV_OPT_TYPE_SAMPLE_FMT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "%s", (char 
>> *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), 
>> "none"));
>> +                break;
>> +            case AV_OPT_TYPE_COLOR:
>> +            case AV_OPT_TYPE_IMAGE_SIZE:
>> +            case AV_OPT_TYPE_STRING:
>> +            case AV_OPT_TYPE_DICT:
>> +            case AV_OPT_TYPE_VIDEO_RATE:
>> +            case AV_OPT_TYPE_CHLAYOUT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", 
>> opt->default_val.str);
>> +                break;
>> +#if FF_API_OLD_CHANNEL_LAYOUT
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> +            case AV_OPT_TYPE_CHANNEL_LAYOUT:
>> +                av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, 
>> opt->default_val.i64);
>> +                break;
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +            }
>> +            av_log(av_log_obj, AV_LOG_INFO, ")");
>> +        }
>> +
>> +        av_log(av_log_obj, AV_LOG_INFO, "\n");
>> +        if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
>> +            arg_list(obj, av_log_obj, opt->unit, opt->type);
>> +    }
>> +}
>> +
>
>
> _______________________________________________
> 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".
diff mbox series

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 0d7e1a479d..f44d863f02 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -897,6 +897,35 @@  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/output,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 for input streams, @code{-autorotate} logic will be affected.
+For additional parameters affecting display matrix side data into which this
+information is saved, see @code{-display_hflip} and @code{-display_vflip}.
+
+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.
+
+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/output,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/output,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 -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 9f0c093e34..0da01b0668 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -180,6 +180,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 47e8b9b7bd..0d1c84d6df 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
@@ -43,6 +44,7 @@ 
 #include "libavutil/avutil.h"
 #include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/display.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
@@ -82,6 +84,9 @@  static const char *const opt_name_qscale[]                    = {"q", "qscale",
 static const char *const opt_name_forced_key_frames[]         = {"forced_key_frames", 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};
@@ -739,6 +744,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 display_rotation = DBL_MAX;
+    int hflip = -1, vflip = -1,
+        hflip_set = 0, vflip_set = 0, display_rotation_set = 0;
+    uint8_t *buf = NULL;
+
+    MATCH_PER_STREAM_OPT(display_rotations, dbl, display_rotation, ctx, st);
+    MATCH_PER_STREAM_OPT(display_hflips,    i,   hflip,            ctx, st);
+    MATCH_PER_STREAM_OPT(display_vflips,    i,   vflip,            ctx, st);
+
+    display_rotation_set = display_rotation != DBL_MAX;
+    hflip_set            = hflip != -1;
+    vflip_set            = vflip != -1;
+
+    if (!display_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,
+                            display_rotation_set ? -display_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;
@@ -893,6 +931,8 @@  static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
             ist->top_field_first = -1;
             MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
 
+            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);
@@ -1752,6 +1792,8 @@  static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
         ost->frame_aspect_ratio = q;
     }
 
+    add_display_matrix_to_stream(o, oc, st);
+
     MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
     MATCH_PER_STREAM_OPT(filters,        str, ost->filters,        oc, st);
 
@@ -3731,6 +3773,16 @@  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 |
+                          OPT_OUTPUT,                                            { .off = OFFSET(display_rotations) },
+        "set pure counter-clockwise rotation in degrees for stream(s)",
+        "rotation" },
+    { "display_hflip", OPT_VIDEO | OPT_BOOL | OPT_SPEC | OPT_INPUT | OPT_OUTPUT, { .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 | OPT_OUTPUT, { .off = OFFSET(display_vflips) },
+        "set display vertical flip for stream(s) "
+        "(overrides any display rotation if it is not set)"},
     { "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 86408dd58c..ce6ae255b4 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -856,7 +856,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"
 
 REFCMP_DEPS = FFMPEG LAVFI_INDEV TESTSRC2_FILTER AVGBLUR_FILTER METADATA_FILTER