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.

557 lines
18KB

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