You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
13KB

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