[PATCH] Add frei0r filter.
Stefano Sabatini
stefano.sabatini-lala
Tue Aug 24 19:48:57 CEST 2010
---
configure | 8 +
doc/filters.texi | 40 +++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_frei0r.c | 353 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 403 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/vf_frei0r.c
diff --git a/configure b/configure
index b7bcb48..57a35ab 100755
--- a/configure
+++ b/configure
@@ -162,6 +162,7 @@ Configuration options:
External library support:
--enable-avisynth enable reading of AVISynth script files [no]
--enable-bzlib enable bzlib [autodetect]
+ --enable-frei0r enable frei0r video filtering
--enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
--enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
--enable-libdc1394 enable IIDC-1394 grabbing using libdc1394
@@ -863,6 +864,7 @@ CONFIG_LIST="
ffprobe
ffserver
fft
+ frei0r
golomb
gpl
gray
@@ -997,6 +999,7 @@ HAVE_LIST="
fast_cmov
fcntl
fork
+ frei0r
getaddrinfo
gethrtime
GetProcessMemoryInfo
@@ -1370,6 +1373,9 @@ vfwcap_indev_extralibs="-lavicap32"
x11_grab_device_indev_deps="x11grab XShmCreateImage"
x11_grab_device_indev_extralibs="-lX11 -lXext -lXfixes"
+# filters
+frei0r_filter_deps="frei0r dlopen"
+
# protocols
gopher_protocol_deps="network"
http_protocol_deps="network"
@@ -2685,6 +2691,7 @@ check_mathfunc truncf
# these are off by default, so fail if requested and not available
enabled avisynth && require2 vfw32 "windows.h vfw.h" AVIFileInit -lavifil32
+enabled frei0r && check_header frei0r.h
enabled libdirac && add_cflags $(pkg-config --cflags dirac) &&
require libdirac libdirac_decoder/dirac_parser.h dirac_decoder_init $(pkg-config --libs dirac) &&
require libdirac libdirac_encoder/dirac_encoder.h dirac_encoder_init $(pkg-config --libs dirac)
@@ -2967,6 +2974,7 @@ echo "threading support ${thread_type-no}"
echo "SDL support ${sdl-no}"
echo "Sun medialib support ${mlib-no}"
echo "AVISynth enabled ${avisynth-no}"
+echo "frei0r enabled ${frei0r-no}"
echo "libdc1394 support ${libdc1394-no}"
echo "libdirac enabled ${libdirac-no}"
echo "libfaac enabled ${libfaac-no}"
diff --git a/doc/filters.texi b/doc/filters.texi
index 60ecf56..844dece 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -81,6 +81,46 @@ The following command:
will convert the input video to the format ``yuv420p''.
+ at section frei0r
+
+Apply a frei0r effect to the input video.
+
+To enable compilation of this filter you need to install the frei0r
+headers and configure FFmpeg with --enable-frei0r.
+
+The filter supports the syntax:
+ at example
+ at var{filter_path}:@var{param1}:@var{param2}:...:@var{paramN}
+ at end example
+
+where @var{filter_path} is the path to the frei0r effect to load, and
+ at var{param1}, @var{param2}, ... , @var{paramN} is a list of values
+separated by ":" which specify the parameters for the frei0r effect.
+
+frei0r filters are usually installed in @file{/usr/lib/frei0r-1/}.
+
+A frei0r effect parameter can be a boolean (whose values are specified
+with "true" and "false"), a double, a color (specified by the syntax
+ at var{R} @var{G} @var{B}), a position (specified by the syntax
+ at var{x} @var{y}) and a string.
+
+The number and kind of parameters depend on the loaded effect. If an
+effect parameter is not specified the default value is set.
+
+Follows some example:
+ at example
+# apply the distort0r effect, set the first two double parameters
+frei0r=/usr/lib/frei0r-1/distort0r.so:0.5:0.01
+
+# apply the colordistance effect, takes a color as first parameter
+# you may need some form of escaping
+frei0r=/usr/lib/frei0r-1/colordistance.so:'0.2 0.3 0.4'
+
+# apply the perspective effect, specify the top left and top right
+# image positions
+frei0r=/usr/lib/frei0r-1/perspective.so:'0.2 0.2':'0.8 0.2'
+ at end example
+
@section hflip
Flip the input video horizontally.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index d2a40c4..860049c 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -20,6 +20,7 @@ OBJS-$(CONFIG_ASPECT_FILTER) += vf_aspect.o
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o
OBJS-$(CONFIG_FIFO_FILTER) += vf_fifo.o
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o
+OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o
OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
OBJS-$(CONFIG_NOFORMAT_FILTER) += vf_format.o
OBJS-$(CONFIG_NULL_FILTER) += vf_null.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 27442f6..aa3f9e7 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -40,6 +40,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (CROP, crop, vf);
REGISTER_FILTER (FIFO, fifo, vf);
REGISTER_FILTER (FORMAT, format, vf);
+ REGISTER_FILTER (FREI0R, frei0r, vf);
REGISTER_FILTER (HFLIP, hflip, vf);
REGISTER_FILTER (NOFORMAT, noformat, vf);
REGISTER_FILTER (NULL, null, vf);
diff --git a/libavfilter/vf_frei0r.c b/libavfilter/vf_frei0r.c
new file mode 100644
index 0000000..404bc97
--- /dev/null
+++ b/libavfilter/vf_frei0r.c
@@ -0,0 +1,353 @@
+/*
+ * Stefano Sabatini 2010
+ * 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>
+#include <float.h>
+#include "avfilter.h"
+#include "parseutils.h"
+
+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 {
+ f0r_update_f update;
+ void *dll;
+ char dll_filename[128];
+ f0r_instance_t instance;
+ f0r_plugin_info_t plugin_info;
+ char params[256];
+} Frei0rContext;
+
+static void *load_sym(AVFilterContext *ctx, const char *sym_name)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ void *sym = dlsym(frei0r->dll, sym_name);
+ if (!sym)
+ av_log(ctx, AV_LOG_ERROR,
+ "Could not find symbol '%s' in module '%s'\n", sym_name, frei0r->dll_filename);
+ return sym;
+}
+
+static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ void *v;
+ long double ld;
+ double d;
+ f0r_param_color_t col;
+ f0r_param_position_t pos;
+ f0r_set_param_value_f f0r_set_param_value;
+
+ if (!(f0r_set_param_value = load_sym(ctx, "f0r_set_param_value")))
+ return AVERROR(EINVAL);
+
+ switch (info.type) {
+ case F0R_PARAM_BOOL:
+ {
+ if (!strcmp(param, "true" )) d = 1.0;
+ else if (!strcmp(param, "false")) d = 0.0;
+ else goto fail;
+ v = (void *)(&d);
+ }
+ break;
+
+ case F0R_PARAM_DOUBLE:
+ {
+ char *tail;
+ ld = strtold(param, &tail);
+ if (*tail || ld > DBL_MAX || ld < -DBL_MAX)
+ goto fail;
+ d = ld;
+ v = (void *)(&d);
+ }
+ break;
+
+ case F0R_PARAM_COLOR:
+ {
+ if (sscanf(param, "%f %f %f", &col.r, &col.g, &col.b) != 3)
+ goto fail;
+ v = (void *)(&col);
+ }
+ break;
+
+ case F0R_PARAM_POSITION:
+ {
+ if (sscanf(param, "%lf %lf", &pos.x, &pos.y) != 2)
+ goto fail;
+ v = (void *)(&pos);
+ }
+ break;
+
+ default: /* F0R_PARAM_STRING */
+ v = (void *)param;
+ break;
+ }
+
+ av_log(ctx, AV_LOG_INFO,
+ "Setting param '%s' to the value:'%s'\n", info.name, param);
+ f0r_set_param_value(frei0r->instance, v, index);
+ return 0;
+
+fail:
+ av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'",
+ param, info.name);
+ return AVERROR(EINVAL);
+}
+
+static int set_params(AVFilterContext *ctx, const char *params)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ int i;
+ f0r_get_param_info_f f0r_get_param_info;
+ f0r_get_param_value_f f0r_get_param_value;
+
+ if (!(f0r_get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
+ !(f0r_get_param_value = load_sym(ctx, "f0r_get_param_value")))
+ return AVERROR(EINVAL);
+ for (i = 0; i < frei0r->plugin_info.num_params; i++) {
+ f0r_param_info_t info;
+ char *param, s[128];
+ void *v;
+ double d;
+ int ret;
+ f0r_param_color_t col;
+ f0r_param_position_t pos;
+
+ f0r_get_param_info(&info, i);
+
+ if (*params) {
+ if (!(param = av_get_token(¶ms, ":")))
+ return AVERROR(ENOMEM);
+ params++; /* skip ':' */
+ ret = set_param(ctx, info, i, param);
+ av_free(param);
+ if (ret < 0)
+ return ret;
+ }
+
+ av_log(ctx, AV_LOG_INFO,
+ "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);
+
+ switch (info.type) {
+ case F0R_PARAM_BOOL:
+ v = (void *)(&d);
+ f0r_get_param_value(frei0r->instance, v, i);
+ av_log(ctx, AV_LOG_INFO, "value:'%s'\n", d == 1.0 ? "true" : "false");
+ break;
+ case F0R_PARAM_DOUBLE:
+ v = (void *)(&d);
+ f0r_get_param_value(frei0r->instance, v, i);
+ av_log(ctx, AV_LOG_INFO, "value:'%f'\n", d);
+ break;
+ case F0R_PARAM_COLOR:
+ v = (void *)(&col);
+ f0r_get_param_value(frei0r->instance, v, i);
+ av_log(ctx, AV_LOG_INFO, "value:'%f %f %f'\n", col.r, col.g, col.b);
+ break;
+ case F0R_PARAM_POSITION:
+ v = (void *)(&pos);
+ f0r_get_param_value(frei0r->instance, v, i);
+ av_log(ctx, AV_LOG_INFO, "value:'%lf %lf'\n", pos.x, pos.y);
+ break;
+ default: /* F0R_PARAM_STRING */
+ v = (void *)s;
+ f0r_get_param_value(frei0r->instance, v, i);
+ av_log(ctx, AV_LOG_INFO, "value:'%s'\n", s);
+ break;
+ }
+
+ }
+
+ return 0;
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ f0r_init_f f0r_init;
+ f0r_get_plugin_info_f f0r_get_plugin_info;
+ f0r_plugin_info_t *pi;
+
+ *(frei0r->params) = 0;
+
+ if (args)
+ sscanf(args, "%127[^:]:%255c", frei0r->dll_filename, frei0r->params);
+
+ if (!(frei0r->dll = dlopen(frei0r->dll_filename, RTLD_NOW))) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Could not load module '%s': %s\n", frei0r->dll_filename, dlerror());
+ return AVERROR(EINVAL);
+ }
+
+ if (!(frei0r->update = load_sym(ctx, "f0r_update")))
+ return AVERROR(EINVAL);
+ if (!(f0r_init = load_sym(ctx, "f0r_init")))
+ return AVERROR(EINVAL);
+ if (f0r_init() < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
+ return AVERROR(EINVAL);
+ }
+
+ if (!(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")))
+ return AVERROR(EINVAL);
+ f0r_get_plugin_info(&frei0r->plugin_info);
+
+ pi = &(frei0r->plugin_info);
+ if (pi->plugin_type != F0R_PLUGIN_TYPE_FILTER) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Invalid type '%s' for the plugin, a filter plugin was expected\n",
+ pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
+ 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);
+ }
+
+ av_log(ctx, AV_LOG_INFO,
+ "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;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ f0r_destruct_f f0r_destruct;
+ f0r_deinit_f f0r_deinit;
+
+ if ((f0r_destruct = dlsym(frei0r->dll, "f0r_destruct")))
+ f0r_destruct(frei0r->instance);
+ frei0r->instance = NULL;
+
+ if ((f0r_deinit = dlsym(frei0r->dll, "f0r_deinit")))
+ f0r_deinit();
+
+ frei0r->update = NULL;
+
+ if (frei0r->dll)
+ dlclose(frei0r->dll);
+ frei0r->dll = NULL;
+}
+
+static int config_input_props(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ Frei0rContext *frei0r = ctx->priv;
+ f0r_construct_f f0r_construct;
+ if (!(f0r_construct = load_sym(ctx, "f0r_construct")))
+ return AVERROR(EINVAL);
+
+ if (!(frei0r->instance = f0r_construct(inlink->w, inlink->h))) {
+ av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
+ return AVERROR(EINVAL);
+ }
+
+ return set_params(ctx, frei0r->params);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ Frei0rContext *frei0r = ctx->priv;
+ AVFilterFormats *formats = NULL;
+
+ if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888)
+ avfilter_add_format(&formats, PIX_FMT_BGRA);
+ else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888)
+ avfilter_add_format(&formats, PIX_FMT_RGBA);
+ else { /* F0R_COLOR_MODEL_PACKED32 */
+ static const enum PixelFormat pix_fmts[] = {
+ PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
+ };
+ formats = avfilter_make_format_list(pix_fmts);
+ }
+
+ if (!formats)
+ return AVERROR(ENOMEM);
+
+ avfilter_set_common_formats(ctx, formats);
+ return 0;
+}
+
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+ Frei0rContext *frei0r = inlink->dst->priv;
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+ AVFilterBufferRef * inpicref = inlink ->cur_buf;
+ AVFilterBufferRef *outpicref = outlink->out_buf;
+
+ frei0r->update(frei0r->instance, (double)inpicref->pts / AV_TIME_BASE,
+ (const uint32_t *)inpicref->data[0],
+ (uint32_t *)outpicref->data[0]);
+ avfilter_unref_buffer(inpicref);
+ avfilter_draw_slice(outlink, 0, outlink->h, 1);
+ avfilter_end_frame(outlink);
+ avfilter_unref_buffer(outpicref);
+}
+
+AVFilter avfilter_vf_frei0r = {
+ .name = "frei0r",
+ .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
+
+ .query_formats = query_formats,
+ .init = init,
+ .uninit = uninit,
+
+ .priv_size = sizeof(Frei0rContext),
+
+ .inputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .draw_slice = draw_slice,
+ .config_props = config_input_props,
+ .end_frame = end_frame,
+ .min_perms = AV_PERM_READ },
+ { .name = NULL}},
+
+ .outputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO, },
+ { .name = NULL}},
+};
--
1.7.1
--17pEHd4RhPHOinZp--
More information about the ffmpeg-devel
mailing list