vf_lut.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/attributes.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. static const char *const var_names[] = {
  36. "w", ///< width of the input video
  37. "h", ///< height of the input video
  38. "val", ///< input value for the pixel
  39. "maxval", ///< max value for the pixel
  40. "minval", ///< min value for the pixel
  41. "negval", ///< negated value
  42. "clipval",
  43. NULL
  44. };
  45. enum var_name {
  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 LutContext {
  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 step;
  64. int negate_alpha; /* only used by negate */
  65. } LutContext;
  66. #define Y 0
  67. #define U 1
  68. #define V 2
  69. #define R 0
  70. #define G 1
  71. #define B 2
  72. #define A 3
  73. #define OFFSET(x) offsetof(LutContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. static const AVOption options[] = {
  76. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  77. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  78. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  79. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  80. { "y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  81. { "u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  82. { "v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  83. { "r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  84. { "g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  85. { "b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  86. { "a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  87. { NULL }
  88. };
  89. static av_cold void uninit(AVFilterContext *ctx)
  90. {
  91. LutContext *s = ctx->priv;
  92. int i;
  93. for (i = 0; i < 4; i++) {
  94. av_expr_free(s->comp_expr[i]);
  95. s->comp_expr[i] = NULL;
  96. av_freep(&s->comp_expr_str[i]);
  97. }
  98. }
  99. #define YUV_FORMATS \
  100. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
  101. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
  102. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, \
  103. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
  104. AV_PIX_FMT_YUVJ440P
  105. #define RGB_FORMATS \
  106. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
  107. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
  108. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
  109. static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
  110. static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
  111. static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
  112. static int query_formats(AVFilterContext *ctx)
  113. {
  114. LutContext *s = ctx->priv;
  115. const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
  116. s->is_yuv ? yuv_pix_fmts :
  117. all_pix_fmts;
  118. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  119. return 0;
  120. }
  121. /**
  122. * Clip value val in the minval - maxval range.
  123. */
  124. static double clip(void *opaque, double val)
  125. {
  126. LutContext *s = opaque;
  127. double minval = s->var_values[VAR_MINVAL];
  128. double maxval = s->var_values[VAR_MAXVAL];
  129. return av_clip(val, minval, maxval);
  130. }
  131. /**
  132. * Compute gamma correction for value val, assuming the minval-maxval
  133. * range, val is clipped to a value contained in the same interval.
  134. */
  135. static double compute_gammaval(void *opaque, double gamma)
  136. {
  137. LutContext *s = opaque;
  138. double val = s->var_values[VAR_CLIPVAL];
  139. double minval = s->var_values[VAR_MINVAL];
  140. double maxval = s->var_values[VAR_MAXVAL];
  141. return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
  142. }
  143. /**
  144. * Compute ITU Rec.709 gamma correction of value val.
  145. */
  146. static double compute_gammaval709(void *opaque, double gamma)
  147. {
  148. LutContext *s = opaque;
  149. double val = s->var_values[VAR_CLIPVAL];
  150. double minval = s->var_values[VAR_MINVAL];
  151. double maxval = s->var_values[VAR_MAXVAL];
  152. double level = (val - minval) / (maxval - minval);
  153. level = level < 0.018 ? 4.5 * level
  154. : 1.099 * pow(level, 1.0 / gamma) - 0.099;
  155. return level * (maxval - minval) + minval;
  156. }
  157. static double (* const funcs1[])(void *, double) = {
  158. (void *)clip,
  159. (void *)compute_gammaval,
  160. (void *)compute_gammaval709,
  161. NULL
  162. };
  163. static const char * const funcs1_names[] = {
  164. "clip",
  165. "gammaval",
  166. "gammaval709",
  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. uint8_t rgba_map[4]; /* component index -> RGBA color index map */
  175. int min[4], max[4];
  176. int val, color, 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. case AV_PIX_FMT_YUVA422P:
  190. case AV_PIX_FMT_YUVA444P:
  191. min[Y] = min[U] = min[V] = 16;
  192. max[Y] = 235;
  193. max[U] = max[V] = 240;
  194. min[A] = 0; max[A] = 255;
  195. break;
  196. default:
  197. min[0] = min[1] = min[2] = min[3] = 0;
  198. max[0] = max[1] = max[2] = max[3] = 255;
  199. }
  200. s->is_yuv = s->is_rgb = 0;
  201. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
  202. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
  203. if (s->is_rgb) {
  204. ff_fill_rgba_map(rgba_map, inlink->format);
  205. s->step = av_get_bits_per_pixel(desc) >> 3;
  206. }
  207. for (color = 0; color < desc->nb_components; color++) {
  208. double res;
  209. int comp = s->is_rgb ? rgba_map[color] : color;
  210. /* create the parsed expression */
  211. av_expr_free(s->comp_expr[color]);
  212. s->comp_expr[color] = NULL;
  213. ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
  214. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  215. if (ret < 0) {
  216. av_log(ctx, AV_LOG_ERROR,
  217. "Error when parsing the expression '%s' for the component %d and color %d.\n",
  218. s->comp_expr_str[comp], comp, color);
  219. return AVERROR(EINVAL);
  220. }
  221. /* compute the lut */
  222. s->var_values[VAR_MAXVAL] = max[color];
  223. s->var_values[VAR_MINVAL] = min[color];
  224. for (val = 0; val < 256; val++) {
  225. s->var_values[VAR_VAL] = val;
  226. s->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
  227. s->var_values[VAR_NEGVAL] =
  228. av_clip(min[color] + max[color] - s->var_values[VAR_VAL],
  229. min[color], max[color]);
  230. res = av_expr_eval(s->comp_expr[color], s->var_values, s);
  231. if (isnan(res)) {
  232. av_log(ctx, AV_LOG_ERROR,
  233. "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
  234. s->comp_expr_str[color], val, comp);
  235. return AVERROR(EINVAL);
  236. }
  237. s->lut[comp][val] = av_clip((int)res, min[color], max[color]);
  238. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
  239. }
  240. }
  241. return 0;
  242. }
  243. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  244. {
  245. AVFilterContext *ctx = inlink->dst;
  246. LutContext *s = ctx->priv;
  247. AVFilterLink *outlink = ctx->outputs[0];
  248. AVFrame *out;
  249. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  250. int i, j, plane, direct = 0;
  251. if (av_frame_is_writable(in)) {
  252. direct = 1;
  253. out = in;
  254. } else {
  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. }
  262. if (s->is_rgb) {
  263. /* packed */
  264. const int w = inlink->w;
  265. const int h = in->height;
  266. const uint8_t (*tab)[256] = (const uint8_t (*)[256])s->lut;
  267. const int in_linesize = in->linesize[0];
  268. const int out_linesize = out->linesize[0];
  269. const int step = s->step;
  270. inrow0 = in ->data[0];
  271. outrow0 = out->data[0];
  272. for (i = 0; i < h; i ++) {
  273. inrow = inrow0;
  274. outrow = outrow0;
  275. for (j = 0; j < w; j++) {
  276. switch (step) {
  277. case 4: outrow[3] = tab[3][inrow[3]]; // Fall-through
  278. case 3: outrow[2] = tab[2][inrow[2]]; // Fall-through
  279. case 2: outrow[1] = tab[1][inrow[1]]; // Fall-through
  280. default: outrow[0] = tab[0][inrow[0]];
  281. }
  282. outrow += step;
  283. inrow += step;
  284. }
  285. inrow0 += in_linesize;
  286. outrow0 += out_linesize;
  287. }
  288. } else {
  289. /* planar */
  290. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  291. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  292. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  293. int h = FF_CEIL_RSHIFT(inlink->h, vsub);
  294. int w = FF_CEIL_RSHIFT(inlink->w, hsub);
  295. const uint8_t *tab = s->lut[plane];
  296. const int in_linesize = in->linesize[plane];
  297. const int out_linesize = out->linesize[plane];
  298. inrow = in ->data[plane];
  299. outrow = out->data[plane];
  300. for (i = 0; i < h; i++) {
  301. for (j = 0; j < w; j++)
  302. outrow[j] = tab[inrow[j]];
  303. inrow += in_linesize;
  304. outrow += out_linesize;
  305. }
  306. }
  307. }
  308. if (!direct)
  309. av_frame_free(&in);
  310. return ff_filter_frame(outlink, out);
  311. }
  312. static const AVFilterPad inputs[] = {
  313. { .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .filter_frame = filter_frame,
  316. .config_props = config_props,
  317. },
  318. { NULL }
  319. };
  320. static const AVFilterPad outputs[] = {
  321. { .name = "default",
  322. .type = AVMEDIA_TYPE_VIDEO,
  323. },
  324. { NULL }
  325. };
  326. #define DEFINE_LUT_FILTER(name_, description_) \
  327. AVFilter ff_vf_##name_ = { \
  328. .name = #name_, \
  329. .description = NULL_IF_CONFIG_SMALL(description_), \
  330. .priv_size = sizeof(LutContext), \
  331. .priv_class = &name_ ## _class, \
  332. .init = name_##_init, \
  333. .uninit = uninit, \
  334. .query_formats = query_formats, \
  335. .inputs = inputs, \
  336. .outputs = outputs, \
  337. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, \
  338. }
  339. #if CONFIG_LUT_FILTER
  340. #define lut_options options
  341. AVFILTER_DEFINE_CLASS(lut);
  342. static int lut_init(AVFilterContext *ctx)
  343. {
  344. return 0;
  345. }
  346. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
  347. #endif
  348. #if CONFIG_LUTYUV_FILTER
  349. #define lutyuv_options options
  350. AVFILTER_DEFINE_CLASS(lutyuv);
  351. static av_cold int lutyuv_init(AVFilterContext *ctx)
  352. {
  353. LutContext *s = ctx->priv;
  354. s->is_yuv = 1;
  355. return 0;
  356. }
  357. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
  358. #endif
  359. #if CONFIG_LUTRGB_FILTER
  360. #define lutrgb_options options
  361. AVFILTER_DEFINE_CLASS(lutrgb);
  362. static av_cold int lutrgb_init(AVFilterContext *ctx)
  363. {
  364. LutContext *s = ctx->priv;
  365. s->is_rgb = 1;
  366. return 0;
  367. }
  368. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
  369. #endif
  370. #if CONFIG_NEGATE_FILTER
  371. static const AVOption negate_options[] = {
  372. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  373. { NULL }
  374. };
  375. AVFILTER_DEFINE_CLASS(negate);
  376. static av_cold int negate_init(AVFilterContext *ctx)
  377. {
  378. LutContext *s = ctx->priv;
  379. int i;
  380. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
  381. for (i = 0; i < 4; i++) {
  382. s->comp_expr_str[i] = av_strdup((i == 3 && !s->negate_alpha) ?
  383. "val" : "negval");
  384. if (!s->comp_expr_str[i]) {
  385. uninit(ctx);
  386. return AVERROR(ENOMEM);
  387. }
  388. }
  389. return 0;
  390. }
  391. DEFINE_LUT_FILTER(negate, "Negate input video.");
  392. #endif