vf_frei0r.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * copyright (c) 2010 Stefano Sabatini
  3. * This file is part of FFmpeg.
  4. *
  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.
  9. *
  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.
  14. *
  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
  18. */
  19. /**
  20. * @file
  21. * frei0r wrapper
  22. */
  23. /* #define DEBUG */
  24. #include <dlfcn.h>
  25. #include <frei0r.h>
  26. #include "libavutil/avstring.h"
  27. #include "avfilter.h"
  28. #include "parseutils.h"
  29. typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
  30. typedef void (*f0r_destruct_f)(f0r_instance_t instance);
  31. typedef void (*f0r_deinit_f)(void);
  32. typedef int (*f0r_init_f)(void);
  33. typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
  34. typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
  35. typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
  36. 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);
  37. typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  38. typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  39. typedef struct Frei0rContext {
  40. f0r_update_f update;
  41. void *dl_handle; /* dynamic library handle */
  42. f0r_instance_t instance;
  43. f0r_plugin_info_t plugin_info;
  44. f0r_get_param_info_f get_param_info;
  45. f0r_get_param_value_f get_param_value;
  46. f0r_set_param_value_f set_param_value;
  47. f0r_construct_f construct;
  48. f0r_destruct_f destruct;
  49. f0r_deinit_f deinit;
  50. char params[256];
  51. } Frei0rContext;
  52. static void *load_sym(AVFilterContext *ctx, const char *sym_name)
  53. {
  54. Frei0rContext *frei0r = ctx->priv;
  55. void *sym = dlsym(frei0r->dl_handle, sym_name);
  56. if (!sym)
  57. av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
  58. return sym;
  59. }
  60. static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
  61. {
  62. Frei0rContext *frei0r = ctx->priv;
  63. union {
  64. double d;
  65. f0r_param_color_t col;
  66. f0r_param_position_t pos;
  67. } val;
  68. char *tail;
  69. uint8_t rgba[4];
  70. switch (info.type) {
  71. case F0R_PARAM_BOOL:
  72. if (!strcmp(param, "y")) val.d = 1.0;
  73. else if (!strcmp(param, "n")) val.d = 0.0;
  74. else goto fail;
  75. break;
  76. case F0R_PARAM_DOUBLE:
  77. val.d = strtod(param, &tail);
  78. if (*tail || val.d == HUGE_VAL)
  79. goto fail;
  80. break;
  81. case F0R_PARAM_COLOR:
  82. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  83. if (av_parse_color(rgba, param, ctx) < 0)
  84. goto fail;
  85. val.col.r = rgba[0] / 255.0;
  86. val.col.g = rgba[1] / 255.0;
  87. val.col.b = rgba[2] / 255.0;
  88. }
  89. break;
  90. case F0R_PARAM_POSITION:
  91. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  92. goto fail;
  93. break;
  94. }
  95. frei0r->set_param_value(frei0r->instance, &val, index);
  96. return 0;
  97. fail:
  98. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
  99. param, info.name);
  100. return AVERROR(EINVAL);
  101. }
  102. static int set_params(AVFilterContext *ctx, const char *params)
  103. {
  104. Frei0rContext *frei0r = ctx->priv;
  105. int i;
  106. for (i = 0; i < frei0r->plugin_info.num_params; i++) {
  107. f0r_param_info_t info;
  108. char *param;
  109. int ret;
  110. frei0r->get_param_info(&info, i);
  111. if (*params) {
  112. if (!(param = av_get_token(&params, ":")))
  113. return AVERROR(ENOMEM);
  114. params++; /* skip ':' */
  115. ret = set_param(ctx, info, i, param);
  116. av_free(param);
  117. if (ret < 0)
  118. return ret;
  119. }
  120. av_log(ctx, AV_LOG_INFO,
  121. "idx:%d name:'%s' type:%s explanation:'%s' ",
  122. i, info.name,
  123. info.type == F0R_PARAM_BOOL ? "bool" :
  124. info.type == F0R_PARAM_DOUBLE ? "double" :
  125. info.type == F0R_PARAM_COLOR ? "color" :
  126. info.type == F0R_PARAM_POSITION ? "position" :
  127. info.type == F0R_PARAM_STRING ? "string" : "unknown",
  128. info.explanation);
  129. #ifdef DEBUG
  130. av_log(ctx, AV_LOG_INFO, "value:");
  131. switch (info.type) {
  132. void *v;
  133. double d;
  134. char s[128];
  135. f0r_param_color_t col;
  136. f0r_param_position_t pos;
  137. case F0R_PARAM_BOOL:
  138. v = &d;
  139. frei0r->get_param_value(frei0r->instance, v, i);
  140. av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
  141. break;
  142. case F0R_PARAM_DOUBLE:
  143. v = &d;
  144. frei0r->get_param_value(frei0r->instance, v, i);
  145. av_log(ctx, AV_LOG_INFO, "%f", d);
  146. break;
  147. case F0R_PARAM_COLOR:
  148. v = &col;
  149. frei0r->get_param_value(frei0r->instance, v, i);
  150. av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
  151. break;
  152. case F0R_PARAM_POSITION:
  153. v = &pos;
  154. frei0r->get_param_value(frei0r->instance, v, i);
  155. av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
  156. break;
  157. default: /* F0R_PARAM_STRING */
  158. v = s;
  159. frei0r->get_param_value(frei0r->instance, v, i);
  160. av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
  161. break;
  162. }
  163. #endif
  164. av_log(ctx, AV_LOG_INFO, "\n");
  165. }
  166. return 0;
  167. }
  168. static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
  169. {
  170. char path[1024];
  171. snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
  172. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
  173. return dlopen(path, RTLD_NOW|RTLD_LOCAL);
  174. }
  175. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  176. {
  177. Frei0rContext *frei0r = ctx->priv;
  178. f0r_init_f f0r_init;
  179. f0r_get_plugin_info_f f0r_get_plugin_info;
  180. f0r_plugin_info_t *pi;
  181. char dl_name[1024], *path;
  182. *frei0r->params = 0;
  183. if (args)
  184. sscanf(args, "%1023[^:]:%255c", dl_name, frei0r->params);
  185. /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
  186. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  187. char *p, *ptr = NULL;
  188. for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
  189. if (frei0r->dl_handle = load_path(ctx, p, dl_name))
  190. break;
  191. av_free(path);
  192. }
  193. if (!frei0r->dl_handle && (path = getenv("HOME"))) {
  194. char prefix[1024];
  195. snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
  196. frei0r->dl_handle = load_path(ctx, prefix, dl_name);
  197. }
  198. if (!frei0r->dl_handle)
  199. frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
  200. if (!frei0r->dl_handle)
  201. frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
  202. if (!frei0r->dl_handle) {
  203. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
  204. return AVERROR(EINVAL);
  205. }
  206. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  207. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  208. !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  209. !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  210. !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  211. !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
  212. !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
  213. !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
  214. !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
  215. return AVERROR(EINVAL);
  216. if (f0r_init() < 0) {
  217. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
  218. return AVERROR(EINVAL);
  219. }
  220. f0r_get_plugin_info(&frei0r->plugin_info);
  221. pi = &frei0r->plugin_info;
  222. if (pi->plugin_type != F0R_PLUGIN_TYPE_FILTER) {
  223. av_log(ctx, AV_LOG_ERROR,
  224. "Invalid type '%s' for the plugin, a filter plugin was expected\n",
  225. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  226. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  227. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  228. return AVERROR(EINVAL);
  229. }
  230. av_log(ctx, AV_LOG_INFO,
  231. "name:%s author:'%s' explanation:'%s' color_model:%s "
  232. "frei0r_version:%d version:%d.%d num_params:%d\n",
  233. pi->name, pi->author, pi->explanation,
  234. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  235. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  236. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  237. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  238. return 0;
  239. }
  240. static av_cold void uninit(AVFilterContext *ctx)
  241. {
  242. Frei0rContext *frei0r = ctx->priv;
  243. if (frei0r->destruct)
  244. frei0r->destruct(frei0r->instance);
  245. if (frei0r->deinit)
  246. frei0r->deinit();
  247. if (frei0r->dl_handle)
  248. dlclose(frei0r->dl_handle);
  249. memset(frei0r, 0, sizeof(*frei0r));
  250. }
  251. static int config_input_props(AVFilterLink *inlink)
  252. {
  253. AVFilterContext *ctx = inlink->dst;
  254. Frei0rContext *frei0r = ctx->priv;
  255. if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
  256. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  257. return AVERROR(EINVAL);
  258. }
  259. return set_params(ctx, frei0r->params);
  260. }
  261. static int query_formats(AVFilterContext *ctx)
  262. {
  263. Frei0rContext *frei0r = ctx->priv;
  264. AVFilterFormats *formats = NULL;
  265. if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  266. avfilter_add_format(&formats, PIX_FMT_BGRA);
  267. } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  268. avfilter_add_format(&formats, PIX_FMT_RGBA);
  269. } else { /* F0R_COLOR_MODEL_PACKED32 */
  270. static const enum PixelFormat pix_fmts[] = {
  271. PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
  272. };
  273. formats = avfilter_make_format_list(pix_fmts);
  274. }
  275. if (!formats)
  276. return AVERROR(ENOMEM);
  277. avfilter_set_common_formats(ctx, formats);
  278. return 0;
  279. }
  280. static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  281. static void end_frame(AVFilterLink *inlink)
  282. {
  283. Frei0rContext *frei0r = inlink->dst->priv;
  284. AVFilterLink *outlink = inlink->dst->outputs[0];
  285. AVFilterBufferRef *inpicref = inlink->cur_buf;
  286. AVFilterBufferRef *outpicref = outlink->out_buf;
  287. frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
  288. (const uint32_t *)inpicref->data[0],
  289. (uint32_t *)outpicref->data[0]);
  290. avfilter_unref_buffer(inpicref);
  291. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  292. avfilter_end_frame(outlink);
  293. avfilter_unref_buffer(outpicref);
  294. }
  295. AVFilter avfilter_vf_frei0r = {
  296. .name = "frei0r",
  297. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  298. .query_formats = query_formats,
  299. .init = init,
  300. .uninit = uninit,
  301. .priv_size = sizeof(Frei0rContext),
  302. .inputs = (AVFilterPad[]) {{ .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .draw_slice = null_draw_slice,
  305. .config_props = config_input_props,
  306. .end_frame = end_frame,
  307. .min_perms = AV_PERM_READ },
  308. { .name = NULL}},
  309. .outputs = (AVFilterPad[]) {{ .name = "default",
  310. .type = AVMEDIA_TYPE_VIDEO, },
  311. { .name = NULL}},
  312. };