avfilter: add showvolume filter
Signed-off-by: Paul B Mahol <onemda@gmail.com>
This commit is contained in:
parent
96e73fa649
commit
f76d7d4b01
@ -9,6 +9,7 @@ version <next>:
|
||||
- DirectDraw Surface image/texture decoder
|
||||
- ssim filter
|
||||
- rewritten ASF demuxer
|
||||
- showvolume filter
|
||||
|
||||
|
||||
version 2.7:
|
||||
|
@ -11992,6 +11992,39 @@ ffplay -f lavfi 'amovie=input.mp3, asplit [a][out1];
|
||||
@end example
|
||||
@end itemize
|
||||
|
||||
@section showvolume
|
||||
|
||||
Convert input audio volume to a video output.
|
||||
|
||||
The filter accepts the following options:
|
||||
|
||||
@table @option
|
||||
@item rate, r
|
||||
Set video rate.
|
||||
|
||||
@item b
|
||||
Set border width, allowed range is [0, 5]. Default is 1.
|
||||
|
||||
@item w
|
||||
Set channel width, allowed range is [40, 1080]. Default is 400.
|
||||
|
||||
@item h
|
||||
Set channel height, allowed range is [1, 100]. Default is 20.
|
||||
|
||||
@item f
|
||||
Set fade, allowed range is [1, 255]. Default is 20.
|
||||
|
||||
@item c
|
||||
Set volume color expression.
|
||||
|
||||
The expression can use the following variables:
|
||||
|
||||
@table @option
|
||||
@item VOLUME
|
||||
Current max volume of channel in dB.
|
||||
@end table
|
||||
@end table
|
||||
|
||||
@section showwaves
|
||||
|
||||
Convert input audio to a video output, representing the samples waves.
|
||||
|
@ -241,6 +241,7 @@ OBJS-$(CONFIG_AVECTORSCOPE_FILTER) += avf_avectorscope.o
|
||||
OBJS-$(CONFIG_CONCAT_FILTER) += avf_concat.o
|
||||
OBJS-$(CONFIG_SHOWCQT_FILTER) += avf_showcqt.o
|
||||
OBJS-$(CONFIG_SHOWSPECTRUM_FILTER) += avf_showspectrum.o
|
||||
OBJS-$(CONFIG_SHOWVOLUME_FILTER) += avf_showvolume.o
|
||||
OBJS-$(CONFIG_SHOWWAVES_FILTER) += avf_showwaves.o
|
||||
OBJS-$(CONFIG_SHOWWAVESPIC_FILTER) += avf_showwaves.o
|
||||
|
||||
|
@ -256,6 +256,7 @@ void avfilter_register_all(void)
|
||||
REGISTER_FILTER(CONCAT, concat, avf);
|
||||
REGISTER_FILTER(SHOWCQT, showcqt, avf);
|
||||
REGISTER_FILTER(SHOWSPECTRUM, showspectrum, avf);
|
||||
REGISTER_FILTER(SHOWVOLUME, showvolume, avf);
|
||||
REGISTER_FILTER(SHOWWAVES, showwaves, avf);
|
||||
REGISTER_FILTER(SHOWWAVESPIC, showwavespic, avf);
|
||||
|
||||
|
230
libavfilter/avf_showvolume.c
Normal file
230
libavfilter/avf_showvolume.c
Normal file
@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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
|
||||
*/
|
||||
|
||||
#include "libavutil/channel_layout.h"
|
||||
#include "libavutil/eval.h"
|
||||
#include "libavutil/intreadwrite.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "libavutil/parseutils.h"
|
||||
#include "avfilter.h"
|
||||
#include "formats.h"
|
||||
#include "audio.h"
|
||||
#include "video.h"
|
||||
#include "internal.h"
|
||||
|
||||
typedef struct ShowVolumeContext {
|
||||
const AVClass *class;
|
||||
int w, h;
|
||||
int f, b;
|
||||
AVRational frame_rate;
|
||||
char *color;
|
||||
|
||||
AVFrame *out;
|
||||
AVExpr *c_expr;
|
||||
} ShowVolumeContext;
|
||||
|
||||
#define OFFSET(x) offsetof(ShowVolumeContext, x)
|
||||
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
||||
|
||||
static const AVOption showvolume_options[] = {
|
||||
{ "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 },
|
||||
{ "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
|
||||
{ "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 40, 1080, FLAGS },
|
||||
{ "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 100, FLAGS },
|
||||
{ "f", "set fade", OFFSET(f), AV_OPT_TYPE_INT, {.i64=20}, 1, 255, FLAGS },
|
||||
{ "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-2), if(gte(VOLUME,-1),0xff, 0xffff),0xff00)"}, 0, 0, FLAGS },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
AVFILTER_DEFINE_CLASS(showvolume);
|
||||
|
||||
static const char *const var_names[] = { "VOLUME", NULL };
|
||||
enum { VAR_VOLUME, VAR_VARS_NB };
|
||||
|
||||
static av_cold int init(AVFilterContext *ctx)
|
||||
{
|
||||
ShowVolumeContext *s = ctx->priv;
|
||||
int ret;
|
||||
|
||||
if (s->color) {
|
||||
ret = av_expr_parse(&s->c_expr, s->color, var_names,
|
||||
NULL, NULL, NULL, NULL, 0, ctx);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int query_formats(AVFilterContext *ctx)
|
||||
{
|
||||
AVFilterFormats *formats = NULL;
|
||||
AVFilterChannelLayouts *layouts = NULL;
|
||||
AVFilterLink *inlink = ctx->inputs[0];
|
||||
AVFilterLink *outlink = ctx->outputs[0];
|
||||
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, 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);
|
||||
|
||||
layouts = ff_all_channel_layouts();
|
||||
if (!layouts)
|
||||
return AVERROR(ENOMEM);
|
||||
ff_channel_layouts_ref(layouts, &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;
|
||||
ShowVolumeContext *s = ctx->priv;
|
||||
int nb_samples;
|
||||
|
||||
nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
|
||||
inlink->partial_buf_size =
|
||||
inlink->min_samples =
|
||||
inlink->max_samples = nb_samples;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int config_output(AVFilterLink *outlink)
|
||||
{
|
||||
ShowVolumeContext *s = outlink->src->priv;
|
||||
AVFilterLink *inlink = outlink->src->inputs[0];
|
||||
|
||||
outlink->w = s->w;
|
||||
outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
|
||||
outlink->sample_aspect_ratio = (AVRational){1,1};
|
||||
outlink->frame_rate = s->frame_rate;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
||||
{
|
||||
AVFilterContext *ctx = inlink->dst;
|
||||
AVFilterLink *outlink = ctx->outputs[0];
|
||||
ShowVolumeContext *s = ctx->priv;
|
||||
int c, i, j, k;
|
||||
double values[VAR_VARS_NB];
|
||||
|
||||
if (!s->out || s->out->width != outlink->w ||
|
||||
s->out->height != outlink->h) {
|
||||
av_frame_free(&s->out);
|
||||
s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
||||
if (!s->out) {
|
||||
av_frame_free(&insamples);
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
for (i = 0; i < outlink->h; i++)
|
||||
memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
|
||||
}
|
||||
s->out->pts = insamples->pts;
|
||||
|
||||
for (j = 0; j < outlink->h; j++) {
|
||||
uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
|
||||
for (k = 0; k < s->w; k++) {
|
||||
dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] - s->f, 0);
|
||||
dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] - s->f, 0);
|
||||
dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] - s->f, 0);
|
||||
dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] - s->f, 0);
|
||||
}
|
||||
}
|
||||
|
||||
for (c = 0; c < inlink->channels; c++) {
|
||||
float *src = (float *)insamples->extended_data[c];
|
||||
float max = 0;
|
||||
int color;
|
||||
|
||||
for (i = 0; i < insamples->nb_samples; i++)
|
||||
max = FFMAX(max, src[i]);
|
||||
|
||||
max = av_clipf(max, 0, 1);
|
||||
values[VAR_VOLUME] = 20.0 * log(max) / M_LN10;
|
||||
color = av_expr_eval(s->c_expr, values, NULL);
|
||||
|
||||
for (j = 0; j < s->h; j++) {
|
||||
uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
|
||||
|
||||
for (k = 0; k < s->w * max; k++)
|
||||
AV_WN32A(dst + k * 4, color);
|
||||
}
|
||||
}
|
||||
|
||||
av_frame_free(&insamples);
|
||||
|
||||
return ff_filter_frame(outlink, av_frame_clone(s->out));
|
||||
}
|
||||
|
||||
static av_cold void uninit(AVFilterContext *ctx)
|
||||
{
|
||||
ShowVolumeContext *s = ctx->priv;
|
||||
|
||||
av_frame_free(&s->out);
|
||||
av_expr_free(s->c_expr);
|
||||
}
|
||||
|
||||
static const AVFilterPad showvolume_inputs[] = {
|
||||
{
|
||||
.name = "default",
|
||||
.type = AVMEDIA_TYPE_AUDIO,
|
||||
.config_props = config_input,
|
||||
.filter_frame = filter_frame,
|
||||
},
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static const AVFilterPad showvolume_outputs[] = {
|
||||
{
|
||||
.name = "default",
|
||||
.type = AVMEDIA_TYPE_VIDEO,
|
||||
.config_props = config_output,
|
||||
},
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
AVFilter ff_avf_showvolume = {
|
||||
.name = "showvolume",
|
||||
.description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
|
||||
.init = init,
|
||||
.uninit = uninit,
|
||||
.query_formats = query_formats,
|
||||
.priv_size = sizeof(ShowVolumeContext),
|
||||
.inputs = showvolume_inputs,
|
||||
.outputs = showvolume_outputs,
|
||||
.priv_class = &showvolume_class,
|
||||
};
|
@ -30,7 +30,7 @@
|
||||
#include "libavutil/version.h"
|
||||
|
||||
#define LIBAVFILTER_VERSION_MAJOR 5
|
||||
#define LIBAVFILTER_VERSION_MINOR 18
|
||||
#define LIBAVFILTER_VERSION_MINOR 19
|
||||
#define LIBAVFILTER_VERSION_MICRO 100
|
||||
|
||||
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
|
||||
|
Loading…
Reference in New Issue
Block a user