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.

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