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