-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoiceencoder_opus.cpp
183 lines (140 loc) · 3.8 KB
/
voiceencoder_opus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "ivoicecodec.h"
#include "iframeencoder.h"
#ifdef _WIN32
#include <win32/config.h>
#endif
#include <stdio.h>
#include <opus.h>
#include <opus_custom.h>
//#include <silk/API.h>
#include <tier0\dbg.h>
#include "tier0/memdbgon.h"
#define CHANNELS 1
struct opus_versions
{
int iSampleRate;
int iRawFrameSize;
int iPacketSize;
};
#define OPUS_VERSION 4
opus_versions g_OpusVersion[OPUS_VERSION]
{
{
44100, 256, 120
},
{
22050, 120, 60
},
{
22050, 256, 60
},
{
22050, 512, 64
},
};
class VoiceEncoder_Opus : public IFrameEncoder
{
public:
VoiceEncoder_Opus();
virtual ~VoiceEncoder_Opus();
bool Init(int quality, int& rawFrameSize, int& encodedFrameSize);
void Release();
void DecodeFrame(const char* pCompressed, char* pDecompressedBytes);
void EncodeFrame(const char* pUncompressedBytes, char* pCompressed);
bool ResetState();
private:
bool InitStates();
void TermStates();
OpusCustomEncoder* m_EncoderState;
OpusCustomDecoder* m_DecoderState;
OpusCustomMode* m_Mode;
int m_iVersion;
};
extern IVoiceCodec* CreateVoiceCodec_Frame(IFrameEncoder* pEncoder);
void* CreateOpusVoiceCodec()
{
IFrameEncoder* pEncoder = new VoiceEncoder_Opus;
return CreateVoiceCodec_Frame(pEncoder);
}
EXPOSE_INTERFACE_FN(CreateOpusVoiceCodec, IVoiceCodec, "vaudio_opus");
VoiceEncoder_Opus::VoiceEncoder_Opus()
{
m_EncoderState = NULL;
m_DecoderState = NULL;
m_Mode = NULL;
m_iVersion = 0;
}
VoiceEncoder_Opus::~VoiceEncoder_Opus()
{
TermStates();
}
bool VoiceEncoder_Opus::Init(int quality, int& rawFrameSize, int& encodedFrameSize)
{
m_iVersion = quality;
rawFrameSize = g_OpusVersion[m_iVersion].iRawFrameSize * BYTES_PER_SAMPLE;
int iError = 0;
m_Mode = opus_custom_mode_create(g_OpusVersion[m_iVersion].iSampleRate, g_OpusVersion[m_iVersion].iRawFrameSize, &iError);
m_EncoderState = opus_custom_encoder_create(m_Mode, CHANNELS, NULL);
m_DecoderState = opus_custom_decoder_create(m_Mode, CHANNELS, NULL);
if (!InitStates())
return false;
encodedFrameSize = g_OpusVersion[m_iVersion].iPacketSize;
return true;
}
void VoiceEncoder_Opus::Release()
{
delete this;
}
void VoiceEncoder_Opus::EncodeFrame(const char* pUncompressedBytes, char* pCompressed)
{
unsigned char output[1024];
opus_custom_encode(m_EncoderState, (opus_int16*)pUncompressedBytes, g_OpusVersion[m_iVersion].iRawFrameSize, output, g_OpusVersion[m_iVersion].iPacketSize);
for (int i = 0; i < g_OpusVersion[m_iVersion].iPacketSize; i++)
{
*pCompressed = (char)output[i];
pCompressed++;
}
}
void VoiceEncoder_Opus::DecodeFrame(const char* pCompressed, char* pDecompressedBytes)
{
unsigned char output[1024];
char* out = (char*)pCompressed;
if (!pCompressed)
{
opus_custom_decode(m_DecoderState, NULL, g_OpusVersion[m_iVersion].iPacketSize, (opus_int16*)pDecompressedBytes, g_OpusVersion[m_iVersion].iRawFrameSize);
}
for (int i = 0; i < g_OpusVersion[m_iVersion].iPacketSize; i++)
{
output[i] = (unsigned char)((*out < 0) ? (*out + 256) : *out);
out++;
}
opus_custom_decode(m_DecoderState, output, g_OpusVersion[m_iVersion].iPacketSize, (opus_int16*)pDecompressedBytes, g_OpusVersion[m_iVersion].iRawFrameSize);
}
bool VoiceEncoder_Opus::ResetState()
{
opus_custom_encoder_ctl(m_EncoderState, OPUS_RESET_STATE, NULL);
opus_custom_decoder_ctl(m_DecoderState, OPUS_RESET_STATE, NULL);
return true;
}
bool VoiceEncoder_Opus::InitStates()
{
if (!m_EncoderState || !m_DecoderState)
return false;
opus_custom_encoder_ctl(m_EncoderState, OPUS_RESET_STATE, NULL);
opus_custom_decoder_ctl(m_DecoderState, OPUS_RESET_STATE, NULL);
return true;
}
void VoiceEncoder_Opus::TermStates()
{
if (m_EncoderState)
{
opus_custom_encoder_destroy(m_EncoderState);
m_EncoderState = NULL;
}
if (m_DecoderState)
{
opus_custom_decoder_destroy(m_DecoderState);
m_DecoderState = NULL;
}
opus_custom_mode_destroy(m_Mode);
}