]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_frei0r.c
avfilter/drawutils: fix gray and gbr formats on big endian
[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         av_log(ctx, AV_LOG_VERBOSE,
165                "idx:%d name:'%s' type:%s explanation:'%s' ",
166                i, info.name,
167                info.type == F0R_PARAM_BOOL     ? "bool"     :
168                info.type == F0R_PARAM_DOUBLE   ? "double"   :
169                info.type == F0R_PARAM_COLOR    ? "color"    :
170                info.type == F0R_PARAM_POSITION ? "position" :
171                info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
172                info.explanation);
173
174 #ifdef DEBUG
175         av_log(ctx, AV_LOG_DEBUG, "value:");
176         switch (info.type) {
177             void *v;
178             double d;
179             char str[128];
180             f0r_param_color_t col;
181             f0r_param_position_t pos;
182
183         case F0R_PARAM_BOOL:
184             v = &d;
185             s->get_param_value(s->instance, v, i);
186             av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
187             break;
188         case F0R_PARAM_DOUBLE:
189             v = &d;
190             s->get_param_value(s->instance, v, i);
191             av_log(ctx, AV_LOG_DEBUG, "%f", d);
192             break;
193         case F0R_PARAM_COLOR:
194             v = &col;
195             s->get_param_value(s->instance, v, i);
196             av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
197             break;
198         case F0R_PARAM_POSITION:
199             v = &pos;
200             s->get_param_value(s->instance, v, i);
201             av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
202             break;
203         default: /* F0R_PARAM_STRING */
204             v = str;
205             s->get_param_value(s->instance, v, i);
206             av_log(ctx, AV_LOG_DEBUG, "'%s'", str);
207             break;
208         }
209 #endif
210         av_log(ctx, AV_LOG_VERBOSE, ".\n");
211     }
212
213     return 0;
214 }
215
216 static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
217 {
218     char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
219     if (!path)
220         return AVERROR(ENOMEM);
221     av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
222     *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
223     av_free(path);
224     return 0;
225 }
226
227 static av_cold int frei0r_init(AVFilterContext *ctx,
228                                const char *dl_name, int type)
229 {
230     Frei0rContext *s = ctx->priv;
231     f0r_init_f            f0r_init;
232     f0r_get_plugin_info_f f0r_get_plugin_info;
233     f0r_plugin_info_t *pi;
234     char *path;
235     int ret = 0;
236     int i;
237     static const char* const frei0r_pathlist[] = {
238         "/usr/local/lib/frei0r-1/",
239         "/usr/lib/frei0r-1/",
240         "/usr/local/lib64/frei0r-1/",
241         "/usr/lib64/frei0r-1/"
242     };
243
244     if (!dl_name) {
245         av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
246         return AVERROR(EINVAL);
247     }
248
249     /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
250     if ((path = av_strdup(getenv("FREI0R_PATH")))) {
251 #ifdef _WIN32
252         const char *separator = ";";
253 #else
254         const char *separator = ":";
255 #endif
256         char *p, *ptr = NULL;
257         for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
258             /* add additional trailing slash in case it is missing */
259             char *p1 = av_asprintf("%s/", p);
260             if (!p1) {
261                 ret = AVERROR(ENOMEM);
262                 goto check_path_end;
263             }
264             ret = load_path(ctx, &s->dl_handle, p1, dl_name);
265             av_free(p1);
266             if (ret < 0)
267                 goto check_path_end;
268             if (s->dl_handle)
269                 break;
270         }
271
272     check_path_end:
273         av_free(path);
274         if (ret < 0)
275             return ret;
276     }
277     if (!s->dl_handle && (path = getenv("HOME"))) {
278         char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
279         if (!prefix)
280             return AVERROR(ENOMEM);
281         ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
282         av_free(prefix);
283         if (ret < 0)
284             return ret;
285     }
286     for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
287         ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name);
288         if (ret < 0)
289             return ret;
290     }
291     if (!s->dl_handle) {
292         av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
293         return AVERROR(EINVAL);
294     }
295
296     if (!(f0r_init                = load_sym(ctx, "f0r_init"           )) ||
297         !(f0r_get_plugin_info     = load_sym(ctx, "f0r_get_plugin_info")) ||
298         !(s->get_param_info  = load_sym(ctx, "f0r_get_param_info" )) ||
299         !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
300         !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
301         !(s->update          = load_sym(ctx, "f0r_update"         )) ||
302         !(s->construct       = load_sym(ctx, "f0r_construct"      )) ||
303         !(s->destruct        = load_sym(ctx, "f0r_destruct"       )) ||
304         !(s->deinit          = load_sym(ctx, "f0r_deinit"         )))
305         return AVERROR(EINVAL);
306
307     if (f0r_init() < 0) {
308         av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
309         return AVERROR(EINVAL);
310     }
311
312     f0r_get_plugin_info(&s->plugin_info);
313     pi = &s->plugin_info;
314     if (pi->plugin_type != type) {
315         av_log(ctx, AV_LOG_ERROR,
316                "Invalid type '%s' for this plugin\n",
317                pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
318                pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
319                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
320                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
321         return AVERROR(EINVAL);
322     }
323
324     av_log(ctx, AV_LOG_VERBOSE,
325            "name:%s author:'%s' explanation:'%s' color_model:%s "
326            "frei0r_version:%d version:%d.%d num_params:%d\n",
327            pi->name, pi->author, pi->explanation,
328            pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
329            pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
330            pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
331            pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
332
333     return 0;
334 }
335
336 static av_cold int filter_init(AVFilterContext *ctx)
337 {
338     Frei0rContext *s = ctx->priv;
339
340     return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
341 }
342
343 static av_cold void uninit(AVFilterContext *ctx)
344 {
345     Frei0rContext *s = ctx->priv;
346
347     if (s->destruct && s->instance)
348         s->destruct(s->instance);
349     if (s->deinit)
350         s->deinit();
351     if (s->dl_handle)
352         dlclose(s->dl_handle);
353 }
354
355 static int config_input_props(AVFilterLink *inlink)
356 {
357     AVFilterContext *ctx = inlink->dst;
358     Frei0rContext *s = ctx->priv;
359
360     if (s->destruct && s->instance)
361         s->destruct(s->instance);
362     if (!(s->instance = s->construct(inlink->w, inlink->h))) {
363         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
364         return AVERROR(EINVAL);
365     }
366
367     return set_params(ctx, s->params);
368 }
369
370 static int query_formats(AVFilterContext *ctx)
371 {
372     Frei0rContext *s = ctx->priv;
373     AVFilterFormats *formats = NULL;
374     int ret;
375
376     if        (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
377         if ((ret = ff_add_format(&formats, AV_PIX_FMT_BGRA)) < 0)
378             return ret;
379     } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
380         if ((ret = ff_add_format(&formats, AV_PIX_FMT_RGBA)) < 0)
381             return ret;
382     } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
383         static const enum AVPixelFormat pix_fmts[] = {
384             AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
385         };
386         formats = ff_make_format_list(pix_fmts);
387     }
388
389     if (!formats)
390         return AVERROR(ENOMEM);
391
392     return ff_set_common_formats(ctx, formats);
393 }
394
395 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
396 {
397     Frei0rContext *s = inlink->dst->priv;
398     AVFilterLink *outlink = inlink->dst->outputs[0];
399     AVFrame *out;
400
401     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
402     if (!out) {
403         av_frame_free(&in);
404         return AVERROR(ENOMEM);
405     }
406     av_frame_copy_props(out, in);
407
408     s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
409                    (const uint32_t *)in->data[0],
410                    (uint32_t *)out->data[0]);
411
412     av_frame_free(&in);
413
414     return ff_filter_frame(outlink, out);
415 }
416
417 #define OFFSET(x) offsetof(Frei0rContext, x)
418 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
419 static const AVOption frei0r_options[] = {
420     { "filter_name",   NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
421     { "filter_params", NULL, OFFSET(params),  AV_OPT_TYPE_STRING, .flags = FLAGS },
422     { NULL }
423 };
424
425 AVFILTER_DEFINE_CLASS(frei0r);
426
427 static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
428     {
429         .name         = "default",
430         .type         = AVMEDIA_TYPE_VIDEO,
431         .config_props = config_input_props,
432         .filter_frame = filter_frame,
433     },
434     { NULL }
435 };
436
437 static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
438     {
439         .name = "default",
440         .type = AVMEDIA_TYPE_VIDEO,
441     },
442     { NULL }
443 };
444
445 AVFilter ff_vf_frei0r = {
446     .name          = "frei0r",
447     .description   = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
448     .query_formats = query_formats,
449     .init          = filter_init,
450     .uninit        = uninit,
451     .priv_size     = sizeof(Frei0rContext),
452     .priv_class    = &frei0r_class,
453     .inputs        = avfilter_vf_frei0r_inputs,
454     .outputs       = avfilter_vf_frei0r_outputs,
455 };
456
457 static av_cold int source_init(AVFilterContext *ctx)
458 {
459     Frei0rContext *s = ctx->priv;
460
461     s->time_base.num = s->framerate.den;
462     s->time_base.den = s->framerate.num;
463
464     return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
465 }
466
467 static int source_config_props(AVFilterLink *outlink)
468 {
469     AVFilterContext *ctx = outlink->src;
470     Frei0rContext *s = ctx->priv;
471
472     if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
473         return AVERROR(EINVAL);
474     outlink->w = s->w;
475     outlink->h = s->h;
476     outlink->time_base = s->time_base;
477     outlink->frame_rate = av_inv_q(s->time_base);
478     outlink->sample_aspect_ratio = (AVRational){1,1};
479
480     if (s->destruct && s->instance)
481         s->destruct(s->instance);
482     if (!(s->instance = s->construct(outlink->w, outlink->h))) {
483         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
484         return AVERROR(EINVAL);
485     }
486     if (!s->params) {
487         av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
488         return AVERROR(EINVAL);
489     }
490
491     return set_params(ctx, s->params);
492 }
493
494 static int source_request_frame(AVFilterLink *outlink)
495 {
496     Frei0rContext *s = outlink->src->priv;
497     AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
498
499     if (!frame)
500         return AVERROR(ENOMEM);
501
502     frame->sample_aspect_ratio = (AVRational) {1, 1};
503     frame->pts = s->pts++;
504
505     s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
506                    NULL, (uint32_t *)frame->data[0]);
507
508     return ff_filter_frame(outlink, frame);
509 }
510
511 static const AVOption frei0r_src_options[] = {
512     { "size",          "Dimensions of the generated video.", OFFSET(w),         AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
513     { "framerate",     NULL,                                 OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = FLAGS },
514     { "filter_name",   NULL,                                 OFFSET(dl_name),   AV_OPT_TYPE_STRING,                  .flags = FLAGS },
515     { "filter_params", NULL,                                 OFFSET(params),    AV_OPT_TYPE_STRING,                  .flags = FLAGS },
516     { NULL },
517 };
518
519 AVFILTER_DEFINE_CLASS(frei0r_src);
520
521 static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
522     {
523         .name          = "default",
524         .type          = AVMEDIA_TYPE_VIDEO,
525         .request_frame = source_request_frame,
526         .config_props  = source_config_props
527     },
528     { NULL }
529 };
530
531 AVFilter ff_vsrc_frei0r_src = {
532     .name          = "frei0r_src",
533     .description   = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
534     .priv_size     = sizeof(Frei0rContext),
535     .priv_class    = &frei0r_src_class,
536     .init          = source_init,
537     .uninit        = uninit,
538     .query_formats = query_formats,
539     .inputs        = NULL,
540     .outputs       = avfilter_vsrc_frei0r_src_outputs,
541 };