soxr_resample.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * audio resampling with soxr
  3. * Copyright (c) 2012 Rob Sykes <robs@users.sourceforge.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * audio resampling with soxr
  24. */
  25. #include "libavutil/log.h"
  26. #include "swresample_internal.h"
  27. #include <soxr.h>
  28. static struct ResampleContext *create(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
  29. double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta, double precision, int cheby, int exact_rational){
  30. soxr_error_t error;
  31. soxr_datatype_t type =
  32. format == AV_SAMPLE_FMT_S16P? SOXR_INT16_S :
  33. format == AV_SAMPLE_FMT_S16 ? SOXR_INT16_I :
  34. format == AV_SAMPLE_FMT_S32P? SOXR_INT32_S :
  35. format == AV_SAMPLE_FMT_S32 ? SOXR_INT32_I :
  36. format == AV_SAMPLE_FMT_FLTP? SOXR_FLOAT32_S :
  37. format == AV_SAMPLE_FMT_FLT ? SOXR_FLOAT32_I :
  38. format == AV_SAMPLE_FMT_DBLP? SOXR_FLOAT64_S :
  39. format == AV_SAMPLE_FMT_DBL ? SOXR_FLOAT64_I : (soxr_datatype_t)-1;
  40. soxr_io_spec_t io_spec = soxr_io_spec(type, type);
  41. soxr_quality_spec_t q_spec = soxr_quality_spec((int)((precision-2)/4), (SOXR_HI_PREC_CLOCK|SOXR_ROLLOFF_NONE)*!!cheby);
  42. q_spec.precision = precision;
  43. #if !defined SOXR_VERSION /* Deprecated @ March 2013: */
  44. q_spec.bw_pc = cutoff? FFMAX(FFMIN(cutoff,.995),.8)*100 : q_spec.bw_pc;
  45. #else
  46. q_spec.passband_end = cutoff? FFMAX(FFMIN(cutoff,.995),.8) : q_spec.passband_end;
  47. #endif
  48. soxr_delete((soxr_t)c);
  49. c = (struct ResampleContext *)
  50. soxr_create(in_rate, out_rate, 0, &error, &io_spec, &q_spec, 0);
  51. if (!c)
  52. av_log(NULL, AV_LOG_ERROR, "soxr_create: %s\n", error);
  53. return c;
  54. }
  55. static void destroy(struct ResampleContext * *c){
  56. soxr_delete((soxr_t)*c);
  57. *c = NULL;
  58. }
  59. static int flush(struct SwrContext *s){
  60. s->delayed_samples_fixup = soxr_delay((soxr_t)s->resample);
  61. soxr_process((soxr_t)s->resample, NULL, 0, NULL, NULL, 0, NULL);
  62. {
  63. float f;
  64. size_t idone, odone;
  65. soxr_process((soxr_t)s->resample, &f, 0, &idone, &f, 0, &odone);
  66. s->delayed_samples_fixup -= soxr_delay((soxr_t)s->resample);
  67. }
  68. return 0;
  69. }
  70. static int process(
  71. struct ResampleContext * c, AudioData *dst, int dst_size,
  72. AudioData *src, int src_size, int *consumed){
  73. size_t idone, odone;
  74. soxr_error_t error = soxr_set_error((soxr_t)c, soxr_set_num_channels((soxr_t)c, src->ch_count));
  75. if (!error)
  76. error = soxr_process((soxr_t)c, src->ch, (size_t)src_size,
  77. &idone, dst->ch, (size_t)dst_size, &odone);
  78. else
  79. idone = 0;
  80. *consumed = (int)idone;
  81. return error? -1 : odone;
  82. }
  83. static int64_t get_delay(struct SwrContext *s, int64_t base){
  84. double delayed_samples = soxr_delay((soxr_t)s->resample);
  85. double delay_s;
  86. if (s->flushed)
  87. delayed_samples += s->delayed_samples_fixup;
  88. delay_s = delayed_samples / s->out_sample_rate;
  89. return (int64_t)(delay_s * base + .5);
  90. }
  91. static int invert_initial_buffer(struct ResampleContext *c, AudioData *dst, const AudioData *src,
  92. int in_count, int *out_idx, int *out_sz){
  93. return 0;
  94. }
  95. static int64_t get_out_samples(struct SwrContext *s, int in_samples){
  96. double out_samples = (double)s->out_sample_rate / s->in_sample_rate * in_samples;
  97. double delayed_samples = soxr_delay((soxr_t)s->resample);
  98. if (s->flushed)
  99. delayed_samples += s->delayed_samples_fixup;
  100. return (int64_t)(out_samples + delayed_samples + 1 + .5);
  101. }
  102. struct Resampler const swri_soxr_resampler={
  103. create, destroy, process, flush, NULL /* set_compensation */, get_delay,
  104. invert_initial_buffer, get_out_samples
  105. };