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.

289 lines
9.4KB

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