af_rubberband.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "filters.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. typedef struct RubberBandContext {
  28. const AVClass *class;
  29. RubberBandState rbs;
  30. double tempo, pitch;
  31. int transients, detector, phase, window,
  32. smoothing, formant, opitch, channels;
  33. int64_t nb_samples_out;
  34. int64_t nb_samples_in;
  35. int64_t first_pts;
  36. int nb_samples;
  37. } RubberBandContext;
  38. #define OFFSET(x) offsetof(RubberBandContext, x)
  39. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  40. static const AVOption rubberband_options[] = {
  41. { "tempo", "set tempo scale factor", OFFSET(tempo), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, A },
  42. { "pitch", "set pitch scale factor", OFFSET(pitch), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, A },
  43. { "transients", "set transients", OFFSET(transients), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "transients" },
  44. { "crisp", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsCrisp}, 0, 0, A, "transients" },
  45. { "mixed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsMixed}, 0, 0, A, "transients" },
  46. { "smooth", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsSmooth}, 0, 0, A, "transients" },
  47. { "detector", "set detector", OFFSET(detector), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "detector" },
  48. { "compound", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorCompound}, 0, 0, A, "detector" },
  49. { "percussive", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorPercussive}, 0, 0, A, "detector" },
  50. { "soft", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorSoft}, 0, 0, A, "detector" },
  51. { "phase", "set phase", OFFSET(phase), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "phase" },
  52. { "laminar", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseLaminar}, 0, 0, A, "phase" },
  53. { "independent", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseIndependent}, 0, 0, A, "phase" },
  54. { "window", "set window", OFFSET(window), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "window" },
  55. { "standard", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowStandard}, 0, 0, A, "window" },
  56. { "short", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowShort}, 0, 0, A, "window" },
  57. { "long", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowLong}, 0, 0, A, "window" },
  58. { "smoothing", "set smoothing", OFFSET(smoothing), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "smoothing" },
  59. { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOff}, 0, 0, A, "smoothing" },
  60. { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOn}, 0, 0, A, "smoothing" },
  61. { "formant", "set formant", OFFSET(formant), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "formant" },
  62. { "shifted", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantShifted}, 0, 0, A, "formant" },
  63. { "preserved", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantPreserved}, 0, 0, A, "formant" },
  64. { "pitchq", "set pitch quality", OFFSET(opitch), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "pitch" },
  65. { "quality", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighQuality}, 0, 0, A, "pitch" },
  66. { "speed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighSpeed}, 0, 0, A, "pitch" },
  67. { "consistency", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighConsistency}, 0, 0, A, "pitch" },
  68. { "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, "channels" },
  69. { "apart", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsApart}, 0, 0, A, "channels" },
  70. { "together", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsTogether}, 0, 0, A, "channels" },
  71. { NULL },
  72. };
  73. AVFILTER_DEFINE_CLASS(rubberband);
  74. static av_cold void uninit(AVFilterContext *ctx)
  75. {
  76. RubberBandContext *s = ctx->priv;
  77. if (s->rbs)
  78. rubberband_delete(s->rbs);
  79. }
  80. static int query_formats(AVFilterContext *ctx)
  81. {
  82. AVFilterFormats *formats = NULL;
  83. AVFilterChannelLayouts *layouts = NULL;
  84. static const enum AVSampleFormat sample_fmts[] = {
  85. AV_SAMPLE_FMT_FLTP,
  86. AV_SAMPLE_FMT_NONE,
  87. };
  88. int ret;
  89. layouts = ff_all_channel_counts();
  90. if (!layouts)
  91. return AVERROR(ENOMEM);
  92. ret = ff_set_common_channel_layouts(ctx, layouts);
  93. if (ret < 0)
  94. return ret;
  95. formats = ff_make_format_list(sample_fmts);
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. ret = ff_set_common_formats(ctx, formats);
  99. if (ret < 0)
  100. return ret;
  101. formats = ff_all_samplerates();
  102. if (!formats)
  103. return AVERROR(ENOMEM);
  104. return ff_set_common_samplerates(ctx, formats);
  105. }
  106. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  107. {
  108. RubberBandContext *s = inlink->dst->priv;
  109. AVFilterLink *outlink = inlink->dst->outputs[0];
  110. AVFrame *out;
  111. int ret = 0, nb_samples;
  112. if (s->first_pts == AV_NOPTS_VALUE)
  113. s->first_pts = in->pts;
  114. rubberband_process(s->rbs, (const float *const *)in->data, in->nb_samples, ff_outlink_get_status(inlink));
  115. s->nb_samples_in += in->nb_samples;
  116. nb_samples = rubberband_available(s->rbs);
  117. if (nb_samples > 0) {
  118. out = ff_get_audio_buffer(outlink, nb_samples);
  119. if (!out) {
  120. av_frame_free(&in);
  121. return AVERROR(ENOMEM);
  122. }
  123. out->pts = s->first_pts + av_rescale_q(s->nb_samples_out,
  124. (AVRational){ 1, outlink->sample_rate },
  125. outlink->time_base);
  126. nb_samples = rubberband_retrieve(s->rbs, (float *const *)out->data, nb_samples);
  127. out->nb_samples = nb_samples;
  128. ret = ff_filter_frame(outlink, out);
  129. s->nb_samples_out += nb_samples;
  130. }
  131. av_frame_free(&in);
  132. return ret < 0 ? ret : nb_samples;
  133. }
  134. static int config_input(AVFilterLink *inlink)
  135. {
  136. AVFilterContext *ctx = inlink->dst;
  137. RubberBandContext *s = ctx->priv;
  138. int opts = s->transients|s->detector|s->phase|s->window|
  139. s->smoothing|s->formant|s->opitch|s->channels|
  140. RubberBandOptionProcessRealTime;
  141. if (s->rbs)
  142. rubberband_delete(s->rbs);
  143. s->rbs = rubberband_new(inlink->sample_rate, inlink->channels, opts, 1. / s->tempo, s->pitch);
  144. if (!s->rbs)
  145. return AVERROR(ENOMEM);
  146. s->nb_samples = rubberband_get_samples_required(s->rbs);
  147. s->first_pts = AV_NOPTS_VALUE;
  148. return 0;
  149. }
  150. static int activate(AVFilterContext *ctx)
  151. {
  152. AVFilterLink *inlink = ctx->inputs[0];
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. RubberBandContext *s = ctx->priv;
  155. AVFrame *in = NULL;
  156. int ret;
  157. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  158. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  159. if (ret < 0)
  160. return ret;
  161. if (ret > 0) {
  162. ret = filter_frame(inlink, in);
  163. if (ret != 0)
  164. return ret;
  165. }
  166. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  167. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  168. return FFERROR_NOT_READY;
  169. }
  170. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  171. char *res, int res_len, int flags)
  172. {
  173. RubberBandContext *s = ctx->priv;
  174. if (!strcmp(cmd, "tempo")) {
  175. double arg;
  176. sscanf(args, "%lf", &arg);
  177. if (arg < 0.01 || arg > 100) {
  178. av_log(ctx, AV_LOG_ERROR,
  179. "Tempo scale factor '%f' out of range\n", arg);
  180. return AVERROR(EINVAL);
  181. }
  182. rubberband_set_time_ratio(s->rbs, 1. / arg);
  183. }
  184. if (!strcmp(cmd, "pitch")) {
  185. double arg;
  186. sscanf(args, "%lf", &arg);
  187. if (arg < 0.01 || arg > 100) {
  188. av_log(ctx, AV_LOG_ERROR,
  189. "Pitch scale factor '%f' out of range\n", arg);
  190. return AVERROR(EINVAL);
  191. }
  192. rubberband_set_pitch_scale(s->rbs, arg);
  193. }
  194. return 0;
  195. }
  196. static const AVFilterPad rubberband_inputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_AUDIO,
  200. .config_props = config_input,
  201. },
  202. { NULL }
  203. };
  204. static const AVFilterPad rubberband_outputs[] = {
  205. {
  206. .name = "default",
  207. .type = AVMEDIA_TYPE_AUDIO,
  208. },
  209. { NULL }
  210. };
  211. AVFilter ff_af_rubberband = {
  212. .name = "rubberband",
  213. .description = NULL_IF_CONFIG_SMALL("Apply time-stretching and pitch-shifting."),
  214. .query_formats = query_formats,
  215. .priv_size = sizeof(RubberBandContext),
  216. .priv_class = &rubberband_class,
  217. .uninit = uninit,
  218. .activate = activate,
  219. .inputs = rubberband_inputs,
  220. .outputs = rubberband_outputs,
  221. .process_command = process_command,
  222. };