Fix some compilation errors on Mac OS X
[lsnes.git] / src / library / opus.cpp
blob49919d84f05601c1edc8daabf789b0340dbd585b
1 #include "opus.hpp"
2 #include "loadlib.hpp"
3 #include "threads.hpp"
4 #include <sstream>
5 #include <cstring>
6 #include <map>
7 #include <functional>
9 #define OPUS_AUTO -1000
10 #define OPUS_ALLOC_FAIL -7
11 #define OPUS_APPLICATION_AUDIO 2049
12 #define OPUS_APPLICATION_RESTRICTED_LOWDELAY 2051
13 #define OPUS_APPLICATION_VOIP 2048
14 #define OPUS_AUTO -1000
15 #define OPUS_BAD_ARG -1
16 #define OPUS_BANDWIDTH_FULLBAND 1105
17 #define OPUS_BANDWIDTH_MEDIUMBAND 1102
18 #define OPUS_BANDWIDTH_NARROWBAND 1101
19 #define OPUS_BANDWIDTH_SUPERWIDEBAND 1104
20 #define OPUS_BANDWIDTH_WIDEBAND 1103
21 #define OPUS_BITRATE_MAX -1
22 #define OPUS_BUFFER_TOO_SMALL -2
23 #define OPUS_FRAMESIZE_ARG 5000
24 #define OPUS_FRAMESIZE_2_5_MS 5001
25 #define OPUS_FRAMESIZE_5_MS 5002
26 #define OPUS_FRAMESIZE_10_MS 5003
27 #define OPUS_FRAMESIZE_20_MS 5004
28 #define OPUS_FRAMESIZE_40_MS 5005
29 #define OPUS_FRAMESIZE_60_MS 5006
30 #define OPUS_FRAMESIZE_VARIABLE 5010
31 #define OPUS_GET_APPLICATION_REQUEST 4001
32 #define OPUS_GET_BANDWIDTH_REQUEST 4009
33 #define OPUS_GET_BITRATE_REQUEST 4003
34 #define OPUS_GET_COMPLEXITY_REQUEST 4011
35 #define OPUS_GET_DTX_REQUEST 4017
36 #define OPUS_GET_FINAL_RANGE_REQUEST 4031
37 #define OPUS_GET_FORCE_CHANNELS_REQUEST 4023
38 #define OPUS_GET_INBAND_FEC_REQUEST 4013
39 #define OPUS_GET_LOOKAHEAD_REQUEST 4027
40 #define OPUS_GET_MAX_BANDWIDTH_REQUEST 4005
41 #define OPUS_GET_PACKET_LOSS_PERC_REQUEST 4015
42 #define OPUS_GET_PITCH_REQUEST 4033
43 #define OPUS_GET_SAMPLE_RATE_REQUEST 4029
44 #define OPUS_GET_SIGNAL_REQUEST 4025
45 #define OPUS_GET_VBR_CONSTRAINT_REQUEST 4021
46 #define OPUS_GET_VBR_REQUEST 4007
47 #define OPUS_INTERNAL_ERROR -3
48 #define OPUS_INVALID_PACKET -4
49 #define OPUS_INVALID_STATE -6
50 #define OPUS_RESET_STATE 4028
51 #define OPUS_SET_APPLICATION_REQUEST 4000
52 #define OPUS_SET_BANDWIDTH_REQUEST 4008
53 #define OPUS_SET_BITRATE_REQUEST 4002
54 #define OPUS_SET_COMPLEXITY_REQUEST 4010
55 #define OPUS_SET_DTX_REQUEST 4016
56 #define OPUS_SET_FORCE_CHANNELS_REQUEST 4022
57 #define OPUS_SET_INBAND_FEC_REQUEST 4012
58 #define OPUS_SET_MAX_BANDWIDTH_REQUEST 4004
59 #define OPUS_SET_PACKET_LOSS_PERC_REQUEST 4014
60 #define OPUS_SET_SIGNAL_REQUEST 4024
61 #define OPUS_SET_VBR_CONSTRAINT_REQUEST 4020
62 #define OPUS_SET_VBR_REQUEST 4006
63 #define OPUS_SIGNAL_MUSIC 3002
64 #define OPUS_SIGNAL_VOICE 3001
65 #define OPUS_UNIMPLEMENTED -5
66 #define OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST 5120
67 #define OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST 5122
68 #define SURROUND_SUPPORTED opus_surround_supported()
70 //Some of these might not be in stable.
71 #define OPUS_SET_GAIN_REQUEST 4034
72 #define OPUS_GET_GAIN_REQUEST 4045
73 #define OPUS_SET_LSB_DEPTH_REQUEST 4036
74 #define OPUS_GET_LSB_DEPTH_REQUEST 4037
75 #define OPUS_GET_LAST_PACKET_DURATION_REQUEST 4039
76 #define OPUS_SET_EXPERT_FRAME_DURATION_REQUEST 4040
77 #define OPUS_GET_EXPERT_FRAME_DURATION_REQUEST 4041
78 #define OPUS_SET_PREDICTION_DISABLED_REQUEST 4042
79 #define OPUS_GET_PREDICTION_DISABLED_REQUEST 4043
81 namespace opus
83 struct OpusEncoder { int dummy; };
84 struct OpusDecoder { int dummy; };
85 struct OpusMSEncoder { int dummy; };
86 struct OpusMSDecoder { int dummy; };
87 struct OpusRepacketizer { int dummy; };
89 int dummy_encoder_ctl(OpusEncoder* e, int request, ...) { throw not_loaded(); }
90 int dummy_decoder_ctl(OpusDecoder* e, int request, ...) { throw not_loaded(); }
91 int dummy_multistream_encoder_ctl(OpusMSEncoder* e, int request, ...) { throw not_loaded(); }
92 int dummy_multistream_decoder_ctl(OpusMSDecoder* e, int request, ...) { throw not_loaded(); }
94 struct functions
96 int (*encoder_ctl)(OpusEncoder* e, int request, ...);
97 int (*decoder_ctl)(OpusDecoder* e, int request, ...);
98 int (*multistream_encoder_ctl)(OpusMSEncoder* e, int request, ...);
99 int (*multistream_decoder_ctl)(OpusMSDecoder* e, int request, ...);
100 int (*encoder_init)(OpusEncoder* e, int32_t fs, int ch, int app);
101 int (*decoder_init)(OpusDecoder* e, int32_t fs, int ch);
102 int (*encoder_get_size)(int ch);
103 int (*decoder_get_size)(int ch);
104 int32_t (*encode)(OpusEncoder* e, const int16_t* pcm, int frames, unsigned char* data, int32_t maxout);
105 int32_t (*encode_float)(OpusEncoder* e, const float* pcm, int frames, unsigned char* data, int32_t maxout);
106 int32_t (*decode)(OpusDecoder* e, const unsigned char* data, int32_t datasize, int16_t* pcm, int frames,
107 int fec);
108 int32_t (*decode_float)(OpusDecoder* e, const unsigned char* data, int32_t datasize, float* pcm, int frames,
109 int fec);
110 int (*decoder_get_nb_samples)(const OpusDecoder* dec, const unsigned char packet[], int32_t len);
111 OpusRepacketizer *(*repacketizer_init)(OpusRepacketizer* r);
112 int (*repacketizer_get_size)();
113 int (*repacketizer_cat)(OpusRepacketizer* r, const unsigned char* data, int32_t len);
114 int32_t (*repacketizer_out_range)(OpusRepacketizer* r, int begin, int end, unsigned char* data,
115 int32_t maxlen);
116 int32_t (*repacketizer_out)(OpusRepacketizer* r, unsigned char* data, int32_t maxlen);
117 int (*repacketizer_get_nb_frames)(OpusRepacketizer* r);
118 const char* (*get_version_string)();
119 int (*multistream_decode)(OpusMSDecoder* d, const unsigned char* data, int32_t datasize, int16_t* pcm,
120 int pcmlen, int fec);
121 int (*multistream_decode_float)(OpusMSDecoder* d, const unsigned char* data, int32_t datasize, float* pcm,
122 int pcmlen, int fec);
123 int32_t (*multistream_decoder_get_size)(int streams, int coupled);
124 int (*multistream_decoder_init)(OpusMSDecoder* d, int32_t fs, int ch, int st, int co,
125 const unsigned char* map);
126 int (*multistream_encode)(OpusMSEncoder* d, const int16_t* pcm, int pcmlen, unsigned char* data,
127 int32_t datasize);
128 int (*multistream_encode_float)(OpusMSEncoder* d, const float* pcm, int pcmlen, unsigned char* data,
129 int32_t datasize);
130 int32_t (*multistream_encoder_get_size)(int streams, int coupled);
131 int (*multistream_encoder_init)(OpusMSEncoder* d, int32_t fs, int ch, int st, int co,
132 const unsigned char* map, int app);
133 int (*packet_get_bandwidth)(const unsigned char* p);
134 int (*packet_get_nb_channels)(const unsigned char* p);
135 int (*packet_get_nb_frames)(const unsigned char* p, int32_t psize);
136 int (*packet_get_samples_per_frame)(const unsigned char* p, int32_t fs);
137 int (*packet_parse)(const unsigned char* data, int32_t dsize, unsigned char* toc,
138 const unsigned char* fdata[48], short fsize[48], int* poffset);
139 int32_t (*multistream_surround_encoder_get_size)(int ch, int f);
140 int (*multistream_surround_encoder_init)(OpusMSEncoder* e, int32_t fs, int ch, int f, int* st, int* co,
141 unsigned char* map, int app);
144 struct functions dummy_functions = {
145 .encoder_ctl = dummy_encoder_ctl,
146 .decoder_ctl = dummy_decoder_ctl,
147 .multistream_encoder_ctl = dummy_multistream_encoder_ctl,
148 .multistream_decoder_ctl = dummy_multistream_decoder_ctl,
149 .encoder_init = [](OpusEncoder* e, int32_t fs, int ch, int app) -> int { throw not_loaded(); },
150 .decoder_init = [](OpusDecoder* e, int32_t fs, int ch) -> int { throw not_loaded(); },
151 .encoder_get_size = [](int ch) -> int { throw not_loaded(); },
152 .decoder_get_size = [](int ch) -> int { throw not_loaded(); },
153 .encode = [](OpusEncoder* e, const int16_t* pcm, int frames, unsigned char* data, int32_t maxout)
154 -> int32_t { throw not_loaded(); },
155 .encode_float = [](OpusEncoder* e, const float* pcm, int frames, unsigned char* data, int32_t maxout)
156 -> int32_t { throw not_loaded(); },
157 .decode = [](OpusDecoder* e, const unsigned char* data, int32_t datasize, int16_t* pcm, int frames, int fec)
158 -> int32_t { throw not_loaded(); },
159 .decode_float = [](OpusDecoder* e, const unsigned char* data, int32_t datasize, float* pcm, int frames,
160 int fec) -> int32_t { throw not_loaded(); },
161 .decoder_get_nb_samples = [](const OpusDecoder* dec, const unsigned char packet[], int32_t len)
162 -> int { throw not_loaded(); },
163 .repacketizer_init = [](OpusRepacketizer* r) -> OpusRepacketizer* { throw not_loaded(); },
164 .repacketizer_get_size = []() -> int { throw not_loaded(); },
165 .repacketizer_cat = [](OpusRepacketizer* r, const unsigned char* data, int32_t len)
166 -> int { throw not_loaded(); },
167 .repacketizer_out_range = [](OpusRepacketizer* r, int begin, int end, unsigned char* data, int32_t maxlen)
168 -> int32_t { throw not_loaded(); },
169 .repacketizer_out = [](OpusRepacketizer* r, unsigned char* data, int32_t maxlen)
170 -> int32_t { throw not_loaded(); },
171 .repacketizer_get_nb_frames = [](OpusRepacketizer* r) -> int { throw not_loaded(); },
172 .get_version_string = []() -> const char* { throw not_loaded(); },
173 .multistream_decode = [](OpusMSDecoder* d, const unsigned char* data, int32_t datasize, int16_t* pcm,
174 int pcmlen, int fec) -> int { throw not_loaded(); },
175 .multistream_decode_float = [](OpusMSDecoder* d, const unsigned char* data, int32_t datasize, float* pcm,
176 int pcmlen, int fec) -> int { throw not_loaded(); },
177 .multistream_decoder_get_size = [](int streams, int coupled) -> int32_t { throw not_loaded(); },
178 .multistream_decoder_init = [](OpusMSDecoder* d, int32_t fs, int ch, int st, int co, const unsigned char* map)
179 -> int { throw not_loaded(); },
180 .multistream_encode = [](OpusMSEncoder* d, const int16_t* pcm, int pcmlen, unsigned char* data,
181 int32_t datasize) -> int { throw not_loaded(); },
182 .multistream_encode_float = [](OpusMSEncoder* d, const float* pcm, int pcmlen, unsigned char* data,
183 int32_t datasize) -> int { throw not_loaded(); },
184 .multistream_encoder_get_size = [](int streams, int coupled) -> int32_t { throw not_loaded(); },
185 .multistream_encoder_init = [](OpusMSEncoder* d, int32_t fs, int ch, int st, int co, const unsigned char* map,
186 int app) -> int { throw not_loaded(); },
187 .packet_get_bandwidth = [](const unsigned char* p) -> int { throw not_loaded(); },
188 .packet_get_nb_channels = [](const unsigned char* p) -> int { throw not_loaded(); },
189 .packet_get_nb_frames = [](const unsigned char* p, int32_t psize) -> int { throw not_loaded(); },
190 .packet_get_samples_per_frame = [](const unsigned char* p, int32_t fs) -> int { throw not_loaded(); },
191 .packet_parse = [](const unsigned char* data, int32_t dsize, unsigned char* toc,
192 const unsigned char* fdata[48], short fsize[48], int* poffset) -> int { throw not_loaded(); },
193 .multistream_surround_encoder_get_size = [](int ch, int f) -> int32_t { throw not_loaded(); },
194 .multistream_surround_encoder_init = [](OpusMSEncoder* e, int32_t fs, int ch, int f, int* st, int* co,
195 unsigned char* map, int app) -> int { throw not_loaded(); }
198 struct functions* opus_functions = &dummy_functions;
200 std::map<size_t, std::function<void()>> lcbs;
201 threads::lock mut;
203 template<typename T> void CA(T& d, void* s)
205 d = reinterpret_cast<T>(s);
208 void load_libopus(const loadlib::module& lib)
210 static functions lfun;
211 functions tmp;
213 CA(tmp.encoder_ctl, lib["opus_encoder_ctl"]);
214 CA(tmp.decoder_ctl, lib["opus_decoder_ctl"]);
215 CA(tmp.multistream_encoder_ctl, lib["opus_multistream_encoder_ctl"]);
216 CA(tmp.multistream_decoder_ctl, lib["opus_multistream_decoder_ctl"]);
217 CA(tmp.encoder_init, lib["opus_encoder_init"]);
218 CA(tmp.decoder_init, lib["opus_decoder_init"]);
219 CA(tmp.encoder_get_size, lib["opus_encoder_get_size"]);
220 CA(tmp.decoder_get_size, lib["opus_decoder_get_size"]);
221 CA(tmp.encode, lib["opus_encode"]);
222 CA(tmp.encode_float, lib["opus_encode_float"]);
223 CA(tmp.decode, lib["opus_decode"]);
224 CA(tmp.decode_float, lib["opus_decode_float"]);
225 CA(tmp.decoder_get_nb_samples, lib["opus_decoder_get_nb_samples"]);
226 CA(tmp.repacketizer_init, lib["opus_repacketizer_init"]);
227 CA(tmp.repacketizer_get_size, lib["opus_repacketizer_get_size"]);
228 CA(tmp.repacketizer_cat, lib["opus_repacketizer_cat"]);
229 CA(tmp.repacketizer_out_range, lib["opus_repacketizer_out_range"]);
230 CA(tmp.repacketizer_out, lib["opus_repacketizer_out"]);
231 CA(tmp.repacketizer_get_nb_frames, lib["opus_repacketizer_get_nb_frames"]);
232 CA(tmp.get_version_string, lib["opus_get_version_string"]);
233 CA(tmp.multistream_decode, lib["opus_multistream_decode"]);
234 CA(tmp.multistream_decode_float, lib["opus_multistream_decode_float"]);
235 CA(tmp.multistream_decoder_get_size, lib["opus_multistream_decoder_get_size"]);
236 CA(tmp.multistream_decoder_init, lib["opus_multistream_decoder_init"]);
237 CA(tmp.multistream_encode, lib["opus_multistream_encode"]);
238 CA(tmp.multistream_encode_float, lib["opus_multistream_encode_float"]);
239 CA(tmp.multistream_encoder_get_size, lib["opus_multistream_encoder_get_size"]);
240 CA(tmp.multistream_encoder_init, lib["opus_multistream_encoder_init"]);
241 CA(tmp.packet_get_bandwidth, lib["opus_packet_get_bandwidth"]);
242 CA(tmp.packet_get_nb_channels, lib["opus_packet_get_nb_channels"]);
243 CA(tmp.packet_get_nb_frames, lib["opus_packet_get_nb_frames"]);
244 CA(tmp.packet_get_samples_per_frame, lib["opus_packet_get_samples_per_frame"]);
245 CA(tmp.packet_parse, lib["opus_packet_parse"]);
246 try {
247 CA(tmp.multistream_surround_encoder_get_size, lib["opus_multistream_surround_encoder_get_size"]);
248 CA(tmp.multistream_surround_encoder_init, lib["opus_multistream_surround_encoder_init"]);
249 } catch(...) {
250 tmp.multistream_surround_encoder_get_size = dummy_functions.multistream_surround_encoder_get_size;
251 tmp.multistream_surround_encoder_init = dummy_functions.multistream_surround_encoder_init;
254 lfun = tmp;
255 threads::alock h(mut);
256 opus_functions = &lfun;
257 for(auto i : lcbs)
258 (i.second)();
261 bool libopus_loaded()
263 threads::alock h(mut);
264 return (opus_functions != &dummy_functions);
267 size_t add_callback(std::function<void()> fun)
269 threads::alock h(mut);
270 static size_t var = 0;
271 size_t hd = 0;
272 if(opus_functions != &dummy_functions)
273 fun();
274 else
275 lcbs[hd = var++] = fun;
276 return hd;
279 void cancel_callback(size_t handle)
281 threads::alock h(mut);
282 lcbs.erase(handle);
285 int opus_encoder_ctl(OpusEncoder* e, int req)
287 return opus_functions->encoder_ctl(e, req);
290 int opus_decoder_ctl(OpusDecoder* e, int req)
292 return opus_functions->decoder_ctl(e, req);
295 int opus_encoder_ctl(OpusEncoder* e, int req, int* arg)
297 return opus_functions->encoder_ctl(e, req, arg);
300 int opus_decoder_ctl(OpusDecoder* e, int req, int* arg)
302 return opus_functions->decoder_ctl(e, req, arg);
305 int opus_encoder_ctl(OpusEncoder* e, int req, int arg)
307 return opus_functions->encoder_ctl(e, req, arg);
310 int opus_decoder_ctl(OpusDecoder* e, int req, int arg)
312 return opus_functions->decoder_ctl(e, req, arg);
315 int opus_encoder_ctl(OpusEncoder* e, int req, uint32_t* arg)
317 return opus_functions->encoder_ctl(e, req, arg);
320 int opus_decoder_ctl(OpusDecoder* e, int req, uint32_t* arg)
322 return opus_functions->decoder_ctl(e, req, arg);
325 int opus_multistream_encoder_ctl(OpusMSEncoder* e, int req)
327 return opus_functions->multistream_encoder_ctl(e, req);
330 int opus_multistream_decoder_ctl(OpusMSDecoder* e, int req)
332 return opus_functions->multistream_decoder_ctl(e, req);
335 int opus_multistream_encoder_ctl(OpusMSEncoder* e, int req, int arg)
337 return opus_functions->multistream_encoder_ctl(e, req, arg);
340 int opus_multistream_decoder_ctl(OpusMSDecoder* e, int req, int arg)
342 return opus_functions->multistream_decoder_ctl(e, req, arg);
345 int opus_multistream_encoder_ctl(OpusMSEncoder* e, int req, int* arg)
347 return opus_functions->multistream_encoder_ctl(e, req, arg);
350 int opus_multistream_decoder_ctl(OpusMSDecoder* e, int req, int* arg)
352 return opus_functions->multistream_decoder_ctl(e, req, arg);
355 int opus_multistream_encoder_ctl(OpusMSEncoder* e, int req, int arg, OpusEncoder** arg2)
357 return opus_functions->multistream_encoder_ctl(e, req, arg, arg2);
360 int opus_multistream_decoder_ctl(OpusMSDecoder* e, int req, int arg, OpusDecoder** arg2)
362 return opus_functions->multistream_decoder_ctl(e, req, arg, arg2);
365 int opus_encoder_init(OpusEncoder* e, int32_t fs, int ch, int app)
367 return opus_functions->encoder_init(e, fs, ch, app);
370 int opus_decoder_init(OpusDecoder* e, int32_t fs, int ch)
372 return opus_functions->decoder_init(e, fs, ch);
375 int opus_encoder_get_size(int ch)
377 return opus_functions->encoder_get_size(ch);
380 int opus_decoder_get_size(int ch)
382 return opus_functions->decoder_get_size(ch);
385 int32_t opus_encode(OpusEncoder* e, const int16_t* pcm, int frames, unsigned char* data, int32_t maxout)
387 return opus_functions->encode(e, pcm, frames, data, maxout);
390 int32_t opus_encode_float(OpusEncoder* e, const float* pcm, int frames, unsigned char* data, int32_t maxout)
392 return opus_functions->encode_float(e, pcm, frames, data, maxout);
395 int32_t opus_decode(OpusDecoder* e, const unsigned char* data, int32_t datasize, int16_t* pcm, int frames, int fec)
397 return opus_functions->decode(e, data, datasize, pcm, frames, fec);
400 int32_t opus_decode_float(OpusDecoder* e, const unsigned char* data, int32_t datasize, float* pcm, int frames,
401 int fec)
403 return opus_functions->decode_float(e, data, datasize, pcm, frames, fec);
406 int opus_decoder_get_nb_samples(const OpusDecoder* dec, const unsigned char packet[], int32_t len)
408 return opus_functions->decoder_get_nb_samples(dec, packet, len);
411 OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer* r)
413 return opus_functions->repacketizer_init(r);
416 int opus_repacketizer_get_size()
418 return opus_functions->repacketizer_get_size();
421 int opus_repacketizer_cat(OpusRepacketizer* r, const unsigned char* data, int32_t len)
423 return opus_functions->repacketizer_cat(r, data, len);
426 int32_t opus_repacketizer_out_range(OpusRepacketizer* r, int begin, int end, unsigned char* data, int32_t maxlen)
428 return opus_functions->repacketizer_out_range(r, begin, end, data, maxlen);
431 int32_t opus_repacketizer_out(OpusRepacketizer* r, unsigned char* data, int32_t maxlen)
433 return opus_functions->repacketizer_out(r, data, maxlen);
436 int opus_repacketizer_get_nb_frames(OpusRepacketizer* r)
438 return opus_functions->repacketizer_get_nb_frames(r);
441 const char* opus_get_version_string()
443 return opus_functions->get_version_string();
446 int opus_multistream_decode(OpusMSDecoder* d, const unsigned char* data, int32_t datasize, int16_t* pcm, int pcmlen,
447 int fec)
449 return opus_functions->multistream_decode(d, data, datasize, pcm, pcmlen, fec);
452 int opus_multistream_decode_float(OpusMSDecoder* d, const unsigned char* data, int32_t datasize, float* pcm,
453 int pcmlen, int fec)
455 return opus_functions->multistream_decode_float(d, data, datasize, pcm, pcmlen, fec);
458 int32_t opus_multistream_decoder_get_size(int streams, int coupled)
460 return opus_functions->multistream_decoder_get_size(streams, coupled);
463 int opus_multistream_decoder_init(OpusMSDecoder* d, int32_t fs, int ch, int st, int co, const unsigned char* map)
465 return opus_functions->multistream_decoder_init(d, fs, ch, st, co, map);
468 int opus_multistream_encode(OpusMSEncoder* d, const int16_t* pcm, int pcmlen, unsigned char* data, int32_t datasize)
470 return opus_functions->multistream_encode(d, pcm, pcmlen, data, datasize);
473 int opus_multistream_encode_float(OpusMSEncoder* d, const float* pcm, int pcmlen, unsigned char* data,
474 int32_t datasize)
476 return opus_functions->multistream_encode_float(d, pcm, pcmlen, data, datasize);
479 int32_t opus_multistream_encoder_get_size(int streams, int coupled)
481 return opus_functions->multistream_encoder_get_size(streams, coupled);
484 int opus_multistream_encoder_init(OpusMSEncoder* d, int32_t fs, int ch, int st, int co, const unsigned char* map,
485 int app)
487 return opus_functions->multistream_encoder_init(d, fs, ch, st, co, map, app);
490 int opus_packet_get_bandwidth(const unsigned char* p)
492 return opus_functions->packet_get_bandwidth(p);
495 int opus_packet_get_nb_channels(const unsigned char* p)
497 return opus_functions->packet_get_nb_channels(p);
500 int opus_packet_get_nb_frames(const unsigned char* p, int32_t psize)
502 return opus_functions->packet_get_nb_frames(p, psize);
505 int opus_packet_get_samples_per_frame(const unsigned char* p, int32_t fs)
507 return opus_functions->packet_get_samples_per_frame(p, fs);
510 int opus_packet_parse(const unsigned char* data, int32_t dsize, unsigned char* toc, const unsigned char* fdata[48],
511 short fsize[48], int* poffset)
513 return opus_functions->packet_parse(data, dsize, toc, fdata, fsize, poffset);
516 bool opus_surround_supported()
518 return (opus_functions->multistream_surround_encoder_get_size !=
519 dummy_functions.multistream_surround_encoder_get_size);
522 int32_t opus_multistream_surround_encoder_get_size(int ch, int f)
524 return opus_functions->multistream_surround_encoder_get_size(ch, f);
527 int opus_multistream_surround_encoder_init(OpusMSEncoder* e, int32_t fs, int ch, int f, int* st, int* co,
528 unsigned char* map, int app)
530 return opus_functions->multistream_surround_encoder_init(e, fs, ch, f, st, co, map, app);
533 samplerate samplerate::r8k(8000);
534 samplerate samplerate::r12k(12000);
535 samplerate samplerate::r16k(16000);
536 samplerate samplerate::r24k(24000);
537 samplerate samplerate::r48k(48000);
538 bitrate bitrate::_auto(OPUS_AUTO);
539 bitrate bitrate::max(OPUS_BITRATE_MAX);
540 vbr vbr::cbr(false);
541 vbr vbr::_vbr(true);
542 vbr_constraint vbr_constraint::unconstrained(false);
543 vbr_constraint vbr_constraint::constrained(true);
544 force_channels force_channels::_auto(OPUS_AUTO);
545 force_channels force_channels::mono(1);
546 force_channels force_channels::stereo(2);
547 max_bandwidth max_bandwidth::narrow(OPUS_BANDWIDTH_NARROWBAND);
548 max_bandwidth max_bandwidth::medium(OPUS_BANDWIDTH_MEDIUMBAND);
549 max_bandwidth max_bandwidth::wide(OPUS_BANDWIDTH_WIDEBAND);
550 max_bandwidth max_bandwidth::superwide(OPUS_BANDWIDTH_SUPERWIDEBAND);
551 max_bandwidth max_bandwidth::full(OPUS_BANDWIDTH_FULLBAND);
552 bandwidth bandwidth::_auto(OPUS_AUTO);
553 bandwidth bandwidth::narrow(OPUS_BANDWIDTH_NARROWBAND);
554 bandwidth bandwidth::medium(OPUS_BANDWIDTH_MEDIUMBAND);
555 bandwidth bandwidth::wide(OPUS_BANDWIDTH_WIDEBAND);
556 bandwidth bandwidth::superwide(OPUS_BANDWIDTH_SUPERWIDEBAND);
557 bandwidth bandwidth::full(OPUS_BANDWIDTH_FULLBAND);
558 signal signal::_auto(OPUS_AUTO);
559 signal signal::music(OPUS_SIGNAL_MUSIC);
560 signal signal::voice(OPUS_SIGNAL_VOICE);
561 application application::audio(OPUS_APPLICATION_AUDIO);
562 application application::voice(OPUS_APPLICATION_VOIP);
563 application application::lowdelay(OPUS_APPLICATION_RESTRICTED_LOWDELAY);
564 fec fec::disabled(false);
565 fec fec::enabled(true);
566 dtx dtx::disabled(false);
567 dtx dtx::enabled(true);
568 lsbdepth lsbdepth::d8(8);
569 lsbdepth lsbdepth::d16(16);
570 lsbdepth lsbdepth::d24(24);
571 prediction_disabled prediction_disabled::enabled(false);
572 prediction_disabled prediction_disabled::disabled(true);
573 frame_duration frame_duration::argument(OPUS_FRAMESIZE_ARG);
574 frame_duration frame_duration::variable(OPUS_FRAMESIZE_VARIABLE);
575 frame_duration frame_duration::l2ms(OPUS_FRAMESIZE_2_5_MS);
576 frame_duration frame_duration::l5ms(OPUS_FRAMESIZE_5_MS);
577 frame_duration frame_duration::l10ms(OPUS_FRAMESIZE_10_MS);
578 frame_duration frame_duration::l20ms(OPUS_FRAMESIZE_20_MS);
579 frame_duration frame_duration::l40ms(OPUS_FRAMESIZE_40_MS);
580 frame_duration frame_duration::l60ms(OPUS_FRAMESIZE_60_MS);
581 _reset reset;
582 _finalrange finalrange;
583 _pitch pitch;
584 _pktduration pktduration;
585 _lookahead lookahead;
586 _last_packet_duration last_packet_duration;
588 bad_argument::bad_argument()
589 : std::runtime_error("Invalid argument") {}
591 buffer_too_small::buffer_too_small()
592 : std::runtime_error("Buffer too small") {}
594 internal_error::internal_error()
595 : std::runtime_error("Internal error") {}
597 invalid_packet::invalid_packet()
598 : std::runtime_error("Invalid packet") {}
600 unimplemented::unimplemented()
601 : std::runtime_error("Not implemented") {}
603 invalid_state::invalid_state()
604 : std::runtime_error("Invalid state") {}
606 not_loaded::not_loaded()
607 : std::runtime_error("Library not loaded") {}
609 const unsigned f1_streams[] = {0, 1, 1, 2, 2, 3, 4, 4, 5};
610 const unsigned f1_coupled[] = {0, 0, 1, 1, 2, 2, 2, 3, 3};
611 const char* chanmap[] = {"", "A", "AB", "ACB", "ABCD", "ACDEB", "ACDEBF", "ACBFDEG", "ACDEFGBH"};
613 void lookup_params(unsigned channels, unsigned family, int& streams, int& coupled)
615 if(channels == 0)
616 throw bad_argument();
617 if(family == 0) {
618 if(channels > 2)
619 throw unimplemented();
620 streams = 1;
621 coupled = (channels == 2) ? 1 : 0;
622 } else if(family == 1) {
623 if(channels > 8)
624 throw unimplemented();
625 streams = f1_streams[channels];
626 coupled = f1_coupled[channels];
627 } else
628 throw unimplemented();
631 void generate_mapping(unsigned channels, unsigned family, unsigned char* mapping)
633 for(unsigned i = 0; i < channels; i++)
634 mapping[i] = chanmap[channels][i] - 'A';
637 char* alignptr(char* ptr, size_t align)
639 while((intptr_t)ptr % align)
640 ptr++;
641 return ptr;
644 int32_t throwex(int32_t ret)
646 if(ret >= 0)
647 return ret;
648 if(ret == OPUS_BAD_ARG)
649 throw bad_argument();
650 if(ret == OPUS_BUFFER_TOO_SMALL)
651 throw buffer_too_small();
652 if(ret == OPUS_INTERNAL_ERROR)
653 throw internal_error();
654 if(ret == OPUS_INVALID_PACKET)
655 throw invalid_packet();
656 if(ret == OPUS_UNIMPLEMENTED)
657 throw unimplemented();
658 if(ret == OPUS_INVALID_STATE)
659 throw invalid_state();
660 if(ret == OPUS_ALLOC_FAIL)
661 throw std::bad_alloc();
662 std::ostringstream s;
663 s << "Unknown error code " << ret << " from libopus.";
664 throw std::runtime_error(s.str());
667 template<typename T> struct get_ctlnum { const static int32_t num; static T errordefault(); };
669 template<> const int32_t get_ctlnum<complexity>::num = OPUS_GET_COMPLEXITY_REQUEST;
670 template<> const int32_t get_ctlnum<bitrate>::num = OPUS_GET_BITRATE_REQUEST;
671 template<> const int32_t get_ctlnum<vbr>::num = OPUS_GET_VBR_REQUEST;
672 template<> const int32_t get_ctlnum<vbr_constraint>::num = OPUS_GET_VBR_CONSTRAINT_REQUEST;
673 template<> const int32_t get_ctlnum<force_channels>::num = OPUS_GET_FORCE_CHANNELS_REQUEST;
674 template<> const int32_t get_ctlnum<max_bandwidth>::num = OPUS_GET_MAX_BANDWIDTH_REQUEST;
675 template<> const int32_t get_ctlnum<bandwidth>::num = OPUS_GET_BANDWIDTH_REQUEST;
676 template<> const int32_t get_ctlnum<signal>::num = OPUS_GET_SIGNAL_REQUEST;
677 template<> const int32_t get_ctlnum<application>::num = OPUS_GET_APPLICATION_REQUEST;
678 template<> const int32_t get_ctlnum<fec>::num = OPUS_GET_INBAND_FEC_REQUEST;
679 template<> const int32_t get_ctlnum<lossperc>::num = OPUS_GET_PACKET_LOSS_PERC_REQUEST;
680 template<> const int32_t get_ctlnum<dtx>::num = OPUS_GET_DTX_REQUEST;
681 template<> const int32_t get_ctlnum<lsbdepth>::num = OPUS_GET_LSB_DEPTH_REQUEST;
682 template<> const int32_t get_ctlnum<gain>::num = OPUS_GET_GAIN_REQUEST;
683 template<> const int32_t get_ctlnum<prediction_disabled>::num = OPUS_GET_PREDICTION_DISABLED_REQUEST;
684 template<> const int32_t get_ctlnum<frame_duration>::num = OPUS_GET_EXPERT_FRAME_DURATION_REQUEST;
685 template<> const int32_t get_ctlnum<_last_packet_duration>::num = OPUS_GET_LAST_PACKET_DURATION_REQUEST;
687 template<> complexity get_ctlnum<complexity>::errordefault() { return complexity(10); }
688 template<> bitrate get_ctlnum<bitrate>::errordefault() { return bitrate::_auto; }
689 template<> vbr get_ctlnum<vbr>::errordefault() { return vbr::_vbr; }
690 template<> vbr_constraint get_ctlnum<vbr_constraint>::errordefault() { return vbr_constraint::unconstrained; }
691 template<> force_channels get_ctlnum<force_channels>::errordefault() { return force_channels::_auto; }
692 template<> max_bandwidth get_ctlnum<max_bandwidth>::errordefault() { return max_bandwidth::full; }
693 template<> bandwidth get_ctlnum<bandwidth>::errordefault() { return bandwidth::_auto; }
694 template<> signal get_ctlnum<signal>::errordefault() { return signal::_auto; }
695 template<> application get_ctlnum<application>::errordefault() { return application::audio; }
696 template<> fec get_ctlnum<fec>::errordefault() { return fec::disabled; }
697 template<> lossperc get_ctlnum<lossperc>::errordefault() { return lossperc(0); }
698 template<> dtx get_ctlnum<dtx>::errordefault() { return dtx::disabled; }
699 template<> lsbdepth get_ctlnum<lsbdepth>::errordefault() { return lsbdepth(24); }
700 template<> gain get_ctlnum<gain>::errordefault() { return gain(0); }
701 template<> prediction_disabled get_ctlnum<prediction_disabled>::errordefault() { return prediction_disabled::enabled; }
702 template<> frame_duration get_ctlnum<frame_duration>::errordefault() { return frame_duration::argument; }
704 OpusEncoder* E(encoder& e) { return reinterpret_cast<OpusEncoder*>(e.getmem()); }
705 OpusDecoder* D(decoder& d) { return reinterpret_cast<OpusDecoder*>(d.getmem()); }
706 OpusRepacketizer* R(repacketizer& r) { return reinterpret_cast<OpusRepacketizer*>(r.getmem()); }
707 OpusMSEncoder* ME(multistream_encoder& e) { return reinterpret_cast<OpusMSEncoder*>(e.getmem()); }
708 OpusMSEncoder* ME(surround_encoder& e) { return reinterpret_cast<OpusMSEncoder*>(e.getmem()); }
709 OpusMSDecoder* MD(multistream_decoder& d) { return reinterpret_cast<OpusMSDecoder*>(d.getmem()); }
711 template<typename T> T generic_ctl(encoder& e, int32_t ctl)
713 T val;
714 throwex(opus_encoder_ctl(E(e), ctl, &val));
715 return val;
718 template<typename T> T generic_ctl(encoder& e, int32_t ctl, T val)
720 throwex(opus_encoder_ctl(E(e), ctl, val));
721 return val;
724 template<typename T> T generic_ctl(decoder& d, int32_t ctl)
726 T val;
727 throwex(opus_decoder_ctl(D(d), ctl, &val));
728 return val;
731 template<typename T> T generic_ctl(decoder& d, int32_t ctl, T val)
733 throwex(opus_decoder_ctl(D(d), ctl, val));
734 return val;
737 template<typename T> T generic_ctl(multistream_encoder& e, int32_t ctl, T val)
739 throwex(opus_multistream_encoder_ctl(ME(e), ctl, val));
740 return val;
743 template<typename T> T generic_ctl(surround_encoder& e, int32_t ctl, T val)
745 throwex(opus_multistream_encoder_ctl(ME(e), ctl, val));
746 return val;
749 template<typename T> T generic_ctl(multistream_encoder& e, int32_t ctl)
751 T val;
752 throwex(opus_multistream_encoder_ctl(ME(e), ctl, &val));
753 return val;
756 template<typename T> T generic_ctl(surround_encoder& e, int32_t ctl)
758 T val;
759 throwex(opus_multistream_encoder_ctl(ME(e), ctl, &val));
760 return val;
764 template<typename T> T generic_ctl(multistream_decoder& d, int32_t ctl, T val)
766 throwex(opus_multistream_decoder_ctl(MD(d), ctl, val));
767 return val;
770 template<typename T> T generic_ctl(multistream_decoder& d, int32_t ctl)
772 T val;
773 throwex(opus_multistream_decoder_ctl(MD(d), ctl, &val));
774 return val;
778 template<typename T, typename U> T do_generic_get(U& e)
780 return T(generic_ctl<int32_t>(e, get_ctlnum<T>::num));
783 template<typename T> T generic_eget<T>::operator()(encoder& e) const
785 return do_generic_get<T>(e);
788 template<typename T> T generic_meget<T>::operator()(multistream_encoder& e) const
790 return do_generic_get<T>(e);
793 template<typename T> T generic_meget<T>::operator()(surround_encoder& e) const
795 return do_generic_get<T>(e);
798 template<typename T> T generic_dget<T>::operator()(decoder& d) const
800 return do_generic_get<T>(d);
803 template<typename T> T generic_mdget<T>::operator()(multistream_decoder& d) const
805 return do_generic_get<T>(d);
808 template<typename T> T generic_get<T>::operator()(decoder& d) const
810 return do_generic_get<T>(d);
813 template<typename T> T generic_get<T>::operator()(encoder& e) const
815 return do_generic_get<T>(e);
818 template<typename T> T generic_mget<T>::operator()(multistream_encoder& e) const
820 return do_generic_get<T>(e);
823 template<typename T> T generic_mget<T>::operator()(surround_encoder& e) const
825 return do_generic_get<T>(e);
828 template<typename T> T generic_mget<T>::operator()(multistream_decoder& d) const
830 return do_generic_get<T>(d);
833 template<typename T> T generic_eget<T>::errordefault() const
835 return get_ctlnum<T>::errordefault();
838 template<typename T> T generic_dget<T>::errordefault() const
840 return get_ctlnum<T>::errordefault();
843 template<typename T> T generic_get<T>::errordefault() const
845 return get_ctlnum<T>::errordefault();
848 samplerate samplerate::operator()(encoder& e) const
850 return samplerate(generic_ctl<int32_t>(e, OPUS_GET_SAMPLE_RATE_REQUEST));
853 void complexity::operator()(encoder& e) const
855 generic_ctl(e, OPUS_SET_COMPLEXITY_REQUEST, c);
858 generic_meget<complexity> complexity::get;
860 void bitrate::operator()(encoder& e) const
862 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
865 void bitrate::operator()(multistream_encoder& e) const
867 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
870 void bitrate::operator()(surround_encoder& e) const
872 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
875 generic_meget<bitrate> bitrate::get;
877 void vbr::operator()(encoder& e) const
879 generic_ctl<int32_t>(e, OPUS_SET_VBR_REQUEST, (int32_t)(v ? 1 : 0));
882 generic_meget<vbr> vbr::get;
884 void vbr_constraint::operator()(encoder& e) const
886 generic_ctl<int32_t>(e, OPUS_SET_VBR_CONSTRAINT_REQUEST, (int32_t)(c ? 1 : 0));
889 generic_meget<vbr_constraint> vbr_constraint::get;
891 void force_channels::operator()(encoder& e) const
893 generic_ctl(e, OPUS_SET_FORCE_CHANNELS_REQUEST, f);
896 generic_meget<force_channels> force_channels::get;
898 void max_bandwidth::operator()(encoder& e) const
900 generic_ctl(e, OPUS_SET_MAX_BANDWIDTH_REQUEST, bw);
903 generic_eget<max_bandwidth> max_bandwidth::get;
905 void bandwidth::operator()(encoder& e) const
907 generic_ctl(e, OPUS_SET_BANDWIDTH_REQUEST, bw);
910 generic_mget<bandwidth> bandwidth::get;
912 void signal::operator()(encoder& e) const
914 generic_ctl(e, OPUS_SET_SIGNAL_REQUEST, s);
917 generic_meget<signal> signal::get;
919 void application::operator()(encoder& e) const
921 generic_ctl(e, OPUS_SET_APPLICATION_REQUEST, app);
924 generic_meget<application> application::get;
926 _lookahead _lookahead::operator()(encoder& e) const
928 return _lookahead(generic_ctl<int32_t>(e, OPUS_GET_LOOKAHEAD_REQUEST));
931 void fec::operator()(encoder& e) const
933 generic_ctl<int32_t>(e, OPUS_SET_INBAND_FEC_REQUEST, (int32_t)(f ? 1 : 0));
936 generic_meget<fec> fec::get;
938 void lossperc::operator()(encoder& e) const
940 generic_ctl(e, OPUS_SET_PACKET_LOSS_PERC_REQUEST, (int32_t)loss);
943 generic_meget<lossperc> lossperc::get;
945 void dtx::operator()(encoder& e) const
947 generic_ctl<int32_t>(e, OPUS_SET_DTX_REQUEST, (int32_t)(d ? 1 : 0));
950 generic_meget<dtx> dtx::get;
952 void lsbdepth::operator()(encoder& e) const
954 generic_ctl(e, OPUS_SET_LSB_DEPTH_REQUEST, (int32_t)depth);
957 generic_meget<lsbdepth> lsbdepth::get;
959 _pktduration _pktduration::operator()(decoder& d) const
961 return _pktduration(generic_ctl<int32_t>(d, OPUS_GET_LAST_PACKET_DURATION_REQUEST));
964 void _reset::operator()(decoder& d) const
966 throwex(opus_decoder_ctl(D(d), OPUS_RESET_STATE));
969 void _reset::operator()(encoder& e) const
971 throwex(opus_encoder_ctl(E(e), OPUS_RESET_STATE));
974 void _reset::operator()(multistream_decoder& d) const
976 throwex(opus_multistream_decoder_ctl(MD(d), OPUS_RESET_STATE));
979 void _reset::operator()(multistream_encoder& e) const
981 throwex(opus_multistream_encoder_ctl(ME(e), OPUS_RESET_STATE));
984 void _reset::operator()(surround_encoder& e) const
986 throwex(opus_multistream_encoder_ctl(ME(e), OPUS_RESET_STATE));
989 _finalrange _finalrange::operator()(decoder& d) const
991 return _finalrange(generic_ctl<uint32_t>(d, OPUS_GET_FINAL_RANGE_REQUEST));
994 _finalrange _finalrange::operator()(encoder& e) const
996 return _finalrange(generic_ctl<uint32_t>(e, OPUS_GET_FINAL_RANGE_REQUEST));
999 _pitch _pitch::operator()(encoder& e) const
1001 return _pitch(generic_ctl<uint32_t>(e, OPUS_GET_PITCH_REQUEST));
1004 _pitch _pitch::operator()(decoder& d) const
1006 return _pitch(generic_ctl<uint32_t>(d, OPUS_GET_PITCH_REQUEST));
1009 void gain::operator()(decoder& d) const
1011 generic_ctl(d, OPUS_SET_GAIN_REQUEST, g);
1014 generic_mdget<gain> gain::get;
1016 void prediction_disabled::operator()(encoder& e) const
1018 generic_ctl(e, OPUS_SET_PREDICTION_DISABLED_REQUEST, d ? 1 : 0);
1021 void prediction_disabled::operator()(multistream_encoder& e) const
1023 generic_ctl(e, OPUS_SET_PREDICTION_DISABLED_REQUEST, d ? 1 : 0);
1026 void prediction_disabled::operator()(surround_encoder& e) const
1028 generic_ctl(e, OPUS_SET_PREDICTION_DISABLED_REQUEST, d ? 1 : 0);
1031 generic_meget<prediction_disabled> prediction_disabled::get;
1033 void frame_duration::operator()(encoder& e) const
1035 generic_ctl(e, OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, v);
1038 void frame_duration::operator()(multistream_encoder& e) const
1040 generic_ctl(e, OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, v);
1043 void frame_duration::operator()(surround_encoder& e) const
1045 generic_ctl(e, OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, v);
1049 generic_meget<frame_duration> frame_duration::get;
1051 _last_packet_duration _last_packet_duration::operator()(decoder& d) const
1053 return _last_packet_duration(generic_ctl<int32_t>(d, OPUS_GET_LAST_PACKET_DURATION_REQUEST));
1056 _last_packet_duration _last_packet_duration::operator()(multistream_decoder& d) const
1058 return _last_packet_duration(generic_ctl<int32_t>(d, OPUS_GET_LAST_PACKET_DURATION_REQUEST));
1061 void set_control_int::operator()(encoder& e) const
1063 generic_ctl(e, ctl, val);
1066 void set_control_int::operator()(decoder& d) const
1068 generic_ctl(d, ctl, val);
1071 void set_control_int::operator()(multistream_encoder& e) const
1073 generic_ctl(e, ctl, val);
1076 void set_control_int::operator()(surround_encoder& e) const
1078 generic_ctl(e, ctl, val);
1081 void set_control_int::operator()(multistream_decoder& d) const
1083 generic_ctl(d, ctl, val);
1086 int32_t get_control_int::operator()(encoder& e) const
1088 return generic_ctl<int>(e, ctl);
1091 int32_t get_control_int::operator()(decoder& d) const
1093 return generic_ctl<int>(d, ctl);
1096 int32_t get_control_int::operator()(multistream_encoder& e) const
1098 return generic_ctl<int>(e, ctl);
1101 int32_t get_control_int::operator()(surround_encoder& e) const
1103 return generic_ctl<int>(e, ctl);
1106 int32_t get_control_int::operator()(multistream_decoder& d) const
1108 return generic_ctl<int>(d, ctl);
1111 void force_instantiate()
1113 surround_encoder::stream_format sf;
1114 encoder e(samplerate::r48k, true, application::audio);
1115 decoder d(samplerate::r48k, true);
1116 multistream_encoder me(samplerate::r48k, 1, 1, 0, NULL, application::audio);
1117 multistream_decoder md(samplerate::r48k, 1, 1, 0, NULL);
1118 surround_encoder se(samplerate::r48k, 1, 0, application::audio, sf);
1120 complexity::get(e);
1121 bitrate::get(e);
1122 vbr::get(e);
1123 vbr_constraint::get(e);
1124 force_channels::get(e);
1125 max_bandwidth::get(e);
1126 bandwidth::get(e);
1127 bandwidth::get(d);
1128 signal::get(e);
1129 application::get(e);
1130 fec::get(e);
1131 lossperc::get(e);
1132 dtx::get(e);
1133 lsbdepth::get(e);
1134 gain::get(d);
1135 prediction_disabled::get(e);
1136 frame_duration::get(e);
1137 bitrate::get(me);
1138 lsbdepth::get(me);
1139 vbr::get(me);
1140 vbr_constraint::get(me);
1141 application::get(me);
1142 bandwidth::get(me);
1143 bandwidth::get(md);
1144 complexity::get(me);
1145 lossperc::get(me);
1146 dtx::get(me);
1147 signal::get(me);
1148 fec::get(me);
1149 force_channels::get(me);
1150 prediction_disabled::get(me);
1151 frame_duration::get(me);
1152 bitrate::get(se);
1153 lsbdepth::get(se);
1154 vbr::get(se);
1155 vbr_constraint::get(se);
1156 application::get(se);
1157 bandwidth::get(se);
1158 complexity::get(se);
1159 lossperc::get(se);
1160 dtx::get(se);
1161 signal::get(se);
1162 fec::get(se);
1163 force_channels::get(se);
1164 prediction_disabled::get(se);
1165 frame_duration::get(se);
1167 complexity::get.errordefault();
1168 bitrate::get.errordefault();
1169 vbr::get.errordefault();
1170 vbr_constraint::get.errordefault();
1171 force_channels::get.errordefault();
1172 max_bandwidth::get.errordefault();
1173 bandwidth::get.errordefault();
1174 signal::get.errordefault();
1175 application::get.errordefault();
1176 fec::get.errordefault();
1177 lossperc::get.errordefault();
1178 dtx::get.errordefault();
1179 lsbdepth::get.errordefault();
1180 gain::get.errordefault();
1181 prediction_disabled::get.errordefault();
1182 frame_duration::get.errordefault();
1184 d.ctl(opus::pktduration);
1185 //d.ctl_quiet(opus::pktduration);
1186 e.ctl_quiet(opus::reset);
1187 e.ctl_quiet(opus::finalrange);
1188 e.ctl_quiet(opus::signal::get);
1189 d.ctl_quiet(opus::last_packet_duration);
1192 encoder::~encoder()
1194 if(!user)
1195 delete[] reinterpret_cast<char*>(memory);
1198 encoder& encoder::operator=(const encoder& e)
1200 if(stereo != e.stereo)
1201 throw std::runtime_error("Channel mismatch in assignment");
1202 size_t s = size(stereo);
1203 if(memory != e.memory)
1204 memcpy(memory, e.memory, s);
1205 return *this;
1208 encoder::encoder(samplerate rate, bool _stereo, application app, char* _memory)
1210 size_t s = size(_stereo);
1211 memory = _memory ? _memory : new char[s];
1212 stereo = _stereo;
1213 user = (_memory != NULL);
1214 try {
1215 throwex(opus_encoder_init(E(*this), rate, _stereo ? 2 : 1, app));
1216 } catch(...) {
1217 if(!user)
1218 delete[] reinterpret_cast<char*>(memory);
1219 throw;
1223 encoder::encoder(const encoder& e)
1225 size_t s = size(e.stereo);
1226 memory = new char[s];
1227 memcpy(memory, e.memory, s);
1228 stereo = e.stereo;
1229 user = false;
1232 encoder::encoder(const encoder& e, char* _memory)
1234 size_t s = size(e.stereo);
1235 memory = _memory;
1236 memcpy(memory, e.memory, s);
1237 stereo = e.stereo;
1238 user = true;
1241 encoder::encoder(void* state, bool _stereo)
1243 memory = state;
1244 stereo = _stereo;
1245 user = true;
1248 size_t encoder::size(bool stereo)
1250 return opus_encoder_get_size(stereo ? 2 : 1);
1253 size_t encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1255 return throwex(opus_encode(E(*this), in, inframes, out, maxout));
1258 size_t encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1260 return throwex(opus_encode_float(E(*this), in, inframes, out, maxout));
1263 decoder::~decoder()
1265 if(!user)
1266 delete[] reinterpret_cast<char*>(memory);
1269 decoder::decoder(samplerate rate, bool _stereo, char* _memory)
1271 size_t s = size(_stereo);
1272 memory = _memory ? _memory : new char[s];
1273 stereo = _stereo;
1274 user = (_memory != NULL);
1275 try {
1276 throwex(opus_decoder_init(D(*this), rate, _stereo ? 2 : 1));
1277 } catch(...) {
1278 if(!user)
1279 delete[] reinterpret_cast<char*>(memory);
1280 throw;
1284 decoder& decoder::operator=(const decoder& d)
1286 if(stereo != d.stereo)
1287 throw std::runtime_error("Channel mismatch in assignment");
1288 size_t s = size(stereo);
1289 if(memory != d.memory)
1290 memcpy(memory, d.memory, s);
1291 return *this;
1294 size_t decoder::size(bool stereo)
1296 return opus_decoder_get_size(stereo ? 2 : 1);
1299 decoder::decoder(const decoder& d)
1301 size_t s = size(d.stereo);
1302 memory = new char[s];
1303 memcpy(memory, d.memory, s);
1304 stereo = d.stereo;
1305 user = false;
1308 decoder::decoder(const decoder& d, char* _memory)
1310 size_t s = size(d.stereo);
1311 memory = _memory;
1312 memcpy(memory, d.memory, s);
1313 stereo = d.stereo;
1314 user = true;
1317 decoder::decoder(void* state, bool _stereo)
1319 memory = state;
1320 stereo = _stereo;
1321 user = true;
1324 size_t decoder::decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes, bool decode_fec)
1326 return throwex(opus_decode(D(*this), in, insize, out, maxframes, decode_fec ? 1 : 0));
1329 size_t decoder::decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes, bool decode_fec)
1331 return throwex(opus_decode_float(D(*this), in, insize, out, maxframes, decode_fec ? 1 : 0));
1334 uint32_t decoder::get_nb_samples(const unsigned char* buf, size_t bufsize)
1336 return throwex(opus_decoder_get_nb_samples(D(*this), buf, bufsize));
1339 repacketizer::repacketizer(char* _memory)
1341 size_t s = size();
1342 memory = _memory ? _memory : new char[s];
1343 user = false;
1344 opus_repacketizer_init(R(*this));
1347 repacketizer::repacketizer(const repacketizer& rp)
1349 size_t s = size();
1350 memory = new char[s];
1351 user = false;
1352 memcpy(memory, rp.memory, s);
1355 repacketizer::repacketizer(const repacketizer& rp, char* _memory)
1357 memory = _memory;
1358 user = true;
1359 memcpy(memory, rp.memory, size());
1362 repacketizer& repacketizer::operator=(const repacketizer& rp)
1364 if(memory != rp.memory)
1365 memcpy(memory, rp.memory, size());
1366 return *this;
1369 repacketizer::~repacketizer()
1371 if(!user)
1372 delete[] reinterpret_cast<char*>(memory);
1375 size_t repacketizer::size()
1377 return opus_repacketizer_get_size();
1380 void repacketizer::cat(const unsigned char* data, size_t len)
1382 throwex(opus_repacketizer_cat(R(*this), data, len));
1385 size_t repacketizer::out(unsigned char* data, size_t maxlen)
1387 return throwex(opus_repacketizer_out(R(*this), data, maxlen));
1390 size_t repacketizer::out(unsigned char* data, size_t maxlen, unsigned begin, unsigned end)
1392 return throwex(opus_repacketizer_out_range(R(*this), begin, end, data, maxlen));
1395 unsigned repacketizer::get_nb_frames()
1397 return opus_repacketizer_get_nb_frames(R(*this));
1400 encoder& multistream_encoder::operator[](size_t idx)
1402 if(idx > (size_t)streams)
1403 throw opus::bad_argument();
1404 return *reinterpret_cast<encoder*>(substream(idx));
1407 size_t multistream_encoder::size(unsigned streams, unsigned coupled_streams)
1409 return alignof(encoder) + streams * sizeof(encoder) + opus_multistream_encoder_get_size(streams,
1410 coupled_streams);
1413 multistream_encoder::~multistream_encoder()
1415 if(!user)
1416 delete[] memory;
1419 multistream_encoder::multistream_encoder(samplerate rate, unsigned _channels, unsigned _streams,
1420 unsigned coupled_streams, const unsigned char* mapping, application app, char* _memory)
1422 user = (_memory != NULL);
1423 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[size(streams, coupled_streams)];
1424 try {
1425 offset = _streams * sizeof(encoder);
1426 throwex(opus_multistream_encoder_init(ME(*this), rate, _channels, _streams, coupled_streams, mapping,
1427 app));
1428 init_structures(_channels, _streams, coupled_streams);
1429 } catch(...) {
1430 if(!user)
1431 delete[] memory;
1432 throw;
1436 multistream_encoder::multistream_encoder(const multistream_encoder& e)
1438 init_copy(e, new char[e.size()]);
1439 user = false;
1442 multistream_encoder::multistream_encoder(const multistream_encoder& e, char* memory)
1444 init_copy(e, memory);
1445 user = true;
1448 multistream_encoder& multistream_encoder::operator=(const multistream_encoder& e)
1450 if(this == &e)
1451 return *this;
1452 if(streams != e.streams || coupled != e.coupled)
1453 throw std::runtime_error("Stream mismatch in assignment");
1454 memcpy(ME(*this), e.memory + e.offset, opussize);
1455 channels = e.channels;
1456 return *this;
1459 void multistream_encoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled)
1461 channels = _channels;
1462 streams = _streams;
1463 coupled = _coupled;
1464 opussize = opus_multistream_encoder_get_size(streams, coupled);
1465 for(int32_t i = 0; i < streams; i++) {
1466 OpusEncoder* e;
1467 opus_multistream_encoder_ctl(ME(*this), OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST, (int)i, &e);
1468 new(substream(i)) encoder(e, i < coupled);
1472 void multistream_encoder::init_copy(const multistream_encoder& e, char* _memory)
1474 user = (_memory != NULL);
1475 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[e.size()];
1476 offset = e.offset;
1477 memcpy(ME(*this), e.memory + e.offset, e.opussize);
1478 init_structures(e.channels, e.streams, e.coupled);
1481 size_t multistream_encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1483 return throwex(opus_multistream_encode(ME(*this), in, inframes, out, maxout));
1486 size_t multistream_encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1488 return throwex(opus_multistream_encode_float(ME(*this), in, inframes, out, maxout));
1491 char* multistream_encoder::substream(size_t idx)
1493 return memory + idx * sizeof(encoder);
1496 encoder& surround_encoder::operator[](size_t idx)
1498 if(idx > (size_t)streams)
1499 throw opus::bad_argument();
1500 return *reinterpret_cast<encoder*>(substream(idx));
1503 size_t surround_encoder::size(unsigned channels, unsigned family)
1505 //Be conservative with memory, as stream count is not known.
1506 if(SURROUND_SUPPORTED)
1507 return alignof(encoder) + channels * sizeof(encoder) + opus_multistream_surround_encoder_get_size(channels,
1508 family);
1509 int streams, coupled;
1510 lookup_params(channels, family, streams, coupled);
1511 //We use channels and not streams here to keep compatiblity on fallback.
1512 return alignof(encoder) + channels * sizeof(encoder) + opus_multistream_encoder_get_size(streams, coupled);
1515 surround_encoder::~surround_encoder()
1517 if(!user)
1518 delete[] memory;
1521 surround_encoder::surround_encoder(samplerate rate, unsigned _channels, unsigned _family, application app,
1522 stream_format& format, char* _memory)
1524 user = (_memory != NULL);
1525 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[size(channels, family)];
1526 try {
1527 int rstreams, rcoupled;
1528 unsigned char rmapping[256];
1529 offset = _channels * sizeof(encoder); //Conservative.
1530 if(SURROUND_SUPPORTED)
1531 throwex(opus_multistream_surround_encoder_init(ME(*this), rate, _channels, _family, &rstreams,
1532 &rcoupled, rmapping, app));
1533 else
1535 lookup_params(_channels, _family, rstreams, rcoupled);
1536 generate_mapping(_channels, _family, rmapping);
1537 throwex(opus_multistream_encoder_init(ME(*this), rate, channels, rstreams, rcoupled, rmapping, app));
1539 init_structures(_channels, rstreams, rcoupled, _family);
1540 format.channels = channels;
1541 format.family = family;
1542 format.streams = streams;
1543 format.coupled = coupled;
1544 memcpy(format.mapping, rmapping, channels);
1545 } catch(...) {
1546 if(!user)
1547 delete[] memory;
1548 throw;
1552 surround_encoder::surround_encoder(const surround_encoder& e)
1554 init_copy(e, new char[e.size()]);
1555 user = false;
1558 surround_encoder::surround_encoder(const surround_encoder& e, char* memory)
1560 init_copy(e, memory);
1561 user = true;
1564 surround_encoder& surround_encoder::operator=(const surround_encoder& e)
1566 if(this == &e)
1567 return *this;
1568 if(channels != e.channels || family != e.family)
1569 throw std::runtime_error("Stream mismatch in assignment");
1570 memcpy(ME(*this), e.memory + e.offset, opussize);
1571 return *this;
1574 void surround_encoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled, unsigned _family)
1576 channels = _channels;
1577 streams = _streams;
1578 coupled = _coupled;
1579 family = _family;
1580 if(SURROUND_SUPPORTED)
1581 opussize = opus_multistream_surround_encoder_get_size(_channels, family);
1582 else
1583 opussize = opus_multistream_encoder_get_size(streams, coupled);
1584 for(int32_t i = 0; i < streams; i++) {
1585 OpusEncoder* e;
1586 opus_multistream_encoder_ctl(ME(*this), OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST, (int)i, &e);
1587 new(substream(i)) encoder(e, i < coupled);
1591 void surround_encoder::init_copy(const surround_encoder& e, char* _memory)
1593 user = (_memory != NULL);
1594 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[e.size()];
1595 offset = e.offset;
1596 memcpy(ME(*this), e.memory + e.offset, e.opussize);
1597 init_structures(e.channels, e.streams, e.coupled, e.family);
1600 size_t surround_encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1602 return throwex(opus_multistream_encode(ME(*this), in, inframes, out, maxout));
1605 size_t surround_encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1607 return throwex(opus_multistream_encode_float(ME(*this), in, inframes, out, maxout));
1610 char* surround_encoder::substream(size_t idx)
1612 return memory + idx * sizeof(encoder);
1615 decoder& multistream_decoder::operator[](size_t idx)
1617 if(idx > (size_t)streams)
1618 throw opus::bad_argument();
1619 return *reinterpret_cast<decoder*>(substream(idx));
1622 size_t multistream_decoder::size(unsigned streams, unsigned coupled_streams)
1624 return streams * sizeof(decoder) + opus_multistream_decoder_get_size(streams,
1625 coupled_streams);
1628 multistream_decoder::multistream_decoder(samplerate rate, unsigned _channels, unsigned _streams,
1629 unsigned coupled_streams, const unsigned char* mapping, char* _memory)
1631 user = (_memory != NULL);
1632 memory = _memory ? alignptr(_memory, alignof(decoder)) : new char[size(streams, coupled_streams)];
1633 try {
1634 offset = _streams * sizeof(encoder);
1635 throwex(opus_multistream_decoder_init(MD(*this), rate, _channels, _streams, coupled_streams,
1636 mapping));
1637 init_structures(_channels, _streams, coupled_streams);
1638 } catch(...) {
1639 if(!user)
1640 delete[] memory;
1641 throw;
1644 multistream_decoder::multistream_decoder(const multistream_decoder& e)
1646 init_copy(e, NULL);
1649 multistream_decoder::multistream_decoder(const multistream_decoder& e, char* memory)
1651 init_copy(e, memory);
1654 multistream_decoder::~multistream_decoder()
1656 if(!user)
1657 delete[] memory;
1660 multistream_decoder& multistream_decoder::operator=(const multistream_decoder& d)
1662 if(this == &d)
1663 return *this;
1664 if(streams != d.streams || coupled != d.coupled)
1665 throw std::runtime_error("Stream mismatch in assignment");
1666 memcpy(MD(*this), d.memory + d.offset, opussize);
1667 channels = d.channels;
1668 return *this;
1671 void multistream_decoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled)
1673 channels = _channels;
1674 streams = _streams;
1675 coupled = _coupled;
1676 opussize = opus_multistream_decoder_get_size(streams, coupled);
1677 for(uint32_t i = 0; i < (size_t)streams; i++) {
1678 OpusDecoder* d;
1679 opus_multistream_decoder_ctl(MD(*this), OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST, (int)i, &d);
1680 new(substream(i)) decoder(d, i < coupled);
1684 void multistream_decoder::init_copy(const multistream_decoder& e, char* _memory)
1686 user = (_memory != NULL);
1687 memory = _memory ? alignptr(_memory, alignof(decoder)) : new char[e.size()];
1688 offset = e.offset;
1689 memcpy(MD(*this), e.memory + e.offset, e.opussize);
1690 init_structures(e.channels, e.streams, e.coupled);
1693 size_t multistream_decoder::decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes,
1694 bool decode_fec)
1696 return throwex(opus_multistream_decode(MD(*this), in, insize, out, maxframes, decode_fec));
1699 size_t multistream_decoder::decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes,
1700 bool decode_fec)
1702 return throwex(opus_multistream_decode_float(MD(*this), in, insize, out, maxframes, decode_fec));
1705 char* multistream_decoder::substream(size_t idx)
1707 return memory + idx * sizeof(encoder);
1710 uint32_t packet_get_nb_frames(const unsigned char* packet, size_t len)
1712 return throwex(opus_packet_get_nb_frames(packet, len));
1715 uint32_t packet_get_samples_per_frame(const unsigned char* data, samplerate fs)
1717 return throwex(opus_packet_get_samples_per_frame(data, fs));
1720 uint32_t packet_get_nb_samples(const unsigned char* packet, size_t len, samplerate fs)
1722 return packet_get_nb_frames(packet, len) * packet_get_samples_per_frame(packet, fs);
1725 uint32_t packet_get_nb_channels(const unsigned char* packet)
1727 return throwex(opus_packet_get_nb_channels(packet));
1730 bandwidth packet_get_bandwidth(const unsigned char* packet)
1732 return bandwidth(throwex(opus_packet_get_bandwidth(packet)));
1735 parsed_packet packet_parse(const unsigned char* packet, size_t len)
1737 parsed_packet p;
1738 unsigned char toc;
1739 const unsigned char* frames[48];
1740 short size[48];
1741 int payload_offset;
1742 uint32_t framecnt = throwex(opus_packet_parse(packet, len, &toc, frames, size, &payload_offset));
1743 p.toc = toc;
1744 p.payload_offset = payload_offset;
1745 p.frames.resize(framecnt);
1746 for(unsigned i = 0; i < framecnt; i++) {
1747 p.frames[i].ptr = frames[i];
1748 p.frames[i].size = size[i];
1750 return p;
1753 std::string version()
1755 return opus_get_version_string();
1758 uint8_t packet_tick_count(const uint8_t* packet, size_t packetsize)
1760 if(packetsize < 1)
1761 return 0;
1762 uint8_t x = ((packet[0] >= 0x70) ? 1 : 4) << ((packet[0] >> 3) & 3);
1763 x = std::min(x, (uint8_t)24);
1764 uint8_t y = (packetsize < 2) ? 255 : (packet[1] & 0x3F);
1765 uint16_t z = (uint16_t)x * y;
1766 switch(packet[0] & 3) {
1767 case 0: return x;
1768 case 1: return x << 1;
1769 case 2: return x << 1;
1770 case 3: return (z <= 48) ? z : 0;
1772 return 0; //NOTREACHED.