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.

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