Add informative log messages to the set aspect filters.
[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     av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
59     return 0;
60 }
61
62 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
63 {
64     AspectContext *aspect = link->dst->priv;
65
66     picref->video->pixel_aspect = aspect->aspect;
67     avfilter_start_frame(link->dst->outputs[0], picref);
68 }
69
70 #if CONFIG_SETDAR_FILTER
71 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
72 static int setdar_config_props(AVFilterLink *inlink)
73 {
74     AspectContext *aspect = inlink->dst->priv;
75     AVRational dar = aspect->aspect;
76
77     av_reduce(&aspect->aspect.num, &aspect->aspect.den,
78                aspect->aspect.num * inlink->h,
79                aspect->aspect.den * inlink->w, 100);
80
81     av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d par:%d/%d\n",
82            inlink->h, inlink->w, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
83     return 0;
84 }
85
86 AVFilter avfilter_vf_setdar = {
87     .name      = "setdar",
88     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
89
90     .init      = init,
91
92     .priv_size = sizeof(AspectContext),
93
94     .inputs    = (AVFilterPad[]) {{ .name             = "default",
95                                     .type             = AVMEDIA_TYPE_VIDEO,
96                                     .config_props     = setdar_config_props,
97                                     .get_video_buffer = avfilter_null_get_video_buffer,
98                                     .start_frame      = start_frame,
99                                     .end_frame        = avfilter_null_end_frame },
100                                   { .name = NULL}},
101
102     .outputs   = (AVFilterPad[]) {{ .name             = "default",
103                                     .type             = AVMEDIA_TYPE_VIDEO, },
104                                   { .name = NULL}},
105 };
106 #endif /* CONFIG_SETDAR_FILTER */
107
108 #if CONFIG_SETSAR_FILTER
109 AVFilter avfilter_vf_setsar = {
110     .name      = "setsar",
111     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
112
113     .init      = init,
114
115     .priv_size = sizeof(AspectContext),
116
117     .inputs    = (AVFilterPad[]) {{ .name             = "default",
118                                     .type             = AVMEDIA_TYPE_VIDEO,
119                                     .get_video_buffer = avfilter_null_get_video_buffer,
120                                     .start_frame      = start_frame,
121                                     .end_frame        = avfilter_null_end_frame },
122                                   { .name = NULL}},
123
124     .outputs   = (AVFilterPad[]) {{ .name             = "default",
125                                     .type             = AVMEDIA_TYPE_VIDEO, },
126                                   { .name = NULL}},
127 };
128 #endif /* CONFIG_SETSAR_FILTER */
129