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.

549 lines
17KB

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