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