vf_format: use the name 's' for the pointer to the private context
[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
47941088
SS
25#include <dlfcn.h>
26#include <frei0r.h>
7ebe3962
MS
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30#include "config.h"
372e2884 31#include "libavutil/avstring.h"
737eb597 32#include "libavutil/imgutils.h"
7ebe3962 33#include "libavutil/internal.h"
d3f751e6 34#include "libavutil/mathematics.h"
7ebe3962 35#include "libavutil/mem.h"
5aa1a668 36#include "libavutil/opt.h"
737eb597 37#include "libavutil/parseutils.h"
47941088 38#include "avfilter.h"
b74a1da4 39#include "formats.h"
9d0bfc50 40#include "internal.h"
803391f7 41#include "video.h"
47941088
SS
42
43typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
44typedef void (*f0r_destruct_f)(f0r_instance_t instance);
45typedef void (*f0r_deinit_f)(void);
46typedef int (*f0r_init_f)(void);
47typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
48typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
49typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
50typedef 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);
51typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
52typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
53
54typedef struct Frei0rContext {
5aa1a668 55 const AVClass *class;
47941088
SS
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;
5aa1a668
AK
67
68 char *dl_name;
69 char *params;
70 char *size;
71 char *framerate;
f8608dca
SS
72
73 /* only used by the source */
74 int w, h;
75 AVRational time_base;
76 uint64_t pts;
47941088
SS
77} Frei0rContext;
78
79static void *load_sym(AVFilterContext *ctx, const char *sym_name)
80{
81 Frei0rContext *frei0r = ctx->priv;
82 void *sym = dlsym(frei0r->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
88static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
89{
90 Frei0rContext *frei0r = 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) {
f6c182a0 114 if (av_parse_color(rgba, param, -1, ctx) < 0)
47941088
SS
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 frei0r->set_param_value(frei0r->instance, &val, index);
129 return 0;
130
131fail:
132 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
133 param, info.name);
134 return AVERROR(EINVAL);
135}
136
137static int set_params(AVFilterContext *ctx, const char *params)
138{
139 Frei0rContext *frei0r = ctx->priv;
140 int i;
141
142 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
143 f0r_param_info_t info;
144 char *param;
145 int ret;
146
147 frei0r->get_param_info(&info, i);
148
149 if (*params) {
5aa1a668 150 if (!(param = av_get_token(&params, "|")))
47941088
SS
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
1a49a169 159 av_log(ctx, AV_LOG_VERBOSE,
47941088
SS
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
1a49a169 170 av_log(ctx, AV_LOG_DEBUG, "value:");
47941088
SS
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 frei0r->get_param_value(frei0r->instance, v, i);
1a49a169 181 av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
47941088
SS
182 break;
183 case F0R_PARAM_DOUBLE:
184 v = &d;
185 frei0r->get_param_value(frei0r->instance, v, i);
1a49a169 186 av_log(ctx, AV_LOG_DEBUG, "%f", d);
47941088
SS
187 break;
188 case F0R_PARAM_COLOR:
189 v = &col;
190 frei0r->get_param_value(frei0r->instance, v, i);
1a49a169 191 av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
47941088
SS
192 break;
193 case F0R_PARAM_POSITION:
194 v = &pos;
195 frei0r->get_param_value(frei0r->instance, v, i);
bcb8d9eb 196 av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
47941088
SS
197 break;
198 default: /* F0R_PARAM_STRING */
199 v = s;
200 frei0r->get_param_value(frei0r->instance, v, i);
1a49a169 201 av_log(ctx, AV_LOG_DEBUG, "'%s'\n", s);
47941088
SS
202 break;
203 }
204#endif
1a49a169 205 av_log(ctx, AV_LOG_VERBOSE, "\n");
47941088
SS
206 }
207
208 return 0;
209}
210
211static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
212{
213 char path[1024];
214
524aae86 215 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
47941088
SS
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
f8608dca
SS
220static av_cold int frei0r_init(AVFilterContext *ctx,
221 const char *dl_name, int type)
47941088
SS
222{
223 Frei0rContext *frei0r = ctx->priv;
224 f0r_init_f f0r_init;
225 f0r_get_plugin_info_f f0r_get_plugin_info;
226 f0r_plugin_info_t *pi;
f8608dca 227 char *path;
47941088 228
5aa1a668
AK
229 if (!dl_name) {
230 av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
231 return AVERROR(EINVAL);
232 }
233
47941088
SS
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 (frei0r->dl_handle = load_path(ctx, p, dl_name))
239 break;
240 av_free(path);
241 }
32eba9f2 242 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
47941088
SS
243 char prefix[1024];
244 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
245 frei0r->dl_handle = load_path(ctx, prefix, dl_name);
47941088
SS
246 }
247 if (!frei0r->dl_handle)
248 frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
249 if (!frei0r->dl_handle)
250 frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
251 if (!frei0r->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 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
259 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
260 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
261 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
262 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
263 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
264 !(frei0r->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(&frei0r->plugin_info);
273 pi = &frei0r->plugin_info;
f8608dca 274 if (pi->plugin_type != type) {
47941088 275 av_log(ctx, AV_LOG_ERROR,
f8608dca
SS
276 "Invalid type '%s' for the plugin\n",
277 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
47941088
SS
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
1a49a169 284 av_log(ctx, AV_LOG_VERBOSE,
47941088
SS
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
c43a7eca 296static av_cold int filter_init(AVFilterContext *ctx)
f8608dca
SS
297{
298 Frei0rContext *frei0r = ctx->priv;
f8608dca 299
5aa1a668 300 return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_FILTER);
f8608dca
SS
301}
302
47941088
SS
303static av_cold void uninit(AVFilterContext *ctx)
304{
305 Frei0rContext *frei0r = ctx->priv;
306
2467997c 307 if (frei0r->destruct && frei0r->instance)
47941088
SS
308 frei0r->destruct(frei0r->instance);
309 if (frei0r->deinit)
310 frei0r->deinit();
311 if (frei0r->dl_handle)
312 dlclose(frei0r->dl_handle);
47941088
SS
313}
314
315static int config_input_props(AVFilterLink *inlink)
316{
317 AVFilterContext *ctx = inlink->dst;
318 Frei0rContext *frei0r = ctx->priv;
319
320 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
321 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
322 return AVERROR(EINVAL);
323 }
324
325 return set_params(ctx, frei0r->params);
326}
327
328static int query_formats(AVFilterContext *ctx)
329{
330 Frei0rContext *frei0r = ctx->priv;
331 AVFilterFormats *formats = NULL;
332
333 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
716d413c 334 ff_add_format(&formats, AV_PIX_FMT_BGRA);
47941088 335 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
716d413c 336 ff_add_format(&formats, AV_PIX_FMT_RGBA);
47941088 337 } else { /* F0R_COLOR_MODEL_PACKED32 */
716d413c
AK
338 static const enum AVPixelFormat pix_fmts[] = {
339 AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
47941088 340 };
b74a1da4 341 formats = ff_make_format_list(pix_fmts);
47941088
SS
342 }
343
344 if (!formats)
345 return AVERROR(ENOMEM);
346
b74a1da4 347 ff_set_common_formats(ctx, formats);
47941088
SS
348 return 0;
349}
350
7e350379 351static int filter_frame(AVFilterLink *inlink, AVFrame *in)
47941088
SS
352{
353 Frei0rContext *frei0r = inlink->dst->priv;
354 AVFilterLink *outlink = inlink->dst->outputs[0];
7e350379 355 AVFrame *out;
565e4993 356
7e350379 357 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 358 if (!out) {
7e350379 359 av_frame_free(&in);
565e4993
AK
360 return AVERROR(ENOMEM);
361 }
7e350379 362 av_frame_copy_props(out, in);
565e4993
AK
363
364 frei0r->update(frei0r->instance, in->pts * av_q2d(inlink->time_base) * 1000,
365 (const uint32_t *)in->data[0],
366 (uint32_t *)out->data[0]);
367
7e350379 368 av_frame_free(&in);
565e4993
AK
369
370 return ff_filter_frame(outlink, out);
47941088
SS
371}
372
5aa1a668
AK
373#define OFFSET(x) offsetof(Frei0rContext, x)
374#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
375static const AVOption filter_options[] = {
376 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
377 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
378 { NULL },
379};
380
381static const AVClass filter_class = {
382 .class_name = "frei0r",
383 .item_name = av_default_item_name,
384 .option = filter_options,
385 .version = LIBAVUTIL_VERSION_INT,
386};
387
568c70e7
MR
388static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
389 {
390 .name = "default",
391 .type = AVMEDIA_TYPE_VIDEO,
568c70e7 392 .config_props = config_input_props,
565e4993 393 .filter_frame = filter_frame,
568c70e7
MR
394 },
395 { NULL }
396};
397
398static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
399 {
400 .name = "default",
401 .type = AVMEDIA_TYPE_VIDEO,
402 },
403 { NULL }
404};
405
47941088
SS
406AVFilter avfilter_vf_frei0r = {
407 .name = "frei0r",
408 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
409
410 .query_formats = query_formats,
f8608dca 411 .init = filter_init,
47941088
SS
412 .uninit = uninit,
413
414 .priv_size = sizeof(Frei0rContext),
5aa1a668 415 .priv_class = &filter_class,
47941088 416
568c70e7
MR
417 .inputs = avfilter_vf_frei0r_inputs,
418
419 .outputs = avfilter_vf_frei0r_outputs,
47941088 420};
f8608dca 421
c43a7eca 422static av_cold int source_init(AVFilterContext *ctx)
f8608dca
SS
423{
424 Frei0rContext *frei0r = ctx->priv;
f8608dca
SS
425 AVRational frame_rate_q;
426
5aa1a668
AK
427 if (av_parse_video_size(&frei0r->w, &frei0r->h, frei0r->size) < 0) {
428 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frei0r->size);
f8608dca
SS
429 return AVERROR(EINVAL);
430 }
431
5aa1a668 432 if (av_parse_video_rate(&frame_rate_q, frei0r->framerate) < 0 ||
f8608dca 433 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
5aa1a668 434 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frei0r->framerate);
f8608dca
SS
435 return AVERROR(EINVAL);
436 }
437 frei0r->time_base.num = frame_rate_q.den;
438 frei0r->time_base.den = frame_rate_q.num;
439
5aa1a668 440 return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_SOURCE);
f8608dca
SS
441}
442
443static int source_config_props(AVFilterLink *outlink)
444{
445 AVFilterContext *ctx = outlink->src;
446 Frei0rContext *frei0r = ctx->priv;
447
448 if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
449 return AVERROR(EINVAL);
450 outlink->w = frei0r->w;
451 outlink->h = frei0r->h;
452 outlink->time_base = frei0r->time_base;
453
454 if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
455 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
456 return AVERROR(EINVAL);
457 }
458
459 return set_params(ctx, frei0r->params);
460}
461
462static int source_request_frame(AVFilterLink *outlink)
463{
464 Frei0rContext *frei0r = outlink->src->priv;
7e350379 465 AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3825b526 466
7e350379 467 if (!frame)
8f3a3ce7
AK
468 return AVERROR(ENOMEM);
469
7e350379
AK
470 frame->sample_aspect_ratio = (AVRational) {1, 1};
471 frame->pts = frei0r->pts++;
f8608dca 472
7e350379
AK
473 frei0r->update(frei0r->instance, av_rescale_q(frame->pts, frei0r->time_base, (AVRational){1,1000}),
474 NULL, (uint32_t *)frame->data[0]);
f8608dca 475
7e350379 476 return ff_filter_frame(outlink, frame);
f8608dca
SS
477}
478
5aa1a668
AK
479static const AVOption src_options[] = {
480 { "size", "Dimensions of the generated video.", OFFSET(size), AV_OPT_TYPE_STRING, { .str = "" }, .flags = FLAGS },
481 { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
482 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
483 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
484 { NULL },
485};
486
487static const AVClass src_class = {
488 .class_name = "frei0r_src",
489 .item_name = av_default_item_name,
490 .option = src_options,
491 .version = LIBAVUTIL_VERSION_INT,
492};
493
568c70e7
MR
494static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
495 {
496 .name = "default",
497 .type = AVMEDIA_TYPE_VIDEO,
498 .request_frame = source_request_frame,
499 .config_props = source_config_props
500 },
501 { NULL }
502};
503
f8608dca
SS
504AVFilter avfilter_vsrc_frei0r_src = {
505 .name = "frei0r_src",
506 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
507
508 .priv_size = sizeof(Frei0rContext),
5aa1a668 509 .priv_class = &src_class,
f8608dca
SS
510 .init = source_init,
511 .uninit = uninit,
512
513 .query_formats = query_formats,
514
1fce361d 515 .inputs = NULL,
f8608dca 516
568c70e7 517 .outputs = avfilter_vsrc_frei0r_src_outputs,
f8608dca 518};