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.

438 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, 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 *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. case AV_PIX_FMT_YUVA422P:
  173. case AV_PIX_FMT_YUVA444P:
  174. min[Y] = min[U] = min[V] = 16;
  175. max[Y] = 235;
  176. max[U] = max[V] = 240;
  177. min[A] = 0; max[A] = 255;
  178. break;
  179. default:
  180. min[0] = min[1] = min[2] = min[3] = 0;
  181. max[0] = max[1] = max[2] = max[3] = 255;
  182. }
  183. lut->is_yuv = lut->is_rgb = 0;
  184. if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
  185. else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
  186. if (lut->is_rgb) {
  187. ff_fill_rgba_map(rgba_map, inlink->format);
  188. lut->step = av_get_bits_per_pixel(desc) >> 3;
  189. }
  190. for (color = 0; color < desc->nb_components; color++) {
  191. double res;
  192. int comp = lut->is_rgb ? rgba_map[color] : color;
  193. /* create the parsed expression */
  194. ret = av_expr_parse(&lut->comp_expr[color], lut->comp_expr_str[color],
  195. var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
  196. if (ret < 0) {
  197. av_log(ctx, AV_LOG_ERROR,
  198. "Error when parsing the expression '%s' for the component %d and color %d.\n",
  199. lut->comp_expr_str[comp], comp, color);
  200. return AVERROR(EINVAL);
  201. }
  202. /* compute the lut */
  203. lut->var_values[VAR_MAXVAL] = max[color];
  204. lut->var_values[VAR_MINVAL] = min[color];
  205. for (val = 0; val < 256; val++) {
  206. lut->var_values[VAR_VAL] = val;
  207. lut->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
  208. lut->var_values[VAR_NEGVAL] =
  209. av_clip(min[color] + max[color] - lut->var_values[VAR_VAL],
  210. min[color], max[color]);
  211. res = av_expr_eval(lut->comp_expr[color], lut->var_values, lut);
  212. if (isnan(res)) {
  213. av_log(ctx, AV_LOG_ERROR,
  214. "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
  215. lut->comp_expr_str[color], val, comp);
  216. return AVERROR(EINVAL);
  217. }
  218. lut->lut[comp][val] = av_clip((int)res, min[color], max[color]);
  219. av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
  220. }
  221. }
  222. return 0;
  223. }
  224. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  225. {
  226. AVFilterContext *ctx = inlink->dst;
  227. LutContext *lut = ctx->priv;
  228. AVFilterLink *outlink = ctx->outputs[0];
  229. AVFrame *out;
  230. uint8_t *inrow, *outrow, *inrow0, *outrow0;
  231. int i, j, plane, direct = 0;
  232. if (av_frame_is_writable(in)) {
  233. direct = 1;
  234. out = in;
  235. } else {
  236. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  237. if (!out) {
  238. av_frame_free(&in);
  239. return AVERROR(ENOMEM);
  240. }
  241. av_frame_copy_props(out, in);
  242. }
  243. if (lut->is_rgb) {
  244. /* packed */
  245. inrow0 = in ->data[0];
  246. outrow0 = out->data[0];
  247. for (i = 0; i < in->height; i ++) {
  248. int w = inlink->w;
  249. const uint8_t (*tab)[256] = (const uint8_t (*)[256])lut->lut;
  250. inrow = inrow0;
  251. outrow = outrow0;
  252. for (j = 0; j < w; j++) {
  253. switch (lut->step) {
  254. case 4: outrow[3] = tab[3][inrow[3]]; // Fall-through
  255. case 3: outrow[2] = tab[2][inrow[2]]; // Fall-through
  256. case 2: outrow[1] = tab[1][inrow[1]]; // Fall-through
  257. default: outrow[0] = tab[0][inrow[0]];
  258. }
  259. outrow += lut->step;
  260. inrow += lut->step;
  261. }
  262. inrow0 += in ->linesize[0];
  263. outrow0 += out->linesize[0];
  264. }
  265. } else {
  266. /* planar */
  267. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  268. int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
  269. int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
  270. int h = FF_CEIL_RSHIFT(inlink->h, vsub);
  271. int w = FF_CEIL_RSHIFT(inlink->w, hsub);
  272. inrow = in ->data[plane];
  273. outrow = out->data[plane];
  274. for (i = 0; i < h; i++) {
  275. const uint8_t *tab = lut->lut[plane];
  276. for (j = 0; j < w; j++)
  277. outrow[j] = tab[inrow[j]];
  278. inrow += in ->linesize[plane];
  279. outrow += out->linesize[plane];
  280. }
  281. }
  282. }
  283. if (!direct)
  284. av_frame_free(&in);
  285. return ff_filter_frame(outlink, out);
  286. }
  287. static const AVFilterPad inputs[] = {
  288. { .name = "default",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .filter_frame = filter_frame,
  291. .config_props = config_props,
  292. },
  293. { .name = NULL}
  294. };
  295. static const AVFilterPad outputs[] = {
  296. { .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO, },
  298. { .name = NULL}
  299. };
  300. #define DEFINE_LUT_FILTER(name_, description_) \
  301. AVFilter avfilter_vf_##name_ = { \
  302. .name = #name_, \
  303. .description = NULL_IF_CONFIG_SMALL(description_), \
  304. .priv_size = sizeof(LutContext), \
  305. .priv_class = &name_ ## _class, \
  306. \
  307. .init = name_##_init, \
  308. .uninit = uninit, \
  309. .query_formats = query_formats, \
  310. \
  311. .inputs = inputs, \
  312. .outputs = outputs, \
  313. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, \
  314. }
  315. #if CONFIG_LUT_FILTER
  316. #define lut_options options
  317. AVFILTER_DEFINE_CLASS(lut);
  318. static int lut_init(AVFilterContext *ctx)
  319. {
  320. return 0;
  321. }
  322. DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
  323. #endif
  324. #if CONFIG_LUTYUV_FILTER
  325. #define lutyuv_options options
  326. AVFILTER_DEFINE_CLASS(lutyuv);
  327. static av_cold int lutyuv_init(AVFilterContext *ctx)
  328. {
  329. LutContext *lut = ctx->priv;
  330. lut->is_yuv = 1;
  331. return 0;
  332. }
  333. DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
  334. #endif
  335. #if CONFIG_LUTRGB_FILTER
  336. #define lutrgb_options options
  337. AVFILTER_DEFINE_CLASS(lutrgb);
  338. static av_cold int lutrgb_init(AVFilterContext *ctx)
  339. {
  340. LutContext *lut = ctx->priv;
  341. lut->is_rgb = 1;
  342. return 0;
  343. }
  344. DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
  345. #endif
  346. #if CONFIG_NEGATE_FILTER
  347. static const AVOption negate_options[] = {
  348. { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  349. { NULL },
  350. };
  351. AVFILTER_DEFINE_CLASS(negate);
  352. static av_cold int negate_init(AVFilterContext *ctx)
  353. {
  354. LutContext *lut = ctx->priv;
  355. int i;
  356. av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
  357. for (i = 0; i < 4; i++) {
  358. lut->comp_expr_str[i] = av_strdup((i == 3 && !lut->negate_alpha) ?
  359. "val" : "negval");
  360. if (!lut->comp_expr_str[i]) {
  361. uninit(ctx);
  362. return AVERROR(ENOMEM);
  363. }
  364. }
  365. return 0;
  366. }
  367. DEFINE_LUT_FILTER(negate, "Negate input video.");
  368. #endif