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