vf_frei0r.c 17 KB

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