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.

532 lines
19KB

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