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.

202 lines
5.4KB

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