af_astats.c 15 KB

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