FFmpeg
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 HisteqContext {
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 
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 
96 static const enum AVPixelFormat pix_fmts[] = {
100 };
101 
103 {
104  AVFilterContext *ctx = inlink->dst;
105  HisteqContext *histeq = ctx->priv;
106  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
107 
108  histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
109  ff_fill_rgba_map(histeq->rgba_map, inlink->format);
110 
111  return 0;
112 }
113 
114 #define R 0
115 #define G 1
116 #define B 2
117 #define A 3
118 
119 #define GET_RGB_VALUES(r, g, b, src, map) do { \
120  r = src[x + map[R]]; \
121  g = src[x + map[G]]; \
122  b = src[x + map[B]]; \
123 } while (0)
124 
126 {
127  AVFilterContext *ctx = inlink->dst;
128  HisteqContext *histeq = ctx->priv;
129  AVFilterLink *outlink = ctx->outputs[0];
130  int strength = histeq->strength * 1000;
131  int intensity = histeq->intensity * 1000;
132  int x, y, i, luthi, lutlo, lut, luma, oluma, m;
133  AVFrame *outpic;
134  unsigned int r, g, b, jran;
135  uint8_t *src, *dst;
136 
137  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
138  if (!outpic) {
140  return AVERROR(ENOMEM);
141  }
142  av_frame_copy_props(outpic, inpic);
143 
144  /* Seed random generator for antibanding. */
145  jran = LCG_SEED;
146 
147  /* Calculate and store the luminance and calculate the global histogram
148  based on the luminance. */
149  memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
150  src = inpic->data[0];
151  dst = outpic->data[0];
152  for (y = 0; y < inlink->h; y++) {
153  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
154  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
155  luma = (55 * r + 182 * g + 19 * b) >> 8;
156  dst[x + histeq->rgba_map[A]] = luma;
157  histeq->in_histogram[luma]++;
158  }
159  src += inpic->linesize[0];
160  dst += outpic->linesize[0];
161  }
162 
163 #ifdef DEBUG
164  for (x = 0; x < 256; x++)
165  ff_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
166 #endif
167 
168  /* Calculate the lookup table. */
169  histeq->LUT[0] = histeq->in_histogram[0];
170  /* Accumulate */
171  for (x = 1; x < 256; x++)
172  histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
173 
174  /* Normalize */
175  for (x = 0; x < 256; x++)
176  histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
177 
178  /* Adjust the LUT based on the selected strength. This is an alpha
179  mix of the calculated LUT and a linear LUT with gain 1. */
180  for (x = 0; x < 256; x++)
181  histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
182  ((255 - strength) * x) / 255;
183 
184  /* Output the equalized frame. */
185  memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
186 
187  src = inpic->data[0];
188  dst = outpic->data[0];
189  for (y = 0; y < inlink->h; y++) {
190  for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
191  luma = dst[x + histeq->rgba_map[A]];
192  if (luma == 0) {
193  for (i = 0; i < histeq->bpp; ++i)
194  dst[x + i] = 0;
195  histeq->out_histogram[0]++;
196  } else {
197  lut = histeq->LUT[luma];
198  if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
199  if (luma > 0) {
200  lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
201  (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
202  histeq->LUT[luma - 1];
203  } else
204  lutlo = lut;
205 
206  if (luma < 255) {
207  luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
208  (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
209  histeq->LUT[luma + 1];
210  } else
211  luthi = lut;
212 
213  if (lutlo != luthi) {
214  jran = LCG(jran);
215  lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
216  }
217  }
218 
219  GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
220  if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
221  r = (r * 255) / m;
222  g = (g * 255) / m;
223  b = (b * 255) / m;
224  } else {
225  r = (r * lut) / luma;
226  g = (g * lut) / luma;
227  b = (b * lut) / luma;
228  }
229  dst[x + histeq->rgba_map[R]] = r;
230  dst[x + histeq->rgba_map[G]] = g;
231  dst[x + histeq->rgba_map[B]] = b;
232  oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
233  histeq->out_histogram[oluma]++;
234  }
235  }
236  src += inpic->linesize[0];
237  dst += outpic->linesize[0];
238  }
239 #ifdef DEBUG
240  for (x = 0; x < 256; x++)
241  ff_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
242 #endif
243 
245  return ff_filter_frame(outlink, outpic);
246 }
247 
248 static const AVFilterPad histeq_inputs[] = {
249  {
250  .name = "default",
251  .type = AVMEDIA_TYPE_VIDEO,
252  .config_props = config_input,
253  .filter_frame = filter_frame,
254  },
255 };
256 
257 static const AVFilterPad histeq_outputs[] = {
258  {
259  .name = "default",
260  .type = AVMEDIA_TYPE_VIDEO,
261  },
262 };
263 
265  .name = "histeq",
266  .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
267  .priv_size = sizeof(HisteqContext),
268  .init = init,
272  .priv_class = &histeq_class,
274 };
HISTEQ_ANTIBANDING_WEAK
@ HISTEQ_ANTIBANDING_WEAK
Definition: vf_histeq.c:52
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
histeq_options
static const AVOption histeq_options[]
Definition: vf_histeq.c:73
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2888
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:174
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
ff_vf_histeq
const AVFilter ff_vf_histeq
Definition: vf_histeq.c:264
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
B
#define B
Definition: vf_histeq.c:116
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:41
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
av_get_bits_per_pixel
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:2840
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
HisteqContext::bpp
int bpp
bytes per pixel
Definition: vf_histeq.c:66
formats.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
histeq_outputs
static const AVFilterPad histeq_outputs[]
Definition: vf_histeq.c:257
GET_RGB_VALUES
#define GET_RGB_VALUES(r, g, b, src, map)
Definition: vf_histeq.c:119
av_cold
#define av_cold
Definition: attributes.h:90
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_histeq.c:96
g
const char * g
Definition: vf_curves.c:127
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
LCG
#define LCG(x)
Definition: vf_histeq.c:47
HisteqContext::strength
float strength
Definition: vf_histeq.c:59
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:594
inpic
av_frame_free & inpic
Definition: vf_mcdeint.c:270
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_histeq.c:85
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
HISTEQ_ANTIBANDING_NONE
@ HISTEQ_ANTIBANDING_NONE
Definition: vf_histeq.c:51
CONST
#define CONST(name, help, val, unit)
Definition: vf_histeq.c:71
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
LUT
Definition: vf_morpho.c:63
HisteqContext::in_histogram
int in_histogram[256]
input histogram
Definition: vf_histeq.c:62
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
A
#define A
Definition: vf_histeq.c:117
HisteqContext::rgba_map
uint8_t rgba_map[4]
components position
Definition: vf_histeq.c:65
histeq_inputs
static const AVFilterPad histeq_inputs[]
Definition: vf_histeq.c:248
HisteqContext
Definition: vf_histeq.c:57
HisteqContext::antibanding
int antibanding
HisteqAntibanding.
Definition: vf_histeq.c:61
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:142
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
R
#define R
Definition: vf_histeq.c:114
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
common.h
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_histeq.c:102
HisteqContext::out_histogram
int out_histogram[256]
output histogram
Definition: vf_histeq.c:63
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:161
HisteqContext::LUT
int LUT[256]
lookup table derived from histogram[]
Definition: vf_histeq.c:64
OFFSET
#define OFFSET(x)
Definition: vf_histeq.c:69
LCG_M
#define LCG_M
Definition: vf_histeq.c:46
G
#define G
Definition: vf_histeq.c:115
HisteqAntibanding
HisteqAntibanding
Definition: vf_histeq.c:50
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(histeq)
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_histeq.c:125
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FLAGS
#define FLAGS
Definition: vf_histeq.c:70
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
HISTEQ_ANTIBANDING_STRONG
@ HISTEQ_ANTIBANDING_STRONG
Definition: vf_histeq.c:53
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:375
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
drawutils.h
LCG_SEED
#define LCG_SEED
Definition: vf_histeq.c:48
HISTEQ_ANTIBANDING_NB
@ HISTEQ_ANTIBANDING_NB
Definition: vf_histeq.c:54
HisteqContext::intensity
float intensity
Definition: vf_histeq.c:60