vf_frei0r.c 15 KB

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