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.

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