af_silenceremove.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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;
  55. double *window_current;
  56. double *window_end;
  57. int window_size;
  58. double rms_sum;
  59. int leave_silence;
  60. int restart;
  61. int64_t next_pts;
  62. } SilenceRemoveContext;
  63. #define OFFSET(x) offsetof(SilenceRemoveContext, x)
  64. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  65. static const AVOption silenceremove_options[] = {
  66. { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, FLAGS },
  67. { "start_duration", NULL, OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
  68. { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
  69. { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, FLAGS },
  70. { "stop_duration", NULL, OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
  71. { "stop_threshold", NULL, OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
  72. { "leave_silence", NULL, OFFSET(leave_silence), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  73. { NULL }
  74. };
  75. AVFILTER_DEFINE_CLASS(silenceremove);
  76. static av_cold int init(AVFilterContext *ctx)
  77. {
  78. SilenceRemoveContext *s = ctx->priv;
  79. if (s->stop_periods < 0) {
  80. s->stop_periods = -s->stop_periods;
  81. s->restart = 1;
  82. }
  83. return 0;
  84. }
  85. static void clear_rms(SilenceRemoveContext *s)
  86. {
  87. memset(s->window, 0, s->window_size * sizeof(*s->window));
  88. s->window_current = s->window;
  89. s->window_end = s->window + s->window_size;
  90. s->rms_sum = 0;
  91. }
  92. static int config_input(AVFilterLink *inlink)
  93. {
  94. AVFilterContext *ctx = inlink->dst;
  95. SilenceRemoveContext *s = ctx->priv;
  96. s->window_size = (inlink->sample_rate / 50) * inlink->channels;
  97. s->window = av_malloc_array(s->window_size, sizeof(*s->window));
  98. if (!s->window)
  99. return AVERROR(ENOMEM);
  100. clear_rms(s);
  101. s->start_duration = av_rescale(s->start_duration, inlink->sample_rate,
  102. AV_TIME_BASE);
  103. s->stop_duration = av_rescale(s->stop_duration, inlink->sample_rate,
  104. AV_TIME_BASE);
  105. s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
  106. sizeof(*s->start_holdoff) *
  107. inlink->channels);
  108. if (!s->start_holdoff)
  109. return AVERROR(ENOMEM);
  110. s->start_holdoff_offset = 0;
  111. s->start_holdoff_end = 0;
  112. s->start_found_periods = 0;
  113. s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
  114. sizeof(*s->stop_holdoff) *
  115. inlink->channels);
  116. if (!s->stop_holdoff)
  117. return AVERROR(ENOMEM);
  118. s->stop_holdoff_offset = 0;
  119. s->stop_holdoff_end = 0;
  120. s->stop_found_periods = 0;
  121. if (s->start_periods)
  122. s->mode = SILENCE_TRIM;
  123. else
  124. s->mode = SILENCE_COPY;
  125. return 0;
  126. }
  127. static int config_output(AVFilterLink *outlink)
  128. {
  129. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  130. return 0;
  131. }
  132. static double compute_rms(SilenceRemoveContext *s, double sample)
  133. {
  134. double new_sum;
  135. new_sum = s->rms_sum;
  136. new_sum -= *s->window_current;
  137. new_sum += sample * sample;
  138. return sqrt(new_sum / s->window_size);
  139. }
  140. static void update_rms(SilenceRemoveContext *s, double sample)
  141. {
  142. s->rms_sum -= *s->window_current;
  143. *s->window_current = sample * sample;
  144. s->rms_sum += *s->window_current;
  145. s->window_current++;
  146. if (s->window_current >= s->window_end)
  147. s->window_current = s->window;
  148. }
  149. static void flush(AVFrame *out, AVFilterLink *outlink,
  150. int *nb_samples_written, int *ret)
  151. {
  152. if (*nb_samples_written) {
  153. out->nb_samples = *nb_samples_written / outlink->channels;
  154. *ret = ff_filter_frame(outlink, out);
  155. *nb_samples_written = 0;
  156. } else {
  157. av_frame_free(&out);
  158. }
  159. }
  160. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  161. {
  162. AVFilterContext *ctx = inlink->dst;
  163. AVFilterLink *outlink = ctx->outputs[0];
  164. SilenceRemoveContext *s = ctx->priv;
  165. int i, j, threshold, ret = 0;
  166. int nbs, nb_samples_read, nb_samples_written;
  167. double *obuf, *ibuf = (double *)in->data[0];
  168. AVFrame *out;
  169. nb_samples_read = nb_samples_written = 0;
  170. switch (s->mode) {
  171. case SILENCE_TRIM:
  172. silence_trim:
  173. nbs = in->nb_samples - nb_samples_read / inlink->channels;
  174. if (!nbs)
  175. break;
  176. for (i = 0; i < nbs; i++) {
  177. threshold = 0;
  178. for (j = 0; j < inlink->channels; j++) {
  179. threshold |= compute_rms(s, ibuf[j]) > s->start_threshold;
  180. }
  181. if (threshold) {
  182. for (j = 0; j < inlink->channels; j++) {
  183. update_rms(s, *ibuf);
  184. s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
  185. nb_samples_read++;
  186. }
  187. if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
  188. if (++s->start_found_periods >= s->start_periods) {
  189. s->mode = SILENCE_TRIM_FLUSH;
  190. goto silence_trim_flush;
  191. }
  192. s->start_holdoff_offset = 0;
  193. s->start_holdoff_end = 0;
  194. }
  195. } else {
  196. s->start_holdoff_end = 0;
  197. for (j = 0; j < inlink->channels; j++)
  198. update_rms(s, ibuf[j]);
  199. ibuf += inlink->channels;
  200. nb_samples_read += inlink->channels;
  201. }
  202. }
  203. break;
  204. case SILENCE_TRIM_FLUSH:
  205. silence_trim_flush:
  206. nbs = s->start_holdoff_end - s->start_holdoff_offset;
  207. nbs -= nbs % inlink->channels;
  208. if (!nbs)
  209. break;
  210. out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
  211. if (!out) {
  212. av_frame_free(&in);
  213. return AVERROR(ENOMEM);
  214. }
  215. memcpy(out->data[0], &s->start_holdoff[s->start_holdoff_offset],
  216. nbs * sizeof(double));
  217. s->start_holdoff_offset += nbs;
  218. ret = ff_filter_frame(outlink, out);
  219. if (s->start_holdoff_offset == s->start_holdoff_end) {
  220. s->start_holdoff_offset = 0;
  221. s->start_holdoff_end = 0;
  222. s->mode = SILENCE_COPY;
  223. goto silence_copy;
  224. }
  225. break;
  226. case SILENCE_COPY:
  227. silence_copy:
  228. nbs = in->nb_samples - nb_samples_read / inlink->channels;
  229. if (!nbs)
  230. break;
  231. out = ff_get_audio_buffer(inlink, nbs);
  232. if (!out) {
  233. av_frame_free(&in);
  234. return AVERROR(ENOMEM);
  235. }
  236. obuf = (double *)out->data[0];
  237. if (s->stop_periods) {
  238. for (i = 0; i < nbs; i++) {
  239. threshold = 1;
  240. for (j = 0; j < inlink->channels; j++)
  241. threshold &= compute_rms(s, ibuf[j]) > s->stop_threshold;
  242. if (threshold && s->stop_holdoff_end && !s->leave_silence) {
  243. s->mode = SILENCE_COPY_FLUSH;
  244. flush(out, outlink, &nb_samples_written, &ret);
  245. goto silence_copy_flush;
  246. } else if (threshold) {
  247. for (j = 0; j < inlink->channels; j++) {
  248. update_rms(s, *ibuf);
  249. *obuf++ = *ibuf++;
  250. nb_samples_read++;
  251. nb_samples_written++;
  252. }
  253. } else if (!threshold) {
  254. for (j = 0; j < inlink->channels; j++) {
  255. update_rms(s, *ibuf);
  256. if (s->leave_silence) {
  257. *obuf++ = *ibuf;
  258. nb_samples_written++;
  259. }
  260. s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
  261. nb_samples_read++;
  262. }
  263. if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
  264. if (++s->stop_found_periods >= s->stop_periods) {
  265. s->stop_holdoff_offset = 0;
  266. s->stop_holdoff_end = 0;
  267. if (!s->restart) {
  268. s->mode = SILENCE_STOP;
  269. flush(out, outlink, &nb_samples_written, &ret);
  270. goto silence_stop;
  271. } else {
  272. s->stop_found_periods = 0;
  273. s->start_found_periods = 0;
  274. s->start_holdoff_offset = 0;
  275. s->start_holdoff_end = 0;
  276. clear_rms(s);
  277. s->mode = SILENCE_TRIM;
  278. flush(out, outlink, &nb_samples_written, &ret);
  279. goto silence_trim;
  280. }
  281. }
  282. s->mode = SILENCE_COPY_FLUSH;
  283. flush(out, outlink, &nb_samples_written, &ret);
  284. goto silence_copy_flush;
  285. }
  286. }
  287. }
  288. flush(out, outlink, &nb_samples_written, &ret);
  289. } else {
  290. memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
  291. ret = ff_filter_frame(outlink, out);
  292. }
  293. break;
  294. case SILENCE_COPY_FLUSH:
  295. silence_copy_flush:
  296. nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
  297. nbs -= nbs % inlink->channels;
  298. if (!nbs)
  299. break;
  300. out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
  301. if (!out) {
  302. av_frame_free(&in);
  303. return AVERROR(ENOMEM);
  304. }
  305. memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
  306. nbs * sizeof(double));
  307. s->stop_holdoff_offset += nbs;
  308. ret = ff_filter_frame(outlink, out);
  309. if (s->stop_holdoff_offset == s->stop_holdoff_end) {
  310. s->stop_holdoff_offset = 0;
  311. s->stop_holdoff_end = 0;
  312. s->mode = SILENCE_COPY;
  313. goto silence_copy;
  314. }
  315. break;
  316. case SILENCE_STOP:
  317. silence_stop:
  318. break;
  319. }
  320. av_frame_free(&in);
  321. return ret;
  322. }
  323. static int request_frame(AVFilterLink *outlink)
  324. {
  325. AVFilterContext *ctx = outlink->src;
  326. SilenceRemoveContext *s = ctx->priv;
  327. int ret;
  328. ret = ff_request_frame(ctx->inputs[0]);
  329. if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
  330. s->mode == SILENCE_COPY)) {
  331. int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
  332. if (nbs) {
  333. AVFrame *frame;
  334. frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
  335. if (!frame)
  336. return AVERROR(ENOMEM);
  337. memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
  338. nbs * sizeof(double));
  339. ret = ff_filter_frame(ctx->inputs[0], frame);
  340. }
  341. s->mode = SILENCE_STOP;
  342. }
  343. return ret;
  344. }
  345. static int query_formats(AVFilterContext *ctx)
  346. {
  347. AVFilterFormats *formats = NULL;
  348. AVFilterChannelLayouts *layouts = NULL;
  349. static const enum AVSampleFormat sample_fmts[] = {
  350. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
  351. };
  352. layouts = ff_all_channel_layouts();
  353. if (!layouts)
  354. return AVERROR(ENOMEM);
  355. ff_set_common_channel_layouts(ctx, layouts);
  356. formats = ff_make_format_list(sample_fmts);
  357. if (!formats)
  358. return AVERROR(ENOMEM);
  359. ff_set_common_formats(ctx, formats);
  360. formats = ff_all_samplerates();
  361. if (!formats)
  362. return AVERROR(ENOMEM);
  363. ff_set_common_samplerates(ctx, formats);
  364. return 0;
  365. }
  366. static av_cold void uninit(AVFilterContext *ctx)
  367. {
  368. SilenceRemoveContext *s = ctx->priv;
  369. av_freep(&s->start_holdoff);
  370. av_freep(&s->stop_holdoff);
  371. av_freep(&s->window);
  372. }
  373. static const AVFilterPad silenceremove_inputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_AUDIO,
  377. .config_props = config_input,
  378. .filter_frame = filter_frame,
  379. },
  380. { NULL }
  381. };
  382. static const AVFilterPad silenceremove_outputs[] = {
  383. {
  384. .name = "default",
  385. .type = AVMEDIA_TYPE_AUDIO,
  386. .config_props = config_output,
  387. .request_frame = request_frame,
  388. },
  389. { NULL }
  390. };
  391. AVFilter ff_af_silenceremove = {
  392. .name = "silenceremove",
  393. .description = NULL_IF_CONFIG_SMALL("Remove silence."),
  394. .priv_size = sizeof(SilenceRemoveContext),
  395. .priv_class = &silenceremove_class,
  396. .init = init,
  397. .uninit = uninit,
  398. .query_formats = query_formats,
  399. .inputs = silenceremove_inputs,
  400. .outputs = silenceremove_outputs,
  401. };