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.

284 lines
9.4KB

  1. /*
  2. * Copyright (c) 2012 Jeremy Tran
  3. * Copyright (c) 2001 Donald A. Graft
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * Histogram equalization filter, based on the VirtualDub filter by
  24. * Donald A. Graft <neuron2 AT home DOT com>.
  25. * Implements global automatic contrast adjustment by means of
  26. * histogram equalization.
  27. */
  28. #include "libavutil/common.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "drawutils.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. // #define DEBUG
  37. // Linear Congruential Generator, see "Numerical Recipes"
  38. #define LCG_A 4096
  39. #define LCG_C 150889
  40. #define LCG_M 714025
  41. #define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M)
  42. #define LCG_SEED 739187
  43. enum HisteqAntibanding {
  44. HISTEQ_ANTIBANDING_NONE = 0,
  45. HISTEQ_ANTIBANDING_WEAK = 1,
  46. HISTEQ_ANTIBANDING_STRONG = 2,
  47. HISTEQ_ANTIBANDING_NB,
  48. };
  49. typedef struct {
  50. const AVClass *class;
  51. float strength;
  52. float intensity;
  53. enum HisteqAntibanding antibanding;
  54. char* antibanding_str;
  55. int in_histogram [256]; ///< input histogram
  56. int out_histogram[256]; ///< output histogram
  57. int LUT[256]; ///< lookup table derived from histogram[]
  58. uint8_t rgba_map[4]; ///< components position
  59. int bpp; ///< bytes per pixel
  60. } HisteqContext;
  61. #define OFFSET(x) offsetof(HisteqContext, x)
  62. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  63. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  64. static const AVOption histeq_options[] = {
  65. { "strength", "set the strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 1, FLAGS },
  66. { "intensity", "set the intensity", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0.21}, 0, 1, FLAGS },
  67. { "antibanding", "set the antibanding level", OFFSET(antibanding), AV_OPT_TYPE_INT, {.i64=HISTEQ_ANTIBANDING_NONE}, 0, HISTEQ_ANTIBANDING_NB-1, FLAGS, "antibanding" },
  68. CONST("none", "apply no antibanding", HISTEQ_ANTIBANDING_NONE, "antibanding"),
  69. CONST("weak", "apply weak antibanding", HISTEQ_ANTIBANDING_WEAK, "antibanding"),
  70. CONST("strong", "apply strong antibanding", HISTEQ_ANTIBANDING_STRONG, "antibanding"),
  71. { NULL }
  72. };
  73. AVFILTER_DEFINE_CLASS(histeq);
  74. static av_cold int init(AVFilterContext *ctx)
  75. {
  76. HisteqContext *histeq = ctx->priv;
  77. av_log(ctx, AV_LOG_VERBOSE,
  78. "strength:%0.3f intensity:%0.3f antibanding:%d\n",
  79. histeq->strength, histeq->intensity, histeq->antibanding);
  80. return 0;
  81. }
  82. static int query_formats(AVFilterContext *ctx)
  83. {
  84. static const enum PixelFormat pix_fmts[] = {
  85. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  86. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  87. AV_PIX_FMT_NONE
  88. };
  89. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  90. return 0;
  91. }
  92. static int config_input(AVFilterLink *inlink)
  93. {
  94. AVFilterContext *ctx = inlink->dst;
  95. HisteqContext *histeq = ctx->priv;
  96. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  97. histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
  98. ff_fill_rgba_map(histeq->rgba_map, inlink->format);
  99. return 0;
  100. }
  101. #define R 0
  102. #define G 1
  103. #define B 2
  104. #define A 3
  105. #define GET_RGB_VALUES(r, g, b, src, map) do { \
  106. r = src[x + map[R]]; \
  107. g = src[x + map[G]]; \
  108. b = src[x + map[B]]; \
  109. } while (0)
  110. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  111. {
  112. AVFilterContext *ctx = inlink->dst;
  113. HisteqContext *histeq = ctx->priv;
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. int strength = histeq->strength * 1000;
  116. int intensity = histeq->intensity * 1000;
  117. int x, y, i, luthi, lutlo, lut, luma, oluma, m;
  118. AVFrame *outpic;
  119. unsigned int r, g, b, jran;
  120. uint8_t *src, *dst;
  121. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  122. if (!outpic) {
  123. av_frame_free(&inpic);
  124. return AVERROR(ENOMEM);
  125. }
  126. av_frame_copy_props(outpic, inpic);
  127. /* Seed random generator for antibanding. */
  128. jran = LCG_SEED;
  129. /* Calculate and store the luminance and calculate the global histogram
  130. based on the luminance. */
  131. memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
  132. src = inpic->data[0];
  133. dst = outpic->data[0];
  134. for (y = 0; y < inlink->h; y++) {
  135. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  136. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  137. luma = (55 * r + 182 * g + 19 * b) >> 8;
  138. dst[x + histeq->rgba_map[A]] = luma;
  139. histeq->in_histogram[luma]++;
  140. }
  141. src += inpic->linesize[0];
  142. dst += outpic->linesize[0];
  143. }
  144. #ifdef DEBUG
  145. for (x = 0; x < 256; x++)
  146. av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
  147. #endif
  148. /* Calculate the lookup table. */
  149. histeq->LUT[0] = histeq->in_histogram[0];
  150. /* Accumulate */
  151. for (x = 1; x < 256; x++)
  152. histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
  153. /* Normalize */
  154. for (x = 0; x < 256; x++)
  155. histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
  156. /* Adjust the LUT based on the selected strength. This is an alpha
  157. mix of the calculated LUT and a linear LUT with gain 1. */
  158. for (x = 0; x < 256; x++)
  159. histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
  160. ((255 - strength) * x) / 255;
  161. /* Output the equalized frame. */
  162. memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
  163. src = inpic->data[0];
  164. dst = outpic->data[0];
  165. for (y = 0; y < inlink->h; y++) {
  166. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  167. luma = dst[x + histeq->rgba_map[A]];
  168. if (luma == 0) {
  169. for (i = 0; i < histeq->bpp; ++i)
  170. dst[x + i] = 0;
  171. histeq->out_histogram[0]++;
  172. } else {
  173. lut = histeq->LUT[luma];
  174. if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
  175. if (luma > 0) {
  176. lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
  177. (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
  178. histeq->LUT[luma - 1];
  179. } else
  180. lutlo = lut;
  181. if (luma < 255) {
  182. luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
  183. (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
  184. histeq->LUT[luma + 1];
  185. } else
  186. luthi = lut;
  187. if (lutlo != luthi) {
  188. jran = LCG(jran);
  189. lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
  190. }
  191. }
  192. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  193. if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
  194. r = (r * 255) / m;
  195. g = (g * 255) / m;
  196. b = (b * 255) / m;
  197. } else {
  198. r = (r * lut) / luma;
  199. g = (g * lut) / luma;
  200. b = (b * lut) / luma;
  201. }
  202. dst[x + histeq->rgba_map[R]] = r;
  203. dst[x + histeq->rgba_map[G]] = g;
  204. dst[x + histeq->rgba_map[B]] = b;
  205. oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
  206. histeq->out_histogram[oluma]++;
  207. }
  208. }
  209. src += inpic->linesize[0];
  210. dst += outpic->linesize[0];
  211. }
  212. #ifdef DEBUG
  213. for (x = 0; x < 256; x++)
  214. av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
  215. #endif
  216. av_frame_free(&inpic);
  217. return ff_filter_frame(outlink, outpic);
  218. }
  219. static const AVFilterPad histeq_inputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .config_props = config_input,
  224. .filter_frame = filter_frame,
  225. },
  226. { NULL }
  227. };
  228. static const AVFilterPad histeq_outputs[] = {
  229. {
  230. .name = "default",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. },
  233. { NULL }
  234. };
  235. AVFilter avfilter_vf_histeq = {
  236. .name = "histeq",
  237. .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
  238. .priv_size = sizeof(HisteqContext),
  239. .init = init,
  240. .query_formats = query_formats,
  241. .inputs = histeq_inputs,
  242. .outputs = histeq_outputs,
  243. .priv_class = &histeq_class,
  244. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  245. };