]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_nullsrc.c
lavfi: make filters less verbose.
[ffmpeg] / libavfilter / vsrc_nullsrc.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * null video source
22  */
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/parseutils.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31
32 static const char *const var_names[] = {
33     "E",
34     "PHI",
35     "PI",
36     "AVTB",   /* default timebase 1/AV_TIME_BASE */
37     NULL
38 };
39
40 enum var_name {
41     VAR_E,
42     VAR_PHI,
43     VAR_PI,
44     VAR_AVTB,
45     VAR_VARS_NB
46 };
47
48 typedef struct {
49     int w, h;
50     char tb_expr[256];
51     double var_values[VAR_VARS_NB];
52 } NullContext;
53
54 static int init(AVFilterContext *ctx, const char *args)
55 {
56     NullContext *priv = ctx->priv;
57
58     priv->w = 352;
59     priv->h = 288;
60     av_strlcpy(priv->tb_expr, "AVTB", sizeof(priv->tb_expr));
61
62     if (args)
63         sscanf(args, "%d:%d:%255[^:]", &priv->w, &priv->h, priv->tb_expr);
64
65     if (priv->w <= 0 || priv->h <= 0) {
66         av_log(ctx, AV_LOG_ERROR, "Non-positive size values are not acceptable.\n");
67         return AVERROR(EINVAL);
68     }
69
70     return 0;
71 }
72
73 static int config_props(AVFilterLink *outlink)
74 {
75     AVFilterContext *ctx = outlink->src;
76     NullContext *priv = ctx->priv;
77     AVRational tb;
78     int ret;
79     double res;
80
81     priv->var_values[VAR_E]    = M_E;
82     priv->var_values[VAR_PHI]  = M_PHI;
83     priv->var_values[VAR_PI]   = M_PI;
84     priv->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
85
86     if ((ret = av_expr_parse_and_eval(&res, priv->tb_expr, var_names, priv->var_values,
87                                       NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
88         av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", priv->tb_expr);
89         return ret;
90     }
91     tb = av_d2q(res, INT_MAX);
92     if (tb.num <= 0 || tb.den <= 0) {
93         av_log(ctx, AV_LOG_ERROR,
94                "Invalid non-positive value for the timebase %d/%d.\n",
95                tb.num, tb.den);
96         return AVERROR(EINVAL);
97     }
98
99     outlink->w = priv->w;
100     outlink->h = priv->h;
101     outlink->time_base = tb;
102
103     av_log(outlink->src, AV_LOG_VERBOSE, "w:%d h:%d tb:%d/%d\n", priv->w, priv->h,
104            tb.num, tb.den);
105
106     return 0;
107 }
108
109 static int request_frame(AVFilterLink *link)
110 {
111     return -1;
112 }
113
114 AVFilter avfilter_vsrc_nullsrc = {
115     .name        = "nullsrc",
116     .description = NULL_IF_CONFIG_SMALL("Null video source, never return images."),
117
118     .init       = init,
119     .priv_size = sizeof(NullContext),
120
121     .inputs    = (AVFilterPad[]) {{ .name = NULL}},
122
123     .outputs   = (AVFilterPad[]) {
124         {
125             .name            = "default",
126             .type            = AVMEDIA_TYPE_VIDEO,
127             .config_props    = config_props,
128             .request_frame   = request_frame,
129         },
130         { .name = NULL}
131     },
132 };