vf_frei0r.c 16.7 KB
Newer Older
Stefano Sabatini's avatar
Stefano Sabatini committed
1
/*
2
 * Copyright (c) 2010 Stefano Sabatini
3
 * This file is part of Libav.
Stefano Sabatini's avatar
Stefano Sabatini committed
4
 *
5
 * Libav is free software; you can redistribute it and/or
Stefano Sabatini's avatar
Stefano Sabatini committed
6 7 8 9
 * 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.
 *
10
 * Libav is distributed in the hope that it will be useful,
Stefano Sabatini's avatar
Stefano Sabatini committed
11 12 13 14 15
 * 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
16
 * License along with Libav; if not, write to the Free Software
Stefano Sabatini's avatar
Stefano Sabatini committed
17 18 19 20 21 22 23 24 25 26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * frei0r wrapper
 */

#include <dlfcn.h>
#include <frei0r.h>
27 28 29 30
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
31
#include "libavutil/avstring.h"
32
#include "libavutil/imgutils.h"
33
#include "libavutil/internal.h"
34
#include "libavutil/mathematics.h"
35
#include "libavutil/mem.h"
36
#include "libavutil/opt.h"
37
#include "libavutil/parseutils.h"
Stefano Sabatini's avatar
Stefano Sabatini committed
38
#include "avfilter.h"
39
#include "formats.h"
40
#include "internal.h"
41
#include "video.h"
Stefano Sabatini's avatar
Stefano Sabatini committed
42 43 44 45 46 47 48 49 50 51 52 53 54

typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
typedef void (*f0r_destruct_f)(f0r_instance_t instance);
typedef void (*f0r_deinit_f)(void);
typedef int (*f0r_init_f)(void);
typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
typedef void (*f0r_update2_f)(f0r_instance_t instance, double time, const uint32_t *inframe1, const uint32_t *inframe2, const uint32_t *inframe3, uint32_t *outframe);
typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);

typedef struct Frei0rContext {
55
    const AVClass *class;
Stefano Sabatini's avatar
Stefano Sabatini committed
56 57 58 59 60 61 62 63 64 65 66
    f0r_update_f update;
    void *dl_handle;            /* dynamic library handle   */
    f0r_instance_t instance;
    f0r_plugin_info_t plugin_info;

    f0r_get_param_info_f  get_param_info;
    f0r_get_param_value_f get_param_value;
    f0r_set_param_value_f set_param_value;
    f0r_construct_f       construct;
    f0r_destruct_f        destruct;
    f0r_deinit_f          deinit;
67 68 69 70 71

    char *dl_name;
    char *params;
    char *size;
    char *framerate;
Stefano Sabatini's avatar
Stefano Sabatini committed
72 73 74 75 76

    /* only used by the source */
    int w, h;
    AVRational time_base;
    uint64_t pts;
Stefano Sabatini's avatar
Stefano Sabatini committed
77 78 79 80
} Frei0rContext;

static void *load_sym(AVFilterContext *ctx, const char *sym_name)
{
81 82
    Frei0rContext *s = ctx->priv;
    void *sym = dlsym(s->dl_handle, sym_name);
Stefano Sabatini's avatar
Stefano Sabatini committed
83
    if (!sym)
84
        av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name);
Stefano Sabatini's avatar
Stefano Sabatini committed
85 86 87 88 89
    return sym;
}

static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
{
90
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    union {
        double d;
        f0r_param_color_t col;
        f0r_param_position_t pos;
    } val;
    char *tail;
    uint8_t rgba[4];

    switch (info.type) {
    case F0R_PARAM_BOOL:
        if      (!strcmp(param, "y")) val.d = 1.0;
        else if (!strcmp(param, "n")) val.d = 0.0;
        else goto fail;
        break;

    case F0R_PARAM_DOUBLE:
        val.d = strtod(param, &tail);
        if (*tail || val.d == HUGE_VAL)
            goto fail;
        break;

    case F0R_PARAM_COLOR:
        if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
114
            if (av_parse_color(rgba, param, -1, ctx) < 0)
Stefano Sabatini's avatar
Stefano Sabatini committed
115 116 117 118 119 120 121 122 123 124 125 126 127
                goto fail;
            val.col.r = rgba[0] / 255.0;
            val.col.g = rgba[1] / 255.0;
            val.col.b = rgba[2] / 255.0;
        }
        break;

    case F0R_PARAM_POSITION:
        if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
            goto fail;
        break;
    }

