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.

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