vf_aspect.c 4.76 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/common.h"
27
#include "libavutil/mathematics.h"
28
#include "avfilter.h"
29
#include "internal.h"
30
#include "video.h"
31 32 33 34 35

typedef struct {
    AVRational aspect;
} AspectContext;

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

43
    if (args) {
44 45 46 47 48 49 50 51 52 53
        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
54 55 56 57 58
        gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
        if (gcd) {
            aspect->aspect.num /= gcd;
            aspect->aspect.den /= gcd;
        }
59 60
    }

61
    if (aspect->aspect.den == 0)
62 63
        aspect->aspect = (AVRational) {0, 1};

64
    av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
65 66 67
    return 0;
}

68
static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
69 70 71
{
    AspectContext *aspect = link->dst->priv;

72
    picref->video->pixel_aspect = aspect->aspect;
73
    link->cur_buf = NULL;
74
    return ff_start_frame(link->dst->outputs[0], picref);
75 76
}

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

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

88
    av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
89
           inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
90 91 92

    inlink->sample_aspect_ratio = aspect->aspect;

93 94 95
    return 0;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static const AVFilterPad avfilter_vf_setdar_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .config_props     = setdar_config_props,
        .get_video_buffer = ff_null_get_video_buffer,
        .start_frame      = start_frame,
        .end_frame        = ff_null_end_frame
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_setdar_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

116 117 118
AVFilter avfilter_vf_setdar = {
    .name      = "setdar",
    .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
119 120 121 122 123

    .init      = init,

    .priv_size = sizeof(AspectContext),

124 125 126
    .inputs    = avfilter_vf_setdar_inputs,

    .outputs   = avfilter_vf_setdar_outputs,
127
};
128
#endif /* CONFIG_SETDAR_FILTER */
129

130
#if CONFIG_SETSAR_FILTER
131 132 133 134 135 136 137 138 139 140
/* 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;
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static const AVFilterPad avfilter_vf_setsar_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .config_props     = setsar_config_props,
        .get_video_buffer = ff_null_get_video_buffer,
        .start_frame      = start_frame,
        .end_frame        = ff_null_end_frame
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_setsar_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

161 162 163
AVFilter avfilter_vf_setsar = {
    .name      = "setsar",
    .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
164 165 166 167 168

    .init      = init,

    .priv_size = sizeof(AspectContext),

169 170 171
    .inputs    = avfilter_vf_setsar_inputs,

    .outputs   = avfilter_vf_setsar_outputs,
172
};
173
#endif /* CONFIG_SETSAR_FILTER */