LCOV - code coverage report
Current view: top level - libavfilter - avf_avectorscope.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 165 0.0 %
Date: 2017-12-17 23:02:56 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 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             : /**
      22             :  * @file
      23             :  * audio to video multimedia vectorscope filter
      24             :  */
      25             : 
      26             : #include "libavutil/avassert.h"
      27             : #include "libavutil/channel_layout.h"
      28             : #include "libavutil/opt.h"
      29             : #include "libavutil/parseutils.h"
      30             : #include "avfilter.h"
      31             : #include "formats.h"
      32             : #include "audio.h"
      33             : #include "video.h"
      34             : #include "internal.h"
      35             : 
      36             : enum VectorScopeMode {
      37             :     LISSAJOUS,
      38             :     LISSAJOUS_XY,
      39             :     POLAR,
      40             :     MODE_NB,
      41             : };
      42             : 
      43             : enum VectorScopeDraw {
      44             :     DOT,
      45             :     LINE,
      46             :     DRAW_NB,
      47             : };
      48             : 
      49             : enum VectorScopeScale {
      50             :     LIN,
      51             :     SQRT,
      52             :     CBRT,
      53             :     LOG,
      54             :     SCALE_NB,
      55             : };
      56             : 
      57             : typedef struct AudioVectorScopeContext {
      58             :     const AVClass *class;
      59             :     AVFrame *outpicref;
      60             :     int w, h;
      61             :     int hw, hh;
      62             :     int mode;
      63             :     int draw;
      64             :     int scale;
      65             :     int contrast[4];
      66             :     int fade[4];
      67             :     double zoom;
      68             :     int swap;
      69             :     int mirror;
      70             :     unsigned prev_x, prev_y;
      71             :     AVRational frame_rate;
      72             : } AudioVectorScopeContext;
      73             : 
      74             : #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
      75             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      76             : 
      77             : static const AVOption avectorscope_options[] = {
      78             :     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
      79             :     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
      80             :     { "lissajous",    "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS},    0, 0, FLAGS, "mode" },
      81             :     { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
      82             :     { "polar",        "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR},        0, 0, FLAGS, "mode" },
      83             :     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      84             :     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      85             :     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
      86             :     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
      87             :     { "rc", "set red contrast",   OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40},  0, 255, FLAGS },
      88             :     { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
      89             :     { "bc", "set blue contrast",  OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80},  0, 255, FLAGS },
      90             :     { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
      91             :     { "rf", "set red fade",       OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
      92             :     { "gf", "set green fade",     OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
      93             :     { "bf", "set blue fade",      OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
      94             :     { "af", "set alpha fade",     OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
      95             :     { "zoom", "set zoom factor",  OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 10, FLAGS },
      96             :     { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
      97             :     { "dot",   "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
      98             :     { "line",  "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
      99             :     { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, FLAGS, "scale" },
     100             :     { "lin",   "linear",      0, AV_OPT_TYPE_CONST, {.i64=LIN},  0, 0, FLAGS, "scale" },
     101             :     { "sqrt",  "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
     102             :     { "cbrt",  "cube root",   0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
     103             :     { "log",   "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG},  0, 0, FLAGS, "scale" },
     104             :     { "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
     105             :     { "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=2}, 0, 3, FLAGS, "mirror" },
     106             :     { "none",  "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mirror" },
     107             :     { "x",  "mirror x",     0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mirror" },
     108             :     { "y",  "mirror y",     0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mirror" },
     109             :     { "xy", "mirror both",  0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "mirror" },
     110             :     { NULL }
     111             : };
     112             : 
     113             : AVFILTER_DEFINE_CLASS(avectorscope);
     114             : 
     115           0 : static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
     116             : {
     117           0 :     const int linesize = s->outpicref->linesize[0];
     118             :     uint8_t *dst;
     119             : 
     120           0 :     if (s->zoom > 1) {
     121           0 :         if (y >= s->h || x >= s->w)
     122           0 :             return;
     123             :     } else {
     124           0 :         y = FFMIN(y, s->h - 1);
     125           0 :         x = FFMIN(x, s->w - 1);
     126             :     }
     127             : 
     128           0 :     dst = &s->outpicref->data[0][y * linesize + x * 4];
     129           0 :     dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
     130           0 :     dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
     131           0 :     dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
     132           0 :     dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
     133             : }
     134             : 
     135           0 : static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
     136             : {
     137           0 :     int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
     138           0 :     int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
     139           0 :     int err = (dx>dy ? dx : -dy) / 2, e2;
     140             : 
     141             :     for (;;) {
     142           0 :         draw_dot(s, x0, y0);
     143             : 
     144           0 :         if (x0 == x1 && y0 == y1)
     145           0 :             break;
     146             : 
     147           0 :         e2 = err;
     148             : 
     149           0 :         if (e2 >-dx) {
     150           0 :             err -= dy;
     151           0 :             x0 += sx;
     152             :         }
     153             : 
     154           0 :         if (e2 < dy) {
     155           0 :             err += dx;
     156           0 :             y0 += sy;
     157             :         }
     158             :     }
     159           0 : }
     160             : 
     161           0 : static void fade(AudioVectorScopeContext *s)
     162             : {
     163           0 :     const int linesize = s->outpicref->linesize[0];
     164             :     int i, j;
     165             : 
     166           0 :     if (s->fade[0] || s->fade[1] || s->fade[2]) {
     167           0 :         uint8_t *d = s->outpicref->data[0];
     168           0 :         for (i = 0; i < s->h; i++) {
     169           0 :             for (j = 0; j < s->w*4; j+=4) {
     170           0 :                 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
     171           0 :                 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
     172           0 :                 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
     173           0 :                 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
     174             :             }
     175           0 :             d += linesize;
     176             :         }
     177             :     }
     178           0 : }
     179             : 
     180           0 : static int query_formats(AVFilterContext *ctx)
     181             : {
     182           0 :     AVFilterFormats *formats = NULL;
     183           0 :     AVFilterChannelLayouts *layout = NULL;
     184           0 :     AVFilterLink *inlink = ctx->inputs[0];
     185           0 :     AVFilterLink *outlink = ctx->outputs[0];
     186             :     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
     187             :     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
     188             :     int ret;
     189             : 
     190           0 :     formats = ff_make_format_list(sample_fmts);
     191           0 :     if ((ret = ff_formats_ref         (formats, &inlink->out_formats        )) < 0 ||
     192           0 :         (ret = ff_add_channel_layout  (&layout, AV_CH_LAYOUT_STEREO         )) < 0 ||
     193           0 :         (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
     194           0 :         return ret;
     195             : 
     196           0 :     formats = ff_all_samplerates();
     197           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
     198           0 :         return ret;
     199             : 
     200           0 :     formats = ff_make_format_list(pix_fmts);
     201           0 :     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
     202           0 :         return ret;
     203             : 
     204           0 :     return 0;
     205             : }
     206             : 
     207           0 : static int config_input(AVFilterLink *inlink)
     208             : {
     209           0 :     AVFilterContext *ctx = inlink->dst;
     210           0 :     AudioVectorScopeContext *s = ctx->priv;
     211             :     int nb_samples;
     212             : 
     213           0 :     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
     214           0 :     inlink->partial_buf_size =
     215           0 :     inlink->min_samples =
     216           0 :     inlink->max_samples = nb_samples;
     217             : 
     218           0 :     return 0;
     219             : }
     220             : 
     221           0 : static int config_output(AVFilterLink *outlink)
     222             : {
     223           0 :     AudioVectorScopeContext *s = outlink->src->priv;
     224             : 
     225           0 :     outlink->w = s->w;
     226           0 :     outlink->h = s->h;
     227           0 :     outlink->sample_aspect_ratio = (AVRational){1,1};
     228           0 :     outlink->frame_rate = s->frame_rate;
     229             : 
     230           0 :     s->prev_x = s->hw = s->w / 2;
     231           0 :     s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
     232             : 
     233           0 :     return 0;
     234             : }
     235             : 
     236           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     237             : {
     238           0 :     AVFilterContext *ctx = inlink->dst;
     239           0 :     AVFilterLink *outlink = ctx->outputs[0];
     240           0 :     AudioVectorScopeContext *s = ctx->priv;
     241           0 :     const int hw = s->hw;
     242           0 :     const int hh = s->hh;
     243             :     unsigned x, y;
     244           0 :     unsigned prev_x = s->prev_x, prev_y = s->prev_y;
     245           0 :     double zoom = s->zoom;
     246             :     int i;
     247             : 
     248           0 :     if (!s->outpicref || s->outpicref->width  != outlink->w ||
     249           0 :                          s->outpicref->height != outlink->h) {
     250           0 :         av_frame_free(&s->outpicref);
     251           0 :         s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     252           0 :         if (!s->outpicref) {
     253           0 :             av_frame_free(&insamples);
     254           0 :             return AVERROR(ENOMEM);
     255             :         }
     256             : 
     257           0 :         s->outpicref->sample_aspect_ratio = (AVRational){1,1};
     258           0 :         for (i = 0; i < outlink->h; i++)
     259           0 :             memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
     260             :     }
     261           0 :     s->outpicref->pts = insamples->pts;
     262             : 
     263           0 :     fade(s);
     264             : 
     265           0 :     if (zoom < 1) {
     266           0 :         float max = 0;
     267             : 
     268           0 :         switch (insamples->format) {
     269           0 :         case AV_SAMPLE_FMT_S16: {
     270           0 :             int16_t *samples = (int16_t *)insamples->data[0];
     271             : 
     272           0 :             for (i = 0; i < insamples->nb_samples * 2; i++) {
     273           0 :                 float sample = samples[i] / (float)INT16_MAX;
     274           0 :                 max = FFMAX(FFABS(sample), max);
     275             :             }
     276             : 
     277             :             }
     278           0 :             break;
     279           0 :         case AV_SAMPLE_FMT_FLT: {
     280           0 :             float *samples = (float *)insamples->data[0];
     281             : 
     282           0 :             for (i = 0; i < insamples->nb_samples * 2; i++) {
     283           0 :                 max = FFMAX(FFABS(samples[i]), max);
     284             :             }
     285             :             }
     286           0 :             break;
     287           0 :         default:
     288             :             av_assert2(0);
     289             :         }
     290             : 
     291           0 :         zoom = 1. / max;
     292             :     }
     293             : 
     294           0 :     for (i = 0; i < insamples->nb_samples; i++) {
     295           0 :         int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
     296           0 :         float *samplesf = (float *)insamples->data[0] + i * 2;
     297             :         float src[2];
     298             : 
     299           0 :         switch (insamples->format) {
     300           0 :         case AV_SAMPLE_FMT_S16:
     301           0 :             src[0] = samples[0] / (float)INT16_MAX;
     302           0 :             src[1] = samples[1] / (float)INT16_MAX;
     303           0 :             break;
     304           0 :         case AV_SAMPLE_FMT_FLT:
     305           0 :             src[0] = samplesf[0];
     306           0 :             src[1] = samplesf[1];
     307           0 :             break;
     308           0 :         default:
     309             :             av_assert2(0);
     310             :         }
     311             : 
     312           0 :         switch (s->scale) {
     313           0 :         case SQRT:
     314           0 :             src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
     315           0 :             src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
     316           0 :             break;
     317           0 :         case CBRT:
     318           0 :             src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
     319           0 :             src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
     320           0 :             break;
     321           0 :         case LOG:
     322           0 :             src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
     323           0 :             src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
     324           0 :             break;
     325             :         }
     326             : 
     327           0 :         if (s->mirror & 1)
     328           0 :             src[0] = -src[0];
     329             : 
     330           0 :         if (s->mirror & 2)
     331           0 :             src[1] = -src[1];
     332             : 
     333           0 :         if (s->swap)
     334           0 :             FFSWAP(float, src[0], src[1]);;
     335             : 
     336           0 :         if (s->mode == LISSAJOUS) {
     337           0 :             x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
     338           0 :             y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
     339           0 :         } else if (s->mode == LISSAJOUS_XY) {
     340           0 :             x = (src[1] * zoom + 1) * hw;
     341           0 :             y = (src[0] * zoom + 1) * hh;
     342             :         } else {
     343             :             float sx, sy, cx, cy;
     344             : 
     345           0 :             sx = src[1] * zoom;
     346           0 :             sy = src[0] * zoom;
     347           0 :             cx = sx * sqrtf(1 - 0.5 * sy * sy);
     348           0 :             cy = sy * sqrtf(1 - 0.5 * sx * sx);
     349           0 :             x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
     350           0 :             y = s->h - s->h * fabsf(cx + cy) * .7;
     351             :         }
     352             : 
     353           0 :         if (s->draw == DOT) {
     354           0 :             draw_dot(s, x, y);
     355             :         } else {
     356           0 :             draw_line(s, x, y, prev_x, prev_y);
     357             :         }
     358           0 :         prev_x = x;
     359           0 :         prev_y = y;
     360             :     }
     361             : 
     362           0 :     s->prev_x = x, s->prev_y = y;
     363           0 :     av_frame_free(&insamples);
     364             : 
     365           0 :     return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
     366             : }
     367             : 
     368           0 : static av_cold void uninit(AVFilterContext *ctx)
     369             : {
     370           0 :     AudioVectorScopeContext *s = ctx->priv;
     371             : 
     372           0 :     av_frame_free(&s->outpicref);
     373           0 : }
     374             : 
     375             : static const AVFilterPad audiovectorscope_inputs[] = {
     376             :     {
     377             :         .name         = "default",
     378             :         .type         = AVMEDIA_TYPE_AUDIO,
     379             :         .config_props = config_input,
     380             :         .filter_frame = filter_frame,
     381             :     },
     382             :     { NULL }
     383             : };
     384             : 
     385             : static const AVFilterPad audiovectorscope_outputs[] = {
     386             :     {
     387             :         .name         = "default",
     388             :         .type         = AVMEDIA_TYPE_VIDEO,
     389             :         .config_props = config_output,
     390             :     },
     391             :     { NULL }
     392             : };
     393             : 
     394             : AVFilter ff_avf_avectorscope = {
     395             :     .name          = "avectorscope",
     396             :     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
     397             :     .uninit        = uninit,
     398             :     .query_formats = query_formats,
     399             :     .priv_size     = sizeof(AudioVectorScopeContext),
     400             :     .inputs        = audiovectorscope_inputs,
     401             :     .outputs       = audiovectorscope_outputs,
     402             :     .priv_class    = &avectorscope_class,
     403             : };

Generated by: LCOV version 1.13