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 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
84 if (sar.num && sar.den) {
85 av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
87 av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
91 #if CONFIG_SETDAR_FILTER
93 static int setdar_config_props(AVFilterLink *inlink)
95 AspectContext *aspect = inlink->dst->priv;
96 AVRational dar = aspect->aspect, old_dar;
97 AVRational old_sar = inlink->sample_aspect_ratio;
99 if (aspect->aspect.num && aspect->aspect.den) {
100 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
101 aspect->aspect.num * inlink->h,
102 aspect->aspect.den * inlink->w, INT_MAX);
103 inlink->sample_aspect_ratio = aspect->aspect;
105 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
106 dar = (AVRational){ inlink->w, inlink->h };
109 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
110 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
111 inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
112 dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
117 static const AVOption setdar_options[] = {
118 #if FF_API_OLD_FILTER_OPTS
119 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
120 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
122 { "dar", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
123 { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
124 { "r", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
125 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
129 AVFILTER_DEFINE_CLASS(setdar);
131 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
134 .type = AVMEDIA_TYPE_VIDEO,
135 .config_props = setdar_config_props,
136 .get_video_buffer = ff_null_get_video_buffer,
137 .filter_frame = filter_frame,
142 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
145 .type = AVMEDIA_TYPE_VIDEO,
150 AVFilter avfilter_vf_setdar = {
152 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
154 .priv_size = sizeof(AspectContext),
155 .priv_class = &setdar_class,
157 .inputs = avfilter_vf_setdar_inputs,
159 .outputs = avfilter_vf_setdar_outputs,
162 #endif /* CONFIG_SETDAR_FILTER */
164 #if CONFIG_SETSAR_FILTER
166 static int setsar_config_props(AVFilterLink *inlink)
168 AspectContext *aspect = inlink->dst->priv;
169 AVRational old_sar = inlink->sample_aspect_ratio;
170 AVRational old_dar, dar;
172 inlink->sample_aspect_ratio = aspect->aspect;
174 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
175 compute_dar(&dar, aspect->aspect, inlink->w, inlink->h);
176 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
177 inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
178 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
183 static const AVOption setsar_options[] = {
184 #if FF_API_OLD_FILTER_OPTS
185 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
186 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
188 { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
189 { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
190 { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
191 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
195 AVFILTER_DEFINE_CLASS(setsar);
197 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
200 .type = AVMEDIA_TYPE_VIDEO,
201 .config_props = setsar_config_props,
202 .get_video_buffer = ff_null_get_video_buffer,
203 .filter_frame = filter_frame,
208 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
211 .type = AVMEDIA_TYPE_VIDEO,
216 AVFilter avfilter_vf_setsar = {
218 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
220 .priv_size = sizeof(AspectContext),
221 .priv_class = &setsar_class,
223 .inputs = avfilter_vf_setsar_inputs,
225 .outputs = avfilter_vf_setsar_outputs,
228 #endif /* CONFIG_SETSAR_FILTER */