control_audio_bandwidth.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main.h"
  31. #include "tuning_parameters.h"
  32. /* Control internal sampling rate */
  33. opus_int silk_control_audio_bandwidth(
  34. silk_encoder_state *psEncC, /* I/O Pointer to Silk encoder state */
  35. silk_EncControlStruct *encControl /* I Control structure */
  36. )
  37. {
  38. opus_int fs_kHz;
  39. opus_int orig_kHz;
  40. opus_int32 fs_Hz;
  41. orig_kHz = psEncC->fs_kHz;
  42. /* Handle a bandwidth-switching reset where we need to be aware what the last sampling rate was. */
  43. if( orig_kHz == 0 ) {
  44. orig_kHz = psEncC->sLP.saved_fs_kHz;
  45. }
  46. fs_kHz = orig_kHz;
  47. fs_Hz = silk_SMULBB( fs_kHz, 1000 );
  48. if( fs_Hz == 0 ) {
  49. /* Encoder has just been initialized */
  50. fs_Hz = silk_min( psEncC->desiredInternal_fs_Hz, psEncC->API_fs_Hz );
  51. fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
  52. } else if( fs_Hz > psEncC->API_fs_Hz || fs_Hz > psEncC->maxInternal_fs_Hz || fs_Hz < psEncC->minInternal_fs_Hz ) {
  53. /* Make sure internal rate is not higher than external rate or maximum allowed, or lower than minimum allowed */
  54. fs_Hz = psEncC->API_fs_Hz;
  55. fs_Hz = silk_min( fs_Hz, psEncC->maxInternal_fs_Hz );
  56. fs_Hz = silk_max( fs_Hz, psEncC->minInternal_fs_Hz );
  57. fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
  58. } else {
  59. /* State machine for the internal sampling rate switching */
  60. if( psEncC->sLP.transition_frame_no >= TRANSITION_FRAMES ) {
  61. /* Stop transition phase */
  62. psEncC->sLP.mode = 0;
  63. }
  64. if( psEncC->allow_bandwidth_switch || encControl->opusCanSwitch ) {
  65. /* Check if we should switch down */
  66. if( silk_SMULBB( orig_kHz, 1000 ) > psEncC->desiredInternal_fs_Hz )
  67. {
  68. /* Switch down */
  69. if( psEncC->sLP.mode == 0 ) {
  70. /* New transition */
  71. psEncC->sLP.transition_frame_no = TRANSITION_FRAMES;
  72. /* Reset transition filter state */
  73. silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
  74. }
  75. if( encControl->opusCanSwitch ) {
  76. /* Stop transition phase */
  77. psEncC->sLP.mode = 0;
  78. /* Switch to a lower sample frequency */
  79. fs_kHz = orig_kHz == 16 ? 12 : 8;
  80. } else {
  81. if( psEncC->sLP.transition_frame_no <= 0 ) {
  82. encControl->switchReady = 1;
  83. /* Make room for redundancy */
  84. encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
  85. } else {
  86. /* Direction: down (at double speed) */
  87. psEncC->sLP.mode = -2;
  88. }
  89. }
  90. }
  91. else
  92. /* Check if we should switch up */
  93. if( silk_SMULBB( orig_kHz, 1000 ) < psEncC->desiredInternal_fs_Hz )
  94. {
  95. /* Switch up */
  96. if( encControl->opusCanSwitch ) {
  97. /* Switch to a higher sample frequency */
  98. fs_kHz = orig_kHz == 8 ? 12 : 16;
  99. /* New transition */
  100. psEncC->sLP.transition_frame_no = 0;
  101. /* Reset transition filter state */
  102. silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
  103. /* Direction: up */
  104. psEncC->sLP.mode = 1;
  105. } else {
  106. if( psEncC->sLP.mode == 0 ) {
  107. encControl->switchReady = 1;
  108. /* Make room for redundancy */
  109. encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
  110. } else {
  111. /* Direction: up */
  112. psEncC->sLP.mode = 1;
  113. }
  114. }
  115. } else {
  116. if (psEncC->sLP.mode<0)
  117. psEncC->sLP.mode = 1;
  118. }
  119. }
  120. }
  121. return fs_kHz;
  122. }