resample_template.c 6.9 KB

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