af_astats.c 15 KB

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