vf_frei0r.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/common.h"
  31. #include "libavutil/eval.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/mem.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/parseutils.h"
  38. #include "avfilter.h"
  39. #include "formats.h"
  40. #include "internal.h"
  41. #include "video.h"
  42. typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
  43. typedef void (*f0r_destruct_f)(f0r_instance_t instance);
  44. typedef void (*f0r_deinit_f)(void);
  45. typedef int (*f0r_init_f)(void);
  46. typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
  47. typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
  48. typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
  49. 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);
  50. typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  51. typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
  52. typedef struct Frei0rContext {
  53. const AVClass *class;
  54. f0r_update_f update;
  55. void *dl_handle; /* dynamic library handle */
  56. f0r_instance_t instance;
  57. f0r_plugin_info_t plugin_info;
  58. f0r_get_param_info_f get_param_info;
  59. f0r_get_param_value_f get_param_value;
  60. f0r_set_param_value_f set_param_value;
  61. f0r_construct_f construct;
  62. f0r_destruct_f destruct;
  63. f0r_deinit_f deinit;
  64. char *dl_name;
  65. char *params;
  66. AVRational 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 *s = ctx->priv;
  75. void *sym = dlsym(s->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 *s = ctx->priv;
  83. union {
  84. double d;
  85. f0r_param_color_t col;
  86. f0r_param_position_t pos;
  87. f0r_param_string str;
  88. } val;
  89. char *tail;
  90. uint8_t rgba[4];
  91. switch (info.type) {
  92. case F0R_PARAM_BOOL:
  93. if (!strcmp(param, "y")) val.d = 1.0;
  94. else if (!strcmp(param, "n")) val.d = 0.0;
  95. else goto fail;
  96. break;
  97. case F0R_PARAM_DOUBLE:
  98. val.d = av_strtod(param, &tail);
  99. if (*tail || val.d == HUGE_VAL)
  100. goto fail;
  101. break;
  102. case F0R_PARAM_COLOR:
  103. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  104. if (av_parse_color(rgba, param, -1, ctx) < 0)
  105. goto fail;
  106. val.col.r = rgba[0] / 255.0;
  107. val.col.g = rgba[1] / 255.0;
  108. val.col.b = rgba[2] / 255.0;
  109. }
  110. break;
  111. case F0R_PARAM_POSITION:
  112. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  113. goto fail;
  114. break;
  115. case F0R_PARAM_STRING:
  116. val.str = param;
  117. break;
  118. }
  119. s->set_param_value(s->instance, &val, index);
  120. return 0;
  121. fail:
  122. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
  123. param, info.name);
  124. return AVERROR(EINVAL);
  125. }
  126. static int set_params(AVFilterContext *ctx, const char *params)
  127. {
  128. Frei0rContext *s = ctx->priv;
  129. int i;
  130. if (!params)
  131. return 0;
  132. for (i = 0; i < s->plugin_info.num_params; i++) {
  133. f0r_param_info_t info;
  134. char *param;
  135. int ret;
  136. s->get_param_info(&info, i);
  137. if (*params) {
  138. if (!(param = av_get_token(&params, "|")))
  139. return AVERROR(ENOMEM);
  140. if (*params)
  141. params++; /* skip ':' */
  142. ret = set_param(ctx, info, i, param);
  143. av_free(param);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
  151. {
  152. char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
  153. if (!path)
  154. return AVERROR(ENOMEM);
  155. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
  156. *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
  157. av_free(path);
  158. return 0;
  159. }
  160. static av_cold int frei0r_init(AVFilterContext *ctx,
  161. const char *dl_name, int type)
  162. {
  163. Frei0rContext *s = ctx->priv;
  164. f0r_init_f f0r_init;
  165. f0r_get_plugin_info_f f0r_get_plugin_info;
  166. f0r_plugin_info_t *pi;
  167. char *path;
  168. int ret = 0;
  169. int i;
  170. static const char* const frei0r_pathlist[] = {
  171. "/usr/local/lib/frei0r-1/",
  172. "/usr/lib/frei0r-1/",
  173. "/usr/local/lib64/frei0r-1/",
  174. "/usr/lib64/frei0r-1/"
  175. };
  176. if (!dl_name) {
  177. av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
  178. return AVERROR(EINVAL);
  179. }
  180. /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
  181. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  182. #ifdef _WIN32
  183. const char *separator = ";";
  184. #else
  185. const char *separator = ":";
  186. #endif
  187. char *p, *ptr = NULL;
  188. for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
  189. /* add additional trailing slash in case it is missing */
  190. char *p1 = av_asprintf("%s/", p);
  191. if (!p1) {
  192. ret = AVERROR(ENOMEM);
  193. goto check_path_end;
  194. }
  195. ret = load_path(ctx, &s->dl_handle, p1, dl_name);
  196. av_free(p1);
  197. if (ret < 0)
  198. goto check_path_end;
  199. if (s->dl_handle)
  200. break;
  201. }
  202. check_path_end:
  203. av_free(path);
  204. if (ret < 0)
  205. return ret;
  206. }
  207. if (!s->dl_handle && (path = getenv("HOME"))) {
  208. char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
  209. if (!prefix)
  210. return AVERROR(ENOMEM);
  211. ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
  212. av_free(prefix);
  213. if (ret < 0)
  214. return ret;
  215. }
  216. for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
  217. ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name);
  218. if (ret < 0)
  219. return ret;
  220. }
  221. if (!s->dl_handle) {
  222. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
  223. return AVERROR(EINVAL);
  224. }
  225. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  226. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  227. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  228. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  229. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  230. !(s->update = load_sym(ctx, "f0r_update" )) ||
  231. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  232. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  233. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  234. return AVERROR(EINVAL);
  235. if (f0r_init() < 0) {
  236. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
  237. return AVERROR(EINVAL);
  238. }
  239. f0r_get_plugin_info(&s->plugin_info);
  240. pi = &s->plugin_info;
  241. if (pi->plugin_type != type) {
  242. av_log(ctx, AV_LOG_ERROR,
  243. "Invalid type '%s' for this plugin\n",
  244. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  245. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  246. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  247. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  248. return AVERROR(EINVAL);
  249. }
  250. av_log(ctx, AV_LOG_VERBOSE,
  251. "name:%s author:'%s' explanation:'%s' color_model:%s "
  252. "frei0r_version:%d version:%d.%d num_params:%d\n",
  253. pi->name, pi->author, pi->explanation,
  254. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  255. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  256. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  257. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  258. return 0;
  259. }
  260. static av_cold int filter_init(AVFilterContext *ctx)
  261. {
  262. Frei0rContext *s = ctx->priv;
  263. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  264. }
  265. static av_cold void uninit(AVFilterContext *ctx)
  266. {
  267. Frei0rContext *s = ctx->priv;
  268. if (s->destruct && s->instance)
  269. s->destruct(s->instance);
  270. if (s->deinit)
  271. s->deinit();
  272. if (s->dl_handle)
  273. dlclose(s->dl_handle);
  274. }
  275. static int config_input_props(AVFilterLink *inlink)
  276. {
  277. AVFilterContext *ctx = inlink->dst;
  278. Frei0rContext *s = ctx->priv;
  279. if (s->destruct && s->instance)
  280. s->destruct(s->instance);
  281. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  282. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  283. return AVERROR(EINVAL);
  284. }
  285. return set_params(ctx, s->params);
  286. }
  287. static int query_formats(AVFilterContext *ctx)
  288. {
  289. Frei0rContext *s = ctx->priv;
  290. AVFilterFormats *formats = NULL;
  291. int ret;
  292. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  293. if ((ret = ff_add_format(&formats, AV_PIX_FMT_BGRA)) < 0)
  294. return ret;
  295. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  296. if ((ret = ff_add_format(&formats, AV_PIX_FMT_RGBA)) < 0)
  297. return ret;
  298. } else { /* F0R_COLOR_MODEL_PACKED32 */
  299. static const enum AVPixelFormat pix_fmts[] = {
  300. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  301. };
  302. formats = ff_make_format_list(pix_fmts);
  303. }
  304. if (!formats)
  305. return AVERROR(ENOMEM);
  306. return ff_set_common_formats(ctx, formats);
  307. }
  308. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  309. {
  310. Frei0rContext *s = inlink->dst->priv;
  311. AVFilterLink *outlink = inlink->dst->outputs[0];
  312. AVFrame *out;
  313. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  314. if (!out) {
  315. av_frame_free(&in);
  316. return AVERROR(ENOMEM);
  317. }
  318. av_frame_copy_props(out, in);
  319. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  320. (const uint32_t *)in->data[0],
  321. (uint32_t *)out->data[0]);
  322. av_frame_free(&in);
  323. return ff_filter_frame(outlink, out);
  324. }
  325. #define OFFSET(x) offsetof(Frei0rContext, x)
  326. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  327. static const AVOption frei0r_options[] = {
  328. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  329. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  330. { NULL }
  331. };
  332. AVFILTER_DEFINE_CLASS(frei0r);
  333. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  334. {
  335. .name = "default",
  336. .type = AVMEDIA_TYPE_VIDEO,
  337. .config_props = config_input_props,
  338. .filter_frame = filter_frame,
  339. },
  340. { NULL }
  341. };
  342. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  343. {
  344. .name = "default",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. },
  347. { NULL }
  348. };
  349. AVFilter ff_vf_frei0r = {
  350. .name = "frei0r",
  351. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  352. .query_formats = query_formats,
  353. .init = filter_init,
  354. .uninit = uninit,
  355. .priv_size = sizeof(Frei0rContext),
  356. .priv_class = &frei0r_class,
  357. .inputs = avfilter_vf_frei0r_inputs,
  358. .outputs = avfilter_vf_frei0r_outputs,
  359. };
  360. static av_cold int source_init(AVFilterContext *ctx)
  361. {
  362. Frei0rContext *s = ctx->priv;
  363. s->time_base.num = s->framerate.den;
  364. s->time_base.den = s->framerate.num;
  365. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  366. }
  367. static int source_config_props(AVFilterLink *outlink)
  368. {
  369. AVFilterContext *ctx = outlink->src;
  370. Frei0rContext *s = ctx->priv;
  371. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  372. return AVERROR(EINVAL);
  373. outlink->w = s->w;
  374. outlink->h = s->h;
  375. outlink->time_base = s->time_base;
  376. outlink->frame_rate = av_inv_q(s->time_base);
  377. outlink->sample_aspect_ratio = (AVRational){1,1};
  378. if (s->destruct && s->instance)
  379. s->destruct(s->instance);
  380. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  381. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  382. return AVERROR(EINVAL);
  383. }
  384. if (!s->params) {
  385. av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
  386. return AVERROR(EINVAL);
  387. }
  388. return set_params(ctx, s->params);
  389. }
  390. static int source_request_frame(AVFilterLink *outlink)
  391. {
  392. Frei0rContext *s = outlink->src->priv;
  393. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  394. if (!frame)
  395. return AVERROR(ENOMEM);
  396. frame->sample_aspect_ratio = (AVRational) {1, 1};
  397. frame->pts = s->pts++;
  398. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  399. NULL, (uint32_t *)frame->data[0]);
  400. return ff_filter_frame(outlink, frame);
  401. }
  402. static const AVOption frei0r_src_options[] = {
  403. { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
  404. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, .flags = FLAGS },
  405. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  406. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  407. { NULL },
  408. };
  409. AVFILTER_DEFINE_CLASS(frei0r_src);
  410. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  411. {
  412. .name = "default",
  413. .type = AVMEDIA_TYPE_VIDEO,
  414. .request_frame = source_request_frame,
  415. .config_props = source_config_props
  416. },
  417. { NULL }
  418. };
  419. AVFilter ff_vsrc_frei0r_src = {
  420. .name = "frei0r_src",
  421. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  422. .priv_size = sizeof(Frei0rContext),
  423. .priv_class = &frei0r_src_class,
  424. .init = source_init,
  425. .uninit = uninit,
  426. .query_formats = query_formats,
  427. .inputs = NULL,
  428. .outputs = avfilter_vsrc_frei0r_src_outputs,
  429. };