libavfilter/avf_avectorscope.c
005ee7a5
 /*
  * Copyright (c) 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
  */
 
 /**
  * @file
7d8939a0
  * audio to video multimedia vectorscope filter
005ee7a5
  */
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "audio.h"
 #include "video.h"
 #include "internal.h"
 
 enum VectorScopeMode {
     LISSAJOUS,
     LISSAJOUS_XY,
4b0e1124
     POLAR,
005ee7a5
     MODE_NB,
 };
 
 typedef struct AudioVectorScopeContext {
     const AVClass *class;
     AVFrame *outpicref;
     int w, h;
     int hw, hh;
27a5d09c
     int mode;
2bdd026b
     int contrast[4];
     int fade[4];
005ee7a5
     double zoom;
     AVRational frame_rate;
 } AudioVectorScopeContext;
 
 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption avectorscope_options[] = {
     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
     { "lissajous",    "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS},    0, 0, FLAGS, "mode" },
     { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
4b0e1124
     { "polar",        "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR},        0, 0, FLAGS, "mode" },
005ee7a5
     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
2bdd026b
     { "rc", "set red contrast",   OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40},  0, 255, FLAGS },
005ee7a5
     { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
2bdd026b
     { "bc", "set blue contrast",  OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80},  0, 255, FLAGS },
     { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
005ee7a5
     { "rf", "set red fade",       OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
     { "gf", "set green fade",     OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
2bdd026b
     { "bf", "set blue fade",      OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
     { "af", "set alpha fade",     OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
     { "zoom", "set zoom factor",  OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1},  1, 10, FLAGS },
b211607b
     { NULL }
005ee7a5
 };
 
 AVFILTER_DEFINE_CLASS(avectorscope);
 
67aaa705
 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
005ee7a5
 {
67aaa705
     const int linesize = s->outpicref->linesize[0];
005ee7a5
     uint8_t *dst;
 
67aaa705
     if (s->zoom > 1) {
         if (y >= s->h || x >= s->w)
005ee7a5
             return;
     } else {
67aaa705
         y = FFMIN(y, s->h - 1);
         x = FFMIN(x, s->w - 1);
005ee7a5
     }
 
67aaa705
     dst = &s->outpicref->data[0][y * linesize + x * 4];
     dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
     dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
     dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
2bdd026b
     dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
005ee7a5
 }
 
67aaa705
 static void fade(AudioVectorScopeContext *s)
005ee7a5
 {
67aaa705
     const int linesize = s->outpicref->linesize[0];
005ee7a5
     int i, j;
 
67aaa705
     if (s->fade[0] || s->fade[1] || s->fade[2]) {
         uint8_t *d = s->outpicref->data[0];
         for (i = 0; i < s->h; i++) {
             for (j = 0; j < s->w*4; j+=4) {
                 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
                 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
                 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
2bdd026b
                 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
005ee7a5
             }
             d += linesize;
         }
     }
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats *formats = NULL;
     AVFilterChannelLayouts *layout = NULL;
     AVFilterLink *inlink = ctx->inputs[0];
     AVFilterLink *outlink = ctx->outputs[0];
     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
 
     formats = ff_make_format_list(sample_fmts);
     if (!formats)
         return AVERROR(ENOMEM);
     ff_formats_ref(formats, &inlink->out_formats);
 
     ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
     ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
 
     formats = ff_all_samplerates();
     if (!formats)
         return AVERROR(ENOMEM);
     ff_formats_ref(formats, &inlink->out_samplerates);
 
     formats = ff_make_format_list(pix_fmts);
     if (!formats)
         return AVERROR(ENOMEM);
     ff_formats_ref(formats, &outlink->in_formats);
 
     return 0;
 }
 
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
67aaa705
     AudioVectorScopeContext *s = ctx->priv;
005ee7a5
     int nb_samples;
 
67aaa705
     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
005ee7a5
     inlink->partial_buf_size =
     inlink->min_samples =
     inlink->max_samples = nb_samples;
 
     return 0;
 }
 
 static int config_output(AVFilterLink *outlink)
 {
67aaa705
     AudioVectorScopeContext *s = outlink->src->priv;
005ee7a5
 
67aaa705
     outlink->w = s->w;
     outlink->h = s->h;
005ee7a5
     outlink->sample_aspect_ratio = (AVRational){1,1};
67aaa705
     outlink->frame_rate = s->frame_rate;
005ee7a5
 
67aaa705
     s->hw = s->w / 2;
     s->hh = s->h / 2;
005ee7a5
 
     return 0;
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
67aaa705
     AudioVectorScopeContext *s = ctx->priv;
     const int hw = s->hw;
     const int hh = s->hh;
005ee7a5
     unsigned x, y;
67aaa705
     const double zoom = s->zoom;
005ee7a5
     int i;
 
67aaa705
     if (!s->outpicref || s->outpicref->width  != outlink->w ||
                          s->outpicref->height != outlink->h) {
         av_frame_free(&s->outpicref);
         s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!s->outpicref) {
005ee7a5
             av_frame_free(&insamples);
             return AVERROR(ENOMEM);
ec73bd1f
         }
005ee7a5
 
         for (i = 0; i < outlink->h; i++)
67aaa705
             memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
005ee7a5
     }
67aaa705
     s->outpicref->pts = insamples->pts;
005ee7a5
 
67aaa705
     fade(s);
005ee7a5
 
     switch (insamples->format) {
     case AV_SAMPLE_FMT_S16:
         for (i = 0; i < insamples->nb_samples; i++) {
             int16_t *src = (int16_t *)insamples->data[0] + i * 2;
 
67aaa705
             if (s->mode == LISSAJOUS) {
005ee7a5
                 x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
                 y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
4b0e1124
             } else if (s->mode == LISSAJOUS_XY) {
005ee7a5
                 x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
                 y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
4b0e1124
             } else {
                 float sx, sy, cx, cy;
 
                 sx = src[1] * zoom / (float)INT16_MAX;
                 sy = src[0] * zoom / (float)INT16_MAX;
                 cx = sx * sqrtf(1 - 0.5*sy*sy);
                 cy = sy * sqrtf(1 - 0.5*sx*sx);
                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
                 y = s->h - s->h * FFABS(cx + cy) * .7;
005ee7a5
             }
 
67aaa705
             draw_dot(s, x, y);
005ee7a5
         }
         break;
     case AV_SAMPLE_FMT_FLT:
         for (i = 0; i < insamples->nb_samples; i++) {
             float *src = (float *)insamples->data[0] + i * 2;
 
67aaa705
             if (s->mode == LISSAJOUS) {
005ee7a5
                 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
                 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
4b0e1124
             } else if (s->mode == LISSAJOUS_XY){
005ee7a5
                 x = (src[1] * zoom + 1) * hw;
                 y = (src[0] * zoom + 1) * hh;
4b0e1124
             } else {
                 float sx, sy, cx, cy;
 
                 sx = src[1] * zoom;
                 sy = src[0] * zoom;
                 cx = sx * sqrtf(1 - 0.5 * sy * sy);
                 cy = sy * sqrtf(1 - 0.5 * sx * sx);
                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
                 y = s->h - s->h * FFABS(cx + cy) * .7;
005ee7a5
             }
 
67aaa705
             draw_dot(s, x, y);
005ee7a5
         }
         break;
     }
 
     av_frame_free(&insamples);
 
67aaa705
     return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
005ee7a5
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
67aaa705
     AudioVectorScopeContext *s = ctx->priv;
005ee7a5
 
67aaa705
     av_frame_free(&s->outpicref);
005ee7a5
 }
 
 static const AVFilterPad audiovectorscope_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .config_props = config_input,
         .filter_frame = filter_frame,
     },
     { NULL }
 };
 
 static const AVFilterPad audiovectorscope_outputs[] = {
     {
b211607b
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_output,
005ee7a5
     },
     { NULL }
 };
 
325f6e0a
 AVFilter ff_avf_avectorscope = {
005ee7a5
     .name          = "avectorscope",
7d8939a0
     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
005ee7a5
     .uninit        = uninit,
     .query_formats = query_formats,
     .priv_size     = sizeof(AudioVectorScopeContext),
     .inputs        = audiovectorscope_inputs,
     .outputs       = audiovectorscope_outputs,
     .priv_class    = &avectorscope_class,
 };