]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_histogram.c
avfilter/vf_histogram: fix bug in checking pixel format flags
[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     int mode;                           ///< HistogramMode
41     unsigned       histogram[256];
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     int            waveform_mirror;
50     int            display_mode;
51     int            levels_mode;
52     const AVPixFmtDescriptor *desc, *odesc;
53     int            components;
54     int            planewidth[4];
55     int            planeheight[4];
56 } HistogramContext;
57
58 #define OFFSET(x) offsetof(HistogramContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
60
61 static const AVOption histogram_options[] = {
62     { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
63     { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
64     { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
65     { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
66     { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
67     { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
68     { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
69     { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
70     { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
71     { "row",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
72     { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
73     { "waveform_mirror", "set waveform mirroring", OFFSET(waveform_mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mirror"},
74     { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
75     { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
76     { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
77     { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
78     { "linear",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
79     { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
80     { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
81     { NULL }
82 };
83
84 AVFILTER_DEFINE_CLASS(histogram);
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_in_pix_fmts[] = {
92     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
93     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
94     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
95     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
96     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
97     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
98     AV_PIX_FMT_GRAY8,
99     AV_PIX_FMT_NONE
100 };
101
102 static const enum AVPixelFormat levels_out_yuv_pix_fmts[] = {
103     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
104     AV_PIX_FMT_NONE
105 };
106
107 static const enum AVPixelFormat levels_out_rgb_pix_fmts[] = {
108     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
109     AV_PIX_FMT_NONE
110 };
111
112 static const enum AVPixelFormat waveform_pix_fmts[] = {
113      AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
114      AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
115      AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
116      AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
117      AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
118      AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
119      AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
120      AV_PIX_FMT_GRAY8,
121      AV_PIX_FMT_NONE
122 };
123
124 static int query_formats(AVFilterContext *ctx)
125 {
126     HistogramContext *h = ctx->priv;
127     const enum AVPixelFormat *pix_fmts;
128     AVFilterFormats *fmts_list;
129
130     switch (h->mode) {
131     case MODE_WAVEFORM:
132         pix_fmts = waveform_pix_fmts;
133         break;
134     case MODE_LEVELS:
135     {
136         AVFilterFormats *avff;
137         const AVPixFmtDescriptor *desc;
138         const enum AVPixelFormat *out_pix_fmts;
139         int rgb, i;
140
141         if (!ctx->inputs[0]->in_formats ||
142             !ctx->inputs[0]->in_formats->nb_formats) {
143             return AVERROR(EAGAIN);
144         }
145
146         if (!ctx->inputs[0]->out_formats)
147             ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats);
148         avff = ctx->inputs[0]->in_formats;
149         desc = av_pix_fmt_desc_get(avff->formats[0]);
150         rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
151         for (i = 1; i < avff->nb_formats; i++) {
152             desc = av_pix_fmt_desc_get(avff->formats[i]);
153             if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB))
154                 return AVERROR(EAGAIN);
155         }
156
157         if (rgb)
158             out_pix_fmts = levels_out_rgb_pix_fmts;
159         else
160             out_pix_fmts = levels_out_yuv_pix_fmts;
161         ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats);
162
163         return 0;
164     }
165         break;
166     case MODE_COLOR:
167     case MODE_COLOR2:
168         pix_fmts = color_pix_fmts;
169         break;
170     default:
171         av_assert0(0);
172     }
173
174     fmts_list = ff_make_format_list(pix_fmts);
175     if (!fmts_list)
176         return AVERROR(ENOMEM);
177     return ff_set_common_formats(ctx, fmts_list);
178 }
179
180 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
181 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
182 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
183 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
184
185 static int config_input(AVFilterLink *inlink)
186 {
187     HistogramContext *h = inlink->dst->priv;
188
189     h->desc  = av_pix_fmt_desc_get(inlink->format);
190     h->ncomp = h->desc->nb_components;
191
192     switch (inlink->format) {
193     case AV_PIX_FMT_GBRAP:
194     case AV_PIX_FMT_GBRP:
195         h->bg_color = black_gbrp_color;
196         h->fg_color = white_gbrp_color;
197         break;
198     default:
199         h->bg_color = black_yuva_color;
200         h->fg_color = white_yuva_color;
201     }
202
203     h->planeheight[1] = h->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
204     h->planeheight[0] = h->planeheight[3] = inlink->h;
205     h->planewidth[1]  = h->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
206     h->planewidth[0]  = h->planewidth[3]  = inlink->w;
207
208     return 0;
209 }
210
211 static int config_output(AVFilterLink *outlink)
212 {
213     AVFilterContext *ctx = outlink->src;
214     HistogramContext *h = ctx->priv;
215     int ncomp = 0, i;
216
217     switch (h->mode) {
218     case MODE_LEVELS:
219         for (i = 0; i < h->ncomp; i++) {
220             if ((1 << i) & h->components)
221                 ncomp++;
222         }
223         outlink->w = 256;
224         outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * h->display_mode, 1);
225         break;
226     case MODE_WAVEFORM:
227         av_log(ctx, AV_LOG_WARNING, "This mode is deprecated, please use waveform filter instead.\n");
228         if (h->waveform_mode)
229             outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
230         else
231             outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
232         break;
233     case MODE_COLOR:
234     case MODE_COLOR2:
235         av_log(ctx, AV_LOG_WARNING, "This mode is deprecated, use vectorscope filter instead.");
236         outlink->h = outlink->w = 256;
237         break;
238     default:
239         av_assert0(0);
240     }
241
242     h->odesc = av_pix_fmt_desc_get(outlink->format);
243     outlink->sample_aspect_ratio = (AVRational){1,1};
244
245     return 0;
246 }
247
248 static void gen_waveform(HistogramContext *h, AVFrame *inpicref, AVFrame *outpicref,
249                          int component, int intensity, int offset, int col_mode)
250 {
251     const int plane = h->desc->comp[component].plane;
252     const int mirror = h->waveform_mirror;
253     const int is_chroma = (component == 1 || component == 2);
254     const int shift_w = (is_chroma ? h->desc->log2_chroma_w : 0);
255     const int shift_h = (is_chroma ? h->desc->log2_chroma_h : 0);
256     const int src_linesize = inpicref->linesize[plane];
257     const int dst_linesize = outpicref->linesize[plane];
258     const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
259     uint8_t *src_data = inpicref->data[plane];
260     uint8_t *dst_data = outpicref->data[plane] + (col_mode ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
261     uint8_t * const dst_bottom_line = dst_data + dst_linesize * ((256 >> shift_h) - 1);
262     uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
263     const uint8_t max = 255 - intensity;
264     const int src_h = FF_CEIL_RSHIFT(inpicref->height, shift_h);
265     const int src_w = FF_CEIL_RSHIFT(inpicref->width, shift_w);
266     uint8_t *dst, *p;
267     int y;
268
269     if (!col_mode && mirror)
270         dst_data += 256 >> shift_w;
271     for (y = 0; y < src_h; y++) {
272         const uint8_t *src_data_end = src_data + src_w;
273         dst = dst_line;
274         for (p = src_data; p < src_data_end; p++) {
275             uint8_t *target;
276             if (col_mode) {
277                 target = dst++ + dst_signed_linesize * (*p >> shift_h);
278             } else {
279                 if (mirror)
280                     target = dst_data - (*p >> shift_w);
281                 else
282                     target = dst_data + (*p >> shift_w);
283             }
284             if (*target <= max)
285                 *target += intensity;
286             else
287                 *target = 255;
288         }
289         src_data += src_linesize;
290         dst_data += dst_linesize;
291     }
292 }
293
294
295 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
296 {
297     HistogramContext *h   = inlink->dst->priv;
298     AVFilterContext *ctx  = inlink->dst;
299     AVFilterLink *outlink = ctx->outputs[0];
300     AVFrame *out;
301     const uint8_t *src;
302     uint8_t *dst;
303     int i, j, k, l, m;
304
305     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
306     if (!out) {
307         av_frame_free(&in);
308         return AVERROR(ENOMEM);
309     }
310
311     out->pts = in->pts;
312
313     for (k = 0; k < 4 && out->data[k]; k++) {
314         const int is_chroma = (k == 1 || k == 2);
315         const int dst_h = FF_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
316         const int dst_w = FF_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
317         for (i = 0; i < dst_h ; i++)
318             memset(out->data[h->odesc->comp[k].plane] +
319                    i * out->linesize[h->odesc->comp[k].plane],
320                    h->bg_color[k], dst_w);
321     }
322
323     switch (h->mode) {
324     case MODE_LEVELS:
325         for (m = 0, k = 0; k < h->ncomp; k++) {
326             const int p = h->desc->comp[k].plane;
327             const int height = h->planeheight[p];
328             const int width = h->planewidth[p];
329             int start;
330             double max_hval_log;
331             unsigned max_hval = 0;
332
333             if (!((1 << k) & h->components))
334                 continue;
335             start = m++ * (h->level_height + h->scale_height) * h->display_mode;
336
337             for (i = 0; i < height; i++) {
338                 src = in->data[p] + i * in->linesize[p];
339                 for (j = 0; j < width; j++)
340                     h->histogram[src[j]]++;
341             }
342
343             for (i = 0; i < 256; i++)
344                 max_hval = FFMAX(max_hval, h->histogram[i]);
345             max_hval_log = log2(max_hval + 1);
346
347             for (i = 0; i < outlink->w; i++) {
348                 int col_height;
349
350                 if (h->levels_mode)
351                     col_height = round(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
352                 else
353                     col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
354
355                 for (j = h->level_height - 1; j >= col_height; j--) {
356                     if (h->display_mode) {
357                         for (l = 0; l < h->ncomp; l++)
358                             out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
359                     } else {
360                         out->data[p][(j + start) * out->linesize[p] + i] = 255;
361                     }
362                 }
363                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
364                     out->data[p][(j + start) * out->linesize[p] + i] = i;
365             }
366
367             memset(h->histogram, 0, 256 * sizeof(unsigned));
368         }
369         break;
370     case MODE_WAVEFORM:
371         for (k = 0; k < h->ncomp; k++) {
372             const int offset = k * 256 * h->display_mode;
373             gen_waveform(h, in, out, k, h->step, offset, h->waveform_mode);
374         }
375         break;
376     case MODE_COLOR:
377         for (i = 0; i < inlink->h; i++) {
378             const int iw1 = i * in->linesize[1];
379             const int iw2 = i * in->linesize[2];
380             for (j = 0; j < inlink->w; j++) {
381                 const int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
382                 if (out->data[0][pos] < 255)
383                     out->data[0][pos]++;
384             }
385         }
386         for (i = 0; i < 256; i++) {
387             dst = out->data[0] + i * out->linesize[0];
388             for (j = 0; j < 256; j++) {
389                 if (!dst[j]) {
390                     out->data[1][i * out->linesize[0] + j] = i;
391                     out->data[2][i * out->linesize[0] + j] = j;
392                 }
393             }
394         }
395         break;
396     case MODE_COLOR2:
397         for (i = 0; i < inlink->h; i++) {
398             const int iw1 = i * in->linesize[1];
399             const int iw2 = i * in->linesize[2];
400             for (j = 0; j < inlink->w; j++) {
401                 const int u = in->data[1][iw1 + j];
402                 const int v = in->data[2][iw2 + j];
403                 const int pos = u * out->linesize[0] + v;
404                 if (!out->data[0][pos])
405                     out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
406                 out->data[1][pos] = u;
407                 out->data[2][pos] = v;
408             }
409         }
410         break;
411     default:
412         av_assert0(0);
413     }
414
415     av_frame_free(&in);
416     return ff_filter_frame(outlink, out);
417 }
418
419 static const AVFilterPad inputs[] = {
420     {
421         .name         = "default",
422         .type         = AVMEDIA_TYPE_VIDEO,
423         .filter_frame = filter_frame,
424         .config_props = config_input,
425     },
426     { NULL }
427 };
428
429 static const AVFilterPad outputs[] = {
430     {
431         .name         = "default",
432         .type         = AVMEDIA_TYPE_VIDEO,
433         .config_props = config_output,
434     },
435     { NULL }
436 };
437
438 AVFilter ff_vf_histogram = {
439     .name          = "histogram",
440     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
441     .priv_size     = sizeof(HistogramContext),
442     .query_formats = query_formats,
443     .inputs        = inputs,
444     .outputs       = outputs,
445     .priv_class    = &histogram_class,
446 };