2 * Copyright (c) 2010 Bobby Bingham
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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[] = {
65 typedef struct AspectContext {
69 #if FF_API_OLD_FILTER_OPTS
70 float aspect_num, aspect_den;
75 #if FF_API_OLD_FILTER_OPTS
76 static av_cold int init(AVFilterContext *ctx)
78 AspectContext *s = ctx->priv;
80 if (s->aspect_num > 0 && s->aspect_den > 0) {
81 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
82 "dar=<number> or dar=num/den.\n");
83 s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
90 static int filter_frame(AVFilterLink *link, AVFrame *frame)
92 AspectContext *s = link->dst->priv;
94 frame->sample_aspect_ratio = s->sar;
95 return ff_filter_frame(link->dst->outputs[0], frame);
98 #define OFFSET(x) offsetof(AspectContext, x)
99 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
101 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
103 AVFilterContext *ctx = inlink->dst;
104 AspectContext *s = inlink->dst->priv;
105 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
106 double var_values[VARS_NB], res;
109 var_values[VAR_PI] = M_PI;
110 var_values[VAR_PHI] = M_PHI;
111 var_values[VAR_E] = M_E;
112 var_values[VAR_W] = inlink->w;
113 var_values[VAR_H] = inlink->h;
114 var_values[VAR_A] = (double) inlink->w / inlink->h;
115 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
116 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
117 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
118 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
119 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
121 /* evaluate new aspect ratio*/
122 if ((ret = av_expr_parse_and_eval(&res, s->ratio_expr,
123 var_names, var_values,
124 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
125 av_log(NULL, AV_LOG_ERROR,
126 "Error when evaluating the expression '%s'\n", s->ratio_expr);
129 *aspect_ratio = av_d2q(res, INT_MAX);
133 #if CONFIG_SETDAR_FILTER
134 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
135 static int setdar_config_props(AVFilterLink *inlink)
137 AspectContext *s = inlink->dst->priv;
141 #if FF_API_OLD_FILTER_OPTS
142 if (!(s->aspect_num > 0 && s->aspect_den > 0)) {
144 if ((ret = get_aspect_ratio(inlink, &s->dar)))
146 #if FF_API_OLD_FILTER_OPTS
150 if (s->dar.num && s->dar.den) {
151 av_reduce(&s->sar.num, &s->sar.den,
152 s->dar.num * inlink->h,
153 s->dar.den * inlink->w, 100);
154 inlink->sample_aspect_ratio = s->sar;
157 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
158 dar = (AVRational){ inlink->w, inlink->h };
161 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
162 inlink->w, inlink->h, dar.num, dar.den,
163 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
168 static const AVOption setdar_options[] = {
169 #if FF_API_OLD_FILTER_OPTS
170 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
171 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
173 { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
177 static const AVClass setdar_class = {
178 .class_name = "setdar",
179 .item_name = av_default_item_name,
180 .option = setdar_options,
181 .version = LIBAVUTIL_VERSION_INT,
184 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
187 .type = AVMEDIA_TYPE_VIDEO,
188 .config_props = setdar_config_props,
189 .get_video_buffer = ff_null_get_video_buffer,
190 .filter_frame = filter_frame,
195 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
198 .type = AVMEDIA_TYPE_VIDEO,
203 AVFilter ff_vf_setdar = {
205 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
207 #if FF_API_OLD_FILTER_OPTS
211 .priv_size = sizeof(AspectContext),
212 .priv_class = &setdar_class,
214 .inputs = avfilter_vf_setdar_inputs,
216 .outputs = avfilter_vf_setdar_outputs,
218 #endif /* CONFIG_SETDAR_FILTER */
220 #if CONFIG_SETSAR_FILTER
221 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
222 static int setsar_config_props(AVFilterLink *inlink)
224 AspectContext *s = inlink->dst->priv;
227 #if FF_API_OLD_FILTER_OPTS
228 if (!(s->aspect_num > 0 && s->aspect_den > 0)) {
230 if ((ret = get_aspect_ratio(inlink, &s->sar)))
232 #if FF_API_OLD_FILTER_OPTS
236 inlink->sample_aspect_ratio = s->sar;
241 static const AVOption setsar_options[] = {
242 #if FF_API_OLD_FILTER_OPTS
243 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
244 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
246 { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
250 static const AVClass setsar_class = {
251 .class_name = "setsar",
252 .item_name = av_default_item_name,
253 .option = setsar_options,
254 .version = LIBAVUTIL_VERSION_INT,
257 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
260 .type = AVMEDIA_TYPE_VIDEO,
261 .config_props = setsar_config_props,
262 .get_video_buffer = ff_null_get_video_buffer,
263 .filter_frame = filter_frame,
268 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
271 .type = AVMEDIA_TYPE_VIDEO,
276 AVFilter ff_vf_setsar = {
278 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
280 #if FF_API_OLD_FILTER_OPTS
284 .priv_size = sizeof(AspectContext),
285 .priv_class = &setsar_class,
287 .inputs = avfilter_vf_setsar_inputs,
289 .outputs = avfilter_vf_setsar_outputs,
291 #endif /* CONFIG_SETSAR_FILTER */