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.

249 lines
10KB

  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. AVFilterContext *ctx = inlink->dst;
  110. RubberBandContext *s = ctx->priv;
  111. AVFilterLink *outlink = ctx->outputs[0];
  112. AVFrame *out;
  113. int ret = 0, nb_samples;
  114. if (s->first_pts == AV_NOPTS_VALUE)
  115. s->first_pts = in->pts;
  116. rubberband_process(s->rbs, (const float *const *)in->data, in->nb_samples, ff_outlink_get_status(inlink));
  117. s->nb_samples_in += in->nb_samples;
  118. nb_samples = rubberband_available(s->rbs);
  119. if (nb_samples > 0) {
  120. out = ff_get_audio_buffer(outlink, nb_samples);
  121. if (!out) {
  122. av_frame_free(&in);
  123. return AVERROR(ENOMEM);
  124. }
  125. out->pts = s->first_pts + av_rescale_q(s->nb_samples_out,
  126. (AVRational){ 1, outlink->sample_rate },
  127. outlink->time_base);
  128. nb_samples = rubberband_retrieve(s->rbs, (float *const *)out->data, nb_samples);
  129. out->nb_samples = nb_samples;
  130. ret = ff_filter_frame(outlink, out);
  131. s->nb_samples_out += nb_samples;
  132. }
  133. av_frame_free(&in);
  134. if (ff_inlink_queued_samples(inlink) >= s->nb_samples)
  135. ff_filter_set_ready(ctx, 100);
  136. return ret < 0 ? ret : nb_samples;
  137. }
  138. static int config_input(AVFilterLink *inlink)
  139. {
  140. AVFilterContext *ctx = inlink->dst;
  141. RubberBandContext *s = ctx->priv;
  142. int opts = s->transients|s->detector|s->phase|s->window|
  143. s->smoothing|s->formant|s->opitch|s->channels|
  144. RubberBandOptionProcessRealTime;
  145. if (s->rbs)
  146. rubberband_delete(s->rbs);
  147. s->rbs = rubberband_new(inlink->sample_rate, inlink->channels, opts, 1. / s->tempo, s->pitch);
  148. if (!s->rbs)
  149. return AVERROR(ENOMEM);
  150. s->nb_samples = rubberband_get_samples_required(s->rbs);
  151. s->first_pts = AV_NOPTS_VALUE;
  152. return 0;
  153. }
  154. static int activate(AVFilterContext *ctx)
  155. {
  156. AVFilterLink *inlink = ctx->inputs[0];
  157. AVFilterLink *outlink = ctx->outputs[0];
  158. RubberBandContext *s = ctx->priv;
  159. AVFrame *in = NULL;
  160. int ret;
  161. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  162. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  163. if (ret < 0)
  164. return ret;
  165. if (ret > 0) {
  166. ret = filter_frame(inlink, in);
  167. if (ret != 0)
  168. return ret;
  169. }
  170. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  171. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  172. return FFERROR_NOT_READY;
  173. }
  174. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  175. char *res, int res_len, int flags)
  176. {
  177. RubberBandContext *s = ctx->priv;
  178. int ret;
  179. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  180. if (ret < 0)
  181. return ret;
  182. rubberband_set_time_ratio(s->rbs, 1. / s->tempo);
  183. rubberband_set_pitch_scale(s->rbs, s->pitch);
  184. s->nb_samples = rubberband_get_samples_required(s->rbs);
  185. return 0;
  186. }
  187. static const AVFilterPad rubberband_inputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_AUDIO,
  191. .config_props = config_input,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad rubberband_outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_AUDIO,
  199. },
  200. { NULL }
  201. };
  202. AVFilter ff_af_rubberband = {
  203. .name = "rubberband",
  204. .description = NULL_IF_CONFIG_SMALL("Apply time-stretching and pitch-shifting."),
  205. .query_formats = query_formats,
  206. .priv_size = sizeof(RubberBandContext),
  207. .priv_class = &rubberband_class,
  208. .uninit = uninit,
  209. .activate = activate,
  210. .inputs = rubberband_inputs,
  211. .outputs = rubberband_outputs,
  212. .process_command = process_command,
  213. };