af_adelay.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/avstring.h"
  21. #include "libavutil/eval.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 ChanDelay {
  29. int delay;
  30. unsigned delay_index;
  31. unsigned index;
  32. uint8_t *samples;
  33. } ChanDelay;
  34. typedef struct AudioDelayContext {
  35. const AVClass *class;
  36. char *delays;
  37. ChanDelay *chandelay;
  38. int nb_delays;
  39. int block_align;
  40. int64_t padding;
  41. int64_t max_delay;
  42. int64_t next_pts;
  43. int eof;
  44. void (*delay_channel)(ChanDelay *d, int nb_samples,
  45. const uint8_t *src, uint8_t *dst);
  46. } AudioDelayContext;
  47. #define OFFSET(x) offsetof(AudioDelayContext, x)
  48. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. static const AVOption adelay_options[] = {
  50. { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(adelay);
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. AVFilterChannelLayouts *layouts;
  57. AVFilterFormats *formats;
  58. static const enum AVSampleFormat sample_fmts[] = {
  59. AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
  60. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
  61. AV_SAMPLE_FMT_NONE
  62. };
  63. int ret;
  64. layouts = ff_all_channel_counts();
  65. if (!layouts)
  66. return AVERROR(ENOMEM);
  67. ret = ff_set_common_channel_layouts(ctx, layouts);
  68. if (ret < 0)
  69. return ret;
  70. formats = ff_make_format_list(sample_fmts);
  71. if (!formats)
  72. return AVERROR(ENOMEM);
  73. ret = ff_set_common_formats(ctx, formats);
  74. if (ret < 0)
  75. return ret;
  76. formats = ff_all_samplerates();
  77. if (!formats)
  78. return AVERROR(ENOMEM);
  79. return ff_set_common_samplerates(ctx, formats);
  80. }
  81. #define DELAY(name, type, fill) \
  82. static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
  83. const uint8_t *ssrc, uint8_t *ddst) \
  84. { \
  85. const type *src = (type *)ssrc; \
  86. type *dst = (type *)ddst; \
  87. type *samples = (type *)d->samples; \
  88. \
  89. while (nb_samples) { \
  90. if (d->delay_index < d->delay) { \
  91. const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
  92. \
  93. memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
  94. memset(dst, fill, len * sizeof(type)); \
  95. d->delay_index += len; \
  96. src += len; \
  97. dst += len; \
  98. nb_samples -= len; \
  99. } else { \
  100. *dst = samples[d->index]; \
  101. samples[d->index] = *src; \
  102. nb_samples--; \
  103. d->index++; \
  104. src++, dst++; \
  105. d->index = d->index >= d->delay ? 0 : d->index; \
  106. } \
  107. } \
  108. }
  109. DELAY(u8, uint8_t, 0x80)
  110. DELAY(s16, int16_t, 0)
  111. DELAY(s32, int32_t, 0)
  112. DELAY(flt, float, 0)
  113. DELAY(dbl, double, 0)
  114. static int config_input(AVFilterLink *inlink)
  115. {
  116. AVFilterContext *ctx = inlink->dst;
  117. AudioDelayContext *s = ctx->priv;
  118. char *p, *arg, *saveptr = NULL;
  119. int i;
  120. s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
  121. if (!s->chandelay)
  122. return AVERROR(ENOMEM);
  123. s->nb_delays = inlink->channels;
  124. s->block_align = av_get_bytes_per_sample(inlink->format);
  125. p = s->delays;
  126. for (i = 0; i < s->nb_delays; i++) {
  127. ChanDelay *d = &s->chandelay[i];
  128. float delay, div;
  129. char type = 0;
  130. int ret;
  131. if (!(arg = av_strtok(p, "|", &saveptr)))
  132. break;
  133. p = NULL;
  134. ret = av_sscanf(arg, "%d%c", &d->delay, &type);
  135. if (ret != 2 || type != 'S') {
  136. div = type == 's' ? 1.0 : 1000.0;
  137. av_sscanf(arg, "%f", &delay);
  138. d->delay = delay * inlink->sample_rate / div;
  139. }
  140. if (d->delay < 0) {
  141. av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
  142. return AVERROR(EINVAL);
  143. }
  144. }
  145. s->padding = s->chandelay[0].delay;
  146. for (i = 1; i < s->nb_delays; i++) {
  147. ChanDelay *d = &s->chandelay[i];
  148. s->padding = FFMIN(s->padding, d->delay);
  149. }
  150. if (s->padding) {
  151. for (i = 0; i < s->nb_delays; i++) {
  152. ChanDelay *d = &s->chandelay[i];
  153. d->delay -= s->padding;
  154. }
  155. }
  156. for (i = 0; i < s->nb_delays; i++) {
  157. ChanDelay *d = &s->chandelay[i];
  158. if (!d->delay)
  159. continue;
  160. d->samples = av_malloc_array(d->delay, s->block_align);
  161. if (!d->samples)
  162. return AVERROR(ENOMEM);
  163. s->max_delay = FFMAX(s->max_delay, d->delay);
  164. }
  165. switch (inlink->format) {
  166. case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
  167. case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
  168. case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
  169. case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
  170. case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
  171. }
  172. return 0;
  173. }
  174. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  175. {
  176. AVFilterContext *ctx = inlink->dst;
  177. AudioDelayContext *s = ctx->priv;
  178. AVFrame *out_frame;
  179. int i;
  180. if (ctx->is_disabled || !s->delays)
  181. return ff_filter_frame(ctx->outputs[0], frame);
  182. out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
  183. if (!out_frame) {
  184. av_frame_free(&frame);
  185. return AVERROR(ENOMEM);
  186. }
  187. av_frame_copy_props(out_frame, frame);
  188. for (i = 0; i < s->nb_delays; i++) {
  189. ChanDelay *d = &s->chandelay[i];
  190. const uint8_t *src = frame->extended_data[i];
  191. uint8_t *dst = out_frame->extended_data[i];
  192. if (!d->delay)
  193. memcpy(dst, src, frame->nb_samples * s->block_align);
  194. else
  195. s->delay_channel(d, frame->nb_samples, src, dst);
  196. }
  197. out_frame->pts = s->next_pts;
  198. s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  199. av_frame_free(&frame);
  200. return ff_filter_frame(ctx->outputs[0], out_frame);
  201. }
  202. static int activate(AVFilterContext *ctx)
  203. {
  204. AVFilterLink *inlink = ctx->inputs[0];
  205. AVFilterLink *outlink = ctx->outputs[0];
  206. AudioDelayContext *s = ctx->priv;
  207. AVFrame *frame = NULL;
  208. int ret, status;
  209. int64_t pts;
  210. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  211. if (s->padding) {
  212. int nb_samples = FFMIN(s->padding, 2048);
  213. frame = ff_get_audio_buffer(outlink, nb_samples);
  214. if (!frame)
  215. return AVERROR(ENOMEM);
  216. s->padding -= nb_samples;
  217. av_samples_set_silence(frame->extended_data, 0,
  218. frame->nb_samples,
  219. outlink->channels,
  220. frame->format);
  221. frame->pts = s->next_pts;
  222. if (s->next_pts != AV_NOPTS_VALUE)
  223. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  224. return ff_filter_frame(outlink, frame);
  225. }
  226. ret = ff_inlink_consume_frame(inlink, &frame);
  227. if (ret < 0)
  228. return ret;
  229. if (ret > 0)
  230. return filter_frame(inlink, frame);
  231. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  232. if (status == AVERROR_EOF)
  233. s->eof = 1;
  234. }
  235. if (s->eof && s->max_delay) {
  236. int nb_samples = FFMIN(s->max_delay, 2048);
  237. frame = ff_get_audio_buffer(outlink, nb_samples);
  238. if (!frame)
  239. return AVERROR(ENOMEM);
  240. s->max_delay -= nb_samples;
  241. av_samples_set_silence(frame->extended_data, 0,
  242. frame->nb_samples,
  243. outlink->channels,
  244. frame->format);
  245. frame->pts = s->next_pts;
  246. return filter_frame(inlink, frame);
  247. }
  248. if (s->eof && s->max_delay == 0) {
  249. ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
  250. return 0;
  251. }
  252. if (!s->eof)
  253. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  254. return FFERROR_NOT_READY;
  255. }
  256. static av_cold void uninit(AVFilterContext *ctx)
  257. {
  258. AudioDelayContext *s = ctx->priv;
  259. if (s->chandelay) {
  260. for (int i = 0; i < s->nb_delays; i++)
  261. av_freep(&s->chandelay[i].samples);
  262. }
  263. av_freep(&s->chandelay);
  264. }
  265. static const AVFilterPad adelay_inputs[] = {
  266. {
  267. .name = "default",
  268. .type = AVMEDIA_TYPE_AUDIO,
  269. .config_props = config_input,
  270. },
  271. { NULL }
  272. };
  273. static const AVFilterPad adelay_outputs[] = {
  274. {
  275. .name = "default",
  276. .type = AVMEDIA_TYPE_AUDIO,
  277. },
  278. { NULL }
  279. };
  280. AVFilter ff_af_adelay = {
  281. .name = "adelay",
  282. .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
  283. .query_formats = query_formats,
  284. .priv_size = sizeof(AudioDelayContext),
  285. .priv_class = &adelay_class,
  286. .activate = activate,
  287. .uninit = uninit,
  288. .inputs = adelay_inputs,
  289. .outputs = adelay_outputs,
  290. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  291. };