af_rubberband.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if (rubberband_available(s->rbs) > 0) {
  154. AVFrame *out = ff_get_audio_buffer(inlink, 1);
  155. int nb_samples;
  156. if (!out)
  157. return AVERROR(ENOMEM);
  158. rubberband_process(s->rbs, (const float *const *)out->data, 1, 1);
  159. av_frame_free(&out);
  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. }
  174. s->flushed = 1;
  175. av_log(ctx, AV_LOG_DEBUG, "nb_samples_in %"PRId64" nb_samples_out %"PRId64"\n",
  176. s->nb_samples_in, s->nb_samples_out);
  177. }
  178. return ret;
  179. }
  180. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  181. char *res, int res_len, int flags)
  182. {
  183. RubberBandContext *s = ctx->priv;
  184. if (!strcmp(cmd, "tempo")) {
  185. double arg;
  186. sscanf(args, "%lf", &arg);
  187. if (arg < 0.01 || arg > 100) {
  188. av_log(ctx, AV_LOG_ERROR,
  189. "Tempo scale factor '%f' out of range\n", arg);
  190. return AVERROR(EINVAL);
  191. }
  192. rubberband_set_time_ratio(s->rbs, 1. / arg);
  193. }
  194. if (!strcmp(cmd, "pitch")) {
  195. double arg;
  196. sscanf(args, "%lf", &arg);
  197. if (arg < 0.01 || arg > 100) {
  198. av_log(ctx, AV_LOG_ERROR,
  199. "Pitch scale factor '%f' out of range\n", arg);
  200. return AVERROR(EINVAL);
  201. }
  202. rubberband_set_pitch_scale(s->rbs, arg);
  203. }
  204. return 0;
  205. }
  206. static const AVFilterPad rubberband_inputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_AUDIO,
  210. .config_props = config_input,
  211. .filter_frame = filter_frame,
  212. },
  213. { NULL }
  214. };
  215. static const AVFilterPad rubberband_outputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_AUDIO,
  219. .request_frame = request_frame,
  220. },
  221. { NULL }
  222. };
  223. AVFilter ff_af_rubberband = {
  224. .name = "rubberband",
  225. .description = NULL_IF_CONFIG_SMALL("Apply time-stretching and pitch-shifting."),
  226. .query_formats = query_formats,
  227. .priv_size = sizeof(RubberBandContext),
  228. .priv_class = &rubberband_class,
  229. .uninit = uninit,
  230. .inputs = rubberband_inputs,
  231. .outputs = rubberband_outputs,
  232. .process_command = process_command,
  233. };