avfilter/showvolume : add display scale option

linear (current behaviour) or log display (more close to classic audio meter)
pull/285/head
Martin Vignali 7 years ago
parent e1c8bd2389
commit cb2f52b6a7
  1. 4
      doc/filters.texi
  2. 28
      libavfilter/avf_showvolume.c

@ -20014,6 +20014,10 @@ Set background opacity, allowed range is [0, 1]. Default is 0.
@item m @item m
Set metering mode, can be peak: @code{p} or rms: @code{r}, Set metering mode, can be peak: @code{p} or rms: @code{r},
default is @code{p}. default is @code{p}.
@item ds
Set display scale, can be linear: @code{lin} or log: @code{log},
default is @code{lin}.
@end table @end table
@section showwaves @section showwaves

@ -33,6 +33,7 @@
static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL }; static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL };
enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB }; enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
enum DisplayScale { LINEAR, LOG, NB_DISPLAY_SCALE };
typedef struct ShowVolumeContext { typedef struct ShowVolumeContext {
const AVClass *class; const AVClass *class;
@ -54,6 +55,7 @@ typedef struct ShowVolumeContext {
uint32_t *color_lut; uint32_t *color_lut;
float *max; float *max;
float rms_factor; float rms_factor;
int display_scale;
void (*meter)(float *src, int nb_samples, float *max, float factor); void (*meter)(float *src, int nb_samples, float *max, float factor);
} ShowVolumeContext; } ShowVolumeContext;
@ -79,6 +81,9 @@ static const AVOption showvolume_options[] = {
{ "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" }, { "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" }, { "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" }, { "r", "rms", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
{ "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" },
{ NULL } { NULL }
}; };
@ -256,13 +261,28 @@ static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink) {
} }
} }
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;
}
}
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
{ {
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0]; AVFilterLink *outlink = ctx->outputs[0];
ShowVolumeContext *s = ctx->priv; ShowVolumeContext *s = ctx->priv;
const int step = s->step; const int step = s->step;
int c, j, k; int c, j, k, max_draw;
AVFrame *out; AVFrame *out;
if (!s->out || s->out->width != outlink->w || if (!s->out || s->out->width != outlink->w ||
@ -304,8 +324,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max); s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
max = av_clipf(max, 0, 1); max = av_clipf(max, 0, 1);
max_draw = calc_max_draw(s, outlink, max);
for (j = outlink->h - outlink->h * max; j < s->w; j++) { for (j = max_draw; j < s->w; j++) {
uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4; 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++) { for (k = 0; k < s->h; k++) {
AV_WN32A(&dst[k * 4], lut[s->w - j - 1]); AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
@ -332,11 +353,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max); s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
max = av_clipf(max, 0, 1); max = av_clipf(max, 0, 1);
max_draw = calc_max_draw(s, outlink, max);
for (j = 0; j < s->h; j++) { 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]; 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++) { for (k = 0; k < max_draw; k++) {
AV_WN32A(dst + k * 4, lut[k]); AV_WN32A(dst + k * 4, lut[k]);
if (k & step) if (k & step)
k += step; k += step;

Loading…
Cancel
Save