af_astats.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2009 Rob Sykes <robs@users.sourceforge.net>
  3. * Copyright (c) 2013 Paul B Mahol
  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. #include <float.h>
  22. #include "libavutil/opt.h"
  23. #include "audio.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. typedef struct ChannelStats {
  27. double last;
  28. double sigma_x, sigma_x2;
  29. double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
  30. double min, max;
  31. double min_run, max_run;
  32. double min_runs, max_runs;
  33. uint64_t min_count, max_count;
  34. uint64_t nb_samples;
  35. } ChannelStats;
  36. typedef struct {
  37. const AVClass *class;
  38. ChannelStats *chstats;
  39. int nb_channels;
  40. uint64_t tc_samples;
  41. double time_constant;
  42. double mult;
  43. } AudioStatsContext;
  44. #define OFFSET(x) offsetof(AudioStatsContext, x)
  45. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption astats_options[] = {
  47. { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
  48. { NULL }
  49. };
  50. AVFILTER_DEFINE_CLASS(astats);
  51. static int query_formats(AVFilterContext *ctx)
  52. {
  53. AVFilterFormats *formats;
  54. AVFilterChannelLayouts *layouts;
  55. static const enum AVSampleFormat sample_fmts[] = {
  56. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  57. AV_SAMPLE_FMT_NONE
  58. };
  59. layouts = ff_all_channel_layouts();
  60. if (!layouts)
  61. return AVERROR(ENOMEM);
  62. ff_set_common_channel_layouts(ctx, layouts);
  63. formats = ff_make_format_list(sample_fmts);
  64. if (!formats)
  65. return AVERROR(ENOMEM);
  66. ff_set_common_formats(ctx, formats);
  67. formats = ff_all_samplerates();
  68. if (!formats)
  69. return AVERROR(ENOMEM);
  70. ff_set_common_samplerates(ctx, formats);
  71. return 0;
  72. }
  73. static int config_output(AVFilterLink *outlink)
  74. {
  75. AudioStatsContext *s = outlink->src->priv;
  76. int c;
  77. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  78. if (!s->chstats)
  79. return AVERROR(ENOMEM);
  80. s->nb_channels = outlink->channels;
  81. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  82. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  83. for (c = 0; c < s->nb_channels; c++) {
  84. ChannelStats *p = &s->chstats[c];
  85. p->min = p->min_sigma_x2 = DBL_MAX;
  86. p->max = p->max_sigma_x2 = DBL_MIN;
  87. }
  88. return 0;
  89. }
  90. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
  91. {
  92. if (d < p->min) {
  93. p->min = d;
  94. p->min_run = 1;
  95. p->min_runs = 0;
  96. p->min_count = 1;
  97. } else if (d == p->min) {
  98. p->min_count++;
  99. p->min_run = d == p->last ? p->min_run + 1 : 1;
  100. } else if (p->last == p->min) {
  101. p->min_runs += p->min_run * p->min_run;
  102. }
  103. if (d > p->max) {
  104. p->max = d;
  105. p->max_run = 1;
  106. p->max_runs = 0;
  107. p->max_count = 1;
  108. } else if (d == p->max) {
  109. p->max_count++;
  110. p->max_run = d == p->last ? p->max_run + 1 : 1;
  111. } else if (p->last == p->max) {
  112. p->max_runs += p->max_run * p->max_run;
  113. }
  114. p->sigma_x += d;
  115. p->sigma_x2 += d * d;
  116. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
  117. p->last = d;
  118. if (p->nb_samples >= s->tc_samples) {
  119. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  120. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  121. }
  122. p->nb_samples++;
  123. }
  124. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  125. {
  126. AudioStatsContext *s = inlink->dst->priv;
  127. const int channels = s->nb_channels;
  128. const double *src;
  129. int i, c;
  130. switch (inlink->format) {
  131. case AV_SAMPLE_FMT_DBLP:
  132. for (c = 0; c < channels; c++) {
  133. ChannelStats *p = &s->chstats[c];
  134. src = (const double *)buf->extended_data[c];
  135. for (i = 0; i < buf->nb_samples; i++, src++)
  136. update_stat(s, p, *src);
  137. }
  138. break;
  139. case AV_SAMPLE_FMT_DBL:
  140. src = (const double *)buf->extended_data[0];
  141. for (i = 0; i < buf->nb_samples; i++) {
  142. for (c = 0; c < channels; c++, src++)
  143. update_stat(s, &s->chstats[c], *src);
  144. }
  145. break;
  146. }
  147. return ff_filter_frame(inlink->dst->outputs[0], buf);
  148. }
  149. #define LINEAR_TO_DB(x) (log10(x) * 20)
  150. static void print_stats(AVFilterContext *ctx)
  151. {
  152. AudioStatsContext *s = ctx->priv;
  153. uint64_t min_count = 0, max_count = 0, nb_samples = 0;
  154. double min_runs = 0, max_runs = 0,
  155. min = DBL_MAX, max = DBL_MIN,
  156. max_sigma_x = 0,
  157. sigma_x = 0,
  158. sigma_x2 = 0,
  159. min_sigma_x2 = DBL_MAX,
  160. max_sigma_x2 = DBL_MIN;
  161. int c;
  162. for (c = 0; c < s->nb_channels; c++) {
  163. ChannelStats *p = &s->chstats[c];
  164. if (p->nb_samples < s->tc_samples)
  165. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  166. min = FFMIN(min, p->min);
  167. max = FFMAX(max, p->max);
  168. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  169. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  170. sigma_x += p->sigma_x;
  171. sigma_x2 += p->sigma_x2;
  172. min_count += p->min_count;
  173. max_count += p->max_count;
  174. min_runs += p->min_runs;
  175. max_runs += p->max_runs;
  176. nb_samples += p->nb_samples;
  177. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  178. max_sigma_x = p->sigma_x;
  179. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  180. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  181. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  182. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  183. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  184. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  185. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  186. if (p->min_sigma_x2 != 1)
  187. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  188. av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  189. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
  190. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  191. }
  192. av_log(ctx, AV_LOG_INFO, "Overall\n");
  193. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  194. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  195. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  196. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
  197. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  198. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  199. if (min_sigma_x2 != 1)
  200. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  201. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  202. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  203. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  204. }
  205. static av_cold void uninit(AVFilterContext *ctx)
  206. {
  207. AudioStatsContext *s = ctx->priv;
  208. print_stats(ctx);
  209. av_freep(&s->chstats);
  210. }
  211. static const AVFilterPad astats_inputs[] = {
  212. {
  213. .name = "default",
  214. .type = AVMEDIA_TYPE_AUDIO,
  215. .filter_frame = filter_frame,
  216. },
  217. { NULL }
  218. };
  219. static const AVFilterPad astats_outputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_AUDIO,
  223. .config_props = config_output,
  224. },
  225. { NULL }
  226. };
  227. AVFilter ff_af_astats = {
  228. .name = "astats",
  229. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  230. .query_formats = query_formats,
  231. .priv_size = sizeof(AudioStatsContext),
  232. .priv_class = &astats_class,
  233. .uninit = uninit,
  234. .inputs = astats_inputs,
  235. .outputs = astats_outputs,
  236. };