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.

554 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. /* #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 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 *frei0r = 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, &frei0r->dl_handle, p1, dl_name);
  227. av_free(p1);
  228. if (ret < 0)
  229. goto check_path_end;
  230. if (frei0r->dl_handle)
  231. break;
  232. }
  233. check_path_end:
  234. av_free(path);
  235. if (ret < 0)
  236. return ret;
  237. }
  238. if (!frei0r->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, &frei0r->dl_handle, prefix, dl_name);
  243. av_free(prefix);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. if (!frei0r->dl_handle) {
  248. ret = load_path(ctx, &frei0r->dl_handle, "/usr/local/lib/frei0r-1/", dl_name);
  249. if (ret < 0)
  250. return ret;
  251. }
  252. if (!frei0r->dl_handle) {
  253. ret = load_path(ctx, &frei0r->dl_handle, "/usr/lib/frei0r-1/", dl_name);
  254. if (ret < 0)
  255. return ret;
  256. }
  257. if (!frei0r->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. !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  264. !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  265. !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  266. !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
  267. !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
  268. !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
  269. !(frei0r->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(&frei0r->plugin_info);
  276. pi = &frei0r->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, const char *args)
  297. {
  298. Frei0rContext *frei0r = ctx->priv;
  299. return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_FILTER);
  300. }
  301. static av_cold void uninit(AVFilterContext *ctx)
  302. {
  303. Frei0rContext *frei0r = ctx->priv;
  304. if (frei0r->destruct && frei0r->instance)
  305. frei0r->destruct(frei0r->instance);
  306. if (frei0r->deinit)
  307. frei0r->deinit();
  308. if (frei0r->dl_handle)
  309. dlclose(frei0r->dl_handle);
  310. }
  311. static int config_input_props(AVFilterLink *inlink)
  312. {
  313. AVFilterContext *ctx = inlink->dst;
  314. Frei0rContext *frei0r = ctx->priv;
  315. if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
  316. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance\n");
  317. return AVERROR(EINVAL);
  318. }
  319. return set_params(ctx, frei0r->params);
  320. }
  321. static int query_formats(AVFilterContext *ctx)
  322. {
  323. Frei0rContext *frei0r = ctx->priv;
  324. AVFilterFormats *formats = NULL;
  325. if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  326. ff_add_format(&formats, AV_PIX_FMT_BGRA);
  327. } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  328. ff_add_format(&formats, AV_PIX_FMT_RGBA);
  329. } else { /* F0R_COLOR_MODEL_PACKED32 */
  330. static const enum AVPixelFormat pix_fmts[] = {
  331. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  332. };
  333. formats = ff_make_format_list(pix_fmts);
  334. }
  335. if (!formats)
  336. return AVERROR(ENOMEM);
  337. ff_set_common_formats(ctx, formats);
  338. return 0;
  339. }
  340. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  341. {
  342. Frei0rContext *frei0r = inlink->dst->priv;
  343. AVFilterLink *outlink = inlink->dst->outputs[0];
  344. AVFrame *out;
  345. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  346. if (!out) {
  347. av_frame_free(&in);
  348. return AVERROR(ENOMEM);
  349. }
  350. av_frame_copy_props(out, in);
  351. frei0r->update(frei0r->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  352. (const uint32_t *)in->data[0],
  353. (uint32_t *)out->data[0]);
  354. av_frame_free(&in);
  355. return ff_filter_frame(outlink, out);
  356. }
  357. #define OFFSET(x) offsetof(Frei0rContext, x)
  358. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  359. static const AVOption filter_options[] = {
  360. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  361. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  362. { NULL },
  363. };
  364. static const AVClass filter_class = {
  365. .class_name = "frei0r",
  366. .item_name = av_default_item_name,
  367. .option = filter_options,
  368. .version = LIBAVUTIL_VERSION_INT,
  369. };
  370. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  371. {
  372. .name = "default",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .config_props = config_input_props,
  375. .filter_frame = filter_frame,
  376. },
  377. { NULL }
  378. };
  379. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  380. {
  381. .name = "default",
  382. .type = AVMEDIA_TYPE_VIDEO,
  383. },
  384. { NULL }
  385. };
  386. AVFilter avfilter_vf_frei0r = {
  387. .name = "frei0r",
  388. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  389. .query_formats = query_formats,
  390. .init = filter_init,
  391. .uninit = uninit,
  392. .priv_size = sizeof(Frei0rContext),
  393. .priv_class = &filter_class,
  394. .inputs = avfilter_vf_frei0r_inputs,
  395. .outputs = avfilter_vf_frei0r_outputs,
  396. };
  397. static av_cold int source_init(AVFilterContext *ctx, const char *args)
  398. {
  399. Frei0rContext *frei0r = ctx->priv;
  400. AVRational frame_rate_q;
  401. if (av_parse_video_size(&frei0r->w, &frei0r->h, frei0r->size) < 0) {
  402. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frei0r->size);
  403. return AVERROR(EINVAL);
  404. }
  405. if (av_parse_video_rate(&frame_rate_q, frei0r->framerate) < 0) {
  406. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frei0r->framerate);
  407. return AVERROR(EINVAL);
  408. }
  409. frei0r->time_base.num = frame_rate_q.den;
  410. frei0r->time_base.den = frame_rate_q.num;
  411. return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  412. }
  413. static int source_config_props(AVFilterLink *outlink)
  414. {
  415. AVFilterContext *ctx = outlink->src;
  416. Frei0rContext *frei0r = ctx->priv;
  417. if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
  418. return AVERROR(EINVAL);
  419. outlink->w = frei0r->w;
  420. outlink->h = frei0r->h;
  421. outlink->time_base = frei0r->time_base;
  422. outlink->sample_aspect_ratio = (AVRational){1,1};
  423. if (!(frei0r->instance = frei0r->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, frei0r->params);
  428. }
  429. static int source_request_frame(AVFilterLink *outlink)
  430. {
  431. Frei0rContext *frei0r = 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 = frei0r->pts++;
  437. frei0r->update(frei0r->instance, av_rescale_q(frame->pts, frei0r->time_base, (AVRational){1,1000}),
  438. NULL, (uint32_t *)frame->data[0]);
  439. return ff_filter_frame(outlink, frame);
  440. }
  441. static const AVOption 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. static const AVClass src_class = {
  449. .class_name = "frei0r_src",
  450. .item_name = av_default_item_name,
  451. .option = src_options,
  452. .version = LIBAVUTIL_VERSION_INT,
  453. };
  454. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  455. {
  456. .name = "default",
  457. .type = AVMEDIA_TYPE_VIDEO,
  458. .request_frame = source_request_frame,
  459. .config_props = source_config_props
  460. },
  461. { NULL }
  462. };
  463. AVFilter avfilter_vsrc_frei0r_src = {
  464. .name = "frei0r_src",
  465. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  466. .priv_size = sizeof(Frei0rContext),
  467. .priv_class = &src_class,
  468. .init = source_init,
  469. .uninit = uninit,
  470. .query_formats = query_formats,
  471. .inputs = NULL,
  472. .outputs = avfilter_vsrc_frei0r_src_outputs,
  473. };