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.

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