vf_aspect.c 4.89 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2010 Bobby Bingham

4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
18 19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
22
 * @file
23
 * aspect ratio modification video filters
24 25
 */

26
#include "libavutil/mathematics.h"
27 28 29 30 31 32 33 34 35 36 37
#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;
38
    char c = 0;
39

Stefano Sabatini's avatar
Stefano Sabatini committed
40
    if (args) {
41 42 43 44 45 46 47 48 49 50
        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
51 52 53 54 55
        gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
        if (gcd) {
            aspect->aspect.num /= gcd;
            aspect->aspect.den /= gcd;
        }
56 57
    }

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

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

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

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

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

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

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

    inlink->sample_aspect_ratio = aspect->aspect;

89 90 91
    return 0;
}

92 93 94
AVFilter avfilter_vf_setdar = {
    .name      = "setdar",
    .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
95 96 97 98 99 100

    .init      = init,

    .priv_size = sizeof(AspectContext),

    .inputs    = (AVFilterPad[]) {{ .name             = "default",
101
                                    .type             = AVMEDIA_TYPE_VIDEO,
102
                                    .config_props     = setdar_config_props,
103
                                    .get_video_buffer = avfilter_null_get_video_buffer,
104
                                    .start_frame      = start_frame,
105
                                    .end_frame        = avfilter_null_end_frame },
106 107 108
                                  { .name = NULL}},

    .outputs   = (AVFilterPad[]) {{ .name             = "default",
109
                                    .type             = AVMEDIA_TYPE_VIDEO, },
110 111
                                  { .name = NULL}},
};
112
#endif /* CONFIG_SETDAR_FILTER */
113

114
#if CONFIG_SETSAR_FILTER
115 116 117 118 119 120 121 122 123 124
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
static int setsar_config_props(AVFilterLink *inlink)
{
    AspectContext *aspect = inlink->dst->priv;

    inlink->sample_aspect_ratio = aspect->aspect;

    return 0;
}

125 126 127
AVFilter avfilter_vf_setsar = {
    .name      = "setsar",
    .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
128 129 130 131 132 133

    .init      = init,

    .priv_size = sizeof(AspectContext),

    .inputs    = (AVFilterPad[]) {{ .name             = "default",
134
                                    .type             = AVMEDIA_TYPE_VIDEO,
135
                                    .config_props     = setsar_config_props,
136
                                    .get_video_buffer = avfilter_null_get_video_buffer,
137
                                    .start_frame      = start_frame,
138
                                    .end_frame        = avfilter_null_end_frame },
139 140 141
                                  { .name = NULL}},

    .outputs   = (AVFilterPad[]) {{ .name             = "default",
142
                                    .type             = AVMEDIA_TYPE_VIDEO, },
143 144
                                  { .name = NULL}},
};
145
#endif /* CONFIG_SETSAR_FILTER */
146