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