resample_template.c 5.8 KB

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