af_compensationdelay.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Vladimir Sadovnikov and others
  3. * Copyright (c) 2015 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  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 CompensationDelayContext {
  27. const AVClass *class;
  28. int distance_mm;
  29. int distance_cm;
  30. int distance_m;
  31. double dry, wet;
  32. int temp;
  33. unsigned delay;
  34. unsigned w_ptr;
  35. unsigned buf_size;
  36. AVFrame *delay_frame;
  37. } CompensationDelayContext;
  38. #define OFFSET(x) offsetof(CompensationDelayContext, x)
  39. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  40. static const AVOption compensationdelay_options[] = {
  41. { "mm", "set mm distance", OFFSET(distance_mm), AV_OPT_TYPE_INT, {.i64=0}, 0, 10, A },
  42. { "cm", "set cm distance", OFFSET(distance_cm), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, A },
  43. { "m", "set meter distance", OFFSET(distance_m), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, A },
  44. { "dry", "set dry amount", OFFSET(dry), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, A },
  45. { "wet", "set wet amount", OFFSET(wet), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A },
  46. { "temp", "set temperature °C", OFFSET(temp), AV_OPT_TYPE_INT, {.i64=20}, -50, 50, A },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(compensationdelay);
  50. // The maximum distance for options
  51. #define COMP_DELAY_MAX_DISTANCE (100.0 * 100.0 + 100.0 * 1.0 + 1.0)
  52. // The actual speed of sound in normal conditions
  53. #define COMP_DELAY_SOUND_SPEED_KM_H(temp) 1.85325 * (643.95 * sqrt(((temp + 273.15) / 273.15)))
  54. #define COMP_DELAY_SOUND_SPEED_CM_S(temp) (COMP_DELAY_SOUND_SPEED_KM_H(temp) * (1000.0 * 100.0) /* cm/km */ / (60.0 * 60.0) /* s/h */)
  55. #define COMP_DELAY_SOUND_FRONT_DELAY(temp) (1.0 / COMP_DELAY_SOUND_SPEED_CM_S(temp))
  56. // The maximum delay may be reached by this filter
  57. #define COMP_DELAY_MAX_DELAY (COMP_DELAY_MAX_DISTANCE * COMP_DELAY_SOUND_FRONT_DELAY(50))
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. AVFilterChannelLayouts *layouts;
  61. AVFilterFormats *formats;
  62. static const enum AVSampleFormat sample_fmts[] = {
  63. AV_SAMPLE_FMT_DBLP,
  64. AV_SAMPLE_FMT_NONE
  65. };
  66. int ret;
  67. layouts = ff_all_channel_counts();
  68. if (!layouts)
  69. return AVERROR(ENOMEM);
  70. ret = ff_set_common_channel_layouts(ctx, layouts);
  71. if (ret < 0)
  72. return ret;
  73. formats = ff_make_format_list(sample_fmts);
  74. if (!formats)
  75. return AVERROR(ENOMEM);
  76. ret = ff_set_common_formats(ctx, formats);
  77. if (ret < 0)
  78. return ret;
  79. formats = ff_all_samplerates();
  80. if (!formats)
  81. return AVERROR(ENOMEM);
  82. return ff_set_common_samplerates(ctx, formats);
  83. }
  84. static int config_input(AVFilterLink *inlink)
  85. {
  86. AVFilterContext *ctx = inlink->dst;
  87. CompensationDelayContext *s = ctx->priv;
  88. unsigned min_size, new_size = 1;
  89. s->delay = (s->distance_m * 100. + s->distance_cm * 1. + s->distance_mm * .1) *
  90. COMP_DELAY_SOUND_FRONT_DELAY(s->temp) * inlink->sample_rate;
  91. min_size = inlink->sample_rate * COMP_DELAY_MAX_DELAY;
  92. while (new_size < min_size)
  93. new_size <<= 1;
  94. s->delay_frame = av_frame_alloc();
  95. if (!s->delay_frame)
  96. return AVERROR(ENOMEM);
  97. s->buf_size = new_size;
  98. s->delay_frame->format = inlink->format;
  99. s->delay_frame->nb_samples = new_size;
  100. s->delay_frame->channel_layout = inlink->channel_layout;
  101. return av_frame_get_buffer(s->delay_frame, 32);
  102. }
  103. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  104. {
  105. AVFilterContext *ctx = inlink->dst;
  106. CompensationDelayContext *s = ctx->priv;
  107. const unsigned b_mask = s->buf_size - 1;
  108. const unsigned buf_size = s->buf_size;
  109. const unsigned delay = s->delay;
  110. const double dry = s->dry;
  111. const double wet = s->wet;
  112. unsigned r_ptr, w_ptr;
  113. AVFrame *out;
  114. int n, ch;
  115. out = ff_get_audio_buffer(inlink, in->nb_samples);
  116. if (!out) {
  117. av_frame_free(&in);
  118. return AVERROR(ENOMEM);
  119. }
  120. av_frame_copy_props(out, in);
  121. for (ch = 0; ch < inlink->channels; ch++) {
  122. const double *src = (const double *)in->extended_data[ch];
  123. double *dst = (double *)out->extended_data[ch];
  124. double *buffer = (double *)s->delay_frame->extended_data[ch];
  125. w_ptr = s->w_ptr;
  126. r_ptr = (w_ptr + buf_size - delay) & b_mask;
  127. for (n = 0; n < in->nb_samples; n++) {
  128. const double sample = src[n];
  129. buffer[w_ptr] = sample;
  130. dst[n] = dry * sample + wet * buffer[r_ptr];
  131. w_ptr = (w_ptr + 1) & b_mask;
  132. r_ptr = (r_ptr + 1) & b_mask;
  133. }
  134. }
  135. s->w_ptr = w_ptr;
  136. av_frame_free(&in);
  137. return ff_filter_frame(ctx->outputs[0], out);
  138. }
  139. static av_cold void uninit(AVFilterContext *ctx)
  140. {
  141. CompensationDelayContext *s = ctx->priv;
  142. av_frame_free(&s->delay_frame);
  143. }
  144. static const AVFilterPad compensationdelay_inputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_AUDIO,
  148. .config_props = config_input,
  149. .filter_frame = filter_frame,
  150. },
  151. { NULL }
  152. };
  153. static const AVFilterPad compensationdelay_outputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_AUDIO,
  157. },
  158. { NULL }
  159. };
  160. AVFilter ff_af_compensationdelay = {
  161. .name = "compensationdelay",
  162. .description = NULL_IF_CONFIG_SMALL("Audio Compensation Delay Line."),
  163. .query_formats = query_formats,
  164. .priv_size = sizeof(CompensationDelayContext),
  165. .priv_class = &compensationdelay_class,
  166. .uninit = uninit,
  167. .inputs = compensationdelay_inputs,
  168. .outputs = compensationdelay_outputs,
  169. };