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.

283 lines
9.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 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. int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
  97. int dst_index, i;
  98. int index= c->index;
  99. int frac= c->frac;
  100. int dst_incr_frac= c->dst_incr % c->src_incr;
  101. int dst_incr= c->dst_incr / c->src_incr;
  102. int compensation_distance= c->compensation_distance;
  103. av_assert1(c->filter_shift == FILTER_SHIFT);
  104. av_assert1(c->felem_size == sizeof(FELEM));
  105. if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
  106. int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
  107. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
  108. int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
  109. dst_size= FFMIN(dst_size, new_size);
  110. for(dst_index=0; dst_index < dst_size; dst_index++){
  111. dst[dst_index] = src[index2>>32];
  112. index2 += incr;
  113. }
  114. index += dst_index * dst_incr;
  115. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  116. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  117. av_assert2(index >= 0);
  118. *consumed= index;
  119. index = 0;
  120. } else if (compensation_distance == 0 &&
  121. index >= 0 &&
  122. src_size*(int64_t)c->src_incr < (INT64_MAX >> (c->phase_shift+1))) {
  123. int64_t end_index = (1LL + src_size - c->filter_length) << c->phase_shift;
  124. int64_t delta_frac = (end_index - index) * c->src_incr - c->frac;
  125. int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
  126. int n = FFMIN(dst_size, delta_n);
  127. int sample_index;
  128. if (!c->linear) {
  129. sample_index = index >> c->phase_shift;
  130. index &= c->phase_mask;
  131. for (dst_index = 0; dst_index < n; dst_index++) {
  132. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  133. #ifdef COMMON_CORE
  134. COMMON_CORE
  135. #else
  136. FELEM2 val=0;
  137. for (i = 0; i < c->filter_length; i++) {
  138. val += src[sample_index + i] * (FELEM2)filter[i];
  139. }
  140. OUT(dst[dst_index], val);
  141. #endif
  142. frac += dst_incr_frac;
  143. index += dst_incr;
  144. if (frac >= c->src_incr) {
  145. frac -= c->src_incr;
  146. index++;
  147. }
  148. sample_index += index >> c->phase_shift;
  149. index &= c->phase_mask;
  150. }
  151. } else {
  152. sample_index = index >> c->phase_shift;
  153. index &= c->phase_mask;
  154. for (dst_index = 0; dst_index < n; dst_index++) {
  155. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  156. FELEM2 val=0, v2 = 0;
  157. #ifdef LINEAR_CORE
  158. LINEAR_CORE
  159. #else
  160. for (i = 0; i < c->filter_length; i++) {
  161. val += src[sample_index + i] * (FELEM2)filter[i];
  162. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  163. }
  164. #endif
  165. val += (v2 - val) * (FELEML) frac / c->src_incr;
  166. OUT(dst[dst_index], val);
  167. frac += dst_incr_frac;
  168. index += dst_incr;
  169. if (frac >= c->src_incr) {
  170. frac -= c->src_incr;
  171. index++;
  172. }
  173. sample_index += index >> c->phase_shift;
  174. index &= c->phase_mask;
  175. }
  176. }
  177. *consumed = sample_index;
  178. } else {
  179. int sample_index = 0;
  180. for(dst_index=0; dst_index < dst_size; dst_index++){
  181. FELEM *filter;
  182. FELEM2 val=0;
  183. sample_index += index >> c->phase_shift;
  184. index &= c->phase_mask;
  185. filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
  186. if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
  187. break;
  188. }else if(sample_index < 0){
  189. for(i=0; i<c->filter_length; i++)
  190. val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
  191. OUT(dst[dst_index], val);
  192. }else if(c->linear){
  193. FELEM2 v2=0;
  194. #ifdef LINEAR_CORE
  195. LINEAR_CORE
  196. #else
  197. for(i=0; i<c->filter_length; i++){
  198. val += src[sample_index + i] * (FELEM2)filter[i];
  199. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  200. }
  201. #endif
  202. val+=(v2-val)*(FELEML)frac / c->src_incr;
  203. OUT(dst[dst_index], val);
  204. }else{
  205. #ifdef COMMON_CORE
  206. COMMON_CORE
  207. #else
  208. for(i=0; i<c->filter_length; i++){
  209. val += src[sample_index + i] * (FELEM2)filter[i];
  210. }
  211. OUT(dst[dst_index], val);
  212. #endif
  213. }
  214. frac += dst_incr_frac;
  215. index += dst_incr;
  216. if(frac >= c->src_incr){
  217. frac -= c->src_incr;
  218. index++;
  219. }
  220. if(dst_index + 1 == compensation_distance){
  221. compensation_distance= 0;
  222. dst_incr_frac= c->ideal_dst_incr % c->src_incr;
  223. dst_incr= c->ideal_dst_incr / c->src_incr;
  224. }
  225. }
  226. *consumed= FFMAX(sample_index, 0);
  227. index += FFMIN(sample_index, 0) << c->phase_shift;
  228. if(compensation_distance){
  229. compensation_distance -= dst_index;
  230. av_assert1(compensation_distance > 0);
  231. }
  232. }
  233. if(update_ctx){
  234. c->frac= frac;
  235. c->index= index;
  236. c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
  237. c->compensation_distance= compensation_distance;
  238. }
  239. return dst_index;
  240. }
  241. #undef COMMON_CORE
  242. #undef LINEAR_CORE
  243. #undef RENAME
  244. #undef FILTER_SHIFT
  245. #undef DELEM
  246. #undef FELEM
  247. #undef FELEM2
  248. #undef FELEML
  249. #undef FELEM_MAX
  250. #undef FELEM_MIN
  251. #undef OUT