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;
60  enum HisteqAntibanding antibanding;
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)
86 {
87  HisteqContext *histeq = ctx->priv;
88 
90  "strength:%0.3f intensity:%0.3f antibanding:%d\n",
91  histeq->strength, histeq->intensity, histeq->antibanding);
92 
93  return 0;
94 }
95 
97 {
98  static const enum PixelFormat pix_fmts[] = {
102  };
103 
105  return 0;
106 }
107 
108 static int config_input(AVFilterLink *inlink)
109 {
110  AVFilterContext *ctx = inlink->dst;
111  HisteqContext *histeq = ctx->priv;
112  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
113 
114  histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
115  ff_fill_rgba_map(histeq->rgba_map, inlink->format);
116 
117  return 0;
118 }
119 
120 #define R 0
121 #define G 1
122 #define B 2
123 #define A 3
124 
125 #define GET_RGB_VALUES(r, g, b, src, map) do { \
126  r = src[x + map[R]]; \
127  g = src[x + map[G]]; \
128  b = src[x + map[B]]; \
129 } while (0)
130 
131 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
132 {
133  AVFilterContext *ctx = inlink->dst;
134  HisteqContext *histeq = ctx->priv;
135  AVFilterLink *outlink = ctx->outputs[0];
136  int strength = histeq->strength * 1000;
137  int intensity = histeq->intensity * 1000;
138  int x, y, i, luthi, lutlo, lut, luma, oluma, m;
139  AVFrame *outpic;
140  unsigned int r, g, b, jran;
141  uint8_t *src, *dst;
142 
143  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
144  if (!outpic) {
145  av_frame_free(&inpic);
146  return AVERROR(ENOMEM);
147  }
148  av_frame_copy_props(outpic, inpic);
149 
150  /* Seed random generator for antibanding. */
151  jran = LCG_SEED;
152 
153  /* Calculate and store the luminance and calculate the global histogram
154  based on the luminance. */
155  memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
156  src = inpic->data[0];
157  dst = outpic->data[0];
158  for (y = 0; y < inlink->h; y++) {
159  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
160  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
161  luma = (55 * r + 182 * g + 19 * b) >> 8;
162  dst[x + histeq->rgba_map[A]] = luma;
163  histeq->in_histogram[luma]++;
164  }
165  src += inpic->linesize[0];
166  dst += outpic->linesize[0];
167  }
168 
169 #ifdef DEBUG
170  for (x = 0; x < 256; x++)
171  av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
172 #endif
173 
174  /* Calculate the lookup table. */
175  histeq->LUT[0] = histeq->in_histogram[0];
176  /* Accumulate */
177  for (x = 1; x < 256; x++)
178  histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
179 
180  /* Normalize */
181  for (x = 0; x < 256; x++)
182  histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
183 
184  /* Adjust the LUT based on the selected strength. This is an alpha
185  mix of the calculated LUT and a linear LUT with gain 1. */
186  for (x = 0; x < 256; x++)
187  histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
188  ((255 - strength) * x) / 255;
189 
190  /* Output the equalized frame. */
191  memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
192 
193  src = inpic->data[0];
194  dst = outpic->data[0];
195  for (y = 0; y < inlink->h; y++) {
196  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
197  luma = dst[x + histeq->rgba_map[A]];
198  if (luma == 0) {
199  for (i = 0; i < histeq->bpp; ++i)
200  dst[x + i] = 0;
201  histeq->out_histogram[0]++;
202  } else {
203  lut = histeq->LUT[luma];
204  if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
205  if (luma > 0) {
206  lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
207  (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
208  histeq->LUT[luma - 1];
209  } else
210  lutlo = lut;
211 
212  if (luma < 255) {
213  luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
214  (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
215  histeq->LUT[luma + 1];
216  } else
217  luthi = lut;
218 
219  if (lutlo != luthi) {
220  jran = LCG(jran);
221  lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
222  }
223  }
224 
225  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
226  if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
227  r = (r * 255) / m;
228  g = (g * 255) / m;
229  b = (b * 255) / m;
230  } else {
231  r = (r * lut) / luma;
232  g = (g * lut) / luma;
233  b = (b * lut) / luma;
234  }
235  dst[x + histeq->rgba_map[R]] = r;
236  dst[x + histeq->rgba_map[G]] = g;
237  dst[x + histeq->rgba_map[B]] = b;
238  oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
239  histeq->out_histogram[oluma]++;
240  }
241  }
242  src += inpic->linesize[0];
243  dst += outpic->linesize[0];
244  }
245 #ifdef DEBUG
246  for (x = 0; x < 256; x++)
247  av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
248 #endif
249 
250  av_frame_free(&inpic);
251  return ff_filter_frame(outlink, outpic);
252 }
253 
254 static const AVFilterPad histeq_inputs[] = {
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .config_props = config_input,
259  .filter_frame = filter_frame,
260  },
261  { NULL }
262 };
263 
264 static const AVFilterPad histeq_outputs[] = {
265  {
266  .name = "default",
267  .type = AVMEDIA_TYPE_VIDEO,
268  },
269  { NULL }
270 };
271 
273  .name = "histeq",
274  .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
275  .priv_size = sizeof(HisteqContext),
276  .init = init,
278 
279  .inputs = histeq_inputs,
280  .outputs = histeq_outputs,
281  .priv_class = &histeq_class,
282 };
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_histeq.c:131
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
int out_histogram[256]
output histogram
Definition: vf_histeq.c:63
#define FLAGS
Definition: vf_histeq.c:70
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:1731
#define LCG_SEED
Definition: vf_histeq.c:47
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
uint8_t rgba_map[4]
components position
Definition: vf_histeq.c:65
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
static const AVOption histeq_options[]
Definition: vf_histeq.c:73
char * antibanding_str
Definition: vf_histeq.c:61
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
const char * name
Pad name.
int in_histogram[256]
input histogram
Definition: vf_histeq.c:62
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
window constants for m
#define b
Definition: input.c:42
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:98
AVFilter avfilter_vf_histeq
Definition: vf_histeq.c:272
float intensity
Definition: vf_histeq.c:59
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:545
A filter pad used for either input or output.
Discrete Time axis x
#define CONST(name, help, val, unit)
Definition: vf_histeq.c:71
#define LCG(x)
Definition: vf_histeq.c:46
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
const char * r
Definition: vf_curves.c:94
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
enum HisteqAntibanding antibanding
Definition: vf_histeq.c:60
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
static int config_input(AVFilterLink *inlink)
Definition: vf_histeq.c:108
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
#define GET_RGB_VALUES(r, g, b, src, map)
Definition: vf_histeq.c:125
#define AV_LOG_VERBOSE
Definition: log.h:157
FFT buffer for g
Definition: stft_peak.m:17
#define R
Definition: vf_histeq.c:120
static const AVFilterPad histeq_outputs[]
Definition: vf_histeq.c:264
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
#define OFFSET(x)
Definition: vf_histeq.c:69
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
static av_cold int init(AVFilterContext *ctx)
Definition: vf_histeq.c:85
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
int bpp
bytes per pixel
Definition: vf_histeq.c:66
HisteqAntibanding
Definition: vf_histeq.c:49
AVFILTER_DEFINE_CLASS(histeq)
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
synthesis window for stochastic i
#define A
Definition: vf_histeq.c:123
const char * name
filter name
Definition: avfilter.h:437
static int query_formats(AVFilterContext *ctx)
Definition: vf_histeq.c:96
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static const AVFilterPad histeq_inputs[]
Definition: vf_histeq.c:254
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal and external API header
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
function y
Definition: D.m:1
#define B
Definition: vf_histeq.c:122
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
An instance of a filter.
Definition: avfilter.h:524
#define LCG_M
Definition: vf_histeq.c:45
#define G
Definition: vf_histeq.c:121
internal API functions
int LUT[256]
lookup table derived from histogram[]
Definition: vf_histeq.c:64
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
for(j=16;j >0;--j)
#define FFMAX3(a, b, c)
Definition: common.h:57
float strength
Definition: vf_histeq.c:58