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.

645 lines
28KB

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