FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_ahistogram.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 0 261 0.0%
Functions: 0 10 0.0%
Branches: 0 142 0.0%

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