]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_histogram.c
Merge commit '9d4da474f5f40b019cb4cb931c8499deee586174'
[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 "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29
30 enum HistogramMode {
31     MODE_LEVELS,
32     MODE_WAVEFORM,
33     MODE_COLOR,
34     MODE_COLOR2,
35     MODE_NB
36 };
37
38 typedef struct HistogramContext {
39     const AVClass *class;               ///< AVClass context for log and options purpose
40     enum HistogramMode mode;
41     unsigned       histogram[256];
42     unsigned       max_hval;
43     int            ncomp;
44     const uint8_t  *bg_color;
45     const uint8_t  *fg_color;
46     int            level_height;
47     int            scale_height;
48     int            step;
49     int            waveform_mode;
50     int            display_mode;
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     { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
58     { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
59     { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
60     { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
61     { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
62     { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
63     { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
64     { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
65     { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
66     { "row",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
67     { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
68     { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
69     { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
70     { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
71     { NULL },
72 };
73
74 AVFILTER_DEFINE_CLASS(histogram);
75
76 static av_cold int init(AVFilterContext *ctx, const char *args)
77 {
78     HistogramContext *h = ctx->priv;
79     int ret;
80
81     h->class = &histogram_class;
82     av_opt_set_defaults(h);
83
84     if ((ret = (av_set_options_string(h, args, "=", ":"))) < 0)
85         return ret;
86
87     return 0;
88 }
89
90 static const enum AVPixelFormat color_pix_fmts[] = {
91     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
92     AV_PIX_FMT_NONE
93 };
94
95 static const enum AVPixelFormat levels_pix_fmts[] = {
96     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
97     AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE
98 };
99
100 static int query_formats(AVFilterContext *ctx)
101 {
102     HistogramContext *h = ctx->priv;
103     const enum AVPixelFormat *pix_fmts;
104
105     switch (h->mode) {
106     case MODE_WAVEFORM:
107     case MODE_LEVELS:
108         pix_fmts = levels_pix_fmts;
109         break;
110     case MODE_COLOR:
111     case MODE_COLOR2:
112         pix_fmts = color_pix_fmts;
113         break;
114     default:
115         av_assert0(0);
116     }
117
118     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
119
120     return 0;
121 }
122
123 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
124 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
125 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
126 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
127
128 static int config_input(AVFilterLink *inlink)
129 {
130     HistogramContext *h = inlink->dst->priv;
131     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
132
133     h->ncomp = desc->nb_components;
134
135     switch (inlink->format) {
136     case AV_PIX_FMT_GBRP:
137         h->bg_color = black_gbrp_color;
138         h->fg_color = white_gbrp_color;
139         break;
140     default:
141         h->bg_color = black_yuva_color;
142         h->fg_color = white_yuva_color;
143     }
144
145     return 0;
146 }
147
148 static int config_output(AVFilterLink *outlink)
149 {
150     AVFilterContext *ctx = outlink->src;
151     HistogramContext *h = ctx->priv;
152
153     switch (h->mode) {
154     case MODE_LEVELS:
155         outlink->w = 256;
156         outlink->h = (h->level_height + h->scale_height) * FFMAX(h->ncomp * h->display_mode, 1);
157         break;
158     case MODE_WAVEFORM:
159         if (h->waveform_mode)
160             outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
161         else
162             outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
163         break;
164     case MODE_COLOR:
165     case MODE_COLOR2:
166         outlink->h = outlink->w = 256;
167         break;
168     default:
169         av_assert0(0);
170     }
171
172     outlink->sample_aspect_ratio = (AVRational){1,1};
173
174     return 0;
175 }
176
177 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
178 {
179     HistogramContext *h   = inlink->dst->priv;
180     AVFilterContext *ctx  = inlink->dst;
181     AVFilterLink *outlink = ctx->outputs[0];
182     AVFilterBufferRef *out;
183     const uint8_t *src;
184     uint8_t *dst;
185     int i, j, k, l, ret;
186
187     out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
188     if (!out) {
189         avfilter_unref_bufferp(&in);
190         return AVERROR(ENOMEM);
191     }
192
193     out->pts = in->pts;
194     out->pos = in->pos;
195
196     for (k = 0; k < h->ncomp; k++)
197         for (i = 0; i < outlink->h; i++)
198             memset(out->data[k] + i * out->linesize[k], h->bg_color[k], outlink->w);
199
200     switch (h->mode) {
201     case MODE_LEVELS:
202         for (k = 0; k < h->ncomp; k++) {
203             int start = k * (h->level_height + h->scale_height) * h->display_mode;
204
205             for (i = 0; i < in->video->h; i++) {
206                 src = in->data[k] + i * in->linesize[k];
207                 for (j = 0; j < in->video->w; j++)
208                     h->histogram[src[j]]++;
209             }
210
211             for (i = 0; i < 256; i++)
212                 h->max_hval = FFMAX(h->max_hval, h->histogram[i]);
213
214             for (i = 0; i < outlink->w; i++) {
215                 int col_height = h->level_height - (float)h->histogram[i] / h->max_hval * h->level_height;
216
217                 for (j = h->level_height - 1; j >= col_height; j--) {
218                     if (h->display_mode) {
219                         for (l = 0; l < h->ncomp; l++)
220                             out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
221                     } else {
222                         out->data[k][(j + start) * out->linesize[k] + i] = 255;
223                     }
224                 }
225                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
226                     out->data[k][(j + start) * out->linesize[k] + i] = i;
227             }
228
229             memset(h->histogram, 0, 256 * sizeof(unsigned));
230             h->max_hval = 0;
231         }
232         break;
233     case MODE_WAVEFORM:
234         if (h->waveform_mode) {
235             for (k = 0; k < h->ncomp; k++) {
236                 int offset = k * 256 * h->display_mode;
237                 for (i = 0; i < inlink->w; i++) {
238                     for (j = 0; j < inlink->h; j++) {
239                         int pos = (offset +
240                                    in->data[k][j * in->linesize[k] + i]) *
241                                   out->linesize[k] + i;
242                         unsigned value = out->data[k][pos];
243                         value = FFMIN(value + h->step, 255);
244                         out->data[k][pos] = value;
245                     }
246                 }
247             }
248         } else {
249             for (k = 0; k < h->ncomp; k++) {
250                 int offset = k * 256 * h->display_mode;
251                 for (i = 0; i < inlink->h; i++) {
252                     src = in ->data[k] + i * in ->linesize[k];
253                     dst = out->data[k] + i * out->linesize[k];
254                     for (j = 0; j < inlink->w; j++) {
255                         int pos = src[j] + offset;
256                         unsigned value = dst[pos];
257                         value = FFMIN(value + h->step, 255);
258                         dst[pos] = value;
259                     }
260                 }
261             }
262         }
263         break;
264     case MODE_COLOR:
265         for (i = 0; i < inlink->h; i++) {
266             int iw1 = i * in->linesize[1];
267             int iw2 = i * in->linesize[2];
268             for (j = 0; j < inlink->w; j++) {
269                 int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
270                 if (out->data[0][pos] < 255)
271                     out->data[0][pos]++;
272             }
273         }
274         for (i = 0; i < 256; i++) {
275             dst = out->data[0] + i * out->linesize[0];
276             for (j = 0; j < 256; j++) {
277                 if (!dst[j]) {
278                     out->data[1][i * out->linesize[0] + j] = i;
279                     out->data[2][i * out->linesize[0] + j] = j;
280                 }
281             }
282         }
283         break;
284     case MODE_COLOR2:
285         for (i = 0; i < inlink->h; i++) {
286             int iw1 = i * in->linesize[1];
287             int iw2 = i * in->linesize[2];
288             for (j = 0; j < inlink->w; j++) {
289                 int u = in->data[1][iw1 + j];
290                 int v = in->data[2][iw2 + j];
291                 int pos = u * out->linesize[0] + v;
292                 if (!out->data[0][pos])
293                     out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
294                 out->data[1][pos] = u;
295                 out->data[2][pos] = v;
296             }
297         }
298         break;
299     default:
300         av_assert0(0);
301     }
302
303     ret = ff_filter_frame(outlink, out);
304     avfilter_unref_bufferp(&in);
305     if (ret < 0)
306         return ret;
307     return 0;
308 }
309
310 static av_cold void uninit(AVFilterContext *ctx)
311 {
312     HistogramContext *h = ctx->priv;
313
314     av_opt_free(h);
315 }
316
317 static const AVFilterPad inputs[] = {
318     {
319         .name         = "default",
320         .type         = AVMEDIA_TYPE_VIDEO,
321         .filter_frame = filter_frame,
322         .config_props = config_input,
323         .min_perms    = AV_PERM_READ,
324     },
325     { NULL }
326 };
327
328 static const AVFilterPad outputs[] = {
329     {
330         .name         = "default",
331         .type         = AVMEDIA_TYPE_VIDEO,
332         .config_props = config_output,
333     },
334     { NULL }
335 };
336
337 AVFilter avfilter_vf_histogram = {
338     .name          = "histogram",
339     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
340     .priv_size     = sizeof(HistogramContext),
341     .init          = init,
342     .uninit        = uninit,
343     .query_formats = query_formats,
344     .inputs        = inputs,
345     .outputs       = outputs,
346     .priv_class    = &histogram_class,
347 };