FFmpeg
 All Data Structures Namespaces 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/internal.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 
35 #include "avfilter.h"
36 #include "drawutils.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "video.h"
40 
41 // #define DEBUG
42 
43 // Linear Congruential Generator, see "Numerical Recipes"
44 #define LCG_A 4096
45 #define LCG_C 150889
46 #define LCG_M 714025
47 #define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M)
48 #define LCG_SEED 739187
49 
55 };
56 
57 typedef struct {
58  const AVClass *class;
59  float strength;
60  float intensity;
61  int antibanding; ///< HisteqAntibanding
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 AVPixelFormat pix_fmts[] = {
102  };
103  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
104  if (!fmts_list)
105  return AVERROR(ENOMEM);
106  return ff_set_common_formats(ctx, fmts_list);
107 }
108 
109 static int config_input(AVFilterLink *inlink)
110 {
111  AVFilterContext *ctx = inlink->dst;
112  HisteqContext *histeq = ctx->priv;
113  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
114 
115  histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
116  ff_fill_rgba_map(histeq->rgba_map, inlink->format);
117 
118  return 0;
119 }
120 
121 #define R 0
122 #define G 1
123 #define B 2
124 #define A 3
125 
126 #define GET_RGB_VALUES(r, g, b, src, map) do { \
127  r = src[x + map[R]]; \
128  g = src[x + map[G]]; \
129  b = src[x + map[B]]; \
130 } while (0)
131 
132 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
133 {
134  AVFilterContext *ctx = inlink->dst;
135  HisteqContext *histeq = ctx->priv;
136  AVFilterLink *outlink = ctx->outputs[0];
137  int strength = histeq->strength * 1000;
138  int intensity = histeq->intensity * 1000;
139  int x, y, i, luthi, lutlo, lut, luma, oluma, m;
140  AVFrame *outpic;
141  unsigned int r, g, b, jran;
142  uint8_t *src, *dst;
143 
144  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
145  if (!outpic) {
146  av_frame_free(&inpic);
147  return AVERROR(ENOMEM);
148  }
149  av_frame_copy_props(outpic, inpic);
150 
151  /* Seed random generator for antibanding. */
152  jran = LCG_SEED;
153 
154  /* Calculate and store the luminance and calculate the global histogram
155  based on the luminance. */
156  memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
157  src = inpic->data[0];
158  dst = outpic->data[0];
159  for (y = 0; y < inlink->h; y++) {
160  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
161  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
162  luma = (55 * r + 182 * g + 19 * b) >> 8;
163  dst[x + histeq->rgba_map[A]] = luma;
164  histeq->in_histogram[luma]++;
165  }
166  src += inpic->linesize[0];
167  dst += outpic->linesize[0];
168  }
169 
170 #ifdef DEBUG
171  for (x = 0; x < 256; x++)
172  ff_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
173 #endif
174 
175  /* Calculate the lookup table. */
176  histeq->LUT[0] = histeq->in_histogram[0];
177  /* Accumulate */
178  for (x = 1; x < 256; x++)
179  histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
180 
181  /* Normalize */
182  for (x = 0; x < 256; x++)
183  histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
184 
185  /* Adjust the LUT based on the selected strength. This is an alpha
186  mix of the calculated LUT and a linear LUT with gain 1. */
187  for (x = 0; x < 256; x++)
188  histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
189  ((255 - strength) * x) / 255;
190 
191  /* Output the equalized frame. */
192  memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
193 
194  src = inpic->data[0];
195  dst = outpic->data[0];
196  for (y = 0; y < inlink->h; y++) {
197  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
198  luma = dst[x + histeq->rgba_map[A]];
199  if (luma == 0) {
200  for (i = 0; i < histeq->bpp; ++i)
201  dst[x + i] = 0;
202  histeq->out_histogram[0]++;
203  } else {
204  lut = histeq->LUT[luma];
205  if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
206  if (luma > 0) {
207  lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
208  (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
209  histeq->LUT[luma - 1];
210  } else
211  lutlo = lut;
212 
213  if (luma < 255) {
214  luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
215  (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
216  histeq->LUT[luma + 1];
217  } else
218  luthi = lut;
219 
220  if (lutlo != luthi) {
221  jran = LCG(jran);
222  lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
223  }
224  }
225 
226  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
227  if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
228  r = (r * 255) / m;
229  g = (g * 255) / m;
230  b = (b * 255) / m;
231  } else {
232  r = (r * lut) / luma;
233  g = (g * lut) / luma;
234  b = (b * lut) / luma;
235  }
236  dst[x + histeq->rgba_map[R]] = r;
237  dst[x + histeq->rgba_map[G]] = g;
238  dst[x + histeq->rgba_map[B]] = b;
239  oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
240  histeq->out_histogram[oluma]++;
241  }
242  }
243  src += inpic->linesize[0];
244  dst += outpic->linesize[0];
245  }
246 #ifdef DEBUG
247  for (x = 0; x < 256; x++)
248  ff_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
249 #endif
250 
251  av_frame_free(&inpic);
252  return ff_filter_frame(outlink, outpic);
253 }
254 
255 static const AVFilterPad histeq_inputs[] = {
256  {
257  .name = "default",
258  .type = AVMEDIA_TYPE_VIDEO,
259  .config_props = config_input,
260  .filter_frame = filter_frame,
261  },
262  { NULL }
263 };
264 
265 static const AVFilterPad histeq_outputs[] = {
266  {
267  .name = "default",
268  .type = AVMEDIA_TYPE_VIDEO,
269  },
270  { NULL }
271 };
272 
274  .name = "histeq",
275  .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
276  .priv_size = sizeof(HisteqContext),
277  .init = init,
279  .inputs = histeq_inputs,
280  .outputs = histeq_outputs,
281  .priv_class = &histeq_class,
283 };
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_histeq.c:132
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2129
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:171
AVOption.
Definition: opt.h:255
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
const char * g
Definition: vf_curves.c:108
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:2081
const char * b
Definition: vf_curves.c:109
#define LCG_SEED
Definition: vf_histeq.c:48
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:109
AVFilter ff_vf_histeq
Definition: vf_histeq.c:273
static const AVOption histeq_options[]
Definition: vf_histeq.c:73
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:451
const char * name
Pad name.
Definition: internal.h:69
int in_histogram[256]
input histogram
Definition: vf_histeq.c:62
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
av_frame_free & inpic
Definition: vf_mcdeint.c:279
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:96
#define ff_dlog(a,...)
float intensity
Definition: vf_histeq.c:60
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
A filter pad used for either input or output.
Definition: internal.h:63
int 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:542
#define CONST(name, help, val, unit)
Definition: vf_histeq.c:71
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define LCG(x)
Definition: vf_histeq.c:47
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:97
const char * r
Definition: vf_curves.c:107
void * priv
private data for use by the filter
Definition: avfilter.h:654
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:94
static int config_input(AVFilterLink *inlink)
Definition: vf_histeq.c:109
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
#define GET_RGB_VALUES(r, g, b, src, map)
Definition: vf_histeq.c:126
common internal API header
float y
#define R
Definition: vf_histeq.c:121
static const AVFilterPad histeq_outputs[]
Definition: vf_histeq.c:265
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:34
#define OFFSET(x)
Definition: vf_histeq.c:69
AVS_Value src
Definition: avisynth_c.h:482
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:199
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
int bpp
bytes per pixel
Definition: vf_histeq.c:66
HisteqAntibanding
Definition: vf_histeq.c:50
AVFILTER_DEFINE_CLASS(histeq)
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
#define A
Definition: vf_histeq.c:124
const char * name
Filter name.
Definition: avfilter.h:474
static int query_formats(AVFilterContext *ctx)
Definition: vf_histeq.c:96
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
int antibanding
HisteqAntibanding.
Definition: vf_histeq.c:61
static int flags
Definition: cpu.c:47
static const AVFilterPad histeq_inputs[]
Definition: vf_histeq.c:255
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
common internal and external API header
#define B
Definition: vf_histeq.c:123
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
#define LCG_M
Definition: vf_histeq.c:46
#define G
Definition: vf_histeq.c:122
internal API functions
int LUT[256]
lookup table derived from histogram[]
Definition: vf_histeq.c:64
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
for(j=16;j >0;--j)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:553
#define FFMAX3(a, b, c)
Definition: common.h:65
float strength
Definition: vf_histeq.c:59