Browse code

histogram filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2012/12/22 03:50:13
Showing 6 changed files
... ...
@@ -13,6 +13,7 @@ version <next>:
13 13
 - allpass, bass, bandpass, bandreject, biquad, equalizer, highpass, lowpass
14 14
   and treble audio filter
15 15
 - improved showspectrum filter, with multichannel support and sox-like colors
16
+- histogram filter
16 17
 
17 18
 
18 19
 version 1.1:
... ...
@@ -3098,6 +3098,89 @@ the histogram. Possible values are @code{none}, @code{weak} or
3098 3098
 @code{strong}. It defaults to @code{none}.
3099 3099
 @end table
3100 3100
 
3101
+@section histogram
3102
+
3103
+Compute and draw a color distribution histogram for the input video.
3104
+
3105
+The computed histogram is a representation of distribution of color components
3106
+in an image.
3107
+
3108
+The filter accepts the following named parameters:
3109
+
3110
+@table @option
3111
+@item mode
3112
+Set histogram mode.
3113
+
3114
+It accepts the following values:
3115
+@table @samp
3116
+@item levels
3117
+standard histogram that display color components distribution in an image.
3118
+Displays color graph for each color component. Shows distribution
3119
+of the Y, U, V, A or G, B, R components, depending on input format,
3120
+in current frame. Bellow each graph is color component scale meter.
3121
+
3122
+@item color
3123
+chroma values in vectorscope, if brighter more such chroma values are
3124
+distributed in an image.
3125
+Displays chroma values (U/V color placement) in two dimensional graph
3126
+(which is called a vectorscope). It can be used to read of the hue and
3127
+saturation of the current frame. At a same time it is a histogram.
3128
+The whiter a pixel in the vectorscope, the more pixels of the input frame
3129
+correspond to that pixel (that is the more pixels have this chroma value).
3130
+The V component is displayed on the horizontal (X) axis, with the leftmost
3131
+side being V = 0 and the rightmost side being V = 255.
3132
+The U component is displayed on the vertical (Y) axis, with the top
3133
+representing U = 0 and the bottom representing U = 255.
3134
+
3135
+The position of a white pixel in the graph corresponds to the chroma value
3136
+of a pixel of the input clip. So the graph can be used to read of the
3137
+hue (color flavor) and the saturation (the dominance of the hue in the color).
3138
+As the hue of a color changes, it moves around the square. At the center of
3139
+the square, the saturation is zero, which means that the corresponding pixel
3140
+has no color. If you increase the amount of a specific color, while leaving
3141
+the other colors unchanged, the saturation increases, and you move towards
3142
+the edge of the square.
3143
+
3144
+@item color2
3145
+chroma values in vectorscope, similar as @code{color} but actual chroma values
3146
+are displayed.
3147
+
3148
+@item waveform
3149
+per row/column luminance graph. In row mode the left side represents luma = 0
3150
+and right side represents luma = 255. In column mode top side represends
3151
+luma = 0 and bottom side represents luma = 255.
3152
+@end table
3153
+Default value is @code{levels}.
3154
+
3155
+@item level_height
3156
+Set height of level in @code{levels}. Default value is @code{200}.
3157
+Allowed range is [50, 2048].
3158
+
3159
+@item scale_height
3160
+Set height of color scale in @code{levels}. Default value is @code{12}.
3161
+Allowed range is [0, 40].
3162
+
3163
+@item step
3164
+Set step for @code{waveform} mode. Smaller values are useful to find out how much
3165
+of same luminance values across input rows/columns are distributed.
3166
+Default value is @code{10}. Allowed range is [1, 255].
3167
+
3168
+@item waveform_mode
3169
+Set mode for @code{waveform}. Can be either @code{row}, or @code{column}.
3170
+Default is @code{row}.
3171
+
3172
+@subsection Examples
3173
+
3174
+@itemize
3175
+
3176
+@item
3177
+Calculate and draw histogram:
3178
+@example
3179
+ffplay -i input -vf histogram
3180
+@end example
3181
+
3182
+@end itemize
3183
+
3101 3184
 @section hqdn3d
