FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_histeq.c
Go to the documentation of this file.
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 /**
23  * @file
24  * Histogram equalization filter, based on the VirtualDub filter by
25  * Donald A. Graft <neuron2 AT home DOT com>.
26  * Implements global automatic contrast adjustment by means of
27  * histogram equalization.
28  */
29 
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 
34 #include "avfilter.h"
35 #include "drawutils.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39 
40 // #define DEBUG
41 
42 // Linear Congruential Generator, see "Numerical Recipes"
43 #define LCG_A 4096
44 #define LCG_C 150889
45 #define LCG_M 714025
46 #define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M)
47 #define LCG_SEED 739187
48 
54 };
55 
56 typedef struct {
57  const AVClass *class;
58  float strength;
59  float intensity;
62  int in_histogram [256]; ///< input histogram
63  int out_histogram[256]; ///< output histogram
64  int LUT[256]; ///< lookup table derived from histogram[]
65  uint8_t rgba_map[4]; ///< components position
66  int bpp; ///< bytes per pixel
68 
69 #define OFFSET(x) offsetof(HisteqContext, x)
70 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
71 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
72 
73 static const AVOption histeq_options[] = {
74  { "strength", "set the strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 1, FLAGS },
75  { "intensity", "set the intensity", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0.21}, 0, 1, FLAGS },
76  { "antibanding", "set the antibanding level", OFFSET(antibanding), AV_OPT_TYPE_INT, {.i64=HISTEQ_ANTIBANDING_NONE}, 0, HISTEQ_ANTIBANDING_NB-1, FLAGS, "antibanding" },
77  CONST("none", "apply no antibanding", HISTEQ_ANTIBANDING_NONE, "antibanding"),
78  CONST("weak", "apply weak antibanding", HISTEQ_ANTIBANDING_WEAK, "antibanding"),
79  CONST("strong", "apply strong antibanding", HISTEQ_ANTIBANDING_STRONG, "antibanding"),
80  { NULL }
81 };
82 
83 AVFILTER_DEFINE_CLASS(histeq);
84 
85 static av_cold int init(AVFilterContext *ctx, const char *args)
86 {
87  HisteqContext *histeq = ctx->priv;
88  const char *shorthand[] = { "strength", "intensity", "antibanding", NULL };
89  int ret;
90 
91  histeq->class = &histeq_class;
92  av_opt_set_defaults(histeq);
93 
94  if ((ret = av_opt_set_from_string(histeq, args, shorthand, "=", ":")) < 0)
95  return ret;
96 
98  "strength:%0.3f intensity:%0.3f antibanding:%d\n",
99  histeq->strength, histeq->intensity, histeq->antibanding);
100 
101  return 0;
102 }
103 
104 static av_cold void uninit(AVFilterContext *ctx)
105 {
106  HisteqContext *histeq = ctx->priv;
107  av_opt_free(histeq);
108 }
109 
111 {
112  static const enum PixelFormat pix_fmts[] = {
116  };
117 
119  return 0;
120 }
121 
122 static int config_input(AVFilterLink *inlink)
123 {
124  AVFilterContext *ctx = inlink->dst;
125  HisteqContext *histeq = ctx->priv;
126  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
127 
128  histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
129  ff_fill_rgba_map(histeq->rgba_map, inlink->format);
130 
131  return 0;
132 }
133 
134 #define R 0
135 #define G 1
136 #define B 2
137 #define A 3
138 
139 #define GET_RGB_VALUES(r, g, b, src, map) do { \
140  r = src[x + map[R]]; \
141  g = src[x + map[G]]; \
142  b = src[x + map[B]]; \
143 } while (0)
144 
145 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
146 {
147  AVFilterContext *ctx = inlink->dst;
148  HisteqContext *histeq = ctx->priv;
149  AVFilterLink *outlink = ctx->outputs[0];
150  int strength = histeq->strength * 1000;
151  int intensity = histeq->intensity * 1000;
152  int x, y, i, luthi, lutlo, lut, luma, oluma, m;
153  AVFilterBufferRef *outpic;
154  unsigned int r, g, b, jran;
155  uint8_t *src, *dst;
156 
157  outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
158  if (!outpic) {
159  avfilter_unref_bufferp(&inpic);
160  return AVERROR(ENOMEM);
161  }
162  avfilter_copy_buffer_ref_props(outpic, inpic);
163 
164  /* Seed random generator for antibanding. */
165  jran = LCG_SEED;
166 
167  /* Calculate and store the luminance and calculate the global histogram
168  based on the luminance. */
169  memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
170  src = inpic->data[0];
171  dst = outpic->data[0];
172  for (y = 0; y < inlink->h; y++) {
173  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
174  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
175  luma = (55 * r + 182 * g + 19 * b) >> 8;
176  dst[x + histeq->rgba_map[A]] = luma;
177  histeq->in_histogram[luma]++;
178  }
179  src += inpic->linesize[0];
180  dst += outpic->linesize[0];
181  }
182 
183 #ifdef DEBUG
184  for (x = 0; x < 256; x++)
185  av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
186 #endif
187 
188  /* Calculate the lookup table. */
189  histeq->LUT[0] = histeq->in_histogram[0];
190  /* Accumulate */
191  for (x = 1; x < 256; x++)
192  histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
193 
194  /* Normalize */
195  for (x = 0; x < 256; x++)
196  histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
197 
198  /* Adjust the LUT based on the selected strength. This is an alpha
199  mix of the calculated LUT and a linear LUT with gain 1. */
200  for (x = 0; x < 256; x++)
201  histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
202  ((255 - strength) * x) / 255;
203 
204  /* Output the equalized frame. */
205  memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
206 
207  src = inpic->data[0];
208  dst = outpic->data[0];
209  for (y = 0; y < inlink->h; y++) {
210  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
211  luma = dst[x + histeq->rgba_map[A]];
212  if (luma == 0) {
213  for (i = 0; i < histeq->bpp; ++i)
214  dst[x + i] = 0;
215  histeq->out_histogram[0]++;
216  } else {
217  lut = histeq->LUT[luma];
218  if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
219  if (luma > 0) {
220  lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
221  (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
222  histeq->LUT[luma - 1];
223  } else
224  lutlo = lut;
225 
226  if (luma < 255) {
227  luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
228  (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
229  histeq->LUT[luma + 1];
230  } else
231  luthi = lut;
232 
233  if (lutlo != luthi) {
234  jran = LCG(jran);
235  lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
236  }
237  }
238 
239  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
240  if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
241  r = (r * 255) / m;
242  g = (g * 255) / m;
243  b = (b * 255) / m;
244  } else {
245  r = (r * lut) / luma;
246  g = (g * lut) / luma;
247  b = (b * lut) / luma;
248  }
249  dst[x + histeq->rgba_map[R]] = r;
250  dst[x + histeq->rgba_map[G]] = g;
251  dst[x + histeq->rgba_map[B]] = b;
252  oluma = (55 * r + 182 * g + 19 * b) >> 8;
253  histeq->out_histogram[oluma]++;
254  }
255  }
256  src += inpic->linesize[0];
257  dst += outpic->linesize[0];
258  }
259 #ifdef DEBUG
260  for (x = 0; x < 256; x++)
261  av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
262 #endif
263 
264  avfilter_unref_bufferp(&inpic);
265  return ff_filter_frame(outlink, outpic);
266 }
267 
268 static const AVFilterPad histeq_inputs[] = {
269  {
270  .name = "default",
271  .type = AVMEDIA_TYPE_VIDEO,
272  .config_props = config_input,
273  .filter_frame = filter_frame,
274  .min_perms = AV_PERM_READ,
275  },
276  { NULL }
277 };
278 
279 static const AVFilterPad histeq_outputs[] = {
280  {
281  .name = "default",
282  .type = AVMEDIA_TYPE_VIDEO,
283  },
284  { NULL }
285 };
286 
288  .name = "histeq",
289  .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
290  .priv_size = sizeof(HisteqContext),
291  .init = init,
292  .uninit = uninit,
294 
295  .inputs = histeq_inputs,
296  .outputs = histeq_outputs,
297  .priv_class = &histeq_class,
298 };