vf_histeq.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. int in_histogram [256]; ///< input histogram
  55. int out_histogram[256]; ///< output histogram
  56. int LUT[256]; ///< lookup table derived from histogram[]
  57. uint8_t rgba_map[4]; ///< components position
  58. int bpp; ///< bytes per pixel
  59. } HisteqContext;
  60. #define OFFSET(x) offsetof(HisteqContext, x)
  61. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  62. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  63. static const AVOption histeq_options[] = {
  64. { "strength", "set the strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 1, FLAGS },
  65. { "intensity", "set the intensity", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0.21}, 0, 1, FLAGS },
  66. { "antibanding", "set the antibanding level", OFFSET(antibanding), AV_OPT_TYPE_INT, {.i64=HISTEQ_ANTIBANDING_NONE}, 0, HISTEQ_ANTIBANDING_NB-1, FLAGS, "antibanding" },
  67. CONST("none", "apply no antibanding", HISTEQ_ANTIBANDING_NONE, "antibanding"),
  68. CONST("weak", "apply weak antibanding", HISTEQ_ANTIBANDING_WEAK, "antibanding"),
  69. CONST("strong", "apply strong antibanding", HISTEQ_ANTIBANDING_STRONG, "antibanding"),
  70. { NULL }
  71. };
  72. AVFILTER_DEFINE_CLASS(histeq);
  73. static av_cold int init(AVFilterContext *ctx)
  74. {
  75. HisteqContext *histeq = ctx->priv;
  76. av_log(ctx, AV_LOG_VERBOSE,
  77. "strength:%0.3f intensity:%0.3f antibanding:%d\n",
  78. histeq->strength, histeq->intensity, histeq->antibanding);
  79. return 0;
  80. }
  81. static int query_formats(AVFilterContext *ctx)
  82. {
  83. static const enum PixelFormat pix_fmts[] = {
  84. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  85. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  86. AV_PIX_FMT_NONE
  87. };
  88. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  89. return 0;
  90. }
  91. static int config_input(AVFilterLink *inlink)
  92. {
  93. AVFilterContext *ctx = inlink->dst;
  94. HisteqContext *histeq = ctx->priv;
  95. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  96. histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
  97. ff_fill_rgba_map(histeq->rgba_map, inlink->format);
  98. return 0;
  99. }
  100. #define R 0
  101. #define G 1
  102. #define B 2
  103. #define A 3
  104. #define GET_RGB_VALUES(r, g, b, src, map) do { \
  105. r = src[x + map[R]]; \
  106. g = src[x + map[G]]; \
  107. b = src[x + map[B]]; \
  108. } while (0)
  109. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  110. {
  111. AVFilterContext *ctx = inlink->dst;
  112. HisteqContext *histeq = ctx->priv;
  113. AVFilterLink *outlink = ctx->outputs[0];
  114. int strength = histeq->strength * 1000;
  115. int intensity = histeq->intensity * 1000;
  116. int x, y, i, luthi, lutlo, lut, luma, oluma, m;
  117. AVFrame *outpic;
  118. unsigned int r, g, b, jran;
  119. uint8_t *src, *dst;
  120. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  121. if (!outpic) {
  122. av_frame_free(&inpic);
  123. return AVERROR(ENOMEM);
  124. }
  125. av_frame_copy_props(outpic, inpic);
  126. /* Seed random generator for antibanding. */
  127. jran = LCG_SEED;
  128. /* Calculate and store the luminance and calculate the global histogram
  129. based on the luminance. */
  130. memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
  131. src = inpic->data[0];
  132. dst = outpic->data[0];
  133. for (y = 0; y < inlink->h; y++) {
  134. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  135. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  136. luma = (55 * r + 182 * g + 19 * b) >> 8;
  137. dst[x + histeq->rgba_map[A]] = luma;
  138. histeq->in_histogram[luma]++;
  139. }
  140. src += inpic->linesize[0];
  141. dst += outpic->linesize[0];
  142. }
  143. #ifdef DEBUG
  144. for (x = 0; x < 256; x++)
  145. av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
  146. #endif
  147. /* Calculate the lookup table. */
  148. histeq->LUT[0] = histeq->in_histogram[0];
  149. /* Accumulate */
  150. for (x = 1; x < 256; x++)
  151. histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
  152. /* Normalize */
  153. for (x = 0; x < 256; x++)
  154. histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
  155. /* Adjust the LUT based on the selected strength. This is an alpha
  156. mix of the calculated LUT and a linear LUT with gain 1. */
  157. for (x = 0; x < 256; x++)
  158. histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
  159. ((255 - strength) * x) / 255;
  160. /* Output the equalized frame. */
  161. memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
  162. src = inpic->data[0];
  163. dst = outpic->data[0];
  164. for (y = 0; y < inlink->h; y++) {
  165. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  166. luma = dst[x + histeq->rgba_map[A]];
  167. if (luma == 0) {
  168. for (i = 0; i < histeq->bpp; ++i)
  169. dst[x + i] = 0;
  170. histeq->out_histogram[0]++;
  171. } else {
  172. lut = histeq->LUT[luma];
  173. if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
  174. if (luma > 0) {
  175. lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
  176. (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
  177. histeq->LUT[luma - 1];
  178. } else
  179. lutlo = lut;
  180. if (luma < 255) {
  181. luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
  182. (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
  183. histeq->LUT[luma + 1];
  184. } else
  185. luthi = lut;
  186. if (lutlo != luthi) {
  187. jran = LCG(jran);
  188. lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
  189. }
  190. }
  191. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  192. if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
  193. r = (r * 255) / m;
  194. g = (g * 255) / m;
  195. b = (b * 255) / m;
  196. } else {
  197. r = (r * lut) / luma;
  198. g = (g * lut) / luma;
  199. b = (b * lut) / luma;
  200. }
  201. dst[x + histeq->rgba_map[R]] = r;
  202. dst[x + histeq->rgba_map[G]] = g;
  203. dst[x + histeq->rgba_map[B]] = b;
  204. oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
  205. histeq->out_histogram[oluma]++;
  206. }
  207. }
  208. src += inpic->linesize[0];
  209. dst += outpic->linesize[0];
  210. }
  211. #ifdef DEBUG
  212. for (x = 0; x < 256; x++)
  213. av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
  214. #endif
  215. av_frame_free(&inpic);
  216. return ff_filter_frame(outlink, outpic);
  217. }
  218. static const AVFilterPad histeq_inputs[] = {
  219. {
  220. .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .config_props = config_input,
  223. .filter_frame = filter_frame,
  224. },
  225. { NULL }
  226. };
  227. static const AVFilterPad histeq_outputs[] = {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. },
  232. { NULL }
  233. };
  234. AVFilter ff_vf_histeq = {
  235. .name = "histeq",
  236. .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
  237. .priv_size = sizeof(HisteqContext),
  238. .init = init,
  239. .query_formats = query_formats,
  240. .inputs = histeq_inputs,
  241. .outputs = histeq_outputs,
  242. .priv_class = &histeq_class,
  243. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  244. };