vf_frei0r.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/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. AVRational framerate;
  66. /* only used by the source */
  67. int w, h;
  68. AVRational time_base;
  69. uint64_t pts;
  70. } Frei0rContext;
  71. static void *load_sym(AVFilterContext *ctx, const char *sym_name)
  72. {
  73. Frei0rContext *s = ctx->priv;
  74. void *sym = dlsym(s->dl_handle, sym_name);
  75. if (!sym)
  76. av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name);
  77. return sym;
  78. }
  79. static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
  80. {
  81. Frei0rContext *s = ctx->priv;
  82. union {
  83. double d;
  84. f0r_param_color_t col;
  85. f0r_param_position_t pos;
  86. } val;
  87. char *tail;
  88. uint8_t rgba[4];
  89. switch (info.type) {
  90. case F0R_PARAM_BOOL:
  91. if (!strcmp(param, "y")) val.d = 1.0;
  92. else if (!strcmp(param, "n")) val.d = 0.0;
  93. else goto fail;
  94. break;
  95. case F0R_PARAM_DOUBLE:
  96. val.d = strtod(param, &tail);
  97. if (*tail || val.d == HUGE_VAL)
  98. goto fail;
  99. break;
  100. case F0R_PARAM_COLOR:
  101. if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
  102. if (av_parse_color(rgba, param, -1, ctx) < 0)
  103. goto fail;
  104. val.col.r = rgba[0] / 255.0;
  105. val.col.g = rgba[1] / 255.0;
  106. val.col.b = rgba[2] / 255.0;
  107. }
  108. break;
  109. case F0R_PARAM_POSITION:
  110. if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
  111. goto fail;
  112. break;
  113. }
  114. s->set_param_value(s->instance, &val, index);
  115. return 0;
  116. fail:
  117. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
  118. param, info.name);
  119. return AVERROR(EINVAL);
  120. }
  121. static int set_params(AVFilterContext *ctx, const char *params)
  122. {
  123. Frei0rContext *s = ctx->priv;
  124. int i;
  125. if (!params)
  126. return 0;
  127. for (i = 0; i < s->plugin_info.num_params; i++) {
  128. f0r_param_info_t info;
  129. char *param;
  130. int ret;
  131. s->get_param_info(&info, i);
  132. if (*params) {
  133. if (!(param = av_get_token(&params, "|")))
  134. return AVERROR(ENOMEM);
  135. if (*params)
  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. s->get_param_value(s->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. s->get_param_value(s->instance, v, i);
  167. av_log(ctx, AV_LOG_DEBUG, "%f", d);
  168. break;
  169. case F0R_PARAM_COLOR:
  170. v = &col;
  171. s->get_param_value(s->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. s->get_param_value(s->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. s->get_param_value(s->instance, v, i);
  182. av_log(ctx, AV_LOG_DEBUG, "'%s'", 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 *s = 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. int i;
  210. static const char* const frei0r_pathlist[] = {
  211. "/usr/local/lib/frei0r-1/",
  212. "/usr/lib/frei0r-1/",
  213. "/usr/local/lib64/frei0r-1/",
  214. "/usr/lib64/frei0r-1/"
  215. };
  216. if (!dl_name) {
  217. av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
  218. return AVERROR(EINVAL);
  219. }
  220. /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
  221. if ((path = av_strdup(getenv("FREI0R_PATH")))) {
  222. #ifdef _WIN32
  223. const char *separator = ";";
  224. #else
  225. const char *separator = ":";
  226. #endif
  227. char *p, *ptr = NULL;
  228. for (p = path; p = av_strtok(p, separator, &ptr); p = NULL) {
  229. /* add additional trailing slash in case it is missing */
  230. char *p1 = av_asprintf("%s/", p);
  231. if (!p1) {
  232. ret = AVERROR(ENOMEM);
  233. goto check_path_end;
  234. }
  235. ret = load_path(ctx, &s->dl_handle, p1, dl_name);
  236. av_free(p1);
  237. if (ret < 0)
  238. goto check_path_end;
  239. if (s->dl_handle)
  240. break;
  241. }
  242. check_path_end:
  243. av_free(path);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. if (!s->dl_handle && (path = getenv("HOME"))) {
  248. char *prefix = av_asprintf("%s/.frei0r-1/lib/", path);
  249. if (!prefix)
  250. return AVERROR(ENOMEM);
  251. ret = load_path(ctx, &s->dl_handle, prefix, dl_name);
  252. av_free(prefix);
  253. if (ret < 0)
  254. return ret;
  255. }
  256. for (i = 0; !s->dl_handle && i < FF_ARRAY_ELEMS(frei0r_pathlist); i++) {
  257. ret = load_path(ctx, &s->dl_handle, frei0r_pathlist[i], dl_name);
  258. if (ret < 0)
  259. return ret;
  260. }
  261. if (!s->dl_handle) {
  262. av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
  263. return AVERROR(EINVAL);
  264. }
  265. if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
  266. !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
  267. !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
  268. !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
  269. !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
  270. !(s->update = load_sym(ctx, "f0r_update" )) ||
  271. !(s->construct = load_sym(ctx, "f0r_construct" )) ||
  272. !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
  273. !(s->deinit = load_sym(ctx, "f0r_deinit" )))
  274. return AVERROR(EINVAL);
  275. if (f0r_init() < 0) {
  276. av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
  277. return AVERROR(EINVAL);
  278. }
  279. f0r_get_plugin_info(&s->plugin_info);
  280. pi = &s->plugin_info;
  281. if (pi->plugin_type != type) {
  282. av_log(ctx, AV_LOG_ERROR,
  283. "Invalid type '%s' for this plugin\n",
  284. pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
  285. pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
  286. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
  287. pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
  288. return AVERROR(EINVAL);
  289. }
  290. av_log(ctx, AV_LOG_VERBOSE,
  291. "name:%s author:'%s' explanation:'%s' color_model:%s "
  292. "frei0r_version:%d version:%d.%d num_params:%d\n",
  293. pi->name, pi->author, pi->explanation,
  294. pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  295. pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  296. pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
  297. pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
  298. return 0;
  299. }
  300. static av_cold int filter_init(AVFilterContext *ctx)
  301. {
  302. Frei0rContext *s = ctx->priv;
  303. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
  304. }
  305. static av_cold void uninit(AVFilterContext *ctx)
  306. {
  307. Frei0rContext *s = ctx->priv;
  308. if (s->destruct && s->instance)
  309. s->destruct(s->instance);
  310. if (s->deinit)
  311. s->deinit();
  312. if (s->dl_handle)
  313. dlclose(s->dl_handle);
  314. }
  315. static int config_input_props(AVFilterLink *inlink)
  316. {
  317. AVFilterContext *ctx = inlink->dst;
  318. Frei0rContext *s = ctx->priv;
  319. if (s->destruct && s->instance)
  320. s->destruct(s->instance);
  321. if (!(s->instance = s->construct(inlink->w, inlink->h))) {
  322. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  323. return AVERROR(EINVAL);
  324. }
  325. return set_params(ctx, s->params);
  326. }
  327. static int query_formats(AVFilterContext *ctx)
  328. {
  329. Frei0rContext *s = ctx->priv;
  330. AVFilterFormats *formats = NULL;
  331. if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
  332. ff_add_format(&formats, AV_PIX_FMT_BGRA);
  333. } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
  334. ff_add_format(&formats, AV_PIX_FMT_RGBA);
  335. } else { /* F0R_COLOR_MODEL_PACKED32 */
  336. static const enum AVPixelFormat pix_fmts[] = {
  337. AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
  338. };
  339. formats = ff_make_format_list(pix_fmts);
  340. }
  341. if (!formats)
  342. return AVERROR(ENOMEM);
  343. ff_set_common_formats(ctx, formats);
  344. return 0;
  345. }
  346. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  347. {
  348. Frei0rContext *s = inlink->dst->priv;
  349. AVFilterLink *outlink = inlink->dst->outputs[0];
  350. AVFrame *out;
  351. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  352. if (!out) {
  353. av_frame_free(&in);
  354. return AVERROR(ENOMEM);
  355. }
  356. av_frame_copy_props(out, in);
  357. s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
  358. (const uint32_t *)in->data[0],
  359. (uint32_t *)out->data[0]);
  360. av_frame_free(&in);
  361. return ff_filter_frame(outlink, out);
  362. }
  363. #define OFFSET(x) offsetof(Frei0rContext, x)
  364. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  365. static const AVOption frei0r_options[] = {
  366. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  367. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  368. { NULL }
  369. };
  370. AVFILTER_DEFINE_CLASS(frei0r);
  371. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  372. {
  373. .name = "default",
  374. .type = AVMEDIA_TYPE_VIDEO,
  375. .config_props = config_input_props,
  376. .filter_frame = filter_frame,
  377. },
  378. { NULL }
  379. };
  380. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  381. {
  382. .name = "default",
  383. .type = AVMEDIA_TYPE_VIDEO,
  384. },
  385. { NULL }
  386. };
  387. AVFilter ff_vf_frei0r = {
  388. .name = "frei0r",
  389. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  390. .query_formats = query_formats,
  391. .init = filter_init,
  392. .uninit = uninit,
  393. .priv_size = sizeof(Frei0rContext),
  394. .priv_class = &frei0r_class,
  395. .inputs = avfilter_vf_frei0r_inputs,
  396. .outputs = avfilter_vf_frei0r_outputs,
  397. };
  398. static av_cold int source_init(AVFilterContext *ctx)
  399. {
  400. Frei0rContext *s = ctx->priv;
  401. s->time_base.num = s->framerate.den;
  402. s->time_base.den = s->framerate.num;
  403. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  404. }
  405. static int source_config_props(AVFilterLink *outlink)
  406. {
  407. AVFilterContext *ctx = outlink->src;
  408. Frei0rContext *s = ctx->priv;
  409. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  410. return AVERROR(EINVAL);
  411. outlink->w = s->w;
  412. outlink->h = s->h;
  413. outlink->time_base = s->time_base;
  414. outlink->sample_aspect_ratio = (AVRational){1,1};
  415. if (s->destruct && s->instance)
  416. s->destruct(s->instance);
  417. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  418. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  419. return AVERROR(EINVAL);
  420. }
  421. if (!s->params) {
  422. av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
  423. return AVERROR(EINVAL);
  424. }
  425. return set_params(ctx, s->params);
  426. }
  427. static int source_request_frame(AVFilterLink *outlink)
  428. {
  429. Frei0rContext *s = outlink->src->priv;
  430. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  431. if (!frame)
  432. return AVERROR(ENOMEM);
  433. frame->sample_aspect_ratio = (AVRational) {1, 1};
  434. frame->pts = s->pts++;
  435. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  436. NULL, (uint32_t *)frame->data[0]);
  437. return ff_filter_frame(outlink, frame);
  438. }
  439. static const AVOption frei0r_src_options[] = {
  440. { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
  441. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = FLAGS },
  442. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  443. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  444. { NULL },
  445. };
  446. AVFILTER_DEFINE_CLASS(frei0r_src);
  447. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  448. {
  449. .name = "default",
  450. .type = AVMEDIA_TYPE_VIDEO,
  451. .request_frame = source_request_frame,
  452. .config_props = source_config_props
  453. },
  454. { NULL }
  455. };
  456. AVFilter ff_vsrc_frei0r_src = {
  457. .name = "frei0r_src",
  458. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  459. .priv_size = sizeof(Frei0rContext),
  460. .priv_class = &frei0r_src_class,
  461. .init = source_init,
  462. .uninit = uninit,
  463. .query_formats = query_formats,
  464. .inputs = NULL,
  465. .outputs = avfilter_vsrc_frei0r_src_outputs,
  466. };