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.

225 lines
7.3KB

  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 RENAME(N) N ## _float_sse
  47. # endif
  48. #elif defined(TEMPLATE_RESAMPLE_S32)
  49. # define RENAME(N) N ## _int32
  50. # define FILTER_SHIFT 30
  51. # define DELEM int32_t
  52. # define FELEM int32_t
  53. # define FELEM2 int64_t
  54. # define FELEML 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 RENAME(N) N ## _int16_mmx2
  76. # elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
  77. # define COMMON_CORE COMMON_CORE_INT16_SSE2
  78. # define RENAME(N) N ## _int16_sse2
  79. # endif
  80. #endif
  81. int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
  82. int dst_index, i;
  83. int index= c->index;
  84. int frac= c->frac;
  85. int dst_incr_frac= c->dst_incr % c->src_incr;
  86. int dst_incr= c->dst_incr / c->src_incr;
  87. int compensation_distance= c->compensation_distance;
  88. av_assert1(c->filter_shift == FILTER_SHIFT);
  89. av_assert1(c->felem_size == sizeof(FELEM));
  90. if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
  91. int64_t index2= ((int64_t)index)<<32;
  92. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
  93. dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
  94. for(dst_index=0; dst_index < dst_size; dst_index++){
  95. dst[dst_index] = src[index2>>32];
  96. index2 += incr;
  97. }
  98. index += dst_index * dst_incr;
  99. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  100. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  101. av_assert2(index >= 0);
  102. *consumed= index >> c->phase_shift;
  103. index &= c->phase_mask;
  104. }else if(compensation_distance == 0 && !c->linear && index >= 0){
  105. int sample_index = 0;
  106. for(dst_index=0; dst_index < dst_size; dst_index++){
  107. FELEM *filter;
  108. sample_index += index >> c->phase_shift;
  109. index &= c->phase_mask;
  110. filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
  111. if(sample_index + c->filter_length > src_size){
  112. break;
  113. }else{
  114. #ifdef COMMON_CORE
  115. COMMON_CORE
  116. #else
  117. FELEM2 val=0;
  118. for(i=0; i<c->filter_length; i++){
  119. val += src[sample_index + i] * (FELEM2)filter[i];
  120. }
  121. OUT(dst[dst_index], val);
  122. #endif
  123. }
  124. frac += dst_incr_frac;
  125. index += dst_incr;
  126. if(frac >= c->src_incr){
  127. frac -= c->src_incr;
  128. index++;
  129. }
  130. }
  131. *consumed = sample_index;
  132. }else{
  133. int sample_index = 0;
  134. for(dst_index=0; dst_index < dst_size; dst_index++){
  135. FELEM *filter;
  136. FELEM2 val=0;
  137. sample_index += index >> c->phase_shift;
  138. index &= c->phase_mask;
  139. filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
  140. if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
  141. break;
  142. }else if(sample_index < 0){
  143. for(i=0; i<c->filter_length; i++)
  144. val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
  145. OUT(dst[dst_index], val);
  146. }else if(c->linear){
  147. FELEM2 v2=0;
  148. for(i=0; i<c->filter_length; i++){
  149. val += src[sample_index + i] * (FELEM2)filter[i];
  150. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  151. }
  152. val+=(v2-val)*(FELEML)frac / c->src_incr;
  153. OUT(dst[dst_index], val);
  154. }else{
  155. #ifdef COMMON_CORE
  156. COMMON_CORE
  157. #else
  158. for(i=0; i<c->filter_length; i++){
  159. val += src[sample_index + i] * (FELEM2)filter[i];
  160. }
  161. OUT(dst[dst_index], val);
  162. #endif
  163. }
  164. frac += dst_incr_frac;
  165. index += dst_incr;
  166. if(frac >= c->src_incr){
  167. frac -= c->src_incr;
  168. index++;
  169. }
  170. if(dst_index + 1 == compensation_distance){
  171. compensation_distance= 0;
  172. dst_incr_frac= c->ideal_dst_incr % c->src_incr;
  173. dst_incr= c->ideal_dst_incr / c->src_incr;
  174. }
  175. }
  176. *consumed= FFMAX(sample_index, 0);
  177. index += FFMIN(sample_index, 0) << c->phase_shift;
  178. if(compensation_distance){
  179. compensation_distance -= dst_index;
  180. av_assert1(compensation_distance > 0);
  181. }
  182. }
  183. if(update_ctx){
  184. c->frac= frac;
  185. c->index= index;
  186. c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
  187. c->compensation_distance= compensation_distance;
  188. }
  189. return dst_index;
  190. }
  191. #undef COMMON_CORE
  192. #undef RENAME
  193. #undef FILTER_SHIFT
  194. #undef DELEM
  195. #undef FELEM
  196. #undef FELEM2
  197. #undef FELEML
  198. #undef FELEM_MAX
  199. #undef FELEM_MIN
  200. #undef OUT