vsrc_nullsrc.c 3.47 KB
Newer Older
1
/*
2
 * This file is part of Libav.
3
 *
4
 * Libav is free software; you can redistribute it and/or
5 6 7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
9
 * Libav is distributed in the hope that it will be useful,
10 11 12 13 14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 17 18 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
20
 * @file
21 22 23
 * null video source
 */

24 25
#include <stdio.h>

26 27
#include "libavutil/avstring.h"
#include "libavutil/eval.h"
28
#include "libavutil/internal.h"
29
#include "libavutil/mathematics.h"
30
#include "libavutil/parseutils.h"
31
#include "avfilter.h"
32
#include "formats.h"
33
#include "internal.h"
34

35
static const char *const var_names[] = {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    "E",
    "PHI",
    "PI",
    "AVTB",   /* default timebase 1/AV_TIME_BASE */
    NULL
};

enum var_name {
    VAR_E,
    VAR_PHI,
    VAR_PI,
    VAR_AVTB,
    VAR_VARS_NB
};

51 52
typedef struct {
    int w, h;
53 54
    char tb_expr[256];
    double var_values[VAR_VARS_NB];
55 56
} NullContext;

57
static int init(AVFilterContext *ctx, const char *args)
58 59 60 61 62
{
    NullContext *priv = ctx->priv;

    priv->w = 352;
    priv->h = 288;
63
    av_strlcpy(priv->tb_expr, "AVTB", sizeof(priv->tb_expr));
64 65

    if (args)
66
        sscanf(args, "%d:%d:%255[^:]", &priv->w, &priv->h, priv->tb_expr);
67 68 69

    if (priv->w <= 0 || priv->h <= 0) {
        av_log(ctx, AV_LOG_ERROR, "Non-positive size values are not acceptable.\n");
70
        return AVERROR(EINVAL);
71 72 73 74 75 76 77
    }

    return 0;
}

static int config_props(AVFilterLink *outlink)
{
78 79 80 81 82 83 84 85 86 87 88
    AVFilterContext *ctx = outlink->src;
    NullContext *priv = ctx->priv;
    AVRational tb;
    int ret;
    double res;

    priv->var_values[VAR_E]    = M_E;
    priv->var_values[VAR_PHI]  = M_PHI;
    priv->var_values[VAR_PI]   = M_PI;
    priv->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);

89
    if ((ret = av_expr_parse_and_eval(&res, priv->tb_expr, var_names, priv->var_values,
90 91 92 93 94 95 96 97 98 99 100
                                      NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
        av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", priv->tb_expr);
        return ret;
    }
    tb = av_d2q(res, INT_MAX);
    if (tb.num <= 0 || tb.den <= 0) {
        av_log(ctx, AV_LOG_ERROR,
               "Invalid non-positive value for the timebase %d/%d.\n",
               tb.num, tb.den);
        return AVERROR(EINVAL);
    }
101 102 103

    outlink->w = priv->w;
    outlink->h = priv->h;
104
    outlink->time_base = tb;
105

106
    av_log(outlink->src, AV_LOG_VERBOSE, "w:%d h:%d tb:%d/%d\n", priv->w, priv->h,
107
           tb.num, tb.den);
108 109 110 111 112 113 114 115 116 117 118

    return 0;
}

static int request_frame(AVFilterLink *link)
{
    return -1;
}

AVFilter avfilter_vsrc_nullsrc = {
    .name        = "nullsrc",
119
    .description = NULL_IF_CONFIG_SMALL("Null video source, never return images."),
120 121 122 123

    .init       = init,
    .priv_size = sizeof(NullContext),

124
    .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
125

126
    .outputs   = (const AVFilterPad[]) {
127 128
        {
            .name            = "default",
129
            .type            = AVMEDIA_TYPE_VIDEO,
130 131 132 133 134 135
            .config_props    = config_props,
            .request_frame   = request_frame,
        },
        { .name = NULL}
    },
};