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"
31 #include "parseutils.h"
33 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
34 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
35 typedef void (*f0r_deinit_f)(void);
36 typedef int (*f0r_init_f)(void);
37 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
38 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
39 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
40 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);
41 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
42 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
44 typedef struct Frei0rContext {
46 void *dl_handle; /* dynamic library handle */
47 f0r_instance_t instance;
48 f0r_plugin_info_t plugin_info;
50 f0r_get_param_info_f get_param_info;
51 f0r_get_param_value_f get_param_value;
52 f0r_set_param_value_f set_param_value;
53 f0r_construct_f construct;
54 f0r_destruct_f destruct;
59 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
61 Frei0rContext *frei0r = ctx->priv;
62 void *sym = dlsym(frei0r->dl_handle, sym_name);
64 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
68 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
70 Frei0rContext *frei0r = ctx->priv;
73 f0r_param_color_t col;
74 f0r_param_position_t pos;
81 if (!strcmp(param, "y")) val.d = 1.0;
82 else if (!strcmp(param, "n")) val.d = 0.0;
86 case F0R_PARAM_DOUBLE:
87 val.d = strtod(param, &tail);
88 if (*tail || val.d == HUGE_VAL)
93 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
94 if (av_parse_color(rgba, param, ctx) < 0)
96 val.col.r = rgba[0] / 255.0;
97 val.col.g = rgba[1] / 255.0;
98 val.col.b = rgba[2] / 255.0;
102 case F0R_PARAM_POSITION:
103 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
108 frei0r->set_param_value(frei0r->instance, &val, index);
112 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
114 return AVERROR(EINVAL);
117 static int set_params(AVFilterContext *ctx, const char *params)
119 Frei0rContext *frei0r = ctx->priv;
122 for (i = 0; i < frei0r->plugin_info.num_params; i++) {
123 f0r_param_info_t info;
127 frei0r->get_param_info(&info, i);
130 if (!(param = av_get_token(¶ms, ":")))
131 return AVERROR(ENOMEM);
132 params++; /* skip ':' */
133 ret = set_param(ctx, info, i, param);
139 av_log(ctx, AV_LOG_INFO,
140 "idx:%d name:'%s' type:%s explanation:'%s' ",
142 info.type == F0R_PARAM_BOOL ? "bool" :
143 info.type == F0R_PARAM_DOUBLE ? "double" :
144 info.type == F0R_PARAM_COLOR ? "color" :
145 info.type == F0R_PARAM_POSITION ? "position" :
146 info.type == F0R_PARAM_STRING ? "string" : "unknown",
150 av_log(ctx, AV_LOG_INFO, "value:");
155 f0r_param_color_t col;
156 f0r_param_position_t pos;
160 frei0r->get_param_value(frei0r->instance, v, i);
161 av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
163 case F0R_PARAM_DOUBLE:
165 frei0r->get_param_value(frei0r->instance, v, i);
166 av_log(ctx, AV_LOG_INFO, "%f", d);
168 case F0R_PARAM_COLOR:
170 frei0r->get_param_value(frei0r->instance, v, i);
171 av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
173 case F0R_PARAM_POSITION:
175 frei0r->get_param_value(frei0r->instance, v, i);
176 av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
178 default: /* F0R_PARAM_STRING */
180 frei0r->get_param_value(frei0r->instance, v, i);
181 av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
185 av_log(ctx, AV_LOG_INFO, "\n");
191 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
195 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
196 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
197 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
200 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
202 Frei0rContext *frei0r = ctx->priv;
204 f0r_get_plugin_info_f f0r_get_plugin_info;
205 f0r_plugin_info_t *pi;
206 char dl_name[1024], *path;
211 sscanf(args, "%1023[^:]:%255c", dl_name, frei0r->params);
213 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
214 if ((path = av_strdup(getenv("FREI0R_PATH")))) {
215 char *p, *ptr = NULL;
216 for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
217 if (frei0r->dl_handle = load_path(ctx, p, dl_name))
221 if (!frei0r->dl_handle && (path = getenv("HOME"))) {
223 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
224 frei0r->dl_handle = load_path(ctx, prefix, dl_name);
226 if (!frei0r->dl_handle)
227 frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
228 if (!frei0r->dl_handle)
229 frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
230 if (!frei0r->dl_handle) {
231 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
232 return AVERROR(EINVAL);
235 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
236 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
237 !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
238 !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
239 !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
240 !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
241 !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
242 !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
243 !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
244 return AVERROR(EINVAL);
246 if (f0r_init() < 0) {
247 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
248 return AVERROR(EINVAL);
251 f0r_get_plugin_info(&frei0r->plugin_info);
252 pi = &frei0r->plugin_info;
253 if (pi->plugin_type != F0R_PLUGIN_TYPE_FILTER) {
254 av_log(ctx, AV_LOG_ERROR,
255 "Invalid type '%s' for the plugin, a filter plugin was expected\n",
256 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
257 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
258 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
259 return AVERROR(EINVAL);
262 av_log(ctx, AV_LOG_INFO,
263 "name:%s author:'%s' explanation:'%s' color_model:%s "
264 "frei0r_version:%d version:%d.%d num_params:%d\n",
265 pi->name, pi->author, pi->explanation,
266 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
267 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
268 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
269 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
274 static av_cold void uninit(AVFilterContext *ctx)
276 Frei0rContext *frei0r = ctx->priv;
278 if (frei0r->destruct)
279 frei0r->destruct(frei0r->instance);
282 if (frei0r->dl_handle)
283 dlclose(frei0r->dl_handle);
285 memset(frei0r, 0, sizeof(*frei0r));
288 static int config_input_props(AVFilterLink *inlink)
290 AVFilterContext *ctx = inlink->dst;
291 Frei0rContext *frei0r = ctx->priv;
293 if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
294 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
295 return AVERROR(EINVAL);
298 return set_params(ctx, frei0r->params);
301 static int query_formats(AVFilterContext *ctx)
303 Frei0rContext *frei0r = ctx->priv;
304 AVFilterFormats *formats = NULL;
306 if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
307 avfilter_add_format(&formats, PIX_FMT_BGRA);
308 } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
309 avfilter_add_format(&formats, PIX_FMT_RGBA);
310 } else { /* F0R_COLOR_MODEL_PACKED32 */
311 static const enum PixelFormat pix_fmts[] = {
312 PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
314 formats = avfilter_make_format_list(pix_fmts);
318 return AVERROR(ENOMEM);
320 avfilter_set_common_formats(ctx, formats);
324 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
326 static void end_frame(AVFilterLink *inlink)
328 Frei0rContext *frei0r = inlink->dst->priv;
329 AVFilterLink *outlink = inlink->dst->outputs[0];
330 AVFilterBufferRef *inpicref = inlink->cur_buf;
331 AVFilterBufferRef *outpicref = outlink->out_buf;
333 frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
334 (const uint32_t *)inpicref->data[0],
335 (uint32_t *)outpicref->data[0]);
336 avfilter_unref_buffer(inpicref);
337 avfilter_draw_slice(outlink, 0, outlink->h, 1);
338 avfilter_end_frame(outlink);
339 avfilter_unref_buffer(outpicref);
342 AVFilter avfilter_vf_frei0r = {
344 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
346 .query_formats = query_formats,
350 .priv_size = sizeof(Frei0rContext),
352 .inputs = (AVFilterPad[]) {{ .name = "default",
353 .type = AVMEDIA_TYPE_VIDEO,
354 .draw_slice = null_draw_slice,
355 .config_props = config_input_props,
356 .end_frame = end_frame,
357 .min_perms = AV_PERM_READ },
360 .outputs = (AVFilterPad[]) {{ .name = "default",
361 .type = AVMEDIA_TYPE_VIDEO, },