2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
30 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, RLOG, NB_SCALES };
31 enum AmplitudeScale { ALINEAR, ALOG, NB_ASCALES };
32 enum SlideMode { REPLACE, SCROLL, NB_SLIDES };
33 enum DisplayMode { SINGLE, SEPARATE, NB_DMODES };
34 enum HistogramMode { ACCUMULATE, CURRENT, NB_HMODES };
36 typedef struct AudioHistogramContext {
40 AVRational frame_rate;
41 uint64_t *achistogram;
54 float *combine_buffer;
57 } AudioHistogramContext;
59 #define OFFSET(x) offsetof(AudioHistogramContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62 static const AVOption ahistogram_options[] = {
63 { "dmode", "set method to display channels", OFFSET(dmode), AV_OPT_TYPE_INT, {.i64=SINGLE}, 0, NB_DMODES-1, FLAGS, "dmode" },
64 { "single", "all channels use single histogram", 0, AV_OPT_TYPE_CONST, {.i64=SINGLE}, 0, 0, FLAGS, "dmode" },
65 { "separate", "each channel have own histogram", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "dmode" },
66 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
67 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
68 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
69 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
70 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
71 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
72 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
73 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
74 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
75 { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=RLOG}, 0, 0, FLAGS, "scale" },
76 { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=ALOG}, LINEAR, NB_ASCALES-1, FLAGS, "ascale" },
77 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=ALOG}, 0, 0, FLAGS, "ascale" },
78 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=ALINEAR}, 0, 0, FLAGS, "ascale" },
79 { "acount", "how much frames to accumulate", OFFSET(count), AV_OPT_TYPE_INT, {.i64=1}, -1, 100, FLAGS },
80 { "rheight", "set histogram ratio of window height", OFFSET(phisto), AV_OPT_TYPE_FLOAT, {.dbl=0.10}, 0, 1, FLAGS },
81 { "slide", "set sonogram sliding", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=REPLACE}, 0, NB_SLIDES-1, FLAGS, "slide" },
82 { "replace", "replace old rows with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
83 { "scroll", "scroll from top to bottom", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
87 AVFILTER_DEFINE_CLASS(ahistogram);
89 static int query_formats(AVFilterContext *ctx)
91 AVFilterFormats *formats = NULL;
92 AVFilterChannelLayouts *layouts = NULL;
93 AVFilterLink *inlink = ctx->inputs[0];
94 AVFilterLink *outlink = ctx->outputs[0];
95 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
96 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE };
97 int ret = AVERROR(EINVAL);
99 formats = ff_make_format_list(sample_fmts);
100 if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
101 (layouts = ff_all_channel_counts()) == NULL ||
102 (ret = ff_channel_layouts_ref (layouts, &inlink->out_channel_layouts)) < 0)
105 formats = ff_all_samplerates();
106 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
109 formats = ff_make_format_list(pix_fmts);
110 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
116 static int config_input(AVFilterLink *inlink)
118 AVFilterContext *ctx = inlink->dst;
119 AudioHistogramContext *s = ctx->priv;
122 nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
123 inlink->partial_buf_size =
124 inlink->min_samples =
125 inlink->max_samples = nb_samples;
127 s->dchannels = s->dmode == SINGLE ? 1 : inlink->channels;
128 s->shistogram = av_calloc(s->w, s->dchannels * sizeof(*s->shistogram));
130 return AVERROR(ENOMEM);
132 s->achistogram = av_calloc(s->w, s->dchannels * sizeof(*s->achistogram));
134 return AVERROR(ENOMEM);
139 static int config_output(AVFilterLink *outlink)
141 AudioHistogramContext *s = outlink->src->priv;
145 outlink->sample_aspect_ratio = (AVRational){1,1};
146 outlink->frame_rate = s->frame_rate;
148 s->histogram_h = s->h * s->phisto;
149 s->ypos = s->h * s->phisto;
151 if (s->dmode == SEPARATE) {
152 s->combine_buffer = av_malloc_array(outlink->w * 3, sizeof(*s->combine_buffer));
153 if (!s->combine_buffer)
154 return AVERROR(ENOMEM);
160 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
162 AVFilterContext *ctx = inlink->dst;
163 AVFilterLink *outlink = ctx->outputs[0];
164 AudioHistogramContext *s = ctx->priv;
165 const int H = s->histogram_h;
170 if (!s->out || s->out->width != outlink->w ||
171 s->out->height != outlink->h) {
172 av_frame_free(&s->out);
173 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
176 return AVERROR(ENOMEM);
178 for (n = H; n < s->h; n++) {
179 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
180 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
181 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
182 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
186 if (s->dmode == SEPARATE) {
187 for (y = 0; y < w; y++) {
188 s->combine_buffer[3 * y ] = 0;
189 s->combine_buffer[3 * y + 1] = 127.5;
190 s->combine_buffer[3 * y + 2] = 127.5;
194 for (n = 0; n < H; n++) {
195 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
196 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
197 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
198 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
200 s->out->pts = in->pts;
202 s->first = s->frame_count;
206 for (c = 0; c < inlink->channels; c++) {
207 const float *src = (const float *)in->extended_data[c];
208 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
210 for (n = 0; n < in->nb_samples; n++) {
211 bin = lrint(av_clipf(fabsf(src[n]), 0, 1) * (w - 1));
216 if (s->in[s->first] && s->count >= 0) {
217 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
218 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
220 for (n = 0; n < in->nb_samples; n++) {
221 bin = lrint(av_clipf(fabsf(src2[n]), 0, 1) * (w - 1));
229 for (c = 0; c < inlink->channels; c++) {
230 const float *src = (const float *)in->extended_data[c];
231 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
233 for (n = 0; n < in->nb_samples; n++) {
234 bin = lrint(av_clipf(1 + log10(fabsf(src[n])) / 6, 0, 1) * (w - 1));
239 if (s->in[s->first] && s->count >= 0) {
240 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
241 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
243 for (n = 0; n < in->nb_samples; n++) {
244 bin = lrint(av_clipf(1 + log10(fabsf(src2[n])) / 6, 0, 1) * (w - 1));
253 av_frame_free(&s->in[s->frame_count]);
254 s->in[s->frame_count] = in;
256 if (s->frame_count > s->count)
259 for (n = 0; n < w * s->dchannels; n++) {
260 acmax = FFMAX(s->achistogram[n] - s->shistogram[n], acmax);
263 for (c = 0; c < s->dchannels; c++) {
264 uint64_t *shistogram = &s->shistogram[c * w];
265 uint64_t *achistogram = &s->achistogram[c * w];
268 if (s->dmode == SEPARATE) {
269 yf = 256.0f / s->dchannels;
272 uf *= 0.5 * sin((2 * M_PI * c) / s->dchannels);
273 vf *= 0.5 * cos((2 * M_PI * c) / s->dchannels);
276 for (n = 0; n < w; n++) {
280 a = achistogram[n] - shistogram[n];
284 aa = a / (double)acmax;
287 aa = sqrt(a) / sqrt(acmax);
290 aa = cbrt(a) / cbrt(acmax);
293 aa = log2(a + 1) / log2(acmax + 1);
296 aa = 1. - log2(a + 1) / log2(acmax + 1);
306 if (s->dmode == SINGLE) {
308 for (y = H - h; y < H; y++) {
309 s->out->data[0][y * s->out->linesize[0] + n] = 255;
310 s->out->data[3][y * s->out->linesize[0] + n] = 255;
316 s->out->data[0][s->ypos * s->out->linesize[0] + n] = h;
317 s->out->data[1][s->ypos * s->out->linesize[1] + n] = 127;
318 s->out->data[2][s->ypos * s->out->linesize[2] + n] = 127;
319 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
321 } else if (s->dmode == SEPARATE) {
322 float *out = &s->combine_buffer[3 * n];
325 old = s->out->data[0][(H - h) * s->out->linesize[0] + n];
326 for (y = H - h; y < H; y++) {
327 if (s->out->data[0][y * s->out->linesize[0] + n] != old)
329 old = s->out->data[0][y * s->out->linesize[0] + n];
330 s->out->data[0][y * s->out->linesize[0] + n] = yf;
331 s->out->data[1][y * s->out->linesize[1] + n] = 128+uf;
332 s->out->data[2][y * s->out->linesize[2] + n] = 128+vf;
333 s->out->data[3][y * s->out->linesize[3] + n] = 255;
344 if (s->dmode == SEPARATE) {
345 for (n = 0; n < w; n++) {
346 float *cb = &s->combine_buffer[3 * n];
348 s->out->data[0][s->ypos * s->out->linesize[0] + n] = cb[0];
349 s->out->data[1][s->ypos * s->out->linesize[1] + n] = cb[1];
350 s->out->data[2][s->ypos * s->out->linesize[2] + n] = cb[2];
351 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
355 if (s->slide == SCROLL) {
356 for (p = 0; p < 4; p++) {
357 for (y = s->h; y >= H + 1; y--) {
358 memmove(s->out->data[p] + (y ) * s->out->linesize[p],
359 s->out->data[p] + (y-1) * s->out->linesize[p], w);
365 if (s->slide == SCROLL || s->ypos >= s->h)
369 return ff_filter_frame(outlink, av_frame_clone(s->out));
372 static av_cold void uninit(AVFilterContext *ctx)
374 AudioHistogramContext *s = ctx->priv;
377 av_frame_free(&s->out);
378 av_freep(&s->shistogram);
379 av_freep(&s->achistogram);
380 av_freep(&s->combine_buffer);
381 for (i = 0; i < 101; i++)
382 av_frame_free(&s->in[i]);
385 static const AVFilterPad audiovectorscope_inputs[] = {
388 .type = AVMEDIA_TYPE_AUDIO,
389 .config_props = config_input,
390 .filter_frame = filter_frame,
395 static const AVFilterPad audiovectorscope_outputs[] = {
398 .type = AVMEDIA_TYPE_VIDEO,
399 .config_props = config_output,
404 AVFilter ff_avf_ahistogram = {
405 .name = "ahistogram",
406 .description = NULL_IF_CONFIG_SMALL("Convert input audio to histogram video output."),
408 .query_formats = query_formats,
409 .priv_size = sizeof(AudioHistogramContext),
410 .inputs = audiovectorscope_inputs,
411 .outputs = audiovectorscope_outputs,
412 .priv_class = &ahistogram_class,