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
32 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
34 AspectContext *aspect = ctx->priv;
40 if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
41 if (sscanf(args, "%lf%c", &ratio, &c) == 1)
42 aspect->aspect = av_d2q(ratio, 100);
44 if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
45 av_log(ctx, AV_LOG_ERROR,
46 "Invalid string '%s' for aspect ratio.\n", args);
47 return AVERROR(EINVAL);
50 gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
52 aspect->aspect.num /= gcd;
53 aspect->aspect.den /= gcd;
57 if (aspect->aspect.den == 0)
58 aspect->aspect = (AVRational) {0, 1};
60 av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
64 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
66 AspectContext *aspect = link->dst->priv;
68 picref->video->pixel_aspect = aspect->aspect;
69 avfilter_start_frame(link->dst->outputs[0], picref);
72 #if CONFIG_SETDAR_FILTER
73 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
74 static int setdar_config_props(AVFilterLink *inlink)
76 AspectContext *aspect = inlink->dst->priv;
77 AVRational dar = aspect->aspect;
79 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
80 aspect->aspect.num * inlink->h,
81 aspect->aspect.den * inlink->w, 100);
83 av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d par:%d/%d\n",
84 inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
86 inlink->sample_aspect_ratio = aspect->aspect;
91 AVFilter avfilter_vf_setdar = {
93 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
97 .priv_size = sizeof(AspectContext),
99 .inputs = (AVFilterPad[]) {{ .name = "default",
100 .type = AVMEDIA_TYPE_VIDEO,
101 .config_props = setdar_config_props,
102 .get_video_buffer = avfilter_null_get_video_buffer,
103 .start_frame = start_frame,
104 .end_frame = avfilter_null_end_frame },
107 .outputs = (AVFilterPad[]) {{ .name = "default",
108 .type = AVMEDIA_TYPE_VIDEO, },
111 #endif /* CONFIG_SETDAR_FILTER */
113 #if CONFIG_SETSAR_FILTER
114 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
115 static int setsar_config_props(AVFilterLink *inlink)
117 AspectContext *aspect = inlink->dst->priv;
119 inlink->sample_aspect_ratio = aspect->aspect;
124 AVFilter avfilter_vf_setsar = {
126 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
130 .priv_size = sizeof(AspectContext),
132 .inputs = (AVFilterPad[]) {{ .name = "default",
133 .type = AVMEDIA_TYPE_VIDEO,
134 .config_props = setsar_config_props,
135 .get_video_buffer = avfilter_null_get_video_buffer,
136 .start_frame = start_frame,
137 .end_frame = avfilter_null_end_frame },
140 .outputs = (AVFilterPad[]) {{ .name = "default",
141 .type = AVMEDIA_TYPE_VIDEO, },
144 #endif /* CONFIG_SETSAR_FILTER */