128
    s->set_param_value(s->instance, &val, index);
Stefano Sabatini's avatar
Stefano Sabatini committed
129 130 131
    return 0;

fail:
132
    av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
Stefano Sabatini's avatar
Stefano Sabatini committed
133 134 135 136 137 138
           param, info.name);
    return AVERROR(EINVAL);
}

static int set_params(AVFilterContext *ctx, const char *params)
{
139
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
140 141
    int i;

142
    for (i = 0; i < s->plugin_info.num_params; i++) {
Stefano Sabatini's avatar
Stefano Sabatini committed
143 144 145 146
        f0r_param_info_t info;
        char *param;
        int ret;

147
        s->get_param_info(&info, i);
Stefano Sabatini's avatar
Stefano Sabatini committed
148 149

        if (*params) {
150
            if (!(param = av_get_token(&params, "|")))
Stefano Sabatini's avatar
Stefano Sabatini committed
151 152 153 154 155 156 157 158
                return AVERROR(ENOMEM);
            params++;               /* skip ':' */
            ret = set_param(ctx, info, i, param);
            av_free(param);
            if (ret < 0)
                return ret;
        }

159
        av_log(ctx, AV_LOG_VERBOSE,
Stefano Sabatini's avatar
Stefano Sabatini committed
160 161 162 163 164 165 166 167 168 169
               "idx:%d name:'%s' type:%s explanation:'%s' ",
               i, info.name,
               info.type == F0R_PARAM_BOOL     ? "bool"     :
               info.type == F0R_PARAM_DOUBLE   ? "double"   :
               info.type == F0R_PARAM_COLOR    ? "color"    :
               info.type == F0R_PARAM_POSITION ? "position" :
               info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
               info.explanation);

#ifdef DEBUG
170
        av_log(ctx, AV_LOG_DEBUG, "value:");
Stefano Sabatini's avatar
Stefano Sabatini committed
171 172 173 174 175 176 177 178 179
        switch (info.type) {
            void *v;
            double d;
            char s[128];
            f0r_param_color_t col;
            f0r_param_position_t pos;

        case F0R_PARAM_BOOL:
            v = &d;
180
            s->get_param_value(s->instance, v, i);
181
            av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
Stefano Sabatini's avatar
Stefano Sabatini committed
182 183 184
            break;
        case F0R_PARAM_DOUBLE:
            v = &d;
185
            s->get_param_value(s->instance, v, i);
186
            av_log(ctx, AV_LOG_DEBUG, "%f", d);
Stefano Sabatini's avatar
Stefano Sabatini committed
187 188 189
            break;
        case F0R_PARAM_COLOR:
            v = &col;
190
            s->get_param_value(s->instance, v, i);
191
            av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
Stefano Sabatini's avatar
Stefano Sabatini committed
192 193 194
            break;
        case F0R_PARAM_POSITION:
            v = &pos;
195
            s->get_param_value(s->instance, v, i);
196
            av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
Stefano Sabatini's avatar
Stefano Sabatini committed
197 198 199
            break;
        default: /* F0R_PARAM_STRING */
            v = s;
200
            s->get_param_value(s->instance, v, i);
201
            av_log(ctx, AV_LOG_DEBUG, "'%s'", s);
Stefano Sabatini's avatar
Stefano Sabatini committed
202 203 204
            break;
        }
#endif
205
        av_log(ctx, AV_LOG_VERBOSE, ".\n");
Stefano Sabatini's avatar
Stefano Sabatini committed
206 207 208 209 210 211 212 213 214
    }

    return 0;
}

static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
{
    char path[1024];

215
    snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
216
    av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
Stefano Sabatini's avatar
Stefano Sabatini committed
217 218 219
    return dlopen(path, RTLD_NOW|RTLD_LOCAL);
}

Stefano Sabatini's avatar
Stefano Sabatini committed
220 221
static av_cold int frei0r_init(AVFilterContext *ctx,
                               const char *dl_name, int type)
