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.

349 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/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;
  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. av_sscanf(arg, "%f", &delay);
  137. d->delay = delay * inlink->sample_rate / 1000.0;
  138. }
  139. if (d->delay < 0) {
  140. av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
  141. return AVERROR(EINVAL);
  142. }
  143. }
  144. s->padding = s->chandelay[0].delay;
  145. for (i = 1; i < s->nb_delays; i++) {
  146. ChanDelay *d = &s->chandelay[i];
  147. s->padding = FFMIN(s->padding, d->delay);
  148. }
  149. if (s->padding) {
  150. for (i = 0; i < s->nb_delays; i++) {
  151. ChanDelay *d = &s->chandelay[i];
  152. d->delay -= s->padding;
  153. }
  154. }
  155. for (i = 0; i < s->nb_delays; i++) {
  156. ChanDelay *d = &s->chandelay[i];
  157. if (!d->delay)
  158. continue;
  159. d->samples = av_malloc_array(d->delay, s->block_align);
  160. if (!d->samples)
  161. return AVERROR(ENOMEM);
  162. s->max_delay = FFMAX(s->max_delay, d->delay);
  163. }
  164. switch (inlink->format) {
  165. case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
  166. case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
  167. case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
  168. case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
  169. case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
  170. }
  171. return 0;
  172. }
  173. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  174. {
  175. AVFilterContext *ctx = inlink->dst;
  176. AudioDelayContext *s = ctx->priv;
  177. AVFrame *out_frame;
  178. int i;
  179. if (ctx->is_disabled || !s->delays)
  180. return ff_filter_frame(ctx->outputs[0], frame);
  181. out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
  182. if (!out_frame) {
  183. av_frame_free(&frame);
  184. return AVERROR(ENOMEM);
  185. }
  186. av_frame_copy_props(out_frame, frame);
  187. for (i = 0; i < s->nb_delays; i++) {
  188. ChanDelay *d = &s->chandelay[i];
  189. const uint8_t *src = frame->extended_data[i];
  190. uint8_t *dst = out_frame->extended_data[i];
  191. if (!d->delay)
  192. memcpy(dst, src, frame->nb_samples * s->block_align);
  193. else
  194. s->delay_channel(d, frame->nb_samples, src, dst);
  195. }
  196. out_frame->pts = s->next_pts;
  197. s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  198. av_frame_free(&frame);
  199. return ff_filter_frame(ctx->outputs[0], out_frame);
  200. }
  201. static int activate(AVFilterContext *ctx)
  202. {
  203. AVFilterLink *inlink = ctx->inputs[0];
  204. AVFilterLink *outlink = ctx->outputs[0];
  205. AudioDelayContext *s = ctx->priv;
  206. AVFrame *frame = NULL;
  207. int ret, status;
  208. int64_t pts;
  209. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  210. if (s->padding) {
  211. int nb_samples = FFMIN(s->padding, 2048);
  212. frame = ff_get_audio_buffer(outlink, nb_samples);
  213. if (!frame)
  214. return AVERROR(ENOMEM);
  215. s->padding -= nb_samples;
  216. av_samples_set_silence(frame->extended_data, 0,
  217. frame->nb_samples,
  218. outlink->channels,
  219. frame->format);
  220. frame->pts = s->next_pts;
  221. if (s->next_pts != AV_NOPTS_VALUE)
  222. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  223. return ff_filter_frame(outlink, frame);
  224. }
  225. ret = ff_inlink_consume_frame(inlink, &frame);
  226. if (ret < 0)
  227. return ret;
  228. if (ret > 0)
  229. return filter_frame(inlink, frame);
  230. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  231. if (status == AVERROR_EOF)
  232. s->eof = 1;
  233. }
  234. if (s->eof && s->max_delay) {
  235. int nb_samples = FFMIN(s->max_delay, 2048);
  236. frame = ff_get_audio_buffer(outlink, nb_samples);
  237. if (!frame)
  238. return AVERROR(ENOMEM);
  239. s->max_delay -= nb_samples;
  240. av_samples_set_silence(frame->extended_data, 0,
  241. frame->nb_samples,
  242. outlink->channels,
  243. frame->format);
  244. frame->pts = s->next_pts;
  245. return filter_frame(inlink, frame);
  246. }
  247. if (s->eof && s->max_delay == 0) {
  248. ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
  249. return 0;
  250. }
  251. if (!s->eof)
  252. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  253. return FFERROR_NOT_READY;
  254. }
  255. static av_cold void uninit(AVFilterContext *ctx)
  256. {
  257. AudioDelayContext *s = ctx->priv;
  258. if (s->chandelay) {
  259. for (int i = 0; i < s->nb_delays; i++)
  260. av_freep(&s->chandelay[i].samples);
  261. }
  262. av_freep(&s->chandelay);
  263. }
  264. static const AVFilterPad adelay_inputs[] = {
  265. {
  266. .name = "default",
  267. .type = AVMEDIA_TYPE_AUDIO,
  268. .config_props = config_input,
  269. },
  270. { NULL }
  271. };
  272. static const AVFilterPad adelay_outputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. },
  277. { NULL }
  278. };
  279. AVFilter ff_af_adelay = {
  280. .name = "adelay",
  281. .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
  282. .query_formats = query_formats,
  283. .priv_size = sizeof(AudioDelayContext),
  284. .priv_class = &adelay_class,
  285. .activate = activate,
  286. .uninit = uninit,
  287. .inputs = adelay_inputs,
  288. .outputs = adelay_outputs,
  289. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  290. };