resample_template.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : 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 = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : 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 = index >> c->phase_shift;
  83. index &= c->phase_mask;
  84. for (dst_index = 0; dst_index < n; dst_index++) {
  85. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  86. FELEM2 val=0;
  87. int i;
  88. for (i = 0; i < c->filter_length; i++) {
  89. val += src[sample_index + i] * (FELEM2)filter[i];
  90. }
  91. OUT(dst[dst_index], val);
  92. frac += c->dst_incr_mod;
  93. index += c->dst_incr_div;
  94. if (frac >= c->src_incr) {
  95. frac -= c->src_incr;
  96. index++;
  97. }
  98. sample_index += index >> c->phase_shift;
  99. index &= c->phase_mask;
  100. }
  101. if(update_ctx){
  102. c->frac= frac;
  103. c->index= index;
  104. }
  105. return sample_index;
  106. }
  107. static int RENAME(resample_linear)(ResampleContext *c,
  108. void *dest, const void *source,
  109. int n, int update_ctx)
  110. {
  111. DELEM *dst = dest;
  112. const DELEM *src = source;
  113. int dst_index;
  114. int index= c->index;
  115. int frac= c->frac;
  116. int sample_index = index >> c->phase_shift;
  117. #if FILTER_SHIFT == 0
  118. double inv_src_incr = 1.0 / c->src_incr;
  119. #endif
  120. index &= c->phase_mask;
  121. for (dst_index = 0; dst_index < n; dst_index++) {
  122. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
  123. FELEM2 val=0, v2 = 0;
  124. int i;
  125. for (i = 0; i < c->filter_length; i++) {
  126. val += src[sample_index + i] * (FELEM2)filter[i];
  127. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  128. }
  129. #ifdef FELEML
  130. val += (v2 - val) * (FELEML) frac / c->src_incr;
  131. #else
  132. # if FILTER_SHIFT == 0
  133. val += (v2 - val) * inv_src_incr * frac;
  134. # else
  135. val += (v2 - val) / c->src_incr * frac;
  136. # endif
  137. #endif
  138. OUT(dst[dst_index], val);
  139. frac += c->dst_incr_mod;
  140. index += c->dst_incr_div;
  141. if (frac >= c->src_incr) {
  142. frac -= c->src_incr;
  143. index++;
  144. }
  145. sample_index += index >> c->phase_shift;
  146. index &= c->phase_mask;
  147. }
  148. if(update_ctx){
  149. c->frac= frac;
  150. c->index= index;
  151. }
  152. return sample_index;
  153. }
  154. #undef RENAME
  155. #undef FILTER_SHIFT
  156. #undef DELEM
  157. #undef FELEM
  158. #undef FELEM2
  159. #undef FELEML
  160. #undef FELEM_MAX
  161. #undef FELEM_MIN
  162. #undef OUT