af_astats.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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_MAX;
  161. p->min_non_zero = DBL_MAX;
  162. p->min_diff = DBL_MAX;
  163. p->max_diff = 0;
  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. p->last = NAN;
  183. }
  184. }
  185. static int config_output(AVFilterLink *outlink)
  186. {
  187. AudioStatsContext *s = outlink->src->priv;
  188. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  189. if (!s->chstats)
  190. return AVERROR(ENOMEM);
  191. s->nb_channels = outlink->channels;
  192. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  193. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  194. s->nb_frames = 0;
  195. s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
  196. s->is_double = outlink->format == AV_SAMPLE_FMT_DBL ||
  197. outlink->format == AV_SAMPLE_FMT_DBLP;
  198. s->is_float = outlink->format == AV_SAMPLE_FMT_FLT ||
  199. outlink->format == AV_SAMPLE_FMT_FLTP;
  200. reset_stats(s);
  201. return 0;
  202. }
  203. static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
  204. {
  205. unsigned result = s->maxbitdepth;
  206. mask = mask & (~imask);
  207. for (; result && !(mask & 1); --result, mask >>= 1);
  208. depth->den = result;
  209. depth->num = 0;
  210. for (; result; --result, mask >>= 1)
  211. if (mask & 1)
  212. depth->num++;
  213. }
  214. static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
  215. {
  216. if (d < p->min)
  217. p->min = d;
  218. if (d > p->max)
  219. p->max = d;
  220. }
  221. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
  222. {
  223. if (d < p->min) {
  224. p->min = d;
  225. p->nmin = nd;
  226. p->min_run = 1;
  227. p->min_runs = 0;
  228. p->min_count = 1;
  229. } else if (d == p->min) {
  230. p->min_count++;
  231. p->min_run = d == p->last ? p->min_run + 1 : 1;
  232. } else if (p->last == p->min) {
  233. p->min_runs += p->min_run * p->min_run;
  234. }
  235. if (d != 0 && FFABS(d) < p->min_non_zero)
  236. p->min_non_zero = FFABS(d);
  237. if (d > p->max) {
  238. p->max = d;
  239. p->nmax = nd;
  240. p->max_run = 1;
  241. p->max_runs = 0;
  242. p->max_count = 1;
  243. } else if (d == p->max) {
  244. p->max_count++;
  245. p->max_run = d == p->last ? p->max_run + 1 : 1;
  246. } else if (p->last == p->max) {
  247. p->max_runs += p->max_run * p->max_run;
  248. }
  249. if (d != 0) {
  250. p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
  251. p->last_non_zero = d;
  252. }
  253. p->sigma_x += nd;
  254. p->sigma_x2 += nd * nd;
  255. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
  256. if (!isnan(p->last)) {
  257. p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
  258. p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
  259. p->diff1_sum += fabs(d - p->last);
  260. p->diff1_sum_x2 += (d - p->last) * (d - p->last);
  261. }
  262. p->last = d;
  263. p->mask |= i;
  264. p->imask &= i;
  265. if (p->nb_samples >= s->tc_samples) {
  266. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  267. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  268. }
  269. p->nb_samples++;
  270. }
  271. static inline void update_float_stat(AudioStatsContext *s, ChannelStats *p, float d)
  272. {
  273. int type = fpclassify(d);
  274. p->nb_nans += type == FP_NAN;
  275. p->nb_infs += type == FP_INFINITE;
  276. p->nb_denormals += type == FP_SUBNORMAL;
  277. }
  278. static inline void update_double_stat(AudioStatsContext *s, ChannelStats *p, double d)
  279. {
  280. int type = fpclassify(d);
  281. p->nb_nans += type == FP_NAN;
  282. p->nb_infs += type == FP_INFINITE;
  283. p->nb_denormals += type == FP_SUBNORMAL;
  284. }
  285. static void set_meta(AVDictionary **metadata, int chan, const char *key,
  286. const char *fmt, double val)
  287. {
  288. uint8_t value[128];
  289. uint8_t key2[128];
  290. snprintf(value, sizeof(value), fmt, val);
  291. if (chan)
  292. snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
  293. else
  294. snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
  295. av_dict_set(metadata, key2, value, 0);
  296. }
  297. #define LINEAR_TO_DB(x) (log10(x) * 20)
  298. static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
  299. {
  300. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
  301. uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
  302. double min_runs = 0, max_runs = 0,
  303. min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
  304. nmin = DBL_MAX, nmax =-DBL_MAX,
  305. max_sigma_x = 0,
  306. diff1_sum = 0,
  307. diff1_sum_x2 = 0,
  308. sigma_x = 0,
  309. sigma_x2 = 0,
  310. min_sigma_x2 = DBL_MAX,
  311. max_sigma_x2 =-DBL_MAX;
  312. AVRational depth;
  313. int c;
  314. for (c = 0; c < s->nb_channels; c++) {
  315. ChannelStats *p = &s->chstats[c];
  316. if (p->nb_samples < s->tc_samples)
  317. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  318. min = FFMIN(min, p->min);
  319. max = FFMAX(max, p->max);
  320. nmin = FFMIN(nmin, p->nmin);
  321. nmax = FFMAX(nmax, p->nmax);
  322. min_diff = FFMIN(min_diff, p->min_diff);
  323. max_diff = FFMAX(max_diff, p->max_diff);
  324. diff1_sum += p->diff1_sum;
  325. diff1_sum_x2 += p->diff1_sum_x2;
  326. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  327. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  328. sigma_x += p->sigma_x;
  329. sigma_x2 += p->sigma_x2;
  330. min_count += p->min_count;
  331. max_count += p->max_count;
  332. min_runs += p->min_runs;
  333. max_runs += p->max_runs;
  334. mask |= p->mask;
  335. imask &= p->imask;
  336. nb_samples += p->nb_samples;
  337. nb_nans += p->nb_nans;
  338. nb_infs += p->nb_infs;
  339. nb_denormals += p->nb_denormals;
  340. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  341. max_sigma_x = p->sigma_x;
  342. if (s->measure_perchannel & MEASURE_DC_OFFSET)
  343. set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
  344. if (s->measure_perchannel & MEASURE_MIN_LEVEL)
  345. set_meta(metadata, c + 1, "Min_level", "%f", p->min);
  346. if (s->measure_perchannel & MEASURE_MAX_LEVEL)
  347. set_meta(metadata, c + 1, "Max_level", "%f", p->max);
  348. if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
  349. set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
  350. if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
  351. set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
  352. if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
  353. set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
  354. if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
  355. set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
  356. if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
  357. set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
  358. if (s->measure_perchannel & MEASURE_RMS_LEVEL)
  359. set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  360. if (s->measure_perchannel & MEASURE_RMS_PEAK)
  361. set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  362. if (s->measure_perchannel & MEASURE_RMS_TROUGH)
  363. set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  364. if (s->measure_perchannel & MEASURE_CREST_FACTOR)
  365. set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  366. if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
  367. set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
  368. if (s->measure_perchannel & MEASURE_PEAK_COUNT)
  369. set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
  370. if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
  371. bit_depth(s, p->mask, p->imask, &depth);
  372. set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
  373. set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
  374. }
  375. if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
  376. set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
  377. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
  378. set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
  379. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
  380. set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
  381. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
  382. set_meta(metadata, c + 1, "Number of NaNs", "%f", p->nb_nans);
  383. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
  384. set_meta(metadata, c + 1, "Number of Infs", "%f", p->nb_infs);
  385. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
  386. set_meta(metadata, c + 1, "Number of denormals", "%f", p->nb_denormals);
  387. }
  388. if (s->measure_overall & MEASURE_DC_OFFSET)
  389. set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
  390. if (s->measure_overall & MEASURE_MIN_LEVEL)
  391. set_meta(metadata, 0, "Overall.Min_level", "%f", min);
  392. if (s->measure_overall & MEASURE_MAX_LEVEL)
  393. set_meta(metadata, 0, "Overall.Max_level", "%f", max);
  394. if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
  395. set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
  396. if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
  397. set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
  398. if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
  399. set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
  400. if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
  401. set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
  402. if (s->measure_overall & MEASURE_PEAK_LEVEL)
  403. set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
  404. if (s->measure_overall & MEASURE_RMS_LEVEL)
  405. set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  406. if (s->measure_overall & MEASURE_RMS_PEAK)
  407. set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  408. if (s->measure_overall & MEASURE_RMS_TROUGH)
  409. set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  410. if (s->measure_overall & MEASURE_FLAT_FACTOR)
  411. set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  412. if (s->measure_overall & MEASURE_PEAK_COUNT)
  413. set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
  414. if (s->measure_overall & MEASURE_BIT_DEPTH) {
  415. bit_depth(s, mask, imask, &depth);
  416. set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
  417. set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
  418. }
  419. if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
  420. set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
  421. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
  422. set_meta(metadata, 0, "Number of NaNs", "%f", nb_nans / (float)s->nb_channels);
  423. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
  424. set_meta(metadata, 0, "Number of Infs", "%f", nb_infs / (float)s->nb_channels);
  425. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
  426. set_meta(metadata, 0, "Number of denormals", "%f", nb_denormals / (float)s->nb_channels);
  427. }
  428. #define UPDATE_STATS_P(type, update_func, update_float, channel_func) \
  429. for (int c = 0; c < channels; c++) { \
  430. ChannelStats *p = &s->chstats[c]; \
  431. const type *src = (const type *)data[c]; \
  432. const type * const srcend = src + samples; \
  433. for (; src < srcend; src++) { \
  434. update_func; \
  435. update_float; \
  436. } \
  437. channel_func; \
  438. }
  439. #define UPDATE_STATS_I(type, update_func, update_float, channel_func) \
  440. for (int c = 0; c < channels; c++) { \
  441. ChannelStats *p = &s->chstats[c]; \
  442. const type *src = (const type *)data[0]; \
  443. const type * const srcend = src + samples * channels; \
  444. for (src += c; src < srcend; src += channels) { \
  445. update_func; \
  446. update_float; \
  447. } \
  448. channel_func; \
  449. }
  450. #define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
  451. if ((s->measure_overall | s->measure_perchannel) & ~MEASURE_MINMAXPEAK) { \
  452. 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, ); \
  453. } else { \
  454. UPDATE_STATS_##planar(type, update_minmax(s, p, sample), , p->nmin = p->min normalizer_suffix; p->nmax = p->max normalizer_suffix;); \
  455. }
  456. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  457. {
  458. AudioStatsContext *s = inlink->dst->priv;
  459. AVDictionary **metadata = &buf->metadata;
  460. const int channels = s->nb_channels;
  461. const int samples = buf->nb_samples;
  462. const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
  463. if (s->reset_count > 0) {
  464. if (s->nb_frames >= s->reset_count) {
  465. reset_stats(s);
  466. s->nb_frames = 0;
  467. }
  468. s->nb_frames++;
  469. }
  470. switch (inlink->format) {
  471. case AV_SAMPLE_FMT_DBLP:
  472. UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
  473. break;
  474. case AV_SAMPLE_FMT_DBL:
  475. UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
  476. break;
  477. case AV_SAMPLE_FMT_FLTP:
  478. UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
  479. break;
  480. case AV_SAMPLE_FMT_FLT:
  481. UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
  482. break;
  483. case AV_SAMPLE_FMT_S64P:
  484. UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
  485. break;
  486. case AV_SAMPLE_FMT_S64:
  487. UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
  488. break;
  489. case AV_SAMPLE_FMT_S32P:
  490. UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
  491. break;
  492. case AV_SAMPLE_FMT_S32:
  493. UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
  494. break;
  495. case AV_SAMPLE_FMT_S16P:
  496. UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
  497. break;
  498. case AV_SAMPLE_FMT_S16:
  499. UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
  500. break;
  501. }
  502. if (s->metadata)
  503. set_metadata(s, metadata);
  504. return ff_filter_frame(inlink->dst->outputs[0], buf);
  505. }
  506. static void print_stats(AVFilterContext *ctx)
  507. {
  508. AudioStatsContext *s = ctx->priv;
  509. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
  510. uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
  511. double min_runs = 0, max_runs = 0,
  512. min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
  513. nmin = DBL_MAX, nmax =-DBL_MAX,
  514. max_sigma_x = 0,
  515. diff1_sum_x2 = 0,
  516. diff1_sum = 0,
  517. sigma_x = 0,
  518. sigma_x2 = 0,
  519. min_sigma_x2 = DBL_MAX,
  520. max_sigma_x2 =-DBL_MAX;
  521. AVRational depth;
  522. int c;
  523. for (c = 0; c < s->nb_channels; c++) {
  524. ChannelStats *p = &s->chstats[c];
  525. if (p->nb_samples < s->tc_samples)
  526. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  527. min = FFMIN(min, p->min);
  528. max = FFMAX(max, p->max);
  529. nmin = FFMIN(nmin, p->nmin);
  530. nmax = FFMAX(nmax, p->nmax);
  531. min_diff = FFMIN(min_diff, p->min_diff);
  532. max_diff = FFMAX(max_diff, p->max_diff);
  533. diff1_sum_x2 += p->diff1_sum_x2;
  534. diff1_sum += p->diff1_sum;
  535. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  536. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  537. sigma_x += p->sigma_x;
  538. sigma_x2 += p->sigma_x2;
  539. min_count += p->min_count;
  540. max_count += p->max_count;
  541. min_runs += p->min_runs;
  542. max_runs += p->max_runs;
  543. mask |= p->mask;
  544. imask &= p->imask;
  545. nb_samples += p->nb_samples;
  546. nb_nans += p->nb_nans;
  547. nb_infs += p->nb_infs;
  548. nb_denormals += p->nb_denormals;
  549. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  550. max_sigma_x = p->sigma_x;
  551. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  552. if (s->measure_perchannel & MEASURE_DC_OFFSET)
  553. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  554. if (s->measure_perchannel & MEASURE_MIN_LEVEL)
  555. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  556. if (s->measure_perchannel & MEASURE_MAX_LEVEL)
  557. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  558. if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
  559. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
  560. if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
  561. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
  562. if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
  563. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
  564. if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
  565. av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
  566. if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
  567. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
  568. if (s->measure_perchannel & MEASURE_RMS_LEVEL)
  569. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  570. if (s->measure_perchannel & MEASURE_RMS_PEAK)
  571. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  572. if (s->measure_perchannel & MEASURE_RMS_TROUGH)
  573. if (p->min_sigma_x2 != 1)
  574. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  575. if (s->measure_perchannel & MEASURE_CREST_FACTOR)
  576. 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);
  577. if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
  578. 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)));
  579. if (s->measure_perchannel & MEASURE_PEAK_COUNT)
  580. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  581. if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
  582. bit_depth(s, p->mask, p->imask, &depth);
  583. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  584. }
  585. if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
  586. 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));
  587. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
  588. av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
  589. if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
  590. av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
  591. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
  592. av_log(ctx, AV_LOG_INFO, "Number of NaNs: %"PRId64"\n", p->nb_nans);
  593. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
  594. av_log(ctx, AV_LOG_INFO, "Number of Infs: %"PRId64"\n", p->nb_infs);
  595. if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
  596. av_log(ctx, AV_LOG_INFO, "Number of denormals: %"PRId64"\n", p->nb_denormals);
  597. }
  598. av_log(ctx, AV_LOG_INFO, "Overall\n");
  599. if (s->measure_overall & MEASURE_DC_OFFSET)
  600. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  601. if (s->measure_overall & MEASURE_MIN_LEVEL)
  602. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  603. if (s->measure_overall & MEASURE_MAX_LEVEL)
  604. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  605. if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
  606. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
  607. if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
  608. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
  609. if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
  610. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
  611. if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
  612. av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
  613. if (s->measure_overall & MEASURE_PEAK_LEVEL)
  614. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
  615. if (s->measure_overall & MEASURE_RMS_LEVEL)
  616. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  617. if (s->measure_overall & MEASURE_RMS_PEAK)
  618. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  619. if (s->measure_overall & MEASURE_RMS_TROUGH)
  620. if (min_sigma_x2 != 1)
  621. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  622. if (s->measure_overall & MEASURE_FLAT_FACTOR)
  623. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  624. if (s->measure_overall & MEASURE_PEAK_COUNT)
  625. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  626. if (s->measure_overall & MEASURE_BIT_DEPTH) {
  627. bit_depth(s, mask, imask, &depth);
  628. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  629. }
  630. if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
  631. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  632. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
  633. av_log(ctx, AV_LOG_INFO, "Number of NaNs: %f\n", nb_nans / (float)s->nb_channels);
  634. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
  635. av_log(ctx, AV_LOG_INFO, "Number of Infs: %f\n", nb_infs / (float)s->nb_channels);
  636. if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
  637. av_log(ctx, AV_LOG_INFO, "Number of denormals: %f\n", nb_denormals / (float)s->nb_channels);
  638. }
  639. static av_cold void uninit(AVFilterContext *ctx)
  640. {
  641. AudioStatsContext *s = ctx->priv;
  642. if (s->nb_channels)
  643. print_stats(ctx);
  644. av_freep(&s->chstats);
  645. }
  646. static const AVFilterPad astats_inputs[] = {
  647. {
  648. .name = "default",
  649. .type = AVMEDIA_TYPE_AUDIO,
  650. .filter_frame = filter_frame,
  651. },
  652. { NULL }
  653. };
  654. static const AVFilterPad astats_outputs[] = {
  655. {
  656. .name = "default",
  657. .type = AVMEDIA_TYPE_AUDIO,
  658. .config_props = config_output,
  659. },
  660. { NULL }
  661. };
  662. AVFilter ff_af_astats = {
  663. .name = "astats",
  664. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  665. .query_formats = query_formats,
  666. .priv_size = sizeof(AudioStatsContext),
  667. .priv_class = &astats_class,
  668. .uninit = uninit,
  669. .inputs = astats_inputs,
  670. .outputs = astats_outputs,
  671. };