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.

182 lines
4.9KB

  1. /*
  2. * audio resampling
  3. * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
  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
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #if defined(TEMPLATE_RESAMPLE_DBL)
  27. # define RENAME(N) N ## _double
  28. # define FILTER_SHIFT 0
  29. # define DELEM double
  30. # define FELEM double
  31. # define FELEM2 double
  32. # define OUT(d, v) d = v
  33. #elif defined(TEMPLATE_RESAMPLE_FLT)
  34. # define RENAME(N) N ## _float
  35. # define FILTER_SHIFT 0
  36. # define DELEM float
  37. # define FELEM float
  38. # define FELEM2 float
  39. # define OUT(d, v) d = v
  40. #elif defined(TEMPLATE_RESAMPLE_S32)
  41. # define RENAME(N) N ## _int32
  42. # define FILTER_SHIFT 30
  43. # define DELEM int32_t
  44. # define FELEM int32_t
  45. # define FELEM2 int64_t
  46. # define FELEM_MAX INT32_MAX
  47. # define FELEM_MIN INT32_MIN
  48. # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  49. d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
  50. #elif defined(TEMPLATE_RESAMPLE_S16)
  51. # define RENAME(N) N ## _int16
  52. # define FILTER_SHIFT 15
  53. # define DELEM int16_t
  54. # define FELEM int16_t
  55. # define FELEM2 int32_t
  56. # define FELEML int64_t
  57. # define FELEM_MAX INT16_MAX
  58. # define FELEM_MIN INT16_MIN
  59. # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  60. d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
  61. #endif
  62. static void RENAME(resample_one)(DELEM *dst, const DELEM *src,
  63. int dst_size, int64_t index2, int64_t incr)
  64. {
  65. int dst_index;
  66. for (dst_index = 0; dst_index < dst_size; dst_index++) {
  67. dst[dst_index] = src[index2 >> 32];
  68. index2 += incr;
  69. }
  70. }
  71. static int RENAME(resample_common)(ResampleContext *c,
  72. DELEM *dst, const DELEM *src,
  73. int n, int update_ctx)
  74. {
  75. int dst_index;
  76. int index= c->index;
  77. int frac= c->frac;
  78. int sample_index = index >> c->phase_shift;
  79. index &= c->phase_mask;
  80. for (dst_index = 0; dst_index < n; dst_index++) {
  81. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  82. FELEM2 val=0;
  83. int i;
  84. for (i = 0; i < c->filter_length; i++) {
  85. val += src[sample_index + i] * (FELEM2)filter[i];
  86. }
  87. OUT(dst[dst_index], val);
  88. frac += c->dst_incr_mod;
  89. index += c->dst_incr_div;
  90. if (frac >= c->src_incr) {
  91. frac -= c->src_incr;
  92. index++;
  93. }
  94. sample_index += index >> c->phase_shift;
  95. index &= c->phase_mask;
  96. }
  97. if(update_ctx){
  98. c->frac= frac;
  99. c->index= index;
  100. }
  101. return sample_index;
  102. }
  103. static int RENAME(resample_linear)(ResampleContext *c,
  104. DELEM *dst, const DELEM *src,
  105. int n, int update_ctx)
  106. {
  107. int dst_index;
  108. int index= c->index;
  109. int frac= c->frac;
  110. int sample_index = index >> c->phase_shift;
  111. #if FILTER_SHIFT == 0
  112. double inv_src_incr = 1.0 / c->src_incr;
  113. #endif
  114. index &= c->phase_mask;
  115. for (dst_index = 0; dst_index < n; dst_index++) {
  116. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  117. FELEM2 val=0, v2 = 0;
  118. int i;
  119. for (i = 0; i < c->filter_length; i++) {
  120. val += src[sample_index + i] * (FELEM2)filter[i];
  121. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  122. }
  123. #ifdef FELEML
  124. val += (v2 - val) * (FELEML) frac / c->src_incr;
  125. #else
  126. # if FILTER_SHIFT == 0
  127. val += (v2 - val) * inv_src_incr * frac;
  128. # else
  129. val += (v2 - val) / c->src_incr * frac;
  130. # endif
  131. #endif
  132. OUT(dst[dst_index], val);
  133. frac += c->dst_incr_mod;
  134. index += c->dst_incr_div;
  135. if (frac >= c->src_incr) {
  136. frac -= c->src_incr;
  137. index++;
  138. }
  139. sample_index += index >> c->phase_shift;
  140. index &= c->phase_mask;
  141. }
  142. if(update_ctx){
  143. c->frac= frac;
  144. c->index= index;
  145. }
  146. return sample_index;
  147. }
  148. #undef RENAME
  149. #undef FILTER_SHIFT
  150. #undef DELEM
  151. #undef FELEM
  152. #undef FELEM2
  153. #undef FELEML
  154. #undef FELEM_MAX
  155. #undef FELEM_MIN
  156. #undef OUT