You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.9KB

  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. int ret;
  60. layouts = ff_all_channel_layouts();
  61. if (!layouts)
  62. return AVERROR(ENOMEM);
  63. ret = ff_set_common_channel_layouts(ctx, layouts);
  64. if (ret < 0)
  65. return ret;
  66. formats = ff_make_format_list(sample_fmts);
  67. if (!formats)
  68. return AVERROR(ENOMEM);
  69. ret = ff_set_common_formats(ctx, formats);
  70. if (ret < 0)
  71. return ret;
  72. formats = ff_all_samplerates();
  73. if (!formats)
  74. return AVERROR(ENOMEM);
  75. return ff_set_common_samplerates(ctx, formats);
  76. }
  77. static int config_output(AVFilterLink *outlink)
  78. {
  79. AudioStatsContext *s = outlink->src->priv;
  80. int c;
  81. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  82. if (!s->chstats)
  83. return AVERROR(ENOMEM);
  84. s->nb_channels = outlink->channels;
  85. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  86. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  87. for (c = 0; c < s->nb_channels; c++) {
  88. ChannelStats *p = &s->chstats[c];
  89. p->min = p->min_sigma_x2 = DBL_MAX;
  90. p->max = p->max_sigma_x2 = DBL_MIN;
  91. }
  92. return 0;
  93. }
  94. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
  95. {
  96. if (d < p->min) {
  97. p->min = d;
  98. p->min_run = 1;
  99. p->min_runs = 0;
  100. p->min_count = 1;
  101. } else if (d == p->min) {
  102. p->min_count++;
  103. p->min_run = d == p->last ? p->min_run + 1 : 1;
  104. } else if (p->last == p->min) {
  105. p->min_runs += p->min_run * p->min_run;
  106. }
  107. if (d > p->max) {
  108. p->max = d;
  109. p->max_run = 1;
  110. p->max_runs = 0;
  111. p->max_count = 1;
  112. } else if (d == p->max) {
  113. p->max_count++;
  114. p->max_run = d == p->last ? p->max_run + 1 : 1;
  115. } else if (p->last == p->max) {
  116. p->max_runs += p->max_run * p->max_run;
  117. }
  118. p->sigma_x += d;
  119. p->sigma_x2 += d * d;
  120. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
  121. p->last = d;
  122. if (p->nb_samples >= s->tc_samples) {
  123. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  124. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  125. }
  126. p->nb_samples++;
  127. }
  128. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  129. {
  130. AudioStatsContext *s = inlink->dst->priv;
  131. const int channels = s->nb_channels;
  132. const double *src;
  133. int i, c;
  134. switch (inlink->format) {
  135. case AV_SAMPLE_FMT_DBLP:
  136. for (c = 0; c < channels; c++) {
  137. ChannelStats *p = &s->chstats[c];
  138. src = (const double *)buf->extended_data[c];
  139. for (i = 0; i < buf->nb_samples; i++, src++)
  140. update_stat(s, p, *src);
  141. }
  142. break;
  143. case AV_SAMPLE_FMT_DBL:
  144. src = (const double *)buf->extended_data[0];
  145. for (i = 0; i < buf->nb_samples; i++) {
  146. for (c = 0; c < channels; c++, src++)
  147. update_stat(s, &s->chstats[c], *src);
  148. }
  149. break;
  150. }
  151. return ff_filter_frame(inlink->dst->outputs[0], buf);
  152. }
  153. #define LINEAR_TO_DB(x) (log10(x) * 20)
  154. static void print_stats(AVFilterContext *ctx)
  155. {
  156. AudioStatsContext *s = ctx->priv;
  157. uint64_t min_count = 0, max_count = 0, nb_samples = 0;
  158. double min_runs = 0, max_runs = 0,
  159. min = DBL_MAX, max = DBL_MIN,
  160. max_sigma_x = 0,
  161. sigma_x = 0,
  162. sigma_x2 = 0,
  163. min_sigma_x2 = DBL_MAX,
  164. max_sigma_x2 = DBL_MIN;
  165. int c;
  166. for (c = 0; c < s->nb_channels; c++) {
  167. ChannelStats *p = &s->chstats[c];
  168. if (p->nb_samples < s->tc_samples)
  169. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  170. min = FFMIN(min, p->min);
  171. max = FFMAX(max, p->max);
  172. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  173. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  174. sigma_x += p->sigma_x;
  175. sigma_x2 += p->sigma_x2;
  176. min_count += p->min_count;
  177. max_count += p->max_count;
  178. min_runs += p->min_runs;
  179. max_runs += p->max_runs;
  180. nb_samples += p->nb_samples;
  181. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  182. max_sigma_x = p->sigma_x;
  183. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  184. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  185. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  186. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  187. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  188. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  189. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  190. if (p->min_sigma_x2 != 1)
  191. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  192. 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);
  193. 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)));
  194. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  195. }
  196. av_log(ctx, AV_LOG_INFO, "Overall\n");
  197. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  198. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  199. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  200. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
  201. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  202. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  203. if (min_sigma_x2 != 1)
  204. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  205. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  206. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  207. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  208. }
  209. static av_cold void uninit(AVFilterContext *ctx)
  210. {
  211. AudioStatsContext *s = ctx->priv;
  212. if (s->nb_channels)
  213. print_stats(ctx);
  214. av_freep(&s->chstats);
  215. }
  216. static const AVFilterPad astats_inputs[] = {
  217. {
  218. .name = "default",
  219. .type = AVMEDIA_TYPE_AUDIO,
  220. .filter_frame = filter_frame,
  221. },
  222. { NULL }
  223. };
  224. static const AVFilterPad astats_outputs[] = {
  225. {
  226. .name = "default",
  227. .type = AVMEDIA_TYPE_AUDIO,
  228. .config_props = config_output,
  229. },
  230. { NULL }
  231. };
  232. AVFilter ff_af_astats = {
  233. .name = "astats",
  234. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  235. .query_formats = query_formats,
  236. .priv_size = sizeof(AudioStatsContext),
  237. .priv_class = &astats_class,
  238. .uninit = uninit,
  239. .inputs = astats_inputs,
  240. .outputs = astats_outputs,
  241. };