vf_frei0r.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. } 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 = av_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. s->set_param_value(s->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 *s = ctx->priv;
  125. int i;
  126. if (!params)
  127. return 0;
  128. for (i = 0; i < s->plugin_info.num_params; i++) {
  129. f0r_param_info_t info;
  130. char *param;
  131. int ret;
  132. s->get_param_info(&info, i);
  133. if (*params) {
  134. if (!(param = av_get_token(&params, "|")))
  135. return AVERROR(ENOMEM);
  136. if (*params)
  137. params++; /* skip ':' */
  138. ret = set_param(ctx, info, i, param);
  139. av_free(param);
  140. if (ret < 0)
  141. return ret;
  142. }
  143. }
  144. return 0;
  145. }
  146. static int load_path(AVFilterContext *ctx, void **handle_ptr, const char *prefix, const char *name)
  147. {
  148. char *path = av_asprintf("%s%s%s", prefix, name, SLIBSUF);
  149. if (!path)
  150. return AVERROR(ENOMEM);
  151. av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
  152. *handle_ptr = dlopen(path, RTLD_NOW|RTLD_LOCAL);
  153. av_free(path);
  154. return 0;
  155. }
  156. static av_cold int frei0r_init(AVFilterContext *ctx,
  157. const char *dl_name, int type)
  158. {
  159. Frei0rContext *s = ctx->priv;
  160. f0r_init_f f0r_init;
  161. f0r_get_plugin_info_f f0r_get_plugin_info;
  162. f0r_plugin_info_t *pi;
  163. char *path;
  164. int ret = 0;
  165. int i;
  166. static const char* const frei0r_pathlist[] = {
  167. "/usr/local/lib/frei0r-1/",
  168. "/usr/lib/frei0r-1/",
  169. "/usr/local/lib64/frei0r-1/",
  170. "/usr/lib64/frei0r-1/"
  171. };
  172. if (!dl_name) {
  173. av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
  174. return AVERROR(EINVAL);
  175. }
  176. /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
  177. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  178. #ifdef _WIN32
  179. const char *separator = ";";
  180. #else
  181. const char *separator = ":";
  182. #endif
  183. char *p, *ptr = NULL;
  184. for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
  185. /* add additional trailing slash in case it is missing */
  186. char *p1 = av_asprintf("%s/", p);
  187. if (!p1) {
  188. ret = AVERROR(ENOMEM);
  189. goto check_path_end;
  190. }
  191. ret = load_path(ctx, &s->dl_handle, p1, dl_name);
  192. av_free(p1);
  193. if (ret < 0)
  194. goto check_path_end;
  195. if (s->dl_handle)
  196. break;
  197. }
  198. check_path_end:
  199. av_free(path);
  200. if (ret < 0)
  201. return ret;
  202. }
  203. if (!s->dl_handle && (path = getenv("HOME"))) {
  204. char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
  205. if (!prefix)
  206. return AVERROR(ENOMEM);
  207. ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
  208. av_free(prefix);
  209. if (ret < 0)
  210. return ret;
  211. }
  212. for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
  213. ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name);
  214. if (ret < 0)
  215. return ret;
  216. }
  217. if (!s->dl_handle) {
  218. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
  219. return AVERROR(EINVAL);
  220. }
  221. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  222. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  223. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  224. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  225. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  226. !(s->update = load_sym(ctx, "f0r_update" )) ||
  227. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  228. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  229. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  230. return AVERROR(EINVAL);
  231. if (f0r_init() < 0) {
  232. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
  233. return AVERROR(EINVAL);
  234. }
  235. f0r_get_plugin_info(&s->plugin_info);
  236. pi = &s->plugin_info;
  237. if (pi->plugin_type != type) {
  238. av_log(ctx, AV_LOG_ERROR,
  239. "Invalid type '%s' for this plugin\n",
  240. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  241. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  242. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  243. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  244. return AVERROR(EINVAL);
  245. }
  246. av_log(ctx, AV_LOG_VERBOSE,
  247. "name:%s author:'%s' explanation:'%s' color_model:%s "
  248. "frei0r_version:%d version:%d.%d num_params:%d\n",
  249. pi->name, pi->author, pi->explanation,
  250. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  251. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  252. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  253. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  254. return 0;
  255. }
  256. static av_cold int filter_init(AVFilterContext *ctx)
  257. {
  258. Frei0rContext *s = ctx->priv;
  259. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  260. }
  261. static av_cold void uninit(AVFilterContext *ctx)
  262. {
  263. Frei0rContext *s = ctx->priv;
  264. if (s->destruct && s->instance)
  265. s->destruct(s->instance);
  266. if (s->deinit)
  267. s->deinit();
  268. if (s->dl_handle)
  269. dlclose(s->dl_handle);
  270. }
  271. static int config_input_props(AVFilterLink *inlink)
  272. {
  273. AVFilterContext *ctx = inlink->dst;
  274. Frei0rContext *s = ctx->priv;
  275. if (s->destruct && s->instance)
  276. s->destruct(s->instance);
  277. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  278. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  279. return AVERROR(EINVAL);
  280. }
  281. return set_params(ctx, s->params);
  282. }
  283. static int query_formats(AVFilterContext *ctx)
  284. {
  285. Frei0rContext *s = ctx->priv;
  286. AVFilterFormats *formats = NULL;
  287. int ret;
  288. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  289. if ((ret = ff_add_format(&formats, AV_PIX_FMT_BGRA)) < 0)
  290. return ret;
  291. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  292. if ((ret = ff_add_format(&formats, AV_PIX_FMT_RGBA)) < 0)
  293. return ret;
  294. } else { /* F0R_COLOR_MODEL_PACKED32 */
  295. static const enum AVPixelFormat pix_fmts[] = {
  296. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  297. };
  298. formats = ff_make_format_list(pix_fmts);
  299. }
  300. if (!formats)
  301. return AVERROR(ENOMEM);
  302. return ff_set_common_formats(ctx, formats);
  303. }
  304. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  305. {
  306. Frei0rContext *s = inlink->dst->priv;
  307. AVFilterLink *outlink = inlink->dst->outputs[0];
  308. AVFrame *out;
  309. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  310. if (!out) {
  311. av_frame_free(&in);
  312. return AVERROR(ENOMEM);
  313. }
  314. av_frame_copy_props(out, in);
  315. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  316. (const uint32_t *)in->data[0],
  317. (uint32_t *)out->data[0]);
  318. av_frame_free(&in);
  319. return ff_filter_frame(outlink, out);
  320. }
  321. #define OFFSET(x) offsetof(Frei0rContext, x)
  322. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  323. static const AVOption frei0r_options[] = {
  324. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  325. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  326. { NULL }
  327. };
  328. AVFILTER_DEFINE_CLASS(frei0r);
  329. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  330. {
  331. .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .config_props = config_input_props,
  334. .filter_frame = filter_frame,
  335. },
  336. { NULL }
  337. };
  338. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  339. {
  340. .name = "default",
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. },
  343. { NULL }
  344. };
  345. AVFilter ff_vf_frei0r = {
  346. .name = "frei0r",
  347. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  348. .query_formats = query_formats,
  349. .init = filter_init,
  350. .uninit = uninit,
  351. .priv_size = sizeof(Frei0rContext),
  352. .priv_class = &frei0r_class,
  353. .inputs = avfilter_vf_frei0r_inputs,
  354. .outputs = avfilter_vf_frei0r_outputs,
  355. };
  356. static av_cold int source_init(AVFilterContext *ctx)
  357. {
  358. Frei0rContext *s = ctx->priv;
  359. s->time_base.num = s->framerate.den;
  360. s->time_base.den = s->framerate.num;
  361. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  362. }
  363. static int source_config_props(AVFilterLink *outlink)
  364. {
  365. AVFilterContext *ctx = outlink->src;
  366. Frei0rContext *s = ctx->priv;
  367. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  368. return AVERROR(EINVAL);
  369. outlink->w = s->w;
  370. outlink->h = s->h;
  371. outlink->time_base = s->time_base;
  372. outlink->frame_rate = av_inv_q(s->time_base);
  373. outlink->sample_aspect_ratio = (AVRational){1,1};
  374. if (s->destruct && s->instance)
  375. s->destruct(s->instance);
  376. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  377. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  378. return AVERROR(EINVAL);
  379. }
  380. if (!s->params) {
  381. av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
  382. return AVERROR(EINVAL);
  383. }
  384. return set_params(ctx, s->params);
  385. }
  386. static int source_request_frame(AVFilterLink *outlink)
  387. {
  388. Frei0rContext *s = outlink->src->priv;
  389. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  390. if (!frame)
  391. return AVERROR(ENOMEM);
  392. frame->sample_aspect_ratio = (AVRational) {1, 1};
  393. frame->pts = s->pts++;
  394. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  395. NULL, (uint32_t *)frame->data[0]);
  396. return ff_filter_frame(outlink, frame);
  397. }
  398. static const AVOption frei0r_src_options[] = {
  399. { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
  400. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, .flags = FLAGS },
  401. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  402. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  403. { NULL },
  404. };
  405. AVFILTER_DEFINE_CLASS(frei0r_src);
  406. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  407. {
  408. .name = "default",
  409. .type = AVMEDIA_TYPE_VIDEO,
  410. .request_frame = source_request_frame,
  411. .config_props = source_config_props
  412. },
  413. { NULL }
  414. };
  415. AVFilter ff_vsrc_frei0r_src = {
  416. .name = "frei0r_src",
  417. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  418. .priv_size = sizeof(Frei0rContext),
  419. .priv_class = &frei0r_src_class,
  420. .init = source_init,
  421. .uninit = uninit,
  422. .query_formats = query_formats,
  423. .inputs = NULL,
  424. .outputs = avfilter_vsrc_frei0r_src_outputs,
  425. };