2 * Copyright (c) 2010 Bobby Bingham
4 * This file is part of FFmpeg.
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.
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.
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
23 * aspect ratio modification video filters
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
43 #if FF_API_OLD_FILTER_OPTS
44 float aspect_num, aspect_den;
48 #if FF_API_OLD_FILTER_OPTS
49 static av_cold int init(AVFilterContext *ctx, const char *args)
51 AspectContext *s = ctx->priv;
52 static const char *shorthand[] = { "ratio", "max", NULL };
57 if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
58 s->ratio_str = av_strdup(args);
59 av_log(ctx, AV_LOG_WARNING,
60 "num:den syntax is deprecated, please use num/den or named options instead\n");
61 } else if ((ret = av_opt_set_from_string(s, args, shorthand, "=", ":")) < 0) {
66 ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
67 if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
68 av_log(ctx, AV_LOG_ERROR,
69 "Invalid string '%s' for aspect ratio\n", args);
70 return AVERROR(EINVAL);
78 static int filter_frame(AVFilterLink *link, AVFrame *frame)
80 AspectContext *aspect = link->dst->priv;
82 frame->sample_aspect_ratio = aspect->aspect;
83 return ff_filter_frame(link->dst->outputs[0], frame);
86 #define OFFSET(x) offsetof(AspectContext, x)
87 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
89 #if CONFIG_SETDAR_FILTER
91 static int setdar_config_props(AVFilterLink *inlink)
93 AspectContext *aspect = inlink->dst->priv;
94 AVRational dar = aspect->aspect;
96 if (aspect->aspect.num && aspect->aspect.den) {
97 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
98 aspect->aspect.num * inlink->h,
99 aspect->aspect.den * inlink->w, 100);
100 inlink->sample_aspect_ratio = aspect->aspect;
102 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
103 dar = (AVRational){ inlink->w, inlink->h };
106 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
107 inlink->w, inlink->h, dar.num, dar.den,
108 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
113 static const AVOption setdar_options[] = {
114 {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
115 {"ratio", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
116 {"r", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
117 {"dar", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
121 AVFILTER_DEFINE_CLASS(setdar);
123 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
126 .type = AVMEDIA_TYPE_VIDEO,
127 .config_props = setdar_config_props,
128 .get_video_buffer = ff_null_get_video_buffer,
129 .filter_frame = filter_frame,
134 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
137 .type = AVMEDIA_TYPE_VIDEO,
142 AVFilter avfilter_vf_setdar = {
144 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
146 #if FF_API_OLD_FILTER_OPTS
150 .priv_size = sizeof(AspectContext),
151 .priv_class = &setdar_class,
153 .inputs = avfilter_vf_setdar_inputs,
155 .outputs = avfilter_vf_setdar_outputs,
158 #endif /* CONFIG_SETDAR_FILTER */
160 #if CONFIG_SETSAR_FILTER
163 static int setsar_config_props(AVFilterLink *inlink)
165 AspectContext *aspect = inlink->dst->priv;
167 inlink->sample_aspect_ratio = aspect->aspect;
172 static const AVOption setsar_options[] = {
173 {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
174 {"ratio", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
175 {"r", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
176 {"sar", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
180 AVFILTER_DEFINE_CLASS(setsar);
182 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
185 .type = AVMEDIA_TYPE_VIDEO,
186 .config_props = setsar_config_props,
187 .get_video_buffer = ff_null_get_video_buffer,
188 .filter_frame = filter_frame,
193 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
196 .type = AVMEDIA_TYPE_VIDEO,
201 AVFilter avfilter_vf_setsar = {
203 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
205 #if FF_API_OLD_FILTER_OPTS
209 .priv_size = sizeof(AspectContext),
210 .priv_class = &setsar_class,
212 .inputs = avfilter_vf_setsar_inputs,
214 .outputs = avfilter_vf_setsar_outputs,
217 #endif /* CONFIG_SETSAR_FILTER */