2 * Copyright (c) 2012-2019 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"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/intreadwrite.h"
32 typedef struct HistogramContext {
33 const AVClass *class; ///< AVClass context for log and options purpose
35 unsigned histogram[256*256];
48 const AVPixFmtDescriptor *desc, *odesc;
58 #define OFFSET(x) offsetof(HistogramContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61 #define COMMON_OPTIONS \
62 { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"}, \
63 { "d", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"}, \
64 { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" }, \
65 { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" }, \
66 { "stack", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "display_mode" }, \
67 { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, \
68 { "m", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, \
69 { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" }, \
70 { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" }, \
71 { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS}, \
72 { "c", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
74 static const AVOption histogram_options[] = {
75 { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
76 { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
78 { "fgopacity", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
79 { "f", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
80 { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
81 { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
85 AVFILTER_DEFINE_CLASS(histogram);
87 static const enum AVPixelFormat levels_in_pix_fmts[] = {
88 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
89 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
90 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
91 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
92 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
93 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
94 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
95 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
96 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
97 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
98 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
99 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
100 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
105 static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
106 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
110 static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
111 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
115 static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
116 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
120 static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
121 AV_PIX_FMT_YUV444P12,
125 static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
126 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
130 static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
135 static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
136 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
140 static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
141 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
145 static int query_formats(AVFilterContext *ctx)
147 AVFilterFormats *avff;
148 const AVPixFmtDescriptor *desc;
149 const enum AVPixelFormat *out_pix_fmts;
153 if (!ctx->inputs[0]->in_formats ||
154 !ctx->inputs[0]->in_formats->nb_formats) {
155 return AVERROR(EAGAIN);
158 if (!ctx->inputs[0]->out_formats)
159 if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
161 avff = ctx->inputs[0]->in_formats;
162 desc = av_pix_fmt_desc_get(avff->formats[0]);
163 rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
164 bits = desc->comp[0].depth;
165 for (i = 1; i < avff->nb_formats; i++) {
166 desc = av_pix_fmt_desc_get(avff->formats[i]);
167 if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
168 (bits != desc->comp[0].depth))
169 return AVERROR(EAGAIN);
172 if (rgb && bits == 8)
173 out_pix_fmts = levels_out_rgb8_pix_fmts;
174 else if (rgb && bits == 9)
175 out_pix_fmts = levels_out_rgb9_pix_fmts;
176 else if (rgb && bits == 10)
177 out_pix_fmts = levels_out_rgb10_pix_fmts;
178 else if (rgb && bits == 12)
179 out_pix_fmts = levels_out_rgb12_pix_fmts;
181 out_pix_fmts = levels_out_yuv8_pix_fmts;
183 out_pix_fmts = levels_out_yuv9_pix_fmts;
185 out_pix_fmts = levels_out_yuv10_pix_fmts;
187 out_pix_fmts = levels_out_yuv12_pix_fmts;
189 return AVERROR(EAGAIN);
190 if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
196 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
197 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
198 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
199 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
201 static int config_input(AVFilterLink *inlink)
203 HistogramContext *s = inlink->dst->priv;
205 s->desc = av_pix_fmt_desc_get(inlink->format);
206 s->ncomp = s->desc->nb_components;
207 s->histogram_size = 1 << s->desc->comp[0].depth;
208 s->mult = s->histogram_size / 256;
210 switch (inlink->format) {
211 case AV_PIX_FMT_GBRAP12:
212 case AV_PIX_FMT_GBRP12:
213 case AV_PIX_FMT_GBRAP10:
214 case AV_PIX_FMT_GBRP10:
215 case AV_PIX_FMT_GBRP9:
216 case AV_PIX_FMT_GBRAP:
217 case AV_PIX_FMT_GBRP:
218 memcpy(s->bg_color, black_gbrp_color, 4);
219 memcpy(s->fg_color, white_gbrp_color, 4);
220 s->start[0] = s->start[1] = s->start[2] = s->start[3] = 0;
223 memcpy(s->bg_color, black_yuva_color, 4);
224 memcpy(s->fg_color, white_yuva_color, 4);
225 s->start[0] = s->start[3] = 0;
226 s->start[1] = s->start[2] = s->histogram_size / 2;
229 s->fg_color[3] = s->fgopacity * 255;
230 s->bg_color[3] = s->bgopacity * 255;
232 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
233 s->planeheight[0] = s->planeheight[3] = inlink->h;
234 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
235 s->planewidth[0] = s->planewidth[3] = inlink->w;
240 static int config_output(AVFilterLink *outlink)
242 AVFilterContext *ctx = outlink->src;
243 HistogramContext *s = ctx->priv;
246 if (!strcmp(ctx->filter->name, "thistogram"))
249 for (i = 0; i < s->ncomp; i++) {
250 if ((1 << i) & s->components)
256 s->width = ctx->inputs[0]->w;
257 outlink->w = s->width * FFMAX(ncomp * (s->display_mode == 1), 1);
258 outlink->h = s->histogram_size * FFMAX(ncomp * (s->display_mode == 2), 1);
260 outlink->w = s->histogram_size * FFMAX(ncomp * (s->display_mode == 1), 1);
261 outlink->h = (s->level_height + s->scale_height) * FFMAX(ncomp * (s->display_mode == 2), 1);
264 s->odesc = av_pix_fmt_desc_get(outlink->format);
265 s->dncomp = s->odesc->nb_components;
266 outlink->sample_aspect_ratio = (AVRational){1,1};
271 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
273 HistogramContext *s = inlink->dst->priv;
274 AVFilterContext *ctx = inlink->dst;
275 AVFilterLink *outlink = ctx->outputs[0];
276 AVFrame *out = s->out;
279 if (!s->thistogram || !out) {
280 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
283 return AVERROR(ENOMEM);
287 for (k = 0; k < 4 && out->data[k]; k++) {
288 const int is_chroma = (k == 1 || k == 2);
289 const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? s->odesc->log2_chroma_h : 0));
290 const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? s->odesc->log2_chroma_w : 0));
292 if (s->histogram_size <= 256) {
293 for (i = 0; i < dst_h ; i++)
294 memset(out->data[s->odesc->comp[k].plane] +
295 i * out->linesize[s->odesc->comp[k].plane],
296 s->bg_color[k], dst_w);
298 const int mult = s->mult;
300 for (i = 0; i < dst_h ; i++)
301 for (j = 0; j < dst_w; j++)
302 AV_WN16(out->data[s->odesc->comp[k].plane] +
303 i * out->linesize[s->odesc->comp[k].plane] + j * 2,
304 s->bg_color[k] * mult);
309 for (m = 0, k = 0; k < s->ncomp; k++) {
310 const int p = s->desc->comp[k].plane;
311 const int max_value = s->histogram_size - 1 - s->start[p];
312 const int height = s->planeheight[p];
313 const int width = s->planewidth[p];
315 unsigned max_hval = 0;
318 if (!((1 << k) & s->components))
321 starty = m * s->histogram_size * (s->display_mode == 2);
322 startx = m++ * s->width * (s->display_mode == 1);
324 startx = m * s->histogram_size * (s->display_mode == 1);
325 starty = m++ * (s->level_height + s->scale_height) * (s->display_mode == 2);
328 if (s->histogram_size <= 256) {
329 for (i = 0; i < height; i++) {
330 const uint8_t *src = in->data[p] + i * in->linesize[p];
331 for (j = 0; j < width; j++)
332 s->histogram[src[j]]++;
335 for (i = 0; i < height; i++) {
336 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
337 for (j = 0; j < width; j++)
338 s->histogram[src[j]]++;
342 for (i = 0; i < s->histogram_size; i++)
343 max_hval = FFMAX(max_hval, s->histogram[i]);
344 max_hval_log = log2(max_hval + 1);
347 for (int i = 0; i < s->histogram_size; i++) {
348 int idx = s->histogram_size - i - 1;
349 int value = s->start[p];
352 value += lrint(max_value * (log2(s->histogram[idx] + 1) / max_hval_log));
354 value += lrint(max_value * s->histogram[idx] / (float)max_hval);
356 if (s->histogram_size <= 256) {
357 s->out->data[p][(i + starty) * s->out->linesize[p] + startx + s->x_pos] = value;
359 AV_WN16(s->out->data[p] + (i + starty) * s->out->linesize[p] + startx * 2 + s->x_pos * 2, value);
363 for (i = 0; i < s->histogram_size; i++) {
367 col_height = lrint(s->level_height * (1. - (log2(s->histogram[i] + 1) / max_hval_log)));
369 col_height = s->level_height - (s->histogram[i] * (int64_t)s->level_height + max_hval - 1) / max_hval;
371 if (s->histogram_size <= 256) {
372 for (j = s->level_height - 1; j >= col_height; j--) {
373 if (s->display_mode) {
374 for (l = 0; l < s->dncomp; l++)
375 out->data[l][(j + starty) * out->linesize[l] + startx + i] = s->fg_color[l];
377 out->data[p][(j + starty) * out->linesize[p] + startx + i] = 255;
380 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
381 out->data[p][(j + starty) * out->linesize[p] + startx + i] = i;
383 const int mult = s->mult;
385 for (j = s->level_height - 1; j >= col_height; j--) {
386 if (s->display_mode) {
387 for (l = 0; l < s->dncomp; l++)
388 AV_WN16(out->data[l] + (j + starty) * out->linesize[l] + startx * 2 + i * 2, s->fg_color[l] * mult);
390 AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, 255 * mult);
393 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
394 AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, i);
399 memset(s->histogram, 0, s->histogram_size * sizeof(unsigned));
405 if (s->x_pos >= s->width)
409 AVFrame *clone = av_frame_clone(out);
412 return AVERROR(ENOMEM);
413 return ff_filter_frame(outlink, clone);
415 return ff_filter_frame(outlink, out);
418 static const AVFilterPad inputs[] = {
421 .type = AVMEDIA_TYPE_VIDEO,
422 .filter_frame = filter_frame,
423 .config_props = config_input,
428 static const AVFilterPad outputs[] = {
431 .type = AVMEDIA_TYPE_VIDEO,
432 .config_props = config_output,
437 #if CONFIG_HISTOGRAM_FILTER
439 AVFilter ff_vf_histogram = {
441 .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
442 .priv_size = sizeof(HistogramContext),
443 .query_formats = query_formats,
446 .priv_class = &histogram_class,
449 #endif /* CONFIG_HISTOGRAM_FILTER */
451 #if CONFIG_THISTOGRAM_FILTER
453 static const AVOption thistogram_options[] = {
454 { "width", "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
455 { "w", "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
457 { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
458 { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
462 AVFILTER_DEFINE_CLASS(thistogram);
464 AVFilter ff_vf_thistogram = {
465 .name = "thistogram",
466 .description = NULL_IF_CONFIG_SMALL("Compute and draw a temporal histogram."),
467 .priv_size = sizeof(HistogramContext),
468 .query_formats = query_formats,
471 .priv_class = &thistogram_class,
474 #endif /* CONFIG_THISTOGRAM_FILTER */