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.

450 lines
16KB

  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. typedef struct ChannelStats {
  27. double last;
  28. double sigma_x, sigma_x2;
  29. double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
  30. double min, max;
  31. double min_run, max_run;
  32. double min_runs, max_runs;
  33. double min_diff, max_diff;
  34. double diff1_sum;
  35. uint64_t mask, imask;
  36. uint64_t min_count, max_count;
  37. uint64_t nb_samples;
  38. } ChannelStats;
  39. typedef struct {
  40. const AVClass *class;
  41. ChannelStats *chstats;
  42. int nb_channels;
  43. uint64_t tc_samples;
  44. double time_constant;
  45. double mult;
  46. int metadata;
  47. int reset_count;
  48. int nb_frames;
  49. } AudioStatsContext;
  50. #define OFFSET(x) offsetof(AudioStatsContext, x)
  51. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  52. static const AVOption astats_options[] = {
  53. { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
  54. { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  55. { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(astats);
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. AVFilterFormats *formats;
  62. AVFilterChannelLayouts *layouts;
  63. static const enum AVSampleFormat sample_fmts[] = {
  64. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  65. AV_SAMPLE_FMT_NONE
  66. };
  67. int ret;
  68. layouts = ff_all_channel_counts();
  69. if (!layouts)
  70. return AVERROR(ENOMEM);
  71. ret = ff_set_common_channel_layouts(ctx, layouts);
  72. if (ret < 0)
  73. return ret;
  74. formats = ff_make_format_list(sample_fmts);
  75. if (!formats)
  76. return AVERROR(ENOMEM);
  77. ret = ff_set_common_formats(ctx, formats);
  78. if (ret < 0)
  79. return ret;
  80. formats = ff_all_samplerates();
  81. if (!formats)
  82. return AVERROR(ENOMEM);
  83. return ff_set_common_samplerates(ctx, formats);
  84. }
  85. static void reset_stats(AudioStatsContext *s)
  86. {
  87. int c;
  88. for (c = 0; c < s->nb_channels; c++) {
  89. ChannelStats *p = &s->chstats[c];
  90. p->min = p->min_sigma_x2 = DBL_MAX;
  91. p->max = p->max_sigma_x2 = DBL_MIN;
  92. p->min_diff = DBL_MAX;
  93. p->max_diff = DBL_MIN;
  94. p->sigma_x = 0;
  95. p->sigma_x2 = 0;
  96. p->avg_sigma_x2 = 0;
  97. p->min_sigma_x2 = 0;
  98. p->max_sigma_x2 = 0;
  99. p->min_run = 0;
  100. p->max_run = 0;
  101. p->min_runs = 0;
  102. p->max_runs = 0;
  103. p->diff1_sum = 0;
  104. p->mask = 0;
  105. p->imask = 0xFFFFFFFFFFFFFFFF;
  106. p->min_count = 0;
  107. p->max_count = 0;
  108. p->nb_samples = 0;
  109. }
  110. }
  111. static int config_output(AVFilterLink *outlink)
  112. {
  113. AudioStatsContext *s = outlink->src->priv;
  114. s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
  115. if (!s->chstats)
  116. return AVERROR(ENOMEM);
  117. s->nb_channels = outlink->channels;
  118. s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
  119. s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
  120. s->nb_frames = 0;
  121. reset_stats(s);
  122. return 0;
  123. }
  124. static void bit_depth(uint64_t mask, uint64_t imask, AVRational *depth)
  125. {
  126. unsigned result = 64;
  127. mask = mask & (~imask);
  128. for (; result && !(mask & 1); --result, mask >>= 1);
  129. depth->den = result;
  130. depth->num = 0;
  131. for (; result; --result, mask >>= 1)
  132. if (mask & 1)
  133. depth->num++;
  134. }
  135. static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
  136. {
  137. if (d < p->min) {
  138. p->min = d;
  139. p->min_run = 1;
  140. p->min_runs = 0;
  141. p->min_count = 1;
  142. } else if (d == p->min) {
  143. p->min_count++;
  144. p->min_run = d == p->last ? p->min_run + 1 : 1;
  145. } else if (p->last == p->min) {
  146. p->min_runs += p->min_run * p->min_run;
  147. }
  148. if (d > p->max) {
  149. p->max = d;
  150. p->max_run = 1;
  151. p->max_runs = 0;
  152. p->max_count = 1;
  153. } else if (d == p->max) {
  154. p->max_count++;
  155. p->max_run = d == p->last ? p->max_run + 1 : 1;
  156. } else if (p->last == p->max) {
  157. p->max_runs += p->max_run * p->max_run;
  158. }
  159. p->sigma_x += d;
  160. p->sigma_x2 += d * d;
  161. p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
  162. p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
  163. p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
  164. p->diff1_sum += fabs(d - p->last);
  165. p->last = d;
  166. p->mask |= llrint(d * (UINT64_C(1) << 63));
  167. p->imask &= llrint(d * (UINT64_C(1) << 63));
  168. if (p->nb_samples >= s->tc_samples) {
  169. p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
  170. p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
  171. }
  172. p->nb_samples++;
  173. }
  174. static void set_meta(AVDictionary **metadata, int chan, const char *key,
  175. const char *fmt, double val)
  176. {
  177. uint8_t value[128];
  178. uint8_t key2[128];
  179. snprintf(value, sizeof(value), fmt, val);
  180. if (chan)
  181. snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
  182. else
  183. snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
  184. av_dict_set(metadata, key2, value, 0);
  185. }
  186. #define LINEAR_TO_DB(x) (log10(x) * 20)
  187. static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
  188. {
  189. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
  190. double min_runs = 0, max_runs = 0,
  191. min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
  192. max_sigma_x = 0,
  193. diff1_sum = 0,
  194. sigma_x = 0,
  195. sigma_x2 = 0,
  196. min_sigma_x2 = DBL_MAX,
  197. max_sigma_x2 = DBL_MIN;
  198. AVRational depth;
  199. int c;
  200. for (c = 0; c < s->nb_channels; c++) {
  201. ChannelStats *p = &s->chstats[c];
  202. if (p->nb_samples < s->tc_samples)
  203. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  204. min = FFMIN(min, p->min);
  205. max = FFMAX(max, p->max);
  206. min_diff = FFMIN(min_diff, p->min_diff);
  207. max_diff = FFMAX(max_diff, p->max_diff);
  208. diff1_sum += p->diff1_sum,
  209. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  210. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  211. sigma_x += p->sigma_x;
  212. sigma_x2 += p->sigma_x2;
  213. min_count += p->min_count;
  214. max_count += p->max_count;
  215. min_runs += p->min_runs;
  216. max_runs += p->max_runs;
  217. mask |= p->mask;
  218. imask &= p->imask;
  219. nb_samples += p->nb_samples;
  220. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  221. max_sigma_x = p->sigma_x;
  222. set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
  223. set_meta(metadata, c + 1, "Min_level", "%f", p->min);
  224. set_meta(metadata, c + 1, "Max_level", "%f", p->max);
  225. set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
  226. set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
  227. set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
  228. set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  229. set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  230. set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  231. set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  232. set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  233. set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
  234. set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
  235. bit_depth(p->mask, p->imask, &depth);
  236. set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
  237. set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
  238. }
  239. set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
  240. set_meta(metadata, 0, "Overall.Min_level", "%f", min);
  241. set_meta(metadata, 0, "Overall.Max_level", "%f", max);
  242. set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
  243. set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
  244. set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
  245. set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-min, max)));
  246. set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  247. set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  248. set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  249. set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  250. set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
  251. bit_depth(mask, imask, &depth);
  252. set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
  253. set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
  254. set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
  255. }
  256. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  257. {
  258. AudioStatsContext *s = inlink->dst->priv;
  259. AVDictionary **metadata = avpriv_frame_get_metadatap(buf);
  260. const int channels = s->nb_channels;
  261. const double *src;
  262. int i, c;
  263. if (s->reset_count > 0) {
  264. if (s->nb_frames >= s->reset_count) {
  265. reset_stats(s);
  266. s->nb_frames = 0;
  267. }
  268. s->nb_frames++;
  269. }
  270. switch (inlink->format) {
  271. case AV_SAMPLE_FMT_DBLP:
  272. for (c = 0; c < channels; c++) {
  273. ChannelStats *p = &s->chstats[c];
  274. src = (const double *)buf->extended_data[c];
  275. for (i = 0; i < buf->nb_samples; i++, src++)
  276. update_stat(s, p, *src);
  277. }
  278. break;
  279. case AV_SAMPLE_FMT_DBL:
  280. src = (const double *)buf->extended_data[0];
  281. for (i = 0; i < buf->nb_samples; i++) {
  282. for (c = 0; c < channels; c++, src++)
  283. update_stat(s, &s->chstats[c], *src);
  284. }
  285. break;
  286. }
  287. if (s->metadata)
  288. set_metadata(s, metadata);
  289. return ff_filter_frame(inlink->dst->outputs[0], buf);
  290. }
  291. static void print_stats(AVFilterContext *ctx)
  292. {
  293. AudioStatsContext *s = ctx->priv;
  294. uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
  295. double min_runs = 0, max_runs = 0,
  296. min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
  297. max_sigma_x = 0,
  298. diff1_sum = 0,
  299. sigma_x = 0,
  300. sigma_x2 = 0,
  301. min_sigma_x2 = DBL_MAX,
  302. max_sigma_x2 = DBL_MIN;
  303. AVRational depth;
  304. int c;
  305. for (c = 0; c < s->nb_channels; c++) {
  306. ChannelStats *p = &s->chstats[c];
  307. if (p->nb_samples < s->tc_samples)
  308. p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
  309. min = FFMIN(min, p->min);
  310. max = FFMAX(max, p->max);
  311. min_diff = FFMIN(min_diff, p->min_diff);
  312. max_diff = FFMAX(max_diff, p->max_diff);
  313. diff1_sum += p->diff1_sum,
  314. min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
  315. max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
  316. sigma_x += p->sigma_x;
  317. sigma_x2 += p->sigma_x2;
  318. min_count += p->min_count;
  319. max_count += p->max_count;
  320. min_runs += p->min_runs;
  321. max_runs += p->max_runs;
  322. mask |= p->mask;
  323. imask &= p->imask;
  324. nb_samples += p->nb_samples;
  325. if (fabs(p->sigma_x) > fabs(max_sigma_x))
  326. max_sigma_x = p->sigma_x;
  327. av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
  328. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
  329. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
  330. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
  331. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
  332. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
  333. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
  334. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
  335. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
  336. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
  337. if (p->min_sigma_x2 != 1)
  338. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
  339. av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
  340. 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)));
  341. av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
  342. bit_depth(p->mask, p->imask, &depth);
  343. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  344. }
  345. av_log(ctx, AV_LOG_INFO, "Overall\n");
  346. av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
  347. av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
  348. av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
  349. av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
  350. av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
  351. av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
  352. av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
  353. av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
  354. av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
  355. if (min_sigma_x2 != 1)
  356. av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
  357. av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
  358. av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
  359. bit_depth(mask, imask, &depth);
  360. av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
  361. av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
  362. }
  363. static av_cold void uninit(AVFilterContext *ctx)
  364. {
  365. AudioStatsContext *s = ctx->priv;
  366. if (s->nb_channels)
  367. print_stats(ctx);
  368. av_freep(&s->chstats);
  369. }
  370. static const AVFilterPad astats_inputs[] = {
  371. {
  372. .name = "default",
  373. .type = AVMEDIA_TYPE_AUDIO,
  374. .filter_frame = filter_frame,
  375. },
  376. { NULL }
  377. };
  378. static const AVFilterPad astats_outputs[] = {
  379. {
  380. .name = "default",
  381. .type = AVMEDIA_TYPE_AUDIO,
  382. .config_props = config_output,
  383. },
  384. { NULL }
  385. };
  386. AVFilter ff_af_astats = {
  387. .name = "astats",
  388. .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
  389. .query_formats = query_formats,
  390. .priv_size = sizeof(AudioStatsContext),
  391. .priv_class = &astats_class,
  392. .uninit = uninit,
  393. .inputs = astats_inputs,
  394. .outputs = astats_outputs,
  395. };