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
29 #include "libavutil/avstring.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/parseutils.h"
38 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
39 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
40 typedef void (*f0r_deinit_f)(void);
41 typedef int (*f0r_init_f)(void);
42 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
43 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
44 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
45 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);
46 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
47 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
49 typedef struct Frei0rContext {
51 void *dl_handle; /* dynamic library handle */
52 f0r_instance_t instance;
53 f0r_plugin_info_t plugin_info;
55 f0r_get_param_info_f get_param_info;
56 f0r_get_param_value_f get_param_value;
57 f0r_set_param_value_f set_param_value;
58 f0r_construct_f construct;
59 f0r_destruct_f destruct;
63 /* only used by the source */
69 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
71 Frei0rContext *frei0r = ctx->priv;
72 void *sym = dlsym(frei0r->dl_handle, sym_name);
74 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
78 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
80 Frei0rContext *frei0r = ctx->priv;
83 f0r_param_color_t col;
84 f0r_param_position_t pos;
91 if (!strcmp(param, "y")) val.d = 1.0;
92 else if (!strcmp(param, "n")) val.d = 0.0;
96 case F0R_PARAM_DOUBLE:
97 val.d = strtod(param, &tail);
98 if (*tail || val.d == HUGE_VAL)
102 case F0R_PARAM_COLOR:
103 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
104 if (av_parse_color(rgba, param, -1, ctx) < 0)
106 val.col.r = rgba[0] / 255.0;
107 val.col.g = rgba[1] / 255.0;
108 val.col.b = rgba[2] / 255.0;
112 case F0R_PARAM_POSITION:
113 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
118 frei0r->set_param_value(frei0r->instance, &val, index);
122 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
124 return AVERROR(EINVAL);
127 static int set_params(AVFilterContext *ctx, const char *params)
129 Frei0rContext *frei0r = ctx->priv;
132 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
133 f0r_param_info_t info;
137 frei0r->get_param_info(&info, i);
140 if (!(param = av_get_token(¶ms, ":")))
141 return AVERROR(ENOMEM);
142 params++; /* skip ':' */
143 ret = set_param(ctx, info, i, param);
149 av_log(ctx, AV_LOG_VERBOSE,
150 "idx:%d name:'%s' type:%s explanation:'%s' ",
152 info.type == F0R_PARAM_BOOL ? "bool" :
153 info.type == F0R_PARAM_DOUBLE ? "double" :
154 info.type == F0R_PARAM_COLOR ? "color" :
155 info.type == F0R_PARAM_POSITION ? "position" :
156 info.type == F0R_PARAM_STRING ? "string" : "unknown",
160 av_log(ctx, AV_LOG_DEBUG, "value:");
165 f0r_param_color_t col;
166 f0r_param_position_t pos;
170 frei0r->get_param_value(frei0r->instance, v, i);
171 av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
173 case F0R_PARAM_DOUBLE:
175 frei0r->get_param_value(frei0r->instance, v, i);
176 av_log(ctx, AV_LOG_DEBUG, "%f", d);
178 case F0R_PARAM_COLOR:
180 frei0r->get_param_value(frei0r->instance, v, i);
181 av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
183 case F0R_PARAM_POSITION:
185 frei0r->get_param_value(frei0r->instance, v, i);
186 av_log(ctx, AV_LOG_DEBUG, "%lf/%lf", pos.x, pos.y);
188 default: /* F0R_PARAM_STRING */
190 frei0r->get_param_value(frei0r->instance, v, i);
191 av_log(ctx, AV_LOG_DEBUG, "'%s'\n", s);
195 av_log(ctx, AV_LOG_VERBOSE, "\n");
201 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
205 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
206 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
207 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
210 static av_cold int frei0r_init(AVFilterContext *ctx,
211 const char *dl_name, int type)
213 Frei0rContext *frei0r = ctx->priv;
215 f0r_get_plugin_info_f f0r_get_plugin_info;
216 f0r_plugin_info_t *pi;
219 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
220 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
221 char *p, *ptr = NULL;
222 for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
223 if (frei0r->dl_handle = load_path(ctx, p, dl_name))
227 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
229 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
230 frei0r->dl_handle = load_path(ctx, prefix, dl_name);
232 if (!frei0r->dl_handle)
233 frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
234 if (!frei0r->dl_handle)
235 frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
236 if (!frei0r->dl_handle) {
237 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
238 return AVERROR(EINVAL);
241 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
242 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
243 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
244 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
245 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
246 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
247 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
248 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
249 !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
250 return AVERROR(EINVAL);
252 if (f0r_init() < 0) {
253 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
254 return AVERROR(EINVAL);
257 f0r_get_plugin_info(&frei0r->plugin_info);
258 pi = &frei0r->plugin_info;
259 if (pi->plugin_type != type) {
260 av_log(ctx, AV_LOG_ERROR,
261 "Invalid type '%s' for the plugin\n",
262 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
263 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
264 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
265 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
266 return AVERROR(EINVAL);
269 av_log(ctx, AV_LOG_VERBOSE,
270 "name:%s author:'%s' explanation:'%s' color_model:%s "
271 "frei0r_version:%d version:%d.%d num_params:%d\n",
272 pi->name, pi->author, pi->explanation,
273 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
274 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
275 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
276 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
281 static av_cold int filter_init(AVFilterContext *ctx, const char *args)
283 Frei0rContext *frei0r = ctx->priv;
284 char dl_name[1024], c;
288 sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
290 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
293 static av_cold void uninit(AVFilterContext *ctx)
295 Frei0rContext *frei0r = ctx->priv;
297 if (frei0r->destruct && frei0r->instance)
298 frei0r->destruct(frei0r->instance);
301 if (frei0r->dl_handle)
302 dlclose(frei0r->dl_handle);
304 memset(frei0r, 0, sizeof(*frei0r));
307 static int config_input_props(AVFilterLink *inlink)
309 AVFilterContext *ctx = inlink->dst;
310 Frei0rContext *frei0r = ctx->priv;
312 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
313 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
314 return AVERROR(EINVAL);
317 return set_params(ctx, frei0r->params);
320 static int query_formats(AVFilterContext *ctx)
322 Frei0rContext *frei0r = ctx->priv;
323 AVFilterFormats *formats = NULL;
325 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
326 ff_add_format(&formats, PIX_FMT_BGRA);
327 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
328 ff_add_format(&formats, PIX_FMT_RGBA);
329 } else { /* F0R_COLOR_MODEL_PACKED32 */
330 static const enum PixelFormat pix_fmts[] = {
331 PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
333 formats = ff_make_format_list(pix_fmts);
337 return AVERROR(ENOMEM);
339 ff_set_common_formats(ctx, formats);
343 static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
348 static int end_frame(AVFilterLink *inlink)
350 Frei0rContext *frei0r = inlink->dst->priv;
351 AVFilterLink *outlink = inlink->dst->outputs[0];
352 AVFilterBufferRef *inpicref = inlink->cur_buf;
353 AVFilterBufferRef *outpicref = outlink->out_buf;
356 frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
357 (const uint32_t *)inpicref->data[0],
358 (uint32_t *)outpicref->data[0]);
359 if ((ret = ff_draw_slice(outlink, 0, outlink->h, 1)) ||
360 (ret = ff_end_frame(outlink)) < 0)
365 AVFilter avfilter_vf_frei0r = {
367 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
369 .query_formats = query_formats,
373 .priv_size = sizeof(Frei0rContext),
375 .inputs = (const AVFilterPad[]) {{ .name = "default",
376 .type = AVMEDIA_TYPE_VIDEO,
377 .draw_slice = null_draw_slice,
378 .config_props = config_input_props,
379 .end_frame = end_frame,
380 .min_perms = AV_PERM_READ },
383 .outputs = (const AVFilterPad[]) {{ .name = "default",
384 .type = AVMEDIA_TYPE_VIDEO, },
388 static av_cold int source_init(AVFilterContext *ctx, const char *args)
390 Frei0rContext *frei0r = ctx->priv;
391 char dl_name[1024], c;
392 char frame_size[128] = "";
393 char frame_rate[128] = "";
394 AVRational frame_rate_q;
396 memset(frei0r->params, 0, sizeof(frei0r->params));
399 sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
400 frame_size, frame_rate, dl_name, &c, frei0r->params);
402 if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
403 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
404 return AVERROR(EINVAL);
407 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0) {
408 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
409 return AVERROR(EINVAL);
411 frei0r->time_base.num = frame_rate_q.den;
412 frei0r->time_base.den = frame_rate_q.num;
414 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
417 static int source_config_props(AVFilterLink *outlink)
419 AVFilterContext *ctx = outlink->src;
420 Frei0rContext *frei0r = ctx->priv;
422 if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
423 return AVERROR(EINVAL);
424 outlink->w = frei0r->w;
425 outlink->h = frei0r->h;
426 outlink->time_base = frei0r->time_base;
427 outlink->sample_aspect_ratio = (AVRational){1,1};
429 if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
430 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
431 return AVERROR(EINVAL);
434 return set_params(ctx, frei0r->params);
437 static int source_request_frame(AVFilterLink *outlink)
439 Frei0rContext *frei0r = outlink->src->priv;
440 AVFilterBufferRef *picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
441 AVFilterBufferRef *buf_out;
445 return AVERROR(ENOMEM);
447 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
448 picref->pts = frei0r->pts++;
451 buf_out = avfilter_ref_buffer(picref, ~0);
453 ret = AVERROR(ENOMEM);
457 ret = ff_start_frame(outlink, buf_out);
461 frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
462 NULL, (uint32_t *)picref->data[0]);
463 ret = ff_draw_slice(outlink, 0, outlink->h, 1);
467 ret = ff_end_frame(outlink);
470 avfilter_unref_buffer(picref);
475 AVFilter avfilter_vsrc_frei0r_src = {
476 .name = "frei0r_src",
477 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
479 .priv_size = sizeof(Frei0rContext),
483 .query_formats = query_formats,
485 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
487 .outputs = (const AVFilterPad[]) {{ .name = "default",
488 .type = AVMEDIA_TYPE_VIDEO,
489 .request_frame = source_request_frame,
490 .config_props = source_config_props },