LCOV - code coverage report
Current view: top level - src/libavfilter - avf_ahistogram.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 187 0.0 %
Date: 2017-01-23 11:54:22 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 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 "avfilter.h"
      25             : #include "formats.h"
      26             : #include "audio.h"
      27             : #include "video.h"
      28             : #include "internal.h"
      29             : 
      30             : enum DisplayScale   { LINEAR, SQRT, CBRT, LOG, RLOG, NB_SCALES };
      31             : enum AmplitudeScale { ALINEAR, ALOG, NB_ASCALES };
      32             : enum SlideMode      { REPLACE, SCROLL, NB_SLIDES };
      33             : enum DisplayMode    { SINGLE, SEPARATE, NB_DMODES };
      34             : enum HistogramMode  { ACCUMULATE, CURRENT, NB_HMODES };
      35             : 
      36             : typedef struct AudioHistogramContext {
      37             :     const AVClass *class;
      38             :     AVFrame *out;
      39             :     int w, h;
      40             :     AVRational frame_rate;
      41             :     uint64_t *achistogram;
      42             :     uint64_t *shistogram;
      43             :     int ascale;
      44             :     int scale;
      45             :     float phisto;
      46             :     int histogram_h;
      47             :     int apos;
      48             :     int ypos;
      49             :     int slide;
      50             :     int dmode;
      51             :     int dchannels;
      52             :     int count;
      53             :     int frame_count;
      54             :     float *combine_buffer;
      55             :     AVFrame *in[101];
      56             :     int first;
      57             : } AudioHistogramContext;
      58             : 
      59             : #define OFFSET(x) offsetof(AudioHistogramContext, x)
      60             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      61             : 
      62             : static const AVOption ahistogram_options[] = {
      63             :     { "dmode", "set method to display channels", OFFSET(dmode), AV_OPT_TYPE_INT, {.i64=SINGLE}, 0, NB_DMODES-1, FLAGS, "dmode" },
      64             :         { "single", "all channels use single histogram", 0, AV_OPT_TYPE_CONST, {.i64=SINGLE},   0, 0, FLAGS, "dmode" },
      65             :         { "separate", "each channel have own histogram", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "dmode" },
      66             :     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      67             :     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      68             :     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
      69             :     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
      70             :     { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
      71             :         { "log",  "logarithmic",         0, AV_OPT_TYPE_CONST, {.i64=LOG},    0, 0, FLAGS, "scale" },
      72             :         { "sqrt", "square root",         0, AV_OPT_TYPE_CONST, {.i64=SQRT},   0, 0, FLAGS, "scale" },
      73             :         { "cbrt", "cubic root",          0, AV_OPT_TYPE_CONST, {.i64=CBRT},   0, 0, FLAGS, "scale" },
      74             :         { "lin",  "linear",              0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
      75             :         { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=RLOG},   0, 0, FLAGS, "scale" },
      76             :     { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=ALOG}, LINEAR, NB_ASCALES-1, FLAGS, "ascale" },
      77             :         { "log",  "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=ALOG},    0, 0, FLAGS, "ascale" },
      78             :         { "lin",  "linear",      0, AV_OPT_TYPE_CONST, {.i64=ALINEAR}, 0, 0, FLAGS, "ascale" },
      79             :     { "acount", "how much frames to accumulate", OFFSET(count), AV_OPT_TYPE_INT, {.i64=1}, -1, 100, FLAGS },
      80             :     { "rheight", "set histogram ratio of window height", OFFSET(phisto), AV_OPT_TYPE_FLOAT, {.dbl=0.10}, 0, 1, FLAGS },
      81             :     { "slide", "set sonogram sliding", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=REPLACE}, 0, NB_SLIDES-1, FLAGS, "slide" },
      82             :         { "replace", "replace old rows with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE},    0, 0, FLAGS, "slide" },
      83             :         { "scroll",  "scroll from top to bottom", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
      84             :     { NULL }
      85             : };
      86             : 
      87             : AVFILTER_DEFINE_CLASS(ahistogram);
      88             : 
      89           0 : static int query_formats(AVFilterContext *ctx)
      90             : {
      91           0 :     AVFilterFormats *formats = NULL;
      92           0 :     AVFilterChannelLayouts *layouts = NULL;
      93           0 :     AVFilterLink *inlink = ctx->inputs[0];
      94           0 :     AVFilterLink *outlink = ctx->outputs[0];
      95             :     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
      96             :     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE };
      97           0 :     int ret = AVERROR(EINVAL);
      98             : 
      99           0 :     formats = ff_make_format_list(sample_fmts);
     100           0 :     if ((ret = ff_formats_ref         (formats, &inlink->out_formats        )) < 0 ||
     101           0 :         (layouts = ff_all_channel_counts()) == NULL ||
     102           0 :         (ret = ff_channel_layouts_ref (layouts, &inlink->out_channel_layouts)) < 0)
     103           0 :         return ret;
     104             : 
     105           0 :     formats = ff_all_samplerates();
     106           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
     107           0 :         return ret;
     108             : 
     109           0 :     formats = ff_make_format_list(pix_fmts);
     110           0 :     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
     111           0 :         return ret;
     112             : 
     113           0 :     return 0;
     114             : }
     115             : 
     116           0 : static int config_input(AVFilterLink *inlink)
     117             : {
     118           0 :     AVFilterContext *ctx = inlink->dst;
     119           0 :     AudioHistogramContext *s = ctx->priv;
     120             :     int nb_samples;
     121             : 
     122           0 :     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
     123           0 :     inlink->partial_buf_size =
     124           0 :     inlink->min_samples =
     125           0 :     inlink->max_samples = nb_samples;
     126             : 
     127           0 :     s->dchannels = s->dmode == SINGLE ? 1 : inlink->channels;
     128           0 :     s->shistogram = av_calloc(s->w, s->dchannels * sizeof(*s->shistogram));
     129           0 :     if (!s->shistogram)
     130           0 :         return AVERROR(ENOMEM);
     131             : 
     132           0 :     s->achistogram = av_calloc(s->w, s->dchannels * sizeof(*s->achistogram));
     133           0 :     if (!s->achistogram)
     134           0 :         return AVERROR(ENOMEM);
     135             : 
     136           0 :     return 0;
     137             : }
     138             : 
     139           0 : static int config_output(AVFilterLink *outlink)
     140             : {
     141           0 :     AudioHistogramContext *s = outlink->src->priv;
     142             : 
     143           0 :     outlink->w = s->w;
     144           0 :     outlink->h = s->h;
     145           0 :     outlink->sample_aspect_ratio = (AVRational){1,1};
     146           0 :     outlink->frame_rate = s->frame_rate;
     147             : 
     148           0 :     s->histogram_h = s->h * s->phisto;
     149           0 :     s->ypos = s->h * s->phisto;
     150             : 
     151           0 :     if (s->dmode == SEPARATE) {
     152           0 :         s->combine_buffer = av_malloc_array(outlink->w * 3, sizeof(*s->combine_buffer));
     153           0 :         if (!s->combine_buffer)
     154           0 :             return AVERROR(ENOMEM);
     155             :     }
     156             : 
     157           0 :     return 0;
     158             : }
     159             : 
     160           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     161             : {
     162           0 :     AVFilterContext *ctx = inlink->dst;
     163           0 :     AVFilterLink *outlink = ctx->outputs[0];
     164           0 :     AudioHistogramContext *s = ctx->priv;
     165           0 :     const int H = s->histogram_h;
     166           0 :     const int w = s->w;
     167             :     int c, y, n, p, bin;
     168           0 :     uint64_t acmax = 1;
     169             : 
     170           0 :     if (!s->out || s->out->width  != outlink->w ||
     171           0 :                    s->out->height != outlink->h) {
     172           0 :         av_frame_free(&s->out);
     173           0 :         s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     174           0 :         if (!s->out) {
     175           0 :             av_frame_free(&in);
     176           0 :             return AVERROR(ENOMEM);
     177             :         }
     178           0 :         for (n = H; n < s->h; n++) {
     179           0 :             memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
     180           0 :             memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
     181           0 :             memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
     182           0 :             memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
     183             :         }
     184             :     }
     185             : 
     186           0 :     if (s->dmode == SEPARATE) {
     187           0 :         for (y = 0; y < w; y++) {
     188           0 :             s->combine_buffer[3 * y    ] = 0;
     189           0 :             s->combine_buffer[3 * y + 1] = 127.5;
     190           0 :             s->combine_buffer[3 * y + 2] = 127.5;
     191             :         }
     192             :     }
     193             : 
     194           0 :     for (n = 0; n < H; n++) {
     195           0 :         memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
     196           0 :         memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
     197           0 :         memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
     198           0 :         memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
     199             :     }
     200           0 :     s->out->pts = in->pts;
     201             : 
     202           0 :     s->first = s->frame_count;
     203             : 
     204           0 :     switch (s->ascale) {
     205             :     case ALINEAR:
     206           0 :         for (c = 0; c < inlink->channels; c++) {
     207           0 :             const float *src = (const float *)in->extended_data[c];
     208           0 :             uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
     209             : 
     210           0 :             for (n = 0; n < in->nb_samples; n++) {
     211           0 :                 bin = lrint(av_clipf(fabsf(src[n]), 0, 1) * (w - 1));
     212             : 
     213           0 :                 achistogram[bin]++;
     214             :             }
     215             : 
     216           0 :             if (s->in[s->first] && s->count >= 0) {
     217           0 :                 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
     218           0 :                 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
     219             : 
     220           0 :                 for (n = 0; n < in->nb_samples; n++) {
     221           0 :                     bin = lrint(av_clipf(fabsf(src2[n]), 0, 1) * (w - 1));
     222             : 
     223           0 :                     shistogram[bin]++;
     224             :                 }
     225             :             }
     226             :         }
     227           0 :         break;
     228             :     case ALOG:
     229           0 :         for (c = 0; c < inlink->channels; c++) {
     230           0 :             const float *src = (const float *)in->extended_data[c];
     231           0 :             uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
     232             : 
     233           0 :             for (n = 0; n < in->nb_samples; n++) {
     234           0 :                 bin = lrint(av_clipf(1 + log10(fabsf(src[n])) / 6, 0, 1) * (w - 1));
     235             : 
     236           0 :                 achistogram[bin]++;
     237             :             }
     238             : 
     239           0 :             if (s->in[s->first] && s->count >= 0) {
     240           0 :                 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
     241           0 :                 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
     242             : 
     243           0 :                 for (n = 0; n < in->nb_samples; n++) {
     244           0 :                     bin = lrint(av_clipf(1 + log10(fabsf(src2[n])) / 6, 0, 1) * (w - 1));
     245             : 
     246           0 :                     shistogram[bin]++;
     247             :                 }
     248             :             }
     249             :         }
     250           0 :         break;
     251             :     }
     252             : 
     253           0 :     av_frame_free(&s->in[s->frame_count]);
     254           0 :     s->in[s->frame_count] = in;
     255           0 :     s->frame_count++;
     256           0 :     if (s->frame_count > s->count)
     257           0 :         s->frame_count = 0;
     258             : 
     259           0 :     for (n = 0; n < w * s->dchannels; n++) {
     260           0 :         acmax = FFMAX(s->achistogram[n] - s->shistogram[n], acmax);
     261             :     }
     262             : 
     263           0 :     for (c = 0; c < s->dchannels; c++) {
     264           0 :         uint64_t *shistogram  = &s->shistogram[c * w];
     265           0 :         uint64_t *achistogram = &s->achistogram[c * w];
     266             :         float yf, uf, vf;
     267             : 
     268           0 :         if (s->dmode == SEPARATE) {
     269           0 :             yf = 256.0f / s->dchannels;
     270           0 :             uf = yf * M_PI;
     271           0 :             vf = yf * M_PI;
     272           0 :             uf *= 0.5 * sin((2 * M_PI * c) / s->dchannels);
     273           0 :             vf *= 0.5 * cos((2 * M_PI * c) / s->dchannels);
     274             :         }
     275             : 
     276           0 :         for (n = 0; n < w; n++) {
     277             :             double a, aa;
     278             :             int h;
     279             : 
     280           0 :             a = achistogram[n] - shistogram[n];
     281             : 
     282           0 :             switch (s->scale) {
     283             :             case LINEAR:
     284           0 :                 aa = a / (double)acmax;
     285           0 :                 break;
     286             :             case SQRT:
     287           0 :                 aa = sqrt(a) / sqrt(acmax);
     288           0 :                 break;
     289             :             case CBRT:
     290           0 :                 aa = cbrt(a) / cbrt(acmax);
     291           0 :                 break;
     292             :             case LOG:
     293           0 :                 aa = log2(a + 1) / log2(acmax + 1);
     294           0 :                 break;
     295             :             case RLOG:
     296           0 :                 aa = 1. - log2(a + 1) / log2(acmax + 1);
     297           0 :                 if (aa == 1.)
     298           0 :                     aa = 0;
     299           0 :                 break;
     300             :             default:
     301           0 :                 av_assert0(0);
     302             :             }
     303             : 
     304           0 :             h = aa * (H - 1);
     305             : 
     306           0 :             if (s->dmode == SINGLE) {
     307             : 
     308           0 :                 for (y = H - h; y < H; y++) {
     309           0 :                     s->out->data[0][y * s->out->linesize[0] + n] = 255;
     310           0 :                     s->out->data[3][y * s->out->linesize[0] + n] = 255;
     311             :                 }
     312             : 
     313           0 :                 if (s->h - H > 0) {
     314           0 :                     h = aa * 255;
     315             : 
     316           0 :                     s->out->data[0][s->ypos * s->out->linesize[0] + n] = h;
     317           0 :                     s->out->data[1][s->ypos * s->out->linesize[1] + n] = 127;
     318           0 :                     s->out->data[2][s->ypos * s->out->linesize[2] + n] = 127;
     319           0 :                     s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
     320             :                 }
     321           0 :             } else if (s->dmode == SEPARATE) {
     322           0 :                 float *out = &s->combine_buffer[3 * n];
     323             :                 int old;
     324             : 
     325           0 :                 old = s->out->data[0][(H - h) * s->out->linesize[0] + n];
     326           0 :                 for (y = H - h; y < H; y++) {
     327           0 :                     if (s->out->data[0][y * s->out->linesize[0] + n] != old)
     328           0 :                         break;
     329           0 :                     old = s->out->data[0][y * s->out->linesize[0] + n];
     330           0 :                     s->out->data[0][y * s->out->linesize[0] + n] = yf;
     331           0 :                     s->out->data[1][y * s->out->linesize[1] + n] = 128+uf;
     332           0 :                     s->out->data[2][y * s->out->linesize[2] + n] = 128+vf;
     333           0 :                     s->out->data[3][y * s->out->linesize[3] + n] = 255;
     334             :                 }
     335             : 
     336           0 :                 out[0] += aa * yf;
     337           0 :                 out[1] += aa * uf;
     338           0 :                 out[2] += aa * vf;
     339             :             }
     340             :         }
     341             :     }
     342             : 
     343           0 :     if (s->h - H > 0) {
     344           0 :         if (s->dmode == SEPARATE) {
     345           0 :             for (n = 0; n < w; n++) {
     346           0 :                 float *cb = &s->combine_buffer[3 * n];
     347             : 
     348           0 :                 s->out->data[0][s->ypos * s->out->linesize[0] + n] = cb[0];
     349           0 :                 s->out->data[1][s->ypos * s->out->linesize[1] + n] = cb[1];
     350           0 :                 s->out->data[2][s->ypos * s->out->linesize[2] + n] = cb[2];
     351           0 :                 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
     352             :             }
     353             :         }
     354             : 
     355           0 :         if (s->slide == SCROLL) {
     356           0 :             for (p = 0; p < 4; p++) {
     357           0 :                 for (y = s->h; y >= H + 1; y--) {
     358           0 :                     memmove(s->out->data[p] + (y  ) * s->out->linesize[p],
     359           0 :                             s->out->data[p] + (y-1) * s->out->linesize[p], w);
     360             :                 }
     361             :             }
     362             :         }
     363             : 
     364           0 :         s->ypos++;
     365           0 :         if (s->slide == SCROLL || s->ypos >= s->h)
     366           0 :             s->ypos = H;
     367             :     }
     368             : 
     369           0 :     return ff_filter_frame(outlink, av_frame_clone(s->out));
     370             : }
     371             : 
     372           0 : static av_cold void uninit(AVFilterContext *ctx)
     373             : {
     374           0 :     AudioHistogramContext *s = ctx->priv;
     375             :     int i;
     376             : 
     377           0 :     av_frame_free(&s->out);
     378           0 :     av_freep(&s->shistogram);
     379           0 :     av_freep(&s->achistogram);
     380           0 :     av_freep(&s->combine_buffer);
     381           0 :     for (i = 0; i < 101; i++)
     382           0 :         av_frame_free(&s->in[i]);
     383           0 : }
     384             : 
     385             : static const AVFilterPad audiovectorscope_inputs[] = {
     386             :     {
     387             :         .name         = "default",
     388             :         .type         = AVMEDIA_TYPE_AUDIO,
     389             :         .config_props = config_input,
     390             :         .filter_frame = filter_frame,
     391             :     },
     392             :     { NULL }
     393             : };
     394             : 
     395             : static const AVFilterPad audiovectorscope_outputs[] = {
     396             :     {
     397             :         .name         = "default",
     398             :         .type         = AVMEDIA_TYPE_VIDEO,
     399             :         .config_props = config_output,
     400             :     },
     401             :     { NULL }
     402             : };
     403             : 
     404             : AVFilter ff_avf_ahistogram = {
     405             :     .name          = "ahistogram",
     406             :     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to histogram video output."),
     407             :     .uninit        = uninit,
     408             :     .query_formats = query_formats,
     409             :     .priv_size     = sizeof(AudioHistogramContext),
     410             :     .inputs        = audiovectorscope_inputs,
     411             :     .outputs       = audiovectorscope_outputs,
     412             :     .priv_class    = &ahistogram_class,
     413             : };

Generated by: LCOV version 1.12