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.

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