3102 3185
 
3103 3186
 High precision/quality 3d denoise filter. This filter aims to reduce
... ...
@@ -122,6 +122,7 @@ OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
122 122
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
123 123
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
124 124
 OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
125
+OBJS-$(CONFIG_HISTOGRAM_FILTER)              += vf_histogram.o
125 126
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
126 127
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
127 128
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
... ...
@@ -116,6 +116,7 @@ void avfilter_register_all(void)
116 116
     REGISTER_FILTER(GRADFUN,        gradfun,        vf);
117 117
     REGISTER_FILTER(HFLIP,          hflip,          vf);
118 118
     REGISTER_FILTER(HISTEQ,         histeq,         vf);
119
+    REGISTER_FILTER(HISTOGRAM,      histogram,      vf);
119 120
     REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
120 121
     REGISTER_FILTER(HUE,            hue,            vf);
121 122
     REGISTER_FILTER(IDET,           idet,           vf);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  35
33
-#define LIBAVFILTER_VERSION_MICRO 101
32
+#define LIBAVFILTER_VERSION_MINOR  36
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,329 @@
0
+/*
1
+ * Copyright (c) 2012-2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/avassert.h"
21
+#include "libavutil/opt.h"
22
+#include "libavutil/parseutils.h"
23
+#include "libavutil/pixdesc.h"
24
+#include "avfilter.h"
25
+#include "formats.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+enum HistogramMode {
30
+    MODE_LEVELS,
31
+    MODE_WAVEFORM,
32
+    MODE_COLOR,
33
+    MODE_COLOR2,
34
+    MODE_NB
35
+};
36
+
37
+typedef struct HistogramContext {
38
+    const AVClass *class;               ///< AVClass context for log and options purpose
39
+    enum HistogramMode mode;
40
+    unsigned       histogram[256];
41
+    unsigned       max_hval;
42
+    int            ncomp;
43
+    const uint8_t  *bg_color;
44
+    const uint8_t  *fg_color;
45
+    int            level_height;
46
+    int            scale_height;
47
+    int            step;
48
+    int            waveform_mode;
49
+} HistogramContext;
50
+
51
+#define OFFSET(x) offsetof(HistogramContext, x)
52
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53
+
54
+static const AVOption histogram_options[] = {
55
+    { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
56
+    { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
57
+    { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
58
+    { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
59
+    { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
60
+    { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
61
+    { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
62
+    { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
63
+    { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
64
+    { "row",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
65
+    { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
66
+    { NULL },
67
+};
68
+
69
+AVFILTER_DEFINE_CLASS(histogram);
70
+
71
+static av_cold int init(AVFilterContext *ctx, const char *args)
72
+{
73
+    HistogramContext *h = ctx->priv;
74
+    int ret;
75
+
76
+    h->class = &histogram_class;
77
+    av_opt_set_defaults(h);
78
+
79
+    if ((ret = (av_set_options_string(h, args, "=", ":"))) < 0)
80
+        return ret;
81
+
82
+    return 0;
83
+}
84
+
85
+static const enum AVPixelFormat color_pix_fmts[] = {
86
+    AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
87
+    AV_PIX_FMT_NONE
88
+};
89
+
90
+static const enum AVPixelFormat levels_pix_fmts[] = {
91
+    AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
92
+    AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE
93
+};
94
+
95
+static int query_formats(AVFilterContext *ctx)
96
+{
97
+    HistogramContext *h = ctx->priv;
98
+    const enum AVPixelFormat *pix_fmts;
99
+
100
+    switch (h->mode) {
101
+    case MODE_LEVELS:
102
+        pix_fmts = levels_pix_fmts;
103
+        break;
104
+    case MODE_WAVEFORM:
105
+    case MODE_COLOR:
106
+    case MODE_COLOR2:
107
+        pix_fmts = color_pix_fmts;
108
+        break;
109
+    default:
110
+        av_assert0(0);
111
+    }
112
+
113
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
114
+
115
+    return 0;
116
+}
117
+
118
+static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
119
+static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
120
+static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
121
+static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
122
+
123
+static int config_input(AVFilterLink *inlink)
124
+{
125
+    HistogramContext *h = inlink->dst->priv;
126
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
127
+
128
+    h->ncomp = desc->nb_components;
129
+
130
+    switch (inlink->format) {
131
+    case AV_PIX_FMT_GBRP:
132
+        h->bg_color = black_gbrp_color;
133
+        h->fg_color = white_gbrp_color;
134
+        break;
135
+    default:
136
+        h->bg_color = black_yuva_color;
137
+        h->fg_color = white_yuva_color;
138
+    }
139
+
140
+    return 0;
141
+}
142
+
143
+static int config_output(AVFilterLink *outlink)
144
+{
145
+    AVFilterContext *ctx = outlink->src;
146
+    HistogramContext *h = ctx->priv;
147
+
148
+    switch (h->mode) {
149
+    case MODE_LEVELS:
150
+        outlink->w = 256;
151
+        outlink->h = (h->level_height + h->scale_height) * h->ncomp;
152
+        break;
153
+    case MODE_WAVEFORM:
154
+        if (h->waveform_mode)
155
+            outlink->h = 256;
156
+        else
157
+            outlink->w = 256;
158
+        break;
159
+    case MODE_COLOR:
160
+    case MODE_COLOR2:
161
+        outlink->h = outlink->w = 256;
162
+        break;
163
+    default:
164
+        av_assert0(0);
165
+    }
166
+
167
+    outlink->sample_aspect_ratio = (AVRational){1,1};
168
+
169
+    return 0;
170
+}
171
+
172
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
173
+{
174
+    HistogramContext *h   = inlink->dst->priv;
175
+    AVFilterContext *ctx  = inlink->dst;
176
+    AVFilterLink *outlink = ctx->outputs[0];
177
+    AVFilterBufferRef *out;
178
+    const uint8_t *src;
179
+    uint8_t *dst;
180
+    int i, j, k, l, ret;
181
+
182
+    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
183
+    if (!out) {
184
+        avfilter_unref_bufferp(&in);
185
+        return AVERROR(ENOMEM);
186
+    }
187
+
188
+    out->pts = in->pts;
189
+    out->pos = in->pos;
190
+
191
+    for (k = 0; k < h->ncomp; k++)
192
+        for (i = 0; i < outlink->h; i++)
193
+            memset(out->data[k] + i * out->linesize[k], h->bg_color[k], outlink->w);
194
+
195
+    switch (h->mode) {
196
+    case MODE_LEVELS:
197
+        for (k = 0; k < h->ncomp; k++) {
198
+            for (i = 0; i < in->video->h; i++) {
199
+                src = in->data[k] + i * in->linesize[k];
200
+                for (j = 0; j < in->video->w; j++)
201
+                    h->histogram[src[j]]++;
202
+            }
203
+
204
+            for (i = 0; i < 256; i++)
205
+                h->max_hval = FFMAX(h->max_hval, h->histogram[i]);
206
+
207
+            int start = k * (h->level_height + h->scale_height);
208
+            for (i = 0; i < outlink->w; i++) {
209
+                int col_height = h->level_height - (float)h->histogram[i] / h->max_hval * h->level_height;
210
+
211
+                for (j = h->level_height - 1; j >= col_height; j--)
212
+                    for (l = 0; l < h->ncomp; l++)
213
+                        out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
214
+                for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
215
+                    out->data[k][(j + start) * out->linesize[k] + i] = i;
216
+            }
217
+
218
+            memset(h->histogram, 0, 256 * sizeof(unsigned));
219
+            h->max_hval = 0;
220
+        }
221
+        break;
222
+    case MODE_WAVEFORM:
223
+        if (h->waveform_mode) {
224
+            for (i = 0; i < inlink->w; i++) {
225
+                for (j = 0; j < inlink->h; j++) {
226
+                    int pos = in->data[0][j * in->linesize[0] + i] * out->linesize[0] + i;
227
+                    unsigned value = out->data[0][pos];
228
+                    value = FFMIN(value + h->step, 255);
229
+                    out->data[0][pos] = value;
230
+                }
231
+            }
232
+        } else {
233
+            for (i = 0; i < inlink->h; i++) {
234
+                src = in ->data[0] + i * in ->linesize[0];
235
+                dst = out->data[0] + i * out->linesize[0];
236
+                for (j = 0; j < inlink->w; j++) {
237
+                    int pos = src[j];
238
+                    unsigned value = dst[pos];
239
+                    value = FFMIN(value + h->step, 255);
240
+                    dst[pos] = value;
241
+                }
242
+            }
243
+        }
244
+        break;
245
+    case MODE_COLOR:
246
+        for (i = 0; i < inlink->h; i++) {
247
+            int iw1 = i * in->linesize[1];
248
+            int iw2 = i * in->linesize[2];
249
+            for (j = 0; j < inlink->w; j++) {
250
+                int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
251
+                if (out->data[0][pos] < 255)
252
+                    out->data[0][pos]++;
253
+            }
254
+        }
255
+        for (i = 0; i < 256; i++) {
256
+            dst = out->data[0] + i * out->linesize[0];
257
+            for (j = 0; j < 256; j++) {
258
+                if (!dst[j]) {
259
+                    out->data[1][i * out->linesize[0] + j] = i;
260
+                    out->data[2][i * out->linesize[0] + j] = j;
261
+                }
262
+            }
263
+        }
264
+        break;
265
+    case MODE_COLOR2:
266
+        for (i = 0; i < inlink->h; i++) {
267
+            int iw1 = i * in->linesize[1];
268
+            int iw2 = i * in->linesize[2];
269
+            for (j = 0; j < inlink->w; j++) {
270
+                int u = in->data[1][iw1 + j];
271
+                int v = in->data[2][iw2 + j];
272
+                int pos = u * out->linesize[0] + v;
273
+                if (!out->data[0][pos])
274
+                    out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
275
+                out->data[1][pos] = u;
276
+                out->data[2][pos] = v;
277
+            }
278
+        }
279
+        break;
280
+    default:
281
+        av_assert0(0);
282
+    }
283
+
284
+    ret = ff_filter_frame(outlink, out);
285
+    avfilter_unref_bufferp(&in);
286
+    if (ret < 0)
287
+        return ret;
288
+    return 0;
289
+}
290
+
291
+static av_cold void uninit(AVFilterContext *ctx)
292
+{
293
+    HistogramContext *h = ctx->priv;
294
+
295
+    av_opt_free(h);
296
+}
297
+
298
+static const AVFilterPad inputs[] = {
299
+    {
300
+        .name         = "default",
301
+        .type         = AVMEDIA_TYPE_VIDEO,
302
+        .filter_frame = filter_frame,
303
+        .config_props = config_input,
304
+        .min_perms    = AV_PERM_READ,
305
+    },
306
+    { NULL }
307
+};
308
+
309
+static const AVFilterPad outputs[] = {
310
+    {
311
+        .name         = "default",
312
+        .type         = AVMEDIA_TYPE_VIDEO,
313
+        .config_props = config_output,
314
+    },
315
+    { NULL }
316
+};
317
+
318
+AVFilter avfilter_vf_histogram = {
319
+    .name          = "histogram",
320
+    .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
321
+    .priv_size     = sizeof(HistogramContext),
322
+    .init          = init,
323
+    .uninit        = uninit,
324
+    .query_formats = query_formats,
325
+    .inputs        = inputs,
326
+    .outputs       = outputs,
327
+    .priv_class    = &histogram_class,
328
+};