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.

237 lines
9.8KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <rubberband/rubberband-c.h>
  19. #include "libavutil/channel_layout.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/opt.h"
  22. #include "audio.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. typedef struct RubberBandContext {
  27. const AVClass *class;
  28. RubberBandState rbs;
  29. double tempo, pitch;
  30. int transients, detector, phase, window,
  31. smoothing, formant, opitch, channels;
  32. int64_t nb_samples_out;
  33. int64_t nb_samples_in;
  34. int flushed;
  35. } RubberBandContext;
  36. #define OFFSET(x) offsetof(RubberBandContext, x)
  37. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  38. static const AVOption rubberband_options[] = {
  39. { "tempo", "set tempo scale factor", OFFSET(tempo), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, A },
  40. { "pitch", "set pitch scale factor", OFFSET(pitch), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, A },
  41. { "transients", "set transients", OFFSET(transients), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "transients" },
  42. { "crisp", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsCrisp}, 0, 0, A, "transients" },
  43. { "mixed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsMixed}, 0, 0, A, "transients" },
  44. { "smooth", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsSmooth}, 0, 0, A, "transients" },
  45. { "detector", "set detector", OFFSET(detector), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "detector" },
  46. { "compound", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorCompound}, 0, 0, A, "detector" },
  47. { "percussive", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorPercussive}, 0, 0, A, "detector" },
  48. { "soft", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorSoft}, 0, 0, A, "detector" },
  49. { "phase", "set phase", OFFSET(phase), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "phase" },
  50. { "laminar", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseLaminar}, 0, 0, A, "phase" },
  51. { "independent", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseIndependent}, 0, 0, A, "phase" },
  52. { "window", "set window", OFFSET(window), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "window" },
  53. { "standard", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowStandard}, 0, 0, A, "window" },
  54. { "short", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowShort}, 0, 0, A, "window" },
  55. { "long", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowLong}, 0, 0, A, "window" },
  56. { "smoothing", "set smoothing", OFFSET(smoothing), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "smoothing" },
  57. { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOff}, 0, 0, A, "smoothing" },
  58. { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOn}, 0, 0, A, "smoothing" },
  59. { "formant", "set formant", OFFSET(formant), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "formant" },
  60. { "shifted", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantShifted}, 0, 0, A, "formant" },
  61. { "preserved", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantPreserved}, 0, 0, A, "formant" },
  62. { "pitchq", "set pitch quality", OFFSET(opitch), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "pitch" },
  63. { "quality", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighQuality}, 0, 0, A, "pitch" },
  64. { "speed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighSpeed}, 0, 0, A, "pitch" },
  65. { "consistency", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighConsistency}, 0, 0, A, "pitch" },
  66. { "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "channels" },
  67. { "apart", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsApart}, 0, 0, A, "channels" },
  68. { "together", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsTogether}, 0, 0, A, "channels" },
  69. { NULL },
  70. };
  71. AVFILTER_DEFINE_CLASS(rubberband);
  72. static av_cold void uninit(AVFilterContext *ctx)
  73. {
  74. RubberBandContext *s = ctx->priv;
  75. if (s->rbs)
  76. rubberband_delete(s->rbs);
  77. }
  78. static int query_formats(AVFilterContext *ctx)
  79. {
  80. AVFilterFormats *formats = NULL;
  81. AVFilterChannelLayouts *layouts = NULL;
  82. static const enum AVSampleFormat sample_fmts[] = {
  83. AV_SAMPLE_FMT_FLTP,
  84. AV_SAMPLE_FMT_NONE,
  85. };
  86. int ret;
  87. layouts = ff_all_channel_counts();
  88. if (!layouts)
  89. return AVERROR(ENOMEM);
  90. ret = ff_set_common_channel_layouts(ctx, layouts);
  91. if (ret < 0)
  92. return ret;
  93. formats = ff_make_format_list(sample_fmts);
  94. if (!formats)
  95. return AVERROR(ENOMEM);
  96. ret = ff_set_common_formats(ctx, formats);
  97. if (ret < 0)
  98. return ret;
  99. formats = ff_all_samplerates();
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. return ff_set_common_samplerates(ctx, formats);
  103. }
  104. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  105. {
  106. RubberBandContext *s = inlink->dst->priv;
  107. AVFilterLink *outlink = inlink->dst->outputs[0];
  108. AVFrame *out;
  109. int ret = 0, nb_samples;
  110. rubberband_process(s->rbs, (const float *const *)in->data, in->nb_samples, 0);
  111. s->nb_samples_in += in->nb_samples;
  112. nb_samples = rubberband_available(s->rbs);
  113. if (nb_samples > 0) {
  114. out = ff_get_audio_buffer(inlink, nb_samples);
  115. if (!out) {
  116. av_frame_free(&in);
  117. return AVERROR(ENOMEM);
  118. }
  119. out->pts = av_rescale_q(s->nb_samples_out,
  120. (AVRational){ 1, outlink->sample_rate },
  121. outlink->time_base);
  122. nb_samples = rubberband_retrieve(s->rbs, (float *const *)out->data, nb_samples);
  123. out->nb_samples = nb_samples;
  124. ret = ff_filter_frame(outlink, out);
  125. s->nb_samples_out += nb_samples;
  126. }
  127. av_frame_free(&in);
  128. return ret;
  129. }
  130. static int config_input(AVFilterLink *inlink)
  131. {
  132. AVFilterContext *ctx = inlink->dst;
  133. RubberBandContext *s = ctx->priv;
  134. int opts = s->transients|s->detector|s->phase|s->window|
  135. s->smoothing|s->formant|s->opitch|s->channels|
  136. RubberBandOptionProcessRealTime;
  137. if (s->rbs)
  138. rubberband_delete(s->rbs);
  139. s->rbs = rubberband_new(inlink->sample_rate, inlink->channels, opts, 1. / s->tempo, s->pitch);
  140. inlink->partial_buf_size =
  141. inlink->min_samples =
  142. inlink->max_samples = rubberband_get_samples_required(s->rbs);
  143. return 0;
  144. }
  145. static int request_frame(AVFilterLink *outlink)
  146. {
  147. AVFilterContext *ctx = outlink->src;
  148. RubberBandContext *s = ctx->priv;
  149. AVFilterLink *inlink = ctx->inputs[0];
  150. int ret = 0;
  151. ret = ff_request_frame(ctx->inputs[0]);
  152. if (ret == AVERROR_EOF && !s->flushed) {
  153. AVFrame *out = ff_get_audio_buffer(inlink, 1);
  154. int nb_samples;
  155. if (!out)
  156. return AVERROR(ENOMEM);
  157. rubberband_process(s->rbs, (const float *const *)out->data, 1, 1);
  158. av_frame_free(&out);
  159. s->flushed = 1;
  160. nb_samples = rubberband_available(s->rbs);
  161. if (nb_samples > 0) {
  162. out = ff_get_audio_buffer(inlink, nb_samples);
  163. if (!out)
  164. return AVERROR(ENOMEM);
  165. out->pts = av_rescale_q(s->nb_samples_out,
  166. (AVRational){ 1, outlink->sample_rate },
  167. outlink->time_base);
  168. nb_samples = rubberband_retrieve(s->rbs, (float *const *)out->data, nb_samples);
  169. out->nb_samples = nb_samples;
  170. ret = ff_filter_frame(outlink, out);
  171. s->nb_samples_out += nb_samples;
  172. }
  173. av_log(ctx, AV_LOG_DEBUG, "nb_samples_in %"PRId64" nb_samples_out %"PRId64"\n",
  174. s->nb_samples_in, s->nb_samples_out);
  175. }
  176. return ret;
  177. }
  178. static const AVFilterPad rubberband_inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_AUDIO,
  182. .config_props = config_input,
  183. .filter_frame = filter_frame,
  184. },
  185. { NULL }
  186. };
  187. static const AVFilterPad rubberband_outputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_AUDIO,
  191. .request_frame = request_frame,
  192. },
  193. { NULL }
  194. };
  195. AVFilter ff_af_rubberband = {
  196. .name = "rubberband",
  197. .description = NULL_IF_CONFIG_SMALL("Apply time-stretching and pitch-shifting."),
  198. .query_formats = query_formats,
  199. .priv_size = sizeof(RubberBandContext),
  200. .priv_class = &rubberband_class,
  201. .uninit = uninit,
  202. .inputs = rubberband_inputs,
  203. .outputs = rubberband_outputs,
  204. };