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.

395 lines
13KB

  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/eval.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. static const char *var_names[] = {
  32. "E",
  33. "PHI",
  34. "PI",
  35. "w", ///< width of the input video
  36. "h", ///< height of the input video
  37. "val", ///< input value for the pixel
  38. "maxval", ///< max value for the pixel
  39. "minval", ///< min value for the pixel
  40. "negval", ///< negated value
  41. "clipval",
  42. NULL
  43. };
  44. enum var_name {
  45. VAR_E,
  46. VAR_PHI,
  47. VAR_PI,
  48. VAR_W,
  49. VAR_H,
  50. VAR_VAL,
  51. VAR_MAXVAL,
  52. VAR_MINVAL,
  53. VAR_NEGVAL,
  54. VAR_CLIPVAL,
  55. VAR_VARS_NB
  56. };
  57. typedef struct {
  58. const AVClass *class;
  59. uint8_t lut[4][256]; ///< lookup table for each component
  60. char *comp_expr_str[4];
  61. AVExpr *comp_expr[4];
  62. int hsub, vsub;
  63. double var_values[VAR_VARS_NB];
  64. int is_rgb, is_yuv;
  65. int rgba_map[4];
  66. int step;
  67. int negate_alpha; /* only used by negate */
  68. } LutContext;
  69. #define Y 0
  70. #define U 1
  71. #define V 2
  72. #define R 0
  73. #define G 1
  74. #define B 2
  75. #define A 3
  76. #define OFFSET(x) offsetof(LutContext, x)
  77. static const AVOption lut_options[] = {
  78. {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  79. {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  80. {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  81. {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  82. {"y", "set Y expression", OFFSET(comp_expr_str[Y]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  83. {"u", "set U expression", OFFSET(comp_expr_str[U]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  84. {"v", "set V expression", OFFSET(comp_expr_str[V]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  85. {"r", "set R expression", OFFSET(comp_expr_str[R]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  86. {"g", "set G expression", OFFSET(comp_expr_str[G]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  87. {"b", "set B expression", OFFSET(comp_expr_str[B]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  88. {"a", "set A expression", OFFSET(comp_expr_str[A]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
  89. {NULL},
  90. };
  91. static const char *lut_get_name(void *ctx)
  92. {
  93. return "lut";
  94. }
  95. static const AVClass lut_class = {
  96. "LutContext",
  97. lut_get_name,
  98. lut_options
  99. };
  100. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  101. {
  102. LutContext *lut = ctx->priv;
  103. int ret;
  104. lut->class = &lut_class;
  105. av_opt_set_defaults(lut);
  106. lut->var_values[VAR_PHI] = M_PHI;
  107. lut->var_values[VAR_PI] = M_PI;
  108. lut->var_values[VAR_E ] = M_E;
  109. lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
  110. lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
  111. if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
  112. return ret;
  113. return 0;
  114. }
  115. static av_cold void uninit(AVFilterContext *ctx)
  116. {
  117. LutContext *lut = ctx->priv;
  118. int i;
  119. for (i = 0; i < 4; i++) {
  120. av_expr_free(lut->comp_expr[i]);
  121. lut->comp_expr[i] = NULL;
  122. av_freep(&lut->comp_expr_str[i]);
  123. }
  124. }
  125. #define YUV_FORMATS \
  126. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, \
  127. PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P, \
  128. PIX_FMT_YUVA420P, \
  129. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P, \
  130. PIX_FMT_YUVJ440P
  131. #define RGB_FORMATS \
  132. PIX_FMT_ARGB, PIX_FMT_RGBA, \
  133. PIX_FMT_ABGR, PIX_FMT_BGRA, \
  134. PIX_FMT_RGB24, PIX_FMT_BGR24
  135. static enum PixelFormat yuv_pix_fmts[] = { YUV_FORMATS, PIX_FMT_NONE };
  136. static enum PixelFormat rgb_pix_fmts[] = { RGB_FORMATS, PIX_FMT_NONE };
  137. static enum PixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, PIX_FMT_NONE };
  138. static int query_formats(AVFilterContext *ctx)
  139. {
  140. LutContext *lut = ctx->priv;
  141. enum PixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
  142. lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
  143. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  144. return 0;
  145. }
  146. /**
  147. * Clip value val in the minval - maxval range.
  148. */
  149. static double clip(void *opaque, double val)
  150. {
  151. LutContext *lut = opaque;
  152. double minval = lut->var_values[VAR_MINVAL];
  153. double maxval = lut->var_values[VAR_MAXVAL];
  154. return av_clip(val, minval, maxval);
  155. }
  156. /**
  157. * Compute gamma correction for value val, assuming the minval-maxval
  158. * range, val is clipped to a value contained in the same interval.
  159. */
  160. static double compute_gammaval(void *opaque, double gamma)
  161. {
  162. LutContext *lut = opaque;
  163. double val = lut->var_values[VAR_CLIPVAL];
  164. double minval = lut->var_values[VAR_MINVAL];
  165. double maxval = lut->var_values[VAR_MAXVAL];
  166. return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
  167. }
  168. static double (* const funcs1[])(void *, double) = {
  169. clip,
  170. compute_gammaval,
  171. NULL
  172. };
  173. static const char * const funcs1_names[] = {
  174. "clip",
  175. "gammaval",
  176. NULL
  177. };
  178. static int config_props(AVFilterLink *inlink)
  179. {
  180. AVFilterContext *ctx = inlink->dst;
  181. LutContext *lut = ctx->priv;
  182. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  183. int min[4], max[4];
  184. int val, comp, ret;
  185. lut->hsub = desc->log2_chroma_w;
  186. lut->vsub = desc->log2_chroma_h;
  187. lut->var_values[VAR_W] = inlink->w;
  188. lut->var_values[VAR_H] = inlink->h;
  189. switch (inlink->format) {
  190. case PIX_FMT_YUV410P:
  191. case PIX_FMT_YUV411P:
  192. case PIX_FMT_YUV420P:
  193. case PIX_FMT_YUV422P:
  194. case PIX_FMT_YUV440P:
  195. case PIX_FMT_YUV444P:
  196. case PIX_FMT_YUVA420P:
  197. min[Y] = min[U] = min[V] = 16;
  198. max[Y] = 235;
  199. max[U] = max[V] = 240;
  200. min[A] = 0; max[A] = 255;
  201. break;
  202. default:
  203. min[0] = min[1] = min[2] = min[3] = 0;
  204. max[0] = max[1] = max[2] = max[3] = 255;
  205. }
  206. lut->is_yuv = lut->is_rgb = 0;
  207. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
  208. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
  209. if (lut->is_rgb) {
  210. switch (inlink->format) {
  211. case PIX_FMT_ARGB: lut->rgba_map[A] = 0; lut->rgba_map[R] = 1; lut->rgba_map[G] = 2; lut->rgba_map[B] = 3; break;
  212. case PIX_FMT_ABGR: lut->rgba_map[A] = 0; lut->rgba_map[B] = 1; lut->rgba_map[G] = 2; lut->rgba_map[R] = 3; break;
  213. case PIX_FMT_RGBA:
  214. case PIX_FMT_RGB24: lut->rgba_map[R] = 0; lut->rgba_map[G] = 1; lut->rgba_map[B] = 2; lut->rgba_map[A] = 3; break;
  215. case PIX_FMT_BGRA:
  216. case PIX_FMT_BGR24: lut->rgba_map[B] = 0; lut->rgba_map[G] = 1; lut->rgba_map[R] = 2; lut->rgba_map[A] = 3; break;
  217. }
  218. lut->step = av_get_bits_per_pixel(desc) >> 3;
  219. }
  220. for (comp = 0; comp < desc->nb_components; comp++) {
  221. double res;
  222. /* create the parsed expression */
  223. ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
  224. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  225. if (ret < 0) {
  226. av_log(ctx, AV_LOG_ERROR,
  227. "Error when parsing the expression '%s' for the component %d.\n",
  228. lut->comp_expr_str[comp], comp);
  229. return AVERROR(EINVAL);
  230. }
  231. /* compute the lut */
  232. lut->var_values[VAR_MAXVAL] = max[comp];
  233. lut->var_values[VAR_MINVAL] = min[comp];
  234. for (val = 0; val < 256; val++) {
  235. lut->var_values[VAR_VAL] = val;
  236. lut->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
  237. lut->var_values[VAR_NEGVAL] =
  238. av_clip(min[comp] + max[comp] - lut->var_values[VAR_VAL],
  239. min[comp], max[comp]);
  240. res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
  241. if (isnan(res)) {
  242. av_log(ctx, AV_LOG_ERROR,
  243. "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
  244. lut->comp_expr_str[comp], val, comp);
  245. return AVERROR(EINVAL);
  246. }
  247. lut->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
  248. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
  249. }
  250. }
  251. return 0;
  252. }
  253. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  254. {
  255. AVFilterContext *ctx = inlink->dst;
  256. LutContext *lut = ctx->priv;
  257. AVFilterLink *outlink = ctx->outputs[0];
  258. AVFilterBufferRef *inpic = inlink ->cur_buf;
  259. AVFilterBufferRef *outpic = outlink->out_buf;
  260. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  261. int i, j, k, plane;
  262. if (lut->is_rgb) {
  263. /* packed */
  264. inrow0 = inpic ->data[0] + y * inpic ->linesize[0];
  265. outrow0 = outpic->data[0] + y * outpic->linesize[0];
  266. for (i = 0; i < h; i ++) {
  267. inrow = inrow0;
  268. outrow = outrow0;
  269. for (j = 0; j < inlink->w; j++) {
  270. for (k = 0; k < lut->step; k++)
  271. outrow[k] = lut->lut[lut->rgba_map[k]][inrow[k]];
  272. outrow += lut->step;
  273. inrow += lut->step;
  274. }
  275. inrow0 += inpic ->linesize[0];
  276. outrow0 += outpic->linesize[0];
  277. }
  278. } else {
  279. /* planar */
  280. for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
  281. int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
  282. int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
  283. inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
  284. outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
  285. for (i = 0; i < h>>vsub; i ++) {
  286. for (j = 0; j < inlink->w>>hsub; j++)
  287. outrow[j] = lut->lut[plane][inrow[j]];
  288. inrow += inpic ->linesize[plane];
  289. outrow += outpic->linesize[plane];
  290. }
  291. }
  292. }
  293. avfilter_draw_slice(outlink, y, h, slice_dir);
  294. }
  295. #define DEFINE_LUT_FILTER(name_, description_, init_) \
  296. AVFilter avfilter_vf_##name_ = { \
  297. .name = #name_, \
  298. .description = NULL_IF_CONFIG_SMALL(description_), \
  299. .priv_size = sizeof(LutContext), \
  300. \
  301. .init = init_, \
  302. .uninit = uninit, \
  303. .query_formats = query_formats, \
  304. \
  305. .inputs = (AVFilterPad[]) {{ .name = "default", \
  306. .type = AVMEDIA_TYPE_VIDEO, \
  307. .draw_slice = draw_slice, \
  308. .config_props = config_props, \
  309. .min_perms = AV_PERM_READ, }, \
  310. { .name = NULL}}, \
  311. .outputs = (AVFilterPad[]) {{ .name = "default", \
  312. .type = AVMEDIA_TYPE_VIDEO, }, \
  313. { .name = NULL}}, \
  314. }
  315. #if CONFIG_LUT_FILTER
  316. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init);
  317. #endif
  318. #if CONFIG_LUTYUV_FILTER
  319. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init);
  320. #endif
  321. #if CONFIG_LUTRGB_FILTER
  322. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init);
  323. #endif
  324. #if CONFIG_NEGATE_FILTER
  325. static int negate_init(AVFilterContext *ctx, const char *args, void *opaque)
  326. {
  327. LutContext *lut = ctx->priv;
  328. char lut_params[64];
  329. if (args)
  330. sscanf(args, "%d", &lut->negate_alpha);
  331. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
  332. snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
  333. lut->negate_alpha ? "negval" : "val");
  334. return init(ctx, lut_params, opaque);
  335. }
  336. DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init);
  337. #endif