You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
16KB

  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_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. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  326. char *res, int res_len, int flags)
  327. {
  328. Frei0rContext *s = ctx->priv;
  329. int ret;
  330. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  331. if (ret < 0)
  332. return ret;
  333. return set_params(ctx, s->params);
  334. }
  335. #define OFFSET(x) offsetof(Frei0rContext, x)
  336. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  337. #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
  338. static const AVOption frei0r_options[] = {
  339. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  340. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = TFLAGS },
  341. { NULL }
  342. };
  343. AVFILTER_DEFINE_CLASS(frei0r);
  344. static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .config_props = config_input_props,
  349. .filter_frame = filter_frame,
  350. },
  351. { NULL }
  352. };
  353. static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vf_frei0r = {
  361. .name = "frei0r",
  362. .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
  363. .query_formats = query_formats,
  364. .init = filter_init,
  365. .uninit = uninit,
  366. .priv_size = sizeof(Frei0rContext),
  367. .priv_class = &frei0r_class,
  368. .inputs = avfilter_vf_frei0r_inputs,
  369. .outputs = avfilter_vf_frei0r_outputs,
  370. .process_command = process_command,
  371. };
  372. static av_cold int source_init(AVFilterContext *ctx)
  373. {
  374. Frei0rContext *s = ctx->priv;
  375. s->time_base.num = s->framerate.den;
  376. s->time_base.den = s->framerate.num;
  377. return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
  378. }
  379. static int source_config_props(AVFilterLink *outlink)
  380. {
  381. AVFilterContext *ctx = outlink->src;
  382. Frei0rContext *s = ctx->priv;
  383. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  384. return AVERROR(EINVAL);
  385. outlink->w = s->w;
  386. outlink->h = s->h;
  387. outlink->time_base = s->time_base;
  388. outlink->frame_rate = av_inv_q(s->time_base);
  389. outlink->sample_aspect_ratio = (AVRational){1,1};
  390. if (s->destruct && s->instance)
  391. s->destruct(s->instance);
  392. if (!(s->instance = s->construct(outlink->w, outlink->h))) {
  393. av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
  394. return AVERROR(EINVAL);
  395. }
  396. if (!s->params) {
  397. av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
  398. return AVERROR(EINVAL);
  399. }
  400. return set_params(ctx, s->params);
  401. }
  402. static int source_request_frame(AVFilterLink *outlink)
  403. {
  404. Frei0rContext *s = outlink->src->priv;
  405. AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  406. if (!frame)
  407. return AVERROR(ENOMEM);
  408. frame->sample_aspect_ratio = (AVRational) {1, 1};
  409. frame->pts = s->pts++;
  410. s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
  411. NULL, (uint32_t *)frame->data[0]);
  412. return ff_filter_frame(outlink, frame);
  413. }
  414. static const AVOption frei0r_src_options[] = {
  415. { "size", "Dimensions of the generated video.", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str = "320x240" }, .flags = FLAGS },
  416. { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, .flags = FLAGS },
  417. { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
  418. { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
  419. { NULL },
  420. };
  421. AVFILTER_DEFINE_CLASS(frei0r_src);
  422. static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
  423. {
  424. .name = "default",
  425. .type = AVMEDIA_TYPE_VIDEO,
  426. .request_frame = source_request_frame,
  427. .config_props = source_config_props
  428. },
  429. { NULL }
  430. };
  431. AVFilter ff_vsrc_frei0r_src = {
  432. .name = "frei0r_src",
  433. .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
  434. .priv_size = sizeof(Frei0rContext),
  435. .priv_class = &frei0r_src_class,
  436. .init = source_init,
  437. .uninit = uninit,
  438. .query_formats = query_formats,
  439. .inputs = NULL,
  440. .outputs = avfilter_vsrc_frei0r_src_outputs,
  441. };