2
0

resample_template.c 5.5 KB

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