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