[FFmpeg-devel] [PATCH] ffplay: support rotated video.

Marton Balint cus at passwd.hu
Sat Apr 19 00:15:50 CEST 2014


On Fri, 18 Apr 2014, Clément Bœsch wrote:

> Trolled-by: Linus Torvalds
> ---
> doc/ffplay.texi |  4 ++++
> ffplay.c        | 47 +++++++++++++++++++++++++++++++++++++++--------
> 2 files changed, 43 insertions(+), 8 deletions(-)
>
> diff --git a/doc/ffplay.texi b/doc/ffplay.texi
> index c26c68d..d73eadb 100644
> --- a/doc/ffplay.texi
> +++ b/doc/ffplay.texi
> @@ -159,6 +159,10 @@ Force a specific video decoder.
>
> @item -scodec @var{codec_name}
> Force a specific subtitle decoder.
> +
> + at item -autorotate
> +Automatically rotate the video according to presentation metadata. Set by
> +default, use -noautorotate to disable.
> @end table
>
> @section While playing
> diff --git a/ffplay.c b/ffplay.c
> index 86b9126..520b337 100644
> --- a/ffplay.c
> +++ b/ffplay.c
> @@ -327,6 +327,7 @@ static int cursor_hidden = 0;
> static char *vfilters = NULL;
> static char *afilters = NULL;
> #endif
> +static int autorotate = 1;
>
> /* current context */
> static int is_full_screen;
> @@ -1773,7 +1774,7 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c
>     char sws_flags_str[128];
>     char buffersrc_args[256];
>     int ret;
> -    AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
> +    AVFilterContext *filt_src = NULL, *filt_out = NULL, *last_filter = NULL;
>     AVCodecContext *codec = is->video_st->codec;
>     AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
>
> @@ -1804,16 +1805,45 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c
>     if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts,  AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
>         goto fail;
>
> +    last_filter = filt_out;
> +
> +#define INSERT_FILT(name, arg) do {                                         \

I think a comment would be useful describing that this actually adds a 
filter before the lastly added filter, so the processing order of the 
filters will be in reverse...

> +    AVFilterContext *filt_ctx;                                              \
> +                                                                            \
> +    ret = avfilter_graph_create_filter(&filt_ctx,                           \
> +                                       avfilter_get_by_name(name),          \
> +                                       "ffplay_" name, arg, NULL, graph);   \
> +    if (ret < 0)                                                            \
> +        goto fail;                                                          \
> +                                                                            \
> +    ret = avfilter_link(filt_ctx, 0, last_filter, 0);                       \
> +    if (ret < 0)                                                            \
> +        goto fail;                                                          \
> +                                                                            \
> +    last_filter = filt_ctx;                                                 \
> +} while (0)
> +
>     /* SDL YUV code is not handling odd width/height for some driver
>      * combinations, therefore we crop the picture to an even width/height. */
> -    if ((ret = avfilter_graph_create_filter(&filt_crop,
> -                                            avfilter_get_by_name("crop"),
> -                                            "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
> -        goto fail;
> -    if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
> -        goto fail;
> +    INSERT_FILT("crop", "floor(in_w/2)*2:floor(in_h/2)*2");
> +
> +    if (autorotate) {
> +        AVDictionaryEntry *rotate_tag = av_dict_get(is->video_st->metadata, "rotate", NULL, 0);
> +        if (rotate_tag) {
> +            if (!strcmp(rotate_tag->value, "90")) {
> +                INSERT_FILT("transpose", "1");
> +            } else if (!strcmp(rotate_tag->value, "180")) {
> +                INSERT_FILT("hflip", NULL);
> +                INSERT_FILT("vflip", NULL);
> +            } else if (!strcmp(rotate_tag->value, "270")) {
> +                INSERT_FILT("transpose", "2");
> +            } else {

You may want to check if the rotation is zero or emtpy string and not 
insert a filter in that case.

> +                INSERT_FILT("rotate", rotate_tag->value);
> +            }
> +        }
> +    }
>
> -    if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
> +    if ((ret = configure_filtergraph(graph, vfilters, filt_src, last_filter)) < 0)
>         goto fail;
>
>     is->in_video_filter  = filt_src;
> @@ -3527,6 +3557,7 @@ static const OptionDef options[] = {
>     { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &audio_codec_name }, "force audio decoder",    "decoder_name" },
>     { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
>     { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &video_codec_name }, "force video decoder",    "decoder_name" },
> +    { "autorotate", OPT_BOOL, { &autorotate }, "automatically rotate video", "" },
>     { NULL, },
> };
>

The used filters should be added to ffplay_select in configure.

Regards,
Marton


More information about the ffmpeg-devel mailing list