| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2015 Paul B Mahol | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavutil/channel_layout.h" | ||
| 22 | #include "libavutil/eval.h" | ||
| 23 | #include "libavutil/intreadwrite.h" | ||
| 24 | #include "libavutil/mem.h" | ||
| 25 | #include "libavutil/opt.h" | ||
| 26 | #include "libavutil/xga_font_data.h" | ||
| 27 | #include "avfilter.h" | ||
| 28 | #include "filters.h" | ||
| 29 | #include "formats.h" | ||
| 30 | #include "video.h" | ||
| 31 | |||
| 32 | static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL }; | ||
| 33 | enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB }; | ||
| 34 | enum DisplayScale { LINEAR, LOG, NB_DISPLAY_SCALE }; | ||
| 35 | |||
| 36 | typedef struct ShowVolumeContext { | ||
| 37 | const AVClass *class; | ||
| 38 | int w, h; | ||
| 39 | int b; | ||
| 40 | double f; | ||
| 41 | AVRational frame_rate; | ||
| 42 | char *color; | ||
| 43 | int orientation; | ||
| 44 | int step; | ||
| 45 | float bgopacity; | ||
| 46 | int mode; | ||
| 47 | |||
| 48 | int nb_samples; | ||
| 49 | AVFrame *out; | ||
| 50 | AVExpr *c_expr; | ||
| 51 | int draw_text; | ||
| 52 | int draw_volume; | ||
| 53 | double *values; | ||
| 54 | uint32_t *color_lut; | ||
| 55 | float *max; | ||
| 56 | int display_scale; | ||
| 57 | |||
| 58 | double draw_persistent_duration; /* in second */ | ||
| 59 | uint8_t persistant_max_rgba[4]; | ||
| 60 | int persistent_max_frames; /* number of frames to check max value */ | ||
| 61 | float *max_persistent; /* max value for draw_persistent_max for each channel */ | ||
| 62 | int *nb_frames_max_display; /* number of frame for each channel, for displaying the max value */ | ||
| 63 | |||
| 64 | void (*meter)(float *src, int nb_samples, float *max); | ||
| 65 | } ShowVolumeContext; | ||
| 66 | |||
| 67 | #define OFFSET(x) offsetof(ShowVolumeContext, x) | ||
| 68 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
| 69 | |||
| 70 | static const AVOption showvolume_options[] = { | ||
| 71 | { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS }, | ||
| 72 | { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS }, | ||
| 73 | { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS }, | ||
| 74 | { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS }, | ||
| 75 | { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS }, | ||
| 76 | { "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0, 1, FLAGS }, | ||
| 77 | { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS }, | ||
| 78 | { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS }, | ||
| 79 | { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS }, | ||
| 80 | { "dm", "duration for max value display", OFFSET(draw_persistent_duration), AV_OPT_TYPE_DOUBLE, {.dbl=0.}, 0, 9000, FLAGS}, | ||
| 81 | { "dmc","set color of the max value line", OFFSET(persistant_max_rgba), AV_OPT_TYPE_COLOR, {.str = "orange"}, 0, 0, FLAGS }, | ||
| 82 | { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "orientation" }, | ||
| 83 | { "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "orientation" }, | ||
| 84 | { "v", "vertical", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "orientation" }, | ||
| 85 | { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS }, | ||
| 86 | { "p", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS }, | ||
| 87 | { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "mode" }, | ||
| 88 | { "p", "peak", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 89 | { "r", "rms", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 90 | { "ds", "set display scale", OFFSET(display_scale), AV_OPT_TYPE_INT, {.i64=LINEAR}, LINEAR, NB_DISPLAY_SCALE - 1, FLAGS, .unit = "display_scale" }, | ||
| 91 | { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, .unit = "display_scale" }, | ||
| 92 | { "log", "log", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, .unit = "display_scale" }, | ||
| 93 | { NULL } | ||
| 94 | }; | ||
| 95 | |||
| 96 | AVFILTER_DEFINE_CLASS(showvolume); | ||
| 97 | |||
| 98 | ✗ | static av_cold int init(AVFilterContext *ctx) | |
| 99 | { | ||
| 100 | ✗ | ShowVolumeContext *s = ctx->priv; | |
| 101 | int ret; | ||
| 102 | |||
| 103 | ✗ | if (s->color) { | |
| 104 | ✗ | ret = av_expr_parse(&s->c_expr, s->color, var_names, | |
| 105 | NULL, NULL, NULL, NULL, 0, ctx); | ||
| 106 | ✗ | if (ret < 0) | |
| 107 | ✗ | return ret; | |
| 108 | } | ||
| 109 | |||
| 110 | ✗ | return 0; | |
| 111 | } | ||
| 112 | |||
| 113 | ✗ | static int query_formats(const AVFilterContext *ctx, | |
| 114 | AVFilterFormatsConfig **cfg_in, | ||
| 115 | AVFilterFormatsConfig **cfg_out) | ||
| 116 | { | ||
| 117 | ✗ | AVFilterFormats *formats = NULL; | |
| 118 | static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; | ||
| 119 | static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE }; | ||
| 120 | int ret; | ||
| 121 | |||
| 122 | ✗ | formats = ff_make_format_list(sample_fmts); | |
| 123 | ✗ | if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0) | |
| 124 | ✗ | return ret; | |
| 125 | |||
| 126 | ✗ | formats = ff_make_format_list(pix_fmts); | |
| 127 | ✗ | if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0) | |
| 128 | ✗ | return ret; | |
| 129 | |||
| 130 | ✗ | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | static void find_peak(float *src, int nb_samples, float *peak) | |
| 134 | { | ||
| 135 | ✗ | float max = 0.f; | |
| 136 | |||
| 137 | ✗ | max = 0; | |
| 138 | ✗ | for (int i = 0; i < nb_samples; i++) | |
| 139 | ✗ | max = fmaxf(max, fabsf(src[i])); | |
| 140 | ✗ | *peak = max; | |
| 141 | ✗ | } | |
| 142 | |||
| 143 | ✗ | static void find_rms(float *src, int nb_samples, float *rms) | |
| 144 | { | ||
| 145 | ✗ | float sum = 0.f; | |
| 146 | |||
| 147 | ✗ | for (int i = 0; i < nb_samples; i++) | |
| 148 | ✗ | sum += src[i] * src[i]; | |
| 149 | ✗ | *rms = sqrtf(sum / nb_samples); | |
| 150 | ✗ | } | |
| 151 | |||
| 152 | ✗ | static int config_input(AVFilterLink *inlink) | |
| 153 | { | ||
| 154 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 155 | ✗ | ShowVolumeContext *s = ctx->priv; | |
| 156 | |||
| 157 | ✗ | s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num)); | |
| 158 | ✗ | s->values = av_calloc(inlink->ch_layout.nb_channels * VAR_VARS_NB, sizeof(double)); | |
| 159 | ✗ | if (!s->values) | |
| 160 | ✗ | return AVERROR(ENOMEM); | |
| 161 | |||
| 162 | ✗ | s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->ch_layout.nb_channels); | |
| 163 | ✗ | if (!s->color_lut) | |
| 164 | ✗ | return AVERROR(ENOMEM); | |
| 165 | |||
| 166 | ✗ | s->max = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->max)); | |
| 167 | ✗ | if (!s->max) | |
| 168 | ✗ | return AVERROR(ENOMEM); | |
| 169 | |||
| 170 | ✗ | switch (s->mode) { | |
| 171 | ✗ | case 0: s->meter = find_peak; break; | |
| 172 | ✗ | case 1: s->meter = find_rms; break; | |
| 173 | ✗ | default: return AVERROR_BUG; | |
| 174 | } | ||
| 175 | |||
| 176 | ✗ | if (s->draw_persistent_duration > 0.) { | |
| 177 | ✗ | s->persistent_max_frames = (int) FFMAX(av_q2d(s->frame_rate) * s->draw_persistent_duration, 1.); | |
| 178 | ✗ | s->max_persistent = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->max_persistent)); | |
| 179 | ✗ | s->nb_frames_max_display = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->nb_frames_max_display)); | |
| 180 | ✗ | if (!s->max_persistent || | |
| 181 | ✗ | !s->nb_frames_max_display) | |
| 182 | ✗ | return AVERROR(ENOMEM); | |
| 183 | } | ||
| 184 | ✗ | return 0; | |
| 185 | } | ||
| 186 | |||
| 187 | ✗ | static int config_output(AVFilterLink *outlink) | |
| 188 | { | ||
| 189 | ✗ | FilterLink *l = ff_filter_link(outlink); | |
| 190 | ✗ | ShowVolumeContext *s = outlink->src->priv; | |
| 191 | ✗ | AVFilterLink *inlink = outlink->src->inputs[0]; | |
| 192 | int ch; | ||
| 193 | |||
| 194 | ✗ | if (s->orientation) { | |
| 195 | ✗ | outlink->h = s->w; | |
| 196 | ✗ | outlink->w = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b; | |
| 197 | } else { | ||
| 198 | ✗ | outlink->w = s->w; | |
| 199 | ✗ | outlink->h = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b; | |
| 200 | } | ||
| 201 | |||
| 202 | ✗ | outlink->sample_aspect_ratio = (AVRational){1,1}; | |
| 203 | ✗ | l->frame_rate = s->frame_rate; | |
| 204 | ✗ | outlink->time_base = av_inv_q(l->frame_rate); | |
| 205 | |||
| 206 | ✗ | for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { | |
| 207 | int i; | ||
| 208 | |||
| 209 | ✗ | for (i = 0; i < s->w; i++) { | |
| 210 | ✗ | float max = i / (float)(s->w - 1); | |
| 211 | |||
| 212 | ✗ | s->values[ch * VAR_VARS_NB + VAR_PEAK] = max; | |
| 213 | ✗ | s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max); | |
| 214 | ✗ | s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch; | |
| 215 | ✗ | s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL); | |
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | ✗ | return 0; | |
| 220 | } | ||
| 221 | |||
| 222 | ✗ | static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o) | |
| 223 | { | ||
| 224 | const uint8_t *font; | ||
| 225 | int font_height; | ||
| 226 | int i; | ||
| 227 | |||
| 228 | ✗ | font = avpriv_cga_font_get(), font_height = 8; | |
| 229 | |||
| 230 | ✗ | for (i = 0; txt[i]; i++) { | |
| 231 | int char_y, mask; | ||
| 232 | |||
| 233 | ✗ | if (o) { /* vertical orientation */ | |
| 234 | ✗ | for (char_y = font_height - 1; char_y >= 0; char_y--) { | |
| 235 | ✗ | uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4; | |
| 236 | ✗ | for (mask = 0x80; mask; mask >>= 1) { | |
| 237 | ✗ | if (font[txt[i] * font_height + font_height - 1 - char_y] & mask) | |
| 238 | ✗ | AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4])); | |
| 239 | ✗ | p += pic->linesize[0]; | |
| 240 | } | ||
| 241 | } | ||
| 242 | } else { /* horizontal orientation */ | ||
| 243 | ✗ | uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4; | |
| 244 | ✗ | for (char_y = 0; char_y < font_height; char_y++) { | |
| 245 | ✗ | for (mask = 0x80; mask; mask >>= 1) { | |
| 246 | ✗ | if (font[txt[i] * font_height + char_y] & mask) | |
| 247 | ✗ | AV_WN32(p, ~AV_RN32(p)); | |
| 248 | ✗ | p += 4; | |
| 249 | } | ||
| 250 | ✗ | p += pic->linesize[0] - 8 * 4; | |
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | ✗ | } | |
| 255 | |||
| 256 | ✗ | static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink) | |
| 257 | { | ||
| 258 | int i, j; | ||
| 259 | ✗ | const uint32_t bg = (uint32_t)(s->bgopacity * 255) << 24; | |
| 260 | |||
| 261 | ✗ | for (i = 0; i < outlink->h; i++) { | |
| 262 | ✗ | uint32_t *dst = (uint32_t *)(s->out->data[0] + i * s->out->linesize[0]); | |
| 263 | ✗ | for (j = 0; j < outlink->w; j++) | |
| 264 | ✗ | AV_WN32A(dst + j, bg); | |
| 265 | } | ||
| 266 | ✗ | } | |
| 267 | |||
| 268 | ✗ | static inline int calc_max_draw(ShowVolumeContext *s, AVFilterLink *outlink, float max) | |
| 269 | { | ||
| 270 | float max_val; | ||
| 271 | ✗ | if (s->display_scale == LINEAR) { | |
| 272 | ✗ | max_val = max; | |
| 273 | } else { /* log */ | ||
| 274 | ✗ | max_val = av_clipf(0.21 * log10(max) + 1, 0, 1); | |
| 275 | } | ||
| 276 | ✗ | if (s->orientation) { /* vertical */ | |
| 277 | ✗ | return outlink->h - outlink->h * max_val; | |
| 278 | } else { /* horizontal */ | ||
| 279 | ✗ | return s->w * max_val; | |
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | ✗ | static inline void calc_persistent_max(ShowVolumeContext *s, float max, int channel) | |
| 284 | { | ||
| 285 | /* update max value for persistent max display */ | ||
| 286 | ✗ | if ((max >= s->max_persistent[channel]) || (s->nb_frames_max_display[channel] >= s->persistent_max_frames)) { /* update max value for display */ | |
| 287 | ✗ | s->max_persistent[channel] = max; | |
| 288 | ✗ | s->nb_frames_max_display[channel] = 0; | |
| 289 | } else { | ||
| 290 | ✗ | s->nb_frames_max_display[channel] += 1; /* incremente display frame count */ | |
| 291 | } | ||
| 292 | ✗ | } | |
| 293 | |||
| 294 | ✗ | static inline void draw_max_line(ShowVolumeContext *s, int max_draw, int channel) | |
| 295 | { | ||
| 296 | int k; | ||
| 297 | ✗ | if (s->orientation) { /* vertical */ | |
| 298 | ✗ | uint8_t *dst = s->out->data[0] + max_draw * s->out->linesize[0] + channel * (s->b + s->h) * 4; | |
| 299 | ✗ | for (k = 0; k < s->h; k++) { | |
| 300 | ✗ | memcpy(dst + k * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba)); | |
| 301 | } | ||
| 302 | } else { /* horizontal */ | ||
| 303 | ✗ | for (k = 0; k < s->h; k++) { | |
| 304 | ✗ | uint8_t *dst = s->out->data[0] + (channel * s->h + channel * s->b + k) * s->out->linesize[0]; | |
| 305 | ✗ | memcpy(dst + max_draw * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba)); | |
| 306 | } | ||
| 307 | } | ||
| 308 | ✗ | } | |
| 309 | |||
| 310 | ✗ | static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) | |
| 311 | { | ||
| 312 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 313 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 314 | ✗ | ShowVolumeContext *s = ctx->priv; | |
| 315 | ✗ | const int step = s->step; | |
| 316 | int c, j, k, max_draw, ret; | ||
| 317 | char channel_name[64]; | ||
| 318 | AVFrame *out; | ||
| 319 | |||
| 320 | ✗ | if (!s->out || s->out->width != outlink->w || | |
| 321 | ✗ | s->out->height != outlink->h) { | |
| 322 | ✗ | av_frame_free(&s->out); | |
| 323 | ✗ | s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 324 | ✗ | if (!s->out) { | |
| 325 | ✗ | av_frame_free(&insamples); | |
| 326 | ✗ | return AVERROR(ENOMEM); | |
| 327 | } | ||
| 328 | ✗ | clear_picture(s, outlink); | |
| 329 | } | ||
| 330 | ✗ | s->out->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base); | |
| 331 | ✗ | s->out->duration = 1; | |
| 332 | |||
| 333 | ✗ | if ((s->f < 1.) && (s->f > 0.)) { | |
| 334 | ✗ | for (j = 0; j < outlink->h; j++) { | |
| 335 | ✗ | uint8_t *dst = s->out->data[0] + j * s->out->linesize[0]; | |
| 336 | ✗ | const uint32_t alpha = s->bgopacity * 255; | |
| 337 | |||
| 338 | ✗ | for (k = 0; k < outlink->w; k++) { | |
| 339 | ✗ | dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0); | |
| 340 | ✗ | dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0); | |
| 341 | ✗ | dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0); | |
| 342 | ✗ | dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, alpha); | |
| 343 | } | ||
| 344 | } | ||
| 345 | ✗ | } else if (s->f == 0.) { | |
| 346 | ✗ | clear_picture(s, outlink); | |
| 347 | } | ||
| 348 | |||
| 349 | ✗ | if (s->orientation) { /* vertical */ | |
| 350 | ✗ | for (c = 0; c < inlink->ch_layout.nb_channels; c++) { | |
| 351 | ✗ | float *src = (float *)insamples->extended_data[c]; | |
| 352 | ✗ | uint32_t *lut = s->color_lut + s->w * c; | |
| 353 | float max; | ||
| 354 | |||
| 355 | ✗ | s->meter(src, insamples->nb_samples, &s->max[c]); | |
| 356 | ✗ | max = s->max[c]; | |
| 357 | |||
| 358 | ✗ | s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max); | |
| 359 | ✗ | max = av_clipf(max, 0, 1); | |
| 360 | ✗ | max_draw = calc_max_draw(s, outlink, max); | |
| 361 | |||
| 362 | ✗ | for (j = s->w - 1; j >= max_draw; j--) { | |
| 363 | ✗ | uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4; | |
| 364 | ✗ | for (k = 0; k < s->h; k++) { | |
| 365 | ✗ | AV_WN32A(&dst[k * 4], lut[s->w - j - 1]); | |
| 366 | } | ||
| 367 | ✗ | if (j & step) | |
| 368 | ✗ | j -= step; | |
| 369 | } | ||
| 370 | |||
| 371 | ✗ | if (s->draw_persistent_duration > 0.) { | |
| 372 | ✗ | calc_persistent_max(s, max, c); | |
| 373 | ✗ | max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1); | |
| 374 | ✗ | draw_max_line(s, max_draw, c); | |
| 375 | } | ||
| 376 | } | ||
| 377 | } else { /* horizontal */ | ||
| 378 | ✗ | for (c = 0; c < inlink->ch_layout.nb_channels; c++) { | |
| 379 | ✗ | float *src = (float *)insamples->extended_data[c]; | |
| 380 | ✗ | uint32_t *lut = s->color_lut + s->w * c; | |
| 381 | float max; | ||
| 382 | |||
| 383 | ✗ | s->meter(src, insamples->nb_samples, &s->max[c]); | |
| 384 | ✗ | max = s->max[c]; | |
| 385 | |||
| 386 | ✗ | s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max); | |
| 387 | ✗ | max = av_clipf(max, 0, 1); | |
| 388 | ✗ | max_draw = calc_max_draw(s, outlink, max); | |
| 389 | |||
| 390 | ✗ | for (j = 0; j < s->h; j++) { | |
| 391 | ✗ | uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0]; | |
| 392 | |||
| 393 | ✗ | for (k = 0; k < max_draw; k++) { | |
| 394 | ✗ | AV_WN32A(dst + k * 4, lut[k]); | |
| 395 | ✗ | if (k & step) | |
| 396 | ✗ | k += step; | |
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | ✗ | if (s->draw_persistent_duration > 0.) { | |
| 401 | ✗ | calc_persistent_max(s, max, c); | |
| 402 | ✗ | max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1); | |
| 403 | ✗ | draw_max_line(s, max_draw, c); | |
| 404 | } | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | ✗ | av_frame_free(&insamples); | |
| 409 | ✗ | out = av_frame_clone(s->out); | |
| 410 | ✗ | if (!out) | |
| 411 | ✗ | return AVERROR(ENOMEM); | |
| 412 | ✗ | ret = ff_inlink_make_frame_writable(outlink, &out); | |
| 413 | ✗ | if (ret < 0) { | |
| 414 | ✗ | av_frame_free(&out); | |
| 415 | ✗ | return ret; | |
| 416 | } | ||
| 417 | |||
| 418 | /* draw channel names */ | ||
| 419 | ✗ | for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 10 && s->draw_text; c++) { | |
| 420 | ✗ | if (s->orientation) { /* vertical */ | |
| 421 | ✗ | int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&inlink->ch_layout, c)); | |
| 422 | ✗ | if (ret < 0) | |
| 423 | ✗ | continue; | |
| 424 | ✗ | drawtext(out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1); | |
| 425 | } else { /* horizontal */ | ||
| 426 | ✗ | int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&inlink->ch_layout, c)); | |
| 427 | ✗ | if (ret < 0) | |
| 428 | ✗ | continue; | |
| 429 | ✗ | drawtext(out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0); | |
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | /* draw volume level */ | ||
| 434 | ✗ | for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) { | |
| 435 | char buf[16]; | ||
| 436 | |||
| 437 | ✗ | if (s->orientation) { /* vertical */ | |
| 438 | ✗ | snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]); | |
| 439 | ✗ | drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1); | |
| 440 | } else { /* horizontal */ | ||
| 441 | ✗ | snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]); | |
| 442 | ✗ | drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0); | |
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | ✗ | return ff_filter_frame(outlink, out); | |
| 447 | } | ||
| 448 | |||
| 449 | ✗ | static int activate(AVFilterContext *ctx) | |
| 450 | { | ||
| 451 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 452 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 453 | ✗ | ShowVolumeContext *s = ctx->priv; | |
| 454 | ✗ | AVFrame *in = NULL; | |
| 455 | int ret; | ||
| 456 | |||
| 457 | ✗ | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); | |
| 458 | |||
| 459 | ✗ | ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in); | |
| 460 | ✗ | if (ret < 0) | |
| 461 | ✗ | return ret; | |
| 462 | ✗ | if (ret > 0) | |
| 463 | ✗ | return filter_frame(inlink, in); | |
| 464 | |||
| 465 | ✗ | if (ff_inlink_queued_samples(inlink) >= s->nb_samples) { | |
| 466 | ✗ | ff_filter_set_ready(ctx, 10); | |
| 467 | ✗ | return 0; | |
| 468 | } | ||
| 469 | |||
| 470 | ✗ | FF_FILTER_FORWARD_STATUS(inlink, outlink); | |
| 471 | ✗ | FF_FILTER_FORWARD_WANTED(outlink, inlink); | |
| 472 | |||
| 473 | ✗ | return FFERROR_NOT_READY; | |
| 474 | } | ||
| 475 | |||
| 476 | ✗ | static av_cold void uninit(AVFilterContext *ctx) | |
| 477 | { | ||
| 478 | ✗ | ShowVolumeContext *s = ctx->priv; | |
| 479 | |||
| 480 | ✗ | av_frame_free(&s->out); | |
| 481 | ✗ | av_expr_free(s->c_expr); | |
| 482 | ✗ | av_freep(&s->values); | |
| 483 | ✗ | av_freep(&s->color_lut); | |
| 484 | ✗ | av_freep(&s->max); | |
| 485 | ✗ | av_freep(&s->max_persistent); | |
| 486 | ✗ | av_freep(&s->nb_frames_max_display); | |
| 487 | ✗ | } | |
| 488 | |||
| 489 | static const AVFilterPad showvolume_inputs[] = { | ||
| 490 | { | ||
| 491 | .name = "default", | ||
| 492 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 493 | .config_props = config_input, | ||
| 494 | }, | ||
| 495 | }; | ||
| 496 | |||
| 497 | static const AVFilterPad showvolume_outputs[] = { | ||
| 498 | { | ||
| 499 | .name = "default", | ||
| 500 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 501 | .config_props = config_output, | ||
| 502 | }, | ||
| 503 | }; | ||
| 504 | |||
| 505 | const FFFilter ff_avf_showvolume = { | ||
| 506 | .p.name = "showvolume", | ||
| 507 | .p.description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."), | ||
| 508 | .p.priv_class = &showvolume_class, | ||
| 509 | .init = init, | ||
| 510 | .activate = activate, | ||
| 511 | .uninit = uninit, | ||
| 512 | .priv_size = sizeof(ShowVolumeContext), | ||
| 513 | FILTER_INPUTS(showvolume_inputs), | ||
| 514 | FILTER_OUTPUTS(showvolume_outputs), | ||
| 515 | FILTER_QUERY_FUNC2(query_formats), | ||
| 516 | }; | ||
| 517 |