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.

538 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. AVRational framerate;
  65. /* only used by the source */
  66. int w, h;
  67. AVRational time_base;
  68. uint64_t pts;
  69. } Frei0rContext;
  70. static void *load_sym(AVFilterContext *ctx, const char *sym_name)
  71. {
  72. Frei0rContext *s = ctx->priv;
  73. void *sym = dlsym(s->dl_handle, sym_name);
  74. if (!sym)
  75. av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
  76. return sym;
  77. }
  78. static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
  79. {
  80. Frei0rContext *s = ctx->priv;
  81. union {
  82. double d;
  83. f0r_param_color_t col;
  84. f0r_param_position_t pos;
  85. } val;
  86. char *tail;
  87. uint8_t rgba[4];
  88. switch (info.type) {
  89. case F0R_PARAM_BOOL:
  90. if (!strcmp(param, "y")) val.d = 1.0;
  91. else if (!strcmp(param, "n")) val.d = 0.0;
  92. else goto fail;
  93. break;
  94. case F0R_PARAM_DOUBLE:
  95. val.d = strtod(param, &tail);
  96. if (*tail || val.d == HUGE_VAL)
  97. goto fail;
  98. break;
  99. case F0R_PARAM_COLOR:
  100. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  101. if (av_parse_color(rgba, param, -1, ctx) < 0)
  102. goto fail;
  103. val.col.r = rgba[0] / 255.0;
  104. val.col.g = rgba[1] / 255.0;
  105. val.col.b = rgba[2] / 255.0;
  106. }
  107. break;
  108. case F0R_PARAM_POSITION:
  109. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  110. goto fail;
  111. break;
  112. }
  113. s->set_param_value(s->instance, &val, index);
  114. return 0;
  115. fail:
  116. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
  117. param, info.name);
  118. return AVERROR(EINVAL);
  119. }
  120. static int set_params(AVFilterContext *ctx, const char *params)
  121. {
  122. Frei0rContext *s = ctx->priv;
  123. int i;
  124. if (!params)
  125. return 0;
  126. for (i = 0; i < s->plugin_info.num_params; i++) {
  127. f0r_param_info_t info;
  128. char *param;
  129. int ret;
  130. s->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. 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'\n", s);
  181. break;
  182. }
  183. #endif
  184. av_log(ctx, AV_LOG_VERBOSE, "\n");
  185. }
  186. return 0;
  187. }
  188. static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
  189. {
  190. char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
  191. if (!path)
  192. return AVERROR(ENOMEM);
  193. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
  194. *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
  195. av_free(path);
  196. return 0;
  197. }
  198. static av_cold int frei0r_init(AVFilterContext *ctx,
  199. const char *dl_name, int type)
  200. {
  201. Frei0rContext *s = ctx->priv;
  202. f0r_init_f f0r_init;
  203. f0r_get_plugin_info_f f0r_get_plugin_info;
  204. f0r_plugin_info_t *pi;
  205. char *path;
  206. int ret = 0;
  207. if (!dl_name) {
  208. av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
  209. return AVERROR(EINVAL);
  210. }
  211. /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
  212. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  213. #ifdef _WIN32
  214. const char *separator = ";";
  215. #else
  216. const char *separator = ":";
  217. #endif
  218. char *p, *ptr = NULL;
  219. for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
  220. /* add additional trailing slash in case it is missing */
  221. char *p1 = av_asprintf("%s/", p);
  222. if (!p1) {
  223. ret = AVERROR(ENOMEM);
  224. goto check_path_end;
  225. }
  226. ret = load_path(ctx, &s->dl_handle, p1, dl_name);
  227. av_free(p1);
  228. if (ret < 0)
  229. goto check_path_end;
  230. if (s->dl_handle)
  231. break;
  232. }
  233. check_path_end:
  234. av_free(path);
  235. if (ret < 0)
  236. return ret;
  237. }
  238. if (!s->dl_handle && (path = getenv("HOME"))) {
  239. char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
  240. if (!prefix)
  241. return AVERROR(ENOMEM);
  242. ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
  243. av_free(prefix);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. if (!s->dl_handle) {
  248. ret = load_path(ctx, &s->dl_handle, "/usr/local/lib/frei0r-1/", dl_name);
  249. if (ret < 0)
  250. return ret;
  251. }
  252. if (!s->dl_handle) {
  253. ret = load_path(ctx, &s->dl_handle, "/usr/lib/frei0r-1/", dl_name);
  254. if (ret < 0)
  255. return ret;
  256. }
  257. if (!s->dl_handle) {
  258. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
  259. return AVERROR(EINVAL);
  260. }
  261. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  262. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  263. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  264. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  265. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  266. !(s->update = load_sym(ctx, "f0r_update" )) ||
  267. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  268. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  269. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  270. return AVERROR(EINVAL);
  271. if (f0r_init() < 0) {
  272. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module\n");
  273. return AVERROR(EINVAL);
  274. }
  275. f0r_get_plugin_info(&s->plugin_info);
  276. pi = &s->plugin_info;
  277. if (pi->plugin_type != type) {
  278. av_log(ctx, AV_LOG_ERROR,
  279. "Invalid type '%s' for the plugin\n",
  280. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  281. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  282. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  283. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  284. return AVERROR(EINVAL);
  285. }
  286. av_log(ctx, AV_LOG_VERBOSE,
  287. "name:%s author:'%s' explanation:'%s' color_model:%s "
  288. "frei0r_version:%d version:%d.%d num_params:%d\n",
  289. pi->name, pi->author, pi->explanation,
  290. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  291. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  292. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  293. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  294. return 0;
  295. }
  296. static av_cold int filter_init(AVFilterContext *ctx)
  297. {
  298. Frei0rContext *s = ctx->priv;
  299. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  300. }
  301. static av_cold void uninit(AVFilterContext *ctx)
  302. {
  303. Frei0rContext *s = ctx->priv;
  304. if (s->destruct && s->instance)
  305. s->destruct(s->instance);
  306. if (s->deinit)
  307. s->deinit();
  308. if (s->dl_handle)
  309. dlclose(s->dl_handle);
  310. }
  311. static int config_input_props(AVFilterLink *inlink)
  312. {
  313. AVFilterContext *ctx = inlink->dst;
  314. Frei0rContext *s = ctx->priv;
  315. if (s->destruct && s->instance)
  316. s->destruct(s->instance);
  317. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  318. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance\n");
  319. return AVERROR(EINVAL);
  320. }
  321. return set_params(ctx, s->params);
  322. }
  323. static int query_formats(AVFilterContext *ctx)
  324. {
  325. Frei0rContext *s = ctx->priv;
  326. AVFilterFormats *formats = NULL;
  327. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  328. ff_add_format(&formats, AV_PIX_FMT_BGRA);
  329. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  330. ff_add_format(&formats, AV_PIX_FMT_RGBA);
  331. } else { /* F0R_COLOR_MODEL_PACKED32 */
  332. static const enum AVPixelFormat pix_fmts[] = {
  333. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  334. };
  335. formats = ff_make_format_list(pix_fmts);
  336. }
  337. if (!formats)
  338. return AVERROR(ENOMEM);
  339. ff_set_common_formats(ctx, formats);
  340. return 0;
  341. }
  342. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  343. {
  344. Frei0rContext *s = inlink->dst->priv;
  345. AVFilterLink *outlink = inlink->dst->outputs[0];
  346. AVFrame *out;
  347. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  348. if (!out) {
  349. av_frame_free(&in);
  350. return AVERROR(ENOMEM);
  351. }
  352. av_frame_copy_props(out, in);
  353. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  354. (const uint32_t *)in->data[0],
  355. (uint32_t *)out->data[0]);
  356. av_frame_free(&in);
  357. return ff_filter_frame(outlink, out);
  358. }
  359. #define OFFSET(x) offsetof(Frei0rContext, x)
  360. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  361. static const AVOption frei0r_options[] = {
  362. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  363. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  364. { NULL },
  365. };
  366. AVFILTER_DEFINE_CLASS(frei0r);
  367. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  368. {
  369. .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .config_props = config_input_props,
  372. .filter_frame = filter_frame,
  373. },
  374. { NULL }
  375. };
  376. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  377. {
  378. .name = "default",
  379. .type = AVMEDIA_TYPE_VIDEO,
  380. },
  381. { NULL }
  382. };
  383. AVFilter avfilter_vf_frei0r = {
  384. .name = "frei0r",
  385. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  386. .query_formats = query_formats,
  387. .init = filter_init,
  388. .uninit = uninit,
  389. .priv_size = sizeof(Frei0rContext),
  390. .priv_class = &frei0r_class,
  391. .inputs = avfilter_vf_frei0r_inputs,
  392. .outputs = avfilter_vf_frei0r_outputs,
  393. };
  394. static av_cold int source_init(AVFilterContext *ctx)
  395. {
  396. Frei0rContext *s = ctx->priv;
  397. s->time_base.num = s->framerate.den;
  398. s->time_base.den = s->framerate.num;
  399. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  400. }
  401. static int source_config_props(AVFilterLink *outlink)
  402. {
  403. AVFilterContext *ctx = outlink->src;
  404. Frei0rContext *s = ctx->priv;
  405. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  406. return AVERROR(EINVAL);
  407. outlink->w = s->w;
  408. outlink->h = s->h;
  409. outlink->time_base = s->time_base;
  410. outlink->sample_aspect_ratio = (AVRational){1,1};
  411. if (s->destruct && s->instance)
  412. s->destruct(s->instance);
  413. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  414. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance\n");
  415. return AVERROR(EINVAL);
  416. }
  417. return set_params(ctx, s->params);
  418. }
  419. static int source_request_frame(AVFilterLink *outlink)
  420. {
  421. Frei0rContext *s = outlink->src->priv;
  422. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  423. if (!frame)
  424. return AVERROR(ENOMEM);
  425. frame->sample_aspect_ratio = (AVRational) {1, 1};
  426. frame->pts = s->pts++;
  427. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  428. NULL, (uint32_t *)frame->data[0]);
  429. return ff_filter_frame(outlink, frame);
  430. }
  431. static const AVOption frei0r_src_options[] = {
  432. { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
  433. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = FLAGS },
  434. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  435. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  436. { NULL },
  437. };
  438. AVFILTER_DEFINE_CLASS(frei0r_src);
  439. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  440. {
  441. .name = "default",
  442. .type = AVMEDIA_TYPE_VIDEO,
  443. .request_frame = source_request_frame,
  444. .config_props = source_config_props
  445. },
  446. { NULL }
  447. };
  448. AVFilter avfilter_vsrc_frei0r_src = {
  449. .name = "frei0r_src",
  450. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  451. .priv_size = sizeof(Frei0rContext),
  452. .priv_class = &frei0r_src_class,
  453. .init = source_init,
  454. .uninit = uninit,
  455. .query_formats = query_formats,
  456. .inputs = NULL,
  457. .outputs = avfilter_vsrc_frei0r_src_outputs,
  458. };