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