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/parseutils.h"
34 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
35 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
36 typedef void (*f0r_deinit_f)(void);
37 typedef int (*f0r_init_f)(void);
38 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
39 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
40 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
41 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);
42 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
43 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
45 typedef struct Frei0rContext {
47 void *dl_handle; /* dynamic library handle */
48 f0r_instance_t instance;
49 f0r_plugin_info_t plugin_info;
51 f0r_get_param_info_f get_param_info;
52 f0r_get_param_value_f get_param_value;
53 f0r_set_param_value_f set_param_value;
54 f0r_construct_f construct;
55 f0r_destruct_f destruct;
59 /* only used by the source */
65 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
67 Frei0rContext *frei0r = ctx->priv;
68 void *sym = dlsym(frei0r->dl_handle, sym_name);
70 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
74 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
76 Frei0rContext *frei0r = ctx->priv;
79 f0r_param_color_t col;
80 f0r_param_position_t pos;
87 if (!strcmp(param, "y")) val.d = 1.0;
88 else if (!strcmp(param, "n")) val.d = 0.0;
92 case F0R_PARAM_DOUBLE:
93 val.d = strtod(param, &tail);
94 if (*tail || val.d == HUGE_VAL)
99 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
100 if (av_parse_color(rgba, param, -1, ctx) < 0)
102 val.col.r = rgba[0] / 255.0;
103 val.col.g = rgba[1] / 255.0;
104 val.col.b = rgba[2] / 255.0;
108 case F0R_PARAM_POSITION:
109 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
114 frei0r->set_param_value(frei0r->instance, &val, index);
118 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
120 return AVERROR(EINVAL);
123 static int set_params(AVFilterContext *ctx, const char *params)
125 Frei0rContext *frei0r = ctx->priv;
128 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
129 f0r_param_info_t info;
133 frei0r->get_param_info(&info, i);
136 if (!(param = av_get_token(¶ms, ":")))
137 return AVERROR(ENOMEM);
138 params++; /* skip ':' */
139 ret = set_param(ctx, info, i, param);
145 av_log(ctx, AV_LOG_INFO,
146 "idx:%d name:'%s' type:%s explanation:'%s' ",
148 info.type == F0R_PARAM_BOOL ? "bool" :
149 info.type == F0R_PARAM_DOUBLE ? "double" :
150 info.type == F0R_PARAM_COLOR ? "color" :
151 info.type == F0R_PARAM_POSITION ? "position" :
152 info.type == F0R_PARAM_STRING ? "string" : "unknown",
156 av_log(ctx, AV_LOG_INFO, "value:");
161 f0r_param_color_t col;
162 f0r_param_position_t pos;
166 frei0r->get_param_value(frei0r->instance, v, i);
167 av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
169 case F0R_PARAM_DOUBLE:
171 frei0r->get_param_value(frei0r->instance, v, i);
172 av_log(ctx, AV_LOG_INFO, "%f", d);
174 case F0R_PARAM_COLOR:
176 frei0r->get_param_value(frei0r->instance, v, i);
177 av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
179 case F0R_PARAM_POSITION:
181 frei0r->get_param_value(frei0r->instance, v, i);
182 av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
184 default: /* F0R_PARAM_STRING */
186 frei0r->get_param_value(frei0r->instance, v, i);
187 av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
191 av_log(ctx, AV_LOG_INFO, "\n");
197 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
201 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
202 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
203 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
206 static av_cold int frei0r_init(AVFilterContext *ctx,
207 const char *dl_name, int type)
209 Frei0rContext *frei0r = ctx->priv;
211 f0r_get_plugin_info_f f0r_get_plugin_info;
212 f0r_plugin_info_t *pi;
215 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
216 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
217 char *p, *ptr = NULL;
218 for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
219 if (frei0r->dl_handle = load_path(ctx, p, dl_name))
223 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
225 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
226 frei0r->dl_handle = load_path(ctx, prefix, dl_name);
228 if (!frei0r->dl_handle)
229 frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
230 if (!frei0r->dl_handle)
231 frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
232 if (!frei0r->dl_handle) {
233 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
234 return AVERROR(EINVAL);
237 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
238 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
239 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
240 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
241 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
242 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
243 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
244 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
245 !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
246 return AVERROR(EINVAL);
248 if (f0r_init() < 0) {
249 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
250 return AVERROR(EINVAL);
253 f0r_get_plugin_info(&frei0r->plugin_info);
254 pi = &frei0r->plugin_info;
255 if (pi->plugin_type != type) {
256 av_log(ctx, AV_LOG_ERROR,
257 "Invalid type '%s' for the plugin\n",
258 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
259 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
260 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
261 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
262 return AVERROR(EINVAL);
265 av_log(ctx, AV_LOG_INFO,
266 "name:%s author:'%s' explanation:'%s' color_model:%s "
267 "frei0r_version:%d version:%d.%d num_params:%d\n",
268 pi->name, pi->author, pi->explanation,
269 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
270 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
271 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
272 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
277 static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
279 Frei0rContext *frei0r = ctx->priv;
280 char dl_name[1024], c;
284 sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
286 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
289 static av_cold void uninit(AVFilterContext *ctx)
291 Frei0rContext *frei0r = ctx->priv;
293 if (frei0r->destruct && frei0r->instance)
294 frei0r->destruct(frei0r->instance);
297 if (frei0r->dl_handle)
298 dlclose(frei0r->dl_handle);
300 memset(frei0r, 0, sizeof(*frei0r));
303 static int config_input_props(AVFilterLink *inlink)
305 AVFilterContext *ctx = inlink->dst;
306 Frei0rContext *frei0r = ctx->priv;
308 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
309 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
310 return AVERROR(EINVAL);
313 return set_params(ctx, frei0r->params);
316 static int query_formats(AVFilterContext *ctx)
318 Frei0rContext *frei0r = ctx->priv;
319 AVFilterFormats *formats = NULL;
321 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
322 avfilter_add_format(&formats, PIX_FMT_BGRA);
323 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
324 avfilter_add_format(&formats, PIX_FMT_RGBA);
325 } else { /* F0R_COLOR_MODEL_PACKED32 */
326 static const enum PixelFormat pix_fmts[] = {
327 PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
329 formats = avfilter_make_format_list(pix_fmts);
333 return AVERROR(ENOMEM);
335 avfilter_set_common_pixel_formats(ctx, formats);
339 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
341 static void end_frame(AVFilterLink *inlink)
343 Frei0rContext *frei0r = inlink->dst->priv;
344 AVFilterLink *outlink = inlink->dst->outputs[0];
345 AVFilterBufferRef *inpicref = inlink->cur_buf;
346 AVFilterBufferRef *outpicref = outlink->out_buf;
348 frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
349 (const uint32_t *)inpicref->data[0],
350 (uint32_t *)outpicref->data[0]);
351 avfilter_unref_buffer(inpicref);
352 avfilter_draw_slice(outlink, 0, outlink->h, 1);
353 avfilter_end_frame(outlink);
354 avfilter_unref_buffer(outpicref);
357 AVFilter avfilter_vf_frei0r = {
359 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
361 .query_formats = query_formats,
365 .priv_size = sizeof(Frei0rContext),
367 .inputs = (AVFilterPad[]) {{ .name = "default",
368 .type = AVMEDIA_TYPE_VIDEO,
369 .draw_slice = null_draw_slice,
370 .config_props = config_input_props,
371 .end_frame = end_frame,
372 .min_perms = AV_PERM_READ },
375 .outputs = (AVFilterPad[]) {{ .name = "default",
376 .type = AVMEDIA_TYPE_VIDEO, },
380 static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
382 Frei0rContext *frei0r = ctx->priv;
383 char dl_name[1024], c;
384 char frame_size[128] = "";
385 char frame_rate[128] = "";
386 AVRational frame_rate_q;
388 memset(frei0r->params, 0, sizeof(frei0r->params));
391 sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
392 frame_size, frame_rate, dl_name, &c, frei0r->params);
394 if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
395 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
396 return AVERROR(EINVAL);
399 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
400 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
401 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
402 return AVERROR(EINVAL);
404 frei0r->time_base.num = frame_rate_q.den;
405 frei0r->time_base.den = frame_rate_q.num;
407 return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
410 static int source_config_props(AVFilterLink *outlink)
412 AVFilterContext *ctx = outlink->src;
413 Frei0rContext *frei0r = ctx->priv;
415 if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
416 return AVERROR(EINVAL);
417 outlink->w = frei0r->w;
418 outlink->h = frei0r->h;
419 outlink->time_base = frei0r->time_base;
421 if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
422 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
423 return AVERROR(EINVAL);
426 return set_params(ctx, frei0r->params);
429 static int source_request_frame(AVFilterLink *outlink)
431 Frei0rContext *frei0r = outlink->src->priv;
432 AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
433 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
434 picref->pts = frei0r->pts++;
437 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
438 frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
439 NULL, (uint32_t *)picref->data[0]);
440 avfilter_draw_slice(outlink, 0, outlink->h, 1);
441 avfilter_end_frame(outlink);
442 avfilter_unref_buffer(picref);
447 AVFilter avfilter_vsrc_frei0r_src = {
448 .name = "frei0r_src",
449 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
451 .priv_size = sizeof(Frei0rContext),
455 .query_formats = query_formats,
457 .inputs = (AVFilterPad[]) {{ .name = NULL}},
459 .outputs = (AVFilterPad[]) {{ .name = "default",
460 .type = AVMEDIA_TYPE_VIDEO,
461 .request_frame = source_request_frame,
462 .config_props = source_config_props },