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