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.

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