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
14KB

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