vf_frei0r.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 "libavutil/imgutils.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
  31. typedef void (*f0r_destruct_f)(f0r_instance_t instance);
  32. typedef void (*f0r_deinit_f)(void);
  33. typedef int (*f0r_init_f)(void);
  34. typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
  35. typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
  36. typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
  37. 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);
  38. typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  39. typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  40. typedef struct Frei0rContext {
  41. f0r_update_f update;
  42. void *dl_handle; /* dynamic library handle */
  43. f0r_instance_t instance;
  44. f0r_plugin_info_t plugin_info;
  45. f0r_get_param_info_f get_param_info;
  46. f0r_get_param_value_f get_param_value;
  47. f0r_set_param_value_f set_param_value;
  48. f0r_construct_f construct;
  49. f0r_destruct_f destruct;
  50. f0r_deinit_f deinit;
  51. char params[256];
  52. /* only used by the source */
  53. int w, h;
  54. AVRational time_base;
  55. uint64_t pts;
  56. } Frei0rContext;
  57. static void *load_sym(AVFilterContext *ctx, const char *sym_name)
  58. {
  59. Frei0rContext *frei0r = ctx->priv;
  60. void *sym = dlsym(frei0r->dl_handle, sym_name);
  61. if (!sym)
  62. av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
  63. return sym;
  64. }
  65. static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
  66. {
  67. Frei0rContext *frei0r = ctx->priv;
  68. union {
  69. double d;
  70. f0r_param_color_t col;
  71. f0r_param_position_t pos;
  72. } val;
  73. char *tail;
  74. uint8_t rgba[4];
  75. switch (info.type) {
  76. case F0R_PARAM_BOOL:
  77. if (!strcmp(param, "y")) val.d = 1.0;
  78. else if (!strcmp(param, "n")) val.d = 0.0;
  79. else goto fail;
  80. break;
  81. case F0R_PARAM_DOUBLE:
  82. val.d = strtod(param, &tail);
  83. if (*tail || val.d == HUGE_VAL)
  84. goto fail;
  85. break;
  86. case F0R_PARAM_COLOR:
  87. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  88. if (av_parse_color(rgba, param, -1, ctx) < 0)
  89. goto fail;
  90. val.col.r = rgba[0] / 255.0;
  91. val.col.g = rgba[1] / 255.0;
  92. val.col.b = rgba[2] / 255.0;
  93. }
  94. break;
  95. case F0R_PARAM_POSITION:
  96. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  97. goto fail;
  98. break;
  99. }
  100. frei0r->set_param_value(frei0r->instance, &val, index);
  101. return 0;
  102. fail:
  103. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
  104. param, info.name);
  105. return AVERROR(EINVAL);
  106. }
  107. static int set_params(AVFilterContext *ctx, const char *params)
  108. {
  109. Frei0rContext *frei0r = ctx->priv;
  110. int i;
  111. for (i = 0; i < frei0r->plugin_info.num_params; i++) {
  112. f0r_param_info_t info;
  113. char *param;
  114. int ret;
  115. frei0r->get_param_info(&info, i);
  116. if (*params) {
  117. if (!(param = av_get_token(&params, ":")))
  118. return AVERROR(ENOMEM);
  119. params++; /* skip ':' */
  120. ret = set_param(ctx, info, i, param);
  121. av_free(param);
  122. if (ret < 0)
  123. return ret;
  124. }
  125. av_log(ctx, AV_LOG_INFO,
  126. "idx:%d name:'%s' type:%s explanation:'%s' ",
  127. i, info.name,
  128. info.type == F0R_PARAM_BOOL ? "bool" :
  129. info.type == F0R_PARAM_DOUBLE ? "double" :
  130. info.type == F0R_PARAM_COLOR ? "color" :
  131. info.type == F0R_PARAM_POSITION ? "position" :
  132. info.type == F0R_PARAM_STRING ? "string" : "unknown",
  133. info.explanation);
  134. #ifdef DEBUG
  135. av_log(ctx, AV_LOG_INFO, "value:");
  136. switch (info.type) {
  137. void *v;
  138. double d;
  139. char s[128];
  140. f0r_param_color_t col;
  141. f0r_param_position_t pos;
  142. case F0R_PARAM_BOOL:
  143. v = &d;
  144. frei0r->get_param_value(frei0r->instance, v, i);
  145. av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
  146. break;
  147. case F0R_PARAM_DOUBLE:
  148. v = &d;
  149. frei0r->get_param_value(frei0r->instance, v, i);
  150. av_log(ctx, AV_LOG_INFO, "%f", d);
  151. break;
  152. case F0R_PARAM_COLOR:
  153. v = &col;
  154. frei0r->get_param_value(frei0r->instance, v, i);
  155. av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
  156. break;
  157. case F0R_PARAM_POSITION:
  158. v = &pos;
  159. frei0r->get_param_value(frei0r->instance, v, i);
  160. av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
  161. break;
  162. default: /* F0R_PARAM_STRING */
  163. v = s;
  164. frei0r->get_param_value(frei0r->instance, v, i);
  165. av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
  166. break;
  167. }
  168. #endif
  169. av_log(ctx, AV_LOG_INFO, "\n");
  170. }
  171. return 0;
  172. }
  173. static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
  174. {
  175. char path[1024];
  176. snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
  177. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
  178. return dlopen(path, RTLD_NOW|RTLD_LOCAL);
  179. }
  180. static av_cold int frei0r_init(AVFilterContext *ctx,
  181. const char *dl_name, int type)
  182. {
  183. Frei0rContext *frei0r = ctx->priv;
  184. f0r_init_f f0r_init;
  185. f0r_get_plugin_info_f f0r_get_plugin_info;
  186. f0r_plugin_info_t *pi;
  187. char *path;
  188. /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
  189. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  190. char *p, *ptr = NULL;
  191. for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
  192. if (frei0r->dl_handle = load_path(ctx, p, dl_name))
  193. break;
  194. av_free(path);
  195. }
  196. if (!frei0r->dl_handle && (path = getenv("HOME"))) {
  197. char prefix[1024];
  198. snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
  199. frei0r->dl_handle = load_path(ctx, prefix, dl_name);
  200. }
  201. if (!frei0r->dl_handle)
  202. frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
  203. if (!frei0r->dl_handle)
  204. frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
  205. if (!frei0r->dl_handle) {
  206. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
  207. return AVERROR(EINVAL);
  208. }
  209. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  210. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  211. !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  212. !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  213. !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  214. !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
  215. !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
  216. !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
  217. !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
  218. return AVERROR(EINVAL);
  219. if (f0r_init() < 0) {
  220. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
  221. return AVERROR(EINVAL);
  222. }
  223. f0r_get_plugin_info(&frei0r->plugin_info);
  224. pi = &frei0r->plugin_info;
  225. if (pi->plugin_type != type) {
  226. av_log(ctx, AV_LOG_ERROR,
  227. "Invalid type '%s' for the plugin\n",
  228. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  229. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  230. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  231. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  232. return AVERROR(EINVAL);
  233. }
  234. av_log(ctx, AV_LOG_INFO,
  235. "name:%s author:'%s' explanation:'%s' color_model:%s "
  236. "frei0r_version:%d version:%d.%d num_params:%d\n",
  237. pi->name, pi->author, pi->explanation,
  238. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  239. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  240. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  241. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  242. return 0;
  243. }
  244. static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
  245. {
  246. Frei0rContext *frei0r = ctx->priv;
  247. char dl_name[1024], c;
  248. *frei0r->params = 0;
  249. if (args)
  250. sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
  251. return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
  252. }
  253. static av_cold void uninit(AVFilterContext *ctx)
  254. {
  255. Frei0rContext *frei0r = ctx->priv;
  256. if (frei0r->destruct && frei0r->instance)
  257. frei0r->destruct(frei0r->instance);
  258. if (frei0r->deinit)
  259. frei0r->deinit();
  260. if (frei0r->dl_handle)
  261. dlclose(frei0r->dl_handle);
  262. memset(frei0r, 0, sizeof(*frei0r));
  263. }
  264. static int config_input_props(AVFilterLink *inlink)
  265. {
  266. AVFilterContext *ctx = inlink->dst;
  267. Frei0rContext *frei0r = ctx->priv;
  268. if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
  269. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  270. return AVERROR(EINVAL);
  271. }
  272. return set_params(ctx, frei0r->params);
  273. }
  274. static int query_formats(AVFilterContext *ctx)
  275. {
  276. Frei0rContext *frei0r = ctx->priv;
  277. AVFilterFormats *formats = NULL;
  278. if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  279. avfilter_add_format(&formats, PIX_FMT_BGRA);
  280. } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  281. avfilter_add_format(&formats, PIX_FMT_RGBA);
  282. } else { /* F0R_COLOR_MODEL_PACKED32 */
  283. static const enum PixelFormat pix_fmts[] = {
  284. PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
  285. };
  286. formats = avfilter_make_format_list(pix_fmts);
  287. }
  288. if (!formats)
  289. return AVERROR(ENOMEM);
  290. avfilter_set_common_pixel_formats(ctx, formats);
  291. return 0;
  292. }
  293. static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  294. static void end_frame(AVFilterLink *inlink)
  295. {
  296. Frei0rContext *frei0r = inlink->dst->priv;
  297. AVFilterLink *outlink = inlink->dst->outputs[0];
  298. AVFilterBufferRef *inpicref = inlink->cur_buf;
  299. AVFilterBufferRef *outpicref = outlink->out_buf;
  300. frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
  301. (const uint32_t *)inpicref->data[0],
  302. (uint32_t *)outpicref->data[0]);
  303. avfilter_unref_buffer(inpicref);
  304. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  305. avfilter_end_frame(outlink);
  306. avfilter_unref_buffer(outpicref);
  307. }
  308. AVFilter avfilter_vf_frei0r = {
  309. .name = "frei0r",
  310. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  311. .query_formats = query_formats,
  312. .init = filter_init,
  313. .uninit = uninit,
  314. .priv_size = sizeof(Frei0rContext),
  315. .inputs = (AVFilterPad[]) {{ .name = "default",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .draw_slice = null_draw_slice,
  318. .config_props = config_input_props,
  319. .end_frame = end_frame,
  320. .min_perms = AV_PERM_READ },
  321. { .name = NULL}},
  322. .outputs = (AVFilterPad[]) {{ .name = "default",
  323. .type = AVMEDIA_TYPE_VIDEO, },
  324. { .name = NULL}},
  325. };
  326. static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
  327. {
  328. Frei0rContext *frei0r = ctx->priv;
  329. char dl_name[1024], c;
  330. char frame_size[128] = "";
  331. char frame_rate[128] = "";
  332. AVRational frame_rate_q;
  333. memset(frei0r->params, 0, sizeof(frei0r->params));
  334. if (args)
  335. sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
  336. frame_size, frame_rate, dl_name, &c, frei0r->params);
  337. if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
  338. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
  339. return AVERROR(EINVAL);
  340. }
  341. if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
  342. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  343. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
  344. return AVERROR(EINVAL);
  345. }
  346. frei0r->time_base.num = frame_rate_q.den;
  347. frei0r->time_base.den = frame_rate_q.num;
  348. return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
  349. }
  350. static int source_config_props(AVFilterLink *outlink)
  351. {
  352. AVFilterContext *ctx = outlink->src;
  353. Frei0rContext *frei0r = ctx->priv;
  354. if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
  355. return AVERROR(EINVAL);
  356. outlink->w = frei0r->w;
  357. outlink->h = frei0r->h;
  358. outlink->time_base = frei0r->time_base;
  359. if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
  360. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  361. return AVERROR(EINVAL);
  362. }
  363. return set_params(ctx, frei0r->params);
  364. }
  365. static int source_request_frame(AVFilterLink *outlink)
  366. {
  367. Frei0rContext *frei0r = outlink->src->priv;
  368. AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  369. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  370. picref->pts = frei0r->pts++;
  371. picref->pos = -1;
  372. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  373. frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
  374. NULL, (uint32_t *)picref->data[0]);
  375. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  376. avfilter_end_frame(outlink);
  377. avfilter_unref_buffer(picref);
  378. return 0;
  379. }
  380. AVFilter avfilter_vsrc_frei0r_src = {
  381. .name = "frei0r_src",
  382. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  383. .priv_size = sizeof(Frei0rContext),
  384. .init = source_init,
  385. .uninit = uninit,
  386. .query_formats = query_formats,
  387. .inputs = (AVFilterPad[]) {{ .name = NULL}},
  388. .outputs = (AVFilterPad[]) {{ .name = "default",
  389. .type = AVMEDIA_TYPE_VIDEO,
  390. .request_frame = source_request_frame,
  391. .config_props = source_config_props },
  392. { .name = NULL}},
  393. };