libavfilter/vf_frei0r.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Stefano Sabatini
00003  * This file is part of FFmpeg.
00004  *
00005  * FFmpeg is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * FFmpeg is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with FFmpeg; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 
00025 /* #define DEBUG */
00026 
00027 #include <dlfcn.h>
00028 #include <frei0r.h>
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/imgutils.h"
00031 #include "libavutil/mathematics.h"
00032 #include "libavutil/parseutils.h"
00033 #include "avfilter.h"
00034 
00035 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
00036 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
00037 typedef void (*f0r_deinit_f)(void);
00038 typedef int (*f0r_init_f)(void);
00039 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
00040 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
00041 typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
00042 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);
00043 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
00044 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
00045 
00046 typedef struct Frei0rContext {
00047     f0r_update_f update;
00048     void *dl_handle;            /* dynamic library handle   */
00049     f0r_instance_t instance;
00050     f0r_plugin_info_t plugin_info;
00051 
00052     f0r_get_param_info_f  get_param_info;
00053     f0r_get_param_value_f get_param_value;
00054     f0r_set_param_value_f set_param_value;
00055     f0r_construct_f       construct;
00056     f0r_destruct_f        destruct;
00057     f0r_deinit_f          deinit;
00058     char params[256];
00059 
00060     /* only used by the source */
00061     int w, h;
00062     AVRational time_base;
00063     uint64_t pts;
00064 } Frei0rContext;
00065 
00066 static void *load_sym(AVFilterContext *ctx, const char *sym_name)
00067 {
00068     Frei0rContext *frei0r = ctx->priv;
00069     void *sym = dlsym(frei0r->dl_handle, sym_name);
00070     if (!sym)
00071         av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
00072     return sym;
00073 }
00074 
00075 static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
00076 {
00077     Frei0rContext *frei0r = ctx->priv;
00078     union {
00079         double d;
00080         f0r_param_color_t col;
00081         f0r_param_position_t pos;
00082     } val;
00083     char *tail;
00084     uint8_t rgba[4];
00085 
00086     switch (info.type) {
00087     case F0R_PARAM_BOOL:
00088         if      (!strcmp(param, "y")) val.d = 1.0;
00089         else if (!strcmp(param, "n")) val.d = 0.0;
00090         else goto fail;
00091         break;
00092 
00093     case F0R_PARAM_DOUBLE:
00094         val.d = strtod(param, &tail);
00095         if (*tail || val.d == HUGE_VAL)
00096             goto fail;
00097         break;
00098 
00099     case F0R_PARAM_COLOR:
00100         if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
00101             if (av_parse_color(rgba, param, -1, ctx) < 0)
00102                 goto fail;
00103             val.col.r = rgba[0] / 255.0;
00104             val.col.g = rgba[1] / 255.0;
00105             val.col.b = rgba[2] / 255.0;
00106         }
00107         break;
00108 
00109     case F0R_PARAM_POSITION:
00110         if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
00111             goto fail;
00112         break;
00113     }
00114 
00115     frei0r->set_param_value(frei0r->instance, &val, index);
00116     return 0;
00117 
00118 fail:
00119     av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
00120            param, info.name);
00121     return AVERROR(EINVAL);
00122 }
00123 
00124 static int set_params(AVFilterContext *ctx, const char *params)
00125 {
00126     Frei0rContext *frei0r = ctx->priv;
00127     int i;
00128 
00129     for (i = 0; i < frei0r->plugin_info.num_params; i++) {
00130         f0r_param_info_t info;
00131         char *param;
00132         int ret;
00133 
00134         frei0r->get_param_info(&info, i);
00135 
00136         if (*params) {
00137             if (!(param = av_get_token(&params, ":")))
00138                 return AVERROR(ENOMEM);
00139             params++;               /* skip ':' */
00140             ret = set_param(ctx, info, i, param);
00141             av_free(param);
00142             if (ret < 0)
00143                 return ret;
00144         }
00145 
00146         av_log(ctx, AV_LOG_INFO,
00147                "idx:%d name:'%s' type:%s explanation:'%s' ",
00148                i, info.name,
00149                info.type == F0R_PARAM_BOOL     ? "bool"     :
00150                info.type == F0R_PARAM_DOUBLE   ? "double"   :
00151                info.type == F0R_PARAM_COLOR    ? "color"    :
00152                info.type == F0R_PARAM_POSITION ? "position" :
00153                info.type == F0R_PARAM_STRING   ? "string"   : "unknown",
00154                info.explanation);
00155 
00156 #ifdef DEBUG
00157         av_log(ctx, AV_LOG_INFO, "value:");
00158         switch (info.type) {
00159             void *v;
00160             double d;
00161             char s[128];
00162             f0r_param_color_t col;
00163             f0r_param_position_t pos;
00164 
00165         case F0R_PARAM_BOOL:
00166             v = &d;
00167             frei0r->get_param_value(frei0r->instance, v, i);
00168             av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
00169             break;
00170         case F0R_PARAM_DOUBLE:
00171             v = &d;
00172             frei0r->get_param_value(frei0r->instance, v, i);
00173             av_log(ctx, AV_LOG_INFO, "%f", d);
00174             break;
00175         case F0R_PARAM_COLOR:
00176             v = &col;
00177             frei0r->get_param_value(frei0r->instance, v, i);
00178             av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
00179             break;
00180         case F0R_PARAM_POSITION:
00181             v = &pos;
00182             frei0r->get_param_value(frei0r->instance, v, i);
00183             av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
00184             break;
00185         default: /* F0R_PARAM_STRING */
00186             v = s;
00187             frei0r->get_param_value(frei0r->instance, v, i);
00188             av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
00189             break;
00190         }
00191 #endif
00192         av_log(ctx, AV_LOG_INFO, "\n");
00193     }
00194 
00195     return 0;
00196 }
00197 
00198 static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
00199 {
00200     char path[1024];
00201 
00202     snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
00203     av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
00204     return dlopen(path, RTLD_NOW|RTLD_LOCAL);
00205 }
00206 
00207 static av_cold int frei0r_init(AVFilterContext *ctx,
00208                                const char *dl_name, int type)
00209 {
00210     Frei0rContext *frei0r = ctx->priv;
00211     f0r_init_f            f0r_init;
00212     f0r_get_plugin_info_f f0r_get_plugin_info;
00213     f0r_plugin_info_t *pi;
00214     char *path;
00215 
00216     /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
00217     if ((path = av_strdup(getenv("FREI0R_PATH")))) {
00218         char *p, *ptr = NULL;
00219         for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
00220             if (frei0r->dl_handle = load_path(ctx, p, dl_name))
00221                 break;
00222         av_free(path);
00223     }
00224     if (!frei0r->dl_handle && (path = getenv("HOME"))) {
00225         char prefix[1024];
00226         snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
00227         frei0r->dl_handle = load_path(ctx, prefix, dl_name);
00228     }
00229     if (!frei0r->dl_handle)
00230         frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
00231     if (!frei0r->dl_handle)
00232         frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
00233     if (!frei0r->dl_handle) {
00234         av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
00235         return AVERROR(EINVAL);
00236     }
00237 
00238     if (!(f0r_init                = load_sym(ctx, "f0r_init"           )) ||
00239         !(f0r_get_plugin_info     = load_sym(ctx, "f0r_get_plugin_info")) ||
00240         !(frei0r->get_param_info  = load_sym(ctx, "f0r_get_param_info" )) ||
00241         !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
00242         !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
00243         !(frei0r->update          = load_sym(ctx, "f0r_update"         )) ||
00244         !(frei0r->construct       = load_sym(ctx, "f0r_construct"      )) ||
00245         !(frei0r->destruct        = load_sym(ctx, "f0r_destruct"       )) ||
00246         !(frei0r->deinit          = load_sym(ctx, "f0r_deinit"         )))
00247         return AVERROR(EINVAL);
00248 
00249     if (f0r_init() < 0) {
00250         av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
00251         return AVERROR(EINVAL);
00252     }
00253 
00254     f0r_get_plugin_info(&frei0r->plugin_info);
00255     pi = &frei0r->plugin_info;
00256     if (pi->plugin_type != type) {
00257         av_log(ctx, AV_LOG_ERROR,
00258                "Invalid type '%s' for the plugin\n",
00259                pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
00260                pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
00261                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
00262                pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
00263         return AVERROR(EINVAL);
00264     }
00265 
00266     av_log(ctx, AV_LOG_INFO,
00267            "name:%s author:'%s' explanation:'%s' color_model:%s "
00268            "frei0r_version:%d version:%d.%d num_params:%d\n",
00269            pi->name, pi->author, pi->explanation,
00270            pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
00271            pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
00272            pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
00273            pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
00274 
00275     return 0;
00276 }
00277 
00278 static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
00279 {
00280     Frei0rContext *frei0r = ctx->priv;
00281     char dl_name[1024], c;
00282     *frei0r->params = 0;
00283 
00284     if (args)
00285         sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
00286 
00287     return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
00288 }
00289 
00290 static av_cold void uninit(AVFilterContext *ctx)
00291 {
00292     Frei0rContext *frei0r = ctx->priv;
00293 
00294     if (frei0r->destruct && frei0r->instance)
00295         frei0r->destruct(frei0r->instance);
00296     if (frei0r->deinit)
00297         frei0r->deinit();
00298     if (frei0r->dl_handle)
00299         dlclose(frei0r->dl_handle);
00300 
00301     memset(frei0r, 0, sizeof(*frei0r));
00302 }
00303 
00304 static int config_input_props(AVFilterLink *inlink)
00305 {
00306     AVFilterContext *ctx = inlink->dst;
00307     Frei0rContext *frei0r = ctx->priv;
00308 
00309     if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
00310         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
00311         return AVERROR(EINVAL);
00312     }
00313 
00314     return set_params(ctx, frei0r->params);
00315 }
00316 
00317 static int query_formats(AVFilterContext *ctx)
00318 {
00319     Frei0rContext *frei0r = ctx->priv;
00320     AVFilterFormats *formats = NULL;
00321 
00322     if        (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
00323         avfilter_add_format(&formats, PIX_FMT_BGRA);
00324     } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
00325         avfilter_add_format(&formats, PIX_FMT_RGBA);
00326     } else {                                   /* F0R_COLOR_MODEL_PACKED32 */
00327         static const enum PixelFormat pix_fmts[] = {
00328             PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
00329         };
00330         formats = avfilter_make_format_list(pix_fmts);
00331     }
00332 
00333     if (!formats)
00334         return AVERROR(ENOMEM);
00335 
00336     avfilter_set_common_pixel_formats(ctx, formats);
00337     return 0;
00338 }
00339 
00340 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
00341 
00342 static void end_frame(AVFilterLink *inlink)
00343 {
00344     Frei0rContext *frei0r = inlink->dst->priv;
00345     AVFilterLink *outlink = inlink->dst->outputs[0];
00346     AVFilterBufferRef  *inpicref =  inlink->cur_buf;
00347     AVFilterBufferRef *outpicref = outlink->out_buf;
00348 
00349     frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
00350                    (const uint32_t *)inpicref->data[0],
00351                    (uint32_t *)outpicref->data[0]);
00352     avfilter_unref_buffer(inpicref);
00353     avfilter_draw_slice(outlink, 0, outlink->h, 1);
00354     avfilter_end_frame(outlink);
00355     avfilter_unref_buffer(outpicref);
00356 }
00357 
00358 AVFilter avfilter_vf_frei0r = {
00359     .name      = "frei0r",
00360     .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
00361 
00362     .query_formats = query_formats,
00363     .init = filter_init,
00364     .uninit = uninit,
00365 
00366     .priv_size = sizeof(Frei0rContext),
00367 
00368     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00369                                     .type             = AVMEDIA_TYPE_VIDEO,
00370                                     .draw_slice       = null_draw_slice,
00371                                     .config_props     = config_input_props,
00372                                     .end_frame        = end_frame,
00373                                     .min_perms        = AV_PERM_READ },
00374                                   { .name = NULL}},
00375 
00376     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
00377                                     .type             = AVMEDIA_TYPE_VIDEO, },
00378                                   { .name = NULL}},
00379 };
00380 
00381 static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
00382 {
00383     Frei0rContext *frei0r = ctx->priv;
00384     char dl_name[1024], c;
00385     char frame_size[128] = "";
00386     char frame_rate[128] = "";
00387     AVRational frame_rate_q;
00388 
00389     memset(frei0r->params, 0, sizeof(frei0r->params));
00390 
00391     if (args)
00392         sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
00393                frame_size, frame_rate, dl_name, &c, frei0r->params);
00394 
00395     if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
00396         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
00397         return AVERROR(EINVAL);
00398     }
00399 
00400     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
00401         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
00402         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
00403         return AVERROR(EINVAL);
00404     }
00405     frei0r->time_base.num = frame_rate_q.den;
00406     frei0r->time_base.den = frame_rate_q.num;
00407 
00408     return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
00409 }
00410 
00411 static int source_config_props(AVFilterLink *outlink)
00412 {
00413     AVFilterContext *ctx = outlink->src;
00414     Frei0rContext *frei0r = ctx->priv;
00415 
00416     if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
00417         return AVERROR(EINVAL);
00418     outlink->w = frei0r->w;
00419     outlink->h = frei0r->h;
00420     outlink->time_base = frei0r->time_base;
00421 
00422     if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
00423         av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
00424         return AVERROR(EINVAL);
00425     }
00426 
00427     return set_params(ctx, frei0r->params);
00428 }
00429 
00430 static int source_request_frame(AVFilterLink *outlink)
00431 {
00432     Frei0rContext *frei0r = outlink->src->priv;
00433     AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00434     picref->video->sample_aspect_ratio = (AVRational) {1, 1};
00435     picref->pts = frei0r->pts++;
00436     picref->pos = -1;
00437 
00438     avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
00439     frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
00440                    NULL, (uint32_t *)picref->data[0]);
00441     avfilter_draw_slice(outlink, 0, outlink->h, 1);
00442     avfilter_end_frame(outlink);
00443     avfilter_unref_buffer(picref);
00444 
00445     return 0;
00446 }
00447 
00448 AVFilter avfilter_vsrc_frei0r_src = {
00449     .name        = "frei0r_src",
00450     .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
00451 
00452     .priv_size = sizeof(Frei0rContext),
00453     .init      = source_init,
00454     .uninit    = uninit,
00455 
00456     .query_formats = query_formats,
00457 
00458     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
00459 
00460     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00461                                     .type            = AVMEDIA_TYPE_VIDEO,
00462                                     .request_frame   = source_request_frame,
00463                                     .config_props    = source_config_props },
00464                                   { .name = NULL}},
00465 };