Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_frei0r.c @ master

History | View | Annotate | Download (15.8 KB)

1
/*
2
 * Copyright (c) 2010 Stefano Sabatini
3
 * This file is part of FFmpeg.
4
 *
5
 * FFmpeg is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * FFmpeg is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with FFmpeg; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
 */
19

    
20
/**
21
 * @file
22
 * frei0r wrapper
23
 */
24

    
25
/* #define DEBUG */
26

    
27
#include <dlfcn.h>
28
#include <frei0r.h>
29
#include "libavutil/avstring.h"
30
#include "libavutil/imgutils.h"
31
#include "libavutil/parseutils.h"
32
#include "avfilter.h"
33

    
34
typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
35
typedef void (*f0r_destruct_f)(f0r_instance_t instance);
36
typedef void (*f0r_deinit_f)(void);
37
typedef int (*f0r_init_f)(void);
38
typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
39
typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
40
typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
41
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);
42
typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
43
typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
44

    
45
typedef struct Frei0rContext {
46
    f0r_update_f update;
47
    void *dl_handle;            /* dynamic library handle   */
48
    f0r_instance_t instance;
49
    f0r_plugin_info_t plugin_info;
50

    
51
    f0r_get_param_info_f  get_param_info;
52
    f0r_get_param_value_f get_param_value;
53
    f0r_set_param_value_f set_param_value;
54
    f0r_construct_f       construct;
55
    f0r_destruct_f        destruct;
56
    f0r_deinit_f          deinit;
57
    char params[256];
58

    
59
    /* only used by the source */
60
    int w, h;
61
    AVRational time_base;
62
    uint64_t pts;
63
} Frei0rContext;
64

    
65
static void *load_sym(AVFilterContext *ctx, const char *sym_name)
66
{
67
    Frei0rContext *frei0r = ctx->priv;
68
    void *sym = dlsym(frei0r->dl_handle, sym_name);
69
    if (!sym)
70
        av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
71
    return sym;
72
}
73

    
74
static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
75
{
76
    Frei0rContext *frei0r = ctx->priv;
77
    union {
78
        double d;
79
        f0r_param_color_t col;
80
        f0r_param_position_t pos;
81
    } val;
82
    char *tail;
83
    uint8_t rgba[4];
84

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

    
92
    case F0R_PARAM_DOUBLE:
93
        val.d = strtod(param, &tail);
94
        if (*tail || val.d == HUGE_VAL)
95
            goto fail;
96
        break;
97

    
98
    case F0R_PARAM_COLOR:
99
        if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
100
            if (av_parse_color(rgba, param, -1, ctx) < 0)
101
                goto fail;
102
            val.col.r = rgba[0] / 255.0;
103
            val.col.g = rgba[1] / 255.0;
104
            val.col.b = rgba[2] / 255.0;
105
        }
106
        break;
107

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

    
114
    frei0r->set_param_value(frei0r->instance, &val, index);
115
    return 0;
116

    
117
fail:
118
    av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
119
           param, info.name);
120
    return AVERROR(EINVAL);
121
}
122

    
123
static int set_params(AVFilterContext *ctx, const char *params)
124
{
125
    Frei0rContext *frei0r = ctx->priv;
126
    int i;
127

    
128
    for (i = 0; i < frei0r->plugin_info.num_params; i++) {
129
        f0r_param_info_t info;
130
        char *param;
131
        int ret;
132

    
133
        frei0r->get_param_info(&info, i);
134

    
135
        if (*params) {
136
            if (!(param = av_get_token(&params, ":")))
137
                return AVERROR(ENOMEM);
138
            params++;               /* skip ':' */
139
            ret = set_param(ctx, info, i, param);
140
            av_free(param);
141
            if (ret < 0)
142
                return ret;
143
        }
144

    
145
        av_log(ctx, AV_LOG_INFO,
146
               "idx:%d name:'%s' type:%s explanation:'%s' ",
147
               i, info.name,
148
               info.type == F0R_PARAM_BOOL     ? "bool"     :
149
               info.type == F0R_PARAM_DOUBLE   ? "double"   :
150
               info.type == F0R_PARAM_COLOR    ? "color"    :
151
               info.type == F0R_PARAM_POSITION ? "position" :
152
               info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
153
               info.explanation);
154

    
155
#ifdef DEBUG
156
        av_log(ctx, AV_LOG_INFO, "value:");
157
        switch (info.type) {
158
            void *v;
159
            double d;
160
            char s[128];
161
            f0r_param_color_t col;
162
            f0r_param_position_t pos;
163

    
164
        case F0R_PARAM_BOOL:
165
            v = &d;
166
            frei0r->get_param_value(frei0r->instance, v, i);
167
            av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
168
            break;
169
        case F0R_PARAM_DOUBLE:
170
            v = &d;
171
            frei0r->get_param_value(frei0r->instance, v, i);
172
            av_log(ctx, AV_LOG_INFO, "%f", d);
173
            break;
174
        case F0R_PARAM_COLOR:
175
            v = &col;
176
            frei0r->get_param_value(frei0r->instance, v, i);
177
            av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
178
            break;
179
        case F0R_PARAM_POSITION:
180
            v = &pos;
181
            frei0r->get_param_value(frei0r->instance, v, i);
182
            av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
183
            break;
184
        default: /* F0R_PARAM_STRING */
185
            v = s;
186
            frei0r->get_param_value(frei0r->instance, v, i);
187
            av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
188
            break;
189
        }
190
#endif
191
        av_log(ctx, AV_LOG_INFO, "\n");
192
    }
193

    
194
    return 0;
195
}
196

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

    
201
    snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
