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
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"
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);
56 typedef struct Frei0rContext {
59 void *dl_handle; /* dynamic library handle */
60 f0r_instance_t instance;
61 f0r_plugin_info_t plugin_info;
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;
74 /* only used by the source */
80 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
82 Frei0rContext *s = ctx->priv;
83 void *sym = dlsym(s->dl_handle, sym_name);
85 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name);
89 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
91 Frei0rContext *s = ctx->priv;
94 f0r_param_color_t col;
95 f0r_param_position_t pos;
103 if (!strcmp(param, "y")) val.d = 1.0;
104 else if (!strcmp(param, "n")) val.d = 0.0;
108 case F0R_PARAM_DOUBLE:
109 val.d = av_strtod(param, &tail);
110 if (*tail || val.d == HUGE_VAL)
114 case F0R_PARAM_COLOR:
115 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
116 if (av_parse_color(rgba, param, -1, ctx) < 0)
118 val.col.r = rgba[0] / 255.0;
119 val.col.g = rgba[1] / 255.0;
120 val.col.b = rgba[2] / 255.0;
124 case F0R_PARAM_POSITION:
125 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
129 case F0R_PARAM_STRING:
134 s->set_param_value(s->instance, &val, index);
138 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
140 return AVERROR(EINVAL);
143 static int set_params(AVFilterContext *ctx, const char *params)
145 Frei0rContext *s = ctx->priv;
151 for (i = 0; i < s->plugin_info.num_params; i++) {
152 f0r_param_info_t info;
156 s->get_param_info(&info, i);
159 if (!(param = av_get_token(¶ms, "|")))
160 return AVERROR(ENOMEM);
162 params++; /* skip ':' */
163 ret = set_param(ctx, info, i, param);
173 static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
175 char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
177 return AVERROR(ENOMEM);
178 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
179 *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
184 static av_cold int frei0r_init(AVFilterContext *ctx,
185 const char *dl_name, int type)
187 Frei0rContext *s = ctx->priv;
189 f0r_get_plugin_info_f f0r_get_plugin_info;
190 f0r_plugin_info_t *pi;
194 static const char* const frei0r_pathlist[] = {
195 "/usr/local/lib/frei0r-1/",
196 "/usr/lib/frei0r-1/",
197 "/usr/local/lib64/frei0r-1/",
198 "/usr/lib64/frei0r-1/"
202 av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
203 return AVERROR(EINVAL);
206 /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
207 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
209 const char *separator = ";";
211 const char *separator = ":";
213 char *p, *ptr = NULL;
214 for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
215 /* add additional trailing slash in case it is missing */
216 char *p1 = av_asprintf("%s/", p);
218 ret = AVERROR(ENOMEM);
221 ret = load_path(ctx, &s->dl_handle, p1, dl_name);
234 if (!s->dl_handle && (path = getenv("HOME"))) {
235 char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
237 return AVERROR(ENOMEM);
238 ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
243 for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
244 ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name);
249 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
250 return AVERROR(EINVAL);
253 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
254 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
255 !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
256 !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
257 !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
258 !(s->update = load_sym(ctx, "f0r_update" )) ||
259 !(s->construct = load_sym(ctx, "f0r_construct" )) ||
260 !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
261 !(s->deinit = load_sym(ctx, "f0r_deinit" )))
262 return AVERROR(EINVAL);
264 if (f0r_init() < 0) {
265 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
266 return AVERROR(EINVAL);
269 f0r_get_plugin_info(&s->plugin_info);
270 pi = &s->plugin_info;
271 if (pi->plugin_type != type) {
272 av_log(ctx, AV_LOG_ERROR,
273 "Invalid type '%s' for this plugin\n",
274 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
275 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
276 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
277 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
278 return AVERROR(EINVAL);
281 av_log(ctx, AV_LOG_VERBOSE,
282 "name:%s author:'%s' explanation:'%s' color_model:%s "
283 "frei0r_version:%d version:%d.%d num_params:%d\n",
284 pi->name, pi->author, pi->explanation,
285 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
286 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
287 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
288 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
293 static av_cold int filter_init(AVFilterContext *ctx)
295 Frei0rContext *s = ctx->priv;
297 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
300 static av_cold void uninit(AVFilterContext *ctx)
302 Frei0rContext *s = ctx->priv;
304 if (s->destruct && s->instance)
305 s->destruct(s->instance);
309 dlclose(s->dl_handle);
312 static int config_input_props(AVFilterLink *inlink)
314 AVFilterContext *ctx = inlink->dst;
315 Frei0rContext *s = ctx->priv;
317 if (s->destruct && s->instance)
318 s->destruct(s->instance);
319 if (!(s->instance = s->construct(inlink->w, inlink->h))) {
320 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
321 return AVERROR(EINVAL);
324 return set_params(ctx, s->params);
327 static int query_formats(AVFilterContext *ctx)
329 Frei0rContext *s = ctx->priv;
330 AVFilterFormats *formats = NULL;
333 if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
334 if ((ret = ff_add_format(&formats, AV_PIX_FMT_BGRA)) < 0)
336 } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
337 if ((ret = ff_add_format(&formats, AV_PIX_FMT_RGBA)) < 0)
339 } else { /* F0R_COLOR_MODEL_PACKED32 */
340 static const enum AVPixelFormat pix_fmts[] = {
341 AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_NONE
343 formats = ff_make_format_list(pix_fmts);
347 return AVERROR(ENOMEM);
349 return ff_set_common_formats(ctx, formats);
352 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
354 Frei0rContext *s = inlink->dst->priv;
355 AVFilterLink *outlink = inlink->dst->outputs[0];
358 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
361 return AVERROR(ENOMEM);
363 av_frame_copy_props(out, in);
365 s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
366 (const uint32_t *)in->data[0],
367 (uint32_t *)out->data[0]);
371 return ff_filter_frame(outlink, out);
374 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
375 char *res, int res_len, int flags)
377 Frei0rContext *s = ctx->priv;
380 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
384 return set_params(ctx, s->params);
387 #define OFFSET(x) offsetof(Frei0rContext, x)
388 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
389 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
390 static const AVOption frei0r_options[] = {
391 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
392 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = TFLAGS },
396 AVFILTER_DEFINE_CLASS(frei0r);
398 static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
401 .type = AVMEDIA_TYPE_VIDEO,
402 .config_props = config_input_props,
403 .filter_frame = filter_frame,
408 static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
411 .type = AVMEDIA_TYPE_VIDEO,
416 const AVFilter ff_vf_frei0r = {
418 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
419 .query_formats = query_formats,
422 .priv_size = sizeof(Frei0rContext),
423 .priv_class = &frei0r_class,
424 .inputs = avfilter_vf_frei0r_inputs,
425 .outputs = avfilter_vf_frei0r_outputs,
426 .process_command = process_command,
427 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
430 static av_cold int source_init(AVFilterContext *ctx)
432 Frei0rContext *s = ctx->priv;
434 s->time_base.num = s->framerate.den;
435 s->time_base.den = s->framerate.num;
437 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
440 static int source_config_props(AVFilterLink *outlink)
442 AVFilterContext *ctx = outlink->src;
443 Frei0rContext *s = ctx->priv;
445 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
446 return AVERROR(EINVAL);
449 outlink->time_base = s->time_base;
450 outlink->frame_rate = av_inv_q(s->time_base);
451 outlink->sample_aspect_ratio = (AVRational){1,1};
453 if (s->destruct && s->instance)
454 s->destruct(s->instance);
455 if (!(s->instance = s->construct(outlink->w, outlink->h))) {
456 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
457 return AVERROR(EINVAL);
460 av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
461 return AVERROR(EINVAL);
464 return set_params(ctx, s->params);
467 static int source_request_frame(AVFilterLink *outlink)
469 Frei0rContext *s = outlink->src->priv;
470 AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
473 return AVERROR(ENOMEM);
475 frame->sample_aspect_ratio = (AVRational) {1, 1};
476 frame->pts = s->pts++;
478 s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
479 NULL, (uint32_t *)frame->data[0]);
481 return ff_filter_frame(outlink, frame);
484 static const AVOption frei0r_src_options[] = {
485 { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
486 { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, .flags = FLAGS },
487 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
488 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
492 AVFILTER_DEFINE_CLASS(frei0r_src);
494 static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
497 .type = AVMEDIA_TYPE_VIDEO,
498 .request_frame = source_request_frame,
499 .config_props = source_config_props
504 const AVFilter ff_vsrc_frei0r_src = {
505 .name = "frei0r_src",
506 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
507 .priv_size = sizeof(Frei0rContext),
508 .priv_class = &frei0r_src_class,
511 .query_formats = query_formats,
513 .outputs = avfilter_vsrc_frei0r_src_outputs,