Add missing check on the existence of avcodec_opts[AVMEDIA_TYPE_X],
[libav.git] / libavfilter / vf_aspect.c
CommitLineData
3922deb5 1/*
3922deb5
BB
2 * Copyright (c) 2010 Bobby Bingham
3
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21/**
ba87f080 22 * @file
cef4b74b 23 * aspect ratio modification video filters
3922deb5
BB
24 */
25
26#include "avfilter.h"
27
28typedef struct {
29 AVRational aspect;
30} AspectContext;
31
32static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
33{
34 AspectContext *aspect = ctx->priv;
35 double ratio;
36 int64_t gcd;
37
bdb47f3a
SS
38 if (args) {
39 if (sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
9abf3441
SS
40 if (sscanf(args, "%lf", &ratio) < 1) {
41 av_log(ctx, AV_LOG_ERROR,
42 "Invalid string '%s' for aspect ratio.\n", args);
43 return AVERROR(EINVAL);
44 }
3922deb5
BB
45 aspect->aspect = av_d2q(ratio, 100);
46 } else {
47 gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
bdb47f3a 48 if (gcd) {
3922deb5
BB
49 aspect->aspect.num /= gcd;
50 aspect->aspect.den /= gcd;
51 }
52 }
53 }
54
bdb47f3a 55 if (aspect->aspect.den == 0)
3922deb5
BB
56 aspect->aspect = (AVRational) {0, 1};
57
58 return 0;
59}
60
ecc8dada 61static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
3922deb5
BB
62{
63 AspectContext *aspect = link->dst->priv;
64
cc80caff 65 picref->video->pixel_aspect = aspect->aspect;
3922deb5
BB
66 avfilter_start_frame(link->dst->outputs[0], picref);
67}
68
3922deb5
BB
69#if CONFIG_ASPECT_FILTER
70/* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
71static int frameaspect_config_props(AVFilterLink *inlink)
72{
73 AspectContext *aspect = inlink->dst->priv;
74
75 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
76 aspect->aspect.num * inlink->h,
77 aspect->aspect.den * inlink->w, 100);
78
79 return 0;
80}
81
82AVFilter avfilter_vf_aspect = {
83 .name = "aspect",
84 .description = NULL_IF_CONFIG_SMALL("Set the frame aspect ratio."),
85
86 .init = init,
87
88 .priv_size = sizeof(AspectContext),
89
90 .inputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 91 .type = AVMEDIA_TYPE_VIDEO,
3922deb5 92 .config_props = frameaspect_config_props,
d313e17a 93 .get_video_buffer = avfilter_null_get_video_buffer,
3922deb5 94 .start_frame = start_frame,
d313e17a 95 .end_frame = avfilter_null_end_frame },
3922deb5
BB
96 { .name = NULL}},
97
98 .outputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 99 .type = AVMEDIA_TYPE_VIDEO, },
3922deb5
BB
100 { .name = NULL}},
101};
102#endif /* CONFIG_ASPECT_FILTER */
103
104#if CONFIG_PIXELASPECT_FILTER
105AVFilter avfilter_vf_pixelaspect = {
106 .name = "pixelaspect",
107 .description = NULL_IF_CONFIG_SMALL("Set the pixel aspect ratio."),
108
109 .init = init,
110
111 .priv_size = sizeof(AspectContext),
112
113 .inputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 114 .type = AVMEDIA_TYPE_VIDEO,
d313e17a 115 .get_video_buffer = avfilter_null_get_video_buffer,
3922deb5 116 .start_frame = start_frame,
d313e17a 117 .end_frame = avfilter_null_end_frame },
3922deb5
BB
118 { .name = NULL}},
119
120 .outputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 121 .type = AVMEDIA_TYPE_VIDEO, },
3922deb5
BB
122 { .name = NULL}},
123};
124#endif /* CONFIG_PIXELASPECT_FILTER */
125