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 (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 start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
85 AspectContext *aspect = link->dst->priv;
87 picref->video->sample_aspect_ratio = aspect->ratio;
89 return ff_start_frame(link->dst->outputs[0], picref);
92 static av_cold void uninit(AVFilterContext *ctx)
94 AspectContext *aspect = ctx->priv;
99 #if CONFIG_SETDAR_FILTER
101 #define setdar_options options
102 AVFILTER_DEFINE_CLASS(setdar);
104 static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
106 return init(ctx, args, &setdar_class);
109 static int setdar_config_props(AVFilterLink *inlink)
111 AspectContext *aspect = inlink->dst->priv;
112 AVRational dar = aspect->ratio;
114 av_reduce(&aspect->ratio.num, &aspect->ratio.den,
115 aspect->ratio.num * inlink->h,
116 aspect->ratio.den * inlink->w, 100);
118 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
119 inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
121 inlink->sample_aspect_ratio = aspect->ratio;
126 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
129 .type = AVMEDIA_TYPE_VIDEO,
130 .config_props = setdar_config_props,
131 .get_video_buffer = ff_null_get_video_buffer,
132 .start_frame = start_frame,
133 .end_frame = ff_null_end_frame
138 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
141 .type = AVMEDIA_TYPE_VIDEO,
146 AVFilter avfilter_vf_setdar = {
148 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
153 .priv_size = sizeof(AspectContext),
155 .inputs = avfilter_vf_setdar_inputs,
157 .outputs = avfilter_vf_setdar_outputs,
158 .priv_class = &setdar_class,
161 #endif /* CONFIG_SETDAR_FILTER */
163 #if CONFIG_SETSAR_FILTER
165 #define setsar_options options
166 AVFILTER_DEFINE_CLASS(setsar);
168 static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
170 return init(ctx, args, &setsar_class);
173 static int setsar_config_props(AVFilterLink *inlink)
175 AspectContext *aspect = inlink->dst->priv;
177 inlink->sample_aspect_ratio = aspect->ratio;
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 .start_frame = start_frame,
189 .end_frame = ff_null_end_frame
194 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
197 .type = AVMEDIA_TYPE_VIDEO,
202 AVFilter avfilter_vf_setsar = {
204 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
209 .priv_size = sizeof(AspectContext),
211 .inputs = avfilter_vf_setsar_inputs,
213 .outputs = avfilter_vf_setsar_outputs,
214 .priv_class = &setsar_class,
217 #endif /* CONFIG_SETSAR_FILTER */