]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_nullsrc.c
lavfi: make formats API private on next bump.
[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
31 static const char *const var_names[] = {
32     "E",
33     "PHI",
34     "PI",
35     "AVTB",   /* default timebase 1/AV_TIME_BASE */
36     NULL
37 };
38
39 enum var_name {
40     VAR_E,
41     VAR_PHI,
42     VAR_PI,
43     VAR_AVTB,
44     VAR_VARS_NB
45 };
46
47 typedef struct {
48     int w, h;
49     char tb_expr[256];
50     double var_values[VAR_VARS_NB];
51 } NullContext;
52
53 static int init(AVFilterContext *ctx, const char *args, void *opaque)
54 {
55     NullContext *priv = ctx->priv;
56
57     priv->w = 352;
58     priv->h = 288;
59     av_strlcpy(priv->tb_expr, "AVTB", sizeof(priv->tb_expr));
60
61     if (args)
62         sscanf(args, "%d:%d:%255[^:]", &priv->w, &priv->h, priv->tb_expr);
63
64     if (priv->w <= 0 || priv->h <= 0) {
65         av_log(ctx, AV_LOG_ERROR, "Non-positive size values are not acceptable.\n");
66         return AVERROR(EINVAL);
67     }
68
69     return 0;
70 }
71
72 static int config_props(AVFilterLink *outlink)
73 {
74     AVFilterContext *ctx = outlink->src;
75     NullContext *priv = ctx->priv;
76     AVRational tb;
77     int ret;
78     double res;
79
80     priv->var_values[VAR_E]    = M_E;
81     priv->var_values[VAR_PHI]  = M_PHI;
82     priv->var_values[VAR_PI]   = M_PI;
83     priv->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
84
85     if ((ret = av_expr_parse_and_eval(&res, priv->tb_expr, var_names, priv->var_values,
86                                       NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
87         av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", priv->tb_expr);
88         return ret;
89     }
90     tb = av_d2q(res, INT_MAX);
91     if (tb.num <= 0 || tb.den <= 0) {
92         av_log(ctx, AV_LOG_ERROR,
93                "Invalid non-positive value for the timebase %d/%d.\n",
94                tb.num, tb.den);
95         return AVERROR(EINVAL);
96     }
97
98     outlink->w = priv->w;
99     outlink->h = priv->h;
100     outlink->time_base = tb;
101
102     av_log(outlink->src, AV_LOG_INFO, "w:%d h:%d tb:%d/%d\n", priv->w, priv->h,
103            tb.num, tb.den);
104
105     return 0;
106 }
107
108 static int request_frame(AVFilterLink *link)
109 {
110     return -1;
111 }
112
113 AVFilter avfilter_vsrc_nullsrc = {
114     .name        = "nullsrc",
115     .description = NULL_IF_CONFIG_SMALL("Null video source, never return images."),
116
117     .init       = init,
118     .priv_size = sizeof(NullContext),
119
120     .inputs    = (AVFilterPad[]) {{ .name = NULL}},
121
122     .outputs   = (AVFilterPad[]) {
123         {
124             .name            = "default",
125             .type            = AVMEDIA_TYPE_VIDEO,
126             .config_props    = config_props,
127             .request_frame   = request_frame,
128         },
129         { .name = NULL}
130     },
131 };