Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_aspect.c @ 910b5b82

History | View | Annotate | Download (4.86 KB)

1 3922deb5 Bobby Bingham
/*
2
 * Copyright (c) 2010 Bobby Bingham
3

4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22 ba87f080 Diego Biurrun
 * @file
23 cef4b74b Stefano Sabatini
 * aspect ratio modification video filters
24 3922deb5 Bobby Bingham
 */
25
26
#include "avfilter.h"
27
28
typedef struct {
29
    AVRational aspect;
30
} AspectContext;
31
32
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
33
{
34
    AspectContext *aspect = ctx->priv;
35
    double  ratio;
36
    int64_t gcd;
37 7de19a32 Stefano Sabatini
    char c = 0;
38 3922deb5 Bobby Bingham
39 bdb47f3a Stefano Sabatini
    if (args) {
40 7de19a32 Stefano Sabatini
        if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
41
            if (sscanf(args, "%lf%c", &ratio, &c) == 1)
42
                aspect->aspect = av_d2q(ratio, 100);
43
44
        if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
45
            av_log(ctx, AV_LOG_ERROR,
46
                   "Invalid string '%s' for aspect ratio.\n", args);
47
            return AVERROR(EINVAL);
48
        }
49
50 b7be04f3 Stefano Sabatini
        gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
51
        if (gcd) {
52
            aspect->aspect.num /= gcd;
53
            aspect->aspect.den /= gcd;
54
        }
55 3922deb5 Bobby Bingham
    }
56
57 bdb47f3a Stefano Sabatini
    if (aspect->aspect.den == 0)
58 3922deb5 Bobby Bingham
        aspect->aspect = (AVRational) {0, 1};
59
60 eee0ef5e Stefano Sabatini
    av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
61 3922deb5 Bobby Bingham
    return 0;
62
}
63
64 ecc8dada S.N. Hemanth Meenakshisundaram
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
65 3922deb5 Bobby Bingham
{
66
    AspectContext *aspect = link->dst->priv;
67
68 cc80caff S.N. Hemanth Meenakshisundaram
    picref->video->pixel_aspect = aspect->aspect;
69 3922deb5 Bobby Bingham
    avfilter_start_frame(link->dst->outputs[0], picref);
70
}
71
72 2fb21bf4 Stefano Sabatini
#if CONFIG_SETDAR_FILTER
73
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
74
static int setdar_config_props(AVFilterLink *inlink)
75 3922deb5 Bobby Bingham
{
76
    AspectContext *aspect = inlink->dst->priv;
77 eee0ef5e Stefano Sabatini
    AVRational dar = aspect->aspect;
78 3922deb5 Bobby Bingham
79
    av_reduce(&aspect->aspect.num, &aspect->aspect.den,
80
               aspect->aspect.num * inlink->h,
81
               aspect->aspect.den * inlink->w, 100);
82
83 eee0ef5e Stefano Sabatini
    av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d par:%d/%d\n",
84 6fd2b8bd Baptiste Coudurier
           inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
85 910b5b82 Michael Niedermayer
86
    inlink->sample_aspect_ratio = aspect->aspect;
87
88 3922deb5 Bobby Bingham
    return 0;
89
}
90
91 2fb21bf4 Stefano Sabatini
AVFilter avfilter_vf_setdar = {
92
    .name      = "setdar",
93
    .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
94 3922deb5 Bobby Bingham
95
    .init      = init,
96
97
    .priv_size = sizeof(AspectContext),
98
99
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
100 72415b2a Stefano Sabatini
                                    .type             = AVMEDIA_TYPE_VIDEO,
101 2fb21bf4 Stefano Sabatini
                                    .config_props     = setdar_config_props,
102 d313e17a Bobby Bingham
                                    .get_video_buffer = avfilter_null_get_video_buffer,
103 3922deb5 Bobby Bingham
                                    .start_frame      = start_frame,
104 d313e17a Bobby Bingham
                                    .end_frame        = avfilter_null_end_frame },
105 3922deb5 Bobby Bingham
                                  { .name = NULL}},
106
107
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
108 72415b2a Stefano Sabatini
                                    .type             = AVMEDIA_TYPE_VIDEO, },
109 3922deb5 Bobby Bingham
                                  { .name = NULL}},
110
};
111 2fb21bf4 Stefano Sabatini
#endif /* CONFIG_SETDAR_FILTER */
112 3922deb5 Bobby Bingham
113 2fb21bf4 Stefano Sabatini
#if CONFIG_SETSAR_FILTER
114 910b5b82 Michael Niedermayer
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
115
static int setsar_config_props(AVFilterLink *inlink)
116
{
117
    AspectContext *aspect = inlink->dst->priv;
118
119
    inlink->sample_aspect_ratio = aspect->aspect;
120
121
    return 0;
122
}
123
124 2fb21bf4 Stefano Sabatini
AVFilter avfilter_vf_setsar = {
125
    .name      = "setsar",
126
    .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
127 3922deb5 Bobby Bingham
128
    .init      = init,
129
130
    .priv_size = sizeof(AspectContext),
131
132
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
133 72415b2a Stefano Sabatini
                                    .type             = AVMEDIA_TYPE_VIDEO,
134 910b5b82 Michael Niedermayer
                                    .config_props     = setsar_config_props,
135 d313e17a Bobby Bingham
                                    .get_video_buffer = avfilter_null_get_video_buffer,
136 3922deb5 Bobby Bingham
                                    .start_frame      = start_frame,
137 d313e17a Bobby Bingham
                                    .end_frame        = avfilter_null_end_frame },
138 3922deb5 Bobby Bingham
                                  { .name = NULL}},
139
140
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
141 72415b2a Stefano Sabatini
                                    .type             = AVMEDIA_TYPE_VIDEO, },
142 3922deb5 Bobby Bingham
                                  { .name = NULL}},
143
};
144 2fb21bf4 Stefano Sabatini
#endif /* CONFIG_SETSAR_FILTER */