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.

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