os_support: Add #endif comments for better readability
[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
2831b307
AK
26#include <float.h>
27
1d9c2dc8 28#include "libavutil/common.h"
0ebcdf5c 29#include "libavutil/mathematics.h"
2831b307
AK
30#include "libavutil/opt.h"
31
3922deb5 32#include "avfilter.h"
9d0bfc50 33#include "internal.h"
c04c533f 34#include "video.h"
3922deb5
BB
35
36typedef struct {
2831b307 37 const AVClass *class;
d5c66d9c
AK
38 AVRational dar;
39 AVRational sar;
2831b307
AK
40#if FF_API_OLD_FILTER_OPTS
41 float aspect_num, aspect_den;
42#endif
3922deb5
BB
43} AspectContext;
44
2831b307 45#if FF_API_OLD_FILTER_OPTS
c43a7eca 46static av_cold int init(AVFilterContext *ctx)
3922deb5 47{
2831b307 48 AspectContext *s = ctx->priv;
3922deb5 49
2831b307
AK
50 if (s->aspect_num > 0 && s->aspect_den > 0) {
51 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
52 "dar=<number> or dar=num/den.\n");
d5c66d9c 53 s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
2831b307 54 }
3922deb5
BB
55
56 return 0;
57}
2831b307 58#endif
3922deb5 59
7e350379 60static int filter_frame(AVFilterLink *link, AVFrame *frame)
3922deb5 61{
b3ea7662 62 AspectContext *s = link->dst->priv;
3922deb5 63
d5c66d9c 64 frame->sample_aspect_ratio = s->sar;
565e4993 65 return ff_filter_frame(link->dst->outputs[0], frame);
3922deb5
BB
66}
67
2831b307
AK
68#define OFFSET(x) offsetof(AspectContext, x)
69#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
70
2fb21bf4
SS
71#if CONFIG_SETDAR_FILTER
72/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
73static int setdar_config_props(AVFilterLink *inlink)
3922deb5 74{
b3ea7662 75 AspectContext *s = inlink->dst->priv;
d5c66d9c
AK
76 AVRational dar;
77
78 if (s->dar.num && s->dar.den) {
79 av_reduce(&s->sar.num, &s->sar.den,
80 s->dar.num * inlink->h,
81 s->dar.den * inlink->w, 100);
82 inlink->sample_aspect_ratio = s->sar;
83 dar = s->dar;
2831b307
AK
84 } else {
85 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
86 dar = (AVRational){ inlink->w, inlink->h };
87 }
3922deb5 88
1a49a169 89 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
2831b307
AK
90 inlink->w, inlink->h, dar.num, dar.den,
91 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
910b5b82 92
3922deb5
BB
93 return 0;
94}
95
2831b307
AK
96static const AVOption setdar_options[] = {
97#if FF_API_OLD_FILTER_OPTS
98 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
99 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
100#endif
d5c66d9c 101 { "dar", "display aspect ratio", OFFSET(dar), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
2831b307
AK
102 { NULL },
103};
104
105static const AVClass setdar_class = {
106 .class_name = "setdar",
107 .item_name = av_default_item_name,
108 .option = setdar_options,
109 .version = LIBAVUTIL_VERSION_INT,
110};
111
568c70e7
MR
112static const AVFilterPad avfilter_vf_setdar_inputs[] = {
113 {
114 .name = "default",
115 .type = AVMEDIA_TYPE_VIDEO,
116 .config_props = setdar_config_props,
117 .get_video_buffer = ff_null_get_video_buffer,
565e4993 118 .filter_frame = filter_frame,
568c70e7
MR
119 },
120 { NULL }
121};
122
123static const AVFilterPad avfilter_vf_setdar_outputs[] = {
124 {
125 .name = "default",
126 .type = AVMEDIA_TYPE_VIDEO,
127 },
128 { NULL }
129};
130
cd43ca04 131AVFilter ff_vf_setdar = {
2fb21bf4
SS
132 .name = "setdar",
133 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
3922deb5 134
2831b307 135#if FF_API_OLD_FILTER_OPTS
3922deb5 136 .init = init,
2831b307 137#endif
3922deb5
BB
138
139 .priv_size = sizeof(AspectContext),
2831b307 140 .priv_class = &setdar_class,
3922deb5 141
568c70e7
MR
142 .inputs = avfilter_vf_setdar_inputs,
143
144 .outputs = avfilter_vf_setdar_outputs,
3922deb5 145};
2fb21bf4 146#endif /* CONFIG_SETDAR_FILTER */
3922deb5 147
2fb21bf4 148#if CONFIG_SETSAR_FILTER
910b5b82
MN
149/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
150static int setsar_config_props(AVFilterLink *inlink)
151{
b3ea7662 152 AspectContext *s = inlink->dst->priv;
910b5b82 153
d5c66d9c 154 inlink->sample_aspect_ratio = s->sar;
910b5b82
MN
155
156 return 0;
157}
158
2831b307
AK
159static const AVOption setsar_options[] = {
160#if FF_API_OLD_FILTER_OPTS
161 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
162 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
163#endif
d5c66d9c 164 { "sar", "sample (pixel) aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
2831b307
AK
165 { NULL },
166};
167
168static const AVClass setsar_class = {
169 .class_name = "setsar",
170 .item_name = av_default_item_name,
171 .option = setsar_options,
172 .version = LIBAVUTIL_VERSION_INT,
173};
174
568c70e7
MR
175static const AVFilterPad avfilter_vf_setsar_inputs[] = {
176 {
177 .name = "default",
178 .type = AVMEDIA_TYPE_VIDEO,
179 .config_props = setsar_config_props,
180 .get_video_buffer = ff_null_get_video_buffer,
565e4993 181 .filter_frame = filter_frame,
568c70e7
MR
182 },
183 { NULL }
184};
185
186static const AVFilterPad avfilter_vf_setsar_outputs[] = {
187 {
188 .name = "default",
189 .type = AVMEDIA_TYPE_VIDEO,
190 },
191 { NULL }
192};
193
cd43ca04 194AVFilter ff_vf_setsar = {
2fb21bf4
SS
195 .name = "setsar",
196 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
3922deb5 197
2831b307 198#if FF_API_OLD_FILTER_OPTS
3922deb5 199 .init = init,
2831b307 200#endif
3922deb5
BB
201
202 .priv_size = sizeof(AspectContext),
2831b307 203 .priv_class = &setsar_class,
3922deb5 204
568c70e7
MR
205 .inputs = avfilter_vf_setsar_inputs,
206
207 .outputs = avfilter_vf_setsar_outputs,
3922deb5 208};
2fb21bf4 209#endif /* CONFIG_SETSAR_FILTER */