2 * Copyright (c) 2010 Stefano Sabatini
3 * This file is part of FFmpeg.
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.
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.
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
33 #include "libavutil/avstring.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/parseutils.h"
44 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
45 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
46 typedef void (*f0r_deinit_f)(void);
47 typedef int (*f0r_init_f)(void);
48 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
49 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
50 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
51 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);
52 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
53 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
55 typedef struct Frei0rContext {
57 void *dl_handle; /* dynamic library handle */
58 f0r_instance_t instance;
59 f0r_plugin_info_t plugin_info;
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;
69 /* only used by the source */
75 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
77 Frei0rContext *frei0r = ctx->priv;
78 void *sym = dlsym(frei0r->dl_handle, sym_name);
80 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
84 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
86 Frei0rContext *frei0r = ctx->priv;
89 f0r_param_color_t col;
90 f0r_param_position_t pos;
97 if (!strcmp(param, "y")) val.d = 1.0;
98 else if (!strcmp(param, "n")) val.d = 0.0;
102 case F0R_PARAM_DOUBLE:
103 val.d = strtod(param, &tail);
104 if (*tail || val.d == HUGE_VAL)
108 case F0R_PARAM_COLOR:
109 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
110 if (av_parse_color(rgba, param, -1, ctx) < 0)
112 val.col.r = rgba[0] / 255.0;
113 val.col.g = rgba[1] / 255.0;
114 val.col.b = rgba[2] / 255.0;
118 case F0R_PARAM_POSITION:
119 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
124 frei0r->set_param_value(frei0r->instance, &val, index);
128 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
130 return AVERROR(EINVAL);
133 static int set_params(AVFilterContext *ctx, const char *params)
135 Frei0rContext *frei0r = ctx->priv;
138 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
139 f0r_param_info_t info;
143 frei0r->get_param_info(&info, i);
146 if (!(param = av_get_token(¶ms, ":")))
147 return AVERROR(ENOMEM);
148 params++; /* skip ':' */
149 ret = set_param(ctx, info, i, param);
155 av_log(ctx, AV_LOG_VERBOSE,
156 "idx:%d name:'%s' type:%s explanation:'%s' ",
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",
166 av_log(ctx, AV_LOG_DEBUG, "value:");
171 f0r_param_color_t col;
172 f0r_param_position_t pos;
176 frei0r->get_param_value(frei0r->instance, v, i);
177 av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
179 case F0R_PARAM_DOUBLE:
181 frei0r->get_param_value(frei0r->instance, v, i);
182 av_log(ctx, AV_LOG_DEBUG, "%f", d);
184 case F0R_PARAM_COLOR:
186 frei0r->get_param_value(frei0r->instance, v, i);
187 av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
189 case F0R_PARAM_POSITION:
191 frei0r->get_param_value(frei0r->instance, v, i);
192 av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
194 default: /* F0R_PARAM_STRING */
196 frei0r->get_param_value(frei0r->instance, v, i);
197 av_log(ctx, AV_LOG_DEBUG, "'%s'\n", s);
201 av_log(ctx, AV_LOG_VERBOSE, "\n");
207 static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
209 char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
211 return AVERROR(ENOMEM);
212 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
213 *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
218 static av_cold int frei0r_init(AVFilterContext *ctx,
219 const char *dl_name, int type)
221 Frei0rContext *frei0r = ctx->priv;
223 f0r_get_plugin_info_f f0r_get_plugin_info;
224 f0r_plugin_info_t *pi;
228 /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
229 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
231 const char *separator = ";";
233 const char *separator = ":";
235 char *p, *ptr = NULL;
236 for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
237 /* add additional trailing slash in case it is missing */
238 char *p1 = av_asprintf("%s/", p);
240 ret = AVERROR(ENOMEM);
243 ret = load_path(ctx, &frei0r->dl_handle, p1, dl_name);
247 if (frei0r->dl_handle)
256 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
257 char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
259 return AVERROR(ENOMEM);
260 ret = load_path(ctx, &frei0r->dl_handle, prefix, dl_name);
265 if (!frei0r->dl_handle) {
266 ret = load_path(ctx, &frei0r->dl_handle, "/usr/local/lib/frei0r-1/", dl_name);
270 if (!frei0r->dl_handle) {
271 ret = load_path(ctx, &frei0r->dl_handle, "/usr/lib/frei0r-1/", dl_name);
275 if (!frei0r->dl_handle) {
276 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
277 return AVERROR(EINVAL);
280 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
281 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
282 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
283 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
284 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
285 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
286 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
287 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
288 !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
289 return AVERROR(EINVAL);
291 if (f0r_init() < 0) {
292 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module\n");
293 return AVERROR(EINVAL);
296 f0r_get_plugin_info(&frei0r->plugin_info);
297 pi = &frei0r->plugin_info;
298 if (pi->plugin_type != type) {
299 av_log(ctx, AV_LOG_ERROR,
300 "Invalid type '%s' for the plugin\n",
301 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
302 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
303 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
304 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
305 return AVERROR(EINVAL);
308 av_log(ctx, AV_LOG_VERBOSE,
309 "name:%s author:'%s' explanation:'%s' color_model:%s "
310 "frei0r_version:%d version:%d.%d num_params:%d\n",
311 pi->name, pi->author, pi->explanation,
312 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
313 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
314 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
315 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
320 static av_cold int filter_init(AVFilterContext *ctx, const char *args)
322 Frei0rContext *frei0r = ctx->priv;
323 char dl_name[1024], c;
327 sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
329 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
332 static av_cold void uninit(AVFilterContext *ctx)
334 Frei0rContext *frei0r = ctx->priv;
336 if (frei0r->destruct && frei0r->instance)
337 frei0r->destruct(frei0r->instance);
340 if (frei0r->dl_handle)
341 dlclose(frei0r->dl_handle);
343 memset(frei0r, 0, sizeof(*frei0r));
346 static int config_input_props(AVFilterLink *inlink)
348 AVFilterContext *ctx = inlink->dst;
349 Frei0rContext *frei0r = ctx->priv;
351 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
352 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance\n");
353 return AVERROR(EINVAL);
356 return set_params(ctx, frei0r->params);
359 static int query_formats(AVFilterContext *ctx)
361 Frei0rContext *frei0r = ctx->priv;
362 AVFilterFormats *formats = NULL;
364 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
365 ff_add_format(&formats, AV_PIX_FMT_BGRA);
366 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
367 ff_add_format(&formats, AV_PIX_FMT_RGBA);
368 } else { /* F0R_COLOR_MODEL_PACKED32 */
369 static const enum AVPixelFormat pix_fmts[] = {
370 AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
372 formats = ff_make_format_list(pix_fmts);
376 return AVERROR(ENOMEM);
378 ff_set_common_formats(ctx, formats);
382 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
384 Frei0rContext *frei0r = inlink->dst->priv;
385 AVFilterLink *outlink = inlink->dst->outputs[0];
386 AVFilterBufferRef *out;
388 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
390 avfilter_unref_bufferp(&in);
391 return AVERROR(ENOMEM);
393 avfilter_copy_buffer_ref_props(out, in);
395 frei0r->update(frei0r->instance, in->pts * av_q2d(inlink->time_base) * 1000,
396 (const uint32_t *)in->data[0],
397 (uint32_t *)out->data[0]);
399 avfilter_unref_bufferp(&in);
401 return ff_filter_frame(outlink, out);
404 static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
407 .type = AVMEDIA_TYPE_VIDEO,
408 .config_props = config_input_props,
409 .filter_frame = filter_frame,
410 .min_perms = AV_PERM_READ
415 static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
418 .type = AVMEDIA_TYPE_VIDEO,
423 AVFilter avfilter_vf_frei0r = {
425 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
427 .query_formats = query_formats,
431 .priv_size = sizeof(Frei0rContext),
433 .inputs = avfilter_vf_frei0r_inputs,
435 .outputs = avfilter_vf_frei0r_outputs,
438 static av_cold int source_init(AVFilterContext *ctx, const char *args)
440 Frei0rContext *frei0r = ctx->priv;
441 char dl_name[1024], c;
442 char frame_size[128] = "";
443 char frame_rate[128] = "";
444 AVRational frame_rate_q;
446 memset(frei0r->params, 0, sizeof(frei0r->params));
449 sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
450 frame_size, frame_rate, dl_name, &c, frei0r->params);
452 if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
453 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
454 return AVERROR(EINVAL);
457 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0) {
458 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
459 return AVERROR(EINVAL);
461 frei0r->time_base.num = frame_rate_q.den;
462 frei0r->time_base.den = frame_rate_q.num;
464 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
467 static int source_config_props(AVFilterLink *outlink)
469 AVFilterContext *ctx = outlink->src;
470 Frei0rContext *frei0r = ctx->priv;
472 if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
473 return AVERROR(EINVAL);
474 outlink->w = frei0r->w;
475 outlink->h = frei0r->h;
476 outlink->time_base = frei0r->time_base;
477 outlink->sample_aspect_ratio = (AVRational){1,1};
479 if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
480 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance\n");
481 return AVERROR(EINVAL);
484 return set_params(ctx, frei0r->params);
487 static int source_request_frame(AVFilterLink *outlink)
489 Frei0rContext *frei0r = outlink->src->priv;
490 AVFilterBufferRef *picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
493 return AVERROR(ENOMEM);
495 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
496 picref->pts = frei0r->pts++;
499 frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
500 NULL, (uint32_t *)picref->data[0]);
502 return ff_filter_frame(outlink, picref);
505 static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
508 .type = AVMEDIA_TYPE_VIDEO,
509 .request_frame = source_request_frame,
510 .config_props = source_config_props
515 AVFilter avfilter_vsrc_frei0r_src = {
516 .name = "frei0r_src",
517 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
519 .priv_size = sizeof(Frei0rContext),
523 .query_formats = query_formats,
527 .outputs = avfilter_vsrc_frei0r_src_outputs,