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