You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

523 lines
16KB

  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. params++; /* skip ':' */
  134. ret = set_param(ctx, info, i, param);
  135. av_free(param);
  136. if (ret < 0)
  137. return ret;
  138. }
  139. av_log(ctx, AV_LOG_VERBOSE,
  140. "idx:%d name:'%s' type:%s explanation:'%s' ",
  141. i, info.name,
  142. info.type == F0R_PARAM_BOOL ? "bool" :
  143. info.type == F0R_PARAM_DOUBLE ? "double" :
  144. info.type == F0R_PARAM_COLOR ? "color" :
  145. info.type == F0R_PARAM_POSITION ? "position" :
  146. info.type == F0R_PARAM_STRING ? "string" : "unknown",
  147. info.explanation);
  148. #ifdef DEBUG
  149. av_log(ctx, AV_LOG_DEBUG, "value:");
  150. switch (info.type) {
  151. void *v;
  152. double d;
  153. char s[128];
  154. f0r_param_color_t col;
  155. f0r_param_position_t pos;
  156. case F0R_PARAM_BOOL:
  157. v = &d;
  158. s->get_param_value(s->instance, v, i);
  159. av_log(ctx, AV_LOG_DEBUG, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
  160. break;
  161. case F0R_PARAM_DOUBLE:
  162. v = &d;
  163. s->get_param_value(s->instance, v, i);
  164. av_log(ctx, AV_LOG_DEBUG, "%f", d);
  165. break;
  166. case F0R_PARAM_COLOR:
  167. v = &col;
  168. s->get_param_value(s->instance, v, i);
  169. av_log(ctx, AV_LOG_DEBUG, "%f/%f/%f", col.r, col.g, col.b);
  170. break;
  171. case F0R_PARAM_POSITION:
  172. v = &pos;
  173. s->get_param_value(s->instance, v, i);
  174. av_log(ctx, AV_LOG_DEBUG, "%f/%f", pos.x, pos.y);
  175. break;
  176. default: /* F0R_PARAM_STRING */
  177. v = s;
  178. s->get_param_value(s->instance, v, i);
  179. av_log(ctx, AV_LOG_DEBUG, "'%s'\n", s);
  180. break;
  181. }
  182. #endif
  183. av_log(ctx, AV_LOG_VERBOSE, "\n");
  184. }
  185. return 0;
  186. }
  187. static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
  188. {
  189. char path[1024];
  190. snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
  191. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
  192. return dlopen(path, RTLD_NOW|RTLD_LOCAL);
  193. }
  194. static av_cold int frei0r_init(AVFilterContext *ctx,
  195. const char *dl_name, int type)
  196. {
  197. Frei0rContext *s = ctx->priv;
  198. f0r_init_f f0r_init;
  199. f0r_get_plugin_info_f f0r_get_plugin_info;
  200. f0r_plugin_info_t *pi;
  201. char *path;
  202. if (!dl_name) {
  203. av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
  204. return AVERROR(EINVAL);
  205. }
  206. /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
  207. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  208. char *p, *ptr = NULL;
  209. for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
  210. if (s->dl_handle = load_path(ctx, p, dl_name))
  211. break;
  212. av_free(path);
  213. }
  214. if (!s->dl_handle && (path = getenv("HOME"))) {
  215. char prefix[1024];
  216. snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
  217. s->dl_handle = load_path(ctx, prefix, dl_name);
  218. }
  219. if (!s->dl_handle)
  220. s->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
  221. if (!s->dl_handle)
  222. s->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
  223. if (!s->dl_handle) {
  224. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
  225. return AVERROR(EINVAL);
  226. }
  227. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  228. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  229. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  230. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  231. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  232. !(s->update = load_sym(ctx, "f0r_update" )) ||
  233. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  234. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  235. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  236. return AVERROR(EINVAL);
  237. if (f0r_init() < 0) {
  238. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
  239. return AVERROR(EINVAL);
  240. }
  241. f0r_get_plugin_info(&s->plugin_info);
  242. pi = &s->plugin_info;
  243. if (pi->plugin_type != type) {
  244. av_log(ctx, AV_LOG_ERROR,
  245. "Invalid type '%s' for the plugin\n",
  246. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  247. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  248. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  249. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  250. return AVERROR(EINVAL);
  251. }
  252. av_log(ctx, AV_LOG_VERBOSE,
  253. "name:%s author:'%s' explanation:'%s' color_model:%s "
  254. "frei0r_version:%d version:%d.%d num_params:%d\n",
  255. pi->name, pi->author, pi->explanation,
  256. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  257. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  258. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  259. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  260. return 0;
  261. }
  262. static av_cold int filter_init(AVFilterContext *ctx)
  263. {
  264. Frei0rContext *s = ctx->priv;
  265. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  266. }
  267. static av_cold void uninit(AVFilterContext *ctx)
  268. {
  269. Frei0rContext *s = ctx->priv;
  270. if (s->destruct && s->instance)
  271. s->destruct(s->instance);
  272. if (s->deinit)
  273. s->deinit();
  274. if (s->dl_handle)
  275. dlclose(s->dl_handle);
  276. }
  277. static int config_input_props(AVFilterLink *inlink)
  278. {
  279. AVFilterContext *ctx = inlink->dst;
  280. Frei0rContext *s = ctx->priv;
  281. if (s->destruct && s->instance)
  282. s->destruct(s->instance);
  283. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  284. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  285. return AVERROR(EINVAL);
  286. }
  287. return set_params(ctx, s->params);
  288. }
  289. static int query_formats(AVFilterContext *ctx)
  290. {
  291. Frei0rContext *s = ctx->priv;
  292. AVFilterFormats *formats = NULL;
  293. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  294. ff_add_format(&formats, AV_PIX_FMT_BGRA);
  295. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  296. ff_add_format(&formats, AV_PIX_FMT_RGBA);
  297. } else { /* F0R_COLOR_MODEL_PACKED32 */
  298. static const enum AVPixelFormat pix_fmts[] = {
  299. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  300. };
  301. formats = ff_make_format_list(pix_fmts);
  302. }
  303. if (!formats)
  304. return AVERROR(ENOMEM);
  305. ff_set_common_formats(ctx, formats);
  306. return 0;
  307. }
  308. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  309. {
  310. Frei0rContext *s = inlink->dst->priv;
  311. AVFilterLink *outlink = inlink->dst->outputs[0];
  312. AVFrame *out;
  313. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  314. if (!out) {
  315. av_frame_free(&in);
  316. return AVERROR(ENOMEM);
  317. }
  318. av_frame_copy_props(out, in);
  319. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  320. (const uint32_t *)in->data[0],
  321. (uint32_t *)out->data[0]);
  322. av_frame_free(&in);
  323. return ff_filter_frame(outlink, out);
  324. }
  325. #define OFFSET(x) offsetof(Frei0rContext, x)
  326. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  327. static const AVOption filter_options[] = {
  328. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  329. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  330. { NULL },
  331. };
  332. static const AVClass filter_class = {
  333. .class_name = "frei0r",
  334. .item_name = av_default_item_name,
  335. .option = filter_options,
  336. .version = LIBAVUTIL_VERSION_INT,
  337. };
  338. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  339. {
  340. .name = "default",
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. .config_props = config_input_props,
  343. .filter_frame = filter_frame,
  344. },
  345. { NULL }
  346. };
  347. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  348. {
  349. .name = "default",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. },
  352. { NULL }
  353. };
  354. AVFilter avfilter_vf_frei0r = {
  355. .name = "frei0r",
  356. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  357. .query_formats = query_formats,
  358. .init = filter_init,
  359. .uninit = uninit,
  360. .priv_size = sizeof(Frei0rContext),
  361. .priv_class = &filter_class,
  362. .inputs = avfilter_vf_frei0r_inputs,
  363. .outputs = avfilter_vf_frei0r_outputs,
  364. };
  365. static av_cold int source_init(AVFilterContext *ctx)
  366. {
  367. Frei0rContext *s = ctx->priv;
  368. AVRational frame_rate_q;
  369. if (av_parse_video_size(&s->w, &s->h, s->size) < 0) {
  370. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", s->size);
  371. return AVERROR(EINVAL);
  372. }
  373. if (av_parse_video_rate(&frame_rate_q, s->framerate) < 0 ||
  374. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  375. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", s->framerate);
  376. return AVERROR(EINVAL);
  377. }
  378. s->time_base.num = frame_rate_q.den;
  379. s->time_base.den = frame_rate_q.num;
  380. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  381. }
  382. static int source_config_props(AVFilterLink *outlink)
  383. {
  384. AVFilterContext *ctx = outlink->src;
  385. Frei0rContext *s = ctx->priv;
  386. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  387. return AVERROR(EINVAL);
  388. outlink->w = s->w;
  389. outlink->h = s->h;
  390. outlink->time_base = s->time_base;
  391. if (s->destruct && s->instance)
  392. s->destruct(s->instance);
  393. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  394. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  395. return AVERROR(EINVAL);
  396. }
  397. return set_params(ctx, s->params);
  398. }
  399. static int source_request_frame(AVFilterLink *outlink)
  400. {
  401. Frei0rContext *s = outlink->src->priv;
  402. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  403. if (!frame)
  404. return AVERROR(ENOMEM);
  405. frame->sample_aspect_ratio = (AVRational) {1, 1};
  406. frame->pts = s->pts++;
  407. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  408. NULL, (uint32_t *)frame->data[0]);
  409. return ff_filter_frame(outlink, frame);
  410. }
  411. static const AVOption src_options[] = {
  412. { "size", "Dimensions of the generated video.", OFFSET(size), AV_OPT_TYPE_STRING, { .str = "" }, .flags = FLAGS },
  413. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
  414. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  415. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  416. { NULL },
  417. };
  418. static const AVClass src_class = {
  419. .class_name = "frei0r_src",
  420. .item_name = av_default_item_name,
  421. .option = src_options,
  422. .version = LIBAVUTIL_VERSION_INT,
  423. };
  424. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  425. {
  426. .name = "default",
  427. .type = AVMEDIA_TYPE_VIDEO,
  428. .request_frame = source_request_frame,
  429. .config_props = source_config_props
  430. },
  431. { NULL }
  432. };
  433. AVFilter avfilter_vsrc_frei0r_src = {
  434. .name = "frei0r_src",
  435. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  436. .priv_size = sizeof(Frei0rContext),
  437. .priv_class = &src_class,
  438. .init = source_init,
  439. .uninit = uninit,
  440. .query_formats = query_formats,
  441. .inputs = NULL,
  442. .outputs = avfilter_vsrc_frei0r_src_outputs,
  443. };