vf_lut.c 14 KB

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