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