Stefano Sabatini's avatar
Stefano Sabatini committed
222
{
223
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
224 225 226
    f0r_init_f            f0r_init;
    f0r_get_plugin_info_f f0r_get_plugin_info;
    f0r_plugin_info_t *pi;
Stefano Sabatini's avatar
Stefano Sabatini committed
227
    char *path;
Stefano Sabatini's avatar
Stefano Sabatini committed
228

229 230 231 232 233
    if (!dl_name) {
        av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
        return AVERROR(EINVAL);
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
234
    /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
235 236 237 238 239 240 241 242 243 244 245 246
    if (path = getenv("FREI0R_PATH")) {
        while(*path) {
            char *ptr = av_get_token((const char **)&path, ":");
            if (!ptr)
                return AVERROR(ENOMEM);
            s->dl_handle = load_path(ctx, ptr, dl_name);
            av_freep(&ptr);
            if (s->dl_handle)
                break;              /* found */
            if (*path)
                path++              /* skip ':' */
        }
Stefano Sabatini's avatar
Stefano Sabatini committed
247
    }
248
    if (!s->dl_handle && (path = getenv("HOME"))) {
Stefano Sabatini's avatar
Stefano Sabatini committed
249 250
        char prefix[1024];
        snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
251
        s->dl_handle = load_path(ctx, prefix, dl_name);
Stefano Sabatini's avatar
Stefano Sabatini committed
252
    }
253 254 255 256 257
    if (!s->dl_handle)
        s->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
    if (!s->dl_handle)
        s->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
    if (!s->dl_handle) {
258
        av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
Stefano Sabatini's avatar
Stefano Sabatini committed
259 260 261 262 263
        return AVERROR(EINVAL);
    }

    if (!(f0r_init                = load_sym(ctx, "f0r_init"           )) ||
        !(f0r_get_plugin_info     = load_sym(ctx, "f0r_get_plugin_info")) ||
264 265 266 267 268 269 270
        !(s->get_param_info  = load_sym(ctx, "f0r_get_param_info" )) ||
        !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
        !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
        !(s->update          = load_sym(ctx, "f0r_update"         )) ||
        !(s->construct       = load_sym(ctx, "f0r_construct"      )) ||
        !(s->destruct        = load_sym(ctx, "f0r_destruct"       )) ||
        !(s->deinit          = load_sym(ctx, "f0r_deinit"         )))
Stefano Sabatini's avatar
Stefano Sabatini committed
271 272 273
        return AVERROR(EINVAL);

    if (f0r_init() < 0) {
274
        av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
Stefano Sabatini's avatar
Stefano Sabatini committed
275 276 277
        return AVERROR(EINVAL);
    }

278 279
    f0r_get_plugin_info(&s->plugin_info);
    pi = &s->plugin_info;
Stefano Sabatini's avatar
Stefano Sabatini committed
280
    if (pi->plugin_type != type) {
Stefano Sabatini's avatar
Stefano Sabatini committed
281
        av_log(ctx, AV_LOG_ERROR,
282
               "Invalid type '%s' for this plugin\n",
Stefano Sabatini's avatar
Stefano Sabatini committed
283
               pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
Stefano Sabatini's avatar
Stefano Sabatini committed
284 285 286 287 288 289
               pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
               pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
               pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
        return AVERROR(EINVAL);
    }

290
    av_log(ctx, AV_LOG_VERBOSE,
Stefano Sabatini's avatar
Stefano Sabatini committed
291 292 293 294 295 296 297 298 299 300 301
           "name:%s author:'%s' explanation:'%s' color_model:%s "
           "frei0r_version:%d version:%d.%d num_params:%d\n",
           pi->name, pi->author, pi->explanation,
           pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
           pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
           pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
           pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);

    return 0;
}

302
static av_cold int filter_init(AVFilterContext *ctx)
Stefano Sabatini's avatar
Stefano Sabatini committed
303
{
304
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
305

306
    return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
Stefano Sabatini's avatar
Stefano Sabatini committed
307 308
}

Stefano Sabatini's avatar
Stefano Sabatini committed
309 310
static av_cold void uninit(AVFilterContext *ctx)
{
311 312 313 314 315 316 317 318
    Frei0rContext *s = ctx->priv;

    if (s->destruct && s->instance)
        s->destruct(s->instance);
    if (s->deinit)
        s->deinit();
    if (s->dl_handle)
        dlclose(s->dl_handle);
Stefano Sabatini's avatar
Stefano Sabatini committed
319 320 321 322 323
}

static int config_input_props(AVFilterLink *inlink)
{
    AVFilterContext *ctx = inlink->dst;
324
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
325

326 327
    if (s->destruct && s->instance)
        s->destruct(s->instance);
328
    if (!(s->instance = s->construct(inlink->w, inlink->h))) {
329
        av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
Stefano Sabatini's avatar
Stefano Sabatini committed
330 331 332
        return AVERROR(EINVAL);
    }

333
    return set_params(ctx, s->params);
Stefano Sabatini's avatar
Stefano Sabatini committed
334 335 336 337
}

static int query_formats(AVFilterContext *ctx)
{
338
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
339 340
    AVFilterFormats *formats = NULL;

341
    if        (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
342
        ff_add_format(&formats, AV_PIX_FMT_BGRA);
343
    } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
344
        ff_add_format(&formats, AV_PIX_FMT_RGBA);
Stefano Sabatini's avatar
Stefano Sabatini committed
345
    } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
346 347
        static const enum AVPixelFormat pix_fmts[] = {
            AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
Stefano Sabatini's avatar
Stefano Sabatini committed
348
        };
349
        formats = ff_make_format_list(pix_fmts);
Stefano Sabatini's avatar
Stefano Sabatini committed
350 351 352 353 354
    }

    if (!formats)
        return AVERROR(ENOMEM);

355
    ff_set_common_formats(ctx, formats);
Stefano Sabatini's avatar
Stefano Sabatini committed
356 357 358
    return 0;
}

