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.

533 lines
17KB

  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'", 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 = getenv("FREI0R_PATH")) {
  208. while(*path) {
  209. char *ptr = av_get_token((const char **)&path, ":");
  210. if (!ptr)
  211. return AVERROR(ENOMEM);
  212. s->dl_handle = load_path(ctx, ptr, dl_name);
  213. av_freep(&ptr);
  214. if (s->dl_handle)
  215. break; /* found */
  216. if (*path)
  217. path++; /* skip ':' */
  218. }
  219. }
  220. if (!s->dl_handle && (path = getenv("HOME"))) {
  221. char prefix[1024];
  222. snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
  223. s->dl_handle = load_path(ctx, prefix, dl_name);
  224. }
  225. if (!s->dl_handle)
  226. s->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
  227. if (!s->dl_handle)
  228. s->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
  229. if (!s->dl_handle) {
  230. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
  231. return AVERROR(EINVAL);
  232. }
  233. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  234. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  235. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  236. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  237. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  238. !(s->update = load_sym(ctx, "f0r_update" )) ||
  239. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  240. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  241. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  242. return AVERROR(EINVAL);
  243. if (f0r_init() < 0) {
  244. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
  245. return AVERROR(EINVAL);
  246. }
  247. f0r_get_plugin_info(&s->plugin_info);
  248. pi = &s->plugin_info;
  249. if (pi->plugin_type != type) {
  250. av_log(ctx, AV_LOG_ERROR,
  251. "Invalid type '%s' for this plugin\n",
  252. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  253. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  254. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  255. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  256. return AVERROR(EINVAL);
  257. }
  258. av_log(ctx, AV_LOG_VERBOSE,
  259. "name:%s author:'%s' explanation:'%s' color_model:%s "
  260. "frei0r_version:%d version:%d.%d num_params:%d\n",
  261. pi->name, pi->author, pi->explanation,
  262. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  263. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  264. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  265. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  266. return 0;
  267. }
  268. static av_cold int filter_init(AVFilterContext *ctx)
  269. {
  270. Frei0rContext *s = ctx->priv;
  271. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  272. }
  273. static av_cold void uninit(AVFilterContext *ctx)
  274. {
  275. Frei0rContext *s = ctx->priv;
  276. if (s->destruct && s->instance)
  277. s->destruct(s->instance);
  278. if (s->deinit)
  279. s->deinit();
  280. if (s->dl_handle)
  281. dlclose(s->dl_handle);
  282. }
  283. static int config_input_props(AVFilterLink *inlink)
  284. {
  285. AVFilterContext *ctx = inlink->dst;
  286. Frei0rContext *s = ctx->priv;
  287. if (s->destruct && s->instance)
  288. s->destruct(s->instance);
  289. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  290. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  291. return AVERROR(EINVAL);
  292. }
  293. return set_params(ctx, s->params);
  294. }
  295. static int query_formats(AVFilterContext *ctx)
  296. {
  297. Frei0rContext *s = ctx->priv;
  298. AVFilterFormats *formats = NULL;
  299. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  300. ff_add_format(&formats, AV_PIX_FMT_BGRA);
  301. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  302. ff_add_format(&formats, AV_PIX_FMT_RGBA);
  303. } else { /* F0R_COLOR_MODEL_PACKED32 */
  304. static const enum AVPixelFormat pix_fmts[] = {
  305. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  306. };
  307. formats = ff_make_format_list(pix_fmts);
  308. }
  309. if (!formats)
  310. return AVERROR(ENOMEM);
  311. ff_set_common_formats(ctx, formats);
  312. return 0;
  313. }
  314. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  315. {
  316. Frei0rContext *s = inlink->dst->priv;
  317. AVFilterLink *outlink = inlink->dst->outputs[0];
  318. AVFrame *out;
  319. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  320. if (!out) {
  321. av_frame_free(&in);
  322. return AVERROR(ENOMEM);
  323. }
  324. av_frame_copy_props(out, in);
  325. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  326. (const uint32_t *)in->data[0],
  327. (uint32_t *)out->data[0]);
  328. av_frame_free(&in);
  329. return ff_filter_frame(outlink, out);
  330. }
  331. #define OFFSET(x) offsetof(Frei0rContext, x)
  332. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  333. static const AVOption filter_options[] = {
  334. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  335. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  336. { NULL },
  337. };
  338. static const AVClass filter_class = {
  339. .class_name = "frei0r",
  340. .item_name = av_default_item_name,
  341. .option = filter_options,
  342. .version = LIBAVUTIL_VERSION_INT,
  343. };
  344. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .config_props = config_input_props,
  349. .filter_frame = filter_frame,
  350. },
  351. { NULL }
  352. };
  353. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vf_frei0r = {
  361. .name = "frei0r",
  362. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  363. .query_formats = query_formats,
  364. .init = filter_init,
  365. .uninit = uninit,
  366. .priv_size = sizeof(Frei0rContext),
  367. .priv_class = &filter_class,
  368. .inputs = avfilter_vf_frei0r_inputs,
  369. .outputs = avfilter_vf_frei0r_outputs,
  370. };
  371. static av_cold int source_init(AVFilterContext *ctx)
  372. {
  373. Frei0rContext *s = ctx->priv;
  374. AVRational frame_rate_q;
  375. if (av_parse_video_size(&s->w, &s->h, s->size) < 0) {
  376. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'.\n", s->size);
  377. return AVERROR(EINVAL);
  378. }
  379. if (av_parse_video_rate(&frame_rate_q, s->framerate) < 0 ||
  380. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  381. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'.\n", s->framerate);
  382. return AVERROR(EINVAL);
  383. }
  384. s->time_base.num = frame_rate_q.den;
  385. s->time_base.den = frame_rate_q.num;
  386. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  387. }
  388. static int source_config_props(AVFilterLink *outlink)
  389. {
  390. AVFilterContext *ctx = outlink->src;
  391. Frei0rContext *s = ctx->priv;
  392. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  393. return AVERROR(EINVAL);
  394. outlink->w = s->w;
  395. outlink->h = s->h;
  396. outlink->time_base = s->time_base;
  397. if (s->destruct && s->instance)
  398. s->destruct(s->instance);
  399. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  400. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  401. return AVERROR(EINVAL);
  402. }
  403. if (!s->params) {
  404. av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
  405. return AVERROR(EINVAL);
  406. }
  407. return set_params(ctx, s->params);
  408. }
  409. static int source_request_frame(AVFilterLink *outlink)
  410. {
  411. Frei0rContext *s = outlink->src->priv;
  412. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  413. if (!frame)
  414. return AVERROR(ENOMEM);
  415. frame->sample_aspect_ratio = (AVRational) {1, 1};
  416. frame->pts = s->pts++;
  417. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  418. NULL, (uint32_t *)frame->data[0]);
  419. return ff_filter_frame(outlink, frame);
  420. }
  421. static const AVOption src_options[] = {
  422. { "size", "Dimensions of the generated video.", OFFSET(size), AV_OPT_TYPE_STRING, { .str = "" }, .flags = FLAGS },
  423. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
  424. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  425. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  426. { NULL },
  427. };
  428. static const AVClass src_class = {
  429. .class_name = "frei0r_src",
  430. .item_name = av_default_item_name,
  431. .option = src_options,
  432. .version = LIBAVUTIL_VERSION_INT,
  433. };
  434. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  435. {
  436. .name = "default",
  437. .type = AVMEDIA_TYPE_VIDEO,
  438. .request_frame = source_request_frame,
  439. .config_props = source_config_props
  440. },
  441. { NULL }
  442. };
  443. AVFilter ff_vsrc_frei0r_src = {
  444. .name = "frei0r_src",
  445. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  446. .priv_size = sizeof(Frei0rContext),
  447. .priv_class = &src_class,
  448. .init = source_init,
  449. .uninit = uninit,
  450. .query_formats = query_formats,
  451. .inputs = NULL,
  452. .outputs = avfilter_vsrc_frei0r_src_outputs,
  453. };