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.

430 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 "drawutils.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. static const char *const var_names[] = {
  35. "w", ///< width of the input video
  36. "h", ///< height of the input video
  37. "val", ///< input value for the pixel
  38. "maxval", ///< max value for the pixel
  39. "minval", ///< min value for the pixel
  40. "negval", ///< negated value
  41. "clipval",
  42. NULL
  43. };
  44. enum var_name {
  45. VAR_W,
  46. VAR_H,
  47. VAR_VAL,
  48. VAR_MAXVAL,
  49. VAR_MINVAL,
  50. VAR_NEGVAL,
  51. VAR_CLIPVAL,
  52. VAR_VARS_NB
  53. };
  54. typedef struct {
  55. const AVClass *class;
  56. uint8_t lut[4][256]; ///< lookup table for each component
  57. char *comp_expr_str[4];
  58. AVExpr *comp_expr[4];
  59. int hsub, vsub;
  60. double var_values[VAR_VARS_NB];
  61. int is_rgb, is_yuv;
  62. int step;
  63. int negate_alpha; /* only used by negate */
  64. } LutContext;
  65. #define Y 0
  66. #define U 1
  67. #define V 2
  68. #define R 0
  69. #define G 1
  70. #define B 2
  71. #define A 3
  72. #define OFFSET(x) offsetof(LutContext, x)
  73. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  74. static const AVOption options[] = {
  75. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  76. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  77. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  78. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  79. { "y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  80. { "u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  81. { "v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  82. { "r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  83. { "g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  84. { "b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  85. { "a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
  86. { NULL },
  87. };
  88. static av_cold void uninit(AVFilterContext *ctx)
  89. {
  90. LutContext *lut = ctx->priv;
  91. int i;
  92. for (i = 0; i < 4; i++) {
  93. av_expr_free(lut->comp_expr[i]);
  94. lut->comp_expr[i] = NULL;
  95. av_freep(&lut->comp_expr_str[i]);
  96. }
  97. }
  98. #define YUV_FORMATS \
  99. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
  100. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
  101. AV_PIX_FMT_YUVA420P, \
  102. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
  103. AV_PIX_FMT_YUVJ440P
  104. #define RGB_FORMATS \
  105. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
  106. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
  107. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
  108. static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
  109. static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
  110. static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
  111. static int query_formats(AVFilterContext *ctx)
  112. {
  113. LutContext *lut = ctx->priv;
  114. const enum AVPixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
  115. lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
  116. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  117. return 0;
  118. }
  119. /**
  120. * Clip value val in the minval - maxval range.
  121. */
  122. static double clip(void *opaque, double val)
  123. {
  124. LutContext *lut = opaque;
  125. double minval = lut->var_values[VAR_MINVAL];
  126. double maxval = lut->var_values[VAR_MAXVAL];
  127. return av_clip(val, minval, maxval);
  128. }
  129. /**
  130. * Compute gamma correction for value val, assuming the minval-maxval
  131. * range, val is clipped to a value contained in the same interval.
  132. */
  133. static double compute_gammaval(void *opaque, double gamma)
  134. {
  135. LutContext *lut = opaque;
  136. double val = lut->var_values[VAR_CLIPVAL];
  137. double minval = lut->var_values[VAR_MINVAL];
  138. double maxval = lut->var_values[VAR_MAXVAL];
  139. return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
  140. }
  141. static double (* const funcs1[])(void *, double) = {
  142. (void *)clip,
  143. (void *)compute_gammaval,
  144. NULL
  145. };
  146. static const char * const funcs1_names[] = {
  147. "clip",
  148. "gammaval",
  149. NULL
  150. };
  151. static int config_props(AVFilterLink *inlink)
  152. {
  153. AVFilterContext *ctx = inlink->dst;
  154. LutContext *lut = ctx->priv;
  155. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  156. uint8_t rgba_map[4]; /* component index -> RGBA color index map */
  157. int min[4], max[4];
  158. int val, color, ret;
  159. lut->hsub = desc->log2_chroma_w;
  160. lut->vsub = desc->log2_chroma_h;
  161. lut->var_values[VAR_W] = inlink->w;
  162. lut->var_values[VAR_H] = inlink->h;
  163. switch (inlink->format) {
  164. case AV_PIX_FMT_YUV410P:
  165. case AV_PIX_FMT_YUV411P:
  166. case AV_PIX_FMT_YUV420P:
  167. case AV_PIX_FMT_YUV422P:
  168. case AV_PIX_FMT_YUV440P:
  169. case AV_PIX_FMT_YUV444P:
  170. case AV_PIX_FMT_YUVA420P:
  171. min[Y] = min[U] = min[V] = 16;
  172. max[Y] = 235;
  173. max[U] = max[V] = 240;
  174. min[A] = 0; max[A] = 255;
  175. break;
  176. default:
  177. min[0] = min[1] = min[2] = min[3] = 0;
  178. max[0] = max[1] = max[2] = max[3] = 255;
  179. }
  180. lut->is_yuv = lut->is_rgb = 0;
  181. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
  182. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
  183. if (lut->is_rgb) {
  184. ff_fill_rgba_map(rgba_map, inlink->format);
  185. lut->step = av_get_bits_per_pixel(desc) >> 3;
  186. }
  187. for (color = 0; color < desc->nb_components; color++) {
  188. double res;
  189. int comp = lut->is_rgb ? rgba_map[color] : color;
  190. /* create the parsed expression */
  191. ret = av_expr_parse(&lut->comp_expr[color], lut->comp_expr_str[color],
  192. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  193. if (ret < 0) {
  194. av_log(ctx, AV_LOG_ERROR,
  195. "Error when parsing the expression '%s' for the component %d and color %d.\n",
  196. lut->comp_expr_str[comp], comp, color);
  197. return AVERROR(EINVAL);
  198. }
  199. /* compute the lut */
  200. lut->var_values[VAR_MAXVAL] = max[color];
  201. lut->var_values[VAR_MINVAL] = min[color];
  202. for (val = 0; val < 256; val++) {
  203. lut->var_values[VAR_VAL] = val;
  204. lut->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
  205. lut->var_values[VAR_NEGVAL] =
  206. av_clip(min[color] + max[color] - lut->var_values[VAR_VAL],
  207. min[color], max[color]);
  208. res = av_expr_eval(lut->comp_expr[color], lut->var_values, lut);
  209. if (isnan(res)) {
  210. av_log(ctx, AV_LOG_ERROR,
  211. "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
  212. lut->comp_expr_str[color], val, comp);
  213. return AVERROR(EINVAL);
  214. }
  215. lut->lut[comp][val] = av_clip((int)res, min[color], max[color]);
  216. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
  217. }
  218. }
  219. return 0;
  220. }
  221. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  222. {
  223. AVFilterContext *ctx = inlink->dst;
  224. LutContext *lut = ctx->priv;
  225. AVFilterLink *outlink = ctx->outputs[0];
  226. AVFrame *out;
  227. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  228. int i, j, plane;
  229. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  230. if (!out) {
  231. av_frame_free(&in);
  232. return AVERROR(ENOMEM);
  233. }
  234. av_frame_copy_props(out, in);
  235. if (lut->is_rgb) {
  236. /* packed */
  237. inrow0 = in ->data[0];
  238. outrow0 = out->data[0];
  239. for (i = 0; i < in->height; 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 += in ->linesize[0];
  259. outrow0 += out->linesize[0];
  260. }
  261. } else {
  262. /* planar */
  263. for (plane = 0; plane < 4 && in->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 = in ->data[plane];
  267. outrow = out->data[plane];
  268. for (i = 0; i < (in->height + (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 += in ->linesize[plane];
  274. outrow += out->linesize[plane];
  275. }
  276. }
  277. }
  278. av_frame_free(&in);
  279. return ff_filter_frame(outlink, out);
  280. }
  281. static const AVFilterPad inputs[] = {
  282. { .name = "default",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .filter_frame = filter_frame,
  285. .config_props = config_props,
  286. },
  287. { .name = NULL}
  288. };
  289. static const AVFilterPad outputs[] = {
  290. { .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO, },
  292. { .name = NULL}
  293. };
  294. #define DEFINE_LUT_FILTER(name_, description_) \
  295. AVFilter avfilter_vf_##name_ = { \
  296. .name = #name_, \
  297. .description = NULL_IF_CONFIG_SMALL(description_), \
  298. .priv_size = sizeof(LutContext), \
  299. .priv_class = &name_ ## _class, \
  300. \
  301. .init = name_##_init, \
  302. .uninit = uninit, \
  303. .query_formats = query_formats, \
  304. \
  305. .inputs = inputs, \
  306. .outputs = outputs, \
  307. }
  308. #if CONFIG_LUT_FILTER
  309. #define lut_options options
  310. AVFILTER_DEFINE_CLASS(lut);
  311. static int lut_init(AVFilterContext *ctx)
  312. {
  313. return 0;
  314. }
  315. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
  316. #endif
  317. #if CONFIG_LUTYUV_FILTER
  318. #define lutyuv_options options
  319. AVFILTER_DEFINE_CLASS(lutyuv);
  320. static int lutyuv_init(AVFilterContext *ctx)
  321. {
  322. LutContext *lut = ctx->priv;
  323. lut->is_yuv = 1;
  324. return 0;
  325. }
  326. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
  327. #endif
  328. #if CONFIG_LUTRGB_FILTER
  329. #define lutrgb_options options
  330. AVFILTER_DEFINE_CLASS(lutrgb);
  331. static int lutrgb_init(AVFilterContext *ctx)
  332. {
  333. LutContext *lut = ctx->priv;
  334. lut->is_rgb = 1;
  335. return 0;
  336. }
  337. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
  338. #endif
  339. #if CONFIG_NEGATE_FILTER
  340. static const AVOption negate_options[] = {
  341. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
  342. { NULL },
  343. };
  344. AVFILTER_DEFINE_CLASS(negate);
  345. static int negate_init(AVFilterContext *ctx)
  346. {
  347. LutContext *lut = ctx->priv;
  348. int i;
  349. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
  350. for (i = 0; i < 4; i++) {
  351. lut->comp_expr_str[i] = av_strdup((i == 3 && !lut->negate_alpha) ?
  352. "val" : "negval");
  353. if (!lut->comp_expr_str[i]) {
  354. uninit(ctx);
  355. return AVERROR(ENOMEM);
  356. }
  357. }
  358. return lut_init(ctx);
  359. }
  360. DEFINE_LUT_FILTER(negate, "Negate input video.");
  361. #endif