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.

391 lines
13KB

  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/samplefmt.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "filters.h"
  27. #include "internal.h"
  28. typedef struct AudioEchoContext {
  29. const AVClass *class;
  30. float in_gain, out_gain;
  31. char *delays, *decays;
  32. float *delay, *decay;
  33. int nb_echoes;
  34. int delay_index;
  35. uint8_t **delayptrs;
  36. int max_samples, fade_out;
  37. int *samples;
  38. int eof;
  39. int64_t next_pts;
  40. void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t **delayptrs,
  41. uint8_t * const *src, uint8_t **dst,
  42. int nb_samples, int channels);
  43. } AudioEchoContext;
  44. #define OFFSET(x) offsetof(AudioEchoContext, x)
  45. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption aecho_options[] = {
  47. { "in_gain", "set signal input gain", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.6}, 0, 1, A },
  48. { "out_gain", "set signal output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.3}, 0, 1, A },
  49. { "delays", "set list of signal delays", OFFSET(delays), AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
  50. { "decays", "set list of signal decays", OFFSET(decays), AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(aecho);
  54. static void count_items(char *item_str, int *nb_items)
  55. {
  56. char *p;
  57. *nb_items = 1;
  58. for (p = item_str; *p; p++) {
  59. if (*p == '|')
  60. (*nb_items)++;
  61. }
  62. }
  63. static void fill_items(char *item_str, int *nb_items, float *items)
  64. {
  65. char *p, *saveptr = NULL;
  66. int i, new_nb_items = 0;
  67. p = item_str;
  68. for (i = 0; i < *nb_items; i++) {
  69. char *tstr = av_strtok(p, "|", &saveptr);
  70. p = NULL;
  71. if (tstr)
  72. new_nb_items += av_sscanf(tstr, "%f", &items[new_nb_items]) == 1;
  73. }
  74. *nb_items = new_nb_items;
  75. }
  76. static av_cold void uninit(AVFilterContext *ctx)
  77. {
  78. AudioEchoContext *s = ctx->priv;
  79. av_freep(&s->delay);
  80. av_freep(&s->decay);
  81. av_freep(&s->samples);
  82. if (s->delayptrs)
  83. av_freep(&s->delayptrs[0]);
  84. av_freep(&s->delayptrs);
  85. }
  86. static av_cold int init(AVFilterContext *ctx)
  87. {
  88. AudioEchoContext *s = ctx->priv;
  89. int nb_delays, nb_decays, i;
  90. if (!s->delays || !s->decays) {
  91. av_log(ctx, AV_LOG_ERROR, "Missing delays and/or decays.\n");
  92. return AVERROR(EINVAL);
  93. }
  94. count_items(s->delays, &nb_delays);
  95. count_items(s->decays, &nb_decays);
  96. s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
  97. s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
  98. if (!s->delay || !s->decay)
  99. return AVERROR(ENOMEM);
  100. fill_items(s->delays, &nb_delays, s->delay);
  101. fill_items(s->decays, &nb_decays, s->decay);
  102. if (nb_delays != nb_decays) {
  103. av_log(ctx, AV_LOG_ERROR, "Number of delays %d differs from number of decays %d.\n", nb_delays, nb_decays);
  104. return AVERROR(EINVAL);
  105. }
  106. s->nb_echoes = nb_delays;
  107. if (!s->nb_echoes) {
  108. av_log(ctx, AV_LOG_ERROR, "At least one decay & delay must be set.\n");
  109. return AVERROR(EINVAL);
  110. }
  111. s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples));
  112. if (!s->samples)
  113. return AVERROR(ENOMEM);
  114. for (i = 0; i < nb_delays; i++) {
  115. if (s->delay[i] <= 0 || s->delay[i] > 90000) {
  116. av_log(ctx, AV_LOG_ERROR, "delay[%d]: %f is out of allowed range: (0, 90000]\n", i, s->delay[i]);
  117. return AVERROR(EINVAL);
  118. }
  119. if (s->decay[i] <= 0 || s->decay[i] > 1) {
  120. av_log(ctx, AV_LOG_ERROR, "decay[%d]: %f is out of allowed range: (0, 1]\n", i, s->decay[i]);
  121. return AVERROR(EINVAL);
  122. }
  123. }
  124. s->next_pts = AV_NOPTS_VALUE;
  125. av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes);
  126. return 0;
  127. }
  128. static int query_formats(AVFilterContext *ctx)
  129. {
  130. AVFilterChannelLayouts *layouts;
  131. AVFilterFormats *formats;
  132. static const enum AVSampleFormat sample_fmts[] = {
  133. AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
  134. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
  135. AV_SAMPLE_FMT_NONE
  136. };
  137. int ret;
  138. layouts = ff_all_channel_counts();
  139. if (!layouts)
  140. return AVERROR(ENOMEM);
  141. ret = ff_set_common_channel_layouts(ctx, layouts);
  142. if (ret < 0)
  143. return ret;
  144. formats = ff_make_format_list(sample_fmts);
  145. if (!formats)
  146. return AVERROR(ENOMEM);
  147. ret = ff_set_common_formats(ctx, formats);
  148. if (ret < 0)
  149. return ret;
  150. formats = ff_all_samplerates();
  151. if (!formats)
  152. return AVERROR(ENOMEM);
  153. return ff_set_common_samplerates(ctx, formats);
  154. }
  155. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  156. #define ECHO(name, type, min, max) \
  157. static void echo_samples_## name ##p(AudioEchoContext *ctx, \
  158. uint8_t **delayptrs, \
  159. uint8_t * const *src, uint8_t **dst, \
  160. int nb_samples, int channels) \
  161. { \
  162. const double out_gain = ctx->out_gain; \
  163. const double in_gain = ctx->in_gain; \
  164. const int nb_echoes = ctx->nb_echoes; \
  165. const int max_samples = ctx->max_samples; \
  166. int i, j, chan, av_uninit(index); \
  167. \
  168. av_assert1(channels > 0); /* would corrupt delay_index */ \
  169. \
  170. for (chan = 0; chan < channels; chan++) { \
  171. const type *s = (type *)src[chan]; \
  172. type *d = (type *)dst[chan]; \
  173. type *dbuf = (type *)delayptrs[chan]; \
  174. \
  175. index = ctx->delay_index; \
  176. for (i = 0; i < nb_samples; i++, s++, d++) { \
  177. double out, in; \
  178. \
  179. in = *s; \
  180. out = in * in_gain; \
  181. for (j = 0; j < nb_echoes; j++) { \
  182. int ix = index + max_samples - ctx->samples[j]; \
  183. ix = MOD(ix, max_samples); \
  184. out += dbuf[ix] * ctx->decay[j]; \
  185. } \
  186. out *= out_gain; \
  187. \
  188. *d = av_clipd(out, min, max); \
  189. dbuf[index] = in; \
  190. \
  191. index = MOD(index + 1, max_samples); \
  192. } \
  193. } \
  194. ctx->delay_index = index; \
  195. }
  196. ECHO(dbl, double, -1.0, 1.0 )
  197. ECHO(flt, float, -1.0, 1.0 )
  198. ECHO(s16, int16_t, INT16_MIN, INT16_MAX)
  199. ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
  200. static int config_output(AVFilterLink *outlink)
  201. {
  202. AVFilterContext *ctx = outlink->src;
  203. AudioEchoContext *s = ctx->priv;
  204. float volume = 1.0;
  205. int i;
  206. for (i = 0; i < s->nb_echoes; i++) {
  207. s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
  208. s->max_samples = FFMAX(s->max_samples, s->samples[i]);
  209. volume += s->decay[i];
  210. }
  211. if (s->max_samples <= 0) {
  212. av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
  213. return AVERROR(EINVAL);
  214. }
  215. s->fade_out = s->max_samples;
  216. if (volume * s->in_gain * s->out_gain > 1.0)
  217. av_log(ctx, AV_LOG_WARNING,
  218. "out_gain %f can cause saturation of output\n", s->out_gain);
  219. switch (outlink->format) {
  220. case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
  221. case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
  222. case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
  223. case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
  224. }
  225. if (s->delayptrs)
  226. av_freep(&s->delayptrs[0]);
  227. av_freep(&s->delayptrs);
  228. return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
  229. outlink->channels,
  230. s->max_samples,
  231. outlink->format, 0);
  232. }
  233. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  234. {
  235. AVFilterContext *ctx = inlink->dst;
  236. AudioEchoContext *s = ctx->priv;
  237. AVFrame *out_frame;
  238. if (av_frame_is_writable(frame)) {
  239. out_frame = frame;
  240. } else {
  241. out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
  242. if (!out_frame) {
  243. av_frame_free(&frame);
  244. return AVERROR(ENOMEM);
  245. }
  246. av_frame_copy_props(out_frame, frame);
  247. }
  248. s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data,
  249. frame->nb_samples, inlink->channels);
  250. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  251. if (frame != out_frame)
  252. av_frame_free(&frame);
  253. return ff_filter_frame(ctx->outputs[0], out_frame);
  254. }
  255. static int request_frame(AVFilterLink *outlink)
  256. {
  257. AVFilterContext *ctx = outlink->src;
  258. AudioEchoContext *s = ctx->priv;
  259. int nb_samples = FFMIN(s->fade_out, 2048);
  260. AVFrame *frame = ff_get_audio_buffer(outlink, nb_samples);
  261. if (!frame)
  262. return AVERROR(ENOMEM);
  263. s->fade_out -= nb_samples;
  264. av_samples_set_silence(frame->extended_data, 0,
  265. frame->nb_samples,
  266. outlink->channels,
  267. frame->format);
  268. s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data,
  269. frame->nb_samples, outlink->channels);
  270. frame->pts = s->next_pts;
  271. if (s->next_pts != AV_NOPTS_VALUE)
  272. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  273. return ff_filter_frame(outlink, frame);
  274. }
  275. static int activate(AVFilterContext *ctx)
  276. {
  277. AVFilterLink *inlink = ctx->inputs[0];
  278. AVFilterLink *outlink = ctx->outputs[0];
  279. AudioEchoContext *s = ctx->priv;
  280. AVFrame *in;
  281. int ret, status;
  282. int64_t pts;
  283. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  284. ret = ff_inlink_consume_frame(inlink, &in);
  285. if (ret < 0)
  286. return ret;
  287. if (ret > 0)
  288. return filter_frame(inlink, in);
  289. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  290. if (status == AVERROR_EOF)
  291. s->eof = 1;
  292. }
  293. if (s->eof && s->fade_out <= 0) {
  294. ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
  295. return 0;
  296. }
  297. if (!s->eof)
  298. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  299. return request_frame(outlink);
  300. }
  301. static const AVFilterPad aecho_inputs[] = {
  302. {
  303. .name = "default",
  304. .type = AVMEDIA_TYPE_AUDIO,
  305. },
  306. { NULL }
  307. };
  308. static const AVFilterPad aecho_outputs[] = {
  309. {
  310. .name = "default",
  311. .config_props = config_output,
  312. .type = AVMEDIA_TYPE_AUDIO,
  313. },
  314. { NULL }
  315. };
  316. AVFilter ff_af_aecho = {
  317. .name = "aecho",
  318. .description = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
  319. .query_formats = query_formats,
  320. .priv_size = sizeof(AudioEchoContext),
  321. .priv_class = &aecho_class,
  322. .init = init,
  323. .activate = activate,
  324. .uninit = uninit,
  325. .inputs = aecho_inputs,
  326. .outputs = aecho_outputs,
  327. };