af_volumedetect.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ff_set_common_formats(ctx, formats);
  44. return 0;
  45. }
  46. static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
  47. {
  48. AVFilterContext *ctx = inlink->dst;
  49. VolDetectContext *vd = ctx->priv;
  50. int64_t layout = samples->channel_layout;
  51. int nb_samples = samples->nb_samples;
  52. int nb_channels = av_get_channel_layout_nb_channels(layout);
  53. int nb_planes = nb_channels;
  54. int plane, i;
  55. int16_t *pcm;
  56. if (!av_sample_fmt_is_planar(samples->format)) {
  57. nb_samples *= nb_channels;
  58. nb_planes = 1;
  59. }
  60. for (plane = 0; plane < nb_planes; plane++) {
  61. pcm = (int16_t *)samples->extended_data[plane];
  62. for (i = 0; i < nb_samples; i++)
  63. vd->histogram[pcm[i] + 0x8000]++;
  64. }
  65. return ff_filter_frame(inlink->dst->outputs[0], samples);
  66. }
  67. #define MAX_DB 91
  68. static inline double logdb(uint64_t v)
  69. {
  70. double d = v / (double)(0x8000 * 0x8000);
  71. if (!v)
  72. return MAX_DB;
  73. return log(d) * -4.3429448190325182765112891891660508229; /* -10/log(10) */
  74. }
  75. static void print_stats(AVFilterContext *ctx)
  76. {
  77. VolDetectContext *vd = ctx->priv;
  78. int i, max_volume, shift;
  79. uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
  80. uint64_t histdb[MAX_DB + 1] = { 0 };
  81. for (i = 0; i < 0x10000; i++)
  82. nb_samples += vd->histogram[i];
  83. av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
  84. if (!nb_samples)
  85. return;
  86. /* If nb_samples > 1<<34, there is a risk of overflow in the
  87. multiplication or the sum: shift all histogram values to avoid that.
  88. The total number of samples must be recomputed to avoid rounding
  89. errors. */
  90. shift = av_log2(nb_samples >> 33);
  91. for (i = 0; i < 0x10000; i++) {
  92. nb_samples_shift += vd->histogram[i] >> shift;
  93. power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
  94. }
  95. if (!nb_samples_shift)
  96. return;
  97. power = (power + nb_samples_shift / 2) / nb_samples_shift;
  98. av_assert0(power <= 0x8000 * 0x8000);
  99. av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
  100. max_volume = 0x8000;
  101. while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
  102. !vd->histogram[0x8000 - max_volume])
  103. max_volume--;
  104. av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
  105. for (i = 0; i < 0x10000; i++)
  106. histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
  107. for (i = 0; i <= MAX_DB && !histdb[i]; i++);
  108. for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
  109. av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
  110. sum += histdb[i];
  111. }
  112. }
  113. static av_cold void uninit(AVFilterContext *ctx)
  114. {
  115. print_stats(ctx);
  116. }
  117. static const AVFilterPad volumedetect_inputs[] = {
  118. {
  119. .name = "default",
  120. .type = AVMEDIA_TYPE_AUDIO,
  121. .filter_frame = filter_frame,
  122. },
  123. { NULL }
  124. };
  125. static const AVFilterPad volumedetect_outputs[] = {
  126. {
  127. .name = "default",
  128. .type = AVMEDIA_TYPE_AUDIO,
  129. },
  130. { NULL }
  131. };
  132. AVFilter ff_af_volumedetect = {
  133. .name = "volumedetect",
  134. .description = NULL_IF_CONFIG_SMALL("Detect audio volume."),
  135. .priv_size = sizeof(VolDetectContext),
  136. .query_formats = query_formats,
  137. .uninit = uninit,
  138. .inputs = volumedetect_inputs,
  139. .outputs = volumedetect_outputs,
  140. };