47941088 |
/* |
3fa77bde |
* Copyright (c) 2010 Stefano Sabatini |
47941088 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* frei0r wrapper
*/
#include <dlfcn.h>
#include <frei0r.h> |
7ebe3962 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h" |
372e2884 |
#include "libavutil/avstring.h" |
ead0dd3c |
#include "libavutil/common.h" |
7ffe76e5 |
#include "libavutil/imgutils.h" |
7ebe3962 |
#include "libavutil/internal.h" |
d3f751e6 |
#include "libavutil/mathematics.h" |
7ebe3962 |
#include "libavutil/mem.h" |
5aa1a668 |
#include "libavutil/opt.h" |
7ffe76e5 |
#include "libavutil/parseutils.h" |
47941088 |
#include "avfilter.h" |
b74a1da4 |
#include "formats.h" |
9d0bfc50 |
#include "internal.h" |
803391f7 |
#include "video.h" |
47941088 |
typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
typedef void (*f0r_destruct_f)(f0r_instance_t instance);
typedef void (*f0r_deinit_f)(void);
typedef int (*f0r_init_f)(void);
typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
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);
typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
typedef struct Frei0rContext { |
5aa1a668 |
const AVClass *class; |
47941088 |
f0r_update_f update;
void *dl_handle; /* dynamic library handle */
f0r_instance_t instance;
f0r_plugin_info_t plugin_info;
f0r_get_param_info_f get_param_info;
f0r_get_param_value_f get_param_value;
f0r_set_param_value_f set_param_value;
f0r_construct_f construct;
f0r_destruct_f destruct;
f0r_deinit_f deinit; |
5aa1a668 |
char *dl_name;
char *params; |
7ac736af |
AVRational framerate; |
f8608dca |
/* only used by the source */
int w, h;
AVRational time_base;
uint64_t pts; |
47941088 |
} Frei0rContext;
static void *load_sym(AVFilterContext *ctx, const char *sym_name)
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv;
void *sym = dlsym(s->dl_handle, sym_name); |
47941088 |
if (!sym) |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name); |
47941088 |
return sym;
}
static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
47941088 |
union {
double d;
f0r_param_color_t col;
f0r_param_position_t pos;
} val;
char *tail;
uint8_t rgba[4];
switch (info.type) {
case F0R_PARAM_BOOL:
if (!strcmp(param, "y")) val.d = 1.0;
else if (!strcmp(param, "n")) val.d = 0.0;
else goto fail;
break;
case F0R_PARAM_DOUBLE:
val.d = strtod(param, &tail);
if (*tail || val.d == HUGE_VAL)
goto fail;
break;
case F0R_PARAM_COLOR:
if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) { |
f6c182a0 |
if (av_parse_color(rgba, param, -1, ctx) < 0) |
47941088 |
goto fail;
val.col.r = rgba[0] / 255.0;
val.col.g = rgba[1] / 255.0;
val.col.b = rgba[2] / 255.0;
}
break;
case F0R_PARAM_POSITION:
if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
goto fail;
break;
}
|
f6b6d6ac |
s->set_param_value(s->instance, &val, index); |
47941088 |
return 0;
fail: |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n", |
47941088 |
param, info.name);
return AVERROR(EINVAL);
}
static int set_params(AVFilterContext *ctx, const char *params)
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
47941088 |
int i;
|
e85ea7d3 |
if (!params)
return 0;
|
f6b6d6ac |
for (i = 0; i < s->plugin_info.num_params; i++) { |
47941088 |
f0r_param_info_t info;
char *param;
int ret;
|
f6b6d6ac |
s->get_param_info(&info, i); |
47941088 |
if (*params) { |
5aa1a668 |
if (!(param = av_get_token(¶ms, "|"))) |
47941088 |
return AVERROR(ENOMEM); |
02a6ee51 |
if (*params)
params++; /* skip ':' */ |
47941088 |
ret = set_param(ctx, info, i, param);
av_free(param);
if (ret < 0)
return ret;
}
|
1a49a169 |
av_log(ctx, AV_LOG_VERBOSE, |
47941088 |
"idx:%d name:'%s' type:%s explanation:'%s' ",
i, info.name,
info.type == F0R_PARAM_BOOL ? "bool" :
info.type == F0R_PARAM_DOUBLE ? "double" :
info.type == F0R_PARAM_COLOR ? "color" :
info.type == F0R_PARAM_POSITION ? "position" :
info.type == F0R_PARAM_STRING ? "string" : "unknown",
info.explanation);
#ifdef DEBUG |
1a49a169 |
av_log(ctx, AV_LOG_DEBUG, "value:"); |
47941088 |
switch (info.type) {
void *v;
double d; |
e0623953 |
char str[128]; |
47941088 |
f0r_param_color_t col;
f0r_param_position_t pos;
case F0R_PARAM_BOOL:
v = &d; |
f6b6d6ac |
s->get_param_value(s->instance, v, i); |
1a49a169 |
av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n"); |
47941088 |
break;
case F0R_PARAM_DOUBLE:
v = &d; |
f6b6d6ac |
s->get_param_value(s->instance, v, i); |
1a49a169 |
av_log(ctx, AV_LOG_DEBUG, "%f", d); |
47941088 |
break;
case F0R_PARAM_COLOR:
v = &col; |
f6b6d6ac |
s->get_param_value(s->instance, v, i); |
1a49a169 |
av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b); |
47941088 |
break;
case F0R_PARAM_POSITION:
v = &pos; |
f6b6d6ac |
s->get_param_value(s->instance, v, i); |
bcb8d9eb |
av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y); |
47941088 |
break;
default: /* F0R_PARAM_STRING */ |
e0623953 |
v = str; |
f6b6d6ac |
s->get_param_value(s->instance, v, i); |
e0623953 |
av_log(ctx, AV_LOG_DEBUG, "'%s'", str); |
47941088 |
break;
}
#endif |
8accddeb |
av_log(ctx, AV_LOG_VERBOSE, ".\n"); |
47941088 |
}
return 0;
}
|
6c7ae493 |
static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name) |
47941088 |
{ |
6c7ae493 |
char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
if (!path)
return AVERROR(ENOMEM); |
8accddeb |
av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path); |
6c7ae493 |
*handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
av_free(path);
return 0; |
47941088 |
}
|
f8608dca |
static av_cold int frei0r_init(AVFilterContext *ctx,
const char *dl_name, int type) |
47941088 |
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
47941088 |
f0r_init_f f0r_init;
f0r_get_plugin_info_f f0r_get_plugin_info;
f0r_plugin_info_t *pi; |
f8608dca |
char *path; |
6c7ae493 |
int ret = 0; |
ead0dd3c |
int i;
static const char* const frei0r_pathlist[] = {
"/usr/local/lib/frei0r-1/",
"/usr/lib/frei0r-1/",
"/usr/local/lib64/frei0r-1/",
"/usr/lib64/frei0r-1/"
}; |
47941088 |
|
5aa1a668 |
if (!dl_name) {
av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
return AVERROR(EINVAL);
}
|
7691860c |
/* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */ |
47941088 |
if ((path = av_strdup(getenv("FREI0R_PATH")))) { |
c1804dc4 |
#ifdef _WIN32
const char *separator = ";";
#else
const char *separator = ":";
#endif |
47941088 |
char *p, *ptr = NULL; |
334a0d15 |
for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
/* add additional trailing slash in case it is missing */
char *p1 = av_asprintf("%s/", p);
if (!p1) { |
15876331 |
ret = AVERROR(ENOMEM);
goto check_path_end; |
334a0d15 |
} |
e55092b1 |
ret = load_path(ctx, &s->dl_handle, p1, dl_name); |
334a0d15 |
av_free(p1); |
15876331 |
if (ret < 0)
goto check_path_end; |
e55092b1 |
if (s->dl_handle) |
47941088 |
break; |
334a0d15 |
} |
15876331 |
check_path_end: |
47941088 |
av_free(path); |
15876331 |
if (ret < 0)
return ret; |
47941088 |
} |
f6b6d6ac |
if (!s->dl_handle && (path = getenv("HOME"))) { |
9a2028d4 |
char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
if (!prefix)
return AVERROR(ENOMEM); |
e55092b1 |
ret = load_path(ctx, &s->dl_handle, prefix, dl_name); |
9a2028d4 |
av_free(prefix); |
6c7ae493 |
if (ret < 0)
return ret;
} |
ead0dd3c |
for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name); |
8e412400 |
if (ret < 0)
return ret;
} |
f6b6d6ac |
if (!s->dl_handle) { |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name); |
47941088 |
return AVERROR(EINVAL);
}
if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
!(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) || |
f6b6d6ac |
!(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
!(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
!(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
!(s->update = load_sym(ctx, "f0r_update" )) ||
!(s->construct = load_sym(ctx, "f0r_construct" )) ||
!(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
!(s->deinit = load_sym(ctx, "f0r_deinit" ))) |
47941088 |
return AVERROR(EINVAL);
if (f0r_init() < 0) { |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n"); |
47941088 |
return AVERROR(EINVAL);
}
|
f6b6d6ac |
f0r_get_plugin_info(&s->plugin_info);
pi = &s->plugin_info; |
f8608dca |
if (pi->plugin_type != type) { |
47941088 |
av_log(ctx, AV_LOG_ERROR, |
8accddeb |
"Invalid type '%s' for this plugin\n", |
f8608dca |
pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" : |
47941088 |
pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
return AVERROR(EINVAL);
}
|
1a49a169 |
av_log(ctx, AV_LOG_VERBOSE, |
47941088 |
"name:%s author:'%s' explanation:'%s' color_model:%s "
"frei0r_version:%d version:%d.%d num_params:%d\n",
pi->name, pi->author, pi->explanation,
pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
return 0;
}
|
d69a4177 |
static av_cold int filter_init(AVFilterContext *ctx) |
f8608dca |
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
f8608dca |
|
f6b6d6ac |
return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER); |
f8608dca |
}
|
47941088 |
static av_cold void uninit(AVFilterContext *ctx)
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv;
if (s->destruct && s->instance)
s->destruct(s->instance);
if (s->deinit)
s->deinit();
if (s->dl_handle)
dlclose(s->dl_handle); |
47941088 |
}
static int config_input_props(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst; |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
47941088 |
|
d371c3c2 |
if (s->destruct && s->instance)
s->destruct(s->instance); |
f6b6d6ac |
if (!(s->instance = s->construct(inlink->w, inlink->h))) { |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n"); |
47941088 |
return AVERROR(EINVAL);
}
|
f6b6d6ac |
return set_params(ctx, s->params); |
47941088 |
}
static int query_formats(AVFilterContext *ctx)
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
47941088 |
AVFilterFormats *formats = NULL;
|
f6b6d6ac |
if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) { |
716d413c |
ff_add_format(&formats, AV_PIX_FMT_BGRA); |
f6b6d6ac |
} else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) { |
716d413c |
ff_add_format(&formats, AV_PIX_FMT_RGBA); |
47941088 |
} else { /* F0R_COLOR_MODEL_PACKED32 */ |
716d413c |
static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE |
47941088 |
}; |
b74a1da4 |
formats = ff_make_format_list(pix_fmts); |
47941088 |
}
if (!formats)
return AVERROR(ENOMEM);
|
b74a1da4 |
ff_set_common_formats(ctx, formats); |
47941088 |
return 0;
}
|
7e350379 |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
47941088 |
{ |
f6b6d6ac |
Frei0rContext *s = inlink->dst->priv; |
47941088 |
AVFilterLink *outlink = inlink->dst->outputs[0]; |
7e350379 |
AVFrame *out; |
6325957b |
|
7e350379 |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
6325957b |
if (!out) { |
7e350379 |
av_frame_free(&in); |
6325957b |
return AVERROR(ENOMEM);
} |
7e350379 |
av_frame_copy_props(out, in); |
6325957b |
|
f6b6d6ac |
s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000, |
6325957b |
(const uint32_t *)in->data[0],
(uint32_t *)out->data[0]);
|
7e350379 |
av_frame_free(&in); |
6325957b |
return ff_filter_frame(outlink, out); |
47941088 |
}
|
5aa1a668 |
#define OFFSET(x) offsetof(Frei0rContext, x) |
3dedcef8 |
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
229d5bfd |
static const AVOption frei0r_options[] = { |
5aa1a668 |
{ "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
{ "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS }, |
b211607b |
{ NULL } |
5aa1a668 |
};
|
229d5bfd |
AVFILTER_DEFINE_CLASS(frei0r); |
5aa1a668 |
|
568c70e7 |
static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_input_props, |
6325957b |
.filter_frame = filter_frame, |
568c70e7 |
},
{ NULL }
};
static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
{ NULL }
};
|
cd43ca04 |
AVFilter ff_vf_frei0r = { |
b211607b |
.name = "frei0r",
.description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."), |
47941088 |
.query_formats = query_formats, |
b211607b |
.init = filter_init,
.uninit = uninit,
.priv_size = sizeof(Frei0rContext),
.priv_class = &frei0r_class,
.inputs = avfilter_vf_frei0r_inputs,
.outputs = avfilter_vf_frei0r_outputs, |
47941088 |
}; |
f8608dca |
|
d69a4177 |
static av_cold int source_init(AVFilterContext *ctx) |
f8608dca |
{ |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
f8608dca |
|
7ac736af |
s->time_base.num = s->framerate.den;
s->time_base.den = s->framerate.num; |
f8608dca |
|
f6b6d6ac |
return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE); |
f8608dca |
}
static int source_config_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src; |
f6b6d6ac |
Frei0rContext *s = ctx->priv; |
f8608dca |
|
f6b6d6ac |
if (av_image_check_size(s->w, s->h, 0, ctx) < 0) |
f8608dca |
return AVERROR(EINVAL); |
f6b6d6ac |
outlink->w = s->w;
outlink->h = s->h;
outlink->time_base = s->time_base; |
353cf95f |
outlink->frame_rate = av_inv_q(s->time_base); |
1b99971d |
outlink->sample_aspect_ratio = (AVRational){1,1}; |
f8608dca |
|
d371c3c2 |
if (s->destruct && s->instance)
s->destruct(s->instance); |
f6b6d6ac |
if (!(s->instance = s->construct(outlink->w, outlink->h))) { |
8accddeb |
av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n"); |
f8608dca |
return AVERROR(EINVAL);
} |
4e0be9c8 |
if (!s->params) {
av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
return AVERROR(EINVAL);
} |
f8608dca |
|
f6b6d6ac |
return set_params(ctx, s->params); |
f8608dca |
}
static int source_request_frame(AVFilterLink *outlink)
{ |
f6b6d6ac |
Frei0rContext *s = outlink->src->priv; |
7e350379 |
AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
3825b526 |
|
7e350379 |
if (!frame) |
8f3a3ce7 |
return AVERROR(ENOMEM);
|
7e350379 |
frame->sample_aspect_ratio = (AVRational) {1, 1}; |
f6b6d6ac |
frame->pts = s->pts++; |
f8608dca |
|
f6b6d6ac |
s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}), |
7e350379 |
NULL, (uint32_t *)frame->data[0]); |
f8608dca |
|
7e350379 |
return ff_filter_frame(outlink, frame); |
f8608dca |
}
|
229d5bfd |
static const AVOption frei0r_src_options[] = { |
be7d6710 |
{ "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS }, |
7ac736af |
{ "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = FLAGS }, |
5aa1a668 |
{ "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
{ "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
{ NULL },
};
|
229d5bfd |
AVFILTER_DEFINE_CLASS(frei0r_src); |
5aa1a668 |
|
568c70e7 |
static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.request_frame = source_request_frame,
.config_props = source_config_props
},
{ NULL }
};
|
cd43ca04 |
AVFilter ff_vsrc_frei0r_src = { |
b211607b |
.name = "frei0r_src",
.description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
.priv_size = sizeof(Frei0rContext),
.priv_class = &frei0r_src_class,
.init = source_init,
.uninit = uninit, |
f8608dca |
.query_formats = query_formats, |
b211607b |
.inputs = NULL,
.outputs = avfilter_vsrc_frei0r_src_outputs, |
f8608dca |
}; |