blob: 587415175b562d7e87389bf4dca151245495db1d
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 "formats.h" |
26 | #include "audio.h" |
27 | #include "video.h" |
28 | #include "internal.h" |
29 | |
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 }; |
35 | |
36 | typedef struct AudioHistogramContext { |
37 | const AVClass *class; |
38 | AVFrame *out; |
39 | int w, h; |
40 | AVRational frame_rate; |
41 | uint64_t *achistogram; |
42 | uint64_t *shistogram; |
43 | int ascale; |
44 | int scale; |
45 | float phisto; |
46 | int histogram_h; |
47 | int apos; |
48 | int ypos; |
49 | int slide; |
50 | int dmode; |
51 | int dchannels; |
52 | int count; |
53 | int frame_count; |
54 | float *combine_buffer; |
55 | AVFrame *in[101]; |
56 | int first; |
57 | } AudioHistogramContext; |
58 | |
59 | #define OFFSET(x) offsetof(AudioHistogramContext, x) |
60 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
61 | |
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" }, |
84 | { NULL } |
85 | }; |
86 | |
87 | AVFILTER_DEFINE_CLASS(ahistogram); |
88 | |
89 | static int query_formats(AVFilterContext *ctx) |
90 | { |
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); |
98 | |
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) |
103 | return ret; |
104 | |
105 | formats = ff_all_samplerates(); |
106 | if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0) |
107 | return ret; |
108 | |
109 | formats = ff_make_format_list(pix_fmts); |
110 | if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0) |
111 | return ret; |
112 | |
113 | return 0; |
114 | } |
115 | |
116 | static int config_input(AVFilterLink *inlink) |
117 | { |
118 | AVFilterContext *ctx = inlink->dst; |
119 | AudioHistogramContext *s = ctx->priv; |
120 | int nb_samples; |
121 | |
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; |
126 | |
127 | s->dchannels = s->dmode == SINGLE ? 1 : inlink->channels; |
128 | s->shistogram = av_calloc(s->w, s->dchannels * sizeof(*s->shistogram)); |
129 | if (!s->shistogram) |
130 | return AVERROR(ENOMEM); |
131 | |
132 | s->achistogram = av_calloc(s->w, s->dchannels * sizeof(*s->achistogram)); |
133 | if (!s->achistogram) |
134 | return AVERROR(ENOMEM); |
135 | |
136 | return 0; |
137 | } |
138 | |
139 | static int config_output(AVFilterLink *outlink) |
140 | { |
141 | AudioHistogramContext *s = outlink->src->priv; |
142 | |
143 | outlink->w = s->w; |
144 | outlink->h = s->h; |
145 | outlink->sample_aspect_ratio = (AVRational){1,1}; |
146 | outlink->frame_rate = s->frame_rate; |
147 | |
148 | s->histogram_h = s->h * s->phisto; |
149 | s->ypos = s->h * s->phisto; |
150 | |
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); |
155 | } |
156 | |
157 | return 0; |
158 | } |
159 | |
160 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
161 | { |
162 | AVFilterContext *ctx = inlink->dst; |
163 | AVFilterLink *outlink = ctx->outputs[0]; |
164 | AudioHistogramContext *s = ctx->priv; |
165 | const int H = s->histogram_h; |
166 | const int w = s->w; |
167 | int c, y, n, p, bin; |
168 | uint64_t acmax = 1; |
169 | |
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); |
174 | if (!s->out) { |
175 | av_frame_free(&in); |
176 | return AVERROR(ENOMEM); |
177 | } |
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); |
183 | } |
184 | } |
185 | |
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; |
191 | } |
192 | } |
193 | |
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); |
199 | } |
200 | s->out->pts = in->pts; |
201 | |
202 | s->first = s->frame_count; |
203 | |
204 | switch (s->ascale) { |
205 | case ALINEAR: |
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]; |
209 | |
210 | for (n = 0; n < in->nb_samples; n++) { |
211 | bin = lrint(av_clipf(fabsf(src[n]), 0, 1) * (w - 1)); |
212 | |
213 | achistogram[bin]++; |
214 | } |
215 | |
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]; |
219 | |
220 | for (n = 0; n < in->nb_samples; n++) { |
221 | bin = lrint(av_clipf(fabsf(src2[n]), 0, 1) * (w - 1)); |
222 | |
223 | shistogram[bin]++; |
224 | } |
225 | } |
226 | } |
227 | break; |
228 | case ALOG: |
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]; |
232 | |
233 | for (n = 0; n < in->nb_samples; n++) { |
234 | bin = lrint(av_clipf(1 + log10(fabsf(src[n])) / 6, 0, 1) * (w - 1)); |
235 | |
236 | achistogram[bin]++; |
237 | } |
238 | |
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]; |
242 | |
243 | for (n = 0; n < in->nb_samples; n++) { |
244 | bin = lrint(av_clipf(1 + log10(fabsf(src2[n])) / 6, 0, 1) * (w - 1)); |
245 | |
246 | shistogram[bin]++; |
247 | } |
248 | } |
249 | } |
250 | break; |
251 | } |
252 | |
253 | av_frame_free(&s->in[s->frame_count]); |
254 | s->in[s->frame_count] = in; |
255 | s->frame_count++; |
256 | if (s->frame_count > s->count) |
257 | s->frame_count = 0; |
258 | |
259 | for (n = 0; n < w * s->dchannels; n++) { |
260 | acmax = FFMAX(s->achistogram[n] - s->shistogram[n], acmax); |
261 | } |
262 | |
263 | for (c = 0; c < s->dchannels; c++) { |
264 | uint64_t *shistogram = &s->shistogram[c * w]; |
265 | uint64_t *achistogram = &s->achistogram[c * w]; |
266 | float yf, uf, vf; |
267 | |
268 | if (s->dmode == SEPARATE) { |
269 | yf = 256.0f / s->dchannels; |
270 | uf = yf * M_PI; |
271 | vf = yf * M_PI; |
272 | uf *= 0.5 * sin((2 * M_PI * c) / s->dchannels); |
273 | vf *= 0.5 * cos((2 * M_PI * c) / s->dchannels); |
274 | } |
275 | |
276 | for (n = 0; n < w; n++) { |
277 | double a, aa; |
278 | int h; |
279 | |
280 | a = achistogram[n] - shistogram[n]; |
281 | |
282 | switch (s->scale) { |
283 | case LINEAR: |
284 | aa = a / (double)acmax; |
285 | break; |
286 | case SQRT: |
287 | aa = sqrt(a) / sqrt(acmax); |
288 | break; |
289 | case CBRT: |
290 | aa = cbrt(a) / cbrt(acmax); |
291 | break; |
292 | case LOG: |
293 | aa = log2(a + 1) / log2(acmax + 1); |
294 | break; |
295 | case RLOG: |
296 | aa = 1. - log2(a + 1) / log2(acmax + 1); |
297 | if (aa == 1.) |
298 | aa = 0; |
299 | break; |
300 | default: |
301 | av_assert0(0); |
302 | } |
303 | |
304 | h = aa * (H - 1); |
305 | |
306 | if (s->dmode == SINGLE) { |
307 | |
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; |
311 | } |
312 | |
313 | if (s->h - H > 0) { |
314 | h = aa * 255; |
315 | |
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; |
320 | } |
321 | } else if (s->dmode == SEPARATE) { |
322 | float *out = &s->combine_buffer[3 * n]; |
323 | int old; |
324 | |
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) |
328 | break; |
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; |
334 | } |
335 | |
336 | out[0] += aa * yf; |
337 | out[1] += aa * uf; |
338 | out[2] += aa * vf; |
339 | } |
340 | } |
341 | } |
342 | |
343 | if (s->h - H > 0) { |
344 | if (s->dmode == SEPARATE) { |
345 | for (n = 0; n < w; n++) { |
346 | float *cb = &s->combine_buffer[3 * n]; |
347 | |
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; |
352 | } |
353 | } |
354 | |
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); |
360 | } |
361 | } |
362 | } |
363 | |
364 | s->ypos++; |
365 | if (s->slide == SCROLL || s->ypos >= s->h) |
366 | s->ypos = H; |
367 | } |
368 | |
369 | return ff_filter_frame(outlink, av_frame_clone(s->out)); |
370 | } |
371 | |
372 | static av_cold void uninit(AVFilterContext *ctx) |
373 | { |
374 | AudioHistogramContext *s = ctx->priv; |
375 | int i; |
376 | |
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]); |
383 | } |
384 | |
385 | static const AVFilterPad audiovectorscope_inputs[] = { |
386 | { |
387 | .name = "default", |
388 | .type = AVMEDIA_TYPE_AUDIO, |
389 | .config_props = config_input, |
390 | .filter_frame = filter_frame, |
391 | }, |
392 | { NULL } |
393 | }; |
394 | |
395 | static const AVFilterPad audiovectorscope_outputs[] = { |
396 | { |
397 | .name = "default", |
398 | .type = AVMEDIA_TYPE_VIDEO, |
399 | .config_props = config_output, |
400 | }, |
401 | { NULL } |
402 | }; |
403 | |
404 | AVFilter ff_avf_ahistogram = { |
405 | .name = "ahistogram", |
406 | .description = NULL_IF_CONFIG_SMALL("Convert input audio to histogram video output."), |
407 | .uninit = uninit, |
408 | .query_formats = query_formats, |
409 | .priv_size = sizeof(AudioHistogramContext), |
410 | .inputs = audiovectorscope_inputs, |
411 | .outputs = audiovectorscope_outputs, |
412 | .priv_class = &ahistogram_class, |
413 | }; |
414 |