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.

284 lines
9.2KB

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