vf_aspect.c 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2010 Bobby Bingham

 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
22
 * @file
23
 * aspect ratio modification video filters
24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include "avfilter.h"

typedef struct {
    AVRational aspect;
} AspectContext;

static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
{
    AspectContext *aspect = ctx->priv;
    double  ratio;
    int64_t gcd;
37
    char c = 0;
38

Stefano Sabatini's avatar
Stefano Sabatini committed
39
    if (args) {
40 41 42 43 44 45 46 47 48 49
        if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
            if (sscanf(args, "%lf%c", &ratio, &c) == 1)
                aspect->aspect = av_d2q(ratio, 100);

        if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
            av_log(ctx, AV_LOG_ERROR,
                   "Invalid string '%s' for aspect ratio.\n", args);
            return AVERROR(EINVAL);
        }

Stefano Sabatini's avatar
Stefano Sabatini committed
50 51 52 53 54
        gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
        if (gcd) {
            aspect->aspect.num /= gcd;
            aspect->aspect.den /= gcd;
        }
55 56
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
57
    if (aspect->aspect.den == 0)
58 59
        aspect->aspect = (AVRational) {0, 1};

60
    av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
61 62 63
    return 0;
}

64
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
65 66 67
{
    AspectContext *aspect = link->dst->priv;

68
    picref->video->pixel_aspect = aspect->aspect;
69 70 71
    avfilter_start_frame(link->dst->outputs[0], picref);
}

72 73 74
#if CONFIG_SETDAR_FILTER
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
static int setdar_config_props(AVFilterLink *inlink)
75 76
{
    AspectContext *aspect = inlink->dst->priv;
77
    AVRational dar = aspect->aspect;
78 79 80 81 82

    av_reduce(&aspect->aspect.num, &aspect->aspect.den,
               aspect->aspect.num * inlink->h,
               aspect->aspect.den * inlink->w, 100);

83 84
    av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d par:%d/%d\n",
           inlink->h, inlink->w, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
85 86 87
    return 0;
}

88 89 90
AVFilter avfilter_vf_setdar = {
    .name      = "setdar",
    .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
91 92 93 94 95 96

    .init      = init,

    .priv_size = sizeof(AspectContext),

    .inputs    = (AVFilterPad[]) {{ .name             = "default",
97
                                    .type             = AVMEDIA_TYPE_VIDEO,
98
                                    .config_props     = setdar_config_props,
99
                                    .get_video_buffer = avfilter_null_get_video_buffer,
100
                                    .start_frame      = start_frame,
101
                                    .end_frame        = avfilter_null_end_frame },
102 103 104
                                  { .name = NULL}},

    .outputs   = (AVFilterPad[]) {{ .name             = "default",
105
                                    .type             = AVMEDIA_TYPE_VIDEO, },
106 107
                                  { .name = NULL}},
};
108
#endif /* CONFIG_SETDAR_FILTER */
109

110 111 112 113
#if CONFIG_SETSAR_FILTER
AVFilter avfilter_vf_setsar = {
    .name      = "setsar",
    .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
114 115 116 117 118 119

    .init      = init,

    .priv_size = sizeof(AspectContext),

    .inputs    = (AVFilterPad[]) {{ .name             = "default",
120
                                    .type             = AVMEDIA_TYPE_VIDEO,
121
                                    .get_video_buffer = avfilter_null_get_video_buffer,
122
                                    .start_frame      = start_frame,
123
                                    .end_frame        = avfilter_null_end_frame },
124 125 126
                                  { .name = NULL}},

    .outputs   = (AVFilterPad[]) {{ .name             = "default",
127
                                    .type             = AVMEDIA_TYPE_VIDEO, },
128 129
                                  { .name = NULL}},
};
130
#endif /* CONFIG_SETSAR_FILTER */
131