speex_callbacks.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File speex_callbacks.c
  3. Callback handling and in-band signalling
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "speex/speex_callbacks.h"
  31. #include "arch.h"
  32. #include "os_support.h"
  33. EXPORT int speex_inband_handler(SpeexBits *bits, SpeexCallback *callback_list, void *state)
  34. {
  35. int id;
  36. SpeexCallback *callback;
  37. /*speex_bits_advance(bits, 5);*/
  38. id=speex_bits_unpack_unsigned(bits, 4);
  39. callback = callback_list+id;
  40. if (callback->func)
  41. {
  42. return callback->func(bits, state, callback->data);
  43. } else
  44. /*If callback is not registered, skip the right number of bits*/
  45. {
  46. int adv;
  47. if (id<2)
  48. adv = 1;
  49. else if (id<8)
  50. adv = 4;
  51. else if (id<10)
  52. adv = 8;
  53. else if (id<12)
  54. adv = 16;
  55. else if (id<14)
  56. adv = 32;
  57. else
  58. adv = 64;
  59. speex_bits_advance(bits, adv);
  60. }
  61. return 0;
  62. }
  63. EXPORT int speex_std_mode_request_handler(SpeexBits *bits, void *state, void *data)
  64. {
  65. spx_int32_t m;
  66. m = speex_bits_unpack_unsigned(bits, 4);
  67. speex_encoder_ctl(data, SPEEX_SET_MODE, &m);
  68. return 0;
  69. }
  70. EXPORT int speex_std_low_mode_request_handler(SpeexBits *bits, void *state, void *data)
  71. {
  72. spx_int32_t m;
  73. m = speex_bits_unpack_unsigned(bits, 4);
  74. speex_encoder_ctl(data, SPEEX_SET_LOW_MODE, &m);
  75. return 0;
  76. }
  77. EXPORT int speex_std_high_mode_request_handler(SpeexBits *bits, void *state, void *data)
  78. {
  79. spx_int32_t m;
  80. m = speex_bits_unpack_unsigned(bits, 4);
  81. speex_encoder_ctl(data, SPEEX_SET_HIGH_MODE, &m);
  82. return 0;
  83. }
  84. #ifndef DISABLE_VBR
  85. EXPORT int speex_std_vbr_request_handler(SpeexBits *bits, void *state, void *data)
  86. {
  87. spx_int32_t vbr;
  88. vbr = speex_bits_unpack_unsigned(bits, 1);
  89. speex_encoder_ctl(data, SPEEX_SET_VBR, &vbr);
  90. return 0;
  91. }
  92. #endif /* #ifndef DISABLE_VBR */
  93. EXPORT int speex_std_enh_request_handler(SpeexBits *bits, void *state, void *data)
  94. {
  95. spx_int32_t enh;
  96. enh = speex_bits_unpack_unsigned(bits, 1);
  97. speex_decoder_ctl(data, SPEEX_SET_ENH, &enh);
  98. return 0;
  99. }
  100. #ifndef DISABLE_VBR
  101. EXPORT int speex_std_vbr_quality_request_handler(SpeexBits *bits, void *state, void *data)
  102. {
  103. float qual;
  104. qual = speex_bits_unpack_unsigned(bits, 4);
  105. speex_encoder_ctl(data, SPEEX_SET_VBR_QUALITY, &qual);
  106. return 0;
  107. }
  108. #endif /* #ifndef DISABLE_VBR */
  109. EXPORT int speex_std_char_handler(SpeexBits *bits, void *state, void *data)
  110. {
  111. unsigned char ch;
  112. ch = speex_bits_unpack_unsigned(bits, 8);
  113. _speex_putc(ch, data);
  114. /*printf("speex_std_char_handler ch=%x\n", ch);*/
  115. return 0;
  116. }
  117. /* Default handler for user callbacks: skip it */
  118. EXPORT int speex_default_user_handler(SpeexBits *bits, void *state, void *data)
  119. {
  120. int req_size = speex_bits_unpack_unsigned(bits, 4);
  121. speex_bits_advance(bits, 5+8*req_size);
  122. return 0;
  123. }