Log error message in case of invalid parsing.
[ffmpeg.git] / libavfilter / vf_aspect.c
1 /*
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 /**
22  * @file
23  * aspect ratio modification video filters
24  */
25
26 #include "avfilter.h"
27
28 typedef struct {
29     AVRational aspect;
30 } AspectContext;
31
32 static 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
38     if (args) {
39         if (sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
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             }
45             aspect->aspect = av_d2q(ratio, 100);
46         } else {
47             gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
48             if (gcd) {
49                 aspect->aspect.num /= gcd;
50                 aspect->aspect.den /= gcd;
51             }
52         }
53     }
54
55     if (aspect->aspect.den == 0)
56         aspect->aspect = (AVRational) {0, 1};
57
58     return 0;
59 }
60
61 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
62 {
63     AspectContext *aspect = link->dst->priv;
64
65     picref->video->pixel_aspect = aspect->aspect;
66     avfilter_start_frame(link->dst->outputs[0], picref);
67 }
68
69 #if CONFIG_ASPECT_FILTER
70 /* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
71 static 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
82 AVFilter 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",
91                                     .type             = AVMEDIA_TYPE_VIDEO,
92                                     .config_props     = frameaspect_config_props,
93                                     .get_video_buffer = avfilter_null_get_video_buffer,
94                                     .start_frame      = start_frame,
95                                     .end_frame        = avfilter_null_end_frame },
96                                   { .name = NULL}},
97
98     .outputs   = (AVFilterPad[]) {{ .name             = "default",
99                                     .type             = AVMEDIA_TYPE_VIDEO, },
100                                   { .name = NULL}},
101 };
102 #endif /* CONFIG_ASPECT_FILTER */
103
104 #if CONFIG_PIXELASPECT_FILTER
105 AVFilter 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",
114                                     .type             = AVMEDIA_TYPE_VIDEO,
115                                     .get_video_buffer = avfilter_null_get_video_buffer,
116                                     .start_frame      = start_frame,
117                                     .end_frame        = avfilter_null_end_frame },
118                                   { .name = NULL}},
119
120     .outputs   = (AVFilterPad[]) {{ .name             = "default",
121                                     .type             = AVMEDIA_TYPE_VIDEO, },
122                                   { .name = NULL}},
123 };
124 #endif /* CONFIG_PIXELASPECT_FILTER */
125