Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /* This product contains Speex software. The license terms of the Speex
  9. software, distributed with this product, are as follows:
  10. 2002-2003, Jean-Marc Valin/Xiph.Org Foundation
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. - Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or
  18. other materials provided with the distribution.
  19. Neither the name of the Xiph.org Foundation nor the names of its
  20. contributors may be used to endorse or promote products derived from this
  21. software without specific prior written permission.
  22. This software is provided by the copyright holders and contributors "as is"
  23. and any express or implied warranties, including, but not limited to, the
  24. implied warranties of merchantability and fitness for a particular purpose
  25. are disclaimed. In no event shall the foundation or contributors be liable
  26. for any direct, indirect, incidental, special, exemplary, or consequential
  27. damages (including, but not limited to, procurement of substitute goods or
  28. services; loss of use, data, or profits; or business interruption) however
  29. caused and on any theory of liability, whether in contract, strict
  30. liability, or tort (including negligence or otherwise) arising in any way
  31. out of the use of this software, even if advised of the possibility of such
  32. damage. */
  33. #include "ivoicecodec.h"
  34. #include "iframeencoder.h"
  35. #ifdef POSIX
  36. #include "source/osx/config.h"
  37. #else
  38. #include "source/msvc/config.h"
  39. #endif
  40. #include <stdio.h>
  41. #include "celt.h"
  42. // NOTE: This has to be the last file included!
  43. #include "tier0/memdbgon.h"
  44. #define CHANNELS 1
  45. struct celt_versions
  46. {
  47. int iSampleRate;
  48. int iRawFrameSize;
  49. int iPacketSize;
  50. };
  51. #define CELT_VERSION 4
  52. celt_versions g_CeltVersion[CELT_VERSION] =
  53. {
  54. {
  55. 44100, 256, 120
  56. },
  57. {
  58. 22050, 120, 60
  59. },
  60. {
  61. 22050, 256, 60
  62. },
  63. {
  64. 22050, 512, 64
  65. },
  66. };
  67. class VoiceEncoder_Celt : public IFrameEncoder
  68. {
  69. public:
  70. VoiceEncoder_Celt();
  71. virtual ~VoiceEncoder_Celt();
  72. // Interfaces IFrameDecoder
  73. bool Init(int quality, int &rawFrameSize, int &encodedFrameSize);
  74. void Release();
  75. void DecodeFrame(const char *pCompressed, char *pDecompressedBytes);
  76. void EncodeFrame(const char *pUncompressedBytes, char *pCompressed);
  77. bool ResetState();
  78. private:
  79. bool InitStates();
  80. void TermStates();
  81. CELTEncoder *m_EncoderState; // Celt internal encoder state
  82. CELTDecoder *m_DecoderState; // Celt internal decoder state
  83. CELTMode *m_Mode;
  84. int m_iVersion;
  85. };
  86. extern IVoiceCodec* CreateVoiceCodec_Frame(IFrameEncoder *pEncoder);
  87. void* CreateCeltVoiceCodec()
  88. {
  89. IFrameEncoder *pEncoder = new VoiceEncoder_Celt;
  90. return CreateVoiceCodec_Frame( pEncoder );
  91. }
  92. EXPOSE_INTERFACE_FN(CreateCeltVoiceCodec, IVoiceCodec, "vaudio_celt")
  93. //////////////////////////////////////////////////////////////////////
  94. // Construction/Destruction
  95. //////////////////////////////////////////////////////////////////////
  96. VoiceEncoder_Celt::VoiceEncoder_Celt()
  97. {
  98. m_EncoderState = NULL;
  99. m_DecoderState = NULL;
  100. m_Mode = NULL;
  101. m_iVersion = 0;
  102. }
  103. VoiceEncoder_Celt::~VoiceEncoder_Celt()
  104. {
  105. TermStates();
  106. }
  107. bool VoiceEncoder_Celt::Init( int quality, int &rawFrameSize, int &encodedFrameSize)
  108. {
  109. if ( quality >= CELT_VERSION )
  110. return false;
  111. m_iVersion = quality;
  112. rawFrameSize = g_CeltVersion[m_iVersion].iRawFrameSize * BYTES_PER_SAMPLE;
  113. int iError = 0;
  114. m_Mode = celt_mode_create( g_CeltVersion[m_iVersion].iSampleRate, g_CeltVersion[m_iVersion].iRawFrameSize, &iError );
  115. m_EncoderState = celt_encoder_create_custom( m_Mode, CHANNELS, NULL);
  116. m_DecoderState = celt_decoder_create_custom( m_Mode, CHANNELS, NULL);
  117. if ( !InitStates() )
  118. return false;
  119. encodedFrameSize = g_CeltVersion[m_iVersion].iPacketSize;
  120. return true;
  121. }
  122. void VoiceEncoder_Celt::Release()
  123. {
  124. delete this;
  125. }
  126. void VoiceEncoder_Celt::EncodeFrame(const char *pUncompressedBytes, char *pCompressed)
  127. {
  128. unsigned char output[1024];
  129. celt_encode( m_EncoderState, (celt_int16*)pUncompressedBytes, g_CeltVersion[m_iVersion].iRawFrameSize, output, g_CeltVersion[m_iVersion].iPacketSize );
  130. for ( int i = 0; i < g_CeltVersion[m_iVersion].iPacketSize; i++ )
  131. {
  132. *pCompressed = (char)output[i];
  133. pCompressed++;
  134. }
  135. }
  136. void VoiceEncoder_Celt::DecodeFrame(const char *pCompressed, char *pDecompressedBytes)
  137. {
  138. unsigned char output[1024];
  139. char *out = (char *)pCompressed;
  140. if ( !pCompressed )
  141. {
  142. celt_decode( m_DecoderState, NULL, g_CeltVersion[m_iVersion].iPacketSize, (celt_int16 *)pDecompressedBytes, g_CeltVersion[m_iVersion].iRawFrameSize );
  143. return;
  144. }
  145. for ( int i = 0; i < g_CeltVersion[m_iVersion].iPacketSize; i++ )
  146. {
  147. output[i] = ( unsigned char ) ( ( *out < 0 ) ? (*out + 256) : *out );
  148. out++;
  149. }
  150. //celt_decoder_ctl( m_DecoderState, CELT_RESET_STATE_REQUEST, NULL );
  151. celt_decode( m_DecoderState, output, g_CeltVersion[m_iVersion].iPacketSize, (celt_int16 *)pDecompressedBytes, g_CeltVersion[m_iVersion].iRawFrameSize );
  152. }
  153. bool VoiceEncoder_Celt::ResetState()
  154. {
  155. celt_encoder_ctl(m_EncoderState, CELT_RESET_STATE_REQUEST , NULL );
  156. celt_decoder_ctl(m_DecoderState, CELT_RESET_STATE_REQUEST , NULL );
  157. return true;
  158. }
  159. bool VoiceEncoder_Celt::InitStates()
  160. {
  161. if ( !m_EncoderState || !m_DecoderState )
  162. return false;
  163. celt_encoder_ctl( m_EncoderState, CELT_RESET_STATE_REQUEST , NULL );
  164. celt_decoder_ctl( m_DecoderState, CELT_RESET_STATE_REQUEST , NULL );
  165. return true;
  166. }
  167. void VoiceEncoder_Celt::TermStates()
  168. {
  169. if( m_EncoderState )
  170. {
  171. celt_encoder_destroy( m_EncoderState );
  172. m_EncoderState = NULL;
  173. }
  174. if( m_DecoderState )
  175. {
  176. celt_decoder_destroy( m_DecoderState );
  177. m_DecoderState = NULL;
  178. }
  179. celt_mode_destroy( m_Mode );
  180. }