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.

735 lines
33KB

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