vf_lut.c 13 KB

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