af_volumedetect.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2012 Nicolas George
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/channel_layout.h"
  21. #include "libavutil/avassert.h"
  22. #include "audio.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. typedef struct {
  26. /**
  27. * Number of samples at each PCM value.
  28. * histogram[0x8000 + i] is the number of samples at value i.
  29. * The extra element is there for symmetry.
  30. */
  31. uint64_t histogram[0x10001];
  32. } VolDetectContext;
  33. static int query_formats(AVFilterContext *ctx)
  34. {
  35. static const enum AVSampleFormat sample_fmts[] = {
  36. AV_SAMPLE_FMT_S16,
  37. AV_SAMPLE_FMT_S16P,
  38. AV_SAMPLE_FMT_NONE
  39. };
  40. AVFilterFormats *formats;
  41. if (!(formats = ff_make_format_list(sample_fmts)))
  42. return AVERROR(ENOMEM);
  43. return ff_set_common_formats(ctx, formats);
  44. }
  45. static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
  46. {
  47. AVFilterContext *ctx = inlink->dst;
  48. VolDetectContext *vd = ctx->priv;
  49. int64_t layout = samples->channel_layout;
  50. int nb_samples = samples->nb_samples;
  51. int nb_channels = av_get_channel_layout_nb_channels(layout);
  52. int nb_planes = nb_channels;
  53. int plane, i;
  54. int16_t *pcm;
  55. if (!av_sample_fmt_is_planar(samples->format)) {
  56. nb_samples *= nb_channels;
  57. nb_planes = 1;
  58. }
  59. for (plane = 0; plane < nb_planes; plane++) {
  60. pcm = (int16_t *)samples->extended_data[plane];
  61. for (i = 0; i < nb_samples; i++)
  62. vd->histogram[pcm[i] + 0x8000]++;
  63. }
  64. return ff_filter_frame(inlink->dst->outputs[0], samples);
  65. }
  66. #define MAX_DB 91
  67. static inline double logdb(uint64_t v)
  68. {
  69. double d = v / (double)(0x8000 * 0x8000);
  70. if (!v)
  71. return MAX_DB;
  72. return -log10(d) * 10;
  73. }
  74. static void print_stats(AVFilterContext *ctx)
  75. {
  76. VolDetectContext *vd = ctx->priv;
  77. int i, max_volume, shift;
  78. uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
  79. uint64_t histdb[MAX_DB + 1] = { 0 };
  80. for (i = 0; i < 0x10000; i++)
  81. nb_samples += vd->histogram[i];
  82. av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
  83. if (!nb_samples)
  84. return;
  85. /* If nb_samples > 1<<34, there is a risk of overflow in the
  86. multiplication or the sum: shift all histogram values to avoid that.
  87. The total number of samples must be recomputed to avoid rounding
  88. errors. */
  89. shift = av_log2(nb_samples >> 33);
  90. for (i = 0; i < 0x10000; i++) {
  91. nb_samples_shift += vd->histogram[i] >> shift;
  92. power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
  93. }
  94. if (!nb_samples_shift)
  95. return;
  96. power = (power + nb_samples_shift / 2) / nb_samples_shift;
  97. av_assert0(power <= 0x8000 * 0x8000);
  98. av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
  99. max_volume = 0x8000;
  100. while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
  101. !vd->histogram[0x8000 - max_volume])
  102. max_volume--;
  103. av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
  104. for (i = 0; i < 0x10000; i++)
  105. histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
  106. for (i = 0; i <= MAX_DB && !histdb[i]; i++);
  107. for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
  108. av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
  109. sum += histdb[i];
  110. }
  111. }
  112. static av_cold void uninit(AVFilterContext *ctx)
  113. {
  114. print_stats(ctx);
  115. }
  116. static const AVFilterPad volumedetect_inputs[] = {
  117. {
  118. .name = "default",
  119. .type = AVMEDIA_TYPE_AUDIO,
  120. .filter_frame = filter_frame,
  121. },
  122. { NULL }
  123. };
  124. static const AVFilterPad volumedetect_outputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_AUDIO,
  128. },
  129. { NULL }
  130. };
  131. AVFilter ff_af_volumedetect = {
  132. .name = "volumedetect",
  133. .description = NULL_IF_CONFIG_SMALL("Detect audio volume."),
  134. .priv_size = sizeof(VolDetectContext),
  135. .query_formats = query_formats,
  136. .uninit = uninit,
  137. .inputs = volumedetect_inputs,
  138. .outputs = volumedetect_outputs,
  139. };