vf_histeq.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/internal.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "drawutils.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. // #define DEBUG
  38. // Linear Congruential Generator, see "Numerical Recipes"
  39. #define LCG_A 4096
  40. #define LCG_C 150889
  41. #define LCG_M 714025
  42. #define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M)
  43. #define LCG_SEED 739187
  44. enum HisteqAntibanding {
  45. HISTEQ_ANTIBANDING_NONE = 0,
  46. HISTEQ_ANTIBANDING_WEAK = 1,
  47. HISTEQ_ANTIBANDING_STRONG = 2,
  48. HISTEQ_ANTIBANDING_NB,
  49. };
  50. typedef struct HisteqContext {
  51. const AVClass *class;
  52. float strength;
  53. float intensity;
  54. int antibanding; ///< HisteqAntibanding
  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 AVPixelFormat 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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  90. if (!fmts_list)
  91. return AVERROR(ENOMEM);
  92. return ff_set_common_formats(ctx, fmts_list);
  93. }
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. HisteqContext *histeq = ctx->priv;
  98. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  99. histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
  100. ff_fill_rgba_map(histeq->rgba_map, inlink->format);
  101. return 0;
  102. }
  103. #define R 0
  104. #define G 1
  105. #define B 2
  106. #define A 3
  107. #define GET_RGB_VALUES(r, g, b, src, map) do { \
  108. r = src[x + map[R]]; \
  109. g = src[x + map[G]]; \
  110. b = src[x + map[B]]; \
  111. } while (0)
  112. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  113. {
  114. AVFilterContext *ctx = inlink->dst;
  115. HisteqContext *histeq = ctx->priv;
  116. AVFilterLink *outlink = ctx->outputs[0];
  117. int strength = histeq->strength * 1000;
  118. int intensity = histeq->intensity * 1000;
  119. int x, y, i, luthi, lutlo, lut, luma, oluma, m;
  120. AVFrame *outpic;
  121. unsigned int r, g, b, jran;
  122. uint8_t *src, *dst;
  123. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  124. if (!outpic) {
  125. av_frame_free(&inpic);
  126. return AVERROR(ENOMEM);
  127. }
  128. av_frame_copy_props(outpic, inpic);
  129. /* Seed random generator for antibanding. */
  130. jran = LCG_SEED;
  131. /* Calculate and store the luminance and calculate the global histogram
  132. based on the luminance. */
  133. memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
  134. src = inpic->data[0];
  135. dst = outpic->data[0];
  136. for (y = 0; y < inlink->h; y++) {
  137. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  138. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  139. luma = (55 * r + 182 * g + 19 * b) >> 8;
  140. dst[x + histeq->rgba_map[A]] = luma;
  141. histeq->in_histogram[luma]++;
  142. }
  143. src += inpic->linesize[0];
  144. dst += outpic->linesize[0];
  145. }
  146. #ifdef DEBUG
  147. for (x = 0; x < 256; x++)
  148. ff_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
  149. #endif
  150. /* Calculate the lookup table. */
  151. histeq->LUT[0] = histeq->in_histogram[0];
  152. /* Accumulate */
  153. for (x = 1; x < 256; x++)
  154. histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
  155. /* Normalize */
  156. for (x = 0; x < 256; x++)
  157. histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
  158. /* Adjust the LUT based on the selected strength. This is an alpha
  159. mix of the calculated LUT and a linear LUT with gain 1. */
  160. for (x = 0; x < 256; x++)
  161. histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
  162. ((255 - strength) * x) / 255;
  163. /* Output the equalized frame. */
  164. memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
  165. src = inpic->data[0];
  166. dst = outpic->data[0];
  167. for (y = 0; y < inlink->h; y++) {
  168. for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
  169. luma = dst[x + histeq->rgba_map[A]];
  170. if (luma == 0) {
  171. for (i = 0; i < histeq->bpp; ++i)
  172. dst[x + i] = 0;
  173. histeq->out_histogram[0]++;
  174. } else {
  175. lut = histeq->LUT[luma];
  176. if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
  177. if (luma > 0) {
  178. lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
  179. (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
  180. histeq->LUT[luma - 1];
  181. } else
  182. lutlo = lut;
  183. if (luma < 255) {
  184. luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
  185. (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
  186. histeq->LUT[luma + 1];
  187. } else
  188. luthi = lut;
  189. if (lutlo != luthi) {
  190. jran = LCG(jran);
  191. lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
  192. }
  193. }
  194. GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
  195. if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
  196. r = (r * 255) / m;
  197. g = (g * 255) / m;
  198. b = (b * 255) / m;
  199. } else {
  200. r = (r * lut) / luma;
  201. g = (g * lut) / luma;
  202. b = (b * lut) / luma;
  203. }
  204. dst[x + histeq->rgba_map[R]] = r;
  205. dst[x + histeq->rgba_map[G]] = g;
  206. dst[x + histeq->rgba_map[B]] = b;
  207. oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
  208. histeq->out_histogram[oluma]++;
  209. }
  210. }
  211. src += inpic->linesize[0];
  212. dst += outpic->linesize[0];
  213. }
  214. #ifdef DEBUG
  215. for (x = 0; x < 256; x++)
  216. ff_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
  217. #endif
  218. av_frame_free(&inpic);
  219. return ff_filter_frame(outlink, outpic);
  220. }
  221. static const AVFilterPad histeq_inputs[] = {
  222. {
  223. .name = "default",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .config_props = config_input,
  226. .filter_frame = filter_frame,
  227. },
  228. { NULL }
  229. };
  230. static const AVFilterPad histeq_outputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. },
  235. { NULL }
  236. };
  237. AVFilter ff_vf_histeq = {
  238. .name = "histeq",
  239. .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
  240. .priv_size = sizeof(HisteqContext),
  241. .init = init,
  242. .query_formats = query_formats,
  243. .inputs = histeq_inputs,
  244. .outputs = histeq_outputs,
  245. .priv_class = &histeq_class,
  246. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  247. };