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.

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