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