]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_histogram.c
avfilter: add thistogram video filter
[ffmpeg] / libavfilter / vf_histogram.c
1 /*
2  * Copyright (c) 2012-2019 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     int            thistogram;
35     unsigned       histogram[256*256];
36     int            histogram_size;
37     int            width;
38     int            x_pos;
39     int            mult;
40     int            ncomp;
41     int            dncomp;
42     uint8_t        bg_color[4];
43     uint8_t        fg_color[4];
44     int            level_height;
45     int            scale_height;
46     int            display_mode;
47     int            levels_mode;
48     const AVPixFmtDescriptor *desc, *odesc;
49     int            components;
50     float          fgopacity;
51     float          bgopacity;
52     int            planewidth[4];
53     int            planeheight[4];
54     int            start[4];
55     AVFrame       *out;
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 #define COMMON_OPTIONS \
62     { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"}, \
63     { "d",            "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"}, \
64         { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" }, \
65         { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" }, \
66         { "stack",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "display_mode" }, \
67     { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, \
68     { "m",           "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"}, \
69         { "linear",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" }, \
70         { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" }, \
71     { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS}, \
72     { "c",          "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
73
74 static const AVOption histogram_options[] = {
75     { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
76     { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
77     COMMON_OPTIONS
78     { "fgopacity", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
79     { "f",         "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
80     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
81     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
82     { NULL }
83 };
84
85 AVFILTER_DEFINE_CLASS(histogram);
86
87 static const enum AVPixelFormat levels_in_pix_fmts[] = {
88     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
89     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
90     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
91     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
92     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
93     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
94     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
95     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
96     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
97     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
98     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
99     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10,  AV_PIX_FMT_GBRAP10,
100     AV_PIX_FMT_GBRP12,   AV_PIX_FMT_GBRAP12,
101     AV_PIX_FMT_GRAY8,
102     AV_PIX_FMT_NONE
103 };
104
105 static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
106     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
107     AV_PIX_FMT_NONE
108 };
109
110 static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
111     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
112     AV_PIX_FMT_NONE
113 };
114
115 static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
116     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
117     AV_PIX_FMT_NONE
118 };
119
120 static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
121     AV_PIX_FMT_YUV444P12,
122     AV_PIX_FMT_NONE
123 };
124
125 static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
126     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
127     AV_PIX_FMT_NONE
128 };
129
130 static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
131     AV_PIX_FMT_GBRP9,
132     AV_PIX_FMT_NONE
133 };
134
135 static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
136     AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
137     AV_PIX_FMT_NONE
138 };
139
140 static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
141     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
142     AV_PIX_FMT_NONE
143 };
144
145 static int query_formats(AVFilterContext *ctx)
146 {
147     AVFilterFormats *avff;
148     const AVPixFmtDescriptor *desc;
149     const enum AVPixelFormat *out_pix_fmts;
150     int rgb, i, bits;
151     int ret;
152
153     if (!ctx->inputs[0]->in_formats ||
154         !ctx->inputs[0]->in_formats->nb_formats) {
155         return AVERROR(EAGAIN);
156     }
157
158     if (!ctx->inputs[0]->out_formats)
159         if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
160             return ret;
161     avff = ctx->inputs[0]->in_formats;
162     desc = av_pix_fmt_desc_get(avff->formats[0]);
163     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
164     bits = desc->comp[0].depth;
165     for (i = 1; i < avff->nb_formats; i++) {
166         desc = av_pix_fmt_desc_get(avff->formats[i]);
167         if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
168             (bits != desc->comp[0].depth))
169             return AVERROR(EAGAIN);
170     }
171
172     if (rgb && bits == 8)
173         out_pix_fmts = levels_out_rgb8_pix_fmts;
174     else if (rgb && bits == 9)
175         out_pix_fmts = levels_out_rgb9_pix_fmts;
176     else if (rgb && bits == 10)
177         out_pix_fmts = levels_out_rgb10_pix_fmts;
178     else if (rgb && bits == 12)
179         out_pix_fmts = levels_out_rgb12_pix_fmts;
180     else if (bits == 8)
181         out_pix_fmts = levels_out_yuv8_pix_fmts;
182     else if (bits == 9)
183         out_pix_fmts = levels_out_yuv9_pix_fmts;
184     else if (bits == 10)
185         out_pix_fmts = levels_out_yuv10_pix_fmts;
186     else if (bits == 12)
187         out_pix_fmts = levels_out_yuv12_pix_fmts;
188     else
189         return AVERROR(EAGAIN);
190     if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
191         return ret;
192
193     return 0;
194 }
195
196 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
197 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
198 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
199 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
200
201 static int config_input(AVFilterLink *inlink)
202 {
203     HistogramContext *s = inlink->dst->priv;
204
205     s->desc  = av_pix_fmt_desc_get(inlink->format);
206     s->ncomp = s->desc->nb_components;
207     s->histogram_size = 1 << s->desc->comp[0].depth;
208     s->mult = s->histogram_size / 256;
209
210     switch (inlink->format) {
211     case AV_PIX_FMT_GBRAP12:
212     case AV_PIX_FMT_GBRP12:
213     case AV_PIX_FMT_GBRAP10:
214     case AV_PIX_FMT_GBRP10:
215     case AV_PIX_FMT_GBRP9:
216     case AV_PIX_FMT_GBRAP:
217     case AV_PIX_FMT_GBRP:
218         memcpy(s->bg_color, black_gbrp_color, 4);
219         memcpy(s->fg_color, white_gbrp_color, 4);
220         s->start[0] = s->start[1] = s->start[2] = s->start[3] = 0;
221         break;
222     default:
223         memcpy(s->bg_color, black_yuva_color, 4);
224         memcpy(s->fg_color, white_yuva_color, 4);
225         s->start[0] = s->start[3] = 0;
226         s->start[1] = s->start[2] = s->histogram_size / 2;
227     }
228
229     s->fg_color[3] = s->fgopacity * 255;
230     s->bg_color[3] = s->bgopacity * 255;
231
232     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
233     s->planeheight[0] = s->planeheight[3] = inlink->h;
234     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
235     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
236
237     return 0;
238 }
239
240 static int config_output(AVFilterLink *outlink)
241 {
242     AVFilterContext *ctx = outlink->src;
243     HistogramContext *s = ctx->priv;
244     int ncomp = 0, i;
245
246     if (!strcmp(ctx->filter->name, "thistogram"))
247         s->thistogram = 1;
248
249     for (i = 0; i < s->ncomp; i++) {
250         if ((1 << i) & s->components)
251             ncomp++;
252     }
253
254     if (s->thistogram) {
255         if (!s->width)
256             s->width = ctx->inputs[0]->w;
257         outlink->w = s->width * FFMAX(ncomp * (s->display_mode == 1), 1);
258         outlink->h = s->histogram_size * FFMAX(ncomp * (s->display_mode == 2), 1);
259     } else {
260     outlink->w = s->histogram_size * FFMAX(ncomp * (s->display_mode == 1), 1);
261     outlink->h = (s->level_height + s->scale_height) * FFMAX(ncomp * (s->display_mode == 2), 1);
262     }
263
264     s->odesc = av_pix_fmt_desc_get(outlink->format);
265     s->dncomp = s->odesc->nb_components;
266     outlink->sample_aspect_ratio = (AVRational){1,1};
267
268     return 0;
269 }
270
271 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
272 {
273     HistogramContext *s   = inlink->dst->priv;
274     AVFilterContext *ctx  = inlink->dst;
275     AVFilterLink *outlink = ctx->outputs[0];
276     AVFrame *out = s->out;
277     int i, j, k, l, m;
278
279     if (!s->thistogram || !out) {
280     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
281     if (!out) {
282         av_frame_free(&in);
283         return AVERROR(ENOMEM);
284     }
285     s->out = out;
286
287     for (k = 0; k < 4 && out->data[k]; k++) {
288         const int is_chroma = (k == 1 || k == 2);
289         const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? s->odesc->log2_chroma_h : 0));
290         const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? s->odesc->log2_chroma_w : 0));
291
292         if (s->histogram_size <= 256) {
293             for (i = 0; i < dst_h ; i++)
294                 memset(out->data[s->odesc->comp[k].plane] +
295                        i * out->linesize[s->odesc->comp[k].plane],
296                        s->bg_color[k], dst_w);
297         } else {
298             const int mult = s->mult;
299
300             for (i = 0; i < dst_h ; i++)
301                 for (j = 0; j < dst_w; j++)
302                     AV_WN16(out->data[s->odesc->comp[k].plane] +
303                         i * out->linesize[s->odesc->comp[k].plane] + j * 2,
304                         s->bg_color[k] * mult);
305         }
306     }
307     }
308
309     for (m = 0, k = 0; k < s->ncomp; k++) {
310         const int p = s->desc->comp[k].plane;
311         const int max_value = s->histogram_size - 1 - s->start[p];
312         const int height = s->planeheight[p];
313         const int width = s->planewidth[p];
314         double max_hval_log;
315         unsigned max_hval = 0;
316         int starty, startx;
317
318         if (!((1 << k) & s->components))
319             continue;
320         if (s->thistogram) {
321             starty = m * s->histogram_size * (s->display_mode == 2);
322             startx = m++ * s->width * (s->display_mode == 1);
323         } else {
324         startx = m * s->histogram_size * (s->display_mode == 1);
325         starty = m++ * (s->level_height + s->scale_height) * (s->display_mode == 2);
326         }
327
328         if (s->histogram_size <= 256) {
329             for (i = 0; i < height; i++) {
330                 const uint8_t *src = in->data[p] + i * in->linesize[p];
331                 for (j = 0; j < width; j++)
332                     s->histogram[src[j]]++;
333             }
334         } else {
335             for (i = 0; i < height; i++) {
336                 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
337                 for (j = 0; j < width; j++)
338                     s->histogram[src[j]]++;
339             }
340         }
341
342         for (i = 0; i < s->histogram_size; i++)
343             max_hval = FFMAX(max_hval, s->histogram[i]);
344         max_hval_log = log2(max_hval + 1);
345
346         if (s->thistogram) {
347             for (int i = 0; i < s->histogram_size; i++) {
348                 int idx = s->histogram_size - i - 1;
349                 int value = s->start[p];
350
351                 if (s->levels_mode)
352                     value += lrint(max_value * (log2(s->histogram[idx] + 1) / max_hval_log));
353                 else
354                     value += lrint(max_value * s->histogram[idx] / (float)max_hval);
355
356                 if (s->histogram_size <= 256) {
357                     s->out->data[p][(i + starty) * s->out->linesize[p] + startx + s->x_pos] = value;
358                 } else {
359                     AV_WN16(s->out->data[p] + (i + starty) * s->out->linesize[p] + startx * 2 + s->x_pos * 2, value);
360                 }
361             }
362         } else {
363         for (i = 0; i < s->histogram_size; i++) {
364             int col_height;
365
366             if (s->levels_mode)
367                 col_height = lrint(s->level_height * (1. - (log2(s->histogram[i] + 1) / max_hval_log)));
368             else
369                 col_height = s->level_height - (s->histogram[i] * (int64_t)s->level_height + max_hval - 1) / max_hval;
370
371             if (s->histogram_size <= 256) {
372                 for (j = s->level_height - 1; j >= col_height; j--) {
373                     if (s->display_mode) {
374                         for (l = 0; l < s->dncomp; l++)
375                             out->data[l][(j + starty) * out->linesize[l] + startx + i] = s->fg_color[l];
376                     } else {
377                         out->data[p][(j + starty) * out->linesize[p] + startx + i] = 255;
378                     }
379                 }
380                 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
381                     out->data[p][(j + starty) * out->linesize[p] + startx + i] = i;
382             } else {
383                 const int mult = s->mult;
384
385                 for (j = s->level_height - 1; j >= col_height; j--) {
386                     if (s->display_mode) {
387                         for (l = 0; l < s->dncomp; l++)
388                             AV_WN16(out->data[l] + (j + starty) * out->linesize[l] + startx * 2 + i * 2, s->fg_color[l] * mult);
389                     } else {
390                         AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, 255 * mult);
391                     }
392                 }
393                 for (j = s->level_height + s->scale_height - 1; j >= s->level_height; j--)
394                     AV_WN16(out->data[p] + (j + starty) * out->linesize[p] + startx * 2 + i * 2, i);
395             }
396         }
397         }
398
399         memset(s->histogram, 0, s->histogram_size * sizeof(unsigned));
400     }
401
402     out->pts = in->pts;
403     av_frame_free(&in);
404     s->x_pos++;
405     if (s->x_pos >= s->width)
406         s->x_pos = 0;
407
408     if (s->thistogram) {
409         AVFrame *clone = av_frame_clone(out);
410
411         if (!clone)
412             return AVERROR(ENOMEM);
413         return ff_filter_frame(outlink, clone);
414     }
415     return ff_filter_frame(outlink, out);
416 }
417
418 static const AVFilterPad inputs[] = {
419     {
420         .name         = "default",
421         .type         = AVMEDIA_TYPE_VIDEO,
422         .filter_frame = filter_frame,
423         .config_props = config_input,
424     },
425     { NULL }
426 };
427
428 static const AVFilterPad outputs[] = {
429     {
430         .name         = "default",
431         .type         = AVMEDIA_TYPE_VIDEO,
432         .config_props = config_output,
433     },
434     { NULL }
435 };
436
437 #if CONFIG_HISTOGRAM_FILTER
438
439 AVFilter ff_vf_histogram = {
440     .name          = "histogram",
441     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
442     .priv_size     = sizeof(HistogramContext),
443     .query_formats = query_formats,
444     .inputs        = inputs,
445     .outputs       = outputs,
446     .priv_class    = &histogram_class,
447 };
448
449 #endif /* CONFIG_HISTOGRAM_FILTER */
450
451 #if CONFIG_THISTOGRAM_FILTER
452
453 static const AVOption thistogram_options[] = {
454     { "width", "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
455     { "w",     "set width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, 8192, FLAGS},
456     COMMON_OPTIONS
457     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
458     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
459     { NULL }
460 };
461
462 AVFILTER_DEFINE_CLASS(thistogram);
463
464 AVFilter ff_vf_thistogram = {
465     .name          = "thistogram",
466     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a temporal histogram."),
467     .priv_size     = sizeof(HistogramContext),
468     .query_formats = query_formats,
469     .inputs        = inputs,
470     .outputs       = outputs,
471     .priv_class    = &thistogram_class,
472 };
473
474 #endif /* CONFIG_THISTOGRAM_FILTER */