2 * Copyright (c) 2010 Bobby Bingham
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * 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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * aspect ratio modification video filters
28 #include "libavutil/common.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
39 #if FF_API_OLD_FILTER_OPTS
40 float aspect_num, aspect_den;
44 #if FF_API_OLD_FILTER_OPTS
45 static av_cold int init(AVFilterContext *ctx)
47 AspectContext *s = ctx->priv;
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);
59 static int filter_frame(AVFilterLink *link, AVFrame *frame)
61 AspectContext *s = link->dst->priv;
63 frame->sample_aspect_ratio = s->aspect;
64 return ff_filter_frame(link->dst->outputs[0], frame);
67 #define OFFSET(x) offsetof(AspectContext, x)
68 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
70 #if CONFIG_SETDAR_FILTER
71 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
72 static int setdar_config_props(AVFilterLink *inlink)
74 AspectContext *s = inlink->dst->priv;
75 AVRational dar = s->aspect;
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;
83 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
84 dar = (AVRational){ inlink->w, inlink->h };
87 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
88 inlink->w, inlink->h, dar.num, dar.den,
89 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
94 static 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 },
99 { "dar", "display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
103 static const AVClass setdar_class = {
104 .class_name = "setdar",
105 .item_name = av_default_item_name,
106 .option = setdar_options,
107 .version = LIBAVUTIL_VERSION_INT,
110 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
113 .type = AVMEDIA_TYPE_VIDEO,
114 .config_props = setdar_config_props,
115 .get_video_buffer = ff_null_get_video_buffer,
116 .filter_frame = filter_frame,
121 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
124 .type = AVMEDIA_TYPE_VIDEO,
129 AVFilter avfilter_vf_setdar = {
131 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
133 #if FF_API_OLD_FILTER_OPTS
137 .priv_size = sizeof(AspectContext),
138 .priv_class = &setdar_class,
140 .inputs = avfilter_vf_setdar_inputs,
142 .outputs = avfilter_vf_setdar_outputs,
144 #endif /* CONFIG_SETDAR_FILTER */
146 #if CONFIG_SETSAR_FILTER
147 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
148 static int setsar_config_props(AVFilterLink *inlink)
150 AspectContext *s = inlink->dst->priv;
152 inlink->sample_aspect_ratio = s->aspect;
157 static 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 },
162 { "sar", "sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
166 static const AVClass setsar_class = {
167 .class_name = "setsar",
168 .item_name = av_default_item_name,
169 .option = setsar_options,
170 .version = LIBAVUTIL_VERSION_INT,
173 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
176 .type = AVMEDIA_TYPE_VIDEO,
177 .config_props = setsar_config_props,
178 .get_video_buffer = ff_null_get_video_buffer,
179 .filter_frame = filter_frame,
184 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
187 .type = AVMEDIA_TYPE_VIDEO,
192 AVFilter avfilter_vf_setsar = {
194 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
196 #if FF_API_OLD_FILTER_OPTS
200 .priv_size = sizeof(AspectContext),
201 .priv_class = &setsar_class,
203 .inputs = avfilter_vf_setsar_inputs,
205 .outputs = avfilter_vf_setsar_outputs,
207 #endif /* CONFIG_SETSAR_FILTER */