resample_template.c 6.9 KB

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