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.

184 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. #if DO_RESAMPLE_ONE
  63. static void RENAME(resample_one)(DELEM *dst, const DELEM *src,
  64. int dst_size, int64_t index2, int64_t incr)
  65. {
  66. int dst_index;
  67. for (dst_index = 0; dst_index < dst_size; dst_index++) {
  68. dst[dst_index] = src[index2 >> 32];
  69. index2 += incr;
  70. }
  71. }
  72. #endif
  73. int RENAME(swri_resample_common)(ResampleContext *c,
  74. DELEM *dst, const DELEM *src,
  75. int n, int update_ctx)
  76. {
  77. int dst_index;
  78. int index= c->index;
  79. int frac= c->frac;
  80. int sample_index = index >> c->phase_shift;
  81. index &= c->phase_mask;
  82. for (dst_index = 0; dst_index < n; dst_index++) {
  83. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  84. FELEM2 val=0;
  85. int i;
  86. for (i = 0; i < c->filter_length; i++) {
  87. val += src[sample_index + i] * (FELEM2)filter[i];
  88. }
  89. OUT(dst[dst_index], val);
  90. frac += c->dst_incr_mod;
  91. index += c->dst_incr_div;
  92. if (frac >= c->src_incr) {
  93. frac -= c->src_incr;
  94. index++;
  95. }
  96. sample_index += index >> c->phase_shift;
  97. index &= c->phase_mask;
  98. }
  99. if(update_ctx){
  100. c->frac= frac;
  101. c->index= index;
  102. }
  103. return sample_index;
  104. }
  105. int RENAME(swri_resample_linear)(ResampleContext *c,
  106. DELEM *dst, const DELEM *src,
  107. int n, int update_ctx)
  108. {
  109. int dst_index;
  110. int index= c->index;
  111. int frac= c->frac;
  112. int sample_index = index >> c->phase_shift;
  113. #if FILTER_SHIFT == 0
  114. double inv_src_incr = 1.0 / c->src_incr;
  115. #endif
  116. index &= c->phase_mask;
  117. for (dst_index = 0; dst_index < n; dst_index++) {
  118. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  119. FELEM2 val=0, v2 = 0;
  120. int i;
  121. for (i = 0; i < c->filter_length; i++) {
  122. val += src[sample_index + i] * (FELEM2)filter[i];
  123. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  124. }
  125. #ifdef FELEML
  126. val += (v2 - val) * (FELEML) frac / c->src_incr;
  127. #else
  128. # if FILTER_SHIFT == 0
  129. val += (v2 - val) * inv_src_incr * frac;
  130. # else
  131. val += (v2 - val) / c->src_incr * frac;
  132. # endif
  133. #endif
  134. OUT(dst[dst_index], val);
  135. frac += c->dst_incr_mod;
  136. index += c->dst_incr_div;
  137. if (frac >= c->src_incr) {
  138. frac -= c->src_incr;
  139. index++;
  140. }
  141. sample_index += index >> c->phase_shift;
  142. index &= c->phase_mask;
  143. }
  144. if(update_ctx){
  145. c->frac= frac;
  146. c->index= index;
  147. }
  148. return sample_index;
  149. }
  150. #undef RENAME
  151. #undef FILTER_SHIFT
  152. #undef DELEM
  153. #undef FELEM
  154. #undef FELEM2
  155. #undef FELEML
  156. #undef FELEM_MAX
  157. #undef FELEM_MIN
  158. #undef OUT