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;
47 #if FF_API_OLD_FILTER_OPTS
48 static av_cold int init(AVFilterContext *ctx)
50 AspectContext *s = ctx->priv;
52 if (s->aspect_num > 0 && s->aspect_den > 0) {
53 av_log(ctx, AV_LOG_WARNING,
54 "num:den syntax is deprecated, please use num/den or named options instead\n");
55 s->aspect = av_d2q(s->aspect_num / s->aspect_den, s->max);
62 static int filter_frame(AVFilterLink *link, AVFrame *frame)
64 AspectContext *aspect = link->dst->priv;
66 frame->sample_aspect_ratio = aspect->aspect;
67 return ff_filter_frame(link->dst->outputs[0], frame);
70 #define OFFSET(x) offsetof(AspectContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73 #if CONFIG_SETDAR_FILTER
75 static int setdar_config_props(AVFilterLink *inlink)
77 AspectContext *aspect = inlink->dst->priv;
78 AVRational dar = aspect->aspect;
80 if (aspect->aspect.num && aspect->aspect.den) {
81 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
82 aspect->aspect.num * inlink->h,
83 aspect->aspect.den * inlink->w, 100);
84 inlink->sample_aspect_ratio = aspect->aspect;
86 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
87 dar = (AVRational){ inlink->w, inlink->h };
90 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
91 inlink->w, inlink->h, dar.num, dar.den,
92 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
97 static const AVOption setdar_options[] = {
98 #if FF_API_OLD_FILTER_OPTS
99 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
100 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
102 { "dar", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
103 { "ratio", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
104 { "r", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
105 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
109 AVFILTER_DEFINE_CLASS(setdar);
111 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
114 .type = AVMEDIA_TYPE_VIDEO,
115 .config_props = setdar_config_props,
116 .get_video_buffer = ff_null_get_video_buffer,
117 .filter_frame = filter_frame,
122 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
125 .type = AVMEDIA_TYPE_VIDEO,
130 AVFilter avfilter_vf_setdar = {
132 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
134 #if FF_API_OLD_FILTER_OPTS
138 .priv_size = sizeof(AspectContext),
139 .priv_class = &setdar_class,
141 .inputs = avfilter_vf_setdar_inputs,
143 .outputs = avfilter_vf_setdar_outputs,
146 #endif /* CONFIG_SETDAR_FILTER */
148 #if CONFIG_SETSAR_FILTER
151 static int setsar_config_props(AVFilterLink *inlink)
153 AspectContext *aspect = inlink->dst->priv;
155 inlink->sample_aspect_ratio = aspect->aspect;
160 static const AVOption setsar_options[] = {
161 #if FF_API_OLD_FILTER_OPTS
162 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
163 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
165 { "sar", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
166 { "ratio", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
167 { "r", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
168 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
172 AVFILTER_DEFINE_CLASS(setsar);
174 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
177 .type = AVMEDIA_TYPE_VIDEO,
178 .config_props = setsar_config_props,
179 .get_video_buffer = ff_null_get_video_buffer,
180 .filter_frame = filter_frame,
185 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
188 .type = AVMEDIA_TYPE_VIDEO,
193 AVFilter avfilter_vf_setsar = {
195 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
197 #if FF_API_OLD_FILTER_OPTS
201 .priv_size = sizeof(AspectContext),
202 .priv_class = &setsar_class,
204 .inputs = avfilter_vf_setsar_inputs,
206 .outputs = avfilter_vf_setsar_outputs,
209 #endif /* CONFIG_SETSAR_FILTER */