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"
42 #if FF_API_OLD_FILTER_OPTS
43 float aspect_num, aspect_den;
48 static av_cold int init(AVFilterContext *ctx)
50 AspectContext *s = ctx->priv;
53 #if FF_API_OLD_FILTER_OPTS
54 if (s->aspect_num > 0 && s->aspect_den > 0) {
55 av_log(ctx, AV_LOG_WARNING,
56 "num:den syntax is deprecated, please use num/den or named options instead\n");
57 s->aspect = av_d2q(s->aspect_num / s->aspect_den, s->max);
61 ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
62 if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
63 av_log(ctx, AV_LOG_ERROR,
64 "Invalid string '%s' for aspect ratio\n", s->ratio_str);
65 return AVERROR(EINVAL);
71 static int filter_frame(AVFilterLink *link, AVFrame *frame)
73 AspectContext *aspect = link->dst->priv;
75 frame->sample_aspect_ratio = aspect->aspect;
76 return ff_filter_frame(link->dst->outputs[0], frame);
79 #define OFFSET(x) offsetof(AspectContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82 #if CONFIG_SETDAR_FILTER
84 static int setdar_config_props(AVFilterLink *inlink)
86 AspectContext *aspect = inlink->dst->priv;
87 AVRational dar = aspect->aspect;
89 if (aspect->aspect.num && aspect->aspect.den) {
90 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
91 aspect->aspect.num * inlink->h,
92 aspect->aspect.den * inlink->w, INT_MAX);
93 inlink->sample_aspect_ratio = aspect->aspect;
95 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
96 dar = (AVRational){ inlink->w, inlink->h };
99 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
100 inlink->w, inlink->h, dar.num, dar.den,
101 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
106 static const AVOption setdar_options[] = {
107 #if FF_API_OLD_FILTER_OPTS
108 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
109 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
111 { "dar", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
112 { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
113 { "r", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
114 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
118 AVFILTER_DEFINE_CLASS(setdar);
120 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
123 .type = AVMEDIA_TYPE_VIDEO,
124 .config_props = setdar_config_props,
125 .get_video_buffer = ff_null_get_video_buffer,
126 .filter_frame = filter_frame,
131 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
134 .type = AVMEDIA_TYPE_VIDEO,
139 AVFilter avfilter_vf_setdar = {
141 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
143 .priv_size = sizeof(AspectContext),
144 .priv_class = &setdar_class,
146 .inputs = avfilter_vf_setdar_inputs,
148 .outputs = avfilter_vf_setdar_outputs,
151 #endif /* CONFIG_SETDAR_FILTER */
153 #if CONFIG_SETSAR_FILTER
156 static int setsar_config_props(AVFilterLink *inlink)
158 AspectContext *aspect = inlink->dst->priv;
160 inlink->sample_aspect_ratio = aspect->aspect;
165 static const AVOption setsar_options[] = {
166 #if FF_API_OLD_FILTER_OPTS
167 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
168 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
170 { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
171 { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
172 { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
173 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
177 AVFILTER_DEFINE_CLASS(setsar);
179 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
182 .type = AVMEDIA_TYPE_VIDEO,
183 .config_props = setsar_config_props,
184 .get_video_buffer = ff_null_get_video_buffer,
185 .filter_frame = filter_frame,
190 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
193 .type = AVMEDIA_TYPE_VIDEO,
198 AVFilter avfilter_vf_setsar = {
200 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
202 #if FF_API_OLD_FILTER_OPTS
206 .priv_size = sizeof(AspectContext),
207 .priv_class = &setsar_class,
209 .inputs = avfilter_vf_setsar_inputs,
211 .outputs = avfilter_vf_setsar_outputs,
214 #endif /* CONFIG_SETSAR_FILTER */