-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpusWriter.cpp
250 lines (214 loc) · 6.38 KB
/
OpusWriter.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "OpusWriter.h"
// See https://tools.ietf.org/html/rfc7845 Section 5.1
//
// outputGain is Q7.8 in dB, recommended to be 0.
static std::vector<uint8_t> OpusHeader(uint8_t channelCount, uint16_t preSkipSamples, uint32_t inputSampleRate, uint16_t outputGain)
{
std::vector<uint8_t> head(19);
head[0] = 'O';
head[1] = 'p';
head[2] = 'u';
head[3] = 's';
head[4] = 'H';
head[5] = 'e';
head[6] = 'a';
head[7] = 'd';
head[8] = 1; // Version
head[9] = channelCount;
head[10] = (preSkipSamples >> 0) & 0xFF;
head[11] = (preSkipSamples >> 8) & 0xFF;
head[12] = (inputSampleRate >> 0) & 0xFF;
head[13] = (inputSampleRate >> 8) & 0xFF;
head[14] = (inputSampleRate >> 16) & 0xFF;
head[15] = (inputSampleRate >> 24) & 0xFF;
head[16] = (outputGain >> 0) & 0xFF;
head[17] = (outputGain >> 8) & 0xFF;
head[18] = 0; // Mapping family - none.
return head;
}
OpusWriter::~OpusWriter()
{
close();
if (mEncoder != nullptr)
opus_encoder_destroy(mEncoder);
}
OpusWriter::OpusWriter(std::string filename,
OpusWriter::SamplingRate samplingRate,
OpusWriter::Channels channels,
OpusWriter::FrameLength frameLength,
int bitrate,
OpusWriter::ComputationalComplexity complexity)
{
// samplingRate must be one of 8000, 12000, 16000, 24000, or 48000.
if (samplingRate != Rate_8000 &&
samplingRate != Rate_12000 &&
samplingRate != Rate_16000 &&
samplingRate != Rate_24000 &&
samplingRate != Rate_48000)
{
mStatus = Status_OpusInvalidSamplingRate;
return;
}
if (channels != 1 && channels != 2)
{
mStatus = Status_OpusInvalidChannelCount;
return;
}
if (frameLength != Frame_2point5ms &&
frameLength != Frame_5ms &&
frameLength != Frame_10ms &&
frameLength != Frame_20ms &&
frameLength != Frame_40ms &&
frameLength != Frame_60ms &&
frameLength != Frame_80ms &&
frameLength != Frame_100ms &&
frameLength != Frame_120ms)
{
mStatus = Status_OpusInvalidFrameLength;
return;
}
mChannels = channels;
// This always works out to an integer:
//
// Sampling Rate (Hz): 8000 12000 16000 24000 48000
// Frame Length (us)
// 2500 20 30 40 60 120
// 5000 40 60 80 120 240
// 10000 80 120 160 240 480
// 20000 160 240 320 480 960
// 40000 320 480 640 960 1920
// 60000 480 720 960 1440 2880
//
mSamplesPerFramePerChannel = frameLength * samplingRate / 1000000;
mFrameLength = frameLength;
// Opus initialisation.
int error = OPUS_INTERNAL_ERROR;
mEncoder = opus_encoder_create(samplingRate, channels, OPUS_APPLICATION_AUDIO, &error);
if (error != OPUS_OK || mEncoder == nullptr)
{
mStatus = Status_OpusInitialisationFailed;
return;
}
// Set bitrate.
error = opus_encoder_ctl(mEncoder, OPUS_SET_BITRATE(bitrate));
if (error != OPUS_OK)
{
mStatus = Status_OpusInitialisationFailed;
return;
}
// Set the computational complexity.
error = opus_encoder_ctl(mEncoder, OPUS_SET_COMPLEXITY(complexity));
if (error != OPUS_OK)
{
mStatus = Status_OpusInitialisationFailed;
return;
}
// Set it to automatically switch between voice and music modes.
error = opus_encoder_ctl(mEncoder, OPUS_SET_SIGNAL(OPUS_AUTO));
if (error != OPUS_OK)
{
mStatus = Status_OpusInitialisationFailed;
return;
}
// Now initialise WebM.
if (!mMuxer.Open(filename.c_str()))
{
mStatus = Status_OutputFileError;
return;
}
// WebM files have one segment.
if (!mMuxerSegment.Init(&mMuxer))
{
mStatus = Status_MuxerSegmentInitialisationFailed;
return;
}
mkvmuxer::SegmentInfo* info = mMuxerSegment.GetSegmentInfo();
if (info == nullptr)
{
mStatus = Status_MuxerSegmentInitialisationFailed;
return;
}
info->set_writing_app("OpusRec");
// Add an audio track.
mTrackNumber = mMuxerSegment.AddAudioTrack(samplingRate, channels, 0); // 0 allows the muxer to device on track number.
if (mTrackNumber == 0)
{
mStatus = Status_MuxerError;
return;
}
mkvmuxer::AudioTrack* audio = static_cast<mkvmuxer::AudioTrack*>(mMuxerSegment.GetTrackByNumber(mTrackNumber));
if (audio == nullptr)
{
mStatus = Status_MuxerError;
return;
}
audio->set_codec_id(mkvmuxer::Tracks::kOpusCodecId);
audio->set_bit_depth(16);
// Delay built into the code during decoding in nanoseconds.
audio->set_codec_delay(6500000); // TODO: How do I know this?
// Amount of audio to discard after a seek, or something like that.
audio->set_seek_pre_roll(80000000); // TODO: How do I know this?
std::vector<uint8_t> opushead = OpusHeader(channels, 0, samplingRate, 0);
if (!audio->SetCodecPrivate(opushead.data(), opushead.size()))
{
mStatus = Status_MuxerError;
return;
}
mFinalize = true;
mStatus = Status_Ok;
}
OpusWriter::Status OpusWriter::status() const
{
return mStatus;
}
bool OpusWriter::write(const int16_t *samples, int sampleCount)
{
if (mEncoder == nullptr)
return false;
// We can only encode an entire frame, so store it in an internal buffer.
mBuffer.insert(mBuffer.end(), samples, samples + sampleCount);
// Now encode as many frames as we can.
while (mBuffer.size() >= mSamplesPerFramePerChannel * mChannels)
{
// Encode to Opus.
const int maxPacketLength = 1024 * 64;
uint8_t packet[maxPacketLength];
opus_int32 len = opus_encode(mEncoder, mBuffer.data(), mSamplesPerFramePerChannel, packet, maxPacketLength);
if (len < 0)
{
mStatus = Status_OpusEncoderError;
return false;
}
// We are allowed to ignore packets shorter than or equal to 2 bytes.
if (len > 2)
{
mkvmuxer::Frame frame;
if (!frame.Init(packet, len))
{
mStatus = Status_MuxerError;
return false;
}
frame.set_track_number(mTrackNumber);
frame.set_timestamp(mTimeCode); // In nanoseconds.
frame.set_is_key(true); // Does this do anything for audio?
if (!mMuxerSegment.AddGenericFrame(&frame))
{
mStatus = Status_MuxerError;
return false;
}
}
mTimeCode += mFrameLength * 1000;
}
return true;
}
bool OpusWriter::close()
{
bool success = true;
if (mFinalize)
{
success = mMuxerSegment.Finalize();
mMuxer.Close();
mFinalize = false;
}
return success;
}