vf_histeq.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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, const char *args)
  75. {
  76. HisteqContext *histeq = ctx->priv;
  77. const char *shorthand[] = { "strength", "intensity", "antibanding", NULL };
  78. int ret;
  79. histeq->class = &histeq_class;
  80. av_opt_set_defaults(histeq);
  81. if ((ret = av_opt_set_from_string(histeq, args, shorthand, "=", ":")) < 0)
  82. return ret;
  83. av_log(ctx, AV_LOG_VERBOSE,
  84. "strength:%0.3f intensity:%0.3f antibanding:%d\n",
  85. histeq->strength, histeq->intensity, histeq->antibanding);
  86. return 0;
  87. }
  88. static av_cold void uninit(AVFilterContext *ctx)
  89. {
  90. HisteqContext *histeq = ctx->priv;
  91. av_opt_free(histeq);
  92. }
  93. static int query_formats(AVFilterContext *ctx)
  94. {
  95. static const enum PixelFormat pix_fmts[] = {
  96. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  97. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  98. AV_PIX_FMT_NONE
  99. };
  100. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  101. return 0;
  102. }
  103. static int config_input(AVFilterLink *inlink)
  104. {
  105. AVFilterContext *ctx = inlink->dst;
  106. HisteqContext *histeq = ctx->priv;
  107. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  108. histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
  109. ff_fill_rgba_map(histeq->rgba_map, inlink->format);
  110. return 0;
  111. }
  112. #define R 0
  113. #define G 1
  114. #define B 2
  115. #define A 3
  116. #define GET_RGB_VALUES(r, g, b, src, map) do { \
  117. r = src[x + map[R]]; \
  118. g = src[x + map[G]]; \
  119. b = src[x + map[B]]; \
  120. } while (0)
  121. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
  122. {
  123. AVFilterContext *ctx = inlink->dst;
  124. HisteqContext *histeq = ctx->priv;
  125. AVFilterLink *outlink = ctx->outputs[0];
  126. int strength = histeq->strength * 1000;
  127. int intensity = histeq->intensity * 1000;
  128. int x, y, i, luthi, lutlo, lut, luma, oluma, m;
  129. AVFilterBufferRef *outpic;
  130. unsigned int r, g, b, jran;
  131. uint8_t *src, *dst;
  132. outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
  133. if (!outpic) {
  134. avfilter_unref_bufferp(&inpic);
  135. return AVERROR(ENOMEM);
  136. }
  137. avfilter_copy_buffer_ref_props(outpic, inpic);
  138. /* Seed random generator for antibanding. */
  139. jran = LCG_SEED;
  140. /* Calculate and store the luminance and calculate the global histogram
  141. based on the luminance. */
  142. memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
  143. src = inpic->data[0];
  144. dst = outpic->data[0];
  145. for (y = 0; y < inlink->h; y++) {
  146. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  147. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  148. luma = (55 * r + 182 * g + 19 * b) >> 8;
  149. dst[x + histeq->rgba_map[A]] = luma;
  150. histeq->in_histogram[luma]++;
  151. }
  152. src += inpic->linesize[0];
  153. dst += outpic->linesize[0];
  154. }
  155. #ifdef DEBUG
  156. for (x = 0; x < 256; x++)
  157. av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
  158. #endif
  159. /* Calculate the lookup table. */
  160. histeq->LUT[0] = histeq->in_histogram[0];
  161. /* Accumulate */
  162. for (x = 1; x < 256; x++)
  163. histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
  164. /* Normalize */
  165. for (x = 0; x < 256; x++)
  166. histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
  167. /* Adjust the LUT based on the selected strength. This is an alpha
  168. mix of the calculated LUT and a linear LUT with gain 1. */
  169. for (x = 0; x < 256; x++)
  170. histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
  171. ((255 - strength) * x) / 255;
  172. /* Output the equalized frame. */
  173. memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
  174. src = inpic->data[0];
  175. dst = outpic->data[0];
  176. for (y = 0; y < inlink->h; y++) {
  177. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  178. luma = dst[x + histeq->rgba_map[A]];
  179. if (luma == 0) {
  180. for (i = 0; i < histeq->bpp; ++i)
  181. dst[x + i] = 0;
  182. histeq->out_histogram[0]++;
  183. } else {
  184. lut = histeq->LUT[luma];
  185. if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
  186. if (luma > 0) {
  187. lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
  188. (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
  189. histeq->LUT[luma - 1];
  190. } else
  191. lutlo = lut;
  192. if (luma < 255) {
  193. luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
  194. (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
  195. histeq->LUT[luma + 1];
  196. } else
  197. luthi = lut;
  198. if (lutlo != luthi) {
  199. jran = LCG(jran);
  200. lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
  201. }
  202. }
  203. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  204. if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
  205. r = (r * 255) / m;
  206. g = (g * 255) / m;
  207. b = (b * 255) / m;
  208. } else {
  209. r = (r * lut) / luma;
  210. g = (g * lut) / luma;
  211. b = (b * lut) / luma;
  212. }
  213. dst[x + histeq->rgba_map[R]] = r;
  214. dst[x + histeq->rgba_map[G]] = g;
  215. dst[x + histeq->rgba_map[B]] = b;
  216. oluma = (55 * r + 182 * g + 19 * b) >> 8;
  217. histeq->out_histogram[oluma]++;
  218. }
  219. }
  220. src += inpic->linesize[0];
  221. dst += outpic->linesize[0];
  222. }
  223. #ifdef DEBUG
  224. for (x = 0; x < 256; x++)
  225. av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
  226. #endif
  227. avfilter_unref_bufferp(&inpic);
  228. return ff_filter_frame(outlink, outpic);
  229. }
  230. static const AVFilterPad histeq_inputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. .config_props = config_input,
  235. .filter_frame = filter_frame,
  236. .min_perms = AV_PERM_READ,
  237. },
  238. { NULL }
  239. };
  240. static const AVFilterPad histeq_outputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_VIDEO,
  244. },
  245. { NULL }
  246. };
  247. AVFilter avfilter_vf_histeq = {
  248. .name = "histeq",
  249. .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
  250. .priv_size = sizeof(HisteqContext),
  251. .init = init,
  252. .uninit = uninit,
  253. .query_formats = query_formats,
  254. .inputs = histeq_inputs,
  255. .outputs = histeq_outputs,
  256. .priv_class = &histeq_class,
  257. };