Anton Khirnov's avatar
Anton Khirnov committed
359
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Stefano Sabatini's avatar
Stefano Sabatini committed
360
{
361
    Frei0rContext *s = inlink->dst->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
362
    AVFilterLink *outlink = inlink->dst->outputs[0];
Anton Khirnov's avatar
Anton Khirnov committed
363
    AVFrame *out;
364

Anton Khirnov's avatar
Anton Khirnov committed
365
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
366
    if (!out) {
Anton Khirnov's avatar
Anton Khirnov committed
367
        av_frame_free(&in);
368 369
        return AVERROR(ENOMEM);
    }
Anton Khirnov's avatar
Anton Khirnov committed
370
    av_frame_copy_props(out, in);
371

372
    s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
373 374 375
                   (const uint32_t *)in->data[0],
                   (uint32_t *)out->data[0]);

Anton Khirnov's avatar
Anton Khirnov committed
376
    av_frame_free(&in);
377 378

    return ff_filter_frame(outlink, out);
Stefano Sabatini's avatar
Stefano Sabatini committed
379 380
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
#define OFFSET(x) offsetof(Frei0rContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
static const AVOption filter_options[] = {
    { "filter_name",   NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
    { "filter_params", NULL, OFFSET(params),  AV_OPT_TYPE_STRING, .flags = FLAGS },
    { NULL },
};

static const AVClass filter_class = {
    .class_name = "frei0r",
    .item_name  = av_default_item_name,
    .option     = filter_options,
    .version    = LIBAVUTIL_VERSION_INT,
};

396 397 398 399 400
static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_VIDEO,
        .config_props = config_input_props,
401
        .filter_frame = filter_frame,
402 403 404 405 406 407 408 409 410 411 412 413
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

414
AVFilter ff_vf_frei0r = {
Stefano Sabatini's avatar
Stefano Sabatini committed
415 416 417 418
    .name      = "frei0r",
    .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),

    .query_formats = query_formats,
Stefano Sabatini's avatar
Stefano Sabatini committed
419
    .init = filter_init,
Stefano Sabatini's avatar
Stefano Sabatini committed
420 421 422
    .uninit = uninit,

    .priv_size = sizeof(Frei0rContext),
423
    .priv_class = &filter_class,
Stefano Sabatini's avatar
Stefano Sabatini committed
424

425 426 427
    .inputs    = avfilter_vf_frei0r_inputs,

    .outputs   = avfilter_vf_frei0r_outputs,
Stefano Sabatini's avatar
Stefano Sabatini committed
428
};
Stefano Sabatini's avatar
Stefano Sabatini committed
429

