vf_lut.c 14 KB

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