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.

233 lines
7.5KB

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