vf_yadif: factorize initializing the filtering callbacks
[FFMpeg-mirror/mplayer-patches.git] / libavfilter / vf_aspect.c
blobd7e851c928ba00ba40f06606d51a422442ddfd89
1 /*
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
21 /**
22 * @file
23 * aspect ratio modification video filters
26 #include "libavutil/common.h"
27 #include "libavutil/mathematics.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "video.h"
32 typedef struct {
33 AVRational aspect;
34 } AspectContext;
36 static av_cold int init(AVFilterContext *ctx, const char *args)
38 AspectContext *aspect = ctx->priv;
39 double ratio;
40 int64_t gcd;
41 char c = 0;
43 if (args) {
44 if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
45 if (sscanf(args, "%lf%c", &ratio, &c) == 1)
46 aspect->aspect = av_d2q(ratio, 100);
48 if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
49 av_log(ctx, AV_LOG_ERROR,
50 "Invalid string '%s' for aspect ratio.\n", args);
51 return AVERROR(EINVAL);
54 gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
55 if (gcd) {
56 aspect->aspect.num /= gcd;
57 aspect->aspect.den /= gcd;
61 if (aspect->aspect.den == 0)
62 aspect->aspect = (AVRational) {0, 1};
64 av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
65 return 0;
68 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
70 AspectContext *aspect = link->dst->priv;
72 frame->video->pixel_aspect = aspect->aspect;
73 return ff_filter_frame(link->dst->outputs[0], frame);
76 #if CONFIG_SETDAR_FILTER
77 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
78 static int setdar_config_props(AVFilterLink *inlink)
80 AspectContext *aspect = inlink->dst->priv;
81 AVRational dar = aspect->aspect;
83 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
84 aspect->aspect.num * inlink->h,
85 aspect->aspect.den * inlink->w, 100);
87 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
88 inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
90 inlink->sample_aspect_ratio = aspect->aspect;
92 return 0;
95 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
97 .name = "default",
98 .type = AVMEDIA_TYPE_VIDEO,
99 .config_props = setdar_config_props,
100 .get_video_buffer = ff_null_get_video_buffer,
101 .filter_frame = filter_frame,
103 { NULL }
106 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
108 .name = "default",
109 .type = AVMEDIA_TYPE_VIDEO,
111 { NULL }
114 AVFilter avfilter_vf_setdar = {
115 .name = "setdar",
116 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
118 .init = init,
120 .priv_size = sizeof(AspectContext),
122 .inputs = avfilter_vf_setdar_inputs,
124 .outputs = avfilter_vf_setdar_outputs,
126 #endif /* CONFIG_SETDAR_FILTER */
128 #if CONFIG_SETSAR_FILTER
129 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
130 static int setsar_config_props(AVFilterLink *inlink)
132 AspectContext *aspect = inlink->dst->priv;
134 inlink->sample_aspect_ratio = aspect->aspect;
136 return 0;
139 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
141 .name = "default",
142 .type = AVMEDIA_TYPE_VIDEO,
143 .config_props = setsar_config_props,
144 .get_video_buffer = ff_null_get_video_buffer,
145 .filter_frame = filter_frame,
147 { NULL }
150 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
152 .name = "default",
153 .type = AVMEDIA_TYPE_VIDEO,
155 { NULL }
158 AVFilter avfilter_vf_setsar = {
159 .name = "setsar",
160 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
162 .init = init,
164 .priv_size = sizeof(AspectContext),
166 .inputs = avfilter_vf_setsar_inputs,
168 .outputs = avfilter_vf_setsar_outputs,
170 #endif /* CONFIG_SETSAR_FILTER */