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/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
39 static const char *const var_names[] = {
59 typedef struct AspectContext {
67 static int filter_frame(AVFilterLink *link, AVFrame *frame)
69 AspectContext *s = link->dst->priv;
71 frame->sample_aspect_ratio = s->sar;
72 return ff_filter_frame(link->dst->outputs[0], frame);
75 #define OFFSET(x) offsetof(AspectContext, x)
76 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
78 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
80 if (sar.num && sar.den) {
81 av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
83 av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
87 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
89 AVFilterContext *ctx = inlink->dst;
90 AspectContext *s = inlink->dst->priv;
91 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
92 double var_values[VARS_NB], res;
95 var_values[VAR_W] = inlink->w;
96 var_values[VAR_H] = inlink->h;
97 var_values[VAR_A] = (double) inlink->w / inlink->h;
98 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
99 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
100 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
101 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
102 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
104 /* evaluate new aspect ratio*/
105 ret = av_expr_parse_and_eval(&res, s->ratio_expr,
106 var_names, var_values,
107 NULL, NULL, NULL, NULL, NULL, 0, ctx);
109 ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
111 *aspect_ratio = av_d2q(res, s->max);
114 av_log(ctx, AV_LOG_ERROR,
115 "Error when evaluating the expression '%s'\n", s->ratio_expr);
118 if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
119 av_log(ctx, AV_LOG_ERROR,
120 "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
121 return AVERROR(EINVAL);
126 #if CONFIG_SETDAR_FILTER
128 static int setdar_config_props(AVFilterLink *outlink)
130 AVFilterContext *ctx = outlink->src;
131 AVFilterLink *inlink = ctx->inputs[0];
132 AspectContext *s = ctx->priv;
135 AVRational old_sar = inlink->sample_aspect_ratio;
138 if ((ret = get_aspect_ratio(inlink, &s->dar)))
141 if (s->dar.num && s->dar.den) {
142 av_reduce(&s->sar.num, &s->sar.den,
143 s->dar.num * inlink->h,
144 s->dar.den * inlink->w, INT_MAX);
145 outlink->sample_aspect_ratio = s->sar;
148 outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
149 dar = (AVRational){ inlink->w, inlink->h };
152 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
153 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
154 inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
155 dar.num, dar.den, outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
160 static const AVOption setdar_options[] = {
161 { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
162 { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
163 { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
164 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
168 AVFILTER_DEFINE_CLASS(setdar);
170 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
173 .type = AVMEDIA_TYPE_VIDEO,
174 .filter_frame = filter_frame,
179 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
182 .type = AVMEDIA_TYPE_VIDEO,
183 .config_props = setdar_config_props,
188 AVFilter ff_vf_setdar = {
190 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
191 .priv_size = sizeof(AspectContext),
192 .priv_class = &setdar_class,
193 .inputs = avfilter_vf_setdar_inputs,
194 .outputs = avfilter_vf_setdar_outputs,
197 #endif /* CONFIG_SETDAR_FILTER */
199 #if CONFIG_SETSAR_FILTER
201 static int setsar_config_props(AVFilterLink *outlink)
203 AVFilterContext *ctx = outlink->src;
204 AVFilterLink *inlink = ctx->inputs[0];
205 AspectContext *s = ctx->priv;
206 AVRational old_sar = inlink->sample_aspect_ratio;
207 AVRational old_dar, dar;
210 if ((ret = get_aspect_ratio(inlink, &s->sar)))
213 outlink->sample_aspect_ratio = s->sar;
215 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
216 compute_dar(&dar, s->sar, inlink->w, inlink->h);
217 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
218 inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
219 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den, dar.num, dar.den);
224 static const AVOption setsar_options[] = {
225 { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
226 { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
227 { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
228 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
232 AVFILTER_DEFINE_CLASS(setsar);
234 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
237 .type = AVMEDIA_TYPE_VIDEO,
238 .filter_frame = filter_frame,
243 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
246 .type = AVMEDIA_TYPE_VIDEO,
247 .config_props = setsar_config_props,
252 AVFilter ff_vf_setsar = {
254 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
255 .priv_size = sizeof(AspectContext),
256 .priv_class = &setsar_class,
257 .inputs = avfilter_vf_setsar_inputs,
258 .outputs = avfilter_vf_setsar_outputs,
261 #endif /* CONFIG_SETSAR_FILTER */