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"
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
34 unsigned histogram[256*256];
38 const uint8_t *bg_color;
39 const uint8_t *fg_color;
44 const AVPixFmtDescriptor *desc, *odesc;
50 #define OFFSET(x) offsetof(HistogramContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static const AVOption histogram_options[] = {
54 { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
55 { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
56 { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
57 { "d", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
58 { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
59 { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
60 { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
61 { "m", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
62 { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
63 { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
64 { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
65 { "c", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
69 AVFILTER_DEFINE_CLASS(histogram);
71 static const enum AVPixelFormat levels_in_pix_fmts[] = {
72 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
73 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
74 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
75 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
76 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
77 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
78 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
79 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
80 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
81 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
82 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
83 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
84 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
89 static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
90 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
94 static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
95 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
99 static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
100 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
104 static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
105 AV_PIX_FMT_YUV444P12,
109 static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
110 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
114 static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
119 static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
124 static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
125 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
129 static int query_formats(AVFilterContext *ctx)
131 AVFilterFormats *avff;
132 const AVPixFmtDescriptor *desc;
133 const enum AVPixelFormat *out_pix_fmts;
137 if (!ctx->inputs[0]->in_formats ||
138 !ctx->inputs[0]->in_formats->nb_formats) {
139 return AVERROR(EAGAIN);
142 if (!ctx->inputs[0]->out_formats)
143 if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
145 avff = ctx->inputs[0]->in_formats;
146 desc = av_pix_fmt_desc_get(avff->formats[0]);
147 rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
148 bits = desc->comp[0].depth;
149 for (i = 1; i < avff->nb_formats; i++) {
150 desc = av_pix_fmt_desc_get(avff->formats[i]);
151 if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
152 (bits != desc->comp[0].depth))
153 return AVERROR(EAGAIN);
156 if (rgb && bits == 8)
157 out_pix_fmts = levels_out_rgb8_pix_fmts;
158 else if (rgb && bits == 9)
159 out_pix_fmts = levels_out_rgb9_pix_fmts;
160 else if (rgb && bits == 10)
161 out_pix_fmts = levels_out_rgb10_pix_fmts;
162 else if (rgb && bits == 12)
163 out_pix_fmts = levels_out_rgb12_pix_fmts;
165 out_pix_fmts = levels_out_yuv8_pix_fmts;
167 out_pix_fmts = levels_out_yuv9_pix_fmts;
169 out_pix_fmts = levels_out_yuv10_pix_fmts;
171 out_pix_fmts = levels_out_yuv12_pix_fmts;
173 return AVERROR(EAGAIN);
174 if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
180 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
181 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
182 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
183 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
185 static int config_input(AVFilterLink *inlink)
187 HistogramContext *h = inlink->dst->priv;
189 h->desc = av_pix_fmt_desc_get(inlink->format);
190 h->ncomp = h->desc->nb_components;
191 h->histogram_size = 1 << h->desc->comp[0].depth;
192 h->mult = h->histogram_size / 256;
194 switch (inlink->format) {
195 case AV_PIX_FMT_GBRP12:
196 case AV_PIX_FMT_GBRP10:
197 case AV_PIX_FMT_GBRP9:
198 case AV_PIX_FMT_GBRAP:
199 case AV_PIX_FMT_GBRP:
200 h->bg_color = black_gbrp_color;
201 h->fg_color = white_gbrp_color;
204 h->bg_color = black_yuva_color;
205 h->fg_color = white_yuva_color;
208 h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
209 h->planeheight[0] = h->planeheight[3] = inlink->h;
210 h->planewidth[1] = h->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
211 h->planewidth[0] = h->planewidth[3] = inlink->w;
216 static int config_output(AVFilterLink *outlink)
218 AVFilterContext *ctx = outlink->src;
219 HistogramContext *h = ctx->priv;
222 for (i = 0; i < h->ncomp; i++) {
223 if ((1 << i) & h->components)
226 outlink->w = h->histogram_size;
227 outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * h->display_mode, 1);
229 h->odesc = av_pix_fmt_desc_get(outlink->format);
230 outlink->sample_aspect_ratio = (AVRational){1,1};
235 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
237 HistogramContext *h = inlink->dst->priv;
238 AVFilterContext *ctx = inlink->dst;
239 AVFilterLink *outlink = ctx->outputs[0];
243 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
246 return AVERROR(ENOMEM);
251 for (k = 0; k < 4 && out->data[k]; k++) {
252 const int is_chroma = (k == 1 || k == 2);
253 const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
254 const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
256 if (h->histogram_size <= 256) {
257 for (i = 0; i < dst_h ; i++)
258 memset(out->data[h->odesc->comp[k].plane] +
259 i * out->linesize[h->odesc->comp[k].plane],
260 h->bg_color[k], dst_w);
262 const int mult = h->mult;
264 for (i = 0; i < dst_h ; i++)
265 for (j = 0; j < dst_w; j++)
266 AV_WN16(out->data[h->odesc->comp[k].plane] +
267 i * out->linesize[h->odesc->comp[k].plane] + j * 2,
268 h->bg_color[k] * mult);
272 for (m = 0, k = 0; k < h->ncomp; k++) {
273 const int p = h->desc->comp[k].plane;
274 const int height = h->planeheight[p];
275 const int width = h->planewidth[p];
277 unsigned max_hval = 0;
280 if (!((1 << k) & h->components))
282 start = m++ * (h->level_height + h->scale_height) * h->display_mode;
284 if (h->histogram_size <= 256) {
285 for (i = 0; i < height; i++) {
286 const uint8_t *src = in->data[p] + i * in->linesize[p];
287 for (j = 0; j < width; j++)
288 h->histogram[src[j]]++;
291 for (i = 0; i < height; i++) {
292 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
293 for (j = 0; j < width; j++)
294 h->histogram[src[j]]++;
298 for (i = 0; i < h->histogram_size; i++)
299 max_hval = FFMAX(max_hval, h->histogram[i]);
300 max_hval_log = log2(max_hval + 1);
302 for (i = 0; i < outlink->w; i++) {
306 col_height = lrint(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
308 col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
310 if (h->histogram_size <= 256) {
311 for (j = h->level_height - 1; j >= col_height; j--) {
312 if (h->display_mode) {
313 for (l = 0; l < h->ncomp; l++)
314 out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
316 out->data[p][(j + start) * out->linesize[p] + i] = 255;
319 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
320 out->data[p][(j + start) * out->linesize[p] + i] = i;
322 const int mult = h->mult;
324 for (j = h->level_height - 1; j >= col_height; j--) {
325 if (h->display_mode) {
326 for (l = 0; l < h->ncomp; l++)
327 AV_WN16(out->data[l] + (j + start) * out->linesize[l] + i * 2, h->fg_color[l] * mult);
329 AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, 255 * mult);
332 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
333 AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, i);
337 memset(h->histogram, 0, h->histogram_size * sizeof(unsigned));
341 return ff_filter_frame(outlink, out);
344 static const AVFilterPad inputs[] = {
347 .type = AVMEDIA_TYPE_VIDEO,
348 .filter_frame = filter_frame,
349 .config_props = config_input,
354 static const AVFilterPad outputs[] = {
357 .type = AVMEDIA_TYPE_VIDEO,
358 .config_props = config_output,
363 AVFilter ff_vf_histogram = {
365 .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
366 .priv_size = sizeof(HistogramContext),
367 .query_formats = query_formats,
370 .priv_class = &histogram_class,