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/mathematics.h"
30 #include "libavutil/opt.h"
40 #if FF_API_OLD_FILTER_OPTS
41 float aspect_num, aspect_den;
45 #if FF_API_OLD_FILTER_OPTS
46 static av_cold int init(AVFilterContext *ctx)
48 AspectContext *s = ctx->priv;
50 if (s->aspect_num > 0 && s->aspect_den > 0) {
51 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
52 "dar=<number> or dar=num/den.\n");
53 s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
60 static int filter_frame(AVFilterLink *link, AVFrame *frame)
62 AspectContext *s = link->dst->priv;
64 frame->sample_aspect_ratio = s->sar;
65 return ff_filter_frame(link->dst->outputs[0], frame);
68 #define OFFSET(x) offsetof(AspectContext, x)
69 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
71 #if CONFIG_SETDAR_FILTER
72 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
73 static int setdar_config_props(AVFilterLink *inlink)
75 AspectContext *s = inlink->dst->priv;
78 if (s->dar.num && s->dar.den) {
79 av_reduce(&s->sar.num, &s->sar.den,
80 s->dar.num * inlink->h,
81 s->dar.den * inlink->w, 100);
82 inlink->sample_aspect_ratio = s->sar;
85 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
86 dar = (AVRational){ inlink->w, inlink->h };
89 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
90 inlink->w, inlink->h, dar.num, dar.den,
91 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
96 static const AVOption setdar_options[] = {
97 #if FF_API_OLD_FILTER_OPTS
98 { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
99 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
101 { "dar", "display aspect ratio", OFFSET(dar), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
105 static const AVClass setdar_class = {
106 .class_name = "setdar",
107 .item_name = av_default_item_name,
108 .option = setdar_options,
109 .version = LIBAVUTIL_VERSION_INT,
112 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
115 .type = AVMEDIA_TYPE_VIDEO,
116 .config_props = setdar_config_props,
117 .get_video_buffer = ff_null_get_video_buffer,
118 .filter_frame = filter_frame,
123 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
126 .type = AVMEDIA_TYPE_VIDEO,
131 AVFilter avfilter_vf_setdar = {
133 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
135 #if FF_API_OLD_FILTER_OPTS
139 .priv_size = sizeof(AspectContext),
140 .priv_class = &setdar_class,
142 .inputs = avfilter_vf_setdar_inputs,
144 .outputs = avfilter_vf_setdar_outputs,
146 #endif /* CONFIG_SETDAR_FILTER */
148 #if CONFIG_SETSAR_FILTER
149 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
150 static int setsar_config_props(AVFilterLink *inlink)
152 AspectContext *s = inlink->dst->priv;
154 inlink->sample_aspect_ratio = s->sar;
159 static const AVOption setsar_options[] = {
160 #if FF_API_OLD_FILTER_OPTS
161 { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
162 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
164 { "sar", "sample (pixel) aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
168 static const AVClass setsar_class = {
169 .class_name = "setsar",
170 .item_name = av_default_item_name,
171 .option = setsar_options,
172 .version = LIBAVUTIL_VERSION_INT,
175 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
178 .type = AVMEDIA_TYPE_VIDEO,
179 .config_props = setsar_config_props,
180 .get_video_buffer = ff_null_get_video_buffer,
181 .filter_frame = filter_frame,
186 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
189 .type = AVMEDIA_TYPE_VIDEO,
194 AVFilter avfilter_vf_setsar = {
196 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
198 #if FF_API_OLD_FILTER_OPTS
202 .priv_size = sizeof(AspectContext),
203 .priv_class = &setsar_class,
205 .inputs = avfilter_vf_setsar_inputs,
207 .outputs = avfilter_vf_setsar_outputs,
209 #endif /* CONFIG_SETSAR_FILTER */