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,
};
|
206f65b0 |
enum VectorScopeDraw {
DOT,
LINE,
DRAW_NB,
};
|
a130165e |
enum VectorScopeScale {
LIN,
SQRT,
CBRT,
LOG,
SCALE_NB,
};
|
005ee7a5 |
typedef struct AudioVectorScopeContext {
const AVClass *class;
AVFrame *outpicref;
int w, h;
int hw, hh; |
27a5d09c |
int mode; |
206f65b0 |
int draw; |
a130165e |
int scale; |
2bdd026b |
int contrast[4];
int fade[4]; |
005ee7a5 |
double zoom; |
afd2bf54 |
int swap;
int mirror; |
206f65b0 |
unsigned prev_x, prev_y; |
005ee7a5 |
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" }, |
f2c8b666 |
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
{ "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS }, |
005ee7a5 |
{ "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 }, |
2c10f054 |
{ "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, FLAGS }, |
206f65b0 |
{ "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
{ "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
{ "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" }, |
a130165e |
{ "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, FLAGS, "scale" },
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, FLAGS, "scale" },
{ "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
{ "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" }, |
afd2bf54 |
{ "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
{ "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=2}, 0, 3, FLAGS, "mirror" },
{ "none", "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mirror" },
{ "x", "mirror x", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mirror" },
{ "y", "mirror y", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mirror" },
{ "xy", "mirror both", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "mirror" }, |
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 |
}
|
206f65b0 |
static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
{
int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy) / 2, e2;
for (;;) {
draw_dot(s, x0, y0);
if (x0 == x1 && y0 == y1)
break;
e2 = err;
if (e2 >-dx) {
err -= dy;
x0 += sx;
}
if (e2 < dy) {
err += dx;
y0 += sy;
}
}
}
|
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 }; |
6aaac24d |
int ret; |
005ee7a5 |
formats = ff_make_format_list(sample_fmts); |
6aaac24d |
if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
(ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
(ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
return ret; |
005ee7a5 |
formats = ff_all_samplerates(); |
6aaac24d |
if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
return ret; |
005ee7a5 |
formats = ff_make_format_list(pix_fmts); |
6aaac24d |
if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
return ret; |
005ee7a5 |
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 |
|
206f65b0 |
s->prev_x = s->hw = s->w / 2; |
eb313725 |
s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : 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; |
206f65b0 |
unsigned prev_x = s->prev_x, prev_y = s->prev_y; |
2c10f054 |
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 |
|
f85cad79 |
s->outpicref->sample_aspect_ratio = (AVRational){1,1}; |
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 |
|
2c10f054 |
if (zoom < 1) {
float max = 0;
switch (insamples->format) {
case AV_SAMPLE_FMT_S16: {
int16_t *samples = (int16_t *)insamples->data[0];
for (i = 0; i < insamples->nb_samples * 2; i++) {
float sample = samples[i] / (float)INT16_MAX;
max = FFMAX(FFABS(sample), max);
}
}
break;
case AV_SAMPLE_FMT_FLT: { |
a5e6cd79 |
float *samples = (float *)insamples->data[0]; |
2c10f054 |
|
a5e6cd79 |
for (i = 0; i < insamples->nb_samples * 2; i++) { |
2c10f054 |
max = FFMAX(FFABS(samples[i]), max);
}
}
break;
default:
av_assert2(0);
}
zoom = 1. / max;
}
|
a130165e |
for (i = 0; i < insamples->nb_samples; i++) {
int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
float *samplesf = (float *)insamples->data[0] + i * 2;
float src[2]; |
005ee7a5 |
|
a130165e |
switch (insamples->format) {
case AV_SAMPLE_FMT_S16:
src[0] = samples[0] / (float)INT16_MAX;
src[1] = samples[1] / (float)INT16_MAX;
break;
case AV_SAMPLE_FMT_FLT:
src[0] = samplesf[0];
src[1] = samplesf[1];
break; |
362f6c91 |
default:
av_assert2(0); |
005ee7a5 |
}
|
a130165e |
switch (s->scale) {
case SQRT:
src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
break;
case CBRT:
src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
break;
case LOG:
src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
break;
}
|
afd2bf54 |
if (s->mirror & 1)
src[0] = -src[0];
if (s->mirror & 2)
src[1] = -src[1];
if (s->swap)
FFSWAP(float, src[0], src[1]);;
|
a130165e |
if (s->mode == LISSAJOUS) {
x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
} else if (s->mode == LISSAJOUS_XY) {
x = (src[1] * zoom + 1) * hw;
y = (src[0] * zoom + 1) * hh;
} 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 * fabsf(cx + cy) * .7;
}
if (s->draw == DOT) {
draw_dot(s, x, y);
} else {
draw_line(s, x, y, prev_x, prev_y); |
005ee7a5 |
} |
a130165e |
prev_x = x;
prev_y = y; |
005ee7a5 |
}
|
206f65b0 |
s->prev_x = x, s->prev_y = y; |
005ee7a5 |
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,
}; |