LCOV - code coverage report
Current view: top level - libavfilter - vf_histogram.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 94 136 69.1 %
Date: 2017-12-15 11:05:35 Functions: 4 4 100.0 %

          Line data    Source code
       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 "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             :     unsigned       histogram[256*256];
      35             :     int            histogram_size;
      36             :     int            mult;
      37             :     int            ncomp;
      38             :     int            dncomp;
      39             :     uint8_t        bg_color[4];
      40             :     uint8_t        fg_color[4];
      41             :     int            level_height;
      42             :     int            scale_height;
      43             :     int            display_mode;
      44             :     int            levels_mode;
      45             :     const AVPixFmtDescriptor *desc, *odesc;
      46             :     int            components;
      47             :     float          fgopacity;
      48             :     float          bgopacity;
      49             :     int            planewidth[4];
      50             :     int            planeheight[4];
      51             : } HistogramContext;
      52             : 
      53             : #define OFFSET(x) offsetof(HistogramContext, x)
      54             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      55             : 
      56             : static const AVOption histogram_options[] = {
      57             :     { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
      58             :     { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
      59             :     { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"},
      60             :     { "d",            "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "display_mode"},
      61             :         { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
      62             :         { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
      63             :         { "stack",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "display_mode" },
      64             :     { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
      65             :     { "m",           "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
      66             :         { "linear",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
      67             :         { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
      68             :     { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
      69             :     { "c",          "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
      70             :     { "fgopacity", "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
      71             :     { "f",         "set foreground opacity", OFFSET(fgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.7}, 0, 1, FLAGS},
      72             :     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
      73             :     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS},
      74             :     { NULL }
      75             : };
      76             : 
      77             : AVFILTER_DEFINE_CLASS(histogram);
      78             : 
      79             : static const enum AVPixelFormat levels_in_pix_fmts[] = {
      80             :     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
      81             :     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
      82             :     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
      83             :     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
      84             :     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
      85             :     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
      86             :     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
      87             :     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
      88             :     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
      89             :     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
      90             :     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
      91             :     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10,  AV_PIX_FMT_GBRAP10,
      92             :     AV_PIX_FMT_GBRP12,   AV_PIX_FMT_GBRAP12,
      93             :     AV_PIX_FMT_GRAY8,
      94             :     AV_PIX_FMT_NONE
      95             : };
      96             : 
      97             : static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
      98             :     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
      99             :     AV_PIX_FMT_NONE
     100             : };
     101             : 
     102             : static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
     103             :     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
     104             :     AV_PIX_FMT_NONE
     105             : };
     106             : 
     107             : static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
     108             :     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
     109             :     AV_PIX_FMT_NONE
     110             : };
     111             : 
     112             : static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
     113             :     AV_PIX_FMT_YUV444P12,
     114             :     AV_PIX_FMT_NONE
     115             : };
     116             : 
     117             : static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
     118             :     AV_PIX_FMT_GBRAP,    AV_PIX_FMT_GBRP,
     119             :     AV_PIX_FMT_NONE
     120             : };
     121             : 
     122             : static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
     123             :     AV_PIX_FMT_GBRP9,
     124             :     AV_PIX_FMT_NONE
     125             : };
     126             : 
     127             : static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
     128             :     AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
     129             :     AV_PIX_FMT_NONE
     130             : };
     131             : 
     132             : static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
     133             :     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
     134             :     AV_PIX_FMT_NONE
     135             : };
     136             : 
     137           2 : static int query_formats(AVFilterContext *ctx)
     138             : {
     139             :     AVFilterFormats *avff;
     140             :     const AVPixFmtDescriptor *desc;
     141             :     const enum AVPixelFormat *out_pix_fmts;
     142             :     int rgb, i, bits;
     143             :     int ret;
     144             : 
     145           3 :     if (!ctx->inputs[0]->in_formats ||
     146           1 :         !ctx->inputs[0]->in_formats->nb_formats) {
     147           1 :         return AVERROR(EAGAIN);
     148             :     }
     149             : 
     150           1 :     if (!ctx->inputs[0]->out_formats)
     151           1 :         if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
     152           0 :             return ret;
     153           1 :     avff = ctx->inputs[0]->in_formats;
     154           1 :     desc = av_pix_fmt_desc_get(avff->formats[0]);
     155           1 :     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
     156           1 :     bits = desc->comp[0].depth;
     157           1 :     for (i = 1; i < avff->nb_formats; i++) {
     158           0 :         desc = av_pix_fmt_desc_get(avff->formats[i]);
     159           0 :         if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
     160           0 :             (bits != desc->comp[0].depth))
     161           0 :             return AVERROR(EAGAIN);
     162             :     }
     163             : 
     164           1 :     if (rgb && bits == 8)
     165           0 :         out_pix_fmts = levels_out_rgb8_pix_fmts;
     166           1 :     else if (rgb && bits == 9)
     167           0 :         out_pix_fmts = levels_out_rgb9_pix_fmts;
     168           1 :     else if (rgb && bits == 10)
     169           0 :         out_pix_fmts = levels_out_rgb10_pix_fmts;
     170           1 :     else if (rgb && bits == 12)
     171           0 :         out_pix_fmts = levels_out_rgb12_pix_fmts;
     172           1 :     else if (bits == 8)
     173           1 :         out_pix_fmts = levels_out_yuv8_pix_fmts;
     174           0 :     else if (bits == 9)
     175           0 :         out_pix_fmts = levels_out_yuv9_pix_fmts;
     176           0 :     else if (bits == 10)
     177           0 :         out_pix_fmts = levels_out_yuv10_pix_fmts;
     178           0 :     else if (bits == 12)
     179           0 :         out_pix_fmts = levels_out_yuv12_pix_fmts;
     180             :     else
     181           0 :         return AVERROR(EAGAIN);
     182           1 :     if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
     183           0 :         return ret;
     184             : 
     185           1 :     return 0;
     186             : }
     187             : 
     188             : static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
     189             : static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
     190             : static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
     191             : static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
     192             : 
     193           1 : static int config_input(AVFilterLink *inlink)
     194             : {
     195           1 :     HistogramContext *h = inlink->dst->priv;
     196             : 
     197           1 :     h->desc  = av_pix_fmt_desc_get(inlink->format);
     198           1 :     h->ncomp = h->desc->nb_components;
     199           1 :     h->histogram_size = 1 << h->desc->comp[0].depth;
     200           1 :     h->mult = h->histogram_size / 256;
     201             : 
     202           1 :     switch (inlink->format) {
     203           0 :     case AV_PIX_FMT_GBRAP12:
     204             :     case AV_PIX_FMT_GBRP12:
     205             :     case AV_PIX_FMT_GBRAP10:
     206             :     case AV_PIX_FMT_GBRP10:
     207             :     case AV_PIX_FMT_GBRP9:
     208             :     case AV_PIX_FMT_GBRAP:
     209             :     case AV_PIX_FMT_GBRP:
     210           0 :         memcpy(h->bg_color, black_gbrp_color, 4);
     211           0 :         memcpy(h->fg_color, white_gbrp_color, 4);
     212           0 :         break;
     213           1 :     default:
     214           1 :         memcpy(h->bg_color, black_yuva_color, 4);
     215           1 :         memcpy(h->fg_color, white_yuva_color, 4);
     216             :     }
     217             : 
     218           1 :     h->fg_color[3] = h->fgopacity * 255;
     219           1 :     h->bg_color[3] = h->bgopacity * 255;
     220             : 
     221           1 :     h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
     222           1 :     h->planeheight[0] = h->planeheight[3] = inlink->h;
     223           1 :     h->planewidth[1]  = h->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
     224           1 :     h->planewidth[0]  = h->planewidth[3]  = inlink->w;
     225             : 
     226           1 :     return 0;
     227             : }
     228             : 
     229           1 : static int config_output(AVFilterLink *outlink)
     230             : {
     231           1 :     AVFilterContext *ctx = outlink->src;
     232           1 :     HistogramContext *h = ctx->priv;
     233           1 :     int ncomp = 0, i;
     234             : 
     235           4 :     for (i = 0; i < h->ncomp; i++) {
     236           3 :         if ((1 << i) & h->components)
     237           3 :             ncomp++;
     238             :     }
     239           1 :     outlink->w = h->histogram_size * FFMAX(ncomp * (h->display_mode == 1), 1);
     240           1 :     outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * (h->display_mode == 2), 1);
     241             : 
     242           1 :     h->odesc = av_pix_fmt_desc_get(outlink->format);
     243           1 :     h->dncomp = h->odesc->nb_components;
     244           1 :     outlink->sample_aspect_ratio = (AVRational){1,1};
     245             : 
     246           1 :     return 0;
     247             : }
     248             : 
     249          50 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     250             : {
     251          50 :     HistogramContext *h   = inlink->dst->priv;
     252          50 :     AVFilterContext *ctx  = inlink->dst;
     253          50 :     AVFilterLink *outlink = ctx->outputs[0];
     254             :     AVFrame *out;
     255             :     int i, j, k, l, m;
     256             : 
     257          50 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     258          50 :     if (!out) {
     259           0 :         av_frame_free(&in);
     260           0 :         return AVERROR(ENOMEM);
     261             :     }
     262             : 
     263          50 :     out->pts = in->pts;
     264             : 
     265         200 :     for (k = 0; k < 4 && out->data[k]; k++) {
     266         150 :         const int is_chroma = (k == 1 || k == 2);
     267         150 :         const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
     268         150 :         const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
     269             : 
     270         150 :         if (h->histogram_size <= 256) {
     271       95550 :             for (i = 0; i < dst_h ; i++)
     272      286200 :                 memset(out->data[h->odesc->comp[k].plane] +
     273       95400 :                        i * out->linesize[h->odesc->comp[k].plane],
     274       95400 :                        h->bg_color[k], dst_w);
     275             :         } else {
     276           0 :             const int mult = h->mult;
     277             : 
     278           0 :             for (i = 0; i < dst_h ; i++)
     279           0 :                 for (j = 0; j < dst_w; j++)
     280           0 :                     AV_WN16(out->data[h->odesc->comp[k].plane] +
     281             :                         i * out->linesize[h->odesc->comp[k].plane] + j * 2,
     282             :                         h->bg_color[k] * mult);
     283             :         }
     284             :     }
     285             : 
     286         200 :     for (m = 0, k = 0; k < h->ncomp; k++) {
     287         150 :         const int p = h->desc->comp[k].plane;
     288         150 :         const int height = h->planeheight[p];
     289         150 :         const int width = h->planewidth[p];
     290             :         double max_hval_log;
     291         150 :         unsigned max_hval = 0;
     292             :         int start, startx;
     293             : 
     294         150 :         if (!((1 << k) & h->components))
     295           0 :             continue;
     296         150 :         startx = m * h->histogram_size * (h->display_mode == 1);
     297         150 :         start = m++ * (h->level_height + h->scale_height) * (h->display_mode == 2);
     298             : 
     299         150 :         if (h->histogram_size <= 256) {
     300       28950 :             for (i = 0; i < height; i++) {
     301       28800 :                 const uint8_t *src = in->data[p] + i * in->linesize[p];
     302     7632000 :                 for (j = 0; j < width; j++)
     303     7603200 :                     h->histogram[src[j]]++;
     304             :             }
     305             :         } else {
     306           0 :             for (i = 0; i < height; i++) {
     307           0 :                 const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
     308           0 :                 for (j = 0; j < width; j++)
     309           0 :                     h->histogram[src[j]]++;
     310             :             }
     311             :         }
     312             : 
     313       38550 :         for (i = 0; i < h->histogram_size; i++)
     314       38400 :             max_hval = FFMAX(max_hval, h->histogram[i]);
     315         150 :         max_hval_log = log2(max_hval + 1);
     316             : 
     317       38550 :         for (i = 0; i < h->histogram_size; i++) {
     318             :             int col_height;
     319             : 
     320       38400 :             if (h->levels_mode)
     321           0 :                 col_height = lrint(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
     322             :             else
     323       38400 :                 col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
     324             : 
     325       38400 :             if (h->histogram_size <= 256) {
     326     2985598 :                 for (j = h->level_height - 1; j >= col_height; j--) {
     327     2947198 :                     if (h->display_mode) {
     328    11788792 :                         for (l = 0; l < h->dncomp; l++)
     329     8841594 :                             out->data[l][(j + start) * out->linesize[l] + startx + i] = h->fg_color[l];
     330             :                     } else {
     331           0 :                         out->data[p][(j + start) * out->linesize[p] + startx + i] = 255;
     332             :                     }
     333             :                 }
     334      499200 :                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
     335      460800 :                     out->data[p][(j + start) * out->linesize[p] + startx + i] = i;
     336             :             } else {
     337           0 :                 const int mult = h->mult;
     338             : 
     339           0 :                 for (j = h->level_height - 1; j >= col_height; j--) {
     340           0 :                     if (h->display_mode) {
     341           0 :                         for (l = 0; l < h->dncomp; l++)
     342           0 :                             AV_WN16(out->data[l] + (j + start) * out->linesize[l] + startx * 2 + i * 2, h->fg_color[l] * mult);
     343             :                     } else {
     344           0 :                         AV_WN16(out->data[p] + (j + start) * out->linesize[p] + startx * 2 + i * 2, 255 * mult);
     345             :                     }
     346             :                 }
     347           0 :                 for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
     348           0 :                     AV_WN16(out->data[p] + (j + start) * out->linesize[p] + startx * 2 + i * 2, i);
     349             :             }
     350             :         }
     351             : 
     352         150 :         memset(h->histogram, 0, h->histogram_size * sizeof(unsigned));
     353             :     }
     354             : 
     355          50 :     av_frame_free(&in);
     356          50 :     return ff_filter_frame(outlink, out);
     357             : }
     358             : 
     359             : static const AVFilterPad inputs[] = {
     360             :     {
     361             :         .name         = "default",
     362             :         .type         = AVMEDIA_TYPE_VIDEO,
     363             :         .filter_frame = filter_frame,
     364             :         .config_props = config_input,
     365             :     },
     366             :     { NULL }
     367             : };
     368             : 
     369             : static const AVFilterPad outputs[] = {
     370             :     {
     371             :         .name         = "default",
     372             :         .type         = AVMEDIA_TYPE_VIDEO,
     373             :         .config_props = config_output,
     374             :     },
     375             :     { NULL }
     376             : };
     377             : 
     378             : AVFilter ff_vf_histogram = {
     379             :     .name          = "histogram",
     380             :     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
     381             :     .priv_size     = sizeof(HistogramContext),
     382             :     .query_formats = query_formats,
     383             :     .inputs        = inputs,
     384             :     .outputs       = outputs,
     385             :     .priv_class    = &histogram_class,
     386             : };

Generated by: LCOV version 1.13