resample_template.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
  27. int dst_index, i;
  28. int index= c->index;
  29. int frac= c->frac;
  30. int dst_incr_frac= c->dst_incr % c->src_incr;
  31. int dst_incr= c->dst_incr / c->src_incr;
  32. int compensation_distance= c->compensation_distance;
  33. av_assert1(c->filter_shift == FILTER_SHIFT);
  34. av_assert1(c->felem_size == sizeof(FELEM));
  35. if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
  36. int64_t index2= ((int64_t)index)<<32;
  37. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
  38. dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
  39. for(dst_index=0; dst_index < dst_size; dst_index++){
  40. dst[dst_index] = src[index2>>32];
  41. index2 += incr;
  42. }
  43. index += dst_index * dst_incr;
  44. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  45. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  46. }else if(compensation_distance == 0 && !c->linear && index >= 0){
  47. for(dst_index=0; dst_index < dst_size; dst_index++){
  48. FELEM *filter= ((FELEM*)c->filter_bank) + c->filter_alloc*(index & c->phase_mask);
  49. int sample_index= index >> c->phase_shift;
  50. if(sample_index + c->filter_length > src_size){
  51. break;
  52. }else{
  53. #ifdef COMMON_CORE
  54. COMMON_CORE
  55. #else
  56. FELEM2 val=0;
  57. for(i=0; i<c->filter_length; i++){
  58. val += src[sample_index + i] * (FELEM2)filter[i];
  59. }
  60. OUT(dst[dst_index], val);
  61. #endif
  62. }
  63. frac += dst_incr_frac;
  64. index += dst_incr;
  65. if(frac >= c->src_incr){
  66. frac -= c->src_incr;
  67. index++;
  68. }
  69. }
  70. }else{
  71. for(dst_index=0; dst_index < dst_size; dst_index++){
  72. FELEM *filter= ((FELEM*)c->filter_bank) + c->filter_alloc*(index & c->phase_mask);
  73. int sample_index= index >> c->phase_shift;
  74. FELEM2 val=0;
  75. if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
  76. break;
  77. }else if(sample_index < 0){
  78. for(i=0; i<c->filter_length; i++)
  79. val += src[FFABS(sample_index + i)] * filter[i];
  80. }else if(c->linear){
  81. FELEM2 v2=0;
  82. for(i=0; i<c->filter_length; i++){
  83. val += src[sample_index + i] * (FELEM2)filter[i];
  84. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
  85. }
  86. val+=(v2-val)*(FELEML)frac / c->src_incr;
  87. }else{
  88. for(i=0; i<c->filter_length; i++){
  89. val += src[sample_index + i] * (FELEM2)filter[i];
  90. }
  91. }
  92. OUT(dst[dst_index], val);
  93. frac += dst_incr_frac;
  94. index += dst_incr;
  95. if(frac >= c->src_incr){
  96. frac -= c->src_incr;
  97. index++;
  98. }
  99. if(dst_index + 1 == compensation_distance){
  100. compensation_distance= 0;
  101. dst_incr_frac= c->ideal_dst_incr % c->src_incr;
  102. dst_incr= c->ideal_dst_incr / c->src_incr;
  103. }
  104. }
  105. }
  106. *consumed= FFMAX(index, 0) >> c->phase_shift;
  107. if(index>=0) index &= c->phase_mask;
  108. if(compensation_distance){
  109. compensation_distance -= dst_index;
  110. av_assert1(compensation_distance > 0);
  111. }
  112. if(update_ctx){
  113. c->frac= frac;
  114. c->index= index;
  115. c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
  116. c->compensation_distance= compensation_distance;
  117. }
  118. #if 0
  119. if(update_ctx && !c->compensation_distance){
  120. #undef rand
  121. av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
  122. av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
  123. }
  124. #endif
  125. return dst_index;
  126. }