fate: add h263 obmc vsynth tests
[libav.git] / libavfilter / vf_aspect.c
CommitLineData
3922deb5 1/*
3922deb5
BB
2 * Copyright (c) 2010 Bobby Bingham
3
2912e87a 4 * This file is part of Libav.
3922deb5 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
3922deb5
BB
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
3922deb5
BB
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
3922deb5
BB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
ba87f080 22 * @file
cef4b74b 23 * aspect ratio modification video filters
3922deb5
BB
24 */
25
1d9c2dc8 26#include "libavutil/common.h"
0ebcdf5c 27#include "libavutil/mathematics.h"
3922deb5 28#include "avfilter.h"
9d0bfc50 29#include "internal.h"
c04c533f 30#include "video.h"
3922deb5
BB
31
32typedef struct {
33 AVRational aspect;
34} AspectContext;
35
a5e8c41c 36static av_cold int init(AVFilterContext *ctx, const char *args)
3922deb5
BB
37{
38 AspectContext *aspect = ctx->priv;
39 double ratio;
40 int64_t gcd;
7de19a32 41 char c = 0;
3922deb5 42
bdb47f3a 43 if (args) {
7de19a32
SS
44 if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
45 if (sscanf(args, "%lf%c", &ratio, &c) == 1)
46 aspect->aspect = av_d2q(ratio, 100);
47
48 if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
49 av_log(ctx, AV_LOG_ERROR,
50 "Invalid string '%s' for aspect ratio.\n", args);
51 return AVERROR(EINVAL);
52 }
53
b7be04f3
SS
54 gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
55 if (gcd) {
56 aspect->aspect.num /= gcd;
57 aspect->aspect.den /= gcd;
58 }
3922deb5
BB
59 }
60
bdb47f3a 61 if (aspect->aspect.den == 0)
3922deb5
BB
62 aspect->aspect = (AVRational) {0, 1};
63
1a49a169 64 av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
3922deb5
BB
65 return 0;
66}
67
ebc8d974 68static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
3922deb5
BB
69{
70 AspectContext *aspect = link->dst->priv;
71
cc80caff 72 picref->video->pixel_aspect = aspect->aspect;
51a8049b 73 link->cur_buf = NULL;
ebc8d974 74 return ff_start_frame(link->dst->outputs[0], picref);
3922deb5
BB
75}
76
2fb21bf4
SS
77#if CONFIG_SETDAR_FILTER
78/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
79static int setdar_config_props(AVFilterLink *inlink)
3922deb5
BB
80{
81 AspectContext *aspect = inlink->dst->priv;
eee0ef5e 82 AVRational dar = aspect->aspect;
3922deb5
BB
83
84 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
85 aspect->aspect.num * inlink->h,
86 aspect->aspect.den * inlink->w, 100);
87
1a49a169 88 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
6fd2b8bd 89 inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
910b5b82
MN
90
91 inlink->sample_aspect_ratio = aspect->aspect;
92
3922deb5
BB
93 return 0;
94}
95
2fb21bf4
SS
96AVFilter avfilter_vf_setdar = {
97 .name = "setdar",
98 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
3922deb5
BB
99
100 .init = init,
101
102 .priv_size = sizeof(AspectContext),
103
3db40703
RB
104 .inputs = (const AVFilterPad[]) {{ .name = "default",
105 .type = AVMEDIA_TYPE_VIDEO,
106 .config_props = setdar_config_props,
107 .get_video_buffer = ff_null_get_video_buffer,
108 .start_frame = start_frame,
109 .end_frame = ff_null_end_frame },
110 { .name = NULL}},
111
112 .outputs = (const AVFilterPad[]) {{ .name = "default",
113 .type = AVMEDIA_TYPE_VIDEO, },
114 { .name = NULL}},
3922deb5 115};
2fb21bf4 116#endif /* CONFIG_SETDAR_FILTER */
3922deb5 117
2fb21bf4 118#if CONFIG_SETSAR_FILTER
910b5b82
MN
119/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
120static int setsar_config_props(AVFilterLink *inlink)
121{
122 AspectContext *aspect = inlink->dst->priv;
123
124 inlink->sample_aspect_ratio = aspect->aspect;
125
126 return 0;
127}
128
2fb21bf4
SS
129AVFilter avfilter_vf_setsar = {
130 .name = "setsar",
131 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
3922deb5
BB
132
133 .init = init,
134
135 .priv_size = sizeof(AspectContext),
136
3db40703
RB
137 .inputs = (const AVFilterPad[]) {{ .name = "default",
138 .type = AVMEDIA_TYPE_VIDEO,
139 .config_props = setsar_config_props,
140 .get_video_buffer = ff_null_get_video_buffer,
141 .start_frame = start_frame,
142 .end_frame = ff_null_end_frame },
143 { .name = NULL}},
144
145 .outputs = (const AVFilterPad[]) {{ .name = "default",
146 .type = AVMEDIA_TYPE_VIDEO, },
147 { .name = NULL}},
3922deb5 148};
2fb21bf4 149#endif /* CONFIG_SETSAR_FILTER */