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.

374 lines
13KB

  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. int metadata;
  44. int reset_count;
  45. int nb_frames;
  46. } AudioStatsContext;
  47. #define OFFSET(x) offsetof(AudioStatsContext, x)
  48. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. static const AVOption astats_options[] = {
  50. { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
  51. { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  52. { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  53. { NULL }
  54. };
  55. AVFILTER_DEFINE_CLASS(astats);
  56. static int query_formats(AVFilterContext *ctx)
  57. {
  58. AVFilterFormats *formats;
  59. AVFilterChannelLayouts *layouts;
  60. static const enum AVSampleFormat sample_fmts[] = {
  61. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  62. AV_SAMPLE_FMT_NONE
  63. };
  64. int ret;
  65. layouts = ff_all_channel_layouts();
  66. if (!layouts)
  67. return AVERROR(ENOMEM);
  68. ret = ff_set_common_channel_layouts(ctx, layouts);
  69. if (ret < 0)
  70. return ret;
  71. formats = ff_make_format_list(sample_fmts);
  72. if (!formats)
  73. return AVERROR(ENOMEM);
  74. ret = ff_set_common_formats(ctx, formats);
  75. if (ret < 0)
  76. return ret;
  77. formats = ff_all_samplerates();
  78. if (!formats)
  79. return AVERROR(ENOMEM);
  80. return ff_set_common_samplerates(ctx, formats);
  81. }
  82. static void reset_stats(AudioStatsContext *s)
  83. {
  84. int c;
  85. memset(s->chstats, 0, sizeof(*s->chstats));
  86. for (c = 0; c < s->nb_channels; c++) {
  87. ChannelStats *p = &s->chstats[c];
  88. p->min = p->min_sigma_x2 = DBL_MAX;
  89. p->max = p->max_sigma_x2 = DBL_MIN;
  90. }
  91. }
  92. static int config_output(AVFilterLink *outlink)
  93. {
  94. AudioStatsContext *s = outlink->src->priv;
  95. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  96. if (!s->chstats)
  97. return AVERROR(ENOMEM);
  98. s->nb_channels = outlink->channels;
  99. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  100. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  101. reset_stats(s);
  102. return 0;
  103. }
  104. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
  105. {
  106. if (d < p->min) {
  107. p->min = d;
  108. p->min_run = 1;
  109. p->min_runs = 0;
  110. p->min_count = 1;
  111. } else if (d == p->min) {
  112. p->min_count++;
  113. p->min_run = d == p->last ? p->min_run + 1 : 1;
  114. } else if (p->last == p->min) {
  115. p->min_runs += p->min_run * p->min_run;
  116. }
  117. if (d > p->max) {
  118. p->max = d;
  119. p->max_run = 1;
  120. p->max_runs = 0;
  121. p->max_count = 1;
  122. } else if (d == p->max) {
  123. p->max_count++;
  124. p->max_run = d == p->last ? p->max_run + 1 : 1;
  125. } else if (p->last == p->max) {
  126. p->max_runs += p->max_run * p->max_run;
  127. }
  128. p->sigma_x += d;
  129. p->sigma_x2 += d * d;
  130. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
  131. p->last = d;
  132. if (p->nb_samples >= s->tc_samples) {
  133. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  134. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  135. }
  136. p->nb_samples++;
  137. }
  138. static void set_meta(AVDictionary **metadata, int chan, const char *key,
  139. const char *fmt, double val)
  140. {
  141. uint8_t value[128];
  142. uint8_t key2[128];
  143. snprintf(value, sizeof(value), fmt, val);
  144. if (chan)
  145. snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
  146. else
  147. snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
  148. av_dict_set(metadata, key2, value, 0);
  149. }
  150. #define LINEAR_TO_DB(x) (log10(x) * 20)
  151. static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
  152. {
  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. set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
  180. set_meta(metadata, c + 1, "Min_level", "%f", p->min);
  181. set_meta(metadata, c + 1, "Max_level", "%f", p->max);
  182. set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  183. set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  184. set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  185. set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  186. set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  187. set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
  188. set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
  189. }
  190. set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
  191. set_meta(metadata, 0, "Overall.Min_level", "%f", min);
  192. set_meta(metadata, 0, "Overall.Max_level", "%f", max);
  193. set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-min, max)));
  194. set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  195. set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  196. set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  197. set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  198. set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
  199. set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
  200. }
  201. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  202. {
  203. AudioStatsContext *s = inlink->dst->priv;
  204. AVDictionary **metadata = avpriv_frame_get_metadatap(buf);
  205. const int channels = s->nb_channels;
  206. const double *src;
  207. int i, c;
  208. switch (inlink->format) {
  209. case AV_SAMPLE_FMT_DBLP:
  210. for (c = 0; c < channels; c++) {
  211. ChannelStats *p = &s->chstats[c];
  212. src = (const double *)buf->extended_data[c];
  213. for (i = 0; i < buf->nb_samples; i++, src++)
  214. update_stat(s, p, *src);
  215. }
  216. break;
  217. case AV_SAMPLE_FMT_DBL:
  218. src = (const double *)buf->extended_data[0];
  219. for (i = 0; i < buf->nb_samples; i++) {
  220. for (c = 0; c < channels; c++, src++)
  221. update_stat(s, &s->chstats[c], *src);
  222. }
  223. break;
  224. }
  225. if (s->metadata)
  226. set_metadata(s, metadata);
  227. if (s->reset_count > 0) {
  228. s->nb_frames++;
  229. if (s->nb_frames >= s->reset_count) {
  230. reset_stats(s);
  231. s->nb_frames = 0;
  232. }
  233. }
  234. return ff_filter_frame(inlink->dst->outputs[0], buf);
  235. }
  236. static void print_stats(AVFilterContext *ctx)
  237. {
  238. AudioStatsContext *s = ctx->priv;
  239. uint64_t min_count = 0, max_count = 0, nb_samples = 0;
  240. double min_runs = 0, max_runs = 0,
  241. min = DBL_MAX, max = DBL_MIN,
  242. max_sigma_x = 0,
  243. sigma_x = 0,
  244. sigma_x2 = 0,
  245. min_sigma_x2 = DBL_MAX,
  246. max_sigma_x2 = DBL_MIN;
  247. int c;
  248. for (c = 0; c < s->nb_channels; c++) {
  249. ChannelStats *p = &s->chstats[c];
  250. if (p->nb_samples < s->tc_samples)
  251. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  252. min = FFMIN(min, p->min);
  253. max = FFMAX(max, p->max);
  254. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  255. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  256. sigma_x += p->sigma_x;
  257. sigma_x2 += p->sigma_x2;
  258. min_count += p->min_count;
  259. max_count += p->max_count;
  260. min_runs += p->min_runs;
  261. max_runs += p->max_runs;
  262. nb_samples += p->nb_samples;
  263. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  264. max_sigma_x = p->sigma_x;
  265. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  266. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  267. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  268. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  269. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  270. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  271. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  272. if (p->min_sigma_x2 != 1)
  273. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  274. 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);
  275. 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)));
  276. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  277. }
  278. av_log(ctx, AV_LOG_INFO, "Overall\n");
  279. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  280. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  281. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  282. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
  283. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  284. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  285. if (min_sigma_x2 != 1)
  286. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  287. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  288. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  289. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  290. }
  291. static av_cold void uninit(AVFilterContext *ctx)
  292. {
  293. AudioStatsContext *s = ctx->priv;
  294. if (s->nb_channels)
  295. print_stats(ctx);
  296. av_freep(&s->chstats);
  297. }
  298. static const AVFilterPad astats_inputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_AUDIO,
  302. .filter_frame = filter_frame,
  303. },
  304. { NULL }
  305. };
  306. static const AVFilterPad astats_outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_AUDIO,
  310. .config_props = config_output,
  311. },
  312. { NULL }
  313. };
  314. AVFilter ff_af_astats = {
  315. .name = "astats",
  316. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  317. .query_formats = query_formats,
  318. .priv_size = sizeof(AudioStatsContext),
  319. .priv_class = &astats_class,
  320. .uninit = uninit,
  321. .inputs = astats_inputs,
  322. .outputs = astats_outputs,
  323. };