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.

851 lines
38KB

  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 <math.h>
  23. #include "libavutil/opt.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #define HISTOGRAM_SIZE 8192
  28. #define HISTOGRAM_MAX (HISTOGRAM_SIZE-1)
  29. #define MEASURE_ALL UINT_MAX
  30. #define MEASURE_NONE 0
  31. #define MEASURE_DC_OFFSET (1 << 0)
  32. #define MEASURE_MIN_LEVEL (1 << 1)
  33. #define MEASURE_MAX_LEVEL (1 << 2)
  34. #define MEASURE_MIN_DIFFERENCE (1 << 3)
  35. #define MEASURE_MAX_DIFFERENCE (1 << 4)
  36. #define MEASURE_MEAN_DIFFERENCE (1 << 5)
  37. #define MEASURE_RMS_DIFFERENCE (1 << 6)
  38. #define MEASURE_PEAK_LEVEL (1 << 7)
  39. #define MEASURE_RMS_LEVEL (1 << 8)
  40. #define MEASURE_RMS_PEAK (1 << 9)
  41. #define MEASURE_RMS_TROUGH (1 << 10)
  42. #define MEASURE_CREST_FACTOR (1 << 11)
  43. #define MEASURE_FLAT_FACTOR (1 << 12)
  44. #define MEASURE_PEAK_COUNT (1 << 13)
  45. #define MEASURE_BIT_DEPTH (1 << 14)
  46. #define MEASURE_DYNAMIC_RANGE (1 << 15)
  47. #define MEASURE_ZERO_CROSSINGS (1 << 16)
  48. #define MEASURE_ZERO_CROSSINGS_RATE (1 << 17)
  49. #define MEASURE_NUMBER_OF_SAMPLES (1 << 18)
  50. #define MEASURE_NUMBER_OF_NANS (1 << 19)
  51. #define MEASURE_NUMBER_OF_INFS (1 << 20)
  52. #define MEASURE_NUMBER_OF_DENORMALS (1 << 21)
  53. #define MEASURE_NOISE_FLOOR (1 << 22)
  54. #define MEASURE_NOISE_FLOOR_COUNT (1 << 23)
  55. #define MEASURE_MINMAXPEAK (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL)
  56. typedef struct ChannelStats {
  57. double last;
  58. double last_non_zero;
  59. double min_non_zero;
  60. double sigma_x, sigma_x2;
  61. double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
  62. double min, max;
  63. double nmin, nmax;
  64. double min_run, max_run;
  65. double min_runs, max_runs;
  66. double min_diff, max_diff;
  67. double diff1_sum;
  68. double diff1_sum_x2;
  69. uint64_t mask, imask;
  70. uint64_t min_count, max_count;
  71. uint64_t noise_floor_count;
  72. uint64_t zero_runs;
  73. uint64_t nb_samples;
  74. uint64_t nb_nans;
  75. uint64_t nb_infs;
  76. uint64_t nb_denormals;
  77. double *win_samples;
  78. unsigned histogram[HISTOGRAM_SIZE];
  79. int win_pos;
  80. int max_index;
  81. double noise_floor;
  82. } ChannelStats;
  83. typedef struct AudioStatsContext {
  84. const AVClass *class;
  85. ChannelStats *chstats;
  86. int nb_channels;
  87. uint64_t tc_samples;
  88. double time_constant;
  89. double mult;
  90. int metadata;
  91. int reset_count;
  92. int nb_frames;
  93. int maxbitdepth;
  94. int measure_perchannel;
  95. int measure_overall;
  96. int is_float;
  97. int is_double;
  98. } AudioStatsContext;
  99. #define OFFSET(x) offsetof(AudioStatsContext, x)
  100. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  101. static const AVOption astats_options[] = {
  102. { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
  103. { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  104. { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  105. { "measure_perchannel", "only measure_perchannel these per-channel statistics", OFFSET(measure_perchannel), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
  106. { "none" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NONE }, 0, 0, FLAGS, "measure" },
  107. { "all" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ALL }, 0, 0, FLAGS, "measure" },
  108. { "DC_offset" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DC_OFFSET }, 0, 0, FLAGS, "measure" },
  109. { "Min_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_LEVEL }, 0, 0, FLAGS, "measure" },
  110. { "Max_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_LEVEL }, 0, 0, FLAGS, "measure" },
  111. { "Min_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
  112. { "Max_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_DIFFERENCE }, 0, 0, FLAGS, "measure" },
  113. { "Mean_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MEAN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
  114. { "RMS_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_DIFFERENCE }, 0, 0, FLAGS, "measure" },
  115. { "Peak_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_LEVEL }, 0, 0, FLAGS, "measure" },
  116. { "RMS_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_LEVEL }, 0, 0, FLAGS, "measure" },
  117. { "RMS_peak" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_PEAK }, 0, 0, FLAGS, "measure" },
  118. { "RMS_trough" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_TROUGH }, 0, 0, FLAGS, "measure" },
  119. { "Crest_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_CREST_FACTOR }, 0, 0, FLAGS, "measure" },
  120. { "Flat_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_FLAT_FACTOR }, 0, 0, FLAGS, "measure" },
  121. { "Peak_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_COUNT }, 0, 0, FLAGS, "measure" },
  122. { "Bit_depth" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_BIT_DEPTH }, 0, 0, FLAGS, "measure" },
  123. { "Dynamic_range" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE }, 0, 0, FLAGS, "measure" },
  124. { "Zero_crossings" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS }, 0, 0, FLAGS, "measure" },
  125. { "Zero_crossings_rate" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
  126. { "Noise_floor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR }, 0, 0, FLAGS, "measure" },
  127. { "Noise_floor_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR_COUNT }, 0, 0, FLAGS, "measure" },
  128. { "Number_of_samples" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES }, 0, 0, FLAGS, "measure" },
  129. { "Number_of_NaNs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_NANS }, 0, 0, FLAGS, "measure" },
  130. { "Number_of_Infs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS }, 0, 0, FLAGS, "measure" },
  131. { "Number_of_denormals" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_DENORMALS }, 0, 0, FLAGS, "measure" },
  132. { "measure_overall", "only measure_perchannel these overall statistics", OFFSET(measure_overall), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
  133. { NULL }
  134. };
  135. AVFILTER_DEFINE_CLASS(astats);
  136. static int query_formats(AVFilterContext *ctx)
  137. {
  138. AVFilterFormats *formats;
  139. AVFilterChannelLayouts *layouts;
  140. static const enum AVSampleFormat sample_fmts[] = {
  141. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  142. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  143. AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
  144. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  145. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  146. AV_SAMPLE_FMT_NONE
  147. };
  148. int ret;
  149. layouts = ff_all_channel_counts();
  150. if (!layouts)
  151. return AVERROR(ENOMEM);
  152. ret = ff_set_common_channel_layouts(ctx, layouts);
  153. if (ret < 0)
  154. return ret;
  155. formats = ff_make_format_list(sample_fmts);
  156. if (!formats)
  157. return AVERROR(ENOMEM);
  158. ret = ff_set_common_formats(ctx, formats);
  159. if (ret < 0)
  160. return ret;
  161. formats = ff_all_samplerates();
  162. if (!formats)
  163. return AVERROR(ENOMEM);
  164. return ff_set_common_samplerates(ctx, formats);
  165. }
  166. static void reset_stats(AudioStatsContext *s)
  167. {
  168. int c;
  169. for (c = 0; c < s->nb_channels; c++) {
  170. ChannelStats *p = &s->chstats[c];
  171. p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
  172. p->max = p->nmax = p->max_sigma_x2 =-DBL_MAX;
  173. p->min_non_zero = DBL_MAX;
  174. p->min_diff = DBL_MAX;
  175. p->max_diff = 0;
  176. p->sigma_x = 0;
  177. p->sigma_x2 = 0;
  178. p->avg_sigma_x2 = 0;
  179. p->min_run = 0;
  180. p->max_run = 0;
  181. p->min_runs = 0;
  182. p->max_runs = 0;
  183. p->diff1_sum = 0;
  184. p->diff1_sum_x2 = 0;
  185. p->mask = 0;
  186. p->imask = 0xFFFFFFFFFFFFFFFF;
  187. p->min_count = 0;
  188. p->max_count = 0;
  189. p->zero_runs = 0;
  190. p->nb_samples = 0;
  191. p->nb_nans = 0;
  192. p->nb_infs = 0;
  193. p->nb_denormals = 0;
  194. p->last = NAN;
  195. p->noise_floor = NAN;
  196. p->noise_floor_count = 0;
  197. p->win_pos = 0;
  198. memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples));
  199. memset(p->histogram, 0, sizeof(p->histogram));
  200. }
  201. }
  202. static int config_output(AVFilterLink *outlink)
  203. {
  204. AudioStatsContext *s = outlink->src->priv;
  205. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  206. if (!s->chstats)
  207. return AVERROR(ENOMEM);
  208. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  209. s->nb_channels = outlink->channels;
  210. for (int i = 0; i < s->nb_channels; i++) {
  211. ChannelStats *p = &s->chstats[i];
  212. p->win_samples = av_calloc(s->tc_samples, sizeof(*p->win_samples));
  213. if (!p->win_samples)
  214. return AVERROR(ENOMEM);
  215. }
  216. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  217. s->nb_frames = 0;
  218. s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
  219. s->is_double = outlink->format == AV_SAMPLE_FMT_DBL ||
  220. outlink->format == AV_SAMPLE_FMT_DBLP;
  221. s->is_float = outlink->format == AV_SAMPLE_FMT_FLT ||
  222. outlink->format == AV_SAMPLE_FMT_FLTP;
  223. reset_stats(s);
  224. return 0;
  225. }
  226. static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
  227. {
  228. unsigned result = s->maxbitdepth;
  229. mask = mask & (~imask);
  230. for (; result && !(mask & 1); --result, mask >>= 1);
  231. depth->den = result;
  232. depth->num = 0;
  233. for (; result; --result, mask >>= 1)
  234. if (mask & 1)
  235. depth->num++;
  236. }
  237. static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
  238. {
  239. if (d < p->min)
  240. p->min = d;
  241. if (d > p->max)
  242. p->max = d;
  243. }
  244. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
  245. {
  246. double drop;
  247. int index;
  248. if (d < p->min) {
  249. p->min = d;
  250. p->nmin = nd;
  251. p->min_run = 1;
  252. p->min_runs = 0;
  253. p->min_count = 1;
  254. } else if (d == p->min) {
  255. p->min_count++;
  256. p->min_run = d == p->last ? p->min_run + 1 : 1;
  257. } else if (p->last == p->min) {
  258. p->min_runs += p->min_run * p->min_run;
  259. }
  260. if (d != 0 && FFABS(d) < p->min_non_zero)
  261. p->min_non_zero = FFABS(d);
  262. if (d > p->max) {
  263. p->max = d;
  264. p->nmax = nd;
  265. p->max_run = 1;
  266. p->max_runs = 0;
  267. p->max_count = 1;
  268. } else if (d == p->max) {
  269. p->max_count++;
  270. p->max_run = d == p->last ? p->max_run + 1 : 1;
  271. } else if (p->last == p->max) {
  272. p->max_runs += p->max_run * p->max_run;
  273. }
  274. if (d != 0) {
  275. p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
  276. p->last_non_zero = d;
  277. }
  278. p->sigma_x += nd;
  279. p->sigma_x2 += nd * nd;
  280. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
  281. if (!isnan(p->last)) {
  282. p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
  283. p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
  284. p->diff1_sum += fabs(d - p->last);
  285. p->diff1_sum_x2 += (d - p->last) * (d - p->last);
  286. }
  287. p->last = d;
  288. p->mask |= i;
  289. p->imask &= i;
  290. drop = p->win_samples[p->win_pos];
  291. p->win_samples[p->win_pos] = nd;
  292. index = av_clip(FFABS(av_clipd(nd, -1.0, 1.0)) * HISTOGRAM_MAX, 0, HISTOGRAM_MAX);
  293. p->max_index = FFMAX(p->max_index, index);
  294. p->histogram[index]++;
  295. if (!isnan(p->noise_floor))
  296. p->histogram[av_clip(FFABS(drop) * HISTOGRAM_MAX, 0, HISTOGRAM_MAX)]--;
  297. p->win_pos++;
  298. while (p->histogram[p->max_index] == 0)
  299. p->max_index--;
  300. if (p->win_pos >= s->tc_samples || !isnan(p->noise_floor)) {
  301. double noise_floor = 1.;
  302. for (int i = p->max_index; i >= 0; i--) {
  303. if (p->histogram[i]) {
  304. noise_floor = i / (double)HISTOGRAM_MAX;
  305. break;
  306. }
  307. }
  308. if (isnan(p->noise_floor)) {
  309. p->noise_floor = noise_floor;
  310. p->noise_floor_count = 1;
  311. } else {
  312. if (noise_floor < p->noise_floor) {
  313. p->noise_floor = noise_floor;
  314. p->noise_floor_count = 1;
  315. } else if (noise_floor == p->noise_floor) {
  316. p->noise_floor_count++;
  317. }
  318. }
  319. }
  320. if (p->win_pos >= s->tc_samples) {
  321. p->win_pos = 0;
  322. }
  323. if (p->nb_samples >= s->tc_samples) {
  324. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  325. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  326. }
  327. p->nb_samples++;
  328. }
  329. static inline void update_float_stat(AudioStatsContext *s, ChannelStats *p, float d)
  330. {
  331. int type = fpclassify(d);
  332. p->nb_nans += type == FP_NAN;
  333. p->nb_infs += type == FP_INFINITE;
  334. p->nb_denormals += type == FP_SUBNORMAL;
  335. }
  336. static inline void update_double_stat(AudioStatsContext *s, ChannelStats *p, double d)
  337. {
  338. int type = fpclassify(d);
  339. p->nb_nans += type == FP_NAN;
  340. p->nb_infs += type == FP_INFINITE;
  341. p->nb_denormals += type == FP_SUBNORMAL;
  342. }
  343. static void set_meta(AVDictionary **metadata, int chan, const char *key,
  344. const char *fmt, double val)
  345. {
  346. uint8_t value[128];
  347. uint8_t key2[128];
  348. snprintf(value, sizeof(value), fmt, val);
  349. if (chan)
  350. snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
  351. else
  352. snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
  353. av_dict_set(metadata, key2, value, 0);
  354. }
  355. #define LINEAR_TO_DB(x) (log10(x) * 20)
  356. static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
  357. {
  358. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
  359. uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
  360. double min_runs = 0, max_runs = 0,
  361. min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
  362. nmin = DBL_MAX, nmax =-DBL_MAX,
  363. max_sigma_x = 0,
  364. diff1_sum = 0,
  365. diff1_sum_x2 = 0,
  366. sigma_x = 0,
  367. sigma_x2 = 0,
  368. noise_floor = 0,
  369. min_sigma_x2 = DBL_MAX,
  370. max_sigma_x2 =-DBL_MAX;
  371. AVRational depth;
  372. int c;
  373. for (c = 0; c < s->nb_channels; c++) {
  374. ChannelStats *p = &s->chstats[c];
  375. if (p->nb_samples < s->tc_samples)
  376. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  377. min = FFMIN(min, p->min);
  378. max = FFMAX(max, p->max);
  379. nmin = FFMIN(nmin, p->nmin);
  380. nmax = FFMAX(nmax, p->nmax);
  381. min_diff = FFMIN(min_diff, p->min_diff);
  382. max_diff = FFMAX(max_diff, p->max_diff);
  383. diff1_sum += p->diff1_sum;
  384. diff1_sum_x2 += p->diff1_sum_x2;
  385. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  386. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  387. sigma_x += p->sigma_x;
  388. sigma_x2 += p->sigma_x2;
  389. noise_floor = FFMAX(noise_floor, p->noise_floor);
  390. noise_floor_count += p->noise_floor_count;
  391. min_count += p->min_count;
  392. max_count += p->max_count;
  393. min_runs += p->min_runs;
  394. max_runs += p->max_runs;
  395. mask |= p->mask;
  396. imask &= p->imask;
  397. nb_samples += p->nb_samples;
  398. nb_nans += p->nb_nans;
  399. nb_infs += p->nb_infs;
  400. nb_denormals += p->nb_denormals;
  401. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  402. max_sigma_x = p->sigma_x;
  403. if (s->measure_perchannel & MEASURE_DC_OFFSET)
  404. set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
  405. if (s->measure_perchannel & MEASURE_MIN_LEVEL)
  406. set_meta(metadata, c + 1, "Min_level", "%f", p->min);
  407. if (s->measure_perchannel & MEASURE_MAX_LEVEL)
  408. set_meta(metadata, c + 1, "Max_level", "%f", p->max);
  409. if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
  410. set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
  411. if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
  412. set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
  413. if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
  414. set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
  415. if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
  416. set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
  417. if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
  418. set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
  419. if (s->measure_perchannel & MEASURE_RMS_LEVEL)
  420. set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  421. if (s->measure_perchannel & MEASURE_RMS_PEAK)
  422. set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  423. if (s->measure_perchannel & MEASURE_RMS_TROUGH)
  424. set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  425. if (s->measure_perchannel & MEASURE_CREST_FACTOR)
  426. set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  427. if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
  428. set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
  429. if (s->measure_perchannel & MEASURE_PEAK_COUNT)
  430. set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
  431. if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
  432. set_meta(metadata, c + 1, "Noise_floor", "%f", LINEAR_TO_DB(p->noise_floor));
  433. if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
  434. set_meta(metadata, c + 1, "Noise_floor_count", "%f", p->noise_floor_count);
  435. if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
  436. bit_depth(s, p->mask, p->imask, &depth);
  437. set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
  438. set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
  439. }
  440. if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
  441. set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
  442. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
  443. set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
  444. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
  445. set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
  446. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
  447. set_meta(metadata, c + 1, "Number of NaNs", "%f", p->nb_nans);
  448. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
  449. set_meta(metadata, c + 1, "Number of Infs", "%f", p->nb_infs);
  450. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
  451. set_meta(metadata, c + 1, "Number of denormals", "%f", p->nb_denormals);
  452. }
  453. if (s->measure_overall & MEASURE_DC_OFFSET)
  454. set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
  455. if (s->measure_overall & MEASURE_MIN_LEVEL)
  456. set_meta(metadata, 0, "Overall.Min_level", "%f", min);
  457. if (s->measure_overall & MEASURE_MAX_LEVEL)
  458. set_meta(metadata, 0, "Overall.Max_level", "%f", max);
  459. if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
  460. set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
  461. if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
  462. set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
  463. if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
  464. set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
  465. if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
  466. set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
  467. if (s->measure_overall & MEASURE_PEAK_LEVEL)
  468. set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
  469. if (s->measure_overall & MEASURE_RMS_LEVEL)
  470. set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  471. if (s->measure_overall & MEASURE_RMS_PEAK)
  472. set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  473. if (s->measure_overall & MEASURE_RMS_TROUGH)
  474. set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  475. if (s->measure_overall & MEASURE_FLAT_FACTOR)
  476. set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  477. if (s->measure_overall & MEASURE_PEAK_COUNT)
  478. set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
  479. if (s->measure_overall & MEASURE_NOISE_FLOOR)
  480. set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor));
  481. if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
  482. set_meta(metadata, 0, "Overall.Noise_floor_count", "%f", noise_floor_count / (double)s->nb_channels);
  483. if (s->measure_overall & MEASURE_BIT_DEPTH) {
  484. bit_depth(s, mask, imask, &depth);
  485. set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
  486. set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
  487. }
  488. if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
  489. set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
  490. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
  491. set_meta(metadata, 0, "Number of NaNs", "%f", nb_nans / (float)s->nb_channels);
  492. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
  493. set_meta(metadata, 0, "Number of Infs", "%f", nb_infs / (float)s->nb_channels);
  494. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
  495. set_meta(metadata, 0, "Number of denormals", "%f", nb_denormals / (float)s->nb_channels);
  496. }
  497. #define UPDATE_STATS_P(type, update_func, update_float, channel_func) \
  498. for (int c = start; c < end; c++) { \
  499. ChannelStats *p = &s->chstats[c]; \
  500. const type *src = (const type *)data[c]; \
  501. const type * const srcend = src + samples; \
  502. for (; src < srcend; src++) { \
  503. update_func; \
  504. update_float; \
  505. } \
  506. channel_func; \
  507. }
  508. #define UPDATE_STATS_I(type, update_func, update_float, channel_func) \
  509. for (int c = start; c < end; c++) { \
  510. ChannelStats *p = &s->chstats[c]; \
  511. const type *src = (const type *)data[0]; \
  512. const type * const srcend = src + samples * channels; \
  513. for (src += c; src < srcend; src += channels) { \
  514. update_func; \
  515. update_float; \
  516. } \
  517. channel_func; \
  518. }
  519. #define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
  520. if ((s->measure_overall | s->measure_perchannel) & ~MEASURE_MINMAXPEAK) { \
  521. UPDATE_STATS_##planar(type, update_stat(s, p, sample, sample normalizer_suffix, int_sample), s->is_float ? update_float_stat(s, p, sample) : s->is_double ? update_double_stat(s, p, sample) : (void)NULL, ); \
  522. } else { \
  523. UPDATE_STATS_##planar(type, update_minmax(s, p, sample), , p->nmin = p->min normalizer_suffix; p->nmax = p->max normalizer_suffix;); \
  524. }
  525. static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  526. {
  527. AudioStatsContext *s = ctx->priv;
  528. AVFilterLink *inlink = ctx->inputs[0];
  529. AVFrame *buf = arg;
  530. const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
  531. const int channels = s->nb_channels;
  532. const int samples = buf->nb_samples;
  533. const int start = (buf->channels * jobnr) / nb_jobs;
  534. const int end = (buf->channels * (jobnr+1)) / nb_jobs;
  535. switch (inlink->format) {
  536. case AV_SAMPLE_FMT_DBLP:
  537. UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
  538. break;
  539. case AV_SAMPLE_FMT_DBL:
  540. UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
  541. break;
  542. case AV_SAMPLE_FMT_FLTP:
  543. UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
  544. break;
  545. case AV_SAMPLE_FMT_FLT:
  546. UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
  547. break;
  548. case AV_SAMPLE_FMT_S64P:
  549. UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
  550. break;
  551. case AV_SAMPLE_FMT_S64:
  552. UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
  553. break;
  554. case AV_SAMPLE_FMT_S32P:
  555. UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
  556. break;
  557. case AV_SAMPLE_FMT_S32:
  558. UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
  559. break;
  560. case AV_SAMPLE_FMT_S16P:
  561. UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
  562. break;
  563. case AV_SAMPLE_FMT_S16:
  564. UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
  565. break;
  566. }
  567. return 0;
  568. }
  569. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  570. {
  571. AVFilterContext *ctx = inlink->dst;
  572. AudioStatsContext *s = ctx->priv;
  573. AVDictionary **metadata = &buf->metadata;
  574. if (s->reset_count > 0) {
  575. if (s->nb_frames >= s->reset_count) {
  576. reset_stats(s);
  577. s->nb_frames = 0;
  578. }
  579. s->nb_frames++;
  580. }
  581. ctx->internal->execute(ctx, filter_channel, buf, NULL, FFMIN(inlink->channels, ff_filter_get_nb_threads(ctx)));
  582. if (s->metadata)
  583. set_metadata(s, metadata);
  584. return ff_filter_frame(inlink->dst->outputs[0], buf);
  585. }
  586. static void print_stats(AVFilterContext *ctx)
  587. {
  588. AudioStatsContext *s = ctx->priv;
  589. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
  590. uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
  591. double min_runs = 0, max_runs = 0,
  592. min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
  593. nmin = DBL_MAX, nmax =-DBL_MAX,
  594. max_sigma_x = 0,
  595. diff1_sum_x2 = 0,
  596. diff1_sum = 0,
  597. sigma_x = 0,
  598. sigma_x2 = 0,
  599. noise_floor = 0,
  600. min_sigma_x2 = DBL_MAX,
  601. max_sigma_x2 =-DBL_MAX;
  602. AVRational depth;
  603. int c;
  604. for (c = 0; c < s->nb_channels; c++) {
  605. ChannelStats *p = &s->chstats[c];
  606. if (p->nb_samples < s->tc_samples)
  607. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  608. min = FFMIN(min, p->min);
  609. max = FFMAX(max, p->max);
  610. nmin = FFMIN(nmin, p->nmin);
  611. nmax = FFMAX(nmax, p->nmax);
  612. min_diff = FFMIN(min_diff, p->min_diff);
  613. max_diff = FFMAX(max_diff, p->max_diff);
  614. diff1_sum_x2 += p->diff1_sum_x2;
  615. diff1_sum += p->diff1_sum;
  616. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  617. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  618. sigma_x += p->sigma_x;
  619. sigma_x2 += p->sigma_x2;
  620. noise_floor = FFMAX(noise_floor, p->noise_floor);
  621. min_count += p->min_count;
  622. max_count += p->max_count;
  623. noise_floor_count += p->noise_floor_count;
  624. min_runs += p->min_runs;
  625. max_runs += p->max_runs;
  626. mask |= p->mask;
  627. imask &= p->imask;
  628. nb_samples += p->nb_samples;
  629. nb_nans += p->nb_nans;
  630. nb_infs += p->nb_infs;
  631. nb_denormals += p->nb_denormals;
  632. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  633. max_sigma_x = p->sigma_x;
  634. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  635. if (s->measure_perchannel & MEASURE_DC_OFFSET)
  636. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  637. if (s->measure_perchannel & MEASURE_MIN_LEVEL)
  638. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  639. if (s->measure_perchannel & MEASURE_MAX_LEVEL)
  640. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  641. if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
  642. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
  643. if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
  644. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
  645. if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
  646. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
  647. if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
  648. av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
  649. if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
  650. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
  651. if (s->measure_perchannel & MEASURE_RMS_LEVEL)
  652. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  653. if (s->measure_perchannel & MEASURE_RMS_PEAK)
  654. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  655. if (s->measure_perchannel & MEASURE_RMS_TROUGH)
  656. if (p->min_sigma_x2 != 1)
  657. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  658. if (s->measure_perchannel & MEASURE_CREST_FACTOR)
  659. 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);
  660. if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
  661. 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)));
  662. if (s->measure_perchannel & MEASURE_PEAK_COUNT)
  663. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  664. if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
  665. av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor));
  666. if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
  667. av_log(ctx, AV_LOG_INFO, "Noise floor count: %"PRId64"\n", p->noise_floor_count);
  668. if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
  669. bit_depth(s, p->mask, p->imask, &depth);
  670. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  671. }
  672. if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
  673. av_log(ctx, AV_LOG_INFO, "Dynamic range: %f\n", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
  674. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
  675. av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
  676. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
  677. av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
  678. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
  679. av_log(ctx, AV_LOG_INFO, "Number of NaNs: %"PRId64"\n", p->nb_nans);
  680. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
  681. av_log(ctx, AV_LOG_INFO, "Number of Infs: %"PRId64"\n", p->nb_infs);
  682. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
  683. av_log(ctx, AV_LOG_INFO, "Number of denormals: %"PRId64"\n", p->nb_denormals);
  684. }
  685. av_log(ctx, AV_LOG_INFO, "Overall\n");
  686. if (s->measure_overall & MEASURE_DC_OFFSET)
  687. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  688. if (s->measure_overall & MEASURE_MIN_LEVEL)
  689. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  690. if (s->measure_overall & MEASURE_MAX_LEVEL)
  691. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  692. if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
  693. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
  694. if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
  695. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
  696. if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
  697. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
  698. if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
  699. av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
  700. if (s->measure_overall & MEASURE_PEAK_LEVEL)
  701. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
  702. if (s->measure_overall & MEASURE_RMS_LEVEL)
  703. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  704. if (s->measure_overall & MEASURE_RMS_PEAK)
  705. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  706. if (s->measure_overall & MEASURE_RMS_TROUGH)
  707. if (min_sigma_x2 != 1)
  708. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  709. if (s->measure_overall & MEASURE_FLAT_FACTOR)
  710. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  711. if (s->measure_overall & MEASURE_PEAK_COUNT)
  712. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  713. if (s->measure_overall & MEASURE_NOISE_FLOOR)
  714. av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor));
  715. if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
  716. av_log(ctx, AV_LOG_INFO, "Noise floor count: %f\n", noise_floor_count / (double)s->nb_channels);
  717. if (s->measure_overall & MEASURE_BIT_DEPTH) {
  718. bit_depth(s, mask, imask, &depth);
  719. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  720. }
  721. if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
  722. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  723. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
  724. av_log(ctx, AV_LOG_INFO, "Number of NaNs: %f\n", nb_nans / (float)s->nb_channels);
  725. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
  726. av_log(ctx, AV_LOG_INFO, "Number of Infs: %f\n", nb_infs / (float)s->nb_channels);
  727. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
  728. av_log(ctx, AV_LOG_INFO, "Number of denormals: %f\n", nb_denormals / (float)s->nb_channels);
  729. }
  730. static av_cold void uninit(AVFilterContext *ctx)
  731. {
  732. AudioStatsContext *s = ctx->priv;
  733. if (s->nb_channels)
  734. print_stats(ctx);
  735. if (s->chstats) {
  736. for (int i = 0; i < s->nb_channels; i++) {
  737. ChannelStats *p = &s->chstats[i];
  738. av_freep(&p->win_samples);
  739. }
  740. }
  741. av_freep(&s->chstats);
  742. }
  743. static const AVFilterPad astats_inputs[] = {
  744. {
  745. .name = "default",
  746. .type = AVMEDIA_TYPE_AUDIO,
  747. .filter_frame = filter_frame,
  748. },
  749. { NULL }
  750. };
  751. static const AVFilterPad astats_outputs[] = {
  752. {
  753. .name = "default",
  754. .type = AVMEDIA_TYPE_AUDIO,
  755. .config_props = config_output,
  756. },
  757. { NULL }
  758. };
  759. AVFilter ff_af_astats = {
  760. .name = "astats",
  761. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  762. .query_formats = query_formats,
  763. .priv_size = sizeof(AudioStatsContext),
  764. .priv_class = &astats_class,
  765. .uninit = uninit,
  766. .inputs = astats_inputs,
  767. .outputs = astats_outputs,
  768. .flags = AVFILTER_FLAG_SLICE_THREADS,
  769. };