Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_frei0r.c @ cf5fe8ec

History | View | Annotate | Download (12.5 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 "avfilter.h"
31
#include "parseutils.h"
32

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

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

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

    
59
static void *load_sym(AVFilterContext *ctx, const char *sym_name)
60
{
61
    Frei0rContext *frei0r = ctx->priv;
62
    void *sym = dlsym(frei0r->dl_handle, sym_name);
63
    if (!sym)
64
        av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
65
    return sym;
66
}
67

    
68
static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
69
{
70
    Frei0rContext *frei0r = ctx->priv;
71
    union {
72
        double d;
73
        f0r_param_color_t col;
74
        f0r_param_position_t pos;
75
    } val;
76
    char *tail;
77
    uint8_t rgba[4];
78

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

    
86
    case F0R_PARAM_DOUBLE:
87
        val.d = strtod(param, &tail);
88
        if (*tail || val.d == HUGE_VAL)
89
            goto fail;
90
        break;
91

    
92
    case F0R_PARAM_COLOR:
93
        if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
94
            if (av_parse_color(rgba, param, ctx) < 0)
95
                goto fail;
96
            val.col.r = rgba[0] / 255.0;
97
            val.col.g = rgba[1] / 255.0;
98
            val.col.b = rgba[2] / 255.0;
99
        }
100
        break;
101

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

    
108
    frei0r->set_param_value(frei0r->instance, &val, index);
109
    return 0;
110

    
111
fail:
112
    av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
113
           param, info.name);
114
    return AVERROR(EINVAL);
115
}
116

    
117
static int set_params(AVFilterContext *ctx, const char *params)
118
{
119
    Frei0rContext *frei0r = ctx->priv;
120
    int i;
121

    
122
    for (i = 0; i < frei0r->plugin_info.num_params; i++) {
123
        f0r_param_info_t info;
124
        char *param;
125
        int ret;
126

    
127
        frei0r->get_param_info(&info, i);
128

    
129
        if (*params) {
130
            if (!(param = av_get_token(&params, ":")))
131
                return AVERROR(ENOMEM);
132
            params++;               /* skip ':' */
133
            ret = set_param(ctx, info, i, param);
134
            av_free(param);
135
            if (ret < 0)
136
                return ret;
137
        }
138

    
139
        av_log(ctx, AV_LOG_INFO,
140
               "idx:%d name:'%s' type:%s explanation:'%s' ",
141
               i, info.name,
142
               info.type == F0R_PARAM_BOOL     ? "bool"     :
143
               info.type == F0R_PARAM_DOUBLE   ? "double"   :
144
               info.type == F0R_PARAM_COLOR    ? "color"    :
145
               info.type == F0R_PARAM_POSITION ? "position" :
146
               info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
147
               info.explanation);
148

    
149
#ifdef DEBUG
150
        av_log(ctx, AV_LOG_INFO, "value:");
151
        switch (info.type) {
152
            void *v;
153
            double d;
154
            char s[128];
155
            f0r_param_color_t col;
156
            f0r_param_position_t pos;
157

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

    
188
    return 0;
189
}
190

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

    
195
    snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
196
    av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
197
    return dlopen(path, RTLD_NOW|RTLD_LOCAL);
198
}
199

    
200
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
201
{
202
    Frei0rContext *frei0r = ctx->priv;
203
    f0r_init_f            f0r_init;
204
    f0r_get_plugin_info_f f0r_get_plugin_info;
205
    f0r_plugin_info_t *pi;
206
    char dl_name[1024], *path;
207

    
208
    *frei0r->params = 0;
209

    
210
    if (args)
211
        sscanf(args, "%1023[^:]:%255c", dl_name, frei0r->params);
212

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

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

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

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

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

    
271
    return 0;
272
}
273

    
274
static av_cold void uninit(AVFilterContext *ctx)
275
{
276
    Frei0rContext *frei0r = ctx->priv;
277

    
278
    if (frei0r->destruct)
279
        frei0r->destruct(frei0r->instance);
280
    if (frei0r->deinit)
281
        frei0r->deinit();
282
    if (frei0r->dl_handle)
283
        dlclose(frei0r->dl_handle);
284

    
285
    memset(frei0r, 0, sizeof(*frei0r));
286
}
287

    
288
static int config_input_props(AVFilterLink *inlink)
289
{
290
    AVFilterContext *ctx = inlink->dst;
291
    Frei0rContext *frei0r = ctx->priv;
292

    
293
    if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
294
        av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
295
        return AVERROR(EINVAL);
296
    }
297

    
298
    return set_params(ctx, frei0r->params);
299
}
300

    
301
static int query_formats(AVFilterContext *ctx)
302
{
303
    Frei0rContext *frei0r = ctx->priv;
304
    AVFilterFormats *formats = NULL;
305

    
306
    if        (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
307
        avfilter_add_format(&formats, PIX_FMT_BGRA);
308
    } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
309
        avfilter_add_format(&formats, PIX_FMT_RGBA);
310
    } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
311
        static const enum PixelFormat pix_fmts[] = {
312
            PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
313
        };
314
        formats = avfilter_make_format_list(pix_fmts);
315
    }
316

    
317
    if (!formats)
318
        return AVERROR(ENOMEM);
319

    
320
    avfilter_set_common_formats(ctx, formats);
321
    return 0;
322
}
323

    
324
static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
325

    
326
static void end_frame(AVFilterLink *inlink)
327
{
328
    Frei0rContext *frei0r = inlink->dst->priv;
329
    AVFilterLink *outlink = inlink->dst->outputs[0];
330
    AVFilterBufferRef  *inpicref =  inlink->cur_buf;
331
    AVFilterBufferRef *outpicref = outlink->out_buf;
332

    
333
    frei0r->update(frei0r->instance, (double)inpicref->pts / AV_TIME_BASE,
334
                   (const uint32_t *)inpicref->data[0],
335
                   (uint32_t *)outpicref->data[0]);
336
    avfilter_unref_buffer(inpicref);
337
    avfilter_draw_slice(outlink, 0, outlink->h, 1);
338
    avfilter_end_frame(outlink);
339
    avfilter_unref_buffer(outpicref);
340
}
341

    
342
AVFilter avfilter_vf_frei0r = {
343
    .name      = "frei0r",
344
    .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
345

    
346
    .query_formats = query_formats,
347
    .init = init,
348
    .uninit = uninit,
349

    
350
    .priv_size = sizeof(Frei0rContext),
351

    
352
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
353
                                    .type             = AVMEDIA_TYPE_VIDEO,
354
                                    .draw_slice       = null_draw_slice,
355
                                    .config_props     = config_input_props,
356
                                    .end_frame        = end_frame,
357
                                    .min_perms        = AV_PERM_READ },
358
                                  { .name = NULL}},
359

    
360
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
361
                                    .type             = AVMEDIA_TYPE_VIDEO, },
362
                                  { .name = NULL}},
363
};