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/avassert.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "audio.h"
28 #include "video.h"
29 #include "internal.h"
30
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 { ABS, SIGN, NB_HMODES };
36
37 typedef struct AudioHistogramContext {
38 const AVClass *class;
39 AVFrame *out;
40 int w, h;
41 AVRational frame_rate;
42 uint64_t *achistogram;
43 uint64_t *shistogram;
44 int ascale;
45 int scale;
46 float phisto;
47 int histogram_h;
48 int apos;
49 int ypos;
50 int slide;
51 int dmode;
52 int hmode;
53 int dchannels;
54 int count;
55 int frame_count;
56 float *combine_buffer;
57 AVFrame *in[101];
58 int first;
59 int nb_samples;
60
61 int (*get_bin)(float in, int w);
62 } AudioHistogramContext;
63
64 #define OFFSET(x) offsetof(AudioHistogramContext, x)
65 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
66
67 static const AVOption ahistogram_options[] = {
68 { "dmode", "set method to display channels", OFFSET(dmode), AV_OPT_TYPE_INT, {.i64=SINGLE}, 0, NB_DMODES-1, FLAGS, "dmode" },
69 { "single", "all channels use single histogram", 0, AV_OPT_TYPE_CONST, {.i64=SINGLE}, 0, 0, FLAGS, "dmode" },
70 { "separate", "each channel have own histogram", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "dmode" },
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 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
74 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
75 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
76 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
77 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
78 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
79 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
80 { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=RLOG}, 0, 0, FLAGS, "scale" },
81 { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=ALOG}, LINEAR, NB_ASCALES-1, FLAGS, "ascale" },
82 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=ALOG}, 0, 0, FLAGS, "ascale" },
83 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=ALINEAR}, 0, 0, FLAGS, "ascale" },
84 { "acount", "how much frames to accumulate", OFFSET(count), AV_OPT_TYPE_INT, {.i64=1}, -1, 100, FLAGS },
85 { "rheight", "set histogram ratio of window height", OFFSET(phisto), AV_OPT_TYPE_FLOAT, {.dbl=0.10}, 0, 1, FLAGS },
86 { "slide", "set sonogram sliding", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=REPLACE}, 0, NB_SLIDES-1, FLAGS, "slide" },
87 { "replace", "replace old rows with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
88 { "scroll", "scroll from top to bottom", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
89 { "hmode", "set histograms mode", OFFSET(hmode), AV_OPT_TYPE_INT, {.i64=ABS}, 0, NB_HMODES-1, FLAGS, "hmode" },
90 { "abs", "use absolute samples", 0, AV_OPT_TYPE_CONST, {.i64=ABS}, 0, 0, FLAGS, "hmode" },
91 { "sign", "use unchanged samples", 0, AV_OPT_TYPE_CONST, {.i64=SIGN},0, 0, FLAGS, "hmode" },
92 { NULL }
93 };
94
95 AVFILTER_DEFINE_CLASS(ahistogram);
96
query_formats(AVFilterContext * ctx)97 static int query_formats(AVFilterContext *ctx)
98 {
99 AVFilterFormats *formats = NULL;
100 AVFilterChannelLayouts *layouts = NULL;
101 AVFilterLink *inlink = ctx->inputs[0];
102 AVFilterLink *outlink = ctx->outputs[0];
103 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
104 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE };
105 int ret = AVERROR(EINVAL);
106
107 formats = ff_make_format_list(sample_fmts);
108 if ((ret = ff_formats_ref (formats, &inlink->outcfg.formats )) < 0 ||
109 (layouts = ff_all_channel_counts()) == NULL ||
110 (ret = ff_channel_layouts_ref (layouts, &inlink->outcfg.channel_layouts)) < 0)
111 return ret;
112
113 formats = ff_all_samplerates();
114 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
115 return ret;
116
117 formats = ff_make_format_list(pix_fmts);
118 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
119 return ret;
120
121 return 0;
122 }
123
config_input(AVFilterLink * inlink)124 static int config_input(AVFilterLink *inlink)
125 {
126 AVFilterContext *ctx = inlink->dst;
127 AudioHistogramContext *s = ctx->priv;
128
129 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
130 s->dchannels = s->dmode == SINGLE ? 1 : inlink->ch_layout.nb_channels;
131 s->shistogram = av_calloc(s->w, s->dchannels * sizeof(*s->shistogram));
132 if (!s->shistogram)
133 return AVERROR(ENOMEM);
134
135 s->achistogram = av_calloc(s->w, s->dchannels * sizeof(*s->achistogram));
136 if (!s->achistogram)
137 return AVERROR(ENOMEM);
138
139 return 0;
140 }
141
get_lin_bin_abs(float in,int w)142 static int get_lin_bin_abs(float in, int w)
143 {
144 return lrintf(av_clipf(fabsf(in), 0.f, 1.f) * (w - 1));
145 }
146
get_lin_bin_sign(float in,int w)147 static int get_lin_bin_sign(float in, int w)
148 {
149 return lrintf((1.f + av_clipf(in, -1.f, 1.f)) * 0.5f * (w - 1));
150 }
151
get_log_bin_abs(float in,int w)152 static int get_log_bin_abs(float in, int w)
153 {
154 return lrintf(av_clipf(1.f + log10f(fabsf(in)) / 6.f, 0.f, 1.f) * (w - 1));
155 }
156
get_log_bin_sign(float in,int w)157 static int get_log_bin_sign(float in, int w)
158 {
159 return (w / 2) + FFSIGN(in) * lrintf(av_clipf(1.f + log10f(fabsf(in)) / 6.f, 0.f, 1.f) * (w / 2));
160 }
161
config_output(AVFilterLink * outlink)162 static int config_output(AVFilterLink *outlink)
163 {
164 AudioHistogramContext *s = outlink->src->priv;
165
166 outlink->w = s->w;
167 outlink->h = s->h;
168 outlink->sample_aspect_ratio = (AVRational){1,1};
169 outlink->frame_rate = s->frame_rate;
170 outlink->time_base = av_inv_q(outlink->frame_rate);
171
172 s->histogram_h = s->h * s->phisto;
173 s->ypos = s->h * s->phisto;
174
175 switch (s->ascale) {
176 case ALINEAR:
177 switch (s->hmode) {
178 case ABS: s->get_bin = get_lin_bin_abs; break;
179 case SIGN: s->get_bin = get_lin_bin_sign; break;
180 default:
181 return AVERROR_BUG;
182 }
183 break;
184 case ALOG:
185 switch (s->hmode) {
186 case ABS: s->get_bin = get_log_bin_abs; break;
187 case SIGN: s->get_bin = get_log_bin_sign; break;
188 default:
189 return AVERROR_BUG;
190 }
191 break;
192 default:
193 return AVERROR_BUG;
194 }
195
196 if (s->dmode == SEPARATE) {
197 s->combine_buffer = av_malloc_array(outlink->w * 3, sizeof(*s->combine_buffer));
198 if (!s->combine_buffer)
199 return AVERROR(ENOMEM);
200 }
201
202 return 0;
203 }
204
filter_frame(AVFilterLink * inlink,AVFrame * in)205 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
206 {
207 AVFilterContext *ctx = inlink->dst;
208 AVFilterLink *outlink = ctx->outputs[0];
209 AudioHistogramContext *s = ctx->priv;
210 const int H = s->histogram_h;
211 const int w = s->w;
212 int c, y, n, p, bin;
213 uint64_t acmax = 1;
214 AVFrame *clone;
215
216 if (!s->out || s->out->width != outlink->w ||
217 s->out->height != outlink->h) {
218 av_frame_free(&s->out);
219 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
220 if (!s->out) {
221 av_frame_free(&in);
222 return AVERROR(ENOMEM);
223 }
224 for (n = H; n < s->h; n++) {
225 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
226 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
227 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
228 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
229 }
230 }
231
232 av_frame_make_writable(s->out);
233 if (s->dmode == SEPARATE) {
234 for (y = 0; y < w; y++) {
235 s->combine_buffer[3 * y ] = 0;
236 s->combine_buffer[3 * y + 1] = 127.5;
237 s->combine_buffer[3 * y + 2] = 127.5;
238 }
239 }
240
241 for (n = 0; n < H; n++) {
242 memset(s->out->data[0] + n * s->out->linesize[0], 0, w);
243 memset(s->out->data[1] + n * s->out->linesize[0], 127, w);
244 memset(s->out->data[2] + n * s->out->linesize[0], 127, w);
245 memset(s->out->data[3] + n * s->out->linesize[0], 0, w);
246 }
247 s->out->pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base);
248
249 s->first = s->frame_count;
250
251 switch (s->ascale) {
252 case ALINEAR:
253 for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
254 const float *src = (const float *)in->extended_data[c];
255 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
256
257 for (n = 0; n < in->nb_samples; n++) {
258 bin = s->get_bin(src[n], w);
259
260 achistogram[bin]++;
261 }
262
263 if (s->in[s->first] && s->count >= 0) {
264 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
265 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
266
267 for (n = 0; n < in->nb_samples; n++) {
268 bin = s->get_bin(src2[n], w);
269
270 shistogram[bin]++;
271 }
272 }
273 }
274 break;
275 case ALOG:
276 for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
277 const float *src = (const float *)in->extended_data[c];
278 uint64_t *achistogram = &s->achistogram[(s->dmode == SINGLE ? 0: c) * w];
279
280 for (n = 0; n < in->nb_samples; n++) {
281 bin = s->get_bin(src[n], w);
282
283 achistogram[bin]++;
284 }
285
286 if (s->in[s->first] && s->count >= 0) {
287 uint64_t *shistogram = &s->shistogram[(s->dmode == SINGLE ? 0: c) * w];
288 const float *src2 = (const float *)s->in[s->first]->extended_data[c];
289
290 for (n = 0; n < in->nb_samples; n++) {
291 bin = s->get_bin(src2[n], w);
292
293 shistogram[bin]++;
294 }
295 }
296 }
297 break;
298 }
299
300 av_frame_free(&s->in[s->frame_count]);
301 s->in[s->frame_count] = in;
302 s->frame_count++;
303 if (s->frame_count > s->count)
304 s->frame_count = 0;
305
306 for (n = 0; n < w * s->dchannels; n++) {
307 acmax = FFMAX(s->achistogram[n] - s->shistogram[n], acmax);
308 }
309
310 for (c = 0; c < s->dchannels; c++) {
311 uint64_t *shistogram = &s->shistogram[c * w];
312 uint64_t *achistogram = &s->achistogram[c * w];
313 float yf, uf, vf;
314
315 if (s->dmode == SEPARATE) {
316 yf = 255.0f / s->dchannels;
317 uf = yf * M_PI;
318 vf = yf * M_PI;
319 uf *= 0.5 * sin((2 * M_PI * c) / s->dchannels);
320 vf *= 0.5 * cos((2 * M_PI * c) / s->dchannels);
321 }
322
323 for (n = 0; n < w; n++) {
324 double a, aa;
325 int h;
326
327 a = achistogram[n] - shistogram[n];
328
329 switch (s->scale) {
330 case LINEAR:
331 aa = a / (double)acmax;
332 break;
333 case SQRT:
334 aa = sqrt(a) / sqrt(acmax);
335 break;
336 case CBRT:
337 aa = cbrt(a) / cbrt(acmax);
338 break;
339 case LOG:
340 aa = log2(a + 1) / log2(acmax + 1);
341 break;
342 case RLOG:
343 aa = 1. - log2(a + 1) / log2(acmax + 1);
344 if (aa == 1.)
345 aa = 0;
346 break;
347 default:
348 av_assert0(0);
349 }
350
351 h = aa * (H - 1);
352
353 if (s->dmode == SINGLE) {
354
355 for (y = H - h; y < H; y++) {
356 s->out->data[0][y * s->out->linesize[0] + n] = 255;
357 s->out->data[3][y * s->out->linesize[0] + n] = 255;
358 }
359
360 if (s->h - H > 0) {
361 h = aa * 255;
362
363 s->out->data[0][s->ypos * s->out->linesize[0] + n] = av_clip_uint8(h);
364 s->out->data[1][s->ypos * s->out->linesize[1] + n] = 127;
365 s->out->data[2][s->ypos * s->out->linesize[2] + n] = 127;
366 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
367 }
368 } else if (s->dmode == SEPARATE) {
369 float *out = &s->combine_buffer[3 * n];
370 int old;
371
372 old = s->out->data[0][(H - h) * s->out->linesize[0] + n];
373 for (y = H - h; y < H; y++) {
374 if (s->out->data[0][y * s->out->linesize[0] + n] != old)
375 break;
376 old = s->out->data[0][y * s->out->linesize[0] + n];
377 s->out->data[0][y * s->out->linesize[0] + n] = av_clip_uint8(yf);
378 s->out->data[1][y * s->out->linesize[1] + n] = av_clip_uint8(128.f+uf);
379 s->out->data[2][y * s->out->linesize[2] + n] = av_clip_uint8(128.f+vf);
380 s->out->data[3][y * s->out->linesize[3] + n] = 255;
381 }
382
383 out[0] += aa * yf;
384 out[1] += aa * uf;
385 out[2] += aa * vf;
386 }
387 }
388 }
389
390 if (s->h - H > 0) {
391 if (s->dmode == SEPARATE) {
392 for (n = 0; n < w; n++) {
393 float *cb = &s->combine_buffer[3 * n];
394
395 s->out->data[0][s->ypos * s->out->linesize[0] + n] = cb[0];
396 s->out->data[1][s->ypos * s->out->linesize[1] + n] = cb[1];
397 s->out->data[2][s->ypos * s->out->linesize[2] + n] = cb[2];
398 s->out->data[3][s->ypos * s->out->linesize[3] + n] = 255;
399 }
400 }
401
402 if (s->slide == SCROLL) {
403 for (p = 0; p < 4; p++) {
404 for (y = s->h - 1; y >= H + 1; y--) {
405 memmove(s->out->data[p] + (y ) * s->out->linesize[p],
406 s->out->data[p] + (y-1) * s->out->linesize[p], w);
407 }
408 }
409 }
410
411 s->ypos++;
412 if (s->slide == SCROLL || s->ypos >= s->h)
413 s->ypos = H;
414 }
415
416 clone = av_frame_clone(s->out);
417 if (!clone)
418 return AVERROR(ENOMEM);
419
420 return ff_filter_frame(outlink, clone);
421 }
422
activate(AVFilterContext * ctx)423 static int activate(AVFilterContext *ctx)
424 {
425 AVFilterLink *inlink = ctx->inputs[0];
426 AVFilterLink *outlink = ctx->outputs[0];
427 AudioHistogramContext *s = ctx->priv;
428 AVFrame *in;
429 int ret;
430
431 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
432
433 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
434 if (ret < 0)
435 return ret;
436 if (ret > 0)
437 return filter_frame(inlink, in);
438
439 if (ff_inlink_queued_samples(inlink) >= s->nb_samples) {
440 ff_filter_set_ready(ctx, 10);
441 return 0;
442 }
443
444 FF_FILTER_FORWARD_STATUS(inlink, outlink);
445 FF_FILTER_FORWARD_WANTED(outlink, inlink);
446
447 return FFERROR_NOT_READY;
448 }
449
uninit(AVFilterContext * ctx)450 static av_cold void uninit(AVFilterContext *ctx)
451 {
452 AudioHistogramContext *s = ctx->priv;
453 int i;
454
455 av_frame_free(&s->out);
456 av_freep(&s->shistogram);
457 av_freep(&s->achistogram);
458 av_freep(&s->combine_buffer);
459 for (i = 0; i < 101; i++)
460 av_frame_free(&s->in[i]);
461 }
462
463 static const AVFilterPad ahistogram_inputs[] = {
464 {
465 .name = "default",
466 .type = AVMEDIA_TYPE_AUDIO,
467 .config_props = config_input,
468 },
469 };
470
471 static const AVFilterPad ahistogram_outputs[] = {
472 {
473 .name = "default",
474 .type = AVMEDIA_TYPE_VIDEO,
475 .config_props = config_output,
476 },
477 };
478
479 const AVFilter ff_avf_ahistogram = {
480 .name = "ahistogram",
481 .description = NULL_IF_CONFIG_SMALL("Convert input audio to histogram video output."),
482 .uninit = uninit,
483 .priv_size = sizeof(AudioHistogramContext),
484 .activate = activate,
485 FILTER_INPUTS(ahistogram_inputs),
486 FILTER_OUTPUTS(ahistogram_outputs),
487 FILTER_QUERY_FUNC(query_formats),
488 .priv_class = &ahistogram_class,
489 };
490