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.

348 lines
11KB

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