resample_template.c 5.2 KB

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