resampler.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /***********************************************************************
  2. Copyright (c) 2006-2012 IETF Trust and Skype Limited. All rights reserved.
  3. This file is extracted from RFC6716. Please see that RFC for additional
  4. information.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  14. names of specific contributors, may be used to endorse or promote
  15. products derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. POSSIBILITY OF SUCH DAMAGE.
  28. ***********************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. /*
  33. * Matrix of resampling methods used:
  34. * Fs_out (kHz)
  35. * 8 12 16 24 48
  36. *
  37. * 8 C UF U UF UF
  38. * 12 AF C UF U UF
  39. * Fs_in (kHz) 16 D AF C UF UF
  40. * 24 AF D AF C U
  41. * 48 AF AF AF D C
  42. *
  43. * C -> Copy (no resampling)
  44. * D -> Allpass-based 2x downsampling
  45. * U -> Allpass-based 2x upsampling
  46. * UF -> Allpass-based 2x upsampling followed by FIR interpolation
  47. * AF -> AR2 filter followed by FIR interpolation
  48. */
  49. #include "resampler_private.h"
  50. /* Tables with delay compensation values to equalize total delay for different modes */
  51. static const opus_int8 delay_matrix_enc[ 5 ][ 3 ] = {
  52. /* in \ out 8 12 16 */
  53. /* 8 */ { 6, 0, 3 },
  54. /* 12 */ { 0, 7, 3 },
  55. /* 16 */ { 0, 1, 10 },
  56. /* 24 */ { 0, 2, 6 },
  57. /* 48 */ { 18, 10, 12 }
  58. };
  59. static const opus_int8 delay_matrix_dec[ 3 ][ 5 ] = {
  60. /* in \ out 8 12 16 24 48 */
  61. /* 8 */ { 4, 0, 2, 0, 0 },
  62. /* 12 */ { 0, 9, 4, 7, 4 },
  63. /* 16 */ { 0, 3, 12, 7, 7 }
  64. };
  65. /* Simple way to make [8000, 12000, 16000, 24000, 48000] to [0, 1, 2, 3, 4] */
  66. #define rateID(R) ( ( ( ((R)>>12) - ((R)>16000) ) >> ((R)>24000) ) - 1 )
  67. #define USE_silk_resampler_copy (0)
  68. #define USE_silk_resampler_private_up2_HQ_wrapper (1)
  69. #define USE_silk_resampler_private_IIR_FIR (2)
  70. #define USE_silk_resampler_private_down_FIR (3)
  71. /* Initialize/reset the resampler state for a given pair of input/output sampling rates */
  72. opus_int silk_resampler_init(
  73. silk_resampler_state_struct *S, /* I/O Resampler state */
  74. opus_int32 Fs_Hz_in, /* I Input sampling rate (Hz) */
  75. opus_int32 Fs_Hz_out, /* I Output sampling rate (Hz) */
  76. opus_int forEnc /* I If 1: encoder; if 0: decoder */
  77. )
  78. {
  79. opus_int up2x;
  80. /* Clear state */
  81. silk_memset( S, 0, sizeof( silk_resampler_state_struct ) );
  82. /* Input checking */
  83. if( forEnc ) {
  84. if( ( Fs_Hz_in != 8000 && Fs_Hz_in != 12000 && Fs_Hz_in != 16000 && Fs_Hz_in != 24000 && Fs_Hz_in != 48000 ) ||
  85. ( Fs_Hz_out != 8000 && Fs_Hz_out != 12000 && Fs_Hz_out != 16000 ) ) {
  86. silk_assert( 0 );
  87. return -1;
  88. }
  89. S->inputDelay = delay_matrix_enc[ rateID( Fs_Hz_in ) ][ rateID( Fs_Hz_out ) ];
  90. } else {
  91. if( ( Fs_Hz_in != 8000 && Fs_Hz_in != 12000 && Fs_Hz_in != 16000 ) ||
  92. ( Fs_Hz_out != 8000 && Fs_Hz_out != 12000 && Fs_Hz_out != 16000 && Fs_Hz_out != 24000 && Fs_Hz_out != 48000 ) ) {
  93. silk_assert( 0 );
  94. return -1;
  95. }
  96. S->inputDelay = delay_matrix_dec[ rateID( Fs_Hz_in ) ][ rateID( Fs_Hz_out ) ];
  97. }
  98. S->Fs_in_kHz = silk_DIV32_16( Fs_Hz_in, 1000 );
  99. S->Fs_out_kHz = silk_DIV32_16( Fs_Hz_out, 1000 );
  100. /* Number of samples processed per batch */
  101. S->batchSize = S->Fs_in_kHz * RESAMPLER_MAX_BATCH_SIZE_MS;
  102. /* Find resampler with the right sampling ratio */
  103. up2x = 0;
  104. if( Fs_Hz_out > Fs_Hz_in ) {
  105. /* Upsample */
  106. if( Fs_Hz_out == silk_MUL( Fs_Hz_in, 2 ) ) { /* Fs_out : Fs_in = 2 : 1 */
  107. /* Special case: directly use 2x upsampler */
  108. S->resampler_function = USE_silk_resampler_private_up2_HQ_wrapper;
  109. } else {
  110. /* Default resampler */
  111. S->resampler_function = USE_silk_resampler_private_IIR_FIR;
  112. up2x = 1;
  113. }
  114. } else if ( Fs_Hz_out < Fs_Hz_in ) {
  115. /* Downsample */
  116. S->resampler_function = USE_silk_resampler_private_down_FIR;
  117. if( silk_MUL( Fs_Hz_out, 4 ) == silk_MUL( Fs_Hz_in, 3 ) ) { /* Fs_out : Fs_in = 3 : 4 */
  118. S->FIR_Fracs = 3;
  119. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR0;
  120. S->Coefs = silk_Resampler_3_4_COEFS;
  121. } else if( silk_MUL( Fs_Hz_out, 3 ) == silk_MUL( Fs_Hz_in, 2 ) ) { /* Fs_out : Fs_in = 2 : 3 */
  122. S->FIR_Fracs = 2;
  123. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR0;
  124. S->Coefs = silk_Resampler_2_3_COEFS;
  125. } else if( silk_MUL( Fs_Hz_out, 2 ) == Fs_Hz_in ) { /* Fs_out : Fs_in = 1 : 2 */
  126. S->FIR_Fracs = 1;
  127. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR1;
  128. S->Coefs = silk_Resampler_1_2_COEFS;
  129. } else if( silk_MUL( Fs_Hz_out, 3 ) == Fs_Hz_in ) { /* Fs_out : Fs_in = 1 : 3 */
  130. S->FIR_Fracs = 1;
  131. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
  132. S->Coefs = silk_Resampler_1_3_COEFS;
  133. } else if( silk_MUL( Fs_Hz_out, 4 ) == Fs_Hz_in ) { /* Fs_out : Fs_in = 1 : 4 */
  134. S->FIR_Fracs = 1;
  135. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
  136. S->Coefs = silk_Resampler_1_4_COEFS;
  137. } else if( silk_MUL( Fs_Hz_out, 6 ) == Fs_Hz_in ) { /* Fs_out : Fs_in = 1 : 6 */
  138. S->FIR_Fracs = 1;
  139. S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
  140. S->Coefs = silk_Resampler_1_6_COEFS;
  141. } else {
  142. /* None available */
  143. silk_assert( 0 );
  144. return -1;
  145. }
  146. } else {
  147. /* Input and output sampling rates are equal: copy */
  148. S->resampler_function = USE_silk_resampler_copy;
  149. }
  150. /* Ratio of input/output samples */
  151. S->invRatio_Q16 = silk_LSHIFT32( silk_DIV32( silk_LSHIFT32( Fs_Hz_in, 14 + up2x ), Fs_Hz_out ), 2 );
  152. /* Make sure the ratio is rounded up */
  153. while( silk_SMULWW( S->invRatio_Q16, Fs_Hz_out ) < silk_LSHIFT32( Fs_Hz_in, up2x ) ) {
  154. S->invRatio_Q16++;
  155. }
  156. return 0;
  157. }
  158. /* Resampler: convert from one sampling rate to another */
  159. /* Input and output sampling rate are at most 48000 Hz */
  160. opus_int silk_resampler(
  161. silk_resampler_state_struct *S, /* I/O Resampler state */
  162. opus_int16 out[], /* O Output signal */
  163. const opus_int16 in[], /* I Input signal */
  164. opus_int32 inLen /* I Number of input samples */
  165. )
  166. {
  167. opus_int nSamples;
  168. /* Need at least 1 ms of input data */
  169. silk_assert( inLen >= S->Fs_in_kHz );
  170. /* Delay can't exceed the 1 ms of buffering */
  171. silk_assert( S->inputDelay <= S->Fs_in_kHz );
  172. nSamples = S->Fs_in_kHz - S->inputDelay;
  173. /* Copy to delay buffer */
  174. silk_memcpy( &S->delayBuf[ S->inputDelay ], in, nSamples * sizeof( opus_int16 ) );
  175. switch( S->resampler_function ) {
  176. case USE_silk_resampler_private_up2_HQ_wrapper:
  177. silk_resampler_private_up2_HQ_wrapper( S, out, S->delayBuf, S->Fs_in_kHz );
  178. silk_resampler_private_up2_HQ_wrapper( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
  179. break;
  180. case USE_silk_resampler_private_IIR_FIR:
  181. silk_resampler_private_IIR_FIR( S, out, S->delayBuf, S->Fs_in_kHz );
  182. silk_resampler_private_IIR_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
  183. break;
  184. case USE_silk_resampler_private_down_FIR:
  185. silk_resampler_private_down_FIR( S, out, S->delayBuf, S->Fs_in_kHz );
  186. silk_resampler_private_down_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
  187. break;
  188. default:
  189. silk_memcpy( out, S->delayBuf, S->Fs_in_kHz * sizeof( opus_int16 ) );
  190. silk_memcpy( &out[ S->Fs_out_kHz ], &in[ nSamples ], ( inLen - S->Fs_in_kHz ) * sizeof( opus_int16 ) );
  191. }
  192. /* Copy to delay buffer */
  193. silk_memcpy( S->delayBuf, &in[ inLen - S->inputDelay ], S->inputDelay * sizeof( opus_int16 ) );
  194. return 0;
  195. }