vf_frei0r.c 16 KB

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