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.

465 lines
15KB

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