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.

469 lines
16KB

  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 "libavutil/avstring.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/parseutils.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "video.h"
  33. typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
  34. typedef void (*f0r_destruct_f)(f0r_instance_t instance);
  35. typedef void (*f0r_deinit_f)(void);
  36. typedef int (*f0r_init_f)(void);
  37. typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
  38. typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
  39. typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
  40. 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);
  41. typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  42. typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  43. typedef struct Frei0rContext {
  44. f0r_update_f update;
  45. void *dl_handle; /* dynamic library handle */
  46. f0r_instance_t instance;
  47. f0r_plugin_info_t plugin_info;
  48. f0r_get_param_info_f get_param_info;
  49. f0r_get_param_value_f get_param_value;
  50. f0r_set_param_value_f set_param_value;
  51. f0r_construct_f construct;
  52. f0r_destruct_f destruct;
  53. f0r_deinit_f deinit;
  54. char params[256];
  55. /* only used by the source */
  56. int w, h;
  57. AVRational time_base;
  58. uint64_t pts;
  59. } Frei0rContext;
  60. static void *load_sym(AVFilterContext *ctx, const char *sym_name)
  61. {
  62. Frei0rContext *frei0r = ctx->priv;
  63. void *sym = dlsym(frei0r->dl_handle, sym_name);
  64. if (!sym)
  65. av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module\n", sym_name);
  66. return sym;
  67. }
  68. static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
  69. {
  70. Frei0rContext *frei0r = ctx->priv;
  71. union {
  72. double d;
  73. f0r_param_color_t col;
  74. f0r_param_position_t pos;
  75. } val;
  76. char *tail;
  77. uint8_t rgba[4];
  78. switch (info.type) {
  79. case F0R_PARAM_BOOL:
  80. if (!strcmp(param, "y")) val.d = 1.0;
  81. else if (!strcmp(param, "n")) val.d = 0.0;
  82. else goto fail;
  83. break;
  84. case F0R_PARAM_DOUBLE:
  85. val.d = strtod(param, &tail);
  86. if (*tail || val.d == HUGE_VAL)
  87. goto fail;
  88. break;
  89. case F0R_PARAM_COLOR:
  90. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  91. if (av_parse_color(rgba, param, -1, ctx) < 0)
  92. goto fail;
  93. val.col.r = rgba[0] / 255.0;
  94. val.col.g = rgba[1] / 255.0;
  95. val.col.b = rgba[2] / 255.0;
  96. }
  97. break;
  98. case F0R_PARAM_POSITION:
  99. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  100. goto fail;
  101. break;
  102. }
  103. frei0r->set_param_value(frei0r->instance, &val, index);
  104. return 0;
  105. fail:
  106. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n",
  107. param, info.name);
  108. return AVERROR(EINVAL);
  109. }
  110. static int set_params(AVFilterContext *ctx, const char *params)
  111. {
  112. Frei0rContext *frei0r = ctx->priv;
  113. int i;
  114. for (i = 0; i < frei0r->plugin_info.num_params; i++) {
  115. f0r_param_info_t info;
  116. char *param;
  117. int ret;
  118. frei0r->get_param_info(&info, i);
  119. if (*params) {
  120. if (!(param = av_get_token(&params, ":")))
  121. return AVERROR(ENOMEM);
  122. params++; /* skip ':' */
  123. ret = set_param(ctx, info, i, param);
  124. av_free(param);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. av_log(ctx, AV_LOG_INFO,
  129. "idx:%d name:'%s' type:%s explanation:'%s' ",
  130. i, info.name,
  131. info.type == F0R_PARAM_BOOL ? "bool" :
  132. info.type == F0R_PARAM_DOUBLE ? "double" :
  133. info.type == F0R_PARAM_COLOR ? "color" :
  134. info.type == F0R_PARAM_POSITION ? "position" :
  135. info.type == F0R_PARAM_STRING ? "string" : "unknown",
  136. info.explanation);
  137. #ifdef DEBUG
  138. av_log(ctx, AV_LOG_INFO, "value:");
  139. switch (info.type) {
  140. void *v;
  141. double d;
  142. char s[128];
  143. f0r_param_color_t col;
  144. f0r_param_position_t pos;
  145. case F0R_PARAM_BOOL:
  146. v = &d;
  147. frei0r->get_param_value(frei0r->instance, v, i);
  148. av_log(ctx, AV_LOG_INFO, "%s", d >= 0.5 && d <= 1.0 ? "y" : "n");
  149. break;
  150. case F0R_PARAM_DOUBLE:
  151. v = &d;
  152. frei0r->get_param_value(frei0r->instance, v, i);
  153. av_log(ctx, AV_LOG_INFO, "%f", d);
  154. break;
  155. case F0R_PARAM_COLOR:
  156. v = &col;
  157. frei0r->get_param_value(frei0r->instance, v, i);
  158. av_log(ctx, AV_LOG_INFO, "%f/%f/%f", col.r, col.g, col.b);
  159. break;
  160. case F0R_PARAM_POSITION:
  161. v = &pos;
  162. frei0r->get_param_value(frei0r->instance, v, i);
  163. av_log(ctx, AV_LOG_INFO, "%lf/%lf", pos.x, pos.y);
  164. break;
  165. default: /* F0R_PARAM_STRING */
  166. v = s;
  167. frei0r->get_param_value(frei0r->instance, v, i);
  168. av_log(ctx, AV_LOG_INFO, "'%s'\n", s);
  169. break;
  170. }
  171. #endif
  172. av_log(ctx, AV_LOG_INFO, "\n");
  173. }
  174. return 0;
  175. }
  176. static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
  177. {
  178. char path[1024];
  179. snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
  180. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'\n", path);
  181. return dlopen(path, RTLD_NOW|RTLD_LOCAL);
  182. }
  183. static av_cold int frei0r_init(AVFilterContext *ctx,
  184. const char *dl_name, int type)
  185. {
  186. Frei0rContext *frei0r = ctx->priv;
  187. f0r_init_f f0r_init;
  188. f0r_get_plugin_info_f f0r_get_plugin_info;
  189. f0r_plugin_info_t *pi;
  190. char *path;
  191. /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
  192. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  193. char *p, *ptr = NULL;
  194. for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
  195. if (frei0r->dl_handle = load_path(ctx, p, dl_name))
  196. break;
  197. av_free(path);
  198. }
  199. if (!frei0r->dl_handle && (path = getenv("HOME"))) {
  200. char prefix[1024];
  201. snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
  202. frei0r->dl_handle = load_path(ctx, prefix, dl_name);
  203. }
  204. if (!frei0r->dl_handle)
  205. frei0r->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
  206. if (!frei0r->dl_handle)
  207. frei0r->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
  208. if (!frei0r->dl_handle) {
  209. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'\n", dl_name);
  210. return AVERROR(EINVAL);
  211. }
  212. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  213. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  214. !(frei0r->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  215. !(frei0r->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  216. !(frei0r->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  217. !(frei0r->update = load_sym(ctx, "f0r_update" )) ||
  218. !(frei0r->construct = load_sym(ctx, "f0r_construct" )) ||
  219. !(frei0r->destruct = load_sym(ctx, "f0r_destruct" )) ||
  220. !(frei0r->deinit = load_sym(ctx, "f0r_deinit" )))
  221. return AVERROR(EINVAL);
  222. if (f0r_init() < 0) {
  223. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module");
  224. return AVERROR(EINVAL);
  225. }
  226. f0r_get_plugin_info(&frei0r->plugin_info);
  227. pi = &frei0r->plugin_info;
  228. if (pi->plugin_type != type) {
  229. av_log(ctx, AV_LOG_ERROR,
  230. "Invalid type '%s' for the plugin\n",
  231. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  232. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  233. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  234. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  235. return AVERROR(EINVAL);
  236. }
  237. av_log(ctx, AV_LOG_INFO,
  238. "name:%s author:'%s' explanation:'%s' color_model:%s "
  239. "frei0r_version:%d version:%d.%d num_params:%d\n",
  240. pi->name, pi->author, pi->explanation,
  241. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  242. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  243. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  244. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  245. return 0;
  246. }
  247. static av_cold int filter_init(AVFilterContext *ctx, const char *args, void *opaque)
  248. {
  249. Frei0rContext *frei0r = ctx->priv;
  250. char dl_name[1024], c;
  251. *frei0r->params = 0;
  252. if (args)
  253. sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
  254. return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
  255. }
  256. static av_cold void uninit(AVFilterContext *ctx)
  257. {
  258. Frei0rContext *frei0r = ctx->priv;
  259. if (frei0r->destruct && frei0r->instance)
  260. frei0r->destruct(frei0r->instance);
  261. if (frei0r->deinit)
  262. frei0r->deinit();
  263. if (frei0r->dl_handle)
  264. dlclose(frei0r->dl_handle);
  265. memset(frei0r, 0, sizeof(*frei0r));
  266. }
  267. static int config_input_props(AVFilterLink *inlink)
  268. {
  269. AVFilterContext *ctx = inlink->dst;
  270. Frei0rContext *frei0r = ctx->priv;
  271. if (!(frei0r->instance = frei0r->construct(inlink->w, inlink->h))) {
  272. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  273. return AVERROR(EINVAL);
  274. }
  275. return set_params(ctx, frei0r->params);
  276. }
  277. static int query_formats(AVFilterContext *ctx)
  278. {
  279. Frei0rContext *frei0r = ctx->priv;
  280. AVFilterFormats *formats = NULL;
  281. if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  282. ff_add_format(&formats, PIX_FMT_BGRA);
  283. } else if (frei0r->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  284. ff_add_format(&formats, PIX_FMT_RGBA);
  285. } else { /* F0R_COLOR_MODEL_PACKED32 */
  286. static const enum PixelFormat pix_fmts[] = {
  287. PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_ARGB, PIX_FMT_NONE
  288. };
  289. formats = ff_make_format_list(pix_fmts);
  290. }
  291. if (!formats)
  292. return AVERROR(ENOMEM);
  293. ff_set_common_formats(ctx, formats);
  294. return 0;
  295. }
  296. static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  297. static void end_frame(AVFilterLink *inlink)
  298. {
  299. Frei0rContext *frei0r = inlink->dst->priv;
  300. AVFilterLink *outlink = inlink->dst->outputs[0];
  301. AVFilterBufferRef *inpicref = inlink->cur_buf;
  302. AVFilterBufferRef *outpicref = outlink->out_buf;
  303. frei0r->update(frei0r->instance, inpicref->pts * av_q2d(inlink->time_base) * 1000,
  304. (const uint32_t *)inpicref->data[0],
  305. (uint32_t *)outpicref->data[0]);
  306. avfilter_unref_buffer(inpicref);
  307. ff_draw_slice(outlink, 0, outlink->h, 1);
  308. ff_end_frame(outlink);
  309. avfilter_unref_buffer(outpicref);
  310. }
  311. AVFilter avfilter_vf_frei0r = {
  312. .name = "frei0r",
  313. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  314. .query_formats = query_formats,
  315. .init = filter_init,
  316. .uninit = uninit,
  317. .priv_size = sizeof(Frei0rContext),
  318. .inputs = (const AVFilterPad[]) {{ .name = "default",
  319. .type = AVMEDIA_TYPE_VIDEO,
  320. .draw_slice = null_draw_slice,
  321. .config_props = config_input_props,
  322. .end_frame = end_frame,
  323. .min_perms = AV_PERM_READ },
  324. { .name = NULL}},
  325. .outputs = (const AVFilterPad[]) {{ .name = "default",
  326. .type = AVMEDIA_TYPE_VIDEO, },
  327. { .name = NULL}},
  328. };
  329. static av_cold int source_init(AVFilterContext *ctx, const char *args, void *opaque)
  330. {
  331. Frei0rContext *frei0r = ctx->priv;
  332. char dl_name[1024], c;
  333. char frame_size[128] = "";
  334. char frame_rate[128] = "";
  335. AVRational frame_rate_q;
  336. memset(frei0r->params, 0, sizeof(frei0r->params));
  337. if (args)
  338. sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
  339. frame_size, frame_rate, dl_name, &c, frei0r->params);
  340. if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
  341. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
  342. return AVERROR(EINVAL);
  343. }
  344. if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
  345. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  346. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
  347. return AVERROR(EINVAL);
  348. }
  349. frei0r->time_base.num = frame_rate_q.den;
  350. frei0r->time_base.den = frame_rate_q.num;
  351. return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
  352. }
  353. static int source_config_props(AVFilterLink *outlink)
  354. {
  355. AVFilterContext *ctx = outlink->src;
  356. Frei0rContext *frei0r = ctx->priv;
  357. if (av_image_check_size(frei0r->w, frei0r->h, 0, ctx) < 0)
  358. return AVERROR(EINVAL);
  359. outlink->w = frei0r->w;
  360. outlink->h = frei0r->h;
  361. outlink->time_base = frei0r->time_base;
  362. outlink->sample_aspect_ratio = (AVRational){1,1};
  363. if (!(frei0r->instance = frei0r->construct(outlink->w, outlink->h))) {
  364. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance");
  365. return AVERROR(EINVAL);
  366. }
  367. return set_params(ctx, frei0r->params);
  368. }
  369. static int source_request_frame(AVFilterLink *outlink)
  370. {
  371. Frei0rContext *frei0r = outlink->src->priv;
  372. AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  373. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  374. picref->pts = frei0r->pts++;
  375. picref->pos = -1;
  376. ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  377. frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
  378. NULL, (uint32_t *)picref->data[0]);
  379. ff_draw_slice(outlink, 0, outlink->h, 1);
  380. ff_end_frame(outlink);
  381. avfilter_unref_buffer(picref);
  382. return 0;
  383. }
  384. AVFilter avfilter_vsrc_frei0r_src = {
  385. .name = "frei0r_src",
  386. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  387. .priv_size = sizeof(Frei0rContext),
  388. .init = source_init,
  389. .uninit = uninit,
  390. .query_formats = query_formats,
  391. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  392. .outputs = (const AVFilterPad[]) {{ .name = "default",
  393. .type = AVMEDIA_TYPE_VIDEO,
  394. .request_frame = source_request_frame,
  395. .config_props = source_config_props },
  396. { .name = NULL}},
  397. };