2 * Copyright (c) 2012-2013 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avassert.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/pixdesc.h"
38 typedef struct HistogramContext {
39 const AVClass *class; ///< AVClass context for log and options purpose
40 enum HistogramMode mode;
41 unsigned histogram[256];
44 const uint8_t *bg_color;
45 const uint8_t *fg_color;
52 #define OFFSET(x) offsetof(HistogramContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55 static const AVOption histogram_options[] = {
56 { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
57 { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
58 { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
59 { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
60 { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
61 { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
62 { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
63 { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
64 { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
65 { "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
66 { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
70 AVFILTER_DEFINE_CLASS(histogram);
72 static av_cold int init(AVFilterContext *ctx, const char *args)
74 HistogramContext *h = ctx->priv;
77 h->class = &histogram_class;
78 av_opt_set_defaults(h);
80 if ((ret = (av_set_options_string(h, args, "=", ":"))) < 0)
86 static const enum AVPixelFormat color_pix_fmts[] = {
87 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
91 static const enum AVPixelFormat levels_pix_fmts[] = {
92 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
93 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE
96 static int query_formats(AVFilterContext *ctx)
98 HistogramContext *h = ctx->priv;
99 const enum AVPixelFormat *pix_fmts;
103 pix_fmts = levels_pix_fmts;
108 pix_fmts = color_pix_fmts;
114 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
119 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
120 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
121 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
122 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
124 static int config_input(AVFilterLink *inlink)
126 HistogramContext *h = inlink->dst->priv;
127 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
129 h->ncomp = desc->nb_components;
131 switch (inlink->format) {
132 case AV_PIX_FMT_GBRP:
133 h->bg_color = black_gbrp_color;
134 h->fg_color = white_gbrp_color;
137 h->bg_color = black_yuva_color;
138 h->fg_color = white_yuva_color;
144 static int config_output(AVFilterLink *outlink)
146 AVFilterContext *ctx = outlink->src;
147 HistogramContext *h = ctx->priv;
152 outlink->h = (h->level_height + h->scale_height) * h->ncomp;
155 if (h->waveform_mode)
162 outlink->h = outlink->w = 256;
168 outlink->sample_aspect_ratio = (AVRational){1,1};
173 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
175 HistogramContext *h = inlink->dst->priv;
176 AVFilterContext *ctx = inlink->dst;
177 AVFilterLink *outlink = ctx->outputs[0];
178 AVFilterBufferRef *out;
183 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
185 avfilter_unref_bufferp(&in);
186 return AVERROR(ENOMEM);
192 for (k = 0; k < h->ncomp; k++)
193 for (i = 0; i < outlink->h; i++)
194 memset(out->data[k] + i * out->linesize[k], h->bg_color[k], outlink->w);
198 for (k = 0; k < h->ncomp; k++) {
199 int start = k * (h->level_height + h->scale_height);
201 for (i = 0; i < in->video->h; i++) {
202 src = in->data[k] + i * in->linesize[k];
203 for (j = 0; j < in->video->w; j++)
204 h->histogram[src[j]]++;
207 for (i = 0; i < 256; i++)
208 h->max_hval = FFMAX(h->max_hval, h->histogram[i]);
210 for (i = 0; i < outlink->w; i++) {
211 int col_height = h->level_height - (float)h->histogram[i] / h->max_hval * h->level_height;
213 for (j = h->level_height - 1; j >= col_height; j--)
214 for (l = 0; l < h->ncomp; l++)
215 out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
216 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
217 out->data[k][(j + start) * out->linesize[k] + i] = i;
220 memset(h->histogram, 0, 256 * sizeof(unsigned));
225 if (h->waveform_mode) {
226 for (i = 0; i < inlink->w; i++) {
227 for (j = 0; j < inlink->h; j++) {
228 int pos = in->data[0][j * in->linesize[0] + i] * out->linesize[0] + i;
229 unsigned value = out->data[0][pos];
230 value = FFMIN(value + h->step, 255);
231 out->data[0][pos] = value;
235 for (i = 0; i < inlink->h; i++) {
236 src = in ->data[0] + i * in ->linesize[0];
237 dst = out->data[0] + i * out->linesize[0];
238 for (j = 0; j < inlink->w; j++) {
240 unsigned value = dst[pos];
241 value = FFMIN(value + h->step, 255);
248 for (i = 0; i < inlink->h; i++) {
249 int iw1 = i * in->linesize[1];
250 int iw2 = i * in->linesize[2];
251 for (j = 0; j < inlink->w; j++) {
252 int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
253 if (out->data[0][pos] < 255)
257 for (i = 0; i < 256; i++) {
258 dst = out->data[0] + i * out->linesize[0];
259 for (j = 0; j < 256; j++) {
261 out->data[1][i * out->linesize[0] + j] = i;
262 out->data[2][i * out->linesize[0] + j] = j;
268 for (i = 0; i < inlink->h; i++) {
269 int iw1 = i * in->linesize[1];
270 int iw2 = i * in->linesize[2];
271 for (j = 0; j < inlink->w; j++) {
272 int u = in->data[1][iw1 + j];
273 int v = in->data[2][iw2 + j];
274 int pos = u * out->linesize[0] + v;
275 if (!out->data[0][pos])
276 out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
277 out->data[1][pos] = u;
278 out->data[2][pos] = v;
286 ret = ff_filter_frame(outlink, out);
287 avfilter_unref_bufferp(&in);
293 static av_cold void uninit(AVFilterContext *ctx)
295 HistogramContext *h = ctx->priv;
300 static const AVFilterPad inputs[] = {
303 .type = AVMEDIA_TYPE_VIDEO,
304 .filter_frame = filter_frame,
305 .config_props = config_input,
306 .min_perms = AV_PERM_READ,
311 static const AVFilterPad outputs[] = {
314 .type = AVMEDIA_TYPE_VIDEO,
315 .config_props = config_output,
320 AVFilter avfilter_vf_histogram = {
322 .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
323 .priv_size = sizeof(HistogramContext),
326 .query_formats = query_formats,
329 .priv_class = &histogram_class,