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_YUVA422P12, AV_PIX_FMT_YUVA444P12,
99 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
100 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
101 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
106 static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
107 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
111 static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
112 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
116 static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
117 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
121 static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
122 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUV444P12,
126 static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
127 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
131 static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
136 static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
137 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
141 static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
142 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
146 static int query_formats(AVFilterContext *ctx)
148 AVFilterFormats *avff;
149 const AVPixFmtDescriptor *desc;
150 const enum AVPixelFormat *out_pix_fmts;
154 if (!ctx->inputs[0]->in_formats ||
155 !ctx->inputs[0]->in_formats->nb_formats) {
156 return AVERROR(EAGAIN);
159 if (!ctx->inputs[0]->out_formats)
160 if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
162 avff = ctx->inputs[0]->in_formats;
163 desc = av_pix_fmt_desc_get(avff->formats[0]);
164 rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
165 bits = desc->comp[0].depth;
166 for (i = 1; i < avff->nb_formats; i++) {
167 desc = av_pix_fmt_desc_get(avff->formats[i]);
168 if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
169 (bits != desc->comp[0].depth))
170 return AVERROR(EAGAIN);
173 if (rgb && bits == 8)
174 out_pix_fmts = levels_out_rgb8_pix_fmts;
175 else if (rgb && bits == 9)
176 out_pix_fmts = levels_out_rgb9_pix_fmts;
177 else if (rgb && bits == 10)
178 out_pix_fmts = levels_out_rgb10_pix_fmts;
179 else if (rgb && bits == 12)
180 out_pix_fmts = levels_out_rgb12_pix_fmts;
182 out_pix_fmts = levels_out_yuv8_pix_fmts;
184 out_pix_fmts = levels_out_yuv9_pix_fmts;
186 out_pix_fmts = levels_out_yuv10_pix_fmts;
188 out_pix_fmts = levels_out_yuv12_pix_fmts;
190 return AVERROR(EAGAIN);
191 if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
197 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
198 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
199 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
200 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
202 static int config_input(AVFilterLink *inlink)
204 HistogramContext *s = inlink->dst->priv;
206 s->desc = av_pix_fmt_desc_get(inlink->format);
207 s->ncomp = s->desc->nb_components;
208 s->histogram_size = 1 << s->desc->comp[0].depth;
209 s->mult = s->histogram_size / 256;
211 switch (inlink->format) {
212 case AV_PIX_FMT_GBRAP12:
213 case AV_PIX_FMT_GBRP12:
214 case AV_PIX_FMT_GBRAP10:
215 case AV_PIX_FMT_GBRP10:
216 case AV_PIX_FMT_GBRP9:
217 case AV_PIX_FMT_GBRAP:
218 case AV_PIX_FMT_GBRP:
219 memcpy(s->bg_color, black_gbrp_color, 4);
220 memcpy(s->fg_color, white_gbrp_color, 4);
221 s->start[0] = s->start[1] = s->start[2] = s->start[3] = 0;
224 memcpy(s->bg_color, black_yuva_color, 4);
225 memcpy(s->fg_color, white_yuva_color, 4);
226 s->start[0] = s->start[3] = 0;
227 s->start[1] = s->start[2] = s->histogram_size / 2;
230 s->fg_color[3] = s->fgopacity * 255;
231 s->bg_color[3] = s->bgopacity * 255;
233 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
234 s->planeheight[0] = s->planeheight[3] = inlink->h;
235 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
236 s->planewidth[0] = s->planewidth[3] = inlink->w;
241 static int config_output(AVFilterLink *outlink)
243 AVFilterContext *ctx = outlink->src;
244 HistogramContext *s = ctx->priv;
247 if (!strcmp(ctx->filter->name, "thistogram"))
250 for (i = 0; i < s->ncomp; i++) {
251 if ((1 << i) & s->components)
257 s->width = ctx->inputs[0]->w;
258 outlink->w = s->width * FFMAX(ncomp * (s->display_mode == 1), 1);
259 outlink->h = s->histogram_size * FFMAX(ncomp * (s->display_mode == 2), 1);
261 outlink->w = s->histogram_size * FFMAX(ncomp * (s->display_mode == 1), 1);
262 outlink->h = (s->level_height + s->scale_height) * FFMAX(ncomp * (s->display_mode == 2), 1);
265 s->odesc = av_pix_fmt_desc_get(outlink->format);
266 s->dncomp = s->odesc->nb_components;
267 outlink->sample_aspect_ratio = (AVRational){1,1};
272 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
274 HistogramContext *s = inlink->dst->priv;
275 AVFilterContext *ctx = inlink->dst;
276 AVFilterLink *outlink = ctx->outputs[0];
277 AVFrame *out = s->out;
280 if (!s->thistogram || !out) {
281 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
284 return AVERROR(ENOMEM);
288 for (k = 0; k < 4 && out->data[k]; k++) {
289 const int is_chroma = (k == 1 || k == 2);
290 const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? s->odesc->log2_chroma_h : 0));
291 const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? s->odesc->log2_chroma_w : 0));
293 if (s->histogram_size <= 256) {
294 for (i = 0; i < dst_h ; i++)
295 memset(out->data[s->odesc->comp[k].plane] +
296 i * out->linesize[s->odesc->comp[k].plane],
297 s->bg_color[k], dst_w);
299 const int mult = s->mult;
301 for (i = 0; i < dst_h ; i++)
302 for (j = 0; j < dst_w; j++)
303 AV_WN16(out->data[s->odesc->comp[k].plane] +
304 i * out->linesize[s->odesc->comp[k].plane] + j * 2,
305 s->bg_color[k] * mult);
310 for (m = 0, k = 0; k < s->ncomp; k++) {
311 const int p = s->desc->comp[k].plane;
312 const int max_value = s->histogram_size - 1 - s->start[p];
313 const int height = s->planeheight[p];
314 const int width = s->planewidth[p];
316 unsigned max_hval = 0;
319 if (!((1 << k) & s->components))
322 starty = m * s->histogram_size * (s->display_mode == 2);
323 startx = m++ * s->width * (s->display_mode == 1);
325 startx = m * s->histogram_size * (s->display_mode == 1);
326 starty = m++ * (s->level_height + s->scale_height) * (s->display_mode == 2);
329 if (s->histogram_size <= 256) {
330 for (i = 0; i < height; i++) {
331 const uint8_t *src = in->data[p] + i * in->linesize[p];
332 for (j = 0; j < width; j++)
333 s->histogram[src[j]]++;
336 for (i = 0; i < height; i++) {
337 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
338 for (j = 0; j < width; j++)
339 s->histogram[src[j]]++;
343 for (i = 0; i < s->histogram_size; i++)
344 max_hval = FFMAX(max_hval, s->histogram[i]);
345 max_hval_log = log2(max_hval + 1);
348 for (int i = 0; i < s->histogram_size; i++) {
349 int idx = s->histogram_size - i - 1;
350 int value = s->start[p];
353 value += lrint(max_value * (log2(s->histogram[idx] + 1) / max_hval_log));
355 value += lrint(max_value * s->histogram[idx] / (float)max_hval);
357 if (s->histogram_size <= 256) {
358 s->out->data[p][(i + starty) * s->out->linesize[p] + startx + s->x_pos] = value;
360 AV_WN16(s->out->data[p] + (i + starty) * s->out->linesize[p] + startx * 2 + s->x_pos * 2, value);
364 for (i = 0; i < s->histogram_size; i++) {
368 col_height = lrint(s->level_height * (1. - (log2(s->histogram[i] + 1) / max_hval_log)));
370 col_height = s->level_height - (s->histogram[i] * (int64_t)s->level_height + max_hval - 1) / max_hval;
372 if (s->histogram_size <= 256) {
373 for (j = s->level_height - 1; j >= col_height; j--) {
374 if (s->display_mode) {
375 for (l = 0; l < s->dncomp; l++)
376 out->data[l][(j + starty) * out->linesize[l] + startx + i] = s->fg_color[l];
378 out->data[p][(j + starty) * out->linesize[p] + startx + i] = 255;
381 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
382 out->data[p][(j + starty) * out->linesize[p] + startx + i] = i;
384 const int mult = s->mult;
386 for (j = s->level_height - 1; j >= col_height; j--) {
387 if (s->display_mode) {
388 for (l = 0; l < s->dncomp; l++)
389 AV_WN16(out->data[l] + (j + starty) * out->linesize[l] + startx * 2 + i * 2, s->fg_color[l] * mult);
391 AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, 255 * mult);
394 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
395 AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, i);
400 memset(s->histogram, 0, s->histogram_size * sizeof(unsigned));
406 if (s->x_pos >= s->width)
410 AVFrame *clone = av_frame_clone(out);
413 return AVERROR(ENOMEM);
414 return ff_filter_frame(outlink, clone);
416 return ff_filter_frame(outlink, out);
419 static const AVFilterPad inputs[] = {
422 .type = AVMEDIA_TYPE_VIDEO,
423 .filter_frame = filter_frame,
424 .config_props = config_input,
429 static const AVFilterPad outputs[] = {
432 .type = AVMEDIA_TYPE_VIDEO,
433 .config_props = config_output,
438 #if CONFIG_HISTOGRAM_FILTER
440 AVFilter ff_vf_histogram = {
442 .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
443 .priv_size = sizeof(HistogramContext),
444 .query_formats = query_formats,
447 .priv_class = &histogram_class,
450 #endif /* CONFIG_HISTOGRAM_FILTER */
452 #if CONFIG_THISTOGRAM_FILTER
454 static const AVOption thistogram_options[] = {
455 { "width", "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
456 { "w", "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
458 { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
459 { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
463 AVFILTER_DEFINE_CLASS(thistogram);
465 AVFilter ff_vf_thistogram = {
466 .name = "thistogram",
467 .description = NULL_IF_CONFIG_SMALL("Compute and draw a temporal histogram."),
468 .priv_size = sizeof(HistogramContext),
469 .query_formats = query_formats,
472 .priv_class = &thistogram_class,
475 #endif /* CONFIG_THISTOGRAM_FILTER */