FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_dynaudnorm.c
Go to the documentation of this file.
1 /*
2  * Dynamic Audio Normalizer
3  * Copyright (c) 2015 LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Dynamic Audio Normalizer
25  */
26 
27 #include <float.h>
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 
32 #define FF_BUFQUEUE_SIZE 302
34 
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "internal.h"
38 
39 typedef struct cqueue {
40  double *elements;
41  int size;
43  int first;
44 } cqueue;
45 
47  const AVClass *class;
48 
49  struct FFBufQueue queue;
50 
51  int frame_len;
57 
58  double peak_value;
60  double target_rms;
65  double *fade_factors[2];
66  double *weights;
67 
68  int channels;
69  int delay;
70 
75 
76 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
77 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
78 
79 static const AVOption dynaudnorm_options[] = {
80  { "f", "set the frame length in msec", OFFSET(frame_len_msec), AV_OPT_TYPE_INT, {.i64 = 500}, 10, 8000, FLAGS },
81  { "g", "set the filter size", OFFSET(filter_size), AV_OPT_TYPE_INT, {.i64 = 31}, 3, 301, FLAGS },
82  { "p", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS },
83  { "m", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
84  { "r", "set the target RMS", OFFSET(target_rms), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
85  { "n", "enable channel coupling", OFFSET(channels_coupled), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
86  { "c", "enable DC correction", OFFSET(dc_correction), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
87  { "b", "enable alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
88  { "s", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
89  { NULL }
90 };
91 
92 AVFILTER_DEFINE_CLASS(dynaudnorm);
93 
94 static av_cold int init(AVFilterContext *ctx)
95 {
97 
98  if (!(s->filter_size & 1)) {
99  av_log(ctx, AV_LOG_ERROR, "filter size %d is invalid. Must be an odd value.\n", s->filter_size);
100  return AVERROR(EINVAL);
101  }
102 
103  return 0;
104 }
105 
107 {
110  static const enum AVSampleFormat sample_fmts[] = {
113  };
114  int ret;
115 
116  layouts = ff_all_channel_layouts();
117  if (!layouts)
118  return AVERROR(ENOMEM);
119  ret = ff_set_common_channel_layouts(ctx, layouts);
120  if (ret < 0)
121  return ret;
122 
123  formats = ff_make_format_list(sample_fmts);
124  if (!formats)
125  return AVERROR(ENOMEM);
126  ret = ff_set_common_formats(ctx, formats);
127  if (ret < 0)
128  return ret;
129 
130  formats = ff_all_samplerates();
131  if (!formats)
132  return AVERROR(ENOMEM);
133  return ff_set_common_samplerates(ctx, formats);
134 }
135 
136 static inline int frame_size(int sample_rate, int frame_len_msec)
137 {
138  const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
139  return frame_size + (frame_size % 2);
140 }
141 
142 static void precalculate_fade_factors(double *fade_factors[2], int frame_len)
143 {
144  const double step_size = 1.0 / frame_len;
145  int pos;
146 
147  for (pos = 0; pos < frame_len; pos++) {
148  fade_factors[0][pos] = 1.0 - (step_size * (pos + 1.0));
149  fade_factors[1][pos] = 1.0 - fade_factors[0][pos];
150  }
151 }
152 
154 {
155  cqueue *q;
156 
157  q = av_malloc(sizeof(cqueue));
158  if (!q)
159  return NULL;
160 
161  q->size = size;
162  q->nb_elements = 0;
163  q->first = 0;
164 
165  q->elements = av_malloc(sizeof(double) * size);
166  if (!q->elements) {
167  av_free(q);
168  return NULL;
169  }
170 
171  return q;
172 }
173 
174 static void cqueue_free(cqueue *q)
175 {
176  av_free(q->elements);
177  av_free(q);
178 }
179 
180 static int cqueue_size(cqueue *q)
181 {
182  return q->nb_elements;
183 }
184 
185 static int cqueue_empty(cqueue *q)
186 {
187  return !q->nb_elements;
188 }
189 
190 static int cqueue_enqueue(cqueue *q, double element)
191 {
192  int i;
193 
194  av_assert2(q->nb_elements != q->size);
195 
196  i = (q->first + q->nb_elements) % q->size;
197  q->elements[i] = element;
198  q->nb_elements++;
199 
200  return 0;
201 }
202 
203 static double cqueue_peek(cqueue *q, int index)
204 {
205  av_assert2(index < q->nb_elements);
206  return q->elements[(q->first + index) % q->size];
207 }
208 
209 static int cqueue_dequeue(cqueue *q, double *element)
210 {
212 
213  *element = q->elements[q->first];
214  q->first = (q->first + 1) % q->size;
215  q->nb_elements--;
216 
217  return 0;
218 }
219 
220 static int cqueue_pop(cqueue *q)
221 {
223 
224  q->first = (q->first + 1) % q->size;
225  q->nb_elements--;
226 
227  return 0;
228 }
229 
230 static const double s_pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
231 
233 {
234  double total_weight = 0.0;
235  const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
236  double adjust;
237  int i;
238 
239  // Pre-compute constants
240  const int offset = s->filter_size / 2;
241  const double c1 = 1.0 / (sigma * sqrt(2.0 * s_pi));
242  const double c2 = 2.0 * pow(sigma, 2.0);
243 
244  // Compute weights
245  for (i = 0; i < s->filter_size; i++) {
246  const int x = i - offset;
247 
248  s->weights[i] = c1 * exp(-(pow(x, 2.0) / c2));
249  total_weight += s->weights[i];
250  }
251 
252  // Adjust weights
253  adjust = 1.0 / total_weight;
254  for (i = 0; i < s->filter_size; i++) {
255  s->weights[i] *= adjust;
256  }
257 }
258 
259 static int config_input(AVFilterLink *inlink)
260 {
261  AVFilterContext *ctx = inlink->dst;
263  int c;
264 
265  s->frame_len =
266  inlink->min_samples =
267  inlink->max_samples =
268  inlink->partial_buf_size = frame_size(inlink->sample_rate, s->frame_len_msec);
269  av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
270 
271  s->fade_factors[0] = av_malloc(s->frame_len * sizeof(*s->fade_factors[0]));
272  s->fade_factors[1] = av_malloc(s->frame_len * sizeof(*s->fade_factors[1]));
273 
275  s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
276  s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
278  s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
280  s->weights = av_malloc(s->filter_size * sizeof(*s->weights));
282  !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
284  !s->gain_history_smoothed || !s->weights)
285  return AVERROR(ENOMEM);
286 
287  for (c = 0; c < inlink->channels; c++) {
288  s->prev_amplification_factor[c] = 1.0;
289 
293 
294  if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
295  !s->gain_history_smoothed[c])
296  return AVERROR(ENOMEM);
297  }
298 
301 
302  s->channels = inlink->channels;
303  s->delay = s->filter_size;
304 
305  return 0;
306 }
307 
308 static int config_output(AVFilterLink *outlink)
309 {
310  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
311  return 0;
312 }
313 
314 static inline double fade(double prev, double next, int pos,
315  double *fade_factors[2])
316 {
317  return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
318 }
319 
320 static inline double pow2(const double value)
321 {
322  return value * value;
323 }
324 
325 static inline double bound(const double threshold, const double val)
326 {
327  const double CONST = 0.8862269254527580136490837416705725913987747280611935; //sqrt(PI) / 2.0
328  return erf(CONST * (val / threshold)) * threshold;
329 }
330 
331 static double find_peak_magnitude(AVFrame *frame, int channel)
332 {
333  double max = DBL_EPSILON;
334  int c, i;
335 
336  if (channel == -1) {
337  for (c = 0; c < av_frame_get_channels(frame); c++) {
338  double *data_ptr = (double *)frame->extended_data[c];
339 
340  for (i = 0; i < frame->nb_samples; i++)
341  max = FFMAX(max, fabs(data_ptr[i]));
342  }
343  } else {
344  double *data_ptr = (double *)frame->extended_data[channel];
345 
346  for (i = 0; i < frame->nb_samples; i++)
347  max = FFMAX(max, fabs(data_ptr[i]));
348  }
349 
350  return max;
351 }
352 
353 static double compute_frame_rms(AVFrame *frame, int channel)
354 {
355  double rms_value = 0.0;
356  int c, i;
357 
358  if (channel == -1) {
359  for (c = 0; c < av_frame_get_channels(frame); c++) {
360  const double *data_ptr = (double *)frame->extended_data[c];
361 
362  for (i = 0; i < frame->nb_samples; i++) {
363  rms_value += pow2(data_ptr[i]);
364  }
365  }
366 
367  rms_value /= frame->nb_samples * av_frame_get_channels(frame);
368  } else {
369  const double *data_ptr = (double *)frame->extended_data[channel];
370  for (i = 0; i < frame->nb_samples; i++) {
371  rms_value += pow2(data_ptr[i]);
372  }
373 
374  rms_value /= frame->nb_samples;
375  }
376 
377  return FFMAX(sqrt(rms_value), DBL_EPSILON);
378 }
379 
381  int channel)
382 {
383  const double maximum_gain = s->peak_value / find_peak_magnitude(frame, channel);
384  const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
385  return bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
386 }
387 
388 static double minimum_filter(cqueue *q)
389 {
390  double min = DBL_MAX;
391  int i;
392 
393  for (i = 0; i < cqueue_size(q); i++) {
394  min = FFMIN(min, cqueue_peek(q, i));
395  }
396 
397  return min;
398 }
399 
401 {
402  double result = 0.0;
403  int i;
404 
405  for (i = 0; i < cqueue_size(q); i++) {
406  result += cqueue_peek(q, i) * s->weights[i];
407  }
408 
409  return result;
410 }
411 
413  double current_gain_factor)
414 {
415  if (cqueue_empty(s->gain_history_original[channel]) ||
416  cqueue_empty(s->gain_history_minimum[channel])) {
417  const int pre_fill_size = s->filter_size / 2;
418 
419  s->prev_amplification_factor[channel] = s->alt_boundary_mode ? current_gain_factor : 1.0;
420 
421  while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
422  cqueue_enqueue(s->gain_history_original[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
423  }
424 
425  while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
426  cqueue_enqueue(s->gain_history_minimum[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
427  }
428  }
429 
430  cqueue_enqueue(s->gain_history_original[channel], current_gain_factor);
431 
432  while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
433  double minimum;
435  minimum = minimum_filter(s->gain_history_original[channel]);
436 
437  cqueue_enqueue(s->gain_history_minimum[channel], minimum);
438 
439  cqueue_pop(s->gain_history_original[channel]);
440  }
441 
442  while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
443  double smoothed;
445  smoothed = gaussian_filter(s, s->gain_history_minimum[channel]);
446 
447  cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
448 
449  cqueue_pop(s->gain_history_minimum[channel]);
450  }
451 }
452 
453 static inline double update_value(double new, double old, double aggressiveness)
454 {
455  av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
456  return aggressiveness * new + (1.0 - aggressiveness) * old;
457 }
458 
460 {
461  const double diff = 1.0 / frame->nb_samples;
462  int is_first_frame = cqueue_empty(s->gain_history_original[0]);
463  int c, i;
464 
465  for (c = 0; c < s->channels; c++) {
466  double *dst_ptr = (double *)frame->extended_data[c];
467  double current_average_value = 0.0;
468  double prev_value;
469 
470  for (i = 0; i < frame->nb_samples; i++)
471  current_average_value += dst_ptr[i] * diff;
472 
473  prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c];
474  s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1);
475 
476  for (i = 0; i < frame->nb_samples; i++) {
477  dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, s->fade_factors);
478  }
479  }
480 }
481 
482 static double setup_compress_thresh(double threshold)
483 {
484  if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
485  double current_threshold = threshold;
486  double step_size = 1.0;
487 
488  while (step_size > DBL_EPSILON) {
489  while ((current_threshold + step_size > current_threshold) &&
490  (bound(current_threshold + step_size, 1.0) <= threshold)) {
491  current_threshold += step_size;
492  }
493 
494  step_size /= 2.0;
495  }
496 
497  return current_threshold;
498  } else {
499  return threshold;
500  }
501 }
502 
504  AVFrame *frame, int channel)
505 {
506  double variance = 0.0;
507  int i, c;
508 
509  if (channel == -1) {
510  for (c = 0; c < s->channels; c++) {
511  const double *data_ptr = (double *)frame->extended_data[c];
512 
513  for (i = 0; i < frame->nb_samples; i++) {
514  variance += pow2(data_ptr[i]); // Assume that MEAN is *zero*
515  }
516  }
517  variance /= (s->channels * frame->nb_samples) - 1;
518  } else {
519  const double *data_ptr = (double *)frame->extended_data[channel];
520 
521  for (i = 0; i < frame->nb_samples; i++) {
522  variance += pow2(data_ptr[i]); // Assume that MEAN is *zero*
523  }
524  variance /= frame->nb_samples - 1;
525  }
526 
527  return FFMAX(sqrt(variance), DBL_EPSILON);
528 }
529 
531 {
532  int is_first_frame = cqueue_empty(s->gain_history_original[0]);
533  int c, i;
534 
535  if (s->channels_coupled) {
536  const double standard_deviation = compute_frame_std_dev(s, frame, -1);
537  const double current_threshold = FFMIN(1.0, s->compress_factor * standard_deviation);
538 
539  const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[0];
540  double prev_actual_thresh, curr_actual_thresh;
541  s->compress_threshold[0] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[0], (1.0/3.0));
542 
543  prev_actual_thresh = setup_compress_thresh(prev_value);
544  curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]);
545 
546  for (c = 0; c < s->channels; c++) {
547  double *const dst_ptr = (double *)frame->extended_data[c];
548  for (i = 0; i < frame->nb_samples; i++) {
549  const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
550  dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
551  }
552  }
553  } else {
554  for (c = 0; c < s->channels; c++) {
555  const double standard_deviation = compute_frame_std_dev(s, frame, c);
556  const double current_threshold = setup_compress_thresh(FFMIN(1.0, s->compress_factor * standard_deviation));
557 
558  const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[c];
559  double prev_actual_thresh, curr_actual_thresh;
560  double *dst_ptr;
561  s->compress_threshold[c] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[c], 1.0/3.0);
562 
563  prev_actual_thresh = setup_compress_thresh(prev_value);
564  curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]);
565 
566  dst_ptr = (double *)frame->extended_data[c];
567  for (i = 0; i < frame->nb_samples; i++) {
568  const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
569  dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
570  }
571  }
572  }
573 }
574 
576 {
577  if (s->dc_correction) {
578  perform_dc_correction(s, frame);
579  }
580 
581  if (s->compress_factor > DBL_EPSILON) {
582  perform_compression(s, frame);
583  }
584 
585  if (s->channels_coupled) {
586  const double current_gain_factor = get_max_local_gain(s, frame, -1);
587  int c;
588 
589  for (c = 0; c < s->channels; c++)
590  update_gain_history(s, c, current_gain_factor);
591  } else {
592  int c;
593 
594  for (c = 0; c < s->channels; c++)
595  update_gain_history(s, c, get_max_local_gain(s, frame, c));
596  }
597 }
598 
600 {
601  int c, i;
602 
603  for (c = 0; c < s->channels; c++) {
604  double *dst_ptr = (double *)frame->extended_data[c];
605  double current_amplification_factor;
606 
607  cqueue_dequeue(s->gain_history_smoothed[c], &current_amplification_factor);
608 
609  for (i = 0; i < frame->nb_samples; i++) {
610  const double amplification_factor = fade(s->prev_amplification_factor[c],
611  current_amplification_factor, i,
612  s->fade_factors);
613 
614  dst_ptr[i] *= amplification_factor;
615 
616  if (fabs(dst_ptr[i]) > s->peak_value)
617  dst_ptr[i] = copysign(s->peak_value, dst_ptr[i]);
618  }
619 
620  s->prev_amplification_factor[c] = current_amplification_factor;
621  }
622 }
623 
624 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
625 {
626  AVFilterContext *ctx = inlink->dst;
628  AVFilterLink *outlink = inlink->dst->outputs[0];
629  int ret = 0;
630 
631  if (!cqueue_empty(s->gain_history_smoothed[0])) {
633 
634  amplify_frame(s, out);
635  ret = ff_filter_frame(outlink, out);
636  }
637 
638  analyze_frame(s, in);
639  ff_bufqueue_add(ctx, &s->queue, in);
640 
641  return ret;
642 }
643 
645  AVFilterLink *outlink)
646 {
647  AVFrame *out = ff_get_audio_buffer(outlink, s->frame_len);
648  int c, i;
649 
650  if (!out)
651  return AVERROR(ENOMEM);
652 
653  for (c = 0; c < s->channels; c++) {
654  double *dst_ptr = (double *)out->extended_data[c];
655 
656  for (i = 0; i < out->nb_samples; i++) {
657  dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? FFMIN(s->peak_value, s->target_rms) : s->peak_value);
658  if (s->dc_correction) {
659  dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
660  dst_ptr[i] += s->dc_correction_value[c];
661  }
662  }
663  }
664 
665  s->delay--;
666  return filter_frame(inlink, out);
667 }
668 
669 static int request_frame(AVFilterLink *outlink)
670 {
671  AVFilterContext *ctx = outlink->src;
673  int ret = 0;
674 
675  ret = ff_request_frame(ctx->inputs[0]);
676 
677  if (ret == AVERROR_EOF && !ctx->is_disabled && s->delay)
678  ret = flush_buffer(s, ctx->inputs[0], outlink);
679 
680  return ret;
681 }
682 
683 static av_cold void uninit(AVFilterContext *ctx)
684 {
686  int c;
687 
691  av_freep(&s->fade_factors[0]);
692  av_freep(&s->fade_factors[1]);
693 
694  for (c = 0; c < s->channels; c++) {
698  }
699 
703 
704  av_freep(&s->weights);
705 
707 }
708 
710  {
711  .name = "default",
712  .type = AVMEDIA_TYPE_AUDIO,
713  .filter_frame = filter_frame,
714  .config_props = config_input,
715  .needs_writable = 1,
716  },
717  { NULL }
718 };
719 
721  {
722  .name = "default",
723  .type = AVMEDIA_TYPE_AUDIO,
724  .config_props = config_output,
725  .request_frame = request_frame,
726  },
727  { NULL }
728 };
729 
731  .name = "dynaudnorm",
732  .description = NULL_IF_CONFIG_SMALL("Dynamic Audio Normalizer."),
733  .query_formats = query_formats,
734  .priv_size = sizeof(DynamicAudioNormalizerContext),
735  .init = init,
736  .uninit = uninit,
737  .inputs = avfilter_af_dynaudnorm_inputs,
738  .outputs = avfilter_af_dynaudnorm_outputs,
739  .priv_class = &dynaudnorm_class,
740 };
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
static const AVFilterPad avfilter_af_dynaudnorm_inputs[]
#define FLAGS
Definition: af_dynaudnorm.c:77
static double bound(const double threshold, const double val)
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:523
const char const char void * val
Definition: avisynth_c.h:634
static double compute_frame_rms(AVFrame *frame, int channel)
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static int cqueue_empty(cqueue *q)
static const AVFilterPad avfilter_af_dynaudnorm_outputs[]
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static int cqueue_size(cqueue *q)
int first
Definition: af_dynaudnorm.c:43
double, planar
Definition: samplefmt.h:71
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static double get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static enum AVSampleFormat formats[]
static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void precalculate_fade_factors(double *fade_factors[2], int frame_len)
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:686
static int request_frame(AVFilterLink *outlink)
static int config_input(AVFilterLink *inlink)
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
Structure holding the queue.
Definition: bufferqueue.h:49
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q)
double * elements
Definition: af_dynaudnorm.c:40
static AVFrame * frame
static const uint64_t c1
Definition: murmur3.c:49
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
static av_cold void uninit(AVFilterContext *ctx)
static void cqueue_free(cqueue *q)
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:63
static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
static int query_formats(AVFilterContext *ctx)
static double cqueue_peek(cqueue *q, int index)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:542
#define CONST(name, help, val, unit)
Definition: vf_histeq.c:71
static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
AVFILTER_DEFINE_CLASS(dynaudnorm)
static double pow2(const double value)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:74
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
tuple adjust
Definition: normalize.py:25
#define OFFSET(x)
Definition: af_dynaudnorm.c:76
static av_always_inline av_const double round(double x)
Definition: libm.h:162
static int config_output(AVFilterLink *outlink)
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:79
#define FFMIN(a, b)
Definition: common.h:81
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
int size
Definition: af_dynaudnorm.c:41
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:374
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
A list of supported channel layouts.
Definition: formats.h:85
sample_rate
int nb_elements
Definition: af_dynaudnorm.c:42
AVFilter ff_af_dynaudnorm
static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, double current_gain_factor)
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static av_cold int init(AVFilterContext *ctx)
Definition: af_dynaudnorm.c:94
Describe the class of an AVClass context structure.
Definition: log.h:67
int av_frame_get_channels(const AVFrame *frame)
Filter definition.
Definition: avfilter.h:470
int index
Definition: gxfenc.c:89
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
static double setup_compress_thresh(double threshold)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static double find_peak_magnitude(AVFrame *frame, int channel)
static int cqueue_pop(cqueue *q)
static double c[64]
static double minimum_filter(cqueue *q)
static const uint64_t c2
Definition: murmur3.c:50
static const double s_pi
static int cqueue_enqueue(cqueue *q, double element)
static double fade(double prev, double next, int pos, double *fade_factors[2])
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
static double update_value(double new, double old, double aggressiveness)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:343
static cqueue * cqueue_create(int size)
static const AVOption dynaudnorm_options[]
Definition: af_dynaudnorm.c:79
internal API functions
static int cqueue_dequeue(cqueue *q, double *element)
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
float min
static int frame_size(int sample_rate, int frame_len_msec)
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530
static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)