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 {
72 static int filter_frame(AVFilterLink *link, AVFrame *frame)
74 AspectContext *s = link->dst->priv;
76 frame->sample_aspect_ratio = s->sar;
77 return ff_filter_frame(link->dst->outputs[0], frame);
80 #define OFFSET(x) offsetof(AspectContext, x)
81 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
83 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
85 AVFilterContext *ctx = inlink->dst;
86 AspectContext *s = inlink->dst->priv;
87 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
88 double var_values[VARS_NB], res;
91 var_values[VAR_PI] = M_PI;
92 var_values[VAR_PHI] = M_PHI;
93 var_values[VAR_E] = M_E;
94 var_values[VAR_W] = inlink->w;
95 var_values[VAR_H] = inlink->h;
96 var_values[VAR_A] = (double) inlink->w / inlink->h;
97 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
98 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
99 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
100 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
101 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
103 /* evaluate new aspect ratio*/
104 if ((ret = av_expr_parse_and_eval(&res, s->ratio_expr,
105 var_names, var_values,
106 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
107 av_log(NULL, AV_LOG_ERROR,
108 "Error when evaluating the expression '%s'\n", s->ratio_expr);
111 *aspect_ratio = av_d2q(res, INT_MAX);
115 #if CONFIG_SETDAR_FILTER
116 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
117 static int setdar_config_props(AVFilterLink *inlink)
119 AspectContext *s = inlink->dst->priv;
123 if ((ret = get_aspect_ratio(inlink, &s->dar)))
126 if (s->dar.num && s->dar.den) {
127 av_reduce(&s->sar.num, &s->sar.den,
128 s->dar.num * inlink->h,
129 s->dar.den * inlink->w, 100);
130 inlink->sample_aspect_ratio = s->sar;
133 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
134 dar = (AVRational){ inlink->w, inlink->h };
137 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
138 inlink->w, inlink->h, dar.num, dar.den,
139 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
144 static const AVOption setdar_options[] = {
145 { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
149 static const AVClass setdar_class = {
150 .class_name = "setdar",
151 .item_name = av_default_item_name,
152 .option = setdar_options,
153 .version = LIBAVUTIL_VERSION_INT,
156 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
159 .type = AVMEDIA_TYPE_VIDEO,
160 .config_props = setdar_config_props,
161 .get_video_buffer = ff_null_get_video_buffer,
162 .filter_frame = filter_frame,
167 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
170 .type = AVMEDIA_TYPE_VIDEO,
175 AVFilter ff_vf_setdar = {
177 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
179 .priv_size = sizeof(AspectContext),
180 .priv_class = &setdar_class,
182 .inputs = avfilter_vf_setdar_inputs,
184 .outputs = avfilter_vf_setdar_outputs,
186 #endif /* CONFIG_SETDAR_FILTER */
188 #if CONFIG_SETSAR_FILTER
189 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
190 static int setsar_config_props(AVFilterLink *inlink)
192 AspectContext *s = inlink->dst->priv;
195 if ((ret = get_aspect_ratio(inlink, &s->sar)))
198 inlink->sample_aspect_ratio = s->sar;
203 static const AVOption setsar_options[] = {
204 { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
208 static const AVClass setsar_class = {
209 .class_name = "setsar",
210 .item_name = av_default_item_name,
211 .option = setsar_options,
212 .version = LIBAVUTIL_VERSION_INT,
215 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
218 .type = AVMEDIA_TYPE_VIDEO,
219 .config_props = setsar_config_props,
220 .get_video_buffer = ff_null_get_video_buffer,
221 .filter_frame = filter_frame,
226 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
229 .type = AVMEDIA_TYPE_VIDEO,
234 AVFilter ff_vf_setsar = {
236 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
238 .priv_size = sizeof(AspectContext),
239 .priv_class = &setsar_class,
241 .inputs = avfilter_vf_setsar_inputs,
243 .outputs = avfilter_vf_setsar_outputs,
245 #endif /* CONFIG_SETSAR_FILTER */