430
static av_cold int source_init(AVFilterContext *ctx)
Stefano Sabatini's avatar
Stefano Sabatini committed
431
{
432
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
433 434
    AVRational frame_rate_q;

435
    if (av_parse_video_size(&s->w, &s->h, s->size) < 0) {
436
        av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'.\n", s->size);
Stefano Sabatini's avatar
Stefano Sabatini committed
437 438 439
        return AVERROR(EINVAL);
    }

440
    if (av_parse_video_rate(&frame_rate_q, s->framerate) < 0 ||
Stefano Sabatini's avatar
Stefano Sabatini committed
441
        frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
442
        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'.\n", s->framerate);
Stefano Sabatini's avatar
Stefano Sabatini committed
443 444
        return AVERROR(EINVAL);
    }
445 446
    s->time_base.num = frame_rate_q.den;
    s->time_base.den = frame_rate_q.num;
Stefano Sabatini's avatar
Stefano Sabatini committed
447

448
    return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
Stefano Sabatini's avatar
Stefano Sabatini committed
449 450 451 452 453
}

static int source_config_props(AVFilterLink *outlink)
{
    AVFilterContext *ctx = outlink->src;
454
    Frei0rContext *s = ctx->priv;
Stefano Sabatini's avatar
Stefano Sabatini committed
455

456
    if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
Stefano Sabatini's avatar
Stefano Sabatini committed
457
        return AVERROR(EINVAL);
458 459 460
    outlink->w = s->w;
    outlink->h = s->h;
    outlink->time_base = s->time_base;
Stefano Sabatini's avatar
Stefano Sabatini committed
461

462 463
    if (s->destruct && s->instance)
        s->destruct(s->instance);
464
    if (!(s->instance = s->construct(outlink->w, outlink->h))) {
465
        av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
Stefano Sabatini's avatar
Stefano Sabatini committed
466 467
        return AVERROR(EINVAL);
    }
468 469 470 471
    if (!s->params) {
        av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
        return AVERROR(EINVAL);
    }
Stefano Sabatini's avatar
Stefano Sabatini committed
472

473
    return set_params(ctx, s->params);
Stefano Sabatini's avatar
Stefano Sabatini committed
474 475 476 477
}

static int source_request_frame(AVFilterLink *outlink)
{
478
    Frei0rContext *s = outlink->src->priv;
Anton Khirnov's avatar
Anton Khirnov committed
479
    AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
480

Anton Khirnov's avatar
Anton Khirnov committed
481
    if (!frame)
482 483
        return AVERROR(ENOMEM);

Anton Khirnov's avatar
Anton Khirnov committed
484
    frame->sample_aspect_ratio = (AVRational) {1, 1};
485
    frame->pts = s->pts++;
Stefano Sabatini's avatar
Stefano Sabatini committed
486

487
    s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
Anton Khirnov's avatar
Anton Khirnov committed
488
                   NULL, (uint32_t *)frame->data[0]);
Stefano Sabatini's avatar
Stefano Sabatini committed
489

Anton Khirnov's avatar
Anton Khirnov committed
490
    return ff_filter_frame(outlink, frame);
Stefano Sabatini's avatar
Stefano Sabatini committed
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
static const AVOption src_options[] = {
    { "size",          "Dimensions of the generated video.", OFFSET(size),      AV_OPT_TYPE_STRING, { .str = "" },   .flags = FLAGS },
    { "framerate",     NULL,                                 OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
    { "filter_name",   NULL,                                 OFFSET(dl_name),   AV_OPT_TYPE_STRING,                  .flags = FLAGS },
    { "filter_params", NULL,                                 OFFSET(params),    AV_OPT_TYPE_STRING,                  .flags = FLAGS },
    { NULL },
};

static const AVClass src_class = {
    .class_name = "frei0r_src",
    .item_name  = av_default_item_name,
    .option     = src_options,
    .version    = LIBAVUTIL_VERSION_INT,
};

508 509 510 511 512 513 514 515 516 517
static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_VIDEO,
        .request_frame = source_request_frame,
        .config_props  = source_config_props
    },
    { NULL }
};

518
AVFilter ff_vsrc_frei0r_src = {
Stefano Sabatini's avatar
Stefano Sabatini committed
519 520 521 522
    .name        = "frei0r_src",
    .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),

    .priv_size = sizeof(Frei0rContext),
523
    .priv_class = &src_class,
Stefano Sabatini's avatar
Stefano Sabatini committed
524 525 526 527 528
    .init      = source_init,
    .uninit    = uninit,

    .query_formats = query_formats,

529
    .inputs    = NULL,
Stefano Sabatini's avatar
Stefano Sabatini committed
530

531
    .outputs   = avfilter_vsrc_frei0r_src_outputs,
Stefano Sabatini's avatar
Stefano Sabatini committed
532
};