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"
31 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, RLOG, NB_SCALES };
32 enum AmplitudeScale { ALINEAR, ALOG, NB_ASCALES };
33 enum SlideMode { REPLACE, SCROLL, NB_SLIDES };
34 enum DisplayMode { SINGLE, SEPARATE, NB_DMODES };
35 enum HistogramMode { ACCUMULATE, CURRENT, NB_HMODES };
37 typedef struct AudioHistogramContext {
41 AVRational frame_rate;
42 uint64_t *achistogram;
55 float *combine_buffer;
59 } AudioHistogramContext;
61 #define OFFSET(x) offsetof(AudioHistogramContext, x)
62 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
64 static const AVOption ahistogram_options[] = {
65 { "dmode", "set method to display channels", OFFSET(dmode), AV_OPT_TYPE_INT, {.i64=SINGLE}, 0, NB_DMODES-1, FLAGS, "dmode" },
66 { "single", "all channels use single histogram", 0, AV_OPT_TYPE_CONST, {.i64=SINGLE}, 0, 0, FLAGS, "dmode" },
67 { "separate", "each channel have own histogram", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "dmode" },
68 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
69 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
70 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
71 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
72 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
73 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
74 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
75 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
76 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
77 { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=RLOG}, 0, 0, FLAGS, "scale" },
78 { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=ALOG}, LINEAR, NB_ASCALES-1, FLAGS, "ascale" },
79 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=ALOG}, 0, 0, FLAGS, "ascale" },
80 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=ALINEAR}, 0, 0, FLAGS, "ascale" },
81 { "acount", "how much frames to accumulate", OFFSET(count), AV_OPT_TYPE_INT, {.i64=1}, -1, 100, FLAGS },
82 { "rheight", "set histogram ratio of window height", OFFSET(phisto), AV_OPT_TYPE_FLOAT, {.dbl=0.10}, 0, 1, FLAGS },
83 { "slide", "set sonogram sliding", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=REPLACE}, 0, NB_SLIDES-1, FLAGS, "slide" },
84 { "replace", "replace old rows with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
85 { "scroll", "scroll from top to bottom", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
89 AVFILTER_DEFINE_CLASS(ahistogram);
91 static int query_formats(AVFilterContext *ctx)
93 AVFilterFormats *formats = NULL;
94 AVFilterChannelLayouts *layouts = NULL;
95 AVFilterLink *inlink = ctx->inputs[0];
96 AVFilterLink *outlink = ctx->outputs[0];
97 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
98 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE };
99 int ret = AVERROR(EINVAL);
101 formats = ff_make_format_list(sample_fmts);
102 if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
103 (layouts = ff_all_channel_counts()) == NULL ||
104 (ret = ff_channel_layouts_ref (layouts, &inlink->out_channel_layouts)) < 0)
107 formats = ff_all_samplerates();
108 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
111 formats = ff_make_format_list(pix_fmts);
112 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
118 static int config_input(AVFilterLink *inlink)
120 AVFilterContext *ctx = inlink->dst;
121 AudioHistogramContext *s = ctx->priv;
123 s->nb_samples = FFMAX(1, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
124 s->dchannels = s->dmode == SINGLE ? 1 : inlink->channels;
125 s->shistogram = av_calloc(s->w, s->dchannels * sizeof(*s->shistogram));
127 return AVERROR(ENOMEM);
129 s->achistogram = av_calloc(s->w, s->dchannels * sizeof(*s->achistogram));
131 return AVERROR(ENOMEM);
136 static int config_output(AVFilterLink *outlink)
138 AudioHistogramContext *s = outlink->src->priv;
142 outlink->sample_aspect_ratio = (AVRational){1,1};
143 outlink->frame_rate = s->frame_rate;
145 s->histogram_h = s->h * s->phisto;
146 s->ypos = s->h * s->phisto;
148 if (s->dmode == SEPARATE) {
149 s->combine_buffer = av_malloc_array(outlink->w * 3, sizeof(*s->combine_buffer));
150 if (!s->combine_buffer)
151 return AVERROR(ENOMEM);
157 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
159 AVFilterContext *ctx = inlink->dst;
160 AVFilterLink *outlink = ctx->outputs[0];
161 AudioHistogramContext *s = ctx->priv;
162 const int H = s->histogram_h;
167 if (!s->out || s->out->width != outlink->w ||
168 s->out->height != outlink->h) {
169 av_frame_free(&s->out);
170 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
173 return AVERROR(ENOMEM);
175 for (n = H; n < s->h; n++) {
176 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
177 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
178 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
179 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
183 if (s->dmode == SEPARATE) {
184 for (y = 0; y < w; y++) {
185 s->combine_buffer[3 * y ] = 0;
186 s->combine_buffer[3 * y + 1] = 127.5;
187 s->combine_buffer[3 * y + 2] = 127.5;
191 for (n = 0; n < H; n++) {
192 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
193 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
194 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
195 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
197 s->out->pts = in->pts;
199 s->first = s->frame_count;
203 for (c = 0; c < inlink->channels; c++) {
204 const float *src = (const float *)in->extended_data[c];
205 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
207 for (n = 0; n < in->nb_samples; n++) {
208 bin = lrint(av_clipf(fabsf(src[n]), 0, 1) * (w - 1));
213 if (s->in[s->first] && s->count >= 0) {
214 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
215 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
217 for (n = 0; n < in->nb_samples; n++) {
218 bin = lrint(av_clipf(fabsf(src2[n]), 0, 1) * (w - 1));
226 for (c = 0; c < inlink->channels; c++) {
227 const float *src = (const float *)in->extended_data[c];
228 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
230 for (n = 0; n < in->nb_samples; n++) {
231 bin = lrint(av_clipf(1 + log10(fabsf(src[n])) / 6, 0, 1) * (w - 1));
236 if (s->in[s->first] && s->count >= 0) {
237 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
238 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
240 for (n = 0; n < in->nb_samples; n++) {
241 bin = lrint(av_clipf(1 + log10(fabsf(src2[n])) / 6, 0, 1) * (w - 1));
250 av_frame_free(&s->in[s->frame_count]);
251 s->in[s->frame_count] = in;
253 if (s->frame_count > s->count)
256 for (n = 0; n < w * s->dchannels; n++) {
257 acmax = FFMAX(s->achistogram[n] - s->shistogram[n], acmax);
260 for (c = 0; c < s->dchannels; c++) {
261 uint64_t *shistogram = &s->shistogram[c * w];
262 uint64_t *achistogram = &s->achistogram[c * w];
265 if (s->dmode == SEPARATE) {
266 yf = 256.0f / s->dchannels;
269 uf *= 0.5 * sin((2 * M_PI * c) / s->dchannels);
270 vf *= 0.5 * cos((2 * M_PI * c) / s->dchannels);
273 for (n = 0; n < w; n++) {
277 a = achistogram[n] - shistogram[n];
281 aa = a / (double)acmax;
284 aa = sqrt(a) / sqrt(acmax);
287 aa = cbrt(a) / cbrt(acmax);
290 aa = log2(a + 1) / log2(acmax + 1);
293 aa = 1. - log2(a + 1) / log2(acmax + 1);
303 if (s->dmode == SINGLE) {
305 for (y = H - h; y < H; y++) {
306 s->out->data[0][y * s->out->linesize[0] + n] = 255;
307 s->out->data[3][y * s->out->linesize[0] + n] = 255;
313 s->out->data[0][s->ypos * s->out->linesize[0] + n] = h;
314 s->out->data[1][s->ypos * s->out->linesize[1] + n] = 127;
315 s->out->data[2][s->ypos * s->out->linesize[2] + n] = 127;
316 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
318 } else if (s->dmode == SEPARATE) {
319 float *out = &s->combine_buffer[3 * n];
322 old = s->out->data[0][(H - h) * s->out->linesize[0] + n];
323 for (y = H - h; y < H; y++) {
324 if (s->out->data[0][y * s->out->linesize[0] + n] != old)
326 old = s->out->data[0][y * s->out->linesize[0] + n];
327 s->out->data[0][y * s->out->linesize[0] + n] = yf;
328 s->out->data[1][y * s->out->linesize[1] + n] = 128+uf;
329 s->out->data[2][y * s->out->linesize[2] + n] = 128+vf;
330 s->out->data[3][y * s->out->linesize[3] + n] = 255;
341 if (s->dmode == SEPARATE) {
342 for (n = 0; n < w; n++) {
343 float *cb = &s->combine_buffer[3 * n];
345 s->out->data[0][s->ypos * s->out->linesize[0] + n] = cb[0];
346 s->out->data[1][s->ypos * s->out->linesize[1] + n] = cb[1];
347 s->out->data[2][s->ypos * s->out->linesize[2] + n] = cb[2];
348 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
352 if (s->slide == SCROLL) {
353 for (p = 0; p < 4; p++) {
354 for (y = s->h; y >= H + 1; y--) {
355 memmove(s->out->data[p] + (y ) * s->out->linesize[p],
356 s->out->data[p] + (y-1) * s->out->linesize[p], w);
362 if (s->slide == SCROLL || s->ypos >= s->h)
366 return ff_filter_frame(outlink, av_frame_clone(s->out));
369 static int activate(AVFilterContext *ctx)
371 AVFilterLink *inlink = ctx->inputs[0];
372 AVFilterLink *outlink = ctx->outputs[0];
373 AudioHistogramContext *s = ctx->priv;
377 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
379 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
383 return filter_frame(inlink, in);
385 FF_FILTER_FORWARD_STATUS(inlink, outlink);
386 FF_FILTER_FORWARD_WANTED(outlink, inlink);
388 return FFERROR_NOT_READY;
391 static av_cold void uninit(AVFilterContext *ctx)
393 AudioHistogramContext *s = ctx->priv;
396 av_frame_free(&s->out);
397 av_freep(&s->shistogram);
398 av_freep(&s->achistogram);
399 av_freep(&s->combine_buffer);
400 for (i = 0; i < 101; i++)
401 av_frame_free(&s->in[i]);
404 static const AVFilterPad ahistogram_inputs[] = {
407 .type = AVMEDIA_TYPE_AUDIO,
408 .config_props = config_input,
413 static const AVFilterPad ahistogram_outputs[] = {
416 .type = AVMEDIA_TYPE_VIDEO,
417 .config_props = config_output,
422 AVFilter ff_avf_ahistogram = {
423 .name = "ahistogram",
424 .description = NULL_IF_CONFIG_SMALL("Convert input audio to histogram video output."),
426 .query_formats = query_formats,
427 .priv_size = sizeof(AudioHistogramContext),
428 .activate = activate,
429 .inputs = ahistogram_inputs,
430 .outputs = ahistogram_outputs,
431 .priv_class = &ahistogram_class,