1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* Copyright (c) 2012-2013 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/avassert.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
enum HistogramMode {
MODE_LEVELS,
MODE_WAVEFORM,
MODE_COLOR,
MODE_COLOR2,
MODE_NB
};
typedef struct HistogramContext {
const AVClass *class; ///< AVClass context for log and options purpose
enum HistogramMode mode;
unsigned histogram[256];
int ncomp;
const uint8_t *bg_color;
const uint8_t *fg_color;
int level_height;
int scale_height;
int step;
int waveform_mode;
int display_mode;
int levels_mode;
} HistogramContext;
#define OFFSET(x) offsetof(HistogramContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption histogram_options[] = {
{ "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
{ "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
{ "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
{ "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
{ "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
{ "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
{ "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
{ "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
{ "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
{ "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
{ "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
{ "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
{ "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
{ "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
{ "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
{ "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
{ "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
{ NULL },
};
AVFILTER_DEFINE_CLASS(histogram);
static const enum AVPixelFormat color_pix_fmts[] = {
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_NONE
};
static const enum AVPixelFormat levels_pix_fmts[] = {
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
};
static int query_formats(AVFilterContext *ctx)
{
HistogramContext *h = ctx->priv;
const enum AVPixelFormat *pix_fmts;
switch (h->mode) {
case MODE_WAVEFORM:
case MODE_LEVELS:
pix_fmts = levels_pix_fmts;
break;
case MODE_COLOR:
case MODE_COLOR2:
pix_fmts = color_pix_fmts;
break;
default:
av_assert0(0);
}
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
return 0;
}
static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
static int config_input(AVFilterLink *inlink)
{
HistogramContext *h = inlink->dst->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
h->ncomp = desc->nb_components;
switch (inlink->format) {
case AV_PIX_FMT_GBRAP:
case AV_PIX_FMT_GBRP:
h->bg_color = black_gbrp_color;
h->fg_color = white_gbrp_color;
break;
default:
h->bg_color = black_yuva_color;
h->fg_color = white_yuva_color;
}
return 0;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
HistogramContext *h = ctx->priv;
switch (h->mode) {
case MODE_LEVELS:
outlink->w = 256;
outlink->h = (h->level_height + h->scale_height) * FFMAX(h->ncomp * h->display_mode, 1);
break;
case MODE_WAVEFORM:
if (h->waveform_mode)
outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
else
outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
break;
case MODE_COLOR:
case MODE_COLOR2:
outlink->h = outlink->w = 256;
break;
default:
av_assert0(0);
}
outlink->sample_aspect_ratio = (AVRational){1,1};
return 0;
}
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{
HistogramContext *h = inlink->dst->priv;
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
AVFrame *out;
const uint8_t *src;
uint8_t *dst;
int i, j, k, l;
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
out->pts = in->pts;
for (k = 0; k < h->ncomp; k++)
for (i = 0; i < outlink->h; i++)
memset(out->data[k] + i * out->linesize[k], h->bg_color[k], outlink->w);
switch (h->mode) {
case MODE_LEVELS:
for (k = 0; k < h->ncomp; k++) {
int start = k * (h->level_height + h->scale_height) * h->display_mode;
double max_hval_log;
unsigned max_hval = 0;
for (i = 0; i < in->height; i++) {
src = in->data[k] + i * in->linesize[k];
for (j = 0; j < in->width; j++)
h->histogram[src[j]]++;
}
for (i = 0; i < 256; i++)
max_hval = FFMAX(max_hval, h->histogram[i]);
max_hval_log = log2(max_hval + 1);
for (i = 0; i < outlink->w; i++) {
int col_height;
if (h->levels_mode)
col_height = round(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
else
col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
for (j = h->level_height - 1; j >= col_height; j--) {
if (h->display_mode) {
for (l = 0; l < h->ncomp; l++)
out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
} else {
out->data[k][(j + start) * out->linesize[k] + i] = 255;
}
}
for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
out->data[k][(j + start) * out->linesize[k] + i] = i;
}
memset(h->histogram, 0, 256 * sizeof(unsigned));
}
break;
case MODE_WAVEFORM:
if (h->waveform_mode) {
for (k = 0; k < h->ncomp; k++) {
int offset = k * 256 * h->display_mode;
for (i = 0; i < inlink->w; i++) {
for (j = 0; j < inlink->h; j++) {
int pos = (offset +
in->data[k][j * in->linesize[k] + i]) *
out->linesize[k] + i;
unsigned value = out->data[k][pos];
value = FFMIN(value + h->step, 255);
out->data[k][pos] = value;
}
}
}
} else {
for (k = 0; k < h->ncomp; k++) {
int offset = k * 256 * h->display_mode;
for (i = 0; i < inlink->h; i++) {
src = in ->data[k] + i * in ->linesize[k];
dst = out->data[k] + i * out->linesize[k];
for (j = 0; j < inlink->w; j++) {
int pos = src[j] + offset;
unsigned value = dst[pos];
value = FFMIN(value + h->step, 255);
dst[pos] = value;
}
}
}
}
break;
case MODE_COLOR:
for (i = 0; i < inlink->h; i++) {
int iw1 = i * in->linesize[1];
int iw2 = i * in->linesize[2];
for (j = 0; j < inlink->w; j++) {
int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
if (out->data[0][pos] < 255)
out->data[0][pos]++;
}
}
for (i = 0; i < 256; i++) {
dst = out->data[0] + i * out->linesize[0];
for (j = 0; j < 256; j++) {
if (!dst[j]) {
out->data[1][i * out->linesize[0] + j] = i;
out->data[2][i * out->linesize[0] + j] = j;
}
}
}
break;
case MODE_COLOR2:
for (i = 0; i < inlink->h; i++) {
int iw1 = i * in->linesize[1];
int iw2 = i * in->linesize[2];
for (j = 0; j < inlink->w; j++) {
int u = in->data[1][iw1 + j];
int v = in->data[2][iw2 + j];
int pos = u * out->linesize[0] + v;
if (!out->data[0][pos])
out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
out->data[1][pos] = u;
out->data[2][pos] = v;
}
}
break;
default:
av_assert0(0);
}
av_frame_free(&in);
return ff_filter_frame(outlink, out);
}
static const AVFilterPad inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
.config_props = config_input,
},
{ NULL }
};
static const AVFilterPad outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_output,
},
{ NULL }
};
AVFilter avfilter_vf_histogram = {
.name = "histogram",
.description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
.priv_size = sizeof(HistogramContext),
.query_formats = query_formats,
.inputs = inputs,
.outputs = outputs,
.priv_class = &histogram_class,
};
|