af_adelay.c 9.2 KB

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