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
29 #include "libavutil/avstring.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/parseutils.h"
36 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
37 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
38 typedef void (*f0r_deinit_f)(void);
39 typedef int (*f0r_init_f)(void);
40 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
41 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
42 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
43 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);
44 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
45 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
47 typedef struct Frei0rContext {
49 void *dl_handle; /* dynamic library handle */
50 f0r_instance_t instance;
51 f0r_plugin_info_t plugin_info;
53 f0r_get_param_info_f get_param_info;
54 f0r_get_param_value_f get_param_value;
55 f0r_set_param_value_f set_param_value;
56 f0r_construct_f construct;
57 f0r_destruct_f destruct;
61 /* only used by the source */
67 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
69 Frei0rContext *frei0r = ctx->priv;
70 void *sym = dlsym(frei0r->dl_handle, sym_name);
72 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
76 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
78 Frei0rContext *frei0r = ctx->priv;
81 f0r_param_color_t col;
82 f0r_param_position_t pos;
89 if (!strcmp(param, "y")) val.d = 1.0;
90 else if (!strcmp(param, "n")) val.d = 0.0;
94 case F0R_PARAM_DOUBLE:
95 val.d = strtod(param, &tail);
96 if (*tail || val.d == HUGE_VAL)
100 case F0R_PARAM_COLOR:
101 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
102 if (av_parse_color(rgba, param, -1, ctx) < 0)
104 val.col.r = rgba[0] / 255.0;
105 val.col.g = rgba[1] / 255.0;
106 val.col.b = rgba[2] / 255.0;
110 case F0R_PARAM_POSITION:
111 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
116 frei0r->set_param_value(frei0r->instance, &val, index);
120 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
122 return AVERROR(EINVAL);
125 static int set_params(AVFilterContext *ctx, const char *params)
127 Frei0rContext *frei0r = ctx->priv;
130 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
131 f0r_param_info_t info;
135 frei0r->get_param_info(&info, i);
138 if (!(param = av_get_token(¶ms, ":")))
139 return AVERROR(ENOMEM);
140 params++; /* skip ':' */
141 ret = set_param(ctx, info, i, param);
147 av_log(ctx, AV_LOG_INFO,
148 "idx:%d name:'%s' type:%s explanation:'%s' ",
150 info.type == F0R_PARAM_BOOL ? "bool" :
151 info.type == F0R_PARAM_DOUBLE ? "double" :
152 info.type == F0R_PARAM_COLOR ? "color" :
153 info.type == F0R_PARAM_POSITION ? "position" :
154 info.type == F0R_PARAM_STRING ? "string" : "unknown",
158 av_log(ctx, AV_LOG_INFO, "value:");
163 f0r_param_color_t col;
164 f0r_param_position_t pos;
168 frei0r->get_param_value(frei0r->instance, v, i);
169 av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
171 case F0R_PARAM_DOUBLE:
173 frei0r->get_param_value(frei0r->instance, v, i);
174 av_log(ctx, AV_LOG_INFO, "%f", d);
176 case F0R_PARAM_COLOR:
178 frei0r->get_param_value(frei0r->instance, v, i);
179 av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
181 case F0R_PARAM_POSITION:
183 frei0r->get_param_value(frei0r->instance, v, i);
184 av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
186 default: /* F0R_PARAM_STRING */
188 frei0r->get_param_value(frei0r->instance, v, i);
189 av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
193 av_log(ctx, AV_LOG_INFO, "\n");
199 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
203 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
204 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
205 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
208 static av_cold int frei0r_init(AVFilterContext *ctx,
209 const char *dl_name, int type)
211 Frei0rContext *frei0r = ctx->priv;
213 f0r_get_plugin_info_f f0r_get_plugin_info;
214 f0r_plugin_info_t *pi;
217 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
218 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
219 char *p, *ptr = NULL;
220 for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
221 if (frei0r->dl_handle = load_path(ctx, p, dl_name))
225 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
227 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
228 frei0r->dl_handle = load_path(ctx, prefix, dl_name);
230 if (!frei0r->dl_handle)
231 frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
232 if (!frei0r->dl_handle)
233 frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
234 if (!frei0r->dl_handle) {
235 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
236 return AVERROR(EINVAL);
239 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
240 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
241 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
242 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
243 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
244 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
245 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
246 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
247 !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
248 return AVERROR(EINVAL);
250 if (f0r_init() < 0) {
251 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
252 return AVERROR(EINVAL);
255 f0r_get_plugin_info(&frei0r->plugin_info);
256 pi = &frei0r->plugin_info;
257 if (pi->plugin_type != type) {
258 av_log(ctx, AV_LOG_ERROR,
259 "Invalid type '%s' for the plugin\n",
260 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
261 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
262 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
263 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
264 return AVERROR(EINVAL);
267 av_log(ctx, AV_LOG_INFO,
268 "name:%s author:'%s' explanation:'%s' color_model:%s "
269 "frei0r_version:%d version:%d.%d num_params:%d\n",
270 pi->name, pi->author, pi->explanation,
271 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
272 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
273 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
274 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
279 static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
281 Frei0rContext *frei0r = ctx->priv;
282 char dl_name[1024], c;
286 sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
288 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
291 static av_cold void uninit(AVFilterContext *ctx)
293 Frei0rContext *frei0r = ctx->priv;
295 if (frei0r->destruct && frei0r->instance)
296 frei0r->destruct(frei0r->instance);
299 if (frei0r->dl_handle)
300 dlclose(frei0r->dl_handle);
302 memset(frei0r, 0, sizeof(*frei0r));
305 static int config_input_props(AVFilterLink *inlink)
307 AVFilterContext *ctx = inlink->dst;
308 Frei0rContext *frei0r = ctx->priv;
310 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
311 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
312 return AVERROR(EINVAL);
315 return set_params(ctx, frei0r->params);
318 static int query_formats(AVFilterContext *ctx)
320 Frei0rContext *frei0r = ctx->priv;
321 AVFilterFormats *formats = NULL;
323 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
324 ff_add_format(&formats, PIX_FMT_BGRA);
325 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
326 ff_add_format(&formats, PIX_FMT_RGBA);
327 } else { /* F0R_COLOR_MODEL_PACKED32 */
328 static const enum PixelFormat pix_fmts[] = {
329 PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
331 formats = ff_make_format_list(pix_fmts);
335 return AVERROR(ENOMEM);
337 ff_set_common_formats(ctx, formats);
341 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
343 static void end_frame(AVFilterLink *inlink)
345 Frei0rContext *frei0r = inlink->dst->priv;
346 AVFilterLink *outlink = inlink->dst->outputs[0];
347 AVFilterBufferRef *inpicref = inlink->cur_buf;
348 AVFilterBufferRef *outpicref = outlink->out_buf;
350 frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
351 (const uint32_t *)inpicref->data[0],
352 (uint32_t *)outpicref->data[0]);
353 avfilter_unref_buffer(inpicref);
354 avfilter_draw_slice(outlink, 0, outlink->h, 1);
355 avfilter_end_frame(outlink);
356 avfilter_unref_buffer(outpicref);
359 AVFilter avfilter_vf_frei0r = {
361 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
363 .query_formats = query_formats,
367 .priv_size = sizeof(Frei0rContext),
369 .inputs = (AVFilterPad[]) {{ .name = "default",
370 .type = AVMEDIA_TYPE_VIDEO,
371 .draw_slice = null_draw_slice,
372 .config_props = config_input_props,
373 .end_frame = end_frame,
374 .min_perms = AV_PERM_READ },
377 .outputs = (AVFilterPad[]) {{ .name = "default",
378 .type = AVMEDIA_TYPE_VIDEO, },
382 static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
384 Frei0rContext *frei0r = ctx->priv;
385 char dl_name[1024], c;
386 char frame_size[128] = "";
387 char frame_rate[128] = "";
388 AVRational frame_rate_q;
390 memset(frei0r->params, 0, sizeof(frei0r->params));
393 sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
394 frame_size, frame_rate, dl_name, &c, frei0r->params);
396 if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
397 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
398 return AVERROR(EINVAL);
401 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
402 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
403 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
404 return AVERROR(EINVAL);
406 frei0r->time_base.num = frame_rate_q.den;
407 frei0r->time_base.den = frame_rate_q.num;
409 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
412 static int source_config_props(AVFilterLink *outlink)
414 AVFilterContext *ctx = outlink->src;
415 Frei0rContext *frei0r = ctx->priv;
417 if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
418 return AVERROR(EINVAL);
419 outlink->w = frei0r->w;
420 outlink->h = frei0r->h;
421 outlink->time_base = frei0r->time_base;
423 if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
424 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
425 return AVERROR(EINVAL);
428 return set_params(ctx, frei0r->params);
431 static int source_request_frame(AVFilterLink *outlink)
433 Frei0rContext *frei0r = outlink->src->priv;
434 AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
435 picref->video->pixel_aspect = (AVRational) {1, 1};
436 picref->pts = frei0r->pts++;
439 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
440 frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
441 NULL, (uint32_t *)picref->data[0]);
442 avfilter_draw_slice(outlink, 0, outlink->h, 1);
443 avfilter_end_frame(outlink);
444 avfilter_unref_buffer(picref);
449 AVFilter avfilter_vsrc_frei0r_src = {
450 .name = "frei0r_src",
451 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
453 .priv_size = sizeof(Frei0rContext),
457 .query_formats = query_formats,
459 .inputs = (AVFilterPad[]) {{ .name = NULL}},
461 .outputs = (AVFilterPad[]) {{ .name = "default",
462 .type = AVMEDIA_TYPE_VIDEO,
463 .request_frame = source_request_frame,
464 .config_props = source_config_props },