vf_lut.c 14 KB

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