202
    av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
203
    return dlopen(path, RTLD_NOW|RTLD_LOCAL);
204
}
205

    
206
static av_cold int frei0r_init(AVFilterContext *ctx,
207
                               const char *dl_name, int type)
208
{
209
    Frei0rContext *frei0r = ctx->priv;
210
    f0r_init_f            f0r_init;
211
    f0r_get_plugin_info_f f0r_get_plugin_info;
212
    f0r_plugin_info_t *pi;
213
    char *path;
214

    
215
    /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
216
    if ((path = av_strdup(getenv("FREI0R_PATH")))) {
217
        char *p, *ptr = NULL;
218
        for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
219
            if (frei0r->dl_handle = load_path(ctx, p, dl_name))
220
                break;
221
        av_free(path);
222
    }
223
    if (!frei0r->dl_handle && (path = getenv("HOME"))) {
224
        char prefix[1024];
225
        snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
226
        frei0r->dl_handle = load_path(ctx, prefix, dl_name);
227
    }
228
    if (!frei0r->dl_handle)
229
        frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
230
    if (!frei0r->dl_handle)
231
        frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
232
    if (!frei0r->dl_handle) {
233
        av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
234
        return AVERROR(EINVAL);
235
    }
236

    
237
    if (!(f0r_init                = load_sym(ctx, "f0r_init"           )) ||
238
        !(f0r_get_plugin_info     = load_sym(ctx, "f0r_get_plugin_info")) ||
239
        !(frei0r->get_param_info  = load_sym(ctx, "f0r_get_param_info" )) ||
240
        !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
241
        !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
242
        !(frei0r->update          = load_sym(ctx, "f0r_update"         )) ||
243
        !(frei0r->construct       = load_sym(ctx, "f0r_construct"      )) ||
244
        !(frei0r->destruct        = load_sym(ctx, "f0r_destruct"       )) ||
245
        !(frei0r->deinit          = load_sym(ctx, "f0r_deinit"         )))
246
        return AVERROR(EINVAL);
247

    
248
    if (f0r_init() < 0) {
249
        av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
250
        return AVERROR(EINVAL);
251
    }
252

    
253
    f0r_get_plugin_info(&frei0r->plugin_info);
254
    pi = &frei0r->plugin_info;
255
    if (pi->plugin_type != type) {
256
        av_log(ctx, AV_LOG_ERROR,
257
               "Invalid type '%s' for the plugin\n",
258
               pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
259
               pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
260
               pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
261
               pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
262
        return AVERROR(EINVAL);
263
    }
264

    
265
    av_log(ctx, AV_LOG_INFO,
266
           "name:%s author:'%s' explanation:'%s' color_model:%s "
267
           "frei0r_version:%d version:%d.%d num_params:%d\n",
268
           pi->name, pi->author, pi->explanation,
269
           pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
270
           pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
271
           pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
272
           pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
273

    
274
    return 0;
275
}
276

    
277
static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
278
{
279
    Frei0rContext *frei0r = ctx->priv;
280
    char dl_name[1024], c;
281
    *frei0r->params = 0;
282

    
283
    if (args)
284
        sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
285

    
286
    return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
287
}
288

    
289
static av_cold void uninit(AVFilterContext *ctx)
290
{
291
    Frei0rContext *frei0r = ctx->priv;
292

    
293
    if (frei0r->destruct && frei0r->instance)
294
        frei0r->destruct(frei0r->instance);
295
    if (frei0r->deinit)
296
        frei0r->deinit();
297
    if (frei0r->dl_handle)
298
        dlclose(frei0r->dl_handle);
299

    
300
    memset(frei0r, 0, sizeof(*frei0r));
301
}
302

    
303
static int config_input_props(AVFilterLink *inlink)
304
{
305
    AVFilterContext *ctx = inlink->dst;
306
    Frei0rContext *frei0r = ctx->priv;
307

    
308
    if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
309
        av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
310
        return AVERROR(EINVAL);
311
    }
312

    
313
    return set_params(ctx, frei0r->params);
314
}
315

    
316
static int query_formats(AVFilterContext *ctx)
317
{
318
    Frei0rContext *frei0r = ctx->priv;
319
    AVFilterFormats *formats = NULL;
320

    
321
    if        (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
322
        avfilter_add_format(&formats, PIX_FMT_BGRA);
323
    } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
324
        avfilter_add_format(&formats, PIX_FMT_RGBA);
325
    } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
326
        static const enum PixelFormat pix_fmts[] = {
327
            PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
328
        };
329
        formats = avfilter_make_format_list(pix_fmts);
330
    }
