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.

661 lines
29KB

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