2015-02-20 10:28:36 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2015-11-29 11:37:48 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2015-02-20 10:28:36 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
2015-06-29 07:38:16 +00:00
|
|
|
#include "libavutil/xga_font_data.h"
|
2015-02-20 10:28:36 +00:00
|
|
|
#include "avfilter.h"
|
2019-05-27 16:44:19 +00:00
|
|
|
#include "filters.h"
|
2015-02-20 10:28:36 +00:00
|
|
|
#include "formats.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "video.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2016-08-18 18:14:58 +00:00
|
|
|
static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL };
|
|
|
|
enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
|
2018-04-16 09:31:43 +00:00
|
|
|
enum DisplayScale { LINEAR, LOG, NB_DISPLAY_SCALE };
|
2015-11-29 11:37:48 +00:00
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
typedef struct ShowVolumeContext {
|
|
|
|
const AVClass *class;
|
|
|
|
int w, h;
|
2015-11-29 11:37:48 +00:00
|
|
|
int b;
|
|
|
|
double f;
|
2015-02-20 10:28:36 +00:00
|
|
|
AVRational frame_rate;
|
|
|
|
char *color;
|
2016-02-27 16:27:23 +00:00
|
|
|
int orientation;
|
|
|
|
int step;
|
2018-03-22 22:08:33 +00:00
|
|
|
float bgopacity;
|
2018-03-23 21:53:13 +00:00
|
|
|
int mode;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2019-05-27 16:44:19 +00:00
|
|
|
int nb_samples;
|
2015-02-20 10:28:36 +00:00
|
|
|
AVFrame *out;
|
|
|
|
AVExpr *c_expr;
|
2015-06-29 07:38:16 +00:00
|
|
|
int draw_text;
|
2015-11-29 11:37:48 +00:00
|
|
|
int draw_volume;
|
|
|
|
double *values;
|
2016-08-21 14:46:21 +00:00
|
|
|
uint32_t *color_lut;
|
2018-03-23 21:53:13 +00:00
|
|
|
float *max;
|
2018-04-16 09:31:43 +00:00
|
|
|
int display_scale;
|
2018-03-23 21:53:13 +00:00
|
|
|
|
2018-04-16 09:32:48 +00:00
|
|
|
double draw_persistent_duration; /* in second */
|
|
|
|
uint8_t persistant_max_rgba[4];
|
|
|
|
int persistent_max_frames; /* number of frames to check max value */
|
|
|
|
float *max_persistent; /* max value for draw_persistent_max for each channel */
|
|
|
|
int *nb_frames_max_display; /* number of frame for each channel, for displaying the max value */
|
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
void (*meter)(float *src, int nb_samples, float *max);
|
2015-02-20 10:28:36 +00:00
|
|
|
} ShowVolumeContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ShowVolumeContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption showvolume_options[] = {
|
2016-06-09 03:00:22 +00:00
|
|
|
{ "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 },
|
2015-02-20 10:28:36 +00:00
|
|
|
{ "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
|
2016-02-27 16:27:23 +00:00
|
|
|
{ "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
|
|
|
|
{ "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
|
2018-03-31 07:26:37 +00:00
|
|
|
{ "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0, 1, FLAGS },
|
2016-08-21 14:46:21 +00:00
|
|
|
{ "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
|
2015-09-08 21:28:44 +00:00
|
|
|
{ "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
|
2015-11-29 11:37:48 +00:00
|
|
|
{ "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
|
2018-04-16 09:32:48 +00:00
|
|
|
{ "dm", "duration for max value display", OFFSET(draw_persistent_duration), AV_OPT_TYPE_DOUBLE, {.dbl=0.}, 0, 9000, FLAGS},
|
2020-03-17 21:46:36 +00:00
|
|
|
{ "dmc","set color of the max value line", OFFSET(persistant_max_rgba), AV_OPT_TYPE_COLOR, {.str = "orange"}, 0, 0, FLAGS },
|
2016-02-27 16:27:23 +00:00
|
|
|
{ "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
|
|
|
|
{ "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
|
|
|
|
{ "v", "vertical", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
|
|
|
|
{ "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
|
2018-03-22 22:08:33 +00:00
|
|
|
{ "p", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
|
2018-03-23 21:53:13 +00:00
|
|
|
{ "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
|
|
|
|
{ "p", "peak", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "r", "rms", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
|
2018-04-16 09:31:43 +00:00
|
|
|
{ "ds", "set display scale", OFFSET(display_scale), AV_OPT_TYPE_INT, {.i64=LINEAR}, LINEAR, NB_DISPLAY_SCALE - 1, FLAGS, "display_scale" },
|
|
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "display_scale" },
|
|
|
|
{ "log", "log", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "display_scale" },
|
2015-02-20 10:28:36 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(showvolume);
|
|
|
|
|
|
|
|
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 };
|
2015-10-05 03:39:25 +00:00
|
|
|
int ret;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2016-02-28 21:52:44 +00:00
|
|
|
layouts = ff_all_channel_counts();
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
|
|
|
formats = ff_make_format_list(pix_fmts);
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
static void find_peak(float *src, int nb_samples, float *peak)
|
2018-03-23 21:53:13 +00:00
|
|
|
{
|
2022-12-06 09:37:40 +00:00
|
|
|
float max = 0.f;
|
2018-03-23 21:53:13 +00:00
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
max = 0;
|
|
|
|
for (int i = 0; i < nb_samples; i++)
|
|
|
|
max = fmaxf(max, fabsf(src[i]));
|
|
|
|
*peak = max;
|
2018-03-23 21:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
static void find_rms(float *src, int nb_samples, float *rms)
|
2018-03-23 21:53:13 +00:00
|
|
|
{
|
2022-12-06 09:37:40 +00:00
|
|
|
float sum = 0.f;
|
2018-03-23 21:53:13 +00:00
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
for (int i = 0; i < nb_samples; i++)
|
|
|
|
sum += src[i] * src[i];
|
|
|
|
*rms = sqrtf(sum / nb_samples);
|
2018-03-23 21:53:13 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
ShowVolumeContext *s = ctx->priv;
|
|
|
|
|
2019-05-28 08:49:09 +00:00
|
|
|
s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
|
2021-08-31 14:03:14 +00:00
|
|
|
s->values = av_calloc(inlink->ch_layout.nb_channels * VAR_VARS_NB, sizeof(double));
|
2015-11-29 11:37:48 +00:00
|
|
|
if (!s->values)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-08-21 14:46:21 +00:00
|
|
|
|
2021-08-31 14:03:14 +00:00
|
|
|
s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->ch_layout.nb_channels);
|
2016-08-21 14:46:21 +00:00
|
|
|
if (!s->color_lut)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-08-31 14:03:14 +00:00
|
|
|
s->max = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->max));
|
2018-03-23 21:53:13 +00:00
|
|
|
if (!s->max)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
switch (s->mode) {
|
|
|
|
case 0: s->meter = find_peak; break;
|
|
|
|
case 1: s->meter = find_rms; break;
|
|
|
|
default: return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
|
2018-04-16 09:32:48 +00:00
|
|
|
if (s->draw_persistent_duration > 0.) {
|
|
|
|
s->persistent_max_frames = (int) FFMAX(av_q2d(s->frame_rate) * s->draw_persistent_duration, 1.);
|
2021-08-31 14:03:14 +00:00
|
|
|
s->max_persistent = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->max_persistent));
|
|
|
|
s->nb_frames_max_display = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->nb_frames_max_display));
|
2022-04-13 16:18:49 +00:00
|
|
|
if (!s->max_persistent ||
|
|
|
|
!s->nb_frames_max_display)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-04-16 09:32:48 +00:00
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
ShowVolumeContext *s = outlink->src->priv;
|
|
|
|
AVFilterLink *inlink = outlink->src->inputs[0];
|
2016-08-21 14:46:21 +00:00
|
|
|
int ch;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2016-02-27 16:27:23 +00:00
|
|
|
if (s->orientation) {
|
|
|
|
outlink->h = s->w;
|
2021-08-31 14:03:14 +00:00
|
|
|
outlink->w = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b;
|
2016-02-27 16:27:23 +00:00
|
|
|
} else {
|
|
|
|
outlink->w = s->w;
|
2021-08-31 14:03:14 +00:00
|
|
|
outlink->h = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b;
|
2016-02-27 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
outlink->sample_aspect_ratio = (AVRational){1,1};
|
|
|
|
outlink->frame_rate = s->frame_rate;
|
2022-04-18 18:19:21 +00:00
|
|
|
outlink->time_base = av_inv_q(outlink->frame_rate);
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2021-08-31 14:03:14 +00:00
|
|
|
for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
|
2016-08-21 14:46:21 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->w; i++) {
|
|
|
|
float max = i / (float)(s->w - 1);
|
|
|
|
|
|
|
|
s->values[ch * VAR_VARS_NB + VAR_PEAK] = max;
|
|
|
|
s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
|
|
|
|
s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch;
|
|
|
|
s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-27 16:27:23 +00:00
|
|
|
static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
|
2015-06-29 07:38:16 +00:00
|
|
|
{
|
|
|
|
const uint8_t *font;
|
|
|
|
int font_height;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
font = avpriv_cga_font, font_height = 8;
|
|
|
|
|
|
|
|
for (i = 0; txt[i]; i++) {
|
|
|
|
int char_y, mask;
|
|
|
|
|
2018-03-30 19:13:27 +00:00
|
|
|
if (o) { /* vertical orientation */
|
2016-02-27 16:27:23 +00:00
|
|
|
for (char_y = font_height - 1; char_y >= 0; char_y--) {
|
|
|
|
uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
|
|
|
|
for (mask = 0x80; mask; mask >>= 1) {
|
|
|
|
if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
|
|
|
|
AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
|
|
|
|
p += pic->linesize[0];
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 19:13:27 +00:00
|
|
|
} else { /* horizontal orientation */
|
2016-02-27 16:27:23 +00:00
|
|
|
uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
|
|
|
|
for (char_y = 0; char_y < font_height; char_y++) {
|
|
|
|
for (mask = 0x80; mask; mask >>= 1) {
|
|
|
|
if (font[txt[i] * font_height + char_y] & mask)
|
|
|
|
AV_WN32(p, ~AV_RN32(p));
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
p += pic->linesize[0] - 8 * 4;
|
2015-06-29 07:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 09:33:14 +00:00
|
|
|
static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink)
|
|
|
|
{
|
2018-03-31 13:50:19 +00:00
|
|
|
int i, j;
|
|
|
|
const uint32_t bg = (uint32_t)(s->bgopacity * 255) << 24;
|
|
|
|
|
|
|
|
for (i = 0; i < outlink->h; i++) {
|
|
|
|
uint32_t *dst = (uint32_t *)(s->out->data[0] + i * s->out->linesize[0]);
|
|
|
|
for (j = 0; j < outlink->w; j++)
|
|
|
|
AV_WN32A(dst + j, bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 09:31:43 +00:00
|
|
|
static inline int calc_max_draw(ShowVolumeContext *s, AVFilterLink *outlink, float max)
|
|
|
|
{
|
|
|
|
float max_val;
|
|
|
|
if (s->display_scale == LINEAR) {
|
|
|
|
max_val = max;
|
|
|
|
} else { /* log */
|
|
|
|
max_val = av_clipf(0.21 * log10(max) + 1, 0, 1);
|
|
|
|
}
|
|
|
|
if (s->orientation) { /* vertical */
|
|
|
|
return outlink->h - outlink->h * max_val;
|
|
|
|
} else { /* horizontal */
|
|
|
|
return s->w * max_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 09:32:48 +00:00
|
|
|
static inline void calc_persistent_max(ShowVolumeContext *s, float max, int channel)
|
|
|
|
{
|
|
|
|
/* update max value for persistent max display */
|
|
|
|
if ((max >= s->max_persistent[channel]) || (s->nb_frames_max_display[channel] >= s->persistent_max_frames)) { /* update max value for display */
|
|
|
|
s->max_persistent[channel] = max;
|
|
|
|
s->nb_frames_max_display[channel] = 0;
|
|
|
|
} else {
|
|
|
|
s->nb_frames_max_display[channel] += 1; /* incremente display frame count */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void draw_max_line(ShowVolumeContext *s, int max_draw, int channel)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
if (s->orientation) { /* vertical */
|
|
|
|
uint8_t *dst = s->out->data[0] + max_draw * s->out->linesize[0] + channel * (s->b + s->h) * 4;
|
|
|
|
for (k = 0; k < s->h; k++) {
|
|
|
|
memcpy(dst + k * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
|
|
|
|
}
|
|
|
|
} else { /* horizontal */
|
|
|
|
for (k = 0; k < s->h; k++) {
|
|
|
|
uint8_t *dst = s->out->data[0] + (channel * s->h + channel * s->b + k) * s->out->linesize[0];
|
|
|
|
memcpy(dst + max_draw * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ShowVolumeContext *s = ctx->priv;
|
2016-02-27 16:27:23 +00:00
|
|
|
const int step = s->step;
|
2018-04-16 09:31:43 +00:00
|
|
|
int c, j, k, max_draw;
|
2021-08-31 14:03:14 +00:00
|
|
|
char channel_name[64];
|
2015-11-29 11:37:48 +00:00
|
|
|
AVFrame *out;
|
2015-02-20 10:28:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-03-31 13:50:19 +00:00
|
|
|
clear_picture(s, outlink);
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
2022-04-18 18:19:21 +00:00
|
|
|
s->out->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2018-03-31 13:50:19 +00:00
|
|
|
if ((s->f < 1.) && (s->f > 0.)) {
|
2018-03-30 19:16:47 +00:00
|
|
|
for (j = 0; j < outlink->h; j++) {
|
|
|
|
uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
|
|
|
|
const uint32_t alpha = s->bgopacity * 255;
|
|
|
|
|
|
|
|
for (k = 0; k < outlink->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, alpha);
|
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
2018-03-31 13:50:19 +00:00
|
|
|
} else if (s->f == 0.) {
|
|
|
|
clear_picture(s, outlink);
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 19:13:27 +00:00
|
|
|
if (s->orientation) { /* vertical */
|
2021-08-31 14:03:14 +00:00
|
|
|
for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
|
2016-02-27 16:27:23 +00:00
|
|
|
float *src = (float *)insamples->extended_data[c];
|
2016-08-21 14:46:21 +00:00
|
|
|
uint32_t *lut = s->color_lut + s->w * c;
|
2018-03-23 21:53:13 +00:00
|
|
|
float max;
|
2016-02-27 16:27:23 +00:00
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
s->meter(src, insamples->nb_samples, &s->max[c]);
|
2018-03-23 21:53:13 +00:00
|
|
|
max = s->max[c];
|
2016-02-27 16:27:23 +00:00
|
|
|
|
|
|
|
s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
|
|
|
|
max = av_clipf(max, 0, 1);
|
2018-04-16 09:31:43 +00:00
|
|
|
max_draw = calc_max_draw(s, outlink, max);
|
2016-02-27 16:27:23 +00:00
|
|
|
|
2018-04-16 09:31:43 +00:00
|
|
|
for (j = max_draw; j < s->w; j++) {
|
2016-02-27 16:27:23 +00:00
|
|
|
uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
|
|
|
|
for (k = 0; k < s->h; k++) {
|
2016-08-21 14:46:21 +00:00
|
|
|
AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
|
2016-02-27 16:27:23 +00:00
|
|
|
if (j & step)
|
|
|
|
j += step;
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2016-08-21 14:46:21 +00:00
|
|
|
if (s->h >= 8 && s->draw_text) {
|
2021-08-31 14:03:14 +00:00
|
|
|
int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&insamples->ch_layout, c));
|
|
|
|
if (ret < 0)
|
2016-02-28 21:52:44 +00:00
|
|
|
continue;
|
|
|
|
drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
|
|
|
|
}
|
2018-04-16 09:32:48 +00:00
|
|
|
|
|
|
|
if (s->draw_persistent_duration > 0.) {
|
|
|
|
calc_persistent_max(s, max, c);
|
|
|
|
max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
|
|
|
|
draw_max_line(s, max_draw, c);
|
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
2018-03-30 19:13:27 +00:00
|
|
|
} else { /* horizontal */
|
2021-08-31 14:03:14 +00:00
|
|
|
for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
|
2016-02-27 16:27:23 +00:00
|
|
|
float *src = (float *)insamples->extended_data[c];
|
2016-08-21 14:46:21 +00:00
|
|
|
uint32_t *lut = s->color_lut + s->w * c;
|
2018-03-23 21:53:13 +00:00
|
|
|
float max;
|
2016-02-27 16:27:23 +00:00
|
|
|
|
2022-12-06 09:37:40 +00:00
|
|
|
s->meter(src, insamples->nb_samples, &s->max[c]);
|
2018-03-23 21:53:13 +00:00
|
|
|
max = s->max[c];
|
2016-02-27 16:27:23 +00:00
|
|
|
|
|
|
|
s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
|
|
|
|
max = av_clipf(max, 0, 1);
|
2018-04-16 09:31:43 +00:00
|
|
|
max_draw = calc_max_draw(s, outlink, max);
|
2016-02-27 16:27:23 +00:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2018-04-16 09:31:43 +00:00
|
|
|
for (k = 0; k < max_draw; k++) {
|
2016-08-21 14:46:21 +00:00
|
|
|
AV_WN32A(dst + k * 4, lut[k]);
|
2016-02-27 16:27:23 +00:00
|
|
|
if (k & step)
|
|
|
|
k += step;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 07:38:16 +00:00
|
|
|
|
2016-02-28 21:52:44 +00:00
|
|
|
if (s->h >= 8 && s->draw_text) {
|
2021-08-31 14:03:14 +00:00
|
|
|
int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&insamples->ch_layout, c));
|
|
|
|
if (ret < 0)
|
2016-02-28 21:52:44 +00:00
|
|
|
continue;
|
|
|
|
drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
|
|
|
|
}
|
2018-04-16 09:32:48 +00:00
|
|
|
|
|
|
|
if (s->draw_persistent_duration > 0.) {
|
|
|
|
calc_persistent_max(s, max, c);
|
|
|
|
max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
|
|
|
|
draw_max_line(s, max_draw, c);
|
|
|
|
}
|
2016-02-27 16:27:23 +00:00
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&insamples);
|
2015-11-29 11:37:48 +00:00
|
|
|
out = av_frame_clone(s->out);
|
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
av_frame_make_writable(out);
|
|
|
|
|
2018-03-31 13:51:18 +00:00
|
|
|
/* draw volume level */
|
2021-08-31 14:03:14 +00:00
|
|
|
for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) {
|
2016-02-27 16:27:23 +00:00
|
|
|
char buf[16];
|
2018-03-31 13:50:53 +00:00
|
|
|
|
2018-03-31 13:51:18 +00:00
|
|
|
if (s->orientation) { /* vertical */
|
|
|
|
snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
|
|
|
|
drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
|
|
|
|
} else { /* horizontal */
|
|
|
|
snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
|
|
|
|
drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
|
2015-11-29 11:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 10:28:36 +00:00
|
|
|
|
2015-11-29 11:37:48 +00:00
|
|
|
return ff_filter_frame(outlink, out);
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:44:19 +00:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ShowVolumeContext *s = ctx->priv;
|
|
|
|
AVFrame *in = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret > 0)
|
|
|
|
return filter_frame(inlink, in);
|
|
|
|
|
2022-04-30 14:16:37 +00:00
|
|
|
if (ff_inlink_queued_samples(inlink) >= s->nb_samples) {
|
|
|
|
ff_filter_set_ready(ctx, 10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:44:19 +00:00
|
|
|
FF_FILTER_FORWARD_STATUS(inlink, outlink);
|
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2015-02-20 10:28:36 +00:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
ShowVolumeContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_frame_free(&s->out);
|
|
|
|
av_expr_free(s->c_expr);
|
2015-11-29 11:37:48 +00:00
|
|
|
av_freep(&s->values);
|
2016-08-21 14:46:21 +00:00
|
|
|
av_freep(&s->color_lut);
|
2018-03-23 21:53:13 +00:00
|
|
|
av_freep(&s->max);
|
2022-04-13 16:18:49 +00:00
|
|
|
av_freep(&s->max_persistent);
|
|
|
|
av_freep(&s->nb_frames_max_display);
|
2015-02-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad showvolume_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad showvolume_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_avf_showvolume = {
|
2015-02-20 10:28:36 +00:00
|
|
|
.name = "showvolume",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
|
|
|
|
.init = init,
|
2019-05-27 16:44:19 +00:00
|
|
|
.activate = activate,
|
2015-02-20 10:28:36 +00:00
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(ShowVolumeContext),
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(showvolume_inputs),
|
|
|
|
FILTER_OUTPUTS(showvolume_outputs),
|
2021-09-27 10:07:35 +00:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2015-02-20 10:28:36 +00:00
|
|
|
.priv_class = &showvolume_class,
|
|
|
|
};
|