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
26 #include "libavutil/common.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/parseutils.h"
41 #define OFFSET(x) offsetof(AspectContext, x)
42 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
44 static const AVOption options[] = {
45 {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
46 {"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
47 {"r", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
51 static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
53 AspectContext *aspect = ctx->priv;
54 static const char *shorthand[] = { "ratio", "max", NULL };
59 aspect->class = class;
60 av_opt_set_defaults(aspect);
62 if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
63 aspect->ratio_str = av_strdup(args);
64 av_log(ctx, AV_LOG_WARNING,
65 "num:den syntax is deprecated, please use num/den or named options instead\n");
66 } else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
70 if (aspect->ratio_str) {
71 ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, aspect->max, 0, ctx);
72 if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
73 av_log(ctx, AV_LOG_ERROR,
74 "Invalid string '%s' for aspect ratio\n", args);
75 return AVERROR(EINVAL);
79 av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
83 static int filter_frame(AVFilterLink *link, AVFrame *frame)
85 AspectContext *aspect = link->dst->priv;
87 frame->sample_aspect_ratio = aspect->ratio;
88 return ff_filter_frame(link->dst->outputs[0], frame);
91 static av_cold void uninit(AVFilterContext *ctx)
93 AspectContext *aspect = ctx->priv;
98 #if CONFIG_SETDAR_FILTER
100 #define setdar_options options
101 AVFILTER_DEFINE_CLASS(setdar);
103 static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
105 return init(ctx, args, &setdar_class);
108 static int setdar_config_props(AVFilterLink *inlink)
110 AspectContext *aspect = inlink->dst->priv;
111 AVRational dar = aspect->ratio;
113 av_reduce(&aspect->ratio.num, &aspect->ratio.den,
114 aspect->ratio.num * inlink->h,
115 aspect->ratio.den * inlink->w, 100);
117 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
118 inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
120 inlink->sample_aspect_ratio = aspect->ratio;
125 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
128 .type = AVMEDIA_TYPE_VIDEO,
129 .config_props = setdar_config_props,
130 .get_video_buffer = ff_null_get_video_buffer,
131 .filter_frame = filter_frame,
136 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
139 .type = AVMEDIA_TYPE_VIDEO,
144 AVFilter avfilter_vf_setdar = {
146 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
151 .priv_size = sizeof(AspectContext),
153 .inputs = avfilter_vf_setdar_inputs,
155 .outputs = avfilter_vf_setdar_outputs,
156 .priv_class = &setdar_class,
159 #endif /* CONFIG_SETDAR_FILTER */
161 #if CONFIG_SETSAR_FILTER
163 #define setsar_options options
164 AVFILTER_DEFINE_CLASS(setsar);
166 static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
168 return init(ctx, args, &setsar_class);
171 static int setsar_config_props(AVFilterLink *inlink)
173 AspectContext *aspect = inlink->dst->priv;
175 inlink->sample_aspect_ratio = aspect->ratio;
180 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
183 .type = AVMEDIA_TYPE_VIDEO,
184 .config_props = setsar_config_props,
185 .get_video_buffer = ff_null_get_video_buffer,
186 .filter_frame = filter_frame,
191 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
194 .type = AVMEDIA_TYPE_VIDEO,
199 AVFilter avfilter_vf_setsar = {
201 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
206 .priv_size = sizeof(AspectContext),
208 .inputs = avfilter_vf_setsar_inputs,
210 .outputs = avfilter_vf_setsar_outputs,
211 .priv_class = &setsar_class,
214 #endif /* CONFIG_SETSAR_FILTER */