2 * Copyright (c) 2010 Stefano Sabatini
3 * This file is part of Libav.
5 * Libav 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 * Libav 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 Libav; 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/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/parseutils.h"
43 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
44 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
45 typedef void (*f0r_deinit_f)(void);
46 typedef int (*f0r_init_f)(void);
47 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
48 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
49 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
50 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);
51 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
52 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
54 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;
73 /* only used by the source */
79 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
81 Frei0rContext *s = ctx->priv;
82 void *sym = dlsym(s->dl_handle, sym_name);
84 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name);
88 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
90 Frei0rContext *s = ctx->priv;
93 f0r_param_color_t col;
94 f0r_param_position_t pos;
101 if (!strcmp(param, "y")) val.d = 1.0;
102 else if (!strcmp(param, "n")) val.d = 0.0;
106 case F0R_PARAM_DOUBLE:
107 val.d = strtod(param, &tail);
108 if (*tail || val.d == HUGE_VAL)
112 case F0R_PARAM_COLOR:
113 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
114 if (av_parse_color(rgba, param, -1, ctx) < 0)
116 val.col.r = rgba[0] / 255.0;
117 val.col.g = rgba[1] / 255.0;
118 val.col.b = rgba[2] / 255.0;
122 case F0R_PARAM_POSITION:
123 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
128 s->set_param_value(s->instance, &val, index);
132 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
134 return AVERROR(EINVAL);
137 static int set_params(AVFilterContext *ctx, const char *params)
139 Frei0rContext *s = ctx->priv;
142 for (i = 0; i < s->plugin_info.num_params; i++) {
143 f0r_param_info_t info;
147 s->get_param_info(&info, i);
150 if (!(param = av_get_token(¶ms, "|")))
151 return AVERROR(ENOMEM);
153 params++; /* skip ':' */
154 ret = set_param(ctx, info, i, param);
160 av_log(ctx, AV_LOG_VERBOSE,
161 "idx:%d name:'%s' type:%s explanation:'%s' ",
163 info.type == F0R_PARAM_BOOL ? "bool" :
164 info.type == F0R_PARAM_DOUBLE ? "double" :
165 info.type == F0R_PARAM_COLOR ? "color" :
166 info.type == F0R_PARAM_POSITION ? "position" :
167 info.type == F0R_PARAM_STRING ? "string" : "unknown",
171 av_log(ctx, AV_LOG_DEBUG, "value:");
176 f0r_param_color_t col;
177 f0r_param_position_t pos;
181 s->get_param_value(s->instance, v, i);
182 av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
184 case F0R_PARAM_DOUBLE:
186 s->get_param_value(s->instance, v, i);
187 av_log(ctx, AV_LOG_DEBUG, "%f", d);
189 case F0R_PARAM_COLOR:
191 s->get_param_value(s->instance, v, i);
192 av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
194 case F0R_PARAM_POSITION:
196 s->get_param_value(s->instance, v, i);
197 av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
199 default: /* F0R_PARAM_STRING */
201 s->get_param_value(s->instance, v, i);
202 av_log(ctx, AV_LOG_DEBUG, "'%s'", s);
206 av_log(ctx, AV_LOG_VERBOSE, ".\n");
212 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
216 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
217 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
218 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
221 static av_cold int frei0r_init(AVFilterContext *ctx,
222 const char *dl_name, int type)
224 Frei0rContext *s = ctx->priv;
226 f0r_get_plugin_info_f f0r_get_plugin_info;
227 f0r_plugin_info_t *pi;
231 av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
232 return AVERROR(EINVAL);
235 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
236 if (path = getenv("FREI0R_PATH")) {
238 char *ptr = av_get_token((const char **)&path, ":");
240 return AVERROR(ENOMEM);
241 s->dl_handle = load_path(ctx, ptr, dl_name);
246 path++; /* skip ':' */
249 if (!s->dl_handle && (path = getenv("HOME"))) {
251 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
252 s->dl_handle = load_path(ctx, prefix, dl_name);
255 s->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
257 s->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
259 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
260 return AVERROR(EINVAL);
263 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
264 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
265 !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
266 !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
267 !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
268 !(s->update = load_sym(ctx, "f0r_update" )) ||
269 !(s->construct = load_sym(ctx, "f0r_construct" )) ||
270 !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
271 !(s->deinit = load_sym(ctx, "f0r_deinit" )))
272 return AVERROR(EINVAL);
274 if (f0r_init() < 0) {
275 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
276 return AVERROR(EINVAL);
279 f0r_get_plugin_info(&s->plugin_info);
280 pi = &s->plugin_info;
281 if (pi->plugin_type != type) {
282 av_log(ctx, AV_LOG_ERROR,
283 "Invalid type '%s' for this plugin\n",
284 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
285 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
286 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
287 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
288 return AVERROR(EINVAL);
291 av_log(ctx, AV_LOG_VERBOSE,
292 "name:%s author:'%s' explanation:'%s' color_model:%s "
293 "frei0r_version:%d version:%d.%d num_params:%d\n",
294 pi->name, pi->author, pi->explanation,
295 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
296 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
297 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
298 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
303 static av_cold int filter_init(AVFilterContext *ctx)
305 Frei0rContext *s = ctx->priv;
307 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
310 static av_cold void uninit(AVFilterContext *ctx)
312 Frei0rContext *s = ctx->priv;
314 if (s->destruct && s->instance)
315 s->destruct(s->instance);
319 dlclose(s->dl_handle);
322 static int config_input_props(AVFilterLink *inlink)
324 AVFilterContext *ctx = inlink->dst;
325 Frei0rContext *s = ctx->priv;
327 if (s->destruct && s->instance)
328 s->destruct(s->instance);
329 if (!(s->instance = s->construct(inlink->w, inlink->h))) {
330 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
331 return AVERROR(EINVAL);
334 return set_params(ctx, s->params);
337 static int query_formats(AVFilterContext *ctx)
339 Frei0rContext *s = ctx->priv;
340 AVFilterFormats *formats = NULL;
342 if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
343 ff_add_format(&formats, AV_PIX_FMT_BGRA);
344 } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
345 ff_add_format(&formats, AV_PIX_FMT_RGBA);
346 } else { /* F0R_COLOR_MODEL_PACKED32 */
347 static const enum AVPixelFormat pix_fmts[] = {
348 AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
350 formats = ff_make_format_list(pix_fmts);
354 return AVERROR(ENOMEM);
356 ff_set_common_formats(ctx, formats);
360 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
362 Frei0rContext *s = inlink->dst->priv;
363 AVFilterLink *outlink = inlink->dst->outputs[0];
366 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
369 return AVERROR(ENOMEM);
371 av_frame_copy_props(out, in);
373 s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
374 (const uint32_t *)in->data[0],
375 (uint32_t *)out->data[0]);
379 return ff_filter_frame(outlink, out);
382 #define OFFSET(x) offsetof(Frei0rContext, x)
383 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
384 static const AVOption filter_options[] = {
385 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
386 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
390 static const AVClass filter_class = {
391 .class_name = "frei0r",
392 .item_name = av_default_item_name,
393 .option = filter_options,
394 .version = LIBAVUTIL_VERSION_INT,
397 static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
400 .type = AVMEDIA_TYPE_VIDEO,
401 .config_props = config_input_props,
402 .filter_frame = filter_frame,
407 static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
410 .type = AVMEDIA_TYPE_VIDEO,
415 AVFilter ff_vf_frei0r = {
417 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
419 .query_formats = query_formats,
423 .priv_size = sizeof(Frei0rContext),
424 .priv_class = &filter_class,
426 .inputs = avfilter_vf_frei0r_inputs,
428 .outputs = avfilter_vf_frei0r_outputs,
431 static av_cold int source_init(AVFilterContext *ctx)
433 Frei0rContext *s = ctx->priv;
434 AVRational frame_rate_q;
436 if (av_parse_video_size(&s->w, &s->h, s->size) < 0) {
437 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'.\n", s->size);
438 return AVERROR(EINVAL);
441 if (av_parse_video_rate(&frame_rate_q, s->framerate) < 0 ||
442 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
443 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'.\n", s->framerate);
444 return AVERROR(EINVAL);
446 s->time_base.num = frame_rate_q.den;
447 s->time_base.den = frame_rate_q.num;
449 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
452 static int source_config_props(AVFilterLink *outlink)
454 AVFilterContext *ctx = outlink->src;
455 Frei0rContext *s = ctx->priv;
457 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
458 return AVERROR(EINVAL);
461 outlink->time_base = s->time_base;
462 outlink->frame_rate = av_inv_q(s->time_base);
464 if (s->destruct && s->instance)
465 s->destruct(s->instance);
466 if (!(s->instance = s->construct(outlink->w, outlink->h))) {
467 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
468 return AVERROR(EINVAL);
471 av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
472 return AVERROR(EINVAL);
475 return set_params(ctx, s->params);
478 static int source_request_frame(AVFilterLink *outlink)
480 Frei0rContext *s = outlink->src->priv;
481 AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
484 return AVERROR(ENOMEM);
486 frame->sample_aspect_ratio = (AVRational) {1, 1};
487 frame->pts = s->pts++;
489 s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
490 NULL, (uint32_t *)frame->data[0]);
492 return ff_filter_frame(outlink, frame);
495 static const AVOption src_options[] = {
496 { "size", "Dimensions of the generated video.", OFFSET(size), AV_OPT_TYPE_STRING, { .str = "" }, .flags = FLAGS },
497 { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
498 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
499 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
503 static const AVClass src_class = {
504 .class_name = "frei0r_src",
505 .item_name = av_default_item_name,
506 .option = src_options,
507 .version = LIBAVUTIL_VERSION_INT,
510 static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
513 .type = AVMEDIA_TYPE_VIDEO,
514 .request_frame = source_request_frame,
515 .config_props = source_config_props
520 AVFilter ff_vsrc_frei0r_src = {
521 .name = "frei0r_src",
522 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
524 .priv_size = sizeof(Frei0rContext),
525 .priv_class = &src_class,
529 .query_formats = query_formats,
533 .outputs = avfilter_vsrc_frei0r_src_outputs,