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.

831 lines
36KB

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