331

    
332
    if (!formats)
333
        return AVERROR(ENOMEM);
334

    
335
    avfilter_set_common_formats(ctx, formats);
336
    return 0;
337
}
338

    
339
static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
340

    
341
static void end_frame(AVFilterLink *inlink)
342
{
343
    Frei0rContext *frei0r = inlink->dst->priv;
344
    AVFilterLink *outlink = inlink->dst->outputs[0];
345
    AVFilterBufferRef  *inpicref =  inlink->cur_buf;
346
    AVFilterBufferRef *outpicref = outlink->out_buf;
347

    
348
    frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
349
                   (const uint32_t *)inpicref->data[0],
350
                   (uint32_t *)outpicref->data[0]);
351
    avfilter_unref_buffer(inpicref);
352
    avfilter_draw_slice(outlink, 0, outlink->h, 1);
353
    avfilter_end_frame(outlink);
354
    avfilter_unref_buffer(outpicref);
355
}
356

    
357
AVFilter avfilter_vf_frei0r = {
358
    .name      = "frei0r",
359
    .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
360

    
361
    .query_formats = query_formats,
362
    .init = filter_init,
363
    .uninit = uninit,
364

    
365
    .priv_size = sizeof(Frei0rContext),
366

    
367
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
368
                                    .type             = AVMEDIA_TYPE_VIDEO,
369
                                    .draw_slice       = null_draw_slice,
370
                                    .config_props     = config_input_props,
371
                                    .end_frame        = end_frame,
372
                                    .min_perms        = AV_PERM_READ },
373
                                  { .name = NULL}},
374

    
375
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
376
                                    .type             = AVMEDIA_TYPE_VIDEO, },
377
                                  { .name = NULL}},
378
};
379

    
380
static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
381
{
382
    Frei0rContext *frei0r = ctx->priv;
383
    char dl_name[1024], c;
384
    char frame_size[128] = "";
385
    char frame_rate[128] = "";
386
    AVRational frame_rate_q;
387

    
388
    memset(frei0r->params, 0, sizeof(frei0r->params));
389

    
390
    if (args)
391
        sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
392
               frame_size, frame_rate, dl_name, &c, frei0r->params);
393

    
394
    if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
395
        av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
396
        return AVERROR(EINVAL);
397
    }
398

    
399
    if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
400
        frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
401
        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
402
        return AVERROR(EINVAL);
403
    }
404
    frei0r->time_base.num = frame_rate_q.den;
405
    frei0r->time_base.den = frame_rate_q.num;
406

    
407
    return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
408
}
409

    
410
static int source_config_props(AVFilterLink *outlink)
411
{
412
    AVFilterContext *ctx = outlink->src;
413
    Frei0rContext *frei0r = ctx->priv;
414

    
415
    if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
416
        return AVERROR(EINVAL);
417
    outlink->w = frei0r->w;
418
    outlink->h = frei0r->h;
419
    outlink->time_base = frei0r->time_base;
420

    
421
    if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
422
        av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
423
        return AVERROR(EINVAL);
424
    }
425

    
426
    return set_params(ctx, frei0r->params);
427
}
428

    
429
static int source_request_frame(AVFilterLink *outlink)
430
{
431
    Frei0rContext *frei0r = outlink->src->priv;
432
    AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
433
    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
434
    picref->pts = frei0r->pts++;
435
    picref->pos = -1;
436

    
437
    avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
438
    frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
439
                   NULL, (uint32_t *)picref->data[0]);
440
    avfilter_draw_slice(outlink, 0, outlink->h, 1);
441
    avfilter_end_frame(outlink);
442
    avfilter_unref_buffer(picref);
443

    
444
    return 0;
445
}
446

    
447
AVFilter avfilter_vsrc_frei0r_src = {
448
    .name        = "frei0r_src",
449
    .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
450

    
451
    .priv_size = sizeof(Frei0rContext),
452
    .init      = source_init,
453
    .uninit    = uninit,
454

    
455
    .query_formats = query_formats,
456

    
457
    .inputs    = (AVFilterPad[]) {{ .name = NULL}},
458

    
459
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
460
                                    .type            = AVMEDIA_TYPE_VIDEO,
461
                                    .request_frame   = source_request_frame,
462
                                    .config_props    = source_config_props },
463
                                  { .name = NULL}},
464
};