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.

244 lines
7.8KB

  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. # define FILTER_SHIFT 0
  44. # define DELEM float
  45. # define FELEM float
  46. # define FELEM2 float
  47. # define FELEML float
  48. # define OUT(d, v) d = v
  49. # if defined(TEMPLATE_RESAMPLE_FLT)
  50. # define RENAME(N) N ## _float
  51. # elif defined(TEMPLATE_RESAMPLE_FLT_SSE)
  52. # define COMMON_CORE COMMON_CORE_FLT_SSE
  53. # define LINEAR_CORE LINEAR_CORE_FLT_SSE
  54. # define RENAME(N) N ## _float_sse
  55. # endif
  56. #elif defined(TEMPLATE_RESAMPLE_S32)
  57. # define RENAME(N) N ## _int32
  58. # define FILTER_SHIFT 30
  59. # define DELEM int32_t
  60. # define FELEM int32_t
  61. # define FELEM2 int64_t
  62. # define FELEML int64_t
  63. # define FELEM_MAX INT32_MAX
  64. # define FELEM_MIN INT32_MIN
  65. # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  66. d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
  67. #elif defined(TEMPLATE_RESAMPLE_S16) \
  68. || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
  69. || defined(TEMPLATE_RESAMPLE_S16_SSE2)
  70. # define FILTER_SHIFT 15
  71. # define DELEM int16_t
  72. # define FELEM int16_t
  73. # define FELEM2 int32_t
  74. # define FELEML int64_t
  75. # define FELEM_MAX INT16_MAX
  76. # define FELEM_MIN INT16_MIN
  77. # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  78. d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
  79. # if defined(TEMPLATE_RESAMPLE_S16)
  80. # define RENAME(N) N ## _int16
  81. # elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
  82. # define COMMON_CORE COMMON_CORE_INT16_MMX2
  83. # define LINEAR_CORE LINEAR_CORE_INT16_MMX2
  84. # define RENAME(N) N ## _int16_mmx2
  85. # elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
  86. # define COMMON_CORE COMMON_CORE_INT16_SSE2
  87. # define LINEAR_CORE LINEAR_CORE_INT16_SSE2
  88. # define RENAME(N) N ## _int16_sse2
  89. # endif
  90. #endif
  91. int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
  92. int dst_index, i;
  93. int index= c->index;
  94. int frac= c->frac;
  95. int dst_incr_frac= c->dst_incr % c->src_incr;
  96. int dst_incr= c->dst_incr / c->src_incr;
  97. int compensation_distance= c->compensation_distance;
  98. av_assert1(c->filter_shift == FILTER_SHIFT);
  99. av_assert1(c->felem_size == sizeof(FELEM));
  100. if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
  101. int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
  102. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
  103. int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
  104. dst_size= FFMIN(dst_size, new_size);
  105. for(dst_index=0; dst_index < dst_size; dst_index++){
  106. dst[dst_index] = src[index2>>32];
  107. index2 += incr;
  108. }
  109. index += dst_index * dst_incr;
  110. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  111. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  112. av_assert2(index >= 0);
  113. *consumed= index;
  114. index = 0;
  115. }else if(compensation_distance == 0 && !c->linear && index >= 0){
  116. int sample_index = 0;
  117. for(dst_index=0; dst_index < dst_size; dst_index++){
  118. FELEM *filter;
  119. sample_index += index >> c->phase_shift;
  120. index &= c->phase_mask;
  121. filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
  122. if(sample_index + c->filter_length > src_size){
  123. break;
  124. }else{
  125. #ifdef COMMON_CORE
  126. COMMON_CORE
  127. #else
  128. FELEM2 val=0;
  129. for(i=0; i<c->filter_length; i++){
  130. val += src[sample_index + i] * (FELEM2)filter[i];
  131. }
  132. OUT(dst[dst_index], val);
  133. #endif
  134. }
  135. frac += dst_incr_frac;
  136. index += dst_incr;
  137. if(frac >= c->src_incr){
  138. frac -= c->src_incr;
  139. index++;
  140. }
  141. }
  142. *consumed = sample_index;
  143. }else{
  144. int sample_index = 0;
  145. for(dst_index=0; dst_index < dst_size; dst_index++){
  146. FELEM *filter;
  147. FELEM2 val=0;
  148. sample_index += index >> c->phase_shift;
  149. index &= c->phase_mask;
  150. filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
  151. if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
  152. break;
  153. }else if(sample_index < 0){
  154. for(i=0; i<c->filter_length; i++)
  155. val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
  156. OUT(dst[dst_index], val);
  157. }else if(c->linear){
  158. FELEM2 v2=0;
  159. #ifdef LINEAR_CORE
  160. LINEAR_CORE
  161. #else
  162. for(i=0; i<c->filter_length; i++){
  163. val += src[sample_index + i] * (FELEM2)filter[i];
  164. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  165. }
  166. #endif
  167. val+=(v2-val)*(FELEML)frac / c->src_incr;
  168. OUT(dst[dst_index], val);
  169. }else{
  170. #ifdef COMMON_CORE
  171. COMMON_CORE
  172. #else
  173. for(i=0; i<c->filter_length; i++){
  174. val += src[sample_index + i] * (FELEM2)filter[i];
  175. }
  176. OUT(dst[dst_index], val);
  177. #endif
  178. }
  179. frac += dst_incr_frac;
  180. index += dst_incr;
  181. if(frac >= c->src_incr){
  182. frac -= c->src_incr;
  183. index++;
  184. }
  185. if(dst_index + 1 == compensation_distance){
  186. compensation_distance= 0;
  187. dst_incr_frac= c->ideal_dst_incr % c->src_incr;
  188. dst_incr= c->ideal_dst_incr / c->src_incr;
  189. }
  190. }
  191. *consumed= FFMAX(sample_index, 0);
  192. index += FFMIN(sample_index, 0) << c->phase_shift;
  193. if(compensation_distance){
  194. compensation_distance -= dst_index;
  195. av_assert1(compensation_distance > 0);
  196. }
  197. }
  198. if(update_ctx){
  199. c->frac= frac;
  200. c->index= index;
  201. c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
  202. c->compensation_distance= compensation_distance;
  203. }
  204. return dst_index;
  205. }
  206. #undef COMMON_CORE
  207. #undef LINEAR_CORE
  208. #undef RENAME
  209. #undef FILTER_SHIFT
  210. #undef DELEM
  211. #undef FELEM
  212. #undef FELEM2
  213. #undef FELEML
  214. #undef FELEM_MAX
  215. #undef FELEM_MIN
  216. #undef OUT