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.

410 lines
14KB

  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/common.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. static const char *const var_names[] = {
  35. "E",
  36. "PHI",
  37. "PI",
  38. "w", ///< width of the input video
  39. "h", ///< height of the input video
  40. "val", ///< input value for the pixel
  41. "maxval", ///< max value for the pixel
  42. "minval", ///< min value for the pixel
  43. "negval", ///< negated value
  44. "clipval",
  45. NULL
  46. };
  47. enum var_name {
  48. VAR_E,
  49. VAR_PHI,
  50. VAR_PI,
  51. VAR_W,
  52. VAR_H,
  53. VAR_VAL,
  54. VAR_MAXVAL,
  55. VAR_MINVAL,
  56. VAR_NEGVAL,
  57. VAR_CLIPVAL,
  58. VAR_VARS_NB
  59. };
  60. typedef struct {
  61. const AVClass *class;
  62. uint8_t lut[4][256]; ///< lookup table for each component
  63. char *comp_expr_str[4];
  64. AVExpr *comp_expr[4];
  65. int hsub, vsub;
  66. double var_values[VAR_VARS_NB];
  67. int is_rgb, is_yuv;
  68. int rgba_map[4];
  69. int step;
  70. int negate_alpha; /* only used by negate */
  71. } LutContext;
  72. #define Y 0
  73. #define U 1
  74. #define V 2
  75. #define R 0
  76. #define G 1
  77. #define B 2
  78. #define A 3
  79. #define OFFSET(x) offsetof(LutContext, x)
  80. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  81. static const AVOption lut_options[] = {
  82. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  83. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  84. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  85. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  86. { "y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  87. { "u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  88. { "v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  89. { "r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  90. { "g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  91. { "b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  92. { "a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  93. { NULL },
  94. };
  95. static int init(AVFilterContext *ctx)
  96. {
  97. LutContext *lut = ctx->priv;
  98. lut->var_values[VAR_PHI] = M_PHI;
  99. lut->var_values[VAR_PI] = M_PI;
  100. lut->var_values[VAR_E ] = M_E;
  101. lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
  102. lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
  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. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
  117. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
  118. AV_PIX_FMT_YUVA420P, \
  119. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
  120. AV_PIX_FMT_YUVJ440P
  121. #define RGB_FORMATS \
  122. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
  123. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
  124. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
  125. static enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
  126. static enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
  127. static enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
  128. static int query_formats(AVFilterContext *ctx)
  129. {
  130. LutContext *lut = ctx->priv;
  131. enum AVPixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
  132. lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
  133. ff_set_common_formats(ctx, ff_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. clip,
  160. 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_desc_get(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 AV_PIX_FMT_YUV410P:
  181. case AV_PIX_FMT_YUV411P:
  182. case AV_PIX_FMT_YUV420P:
  183. case AV_PIX_FMT_YUV422P:
  184. case AV_PIX_FMT_YUV440P:
  185. case AV_PIX_FMT_YUV444P:
  186. case AV_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 AV_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 AV_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 AV_PIX_FMT_RGBA:
  204. case AV_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 AV_PIX_FMT_BGRA:
  206. case AV_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. /* create the parsed expression */
  213. ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
  214. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  215. if (ret < 0) {
  216. av_log(ctx, AV_LOG_ERROR,
  217. "Error when parsing the expression '%s' for the component %d.\n",
  218. lut->comp_expr_str[comp], comp);
  219. return AVERROR(EINVAL);
  220. }
  221. /* compute the lut */
  222. lut->var_values[VAR_MAXVAL] = max[comp];
  223. lut->var_values[VAR_MINVAL] = min[comp];
  224. for (val = 0; val < 256; val++) {
  225. lut->var_values[VAR_VAL] = val;
  226. lut->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
  227. lut->var_values[VAR_NEGVAL] =
  228. av_clip(min[comp] + max[comp] - lut->var_values[VAR_VAL],
  229. min[comp], max[comp]);
  230. res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
  231. if (isnan(res)) {
  232. av_log(ctx, AV_LOG_ERROR,
  233. "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
  234. lut->comp_expr_str[comp], val, comp);
  235. return AVERROR(EINVAL);
  236. }
  237. lut->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
  238. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
  239. }
  240. }
  241. return 0;
  242. }
  243. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  244. {
  245. AVFilterContext *ctx = inlink->dst;
  246. LutContext *lut = ctx->priv;
  247. AVFilterLink *outlink = ctx->outputs[0];
  248. AVFrame *out;
  249. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  250. int i, j, k, plane;
  251. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  252. if (!out) {
  253. av_frame_free(&in);
  254. return AVERROR(ENOMEM);
  255. }
  256. av_frame_copy_props(out, in);
  257. if (lut->is_rgb) {
  258. /* packed */
  259. inrow0 = in ->data[0];
  260. outrow0 = out->data[0];
  261. for (i = 0; i < in->height; i ++) {
  262. inrow = inrow0;
  263. outrow = outrow0;
  264. for (j = 0; j < inlink->w; j++) {
  265. for (k = 0; k < lut->step; k++)
  266. outrow[k] = lut->lut[lut->rgba_map[k]][inrow[k]];
  267. outrow += lut->step;
  268. inrow += lut->step;
  269. }
  270. inrow0 += in ->linesize[0];
  271. outrow0 += out->linesize[0];
  272. }
  273. } else {
  274. /* planar */
  275. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  276. int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
  277. int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
  278. inrow = in ->data[plane];
  279. outrow = out->data[plane];
  280. for (i = 0; i < in->height >> vsub; i ++) {
  281. for (j = 0; j < inlink->w>>hsub; j++)
  282. outrow[j] = lut->lut[plane][inrow[j]];
  283. inrow += in ->linesize[plane];
  284. outrow += out->linesize[plane];
  285. }
  286. }
  287. }
  288. av_frame_free(&in);
  289. return ff_filter_frame(outlink, out);
  290. }
  291. static const AVFilterPad inputs[] = {
  292. { .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .filter_frame = filter_frame,
  295. .config_props = config_props,
  296. },
  297. { .name = NULL}
  298. };
  299. static const AVFilterPad outputs[] = {
  300. { .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO, },
  302. { .name = NULL}
  303. };
  304. #define DEFINE_LUT_FILTER(name_, description_, init_, options) \
  305. static const AVClass name_ ## _class = { \
  306. .class_name = #name_, \
  307. .item_name = av_default_item_name, \
  308. .option = options, \
  309. .version = LIBAVUTIL_VERSION_INT, \
  310. }; \
  311. AVFilter avfilter_vf_##name_ = { \
  312. .name = #name_, \
  313. .description = NULL_IF_CONFIG_SMALL(description_), \
  314. .priv_size = sizeof(LutContext), \
  315. .priv_class = &name_ ## _class, \
  316. \
  317. .init = init_, \
  318. .uninit = uninit, \
  319. .query_formats = query_formats, \
  320. \
  321. .inputs = inputs, \
  322. .outputs = outputs, \
  323. }
  324. #if CONFIG_LUT_FILTER
  325. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
  326. #endif
  327. #if CONFIG_LUTYUV_FILTER
  328. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
  329. #endif
  330. #if CONFIG_LUTRGB_FILTER
  331. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
  332. #endif
  333. #if CONFIG_NEGATE_FILTER
  334. static const AVOption negate_options[] = {
  335. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
  336. { NULL },
  337. };
  338. static int negate_init(AVFilterContext *ctx)
  339. {
  340. LutContext *lut = ctx->priv;
  341. int i;
  342. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
  343. for (i = 0; i < 4; i++) {
  344. lut->comp_expr_str[i] = av_strdup((i == 3 && lut->negate_alpha) ?
  345. "val" : "negval");
  346. if (!lut->comp_expr_str[i]) {
  347. uninit(ctx);
  348. return AVERROR(ENOMEM);
  349. }
  350. }
  351. return init(ctx);
  352. }
  353. DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
  354. #endif