blob: f04f5dea104f7b375504e52de68220cd132c98a4
1 | /* |
2 | * Copyright (c) 2012-2013 Paul B Mahol |
3 | * |
4 | * This file is part of FFmpeg. |
5 | * |
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. |
10 | * |
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. |
15 | * |
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 |
19 | */ |
20 | |
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" |
27 | #include "avfilter.h" |
28 | #include "formats.h" |
29 | #include "internal.h" |
30 | #include "video.h" |
31 | |
32 | typedef struct HistogramContext { |
33 | const AVClass *class; ///< AVClass context for log and options purpose |
34 | unsigned histogram[256*256]; |
35 | int histogram_size; |
36 | int mult; |
37 | int ncomp; |
38 | int dncomp; |
39 | uint8_t bg_color[4]; |
40 | uint8_t fg_color[4]; |
41 | int level_height; |
42 | int scale_height; |
43 | int display_mode; |
44 | int levels_mode; |
45 | const AVPixFmtDescriptor *desc, *odesc; |
46 | int components; |
47 | float fgopacity; |
48 | float bgopacity; |
49 | int planewidth[4]; |
50 | int planeheight[4]; |
51 | } HistogramContext; |
52 | |
53 | #define OFFSET(x) offsetof(HistogramContext, x) |
54 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
55 | |
56 | static const AVOption histogram_options[] = { |
57 | { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS}, |
58 | { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS}, |
59 | { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"}, |
60 | { "d", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"}, |
61 | { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" }, |
62 | { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" }, |
63 | { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, |
64 | { "m", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, |
65 | { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" }, |
66 | { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" }, |
67 | { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS}, |
68 | { "c", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS}, |
69 | { "fgopacity", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS}, |
70 | { "f", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS}, |
71 | { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS}, |
72 | { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS}, |
73 | { NULL } |
74 | }; |
75 | |
76 | AVFILTER_DEFINE_CLASS(histogram); |
77 | |
78 | static const enum AVPixelFormat levels_in_pix_fmts[] = { |
79 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, |
80 | AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, |
81 | AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P, |
82 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P, |
83 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, |
84 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
85 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, |
86 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
87 | AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, |
88 | AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, |
89 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP, |
90 | AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
91 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, |
92 | AV_PIX_FMT_GRAY8, |
93 | AV_PIX_FMT_NONE |
94 | }; |
95 | |
96 | static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = { |
97 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, |
98 | AV_PIX_FMT_NONE |
99 | }; |
100 | |
101 | static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = { |
102 | AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9, |
103 | AV_PIX_FMT_NONE |
104 | }; |
105 | |
106 | static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = { |
107 | AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10, |
108 | AV_PIX_FMT_NONE |
109 | }; |
110 | |
111 | static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = { |
112 | AV_PIX_FMT_YUV444P12, |
113 | AV_PIX_FMT_NONE |
114 | }; |
115 | |
116 | static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = { |
117 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP, |
118 | AV_PIX_FMT_NONE |
119 | }; |
120 | |
121 | static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = { |
122 | AV_PIX_FMT_GBRP9, |
123 | AV_PIX_FMT_NONE |
124 | }; |
125 | |
126 | static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = { |
127 | AV_PIX_FMT_GBRP10, |
128 | AV_PIX_FMT_NONE |
129 | }; |
130 | |
131 | static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = { |
132 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, |
133 | AV_PIX_FMT_NONE |
134 | }; |
135 | |
136 | static int query_formats(AVFilterContext *ctx) |
137 | { |
138 | AVFilterFormats *avff; |
139 | const AVPixFmtDescriptor *desc; |
140 | const enum AVPixelFormat *out_pix_fmts; |
141 | int rgb, i, bits; |
142 | int ret; |
143 | |
144 | if (!ctx->inputs[0]->in_formats || |
145 | !ctx->inputs[0]->in_formats->nb_formats) { |
146 | return AVERROR(EAGAIN); |
147 | } |
148 | |
149 | if (!ctx->inputs[0]->out_formats) |
150 | if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0) |
151 | return ret; |
152 | avff = ctx->inputs[0]->in_formats; |
153 | desc = av_pix_fmt_desc_get(avff->formats[0]); |
154 | rgb = desc->flags & AV_PIX_FMT_FLAG_RGB; |
155 | bits = desc->comp[0].depth; |
156 | for (i = 1; i < avff->nb_formats; i++) { |
157 | desc = av_pix_fmt_desc_get(avff->formats[i]); |
158 | if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) || |
159 | (bits != desc->comp[0].depth)) |
160 | return AVERROR(EAGAIN); |
161 | } |
162 | |
163 | if (rgb && bits == 8) |
164 | out_pix_fmts = levels_out_rgb8_pix_fmts; |
165 | else if (rgb && bits == 9) |
166 | out_pix_fmts = levels_out_rgb9_pix_fmts; |
167 | else if (rgb && bits == 10) |
168 | out_pix_fmts = levels_out_rgb10_pix_fmts; |
169 | else if (rgb && bits == 12) |
170 | out_pix_fmts = levels_out_rgb12_pix_fmts; |
171 | else if (bits == 8) |
172 | out_pix_fmts = levels_out_yuv8_pix_fmts; |
173 | else if (bits == 9) |
174 | out_pix_fmts = levels_out_yuv9_pix_fmts; |
175 | else if (bits == 10) |
176 | out_pix_fmts = levels_out_yuv10_pix_fmts; |
177 | else if (bits == 12) |
178 | out_pix_fmts = levels_out_yuv12_pix_fmts; |
179 | else |
180 | return AVERROR(EAGAIN); |
181 | if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0) |
182 | return ret; |
183 | |
184 | return 0; |
185 | } |
186 | |
187 | static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 }; |
188 | static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 }; |
189 | static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 }; |
190 | static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 }; |
191 | |
192 | static int config_input(AVFilterLink *inlink) |
193 | { |
194 | HistogramContext *h = inlink->dst->priv; |
195 | |
196 | h->desc = av_pix_fmt_desc_get(inlink->format); |
197 | h->ncomp = h->desc->nb_components; |
198 | h->histogram_size = 1 << h->desc->comp[0].depth; |
199 | h->mult = h->histogram_size / 256; |
200 | |
201 | switch (inlink->format) { |
202 | case AV_PIX_FMT_GBRP12: |
203 | case AV_PIX_FMT_GBRP10: |
204 | case AV_PIX_FMT_GBRP9: |
205 | case AV_PIX_FMT_GBRAP: |
206 | case AV_PIX_FMT_GBRP: |
207 | memcpy(h->bg_color, black_gbrp_color, 4); |
208 | memcpy(h->fg_color, white_gbrp_color, 4); |
209 | break; |
210 | default: |
211 | memcpy(h->bg_color, black_yuva_color, 4); |
212 | memcpy(h->fg_color, white_yuva_color, 4); |
213 | } |
214 | |
215 | h->fg_color[3] = h->fgopacity * 255; |
216 | h->bg_color[3] = h->bgopacity * 255; |
217 | |
218 | h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h); |
219 | h->planeheight[0] = h->planeheight[3] = inlink->h; |
220 | h->planewidth[1] = h->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w); |
221 | h->planewidth[0] = h->planewidth[3] = inlink->w; |
222 | |
223 | return 0; |
224 | } |
225 | |
226 | static int config_output(AVFilterLink *outlink) |
227 | { |
228 | AVFilterContext *ctx = outlink->src; |
229 | HistogramContext *h = ctx->priv; |
230 | int ncomp = 0, i; |
231 | |
232 | for (i = 0; i < h->ncomp; i++) { |
233 | if ((1 << i) & h->components) |
234 | ncomp++; |
235 | } |
236 | outlink->w = h->histogram_size; |
237 | outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * h->display_mode, 1); |
238 | |
239 | h->odesc = av_pix_fmt_desc_get(outlink->format); |
240 | h->dncomp = h->odesc->nb_components; |
241 | outlink->sample_aspect_ratio = (AVRational){1,1}; |
242 | |
243 | return 0; |
244 | } |
245 | |
246 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
247 | { |
248 | HistogramContext *h = inlink->dst->priv; |
249 | AVFilterContext *ctx = inlink->dst; |
250 | AVFilterLink *outlink = ctx->outputs[0]; |
251 | AVFrame *out; |
252 | int i, j, k, l, m; |
253 | |
254 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
255 | if (!out) { |
256 | av_frame_free(&in); |
257 | return AVERROR(ENOMEM); |
258 | } |
259 | |
260 | out->pts = in->pts; |
261 | |
262 | for (k = 0; k < 4 && out->data[k]; k++) { |
263 | const int is_chroma = (k == 1 || k == 2); |
264 | const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0)); |
265 | const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0)); |
266 | |
267 | if (h->histogram_size <= 256) { |
268 | for (i = 0; i < dst_h ; i++) |
269 | memset(out->data[h->odesc->comp[k].plane] + |
270 | i * out->linesize[h->odesc->comp[k].plane], |
271 | h->bg_color[k], dst_w); |
272 | } else { |
273 | const int mult = h->mult; |
274 | |
275 | for (i = 0; i < dst_h ; i++) |
276 | for (j = 0; j < dst_w; j++) |
277 | AV_WN16(out->data[h->odesc->comp[k].plane] + |
278 | i * out->linesize[h->odesc->comp[k].plane] + j * 2, |
279 | h->bg_color[k] * mult); |
280 | } |
281 | } |
282 | |
283 | for (m = 0, k = 0; k < h->ncomp; k++) { |
284 | const int p = h->desc->comp[k].plane; |
285 | const int height = h->planeheight[p]; |
286 | const int width = h->planewidth[p]; |
287 | double max_hval_log; |
288 | unsigned max_hval = 0; |
289 | int start; |
290 | |
291 | if (!((1 << k) & h->components)) |
292 | continue; |
293 | start = m++ * (h->level_height + h->scale_height) * h->display_mode; |
294 | |
295 | if (h->histogram_size <= 256) { |
296 | for (i = 0; i < height; i++) { |
297 | const uint8_t *src = in->data[p] + i * in->linesize[p]; |
298 | for (j = 0; j < width; j++) |
299 | h->histogram[src[j]]++; |
300 | } |
301 | } else { |
302 | for (i = 0; i < height; i++) { |
303 | const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]); |
304 | for (j = 0; j < width; j++) |
305 | h->histogram[src[j]]++; |
306 | } |
307 | } |
308 | |
309 | for (i = 0; i < h->histogram_size; i++) |
310 | max_hval = FFMAX(max_hval, h->histogram[i]); |
311 | max_hval_log = log2(max_hval + 1); |
312 | |
313 | for (i = 0; i < outlink->w; i++) { |
314 | int col_height; |
315 | |
316 | if (h->levels_mode) |
317 | col_height = lrint(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log))); |
318 | else |
319 | col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval; |
320 | |
321 | if (h->histogram_size <= 256) { |
322 | for (j = h->level_height - 1; j >= col_height; j--) { |
323 | if (h->display_mode) { |
324 | for (l = 0; l < h->dncomp; l++) |
325 | out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l]; |
326 | } else { |
327 | out->data[p][(j + start) * out->linesize[p] + i] = 255; |
328 | } |
329 | } |
330 | for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--) |
331 | out->data[p][(j + start) * out->linesize[p] + i] = i; |
332 | } else { |
333 | const int mult = h->mult; |
334 | |
335 | for (j = h->level_height - 1; j >= col_height; j--) { |
336 | if (h->display_mode) { |
337 | for (l = 0; l < h->dncomp; l++) |
338 | AV_WN16(out->data[l] + (j + start) * out->linesize[l] + i * 2, h->fg_color[l] * mult); |
339 | } else { |
340 | AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, 255 * mult); |
341 | } |
342 | } |
343 | for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--) |
344 | AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, i); |
345 | } |
346 | } |
347 | |
348 | memset(h->histogram, 0, h->histogram_size * sizeof(unsigned)); |
349 | } |
350 | |
351 | av_frame_free(&in); |
352 | return ff_filter_frame(outlink, out); |
353 | } |
354 | |
355 | static const AVFilterPad inputs[] = { |
356 | { |
357 | .name = "default", |
358 | .type = AVMEDIA_TYPE_VIDEO, |
359 | .filter_frame = filter_frame, |
360 | .config_props = config_input, |
361 | }, |
362 | { NULL } |
363 | }; |
364 | |
365 | static const AVFilterPad outputs[] = { |
366 | { |
367 | .name = "default", |
368 | .type = AVMEDIA_TYPE_VIDEO, |
369 | .config_props = config_output, |
370 | }, |
371 | { NULL } |
372 | }; |
373 | |
374 | AVFilter ff_vf_histogram = { |
375 | .name = "histogram", |
376 | .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."), |
377 | .priv_size = sizeof(HistogramContext), |
378 | .query_formats = query_formats, |
379 | .inputs = inputs, |
380 | .outputs = outputs, |
381 | .priv_class = &histogram_class, |
382 | }; |
383 |