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.

414 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 LutContext {
  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 const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
  127. static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
  128. static const 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. const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
  133. s->is_yuv ? yuv_pix_fmts :
  134. all_pix_fmts;
  135. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  136. return 0;
  137. }
  138. /**
  139. * Clip value val in the minval - maxval range.
  140. */
  141. static double clip(void *opaque, double val)
  142. {
  143. LutContext *s = opaque;
  144. double minval = s->var_values[VAR_MINVAL];
  145. double maxval = s->var_values[VAR_MAXVAL];
  146. return av_clip(val, minval, maxval);
  147. }
  148. /**
  149. * Compute gamma correction for value val, assuming the minval-maxval
  150. * range, val is clipped to a value contained in the same interval.
  151. */
  152. static double compute_gammaval(void *opaque, double gamma)
  153. {
  154. LutContext *s = opaque;
  155. double val = s->var_values[VAR_CLIPVAL];
  156. double minval = s->var_values[VAR_MINVAL];
  157. double maxval = s->var_values[VAR_MAXVAL];
  158. return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
  159. }
  160. static double (* const funcs1[])(void *, double) = {
  161. clip,
  162. compute_gammaval,
  163. NULL
  164. };
  165. static const char * const funcs1_names[] = {
  166. "clip",
  167. "gammaval",
  168. NULL
  169. };
  170. static int config_props(AVFilterLink *inlink)
  171. {
  172. AVFilterContext *ctx = inlink->dst;
  173. LutContext *s = ctx->priv;
  174. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  175. int min[4], max[4];
  176. int val, comp, ret;
  177. s->hsub = desc->log2_chroma_w;
  178. s->vsub = desc->log2_chroma_h;
  179. s->var_values[VAR_W] = inlink->w;
  180. s->var_values[VAR_H] = inlink->h;
  181. switch (inlink->format) {
  182. case AV_PIX_FMT_YUV410P:
  183. case AV_PIX_FMT_YUV411P:
  184. case AV_PIX_FMT_YUV420P:
  185. case AV_PIX_FMT_YUV422P:
  186. case AV_PIX_FMT_YUV440P:
  187. case AV_PIX_FMT_YUV444P:
  188. case AV_PIX_FMT_YUVA420P:
  189. min[Y] = min[U] = min[V] = 16;
  190. max[Y] = 235;
  191. max[U] = max[V] = 240;
  192. min[A] = 0; max[A] = 255;
  193. break;
  194. default:
  195. min[0] = min[1] = min[2] = min[3] = 0;
  196. max[0] = max[1] = max[2] = max[3] = 255;
  197. }
  198. s->is_yuv = s->is_rgb = 0;
  199. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
  200. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
  201. if (s->is_rgb) {
  202. switch (inlink->format) {
  203. 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;
  204. 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;
  205. case AV_PIX_FMT_RGBA:
  206. 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;
  207. case AV_PIX_FMT_BGRA:
  208. 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;
  209. }
  210. s->step = av_get_bits_per_pixel(desc) >> 3;
  211. }
  212. for (comp = 0; comp < desc->nb_components; comp++) {
  213. double res;
  214. /* create the parsed expression */
  215. av_expr_free(s->comp_expr[comp]);
  216. s->comp_expr[comp] = NULL;
  217. ret = av_expr_parse(&s->comp_expr[comp], s->comp_expr_str[comp],
  218. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  219. if (ret < 0) {
  220. av_log(ctx, AV_LOG_ERROR,
  221. "Error when parsing the expression '%s' for the component %d.\n",
  222. s->comp_expr_str[comp], comp);
  223. return AVERROR(EINVAL);
  224. }
  225. /* compute the s */
  226. s->var_values[VAR_MAXVAL] = max[comp];
  227. s->var_values[VAR_MINVAL] = min[comp];
  228. for (val = 0; val < 256; val++) {
  229. s->var_values[VAR_VAL] = val;
  230. s->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
  231. s->var_values[VAR_NEGVAL] =
  232. av_clip(min[comp] + max[comp] - s->var_values[VAR_VAL],
  233. min[comp], max[comp]);
  234. res = av_expr_eval(s->comp_expr[comp], s->var_values, s);
  235. if (isnan(res)) {
  236. av_log(ctx, AV_LOG_ERROR,
  237. "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
  238. s->comp_expr_str[comp], val, comp);
  239. return AVERROR(EINVAL);
  240. }
  241. s->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
  242. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
  243. }
  244. }
  245. return 0;
  246. }
  247. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  248. {
  249. AVFilterContext *ctx = inlink->dst;
  250. LutContext *s = ctx->priv;
  251. AVFilterLink *outlink = ctx->outputs[0];
  252. AVFrame *out;
  253. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  254. int i, j, k, plane;
  255. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  256. if (!out) {
  257. av_frame_free(&in);
  258. return AVERROR(ENOMEM);
  259. }
  260. av_frame_copy_props(out, in);
  261. if (s->is_rgb) {
  262. /* packed */
  263. inrow0 = in ->data[0];
  264. outrow0 = out->data[0];
  265. for (i = 0; i < in->height; i ++) {
  266. inrow = inrow0;
  267. outrow = outrow0;
  268. for (j = 0; j < inlink->w; j++) {
  269. for (k = 0; k < s->step; k++)
  270. outrow[k] = s->lut[s->rgba_map[k]][inrow[k]];
  271. outrow += s->step;
  272. inrow += s->step;
  273. }
  274. inrow0 += in ->linesize[0];
  275. outrow0 += out->linesize[0];
  276. }
  277. } else {
  278. /* planar */
  279. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  280. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  281. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  282. inrow = in ->data[plane];
  283. outrow = out->data[plane];
  284. for (i = 0; i < in->height >> vsub; i ++) {
  285. for (j = 0; j < inlink->w>>hsub; j++)
  286. outrow[j] = s->lut[plane][inrow[j]];
  287. inrow += in ->linesize[plane];
  288. outrow += out->linesize[plane];
  289. }
  290. }
  291. }
  292. av_frame_free(&in);
  293. return ff_filter_frame(outlink, out);
  294. }
  295. static const AVFilterPad inputs[] = {
  296. { .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .filter_frame = filter_frame,
  299. .config_props = config_props,
  300. },
  301. { .name = NULL}
  302. };
  303. static const AVFilterPad outputs[] = {
  304. { .name = "default",
  305. .type = AVMEDIA_TYPE_VIDEO, },
  306. { .name = NULL}
  307. };
  308. #define DEFINE_LUT_FILTER(name_, description_, init_, options) \
  309. static const AVClass name_ ## _class = { \
  310. .class_name = #name_, \
  311. .item_name = av_default_item_name, \
  312. .option = options, \
  313. .version = LIBAVUTIL_VERSION_INT, \
  314. }; \
  315. AVFilter ff_vf_##name_ = { \
  316. .name = #name_, \
  317. .description = NULL_IF_CONFIG_SMALL(description_), \
  318. .priv_size = sizeof(LutContext), \
  319. .priv_class = &name_ ## _class, \
  320. \
  321. .init = init_, \
  322. .uninit = uninit, \
  323. .query_formats = query_formats, \
  324. \
  325. .inputs = inputs, \
  326. .outputs = outputs, \
  327. }
  328. #if CONFIG_LUT_FILTER
  329. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
  330. #endif
  331. #if CONFIG_LUTYUV_FILTER
  332. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
  333. #endif
  334. #if CONFIG_LUTRGB_FILTER
  335. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
  336. #endif
  337. #if CONFIG_NEGATE_FILTER
  338. static const AVOption negate_options[] = {
  339. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
  340. { NULL },
  341. };
  342. static av_cold int negate_init(AVFilterContext *ctx)
  343. {
  344. LutContext *s = ctx->priv;
  345. int i;
  346. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
  347. for (i = 0; i < 4; i++) {
  348. s->comp_expr_str[i] = av_strdup((i == 3 && s->negate_alpha) ?
  349. "val" : "negval");
  350. if (!s->comp_expr_str[i]) {
  351. uninit(ctx);
  352. return AVERROR(ENOMEM);
  353. }
  354. }
  355. return init(ctx);
  356. }
  357. DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
  358. #endif