control_audio_bandwidth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_int32 fs_Hz;
  40. fs_kHz = psEncC->fs_kHz;
  41. fs_Hz = silk_SMULBB( fs_kHz, 1000 );
  42. if( fs_Hz == 0 ) {
  43. /* Encoder has just been initialized */
  44. fs_Hz = silk_min( psEncC->desiredInternal_fs_Hz, psEncC->API_fs_Hz );
  45. fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
  46. } else if( fs_Hz > psEncC->API_fs_Hz || fs_Hz > psEncC->maxInternal_fs_Hz || fs_Hz < psEncC->minInternal_fs_Hz ) {
  47. /* Make sure internal rate is not higher than external rate or maximum allowed, or lower than minimum allowed */
  48. fs_Hz = psEncC->API_fs_Hz;
  49. fs_Hz = silk_min( fs_Hz, psEncC->maxInternal_fs_Hz );
  50. fs_Hz = silk_max( fs_Hz, psEncC->minInternal_fs_Hz );
  51. fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
  52. } else {
  53. /* State machine for the internal sampling rate switching */
  54. if( psEncC->sLP.transition_frame_no >= TRANSITION_FRAMES ) {
  55. /* Stop transition phase */
  56. psEncC->sLP.mode = 0;
  57. }
  58. if( psEncC->allow_bandwidth_switch || encControl->opusCanSwitch ) {
  59. /* Check if we should switch down */
  60. if( silk_SMULBB( psEncC->fs_kHz, 1000 ) > psEncC->desiredInternal_fs_Hz )
  61. {
  62. /* Switch down */
  63. if( psEncC->sLP.mode == 0 ) {
  64. /* New transition */
  65. psEncC->sLP.transition_frame_no = TRANSITION_FRAMES;
  66. /* Reset transition filter state */
  67. silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
  68. }
  69. if( encControl->opusCanSwitch ) {
  70. /* Stop transition phase */
  71. psEncC->sLP.mode = 0;
  72. /* Switch to a lower sample frequency */
  73. fs_kHz = psEncC->fs_kHz == 16 ? 12 : 8;
  74. } else {
  75. if( psEncC->sLP.transition_frame_no <= 0 ) {
  76. encControl->switchReady = 1;
  77. /* Make room for redundancy */
  78. encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
  79. } else {
  80. /* Direction: down (at double speed) */
  81. psEncC->sLP.mode = -2;
  82. }
  83. }
  84. }
  85. else
  86. /* Check if we should switch up */
  87. if( silk_SMULBB( psEncC->fs_kHz, 1000 ) < psEncC->desiredInternal_fs_Hz )
  88. {
  89. /* Switch up */
  90. if( encControl->opusCanSwitch ) {
  91. /* Switch to a higher sample frequency */
  92. fs_kHz = psEncC->fs_kHz == 8 ? 12 : 16;
  93. /* New transition */
  94. psEncC->sLP.transition_frame_no = 0;
  95. /* Reset transition filter state */
  96. silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
  97. /* Direction: up */
  98. psEncC->sLP.mode = 1;
  99. } else {
  100. if( psEncC->sLP.mode == 0 ) {
  101. encControl->switchReady = 1;
  102. /* Make room for redundancy */
  103. encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
  104. } else {
  105. /* Direction: up */
  106. psEncC->sLP.mode = 1;
  107. }
  108. }
  109. } else {
  110. if (psEncC->sLP.mode<0)
  111. psEncC->sLP.mode = 1;
  112. }
  113. }
  114. }
  115. return fs_kHz;
  116. }