]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_frei0r.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_frei0r.c
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/mathematics.h"
32 #include "libavutil/parseutils.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "video.h"
36
37 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
38 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
39 typedef void (*f0r_deinit_f)(void);
40 typedef int (*f0r_init_f)(void);
41 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
42 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
43 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
44 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);
45 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
46 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
47
48 typedef struct Frei0rContext {
49     f0r_update_f update;
50     void *dl_handle;            /* dynamic library handle   */
51     f0r_instance_t instance;
52     f0r_plugin_info_t plugin_info;
53
54     f0r_get_param_info_f  get_param_info;
55     f0r_get_param_value_f get_param_value;
56     f0r_set_param_value_f set_param_value;
57     f0r_construct_f       construct;
58     f0r_destruct_f        destruct;
59     f0r_deinit_f          deinit;
60     char params[256];
61
62     /* only used by the source */
63     int w, h;
64     AVRational time_base;
65     uint64_t pts;
66 } Frei0rContext;
67
68 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
69 {
70     Frei0rContext *frei0r = ctx->priv;
71     void *sym = dlsym(frei0r->dl_handle, sym_name);
72     if (!sym)
73         av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
74     return sym;
75 }
76
77 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
78 {
79     Frei0rContext *frei0r = ctx->priv;
80     union {
81         double d;
82         f0r_param_color_t col;
83         f0r_param_position_t pos;
84     } val;
85     char *tail;
86     uint8_t rgba[4];
87
88     switch (info.type) {
89     case F0R_PARAM_BOOL:
90         if      (!strcmp(param, "y")) val.d = 1.0;
91         else if (!strcmp(param, "n")) val.d = 0.0;
92         else goto fail;
93         break;
94
95     case F0R_PARAM_DOUBLE:
96         val.d = strtod(param, &tail);
97         if (*tail || val.d == HUGE_VAL)
98             goto fail;
99         break;
100
101     case F0R_PARAM_COLOR:
102         if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
103             if (av_parse_color(rgba, param, -1, ctx) < 0)
104                 goto fail;
105             val.col.r = rgba[0] / 255.0;
106             val.col.g = rgba[1] / 255.0;
107             val.col.b = rgba[2] / 255.0;
108         }
109         break;
110
111     case F0R_PARAM_POSITION:
112         if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
113             goto fail;
114         break;
115     }
116
117     frei0r->set_param_value(frei0r->instance, &val, index);
118     return 0;
119
120 fail:
121     av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
122            param, info.name);
123     return AVERROR(EINVAL);
124 }
125
126 static int set_params(AVFilterContext *ctx, const char *params)
127 {
128     Frei0rContext *frei0r = ctx->priv;
129     int i;
130
131     for (i = 0; i < frei0r->plugin_info.num_params; i++) {
132         f0r_param_info_t info;
133         char *param;
134         int ret;
135
136         frei0r->get_param_info(&info, i);
137
138         if (*params) {
139             if (!(param = av_get_token(&params, ":")))
140                 return AVERROR(ENOMEM);
141             params++;               /* skip ':' */
142             ret = set_param(ctx, info, i, param);
143             av_free(param);
144             if (ret < 0)
145                 return ret;
146         }
147
148         av_log(ctx, AV_LOG_INFO,
149                "idx:%d name:'%s' type:%s explanation:'%s' ",
150                i, info.name,
151                info.type == F0R_PARAM_BOOL     ? "bool"     :
152                info.type == F0R_PARAM_DOUBLE   ? "double"   :
153                info.type == F0R_PARAM_COLOR    ? "color"    :
154                info.type == F0R_PARAM_POSITION ? "position" :
155                info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
156                info.explanation);
157
158 #ifdef DEBUG
159         av_log(ctx, AV_LOG_INFO, "value:");
160         switch (info.type) {
161             void *v;
162             double d;
163             char s[128];
164             f0r_param_color_t col;
165             f0r_param_position_t pos;
166
167         case F0R_PARAM_BOOL:
168             v = &d;
169             frei0r->get_param_value(frei0r->instance, v, i);
170             av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
171             break;
172         case F0R_PARAM_DOUBLE:
173             v = &d;
174             frei0r->get_param_value(frei0r->instance, v, i);
175             av_log(ctx, AV_LOG_INFO, "%f", d);
176             break;
177         case F0R_PARAM_COLOR:
178             v = &col;
179             frei0r->get_param_value(frei0r->instance, v, i);
180             av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
181             break;
182         case F0R_PARAM_POSITION:
183             v = &pos;
184             frei0r->get_param_value(frei0r->instance, v, i);
185             av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
186             break;
187         default: /* F0R_PARAM_STRING */
188             v = s;
189             frei0r->get_param_value(frei0r->instance, v, i);
190             av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
191             break;
192         }
193 #endif
194         av_log(ctx, AV_LOG_INFO, "\n");
195     }
196
197     return 0;
198 }
199
200 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
201 {
202     char path[1024];
203
204     snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
205     av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
206     return dlopen(path, RTLD_NOW|RTLD_LOCAL);
207 }
208
209 static av_cold int frei0r_init(AVFilterContext *ctx,
210                                const char *dl_name, int type)
211 {
212     Frei0rContext *frei0r = ctx->priv;
213     f0r_init_f            f0r_init;
214     f0r_get_plugin_info_f f0r_get_plugin_info;
215     f0r_plugin_info_t *pi;
216     char *path;
217
218     /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
219     if ((path = av_strdup(getenv("FREI0R_PATH")))) {
220         char *p, *ptr = NULL;
221         for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
222             if (frei0r->dl_handle = load_path(ctx, p, dl_name))
223                 break;
224         av_free(path);
225     }
226     if (!frei0r->dl_handle && (path = getenv("HOME"))) {
227         char prefix[1024];
228         snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
229         frei0r->dl_handle = load_path(ctx, prefix, dl_name);
230     }
231     if (!frei0r->dl_handle)
232         frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
233     if (!frei0r->dl_handle)
234         frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
235     if (!frei0r->dl_handle) {
236         av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
237         return AVERROR(EINVAL);
238     }
239
240     if (!(f0r_init                = load_sym(ctx, "f0r_init"           )) ||
241         !(f0r_get_plugin_info     = load_sym(ctx, "f0r_get_plugin_info")) ||
242         !(frei0r->get_param_info  = load_sym(ctx, "f0r_get_param_info" )) ||
243         !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
244         !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
245         !(frei0r->update          = load_sym(ctx, "f0r_update"         )) ||
246         !(frei0r->construct       = load_sym(ctx, "f0r_construct"      )) ||
247         !(frei0r->destruct        = load_sym(ctx, "f0r_destruct"       )) ||
248         !(frei0r->deinit          = load_sym(ctx, "f0r_deinit"         )))
249         return AVERROR(EINVAL);
250
251     if (f0r_init() < 0) {
252         av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
253         return AVERROR(EINVAL);
254     }
255
256     f0r_get_plugin_info(&frei0r->plugin_info);
257     pi = &frei0r->plugin_info;
258     if (pi->plugin_type != type) {
259         av_log(ctx, AV_LOG_ERROR,
260                "Invalid type '%s' for the plugin\n",
261                pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
262                pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
263                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
264                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
265         return AVERROR(EINVAL);
266     }
267
268     av_log(ctx, AV_LOG_INFO,
269            "name:%s author:'%s' explanation:'%s' color_model:%s "
270            "frei0r_version:%d version:%d.%d num_params:%d\n",
271            pi->name, pi->author, pi->explanation,
272            pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
273            pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
274            pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
275            pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
276
277     return 0;
278 }
279
280 static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
281 {
282     Frei0rContext *frei0r = ctx->priv;
283     char dl_name[1024], c;
284     *frei0r->params = 0;
285
286     if (args)
287         sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
288
289     return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
290 }
291
292 static av_cold void uninit(AVFilterContext *ctx)
293 {
294     Frei0rContext *frei0r = ctx->priv;
295
296     if (frei0r->destruct && frei0r->instance)
297         frei0r->destruct(frei0r->instance);
298     if (frei0r->deinit)
299         frei0r->deinit();
300     if (frei0r->dl_handle)
301         dlclose(frei0r->dl_handle);
302
303     memset(frei0r, 0, sizeof(*frei0r));
304 }
305
306 static int config_input_props(AVFilterLink *inlink)
307 {
308     AVFilterContext *ctx = inlink->dst;
309     Frei0rContext *frei0r = ctx->priv;
310
311     if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
312         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
313         return AVERROR(EINVAL);
314     }
315
316     return set_params(ctx, frei0r->params);
317 }
318
319 static int query_formats(AVFilterContext *ctx)
320 {
321     Frei0rContext *frei0r = ctx->priv;
322     AVFilterFormats *formats = NULL;
323
324     if        (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
325         ff_add_format(&formats, PIX_FMT_BGRA);
326     } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
327         ff_add_format(&formats, PIX_FMT_RGBA);
328     } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
329         static const enum PixelFormat pix_fmts[] = {
330             PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
331         };
332         formats = ff_make_format_list(pix_fmts);
333     }
334
335     if (!formats)
336         return AVERROR(ENOMEM);
337
338     ff_set_common_formats(ctx, formats);
339     return 0;
340 }
341
342 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
343
344 static void end_frame(AVFilterLink *inlink)
345 {
346     Frei0rContext *frei0r = inlink->dst->priv;
347     AVFilterLink *outlink = inlink->dst->outputs[0];
348     AVFilterBufferRef  *inpicref =  inlink->cur_buf;
349     AVFilterBufferRef *outpicref = outlink->out_buf;
350
351     frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
352                    (const uint32_t *)inpicref->data[0],
353                    (uint32_t *)outpicref->data[0]);
354     avfilter_unref_buffer(inpicref);
355     ff_draw_slice(outlink, 0, outlink->h, 1);
356     ff_end_frame(outlink);
357     avfilter_unref_buffer(outpicref);
358 }
359
360 AVFilter avfilter_vf_frei0r = {
361     .name      = "frei0r",
362     .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
363
364     .query_formats = query_formats,
365     .init = filter_init,
366     .uninit = uninit,
367
368     .priv_size = sizeof(Frei0rContext),
369
370     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
371                                     .type             = AVMEDIA_TYPE_VIDEO,
372                                     .draw_slice       = null_draw_slice,
373                                     .config_props     = config_input_props,
374                                     .end_frame        = end_frame,
375                                     .min_perms        = AV_PERM_READ },
376                                   { .name = NULL}},
377
378     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
379                                     .type             = AVMEDIA_TYPE_VIDEO, },
380                                   { .name = NULL}},
381 };
382
383 static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
384 {
385     Frei0rContext *frei0r = ctx->priv;
386     char dl_name[1024], c;
387     char frame_size[128] = "";
388     char frame_rate[128] = "";
389     AVRational frame_rate_q;
390
391     memset(frei0r->params, 0, sizeof(frei0r->params));
392
393     if (args)
394         sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
395                frame_size, frame_rate, dl_name, &c, frei0r->params);
396
397     if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
398         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
399         return AVERROR(EINVAL);
400     }
401
402     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
403         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
404         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
405         return AVERROR(EINVAL);
406     }
407     frei0r->time_base.num = frame_rate_q.den;
408     frei0r->time_base.den = frame_rate_q.num;
409
410     return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
411 }
412
413 static int source_config_props(AVFilterLink *outlink)
414 {
415     AVFilterContext *ctx = outlink->src;
416     Frei0rContext *frei0r = ctx->priv;
417
418     if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
419         return AVERROR(EINVAL);
420     outlink->w = frei0r->w;
421     outlink->h = frei0r->h;
422     outlink->time_base = frei0r->time_base;
423     outlink->sample_aspect_ratio = (AVRational){1,1};
424
425     if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
426         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
427         return AVERROR(EINVAL);
428     }
429
430     return set_params(ctx, frei0r->params);
431 }
432
433 static int source_request_frame(AVFilterLink *outlink)
434 {
435     Frei0rContext *frei0r = outlink->src->priv;
436     AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
437     picref->video->sample_aspect_ratio = (AVRational) {1, 1};
438     picref->pts = frei0r->pts++;
439     picref->pos = -1;
440
441     ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
442     frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
443                    NULL, (uint32_t *)picref->data[0]);
444     ff_draw_slice(outlink, 0, outlink->h, 1);
445     ff_end_frame(outlink);
446     avfilter_unref_buffer(picref);
447
448     return 0;
449 }
450
451 AVFilter avfilter_vsrc_frei0r_src = {
452     .name        = "frei0r_src",
453     .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
454
455     .priv_size = sizeof(Frei0rContext),
456     .init      = source_init,
457     .uninit    = uninit,
458
459     .query_formats = query_formats,
460
461     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
462
463     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
464                                     .type            = AVMEDIA_TYPE_VIDEO,
465                                     .request_frame   = source_request_frame,
466                                     .config_props    = source_config_props },
467                                   { .name = NULL}},
468 };