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.

413 lines
14KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Compute a look-up table for binding the input value to the output
  23. * value, and apply it to input video.
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. static const char *const var_names[] = {
  36. "E",
  37. "PHI",
  38. "PI",
  39. "w", ///< width of the input video
  40. "h", ///< height of the input video
  41. "val", ///< input value for the pixel
  42. "maxval", ///< max value for the pixel
  43. "minval", ///< min value for the pixel
  44. "negval", ///< negated value
  45. "clipval",
  46. NULL
  47. };
  48. enum var_name {
  49. VAR_E,
  50. VAR_PHI,
  51. VAR_PI,
  52. VAR_W,
  53. VAR_H,
  54. VAR_VAL,
  55. VAR_MAXVAL,
  56. VAR_MINVAL,
  57. VAR_NEGVAL,
  58. VAR_CLIPVAL,
  59. VAR_VARS_NB
  60. };
  61. typedef struct {
  62. const AVClass *class;
  63. uint8_t lut[4][256]; ///< lookup table for each component
  64. char *comp_expr_str[4];
  65. AVExpr *comp_expr[4];
  66. int hsub, vsub;
  67. double var_values[VAR_VARS_NB];
  68. int is_rgb, is_yuv;
  69. int rgba_map[4];
  70. int step;
  71. int negate_alpha; /* only used by negate */
  72. } LutContext;
  73. #define Y 0
  74. #define U 1
  75. #define V 2
  76. #define R 0
  77. #define G 1
  78. #define B 2
  79. #define A 3
  80. #define OFFSET(x) offsetof(LutContext, x)
  81. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  82. static const AVOption lut_options[] = {
  83. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  84. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  85. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  86. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  87. { "y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  88. { "u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  89. { "v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  90. { "r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  91. { "g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  92. { "b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  93. { "a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  94. { NULL },
  95. };
  96. static av_cold int init(AVFilterContext *ctx)
  97. {
  98. LutContext *s = ctx->priv;
  99. s->var_values[VAR_PHI] = M_PHI;
  100. s->var_values[VAR_PI] = M_PI;
  101. s->var_values[VAR_E ] = M_E;
  102. s->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
  103. s->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
  104. return 0;
  105. }
  106. static av_cold void uninit(AVFilterContext *ctx)
  107. {
  108. LutContext *s = ctx->priv;
  109. int i;
  110. for (i = 0; i < 4; i++) {
  111. av_expr_free(s->comp_expr[i]);
  112. s->comp_expr[i] = NULL;
  113. av_freep(&s->comp_expr_str[i]);
  114. }
  115. }
  116. #define YUV_FORMATS \
  117. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
  118. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
  119. AV_PIX_FMT_YUVA420P, \
  120. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
  121. AV_PIX_FMT_YUVJ440P
  122. #define RGB_FORMATS \
  123. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
  124. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
  125. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
  126. static enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
  127. static enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
  128. static enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
  129. static int query_formats(AVFilterContext *ctx)
  130. {
  131. LutContext *s = ctx->priv;
  132. enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
  133. s->is_yuv ? yuv_pix_fmts : all_pix_fmts;
  134. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  135. return 0;
  136. }
  137. /**
  138. * Clip value val in the minval - maxval range.
  139. */
  140. static double clip(void *opaque, double val)
  141. {
  142. LutContext *s = opaque;
  143. double minval = s->var_values[VAR_MINVAL];
  144. double maxval = s->var_values[VAR_MAXVAL];
  145. return av_clip(val, minval, maxval);
  146. }
  147. /**
  148. * Compute gamma correction for value val, assuming the minval-maxval
  149. * range, val is clipped to a value contained in the same interval.
  150. */
  151. static double compute_gammaval(void *opaque, double gamma)
  152. {
  153. LutContext *s = opaque;
  154. double val = s->var_values[VAR_CLIPVAL];
  155. double minval = s->var_values[VAR_MINVAL];
  156. double maxval = s->var_values[VAR_MAXVAL];
  157. return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
  158. }
  159. static double (* const funcs1[])(void *, double) = {
  160. clip,
  161. compute_gammaval,
  162. NULL
  163. };
  164. static const char * const funcs1_names[] = {
  165. "clip",
  166. "gammaval",
  167. NULL
  168. };
  169. static int config_props(AVFilterLink *inlink)
  170. {
  171. AVFilterContext *ctx = inlink->dst;
  172. LutContext *s = ctx->priv;
  173. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  174. int min[4], max[4];
  175. int val, comp, ret;
  176. s->hsub = desc->log2_chroma_w;
  177. s->vsub = desc->log2_chroma_h;
  178. s->var_values[VAR_W] = inlink->w;
  179. s->var_values[VAR_H] = inlink->h;
  180. switch (inlink->format) {
  181. case AV_PIX_FMT_YUV410P:
  182. case AV_PIX_FMT_YUV411P:
  183. case AV_PIX_FMT_YUV420P:
  184. case AV_PIX_FMT_YUV422P:
  185. case AV_PIX_FMT_YUV440P:
  186. case AV_PIX_FMT_YUV444P:
  187. case AV_PIX_FMT_YUVA420P:
  188. min[Y] = min[U] = min[V] = 16;
  189. max[Y] = 235;
  190. max[U] = max[V] = 240;
  191. min[A] = 0; max[A] = 255;
  192. break;
  193. default:
  194. min[0] = min[1] = min[2] = min[3] = 0;
  195. max[0] = max[1] = max[2] = max[3] = 255;
  196. }
  197. s->is_yuv = s->is_rgb = 0;
  198. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
  199. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
  200. if (s->is_rgb) {
  201. switch (inlink->format) {
  202. case AV_PIX_FMT_ARGB: s->rgba_map[A] = 0; s->rgba_map[R] = 1; s->rgba_map[G] = 2; s->rgba_map[B] = 3; break;
  203. case AV_PIX_FMT_ABGR: s->rgba_map[A] = 0; s->rgba_map[B] = 1; s->rgba_map[G] = 2; s->rgba_map[R] = 3; break;
  204. case AV_PIX_FMT_RGBA:
  205. case AV_PIX_FMT_RGB24: s->rgba_map[R] = 0; s->rgba_map[G] = 1; s->rgba_map[B] = 2; s->rgba_map[A] = 3; break;
  206. case AV_PIX_FMT_BGRA:
  207. case AV_PIX_FMT_BGR24: s->rgba_map[B] = 0; s->rgba_map[G] = 1; s->rgba_map[R] = 2; s->rgba_map[A] = 3; break;
  208. }
  209. s->step = av_get_bits_per_pixel(desc) >> 3;
  210. }
  211. for (comp = 0; comp < desc->nb_components; comp++) {
  212. double res;
  213. /* create the parsed expression */
  214. av_expr_free(s->comp_expr[comp]);
  215. s->comp_expr[comp] = NULL;
  216. ret = av_expr_parse(&s->comp_expr[comp], s->comp_expr_str[comp],
  217. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  218. if (ret < 0) {
  219. av_log(ctx, AV_LOG_ERROR,
  220. "Error when parsing the expression '%s' for the component %d.\n",
  221. s->comp_expr_str[comp], comp);
  222. return AVERROR(EINVAL);
  223. }
  224. /* compute the s */
  225. s->var_values[VAR_MAXVAL] = max[comp];
  226. s->var_values[VAR_MINVAL] = min[comp];
  227. for (val = 0; val < 256; val++) {
  228. s->var_values[VAR_VAL] = val;
  229. s->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
  230. s->var_values[VAR_NEGVAL] =
  231. av_clip(min[comp] + max[comp] - s->var_values[VAR_VAL],
  232. min[comp], max[comp]);
  233. res = av_expr_eval(s->comp_expr[comp], s->var_values, s);
  234. if (isnan(res)) {
  235. av_log(ctx, AV_LOG_ERROR,
  236. "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
  237. s->comp_expr_str[comp], val, comp);
  238. return AVERROR(EINVAL);
  239. }
  240. s->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
  241. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
  242. }
  243. }
  244. return 0;
  245. }
  246. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  247. {
  248. AVFilterContext *ctx = inlink->dst;
  249. LutContext *s = ctx->priv;
  250. AVFilterLink *outlink = ctx->outputs[0];
  251. AVFrame *out;
  252. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  253. int i, j, k, plane;
  254. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  255. if (!out) {
  256. av_frame_free(&in);
  257. return AVERROR(ENOMEM);
  258. }
  259. av_frame_copy_props(out, in);
  260. if (s->is_rgb) {
  261. /* packed */
  262. inrow0 = in ->data[0];
  263. outrow0 = out->data[0];
  264. for (i = 0; i < in->height; i ++) {
  265. inrow = inrow0;
  266. outrow = outrow0;
  267. for (j = 0; j < inlink->w; j++) {
  268. for (k = 0; k < s->step; k++)
  269. outrow[k] = s->lut[s->rgba_map[k]][inrow[k]];
  270. outrow += s->step;
  271. inrow += s->step;
  272. }
  273. inrow0 += in ->linesize[0];
  274. outrow0 += out->linesize[0];
  275. }
  276. } else {
  277. /* planar */
  278. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  279. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  280. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  281. inrow = in ->data[plane];
  282. outrow = out->data[plane];
  283. for (i = 0; i < in->height >> vsub; i ++) {
  284. for (j = 0; j < inlink->w>>hsub; j++)
  285. outrow[j] = s->lut[plane][inrow[j]];
  286. inrow += in ->linesize[plane];
  287. outrow += out->linesize[plane];
  288. }
  289. }
  290. }
  291. av_frame_free(&in);
  292. return ff_filter_frame(outlink, out);
  293. }
  294. static const AVFilterPad inputs[] = {
  295. { .name = "default",
  296. .type = AVMEDIA_TYPE_VIDEO,
  297. .filter_frame = filter_frame,
  298. .config_props = config_props,
  299. },
  300. { .name = NULL}
  301. };
  302. static const AVFilterPad outputs[] = {
  303. { .name = "default",
  304. .type = AVMEDIA_TYPE_VIDEO, },
  305. { .name = NULL}
  306. };
  307. #define DEFINE_LUT_FILTER(name_, description_, init_, options) \
  308. static const AVClass name_ ## _class = { \
  309. .class_name = #name_, \
  310. .item_name = av_default_item_name, \
  311. .option = options, \
  312. .version = LIBAVUTIL_VERSION_INT, \
  313. }; \
  314. AVFilter avfilter_vf_##name_ = { \
  315. .name = #name_, \
  316. .description = NULL_IF_CONFIG_SMALL(description_), \
  317. .priv_size = sizeof(LutContext), \
  318. .priv_class = &name_ ## _class, \
  319. \
  320. .init = init_, \
  321. .uninit = uninit, \
  322. .query_formats = query_formats, \
  323. \
  324. .inputs = inputs, \
  325. .outputs = outputs, \
  326. }
  327. #if CONFIG_LUT_FILTER
  328. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
  329. #endif
  330. #if CONFIG_LUTYUV_FILTER
  331. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
  332. #endif
  333. #if CONFIG_LUTRGB_FILTER
  334. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
  335. #endif
  336. #if CONFIG_NEGATE_FILTER
  337. static const AVOption negate_options[] = {
  338. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
  339. { NULL },
  340. };
  341. static av_cold int negate_init(AVFilterContext *ctx)
  342. {
  343. LutContext *s = ctx->priv;
  344. int i;
  345. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
  346. for (i = 0; i < 4; i++) {
  347. s->comp_expr_str[i] = av_strdup((i == 3 && s->negate_alpha) ?
  348. "val" : "negval");
  349. if (!s->comp_expr_str[i]) {
  350. uninit(ctx);
  351. return AVERROR(ENOMEM);
  352. }
  353. }
  354. return init(ctx);
  355. }
  356. DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
  357. #endif