af_silenceremove.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2001 Heikki Leinonen
  3. * Copyright (c) 2001 Chris Bagwell
  4. * Copyright (c) 2003 Donnie Smith
  5. * Copyright (c) 2014 Paul B Mahol
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <float.h> /* DBL_MAX */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/timestamp.h"
  26. #include "audio.h"
  27. #include "formats.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. enum SilenceMode {
  31. SILENCE_TRIM,
  32. SILENCE_TRIM_FLUSH,
  33. SILENCE_COPY,
  34. SILENCE_COPY_FLUSH,
  35. SILENCE_STOP
  36. };
  37. typedef struct SilenceRemoveContext {
  38. const AVClass *class;
  39. enum SilenceMode mode;
  40. int start_periods;
  41. int64_t start_duration;
  42. double start_threshold;
  43. int stop_periods;
  44. int64_t stop_duration;
  45. double stop_threshold;
  46. double *start_holdoff;
  47. size_t start_holdoff_offset;
  48. size_t start_holdoff_end;
  49. int start_found_periods;
  50. double *stop_holdoff;
  51. size_t stop_holdoff_offset;
  52. size_t stop_holdoff_end;
  53. int stop_found_periods;
  54. double window_ratio;
  55. double *window;
  56. double *window_current;
  57. double *window_end;
  58. int window_size;
  59. double sum;
  60. int leave_silence;
  61. int restart;
  62. int64_t next_pts;
  63. int detection;
  64. void (*update)(struct SilenceRemoveContext *s, double sample);
  65. double(*compute)(struct SilenceRemoveContext *s, double sample);
  66. } SilenceRemoveContext;
  67. #define OFFSET(x) offsetof(SilenceRemoveContext, x)
  68. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  69. static const AVOption silenceremove_options[] = {
  70. { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, FLAGS },
  71. { "start_duration", NULL, OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
  72. { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
  73. { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, FLAGS },
  74. { "stop_duration", NULL, OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
  75. { "stop_threshold", NULL, OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
  76. { "leave_silence", NULL, OFFSET(leave_silence), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  77. { "detection", NULL, OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "detection" },
  78. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "detection" },
  79. { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "detection" },
  80. { "window", NULL, OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, FLAGS },
  81. { NULL }
  82. };
  83. AVFILTER_DEFINE_CLASS(silenceremove);
  84. static double compute_peak(SilenceRemoveContext *s, double sample)
  85. {
  86. double new_sum;
  87. new_sum = s->sum;
  88. new_sum -= *s->window_current;
  89. new_sum += fabs(sample);
  90. return new_sum / s->window_size;
  91. }
  92. static void update_peak(SilenceRemoveContext *s, double sample)
  93. {
  94. s->sum -= *s->window_current;
  95. *s->window_current = fabs(sample);
  96. s->sum += *s->window_current;
  97. s->window_current++;
  98. if (s->window_current >= s->window_end)
  99. s->window_current = s->window;
  100. }
  101. static double compute_rms(SilenceRemoveContext *s, double sample)
  102. {
  103. double new_sum;
  104. new_sum = s->sum;
  105. new_sum -= *s->window_current;
  106. new_sum += sample * sample;
  107. return sqrt(new_sum / s->window_size);
  108. }
  109. static void update_rms(SilenceRemoveContext *s, double sample)
  110. {
  111. s->sum -= *s->window_current;
  112. *s->window_current = sample * sample;
  113. s->sum += *s->window_current;
  114. s->window_current++;
  115. if (s->window_current >= s->window_end)
  116. s->window_current = s->window;
  117. }
  118. static av_cold int init(AVFilterContext *ctx)
  119. {
  120. SilenceRemoveContext *s = ctx->priv;
  121. if (s->stop_periods < 0) {
  122. s->stop_periods = -s->stop_periods;
  123. s->restart = 1;
  124. }
  125. switch (s->detection) {
  126. case 0:
  127. s->update = update_peak;
  128. s->compute = compute_peak;
  129. break;
  130. case 1:
  131. s->update = update_rms;
  132. s->compute = compute_rms;
  133. break;
  134. };
  135. return 0;
  136. }
  137. static void clear_window(SilenceRemoveContext *s)
  138. {
  139. memset(s->window, 0, s->window_size * sizeof(*s->window));
  140. s->window_current = s->window;
  141. s->window_end = s->window + s->window_size;
  142. s->sum = 0;
  143. }
  144. static int config_input(AVFilterLink *inlink)
  145. {
  146. AVFilterContext *ctx = inlink->dst;
  147. SilenceRemoveContext *s = ctx->priv;
  148. s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
  149. s->window = av_malloc_array(s->window_size, sizeof(*s->window));
  150. if (!s->window)
  151. return AVERROR(ENOMEM);
  152. clear_window(s);
  153. s->start_duration = av_rescale(s->start_duration, inlink->sample_rate,
  154. AV_TIME_BASE);
  155. if (s->start_duration < 0) {
  156. av_log(ctx, AV_LOG_WARNING, "start duration must be non-negative\n");
  157. s->start_duration = -s->start_duration;
  158. }
  159. s->stop_duration = av_rescale(s->stop_duration, inlink->sample_rate,
  160. AV_TIME_BASE);
  161. if (s->stop_duration < 0) {
  162. av_log(ctx, AV_LOG_WARNING, "stop duration must be non-negative\n");
  163. s->stop_duration = -s->stop_duration;
  164. }
  165. s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
  166. sizeof(*s->start_holdoff) *
  167. inlink->channels);
  168. if (!s->start_holdoff)
  169. return AVERROR(ENOMEM);
  170. s->start_holdoff_offset = 0;
  171. s->start_holdoff_end = 0;
  172. s->start_found_periods = 0;
  173. s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
  174. sizeof(*s->stop_holdoff) *
  175. inlink->channels);
  176. if (!s->stop_holdoff)
  177. return AVERROR(ENOMEM);
  178. s->stop_holdoff_offset = 0;
  179. s->stop_holdoff_end = 0;
  180. s->stop_found_periods = 0;
  181. if (s->start_periods)
  182. s->mode = SILENCE_TRIM;
  183. else
  184. s->mode = SILENCE_COPY;
  185. return 0;
  186. }
  187. static void flush(AVFrame *out, AVFilterLink *outlink,
  188. int *nb_samples_written, int *ret)
  189. {
  190. if (*nb_samples_written) {
  191. out->nb_samples = *nb_samples_written / outlink->channels;
  192. *ret = ff_filter_frame(outlink, out);
  193. *nb_samples_written = 0;
  194. } else {
  195. av_frame_free(&out);
  196. }
  197. }
  198. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  199. {
  200. AVFilterContext *ctx = inlink->dst;
  201. AVFilterLink *outlink = ctx->outputs[0];
  202. SilenceRemoveContext *s = ctx->priv;
  203. int i, j, threshold, ret = 0;
  204. int nbs, nb_samples_read, nb_samples_written;
  205. double *obuf, *ibuf = (double *)in->data[0];
  206. AVFrame *out;
  207. nb_samples_read = nb_samples_written = 0;
  208. switch (s->mode) {
  209. case SILENCE_TRIM:
  210. silence_trim:
  211. nbs = in->nb_samples - nb_samples_read / inlink->channels;
  212. if (!nbs)
  213. break;
  214. for (i = 0; i < nbs; i++) {
  215. threshold = 0;
  216. for (j = 0; j < inlink->channels; j++) {
  217. threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
  218. }
  219. if (threshold) {
  220. for (j = 0; j < inlink->channels; j++) {
  221. s->update(s, *ibuf);
  222. s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
  223. }
  224. nb_samples_read += inlink->channels;
  225. if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
  226. if (++s->start_found_periods >= s->start_periods) {
  227. s->mode = SILENCE_TRIM_FLUSH;
  228. goto silence_trim_flush;
  229. }
  230. s->start_holdoff_offset = 0;
  231. s->start_holdoff_end = 0;
  232. }
  233. } else {
  234. s->start_holdoff_end = 0;
  235. for (j = 0; j < inlink->channels; j++)
  236. s->update(s, ibuf[j]);
  237. ibuf += inlink->channels;
  238. nb_samples_read += inlink->channels;
  239. }
  240. }
  241. break;
  242. case SILENCE_TRIM_FLUSH:
  243. silence_trim_flush:
  244. nbs = s->start_holdoff_end - s->start_holdoff_offset;
  245. nbs -= nbs % inlink->channels;
  246. if (!nbs)
  247. break;
  248. out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
  249. if (!out) {
  250. av_frame_free(&in);
  251. return AVERROR(ENOMEM);
  252. }
  253. memcpy(out->data[0], &s->start_holdoff[s->start_holdoff_offset],
  254. nbs * sizeof(double));
  255. s->start_holdoff_offset += nbs;
  256. ret = ff_filter_frame(outlink, out);
  257. if (s->start_holdoff_offset == s->start_holdoff_end) {
  258. s->start_holdoff_offset = 0;
  259. s->start_holdoff_end = 0;
  260. s->mode = SILENCE_COPY;
  261. goto silence_copy;
  262. }
  263. break;
  264. case SILENCE_COPY:
  265. silence_copy:
  266. nbs = in->nb_samples - nb_samples_read / inlink->channels;
  267. if (!nbs)
  268. break;
  269. out = ff_get_audio_buffer(inlink, nbs);
  270. if (!out) {
  271. av_frame_free(&in);
  272. return AVERROR(ENOMEM);
  273. }
  274. obuf = (double *)out->data[0];
  275. if (s->stop_periods) {
  276. for (i = 0; i < nbs; i++) {
  277. threshold = 1;
  278. for (j = 0; j < inlink->channels; j++)
  279. threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
  280. if (threshold && s->stop_holdoff_end && !s->leave_silence) {
  281. s->mode = SILENCE_COPY_FLUSH;
  282. flush(out, outlink, &nb_samples_written, &ret);
  283. goto silence_copy_flush;
  284. } else if (threshold) {
  285. for (j = 0; j < inlink->channels; j++) {
  286. s->update(s, *ibuf);
  287. *obuf++ = *ibuf++;
  288. }
  289. nb_samples_read += inlink->channels;
  290. nb_samples_written += inlink->channels;
  291. } else if (!threshold) {
  292. for (j = 0; j < inlink->channels; j++) {
  293. s->update(s, *ibuf);
  294. if (s->leave_silence) {
  295. *obuf++ = *ibuf;
  296. nb_samples_written++;
  297. }
  298. s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
  299. }
  300. nb_samples_read += inlink->channels;
  301. if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
  302. if (++s->stop_found_periods >= s->stop_periods) {
  303. s->stop_holdoff_offset = 0;
  304. s->stop_holdoff_end = 0;
  305. if (!s->restart) {
  306. s->mode = SILENCE_STOP;
  307. flush(out, outlink, &nb_samples_written, &ret);
  308. goto silence_stop;
  309. } else {
  310. s->stop_found_periods = 0;
  311. s->start_found_periods = 0;
  312. s->start_holdoff_offset = 0;
  313. s->start_holdoff_end = 0;
  314. clear_window(s);
  315. s->mode = SILENCE_TRIM;
  316. flush(out, outlink, &nb_samples_written, &ret);
  317. goto silence_trim;
  318. }
  319. }
  320. s->mode = SILENCE_COPY_FLUSH;
  321. flush(out, outlink, &nb_samples_written, &ret);
  322. goto silence_copy_flush;
  323. }
  324. }
  325. }
  326. flush(out, outlink, &nb_samples_written, &ret);
  327. } else {
  328. memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
  329. ret = ff_filter_frame(outlink, out);
  330. }
  331. break;
  332. case SILENCE_COPY_FLUSH:
  333. silence_copy_flush:
  334. nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
  335. nbs -= nbs % inlink->channels;
  336. if (!nbs)
  337. break;
  338. out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
  339. if (!out) {
  340. av_frame_free(&in);
  341. return AVERROR(ENOMEM);
  342. }
  343. memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
  344. nbs * sizeof(double));
  345. s->stop_holdoff_offset += nbs;
  346. ret = ff_filter_frame(outlink, out);
  347. if (s->stop_holdoff_offset == s->stop_holdoff_end) {
  348. s->stop_holdoff_offset = 0;
  349. s->stop_holdoff_end = 0;
  350. s->mode = SILENCE_COPY;
  351. goto silence_copy;
  352. }
  353. break;
  354. case SILENCE_STOP:
  355. silence_stop:
  356. break;
  357. }
  358. av_frame_free(&in);
  359. return ret;
  360. }
  361. static int request_frame(AVFilterLink *outlink)
  362. {
  363. AVFilterContext *ctx = outlink->src;
  364. SilenceRemoveContext *s = ctx->priv;
  365. int ret;
  366. ret = ff_request_frame(ctx->inputs[0]);
  367. if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
  368. s->mode == SILENCE_COPY)) {
  369. int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
  370. if (nbs) {
  371. AVFrame *frame;
  372. frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
  373. if (!frame)
  374. return AVERROR(ENOMEM);
  375. memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
  376. nbs * sizeof(double));
  377. ret = ff_filter_frame(ctx->inputs[0], frame);
  378. }
  379. s->mode = SILENCE_STOP;
  380. }
  381. return ret;
  382. }
  383. static int query_formats(AVFilterContext *ctx)
  384. {
  385. AVFilterFormats *formats = NULL;
  386. AVFilterChannelLayouts *layouts = NULL;
  387. static const enum AVSampleFormat sample_fmts[] = {
  388. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
  389. };
  390. int ret;
  391. layouts = ff_all_channel_counts();
  392. if (!layouts)
  393. return AVERROR(ENOMEM);
  394. ret = ff_set_common_channel_layouts(ctx, layouts);
  395. if (ret < 0)
  396. return ret;
  397. formats = ff_make_format_list(sample_fmts);
  398. if (!formats)
  399. return AVERROR(ENOMEM);
  400. ret = ff_set_common_formats(ctx, formats);
  401. if (ret < 0)
  402. return ret;
  403. formats = ff_all_samplerates();
  404. if (!formats)
  405. return AVERROR(ENOMEM);
  406. return ff_set_common_samplerates(ctx, formats);
  407. }
  408. static av_cold void uninit(AVFilterContext *ctx)
  409. {
  410. SilenceRemoveContext *s = ctx->priv;
  411. av_freep(&s->start_holdoff);
  412. av_freep(&s->stop_holdoff);
  413. av_freep(&s->window);
  414. }
  415. static const AVFilterPad silenceremove_inputs[] = {
  416. {
  417. .name = "default",
  418. .type = AVMEDIA_TYPE_AUDIO,
  419. .config_props = config_input,
  420. .filter_frame = filter_frame,
  421. },
  422. { NULL }
  423. };
  424. static const AVFilterPad silenceremove_outputs[] = {
  425. {
  426. .name = "default",
  427. .type = AVMEDIA_TYPE_AUDIO,
  428. .request_frame = request_frame,
  429. },
  430. { NULL }
  431. };
  432. AVFilter ff_af_silenceremove = {
  433. .name = "silenceremove",
  434. .description = NULL_IF_CONFIG_SMALL("Remove silence."),
  435. .priv_size = sizeof(SilenceRemoveContext),
  436. .priv_class = &silenceremove_class,
  437. .init = init,
  438. .uninit = uninit,
  439. .query_formats = query_formats,
  440. .inputs = silenceremove_inputs,
  441. .outputs = silenceremove_outputs,
  442. };