libavfilter/vf_histogram.c
29a92c01
 /*
  * Copyright (c) 2012-2013 Paul B Mahol
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "libavutil/avassert.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
9f2fa95b
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
29a92c01
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
 typedef struct HistogramContext {
     const AVClass *class;               ///< AVClass context for log and options purpose
9f2fa95b
     unsigned       histogram[256*256];
     int            histogram_size;
     int            mult;
29a92c01
     int            ncomp;
c784b5cf
     int            dncomp;
0d8b6a15
     uint8_t        bg_color[4];
     uint8_t        fg_color[4];
29a92c01
     int            level_height;
     int            scale_height;
4fa9defc
     int            display_mode;
c45b823b
     int            levels_mode;
6be5b05f
     const AVPixFmtDescriptor *desc, *odesc;
fa95965f
     int            components;
0d8b6a15
     float          fgopacity;
     float          bgopacity;
6be5b05f
     int            planewidth[4];
     int            planeheight[4];
29a92c01
 } HistogramContext;
 
 #define OFFSET(x) offsetof(HistogramContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption histogram_options[] = {
     { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
     { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
66be2426
     { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"},
     { "d",            "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"},
5d12cfac
         { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
66be2426
         { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
         { "stack",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "display_mode" },
c45b823b
     { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
91bc4252
     { "m",           "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
5d12cfac
         { "linear",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
         { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
fa95965f
     { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
91bc4252
     { "c",          "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
0d8b6a15
     { "fgopacity", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
     { "f",         "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
b211607b
     { NULL }
29a92c01
 };
 
 AVFILTER_DEFINE_CLASS(histogram);
 
6be5b05f
 static const enum AVPixelFormat levels_in_pix_fmts[] = {
     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
9f2fa95b
     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
02f84215
     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
6be5b05f
     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
bac508fe
     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10,  AV_PIX_FMT_GBRAP10,
02f84215
     AV_PIX_FMT_GBRP12,   AV_PIX_FMT_GBRAP12,
6be5b05f
     AV_PIX_FMT_GRAY8,
     AV_PIX_FMT_NONE
 };
 
9f2fa95b
 static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
6be5b05f
     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
     AV_PIX_FMT_NONE
 };
 
9f2fa95b
 static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
     AV_PIX_FMT_NONE
 };
 
 static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
     AV_PIX_FMT_NONE
 };
 
02f84215
 static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
     AV_PIX_FMT_YUV444P12,
     AV_PIX_FMT_NONE
 };
 
9f2fa95b
 static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
6be5b05f
     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
     AV_PIX_FMT_NONE
29a92c01
 };
 
9f2fa95b
 static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
     AV_PIX_FMT_GBRP9,
     AV_PIX_FMT_NONE
 };
 
 static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
bac508fe
     AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
9f2fa95b
     AV_PIX_FMT_NONE
 };
 
02f84215
 static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
     AV_PIX_FMT_NONE
 };
 
29a92c01
 static int query_formats(AVFilterContext *ctx)
 {
cde75e31
     AVFilterFormats *avff;
     const AVPixFmtDescriptor *desc;
     const enum AVPixelFormat *out_pix_fmts;
     int rgb, i, bits;
6aaac24d
     int ret;
29a92c01
 
cde75e31
     if (!ctx->inputs[0]->in_formats ||
         !ctx->inputs[0]->in_formats->nb_formats) {
         return AVERROR(EAGAIN);
     }
6be5b05f
 
cde75e31
     if (!ctx->inputs[0]->out_formats)
         if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
6aaac24d
             return ret;
cde75e31
     avff = ctx->inputs[0]->in_formats;
     desc = av_pix_fmt_desc_get(avff->formats[0]);
     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
     bits = desc->comp[0].depth;
     for (i = 1; i < avff->nb_formats; i++) {
         desc = av_pix_fmt_desc_get(avff->formats[i]);
         if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
             (bits != desc->comp[0].depth))
             return AVERROR(EAGAIN);
29a92c01
     }
 
cde75e31
     if (rgb && bits == 8)
         out_pix_fmts = levels_out_rgb8_pix_fmts;
     else if (rgb && bits == 9)
         out_pix_fmts = levels_out_rgb9_pix_fmts;
     else if (rgb && bits == 10)
         out_pix_fmts = levels_out_rgb10_pix_fmts;
02f84215
     else if (rgb && bits == 12)
         out_pix_fmts = levels_out_rgb12_pix_fmts;
cde75e31
     else if (bits == 8)
         out_pix_fmts = levels_out_yuv8_pix_fmts;
     else if (bits == 9)
         out_pix_fmts = levels_out_yuv9_pix_fmts;
ac15d7a6
     else if (bits == 10)
cde75e31
         out_pix_fmts = levels_out_yuv10_pix_fmts;
02f84215
     else if (bits == 12)
         out_pix_fmts = levels_out_yuv12_pix_fmts;
ac15d7a6
     else
         return AVERROR(EAGAIN);
cde75e31
     if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
         return ret;
 
     return 0;
29a92c01
 }
 
 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
 static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
 static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
 
 static int config_input(AVFilterLink *inlink)
 {
     HistogramContext *h = inlink->dst->priv;
 
31a8461d
     h->desc  = av_pix_fmt_desc_get(inlink->format);
     h->ncomp = h->desc->nb_components;
5d8e836d
     h->histogram_size = 1 << h->desc->comp[0].depth;
9f2fa95b
     h->mult = h->histogram_size / 256;
29a92c01
 
     switch (inlink->format) {
bac508fe
     case AV_PIX_FMT_GBRAP12:
02f84215
     case AV_PIX_FMT_GBRP12:
bac508fe
     case AV_PIX_FMT_GBRAP10:
9f2fa95b
     case AV_PIX_FMT_GBRP10:
     case AV_PIX_FMT_GBRP9:
1f2baec7
     case AV_PIX_FMT_GBRAP:
29a92c01
     case AV_PIX_FMT_GBRP:
0d8b6a15
         memcpy(h->bg_color, black_gbrp_color, 4);
         memcpy(h->fg_color, white_gbrp_color, 4);
29a92c01
         break;
     default:
0d8b6a15
         memcpy(h->bg_color, black_yuva_color, 4);
         memcpy(h->fg_color, white_yuva_color, 4);
29a92c01
     }
 
0d8b6a15
     h->fg_color[3] = h->fgopacity * 255;
     h->bg_color[3] = h->bgopacity * 255;
 
21f94684
     h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
6be5b05f
     h->planeheight[0] = h->planeheight[3] = inlink->h;
21f94684
     h->planewidth[1]  = h->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
6be5b05f
     h->planewidth[0]  = h->planewidth[3]  = inlink->w;
 
29a92c01
     return 0;
 }
 
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     HistogramContext *h = ctx->priv;
fa95965f
     int ncomp = 0, i;
29a92c01
 
cde75e31
     for (i = 0; i < h->ncomp; i++) {
         if ((1 << i) & h->components)
             ncomp++;
29a92c01
     }
66be2426
     outlink->w = h->histogram_size * FFMAX(ncomp * (h->display_mode == 1), 1);
     outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * (h->display_mode == 2), 1);
29a92c01
 
6be5b05f
     h->odesc = av_pix_fmt_desc_get(outlink->format);
c784b5cf
     h->dncomp = h->odesc->nb_components;
29a92c01
     outlink->sample_aspect_ratio = (AVRational){1,1};
 
     return 0;
 }
 
a05a44e2
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
29a92c01
 {
     HistogramContext *h   = inlink->dst->priv;
     AVFilterContext *ctx  = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
a05a44e2
     AVFrame *out;
fa95965f
     int i, j, k, l, m;
29a92c01
 
a05a44e2
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
29a92c01
     if (!out) {
a05a44e2
         av_frame_free(&in);
29a92c01
         return AVERROR(ENOMEM);
     }
 
     out->pts = in->pts;
 
6be5b05f
     for (k = 0; k < 4 && out->data[k]; k++) {
a2c14ba2
         const int is_chroma = (k == 1 || k == 2);
21f94684
         const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
         const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
9f2fa95b
 
         if (h->histogram_size <= 256) {
             for (i = 0; i < dst_h ; i++)
                 memset(out->data[h->odesc->comp[k].plane] +
                        i * out->linesize[h->odesc->comp[k].plane],
                        h->bg_color[k], dst_w);
         } else {
             const int mult = h->mult;
 
             for (i = 0; i < dst_h ; i++)
                 for (j = 0; j < dst_w; j++)
                     AV_WN16(out->data[h->odesc->comp[k].plane] +
                         i * out->linesize[h->odesc->comp[k].plane] + j * 2,
                         h->bg_color[k] * mult);
         }
cac9af68
     }
29a92c01
 
cde75e31
     for (m = 0, k = 0; k < h->ncomp; k++) {
         const int p = h->desc->comp[k].plane;
         const int height = h->planeheight[p];
         const int width = h->planewidth[p];
         double max_hval_log;
         unsigned max_hval = 0;
66be2426
         int start, startx;
e5670cb8
 
cde75e31
         if (!((1 << k) & h->components))
             continue;
66be2426
         startx = m * h->histogram_size * (h->display_mode == 1);
         start = m++ * (h->level_height + h->scale_height) * (h->display_mode == 2);
fa95965f
 
cde75e31
         if (h->histogram_size <= 256) {
             for (i = 0; i < height; i++) {
                 const uint8_t *src = in->data[p] + i * in->linesize[p];
                 for (j = 0; j < width; j++)
                     h->histogram[src[j]]++;
29a92c01
             }
cde75e31
         } else {
             for (i = 0; i < height; i++) {
                 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
                 for (j = 0; j < width; j++)
                     h->histogram[src[j]]++;
             }
         }
29a92c01
 
cde75e31
         for (i = 0; i < h->histogram_size; i++)
             max_hval = FFMAX(max_hval, h->histogram[i]);
         max_hval_log = log2(max_hval + 1);
 
66be2426
         for (i = 0; i < h->histogram_size; i++) {
cde75e31
             int col_height;
 
             if (h->levels_mode)
ea2f04bf
                 col_height = lrint(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
cde75e31
             else
                 col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
 
             if (h->histogram_size <= 256) {
                 for (j = h->level_height - 1; j >= col_height; j--) {
                     if (h->display_mode) {
c784b5cf
                         for (l = 0; l < h->dncomp; l++)
66be2426
                             out->data[l][(j + start) * out->linesize[l] + startx + i] = h->fg_color[l];
cde75e31
                     } else {
66be2426
                         out->data[p][(j + start) * out->linesize[p] + startx + i] = 255;
480ddf2b
                     }
                 }
cde75e31
                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
66be2426
                     out->data[p][(j + start) * out->linesize[p] + startx + i] = i;
cde75e31
             } else {
                 const int mult = h->mult;
 
                 for (j = h->level_height - 1; j >= col_height; j--) {
                     if (h->display_mode) {
c784b5cf
                         for (l = 0; l < h->dncomp; l++)
66be2426
                             AV_WN16(out->data[l] + (j + start) * out->linesize[l] + startx * 2 + i * 2, h->fg_color[l] * mult);
cde75e31
                     } else {
66be2426
                         AV_WN16(out->data[p] + (j + start) * out->linesize[p] + startx * 2 + i * 2, 255 * mult);
cde75e31
                     }
29a92c01
                 }
cde75e31
                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
66be2426
                     AV_WN16(out->data[p] + (j + start) * out->linesize[p] + startx * 2 + i * 2, i);
29a92c01
             }
         }
cde75e31
 
         memset(h->histogram, 0, h->histogram_size * sizeof(unsigned));
29a92c01
     }
 
a05a44e2
     av_frame_free(&in);
8281791d
     return ff_filter_frame(outlink, out);
29a92c01
 }
 
 static const AVFilterPad inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_input,
     },
     { NULL }
 };
 
 static const AVFilterPad outputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_output,
     },
     { NULL }
 };
 
325f6e0a
 AVFilter ff_vf_histogram = {
29a92c01
     .name          = "histogram",
     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
     .priv_size     = sizeof(HistogramContext),
     .query_formats = query_formats,
     .inputs        = inputs,
     .outputs       = outputs,
     .priv_class    = &histogram_class,
 };