opus.hpp: Untested (but compiles) support for surround API
[lsnes.git] / src / library / opus.cpp
blob2cd1938eca9998188c506fb0b589f783603a5e97
1 #ifdef WITH_OPUS_CODEC
2 #define OPUS_BUILD
3 #include "opus.hpp"
4 #include <opus.h>
5 #include <opus_defines.h>
6 #include <opus_multistream.h>
7 #include <sstream>
8 #include <cstring>
10 //Some of these might not be in stable.
11 #ifndef OPUS_SET_GAIN_REQUEST
12 #define OPUS_SET_GAIN_REQUEST 4034
13 #endif
14 #ifndef OPUS_GET_GAIN_REQUEST
15 #define OPUS_GET_GAIN_REQUEST 4045
16 #endif
17 #ifndef OPUS_SET_LSB_DEPTH_REQUEST
18 #define OPUS_SET_LSB_DEPTH_REQUEST 4036
19 #endif
20 #ifndef OPUS_GET_LSB_DEPTH_REQUEST
21 #define OPUS_GET_LSB_DEPTH_REQUEST 4037
22 #endif
23 #ifndef OPUS_GET_LAST_PACKET_DURATION_REQUEST
24 #define OPUS_GET_LAST_PACKET_DURATION_REQUEST 4039
25 #endif
27 namespace opus
29 samplerate samplerate::r8k(8000);
30 samplerate samplerate::r12k(12000);
31 samplerate samplerate::r16k(16000);
32 samplerate samplerate::r24k(24000);
33 samplerate samplerate::r48k(48000);
34 bitrate bitrate::_auto(OPUS_AUTO);
35 bitrate bitrate::max(OPUS_BITRATE_MAX);
36 vbr vbr::cbr(false);
37 vbr vbr::_vbr(true);
38 vbr_constraint vbr_constraint::unconstrained(false);
39 vbr_constraint vbr_constraint::constrained(true);
40 force_channels force_channels::_auto(OPUS_AUTO);
41 force_channels force_channels::mono(1);
42 force_channels force_channels::stereo(2);
43 max_bandwidth max_bandwidth::narrow(OPUS_BANDWIDTH_NARROWBAND);
44 max_bandwidth max_bandwidth::medium(OPUS_BANDWIDTH_MEDIUMBAND);
45 max_bandwidth max_bandwidth::wide(OPUS_BANDWIDTH_WIDEBAND);
46 max_bandwidth max_bandwidth::superwide(OPUS_BANDWIDTH_SUPERWIDEBAND);
47 max_bandwidth max_bandwidth::full(OPUS_BANDWIDTH_FULLBAND);
48 bandwidth bandwidth::_auto(OPUS_AUTO);
49 bandwidth bandwidth::narrow(OPUS_BANDWIDTH_NARROWBAND);
50 bandwidth bandwidth::medium(OPUS_BANDWIDTH_MEDIUMBAND);
51 bandwidth bandwidth::wide(OPUS_BANDWIDTH_WIDEBAND);
52 bandwidth bandwidth::superwide(OPUS_BANDWIDTH_SUPERWIDEBAND);
53 bandwidth bandwidth::full(OPUS_BANDWIDTH_FULLBAND);
54 signal signal::_auto(OPUS_AUTO);
55 signal signal::music(OPUS_SIGNAL_MUSIC);
56 signal signal::voice(OPUS_SIGNAL_VOICE);
57 application application::audio(OPUS_APPLICATION_AUDIO);
58 application application::voice(OPUS_APPLICATION_VOIP);
59 application application::lowdelay(OPUS_APPLICATION_RESTRICTED_LOWDELAY);
60 fec fec::disabled(false);
61 fec fec::enabled(true);
62 dtx dtx::disabled(false);
63 dtx dtx::enabled(true);
64 lsbdepth lsbdepth::d8(8);
65 lsbdepth lsbdepth::d16(16);
66 lsbdepth lsbdepth::d24(24);
67 _reset reset;
68 _finalrange finalrange;
69 _pitch pitch;
70 _pktduration pktduration;
71 _lookahead lookahead;
73 bad_argument::bad_argument()
74 : std::runtime_error("Invalid argument") {}
76 buffer_too_small::buffer_too_small()
77 : std::runtime_error("Buffer too small") {}
79 internal_error::internal_error()
80 : std::runtime_error("Internal error") {}
82 invalid_packet::invalid_packet()
83 : std::runtime_error("Invalid packet") {}
85 unimplemented::unimplemented()
86 : std::runtime_error("Not implemented") {}
88 invalid_state::invalid_state()
89 : std::runtime_error("Invalid state") {}
91 const unsigned f1_streams[] = {0, 1, 1, 2, 2, 3, 4, 4, 5};
92 const unsigned f1_coupled[] = {0, 0, 1, 1, 2, 2, 2, 3, 3};
93 const char* chanmap[] = {"", "A", "AB", "ACB", "ABCD", "ACDEB", "ACDEBF", "ACBFDEG", "ACDEFGBH"};
95 void lookup_params(unsigned channels, unsigned family, int& streams, int& coupled)
97 if(channels == 0)
98 throw bad_argument();
99 if(family == 0) {
100 if(channels > 2)
101 throw unimplemented();
102 streams = 1;
103 coupled = (channels == 2) ? 1 : 0;
104 } else if(family == 1) {
105 if(channels > 8)
106 throw unimplemented();
107 streams = f1_streams[channels];
108 coupled = f1_coupled[channels];
109 } else
110 throw unimplemented();
113 void generate_mapping(unsigned channels, unsigned family, unsigned char* mapping)
115 for(unsigned i = 0; i < channels; i++)
116 mapping[i] = chanmap[channels][i] - 'A';
119 char* alignptr(char* ptr, size_t align)
121 while((intptr_t)ptr % align)
122 ptr++;
123 return ptr;
126 int32_t throwex(int32_t ret)
128 if(ret >= 0)
129 return ret;
130 if(ret == OPUS_BAD_ARG)
131 throw bad_argument();
132 if(ret == OPUS_BUFFER_TOO_SMALL)
133 throw buffer_too_small();
134 if(ret == OPUS_INTERNAL_ERROR)
135 throw internal_error();
136 if(ret == OPUS_INVALID_PACKET)
137 throw invalid_packet();
138 if(ret == OPUS_UNIMPLEMENTED)
139 throw unimplemented();
140 if(ret == OPUS_INVALID_STATE)
141 throw invalid_state();
142 if(ret == OPUS_ALLOC_FAIL)
143 throw std::bad_alloc();
144 std::ostringstream s;
145 s << "Unknown error code " << ret << " from libopus.";
146 throw std::runtime_error(s.str());
149 template<typename T> struct get_ctlnum { const static int32_t num; static T errordefault(); };
151 template<> const int32_t get_ctlnum<complexity>::num = OPUS_GET_COMPLEXITY_REQUEST;
152 template<> const int32_t get_ctlnum<bitrate>::num = OPUS_GET_BITRATE_REQUEST;
153 template<> const int32_t get_ctlnum<vbr>::num = OPUS_GET_VBR_REQUEST;
154 template<> const int32_t get_ctlnum<vbr_constraint>::num = OPUS_GET_VBR_CONSTRAINT_REQUEST;
155 template<> const int32_t get_ctlnum<force_channels>::num = OPUS_GET_FORCE_CHANNELS_REQUEST;
156 template<> const int32_t get_ctlnum<max_bandwidth>::num = OPUS_GET_MAX_BANDWIDTH_REQUEST;
157 template<> const int32_t get_ctlnum<bandwidth>::num = OPUS_GET_BANDWIDTH_REQUEST;
158 template<> const int32_t get_ctlnum<signal>::num = OPUS_GET_SIGNAL_REQUEST;
159 template<> const int32_t get_ctlnum<application>::num = OPUS_GET_APPLICATION_REQUEST;
160 template<> const int32_t get_ctlnum<fec>::num = OPUS_GET_INBAND_FEC_REQUEST;
161 template<> const int32_t get_ctlnum<lossperc>::num = OPUS_GET_PACKET_LOSS_PERC_REQUEST;
162 template<> const int32_t get_ctlnum<dtx>::num = OPUS_GET_DTX_REQUEST;
163 template<> const int32_t get_ctlnum<lsbdepth>::num = OPUS_GET_LSB_DEPTH_REQUEST;
164 template<> const int32_t get_ctlnum<gain>::num = OPUS_GET_GAIN_REQUEST;
166 template<> complexity get_ctlnum<complexity>::errordefault() { return complexity(10); }
167 template<> bitrate get_ctlnum<bitrate>::errordefault() { return bitrate::_auto; }
168 template<> vbr get_ctlnum<vbr>::errordefault() { return vbr::_vbr; }
169 template<> vbr_constraint get_ctlnum<vbr_constraint>::errordefault() { return vbr_constraint::unconstrained; }
170 template<> force_channels get_ctlnum<force_channels>::errordefault() { return force_channels::_auto; }
171 template<> max_bandwidth get_ctlnum<max_bandwidth>::errordefault() { return max_bandwidth::full; }
172 template<> bandwidth get_ctlnum<bandwidth>::errordefault() { return bandwidth::_auto; }
173 template<> signal get_ctlnum<signal>::errordefault() { return signal::_auto; }
174 template<> application get_ctlnum<application>::errordefault() { return application::audio; }
175 template<> fec get_ctlnum<fec>::errordefault() { return fec::disabled; }
176 template<> lossperc get_ctlnum<lossperc>::errordefault() { return lossperc(0); }
177 template<> dtx get_ctlnum<dtx>::errordefault() { return dtx::disabled; }
178 template<> lsbdepth get_ctlnum<lsbdepth>::errordefault() { return lsbdepth(24); }
179 template<> gain get_ctlnum<gain>::errordefault() { return gain(0); }
182 OpusEncoder* E(encoder& e) { return reinterpret_cast<OpusEncoder*>(e.getmem()); }
183 OpusDecoder* D(decoder& d) { return reinterpret_cast<OpusDecoder*>(d.getmem()); }
184 OpusRepacketizer* R(repacketizer& r) { return reinterpret_cast<OpusRepacketizer*>(r.getmem()); }
185 OpusMSEncoder* ME(multistream_encoder& e) { return reinterpret_cast<OpusMSEncoder*>(e.getmem()); }
186 OpusMSEncoder* ME(surround_encoder& e) { return reinterpret_cast<OpusMSEncoder*>(e.getmem()); }
187 OpusMSDecoder* MD(multistream_decoder& d) { return reinterpret_cast<OpusMSDecoder*>(d.getmem()); }
189 template<typename T> T generic_ctl(encoder& e, int32_t ctl)
191 T val;
192 throwex(opus_encoder_ctl(E(e), ctl, &val));
193 return val;
196 template<typename T> T generic_ctl(encoder& e, int32_t ctl, T val)
198 throwex(opus_encoder_ctl(E(e), ctl, val));
201 template<typename T> T generic_ctl(decoder& d, int32_t ctl)
203 T val;
204 throwex(opus_decoder_ctl(D(d), ctl, &val));
205 return val;
208 template<typename T> T generic_ctl(decoder& d, int32_t ctl, T val)
210 throwex(opus_decoder_ctl(D(d), ctl, val));
213 template<typename T> T generic_ctl(multistream_encoder& e, int32_t ctl, T val)
215 throwex(opus_multistream_encoder_ctl(ME(e), ctl, val));
218 template<typename T> T generic_ctl(surround_encoder& e, int32_t ctl, T val)
220 throwex(opus_multistream_encoder_ctl(ME(e), ctl, val));
223 template<typename T> T generic_ctl(multistream_encoder& e, int32_t ctl)
225 T val;
226 throwex(opus_multistream_encoder_ctl(ME(e), ctl, &val));
227 return val;
230 template<typename T> T generic_ctl(surround_encoder& e, int32_t ctl)
232 T val;
233 throwex(opus_multistream_encoder_ctl(ME(e), ctl, &val));
234 return val;
238 template<typename T> T generic_ctl(multistream_decoder& d, int32_t ctl, T val)
240 throwex(opus_multistream_decoder_ctl(MD(d), ctl, val));
243 template<typename T> T generic_ctl(multistream_decoder& d, int32_t ctl)
245 T val;
246 throwex(opus_multistream_decoder_ctl(MD(d), ctl, &val));
247 return val;
251 template<typename T, typename U> T do_generic_get(U& e)
253 return T(generic_ctl<int32_t>(e, get_ctlnum<T>::num));
256 template<typename T> T generic_eget<T>::operator()(encoder& e) const
258 return do_generic_get<T>(e);
261 template<typename T> T generic_meget<T>::operator()(multistream_encoder& e) const
263 return do_generic_get<T>(e);
266 template<typename T> T generic_meget<T>::operator()(surround_encoder& e) const
268 return do_generic_get<T>(e);
271 template<typename T> T generic_dget<T>::operator()(decoder& d) const
273 return do_generic_get<T>(d);
276 template<typename T> T generic_mdget<T>::operator()(multistream_decoder& d) const
278 return do_generic_get<T>(d);
281 template<typename T> T generic_get<T>::operator()(decoder& d) const
283 return do_generic_get<T>(d);
286 template<typename T> T generic_get<T>::operator()(encoder& e) const
288 return do_generic_get<T>(e);
291 template<typename T> T generic_mget<T>::operator()(multistream_encoder& e) const
293 return do_generic_get<T>(e);
296 template<typename T> T generic_mget<T>::operator()(surround_encoder& e) const
298 return do_generic_get<T>(e);
301 template<typename T> T generic_mget<T>::operator()(multistream_decoder& d) const
303 return do_generic_get<T>(d);
306 template<typename T> T generic_eget<T>::errordefault() const
308 return get_ctlnum<T>::errordefault();
311 template<typename T> T generic_dget<T>::errordefault() const
313 return get_ctlnum<T>::errordefault();
316 template<typename T> T generic_get<T>::errordefault() const
318 return get_ctlnum<T>::errordefault();
321 samplerate samplerate::operator()(encoder& e) const
323 return samplerate(generic_ctl<int32_t>(e, OPUS_GET_SAMPLE_RATE_REQUEST));
326 void complexity::operator()(encoder& e) const
328 generic_ctl(e, OPUS_SET_COMPLEXITY_REQUEST, c);
331 generic_meget<complexity> complexity::get;
333 void bitrate::operator()(encoder& e) const
335 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
338 void bitrate::operator()(multistream_encoder& e) const
340 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
343 void bitrate::operator()(surround_encoder& e) const
345 generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b);
348 generic_meget<bitrate> bitrate::get;
350 void vbr::operator()(encoder& e) const
352 generic_ctl<int32_t>(e, OPUS_SET_VBR_REQUEST, (int32_t)(v ? 1 : 0));
355 generic_meget<vbr> vbr::get;
357 void vbr_constraint::operator()(encoder& e) const
359 generic_ctl<int32_t>(e, OPUS_SET_VBR_CONSTRAINT_REQUEST, (int32_t)(c ? 1 : 0));
362 generic_meget<vbr_constraint> vbr_constraint::get;
364 void force_channels::operator()(encoder& e) const
366 generic_ctl(e, OPUS_SET_FORCE_CHANNELS_REQUEST, f);
369 generic_meget<force_channels> force_channels::get;
371 void max_bandwidth::operator()(encoder& e) const
373 generic_ctl(e, OPUS_SET_MAX_BANDWIDTH_REQUEST, bw);
376 generic_eget<max_bandwidth> max_bandwidth::get;
378 void bandwidth::operator()(encoder& e) const
380 generic_ctl(e, OPUS_SET_BANDWIDTH_REQUEST, bw);
383 generic_mget<bandwidth> bandwidth::get;
385 void signal::operator()(encoder& e) const
387 generic_ctl(e, OPUS_SET_SIGNAL_REQUEST, s);
390 generic_meget<signal> signal::get;
392 void application::operator()(encoder& e) const
394 generic_ctl(e, OPUS_SET_APPLICATION_REQUEST, app);
397 generic_meget<application> application::get;
399 _lookahead _lookahead::operator()(encoder& e) const
401 return _lookahead(generic_ctl<int32_t>(e, OPUS_GET_LOOKAHEAD_REQUEST));
404 void fec::operator()(encoder& e) const
406 generic_ctl<int32_t>(e, OPUS_SET_INBAND_FEC_REQUEST, (int32_t)(f ? 1 : 0));
409 generic_meget<fec> fec::get;
411 void lossperc::operator()(encoder& e) const
413 generic_ctl(e, OPUS_SET_PACKET_LOSS_PERC_REQUEST, (int32_t)loss);
416 generic_meget<lossperc> lossperc::get;
418 void dtx::operator()(encoder& e) const
420 generic_ctl<int32_t>(e, OPUS_SET_DTX_REQUEST, (int32_t)(d ? 1 : 0));
423 generic_meget<dtx> dtx::get;
425 void lsbdepth::operator()(encoder& e) const
427 generic_ctl(e, OPUS_SET_LSB_DEPTH_REQUEST, (int32_t)depth);
430 generic_meget<lsbdepth> lsbdepth::get;
432 _pktduration _pktduration::operator()(decoder& d) const
434 return _pktduration(generic_ctl<int32_t>(d, OPUS_GET_LAST_PACKET_DURATION_REQUEST));
437 void _reset::operator()(decoder& d) const
439 throwex(opus_decoder_ctl(D(d), OPUS_RESET_STATE));
442 void _reset::operator()(encoder& e) const
444 throwex(opus_encoder_ctl(E(e), OPUS_RESET_STATE));
447 void _reset::operator()(multistream_decoder& d) const
449 throwex(opus_multistream_decoder_ctl(MD(d), OPUS_RESET_STATE));
452 void _reset::operator()(multistream_encoder& e) const
454 throwex(opus_multistream_encoder_ctl(ME(e), OPUS_RESET_STATE));
457 void _reset::operator()(surround_encoder& e) const
459 throwex(opus_multistream_encoder_ctl(ME(e), OPUS_RESET_STATE));
462 _finalrange _finalrange::operator()(decoder& d) const
464 return _finalrange(generic_ctl<uint32_t>(d, OPUS_GET_FINAL_RANGE_REQUEST));
467 _finalrange _finalrange::operator()(encoder& e) const
469 return _finalrange(generic_ctl<uint32_t>(e, OPUS_GET_FINAL_RANGE_REQUEST));
472 _pitch _pitch::operator()(encoder& e) const
474 return _pitch(generic_ctl<uint32_t>(e, OPUS_GET_PITCH_REQUEST));
477 _pitch _pitch::operator()(decoder& d) const
479 return _pitch(generic_ctl<uint32_t>(d, OPUS_GET_PITCH_REQUEST));
482 void gain::operator()(decoder& d) const
484 generic_ctl(d, OPUS_SET_GAIN_REQUEST, g);
487 generic_mdget<gain> gain::get;
489 void set_control_int::operator()(encoder& e) const
491 generic_ctl(e, ctl, val);
494 void set_control_int::operator()(decoder& d) const
496 generic_ctl(d, ctl, val);
499 void set_control_int::operator()(multistream_encoder& e) const
501 generic_ctl(e, ctl, val);
504 void set_control_int::operator()(surround_encoder& e) const
506 generic_ctl(e, ctl, val);
509 void set_control_int::operator()(multistream_decoder& d) const
511 generic_ctl(d, ctl, val);
514 int32_t get_control_int::operator()(encoder& e) const
516 return generic_ctl<int32_t>(e, ctl);
519 int32_t get_control_int::operator()(decoder& d) const
521 return generic_ctl<int32_t>(d, ctl);
524 int32_t get_control_int::operator()(multistream_encoder& e) const
526 return generic_ctl<int32_t>(e, ctl);
529 int32_t get_control_int::operator()(surround_encoder& e) const
531 return generic_ctl<int32_t>(e, ctl);
534 int32_t get_control_int::operator()(multistream_decoder& d) const
536 return generic_ctl<int32_t>(d, ctl);
539 void force_instantiate()
541 surround_encoder::stream_format sf;
542 encoder e(samplerate::r48k, true, application::audio);
543 decoder d(samplerate::r48k, true);
544 multistream_encoder me(samplerate::r48k, 1, 1, 0, NULL, application::audio);
545 multistream_decoder md(samplerate::r48k, 1, 1, 0, NULL);
546 surround_encoder se(samplerate::r48k, 1, 0, application::audio, sf);
548 complexity::get(e);
549 bitrate::get(e);
550 vbr::get(e);
551 vbr_constraint::get(e);
552 force_channels::get(e);
553 max_bandwidth::get(e);
554 bandwidth::get(e);
555 bandwidth::get(d);
556 signal::get(e);
557 application::get(e);
558 fec::get(e);
559 lossperc::get(e);
560 dtx::get(e);
561 lsbdepth::get(e);
562 gain::get(d);
563 bitrate::get(me);
564 lsbdepth::get(me);
565 vbr::get(me);
566 vbr_constraint::get(me);
567 application::get(me);
568 bandwidth::get(me);
569 bandwidth::get(md);
570 complexity::get(me);
571 lossperc::get(me);
572 dtx::get(me);
573 signal::get(me);
574 fec::get(me);
575 force_channels::get(me);
576 bitrate::get(se);
577 lsbdepth::get(se);
578 vbr::get(se);
579 vbr_constraint::get(se);
580 application::get(se);
581 bandwidth::get(se);
582 complexity::get(se);
583 lossperc::get(se);
584 dtx::get(se);
585 signal::get(se);
586 fec::get(se);
587 force_channels::get(se);
589 complexity::get.errordefault();
590 bitrate::get.errordefault();
591 vbr::get.errordefault();
592 vbr_constraint::get.errordefault();
593 force_channels::get.errordefault();
594 max_bandwidth::get.errordefault();
595 bandwidth::get.errordefault();
596 signal::get.errordefault();
597 application::get.errordefault();
598 fec::get.errordefault();
599 lossperc::get.errordefault();
600 dtx::get.errordefault();
601 lsbdepth::get.errordefault();
602 gain::get.errordefault();
604 d.ctl(opus::pktduration);
605 //d.ctl_quiet(opus::pktduration);
606 e.ctl_quiet(opus::reset);
607 e.ctl_quiet(opus::finalrange);
608 e.ctl_quiet(opus::signal::get);
611 encoder::~encoder()
613 if(!user)
614 delete[] reinterpret_cast<char*>(memory);
617 encoder& encoder::operator=(const encoder& e)
619 if(stereo != e.stereo)
620 throw std::runtime_error("Channel mismatch in assignment");
621 size_t s = size(stereo);
622 if(memory != e.memory)
623 memcpy(memory, e.memory, s);
624 return *this;
627 encoder::encoder(samplerate rate, bool _stereo, application app, char* _memory)
629 size_t s = size(_stereo);
630 memory = _memory ? _memory : new char[s];
631 stereo = _stereo;
632 user = (_memory != NULL);
633 try {
634 throwex(opus_encoder_init(E(*this), rate, _stereo ? 2 : 1, app));
635 } catch(...) {
636 if(!user)
637 delete[] reinterpret_cast<char*>(memory);
638 throw;
642 encoder::encoder(const encoder& e)
644 size_t s = size(e.stereo);
645 memory = new char[s];
646 memcpy(memory, e.memory, s);
647 stereo = e.stereo;
648 user = false;
651 encoder::encoder(const encoder& e, char* _memory)
653 size_t s = size(e.stereo);
654 memory = _memory;
655 memcpy(memory, e.memory, s);
656 stereo = e.stereo;
657 user = true;
660 encoder::encoder(void* state, bool _stereo)
662 memory = state;
663 stereo = _stereo;
664 user = true;
667 size_t encoder::size(bool stereo)
669 return opus_encoder_get_size(stereo ? 2 : 1);
672 size_t encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
674 return throwex(opus_encode(E(*this), in, inframes, out, maxout));
677 size_t encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
679 return throwex(opus_encode_float(E(*this), in, inframes, out, maxout));
682 decoder::~decoder()
684 if(!user)
685 delete[] reinterpret_cast<char*>(memory);
688 decoder::decoder(samplerate rate, bool _stereo, char* _memory)
690 size_t s = size(_stereo);
691 memory = _memory ? _memory : new char[s];
692 stereo = _stereo;
693 user = (_memory != NULL);
694 try {
695 throwex(opus_decoder_init(D(*this), rate, _stereo ? 2 : 1));
696 } catch(...) {
697 if(!user)
698 delete[] reinterpret_cast<char*>(memory);
699 throw;
703 decoder& decoder::operator=(const decoder& d)
705 if(stereo != d.stereo)
706 throw std::runtime_error("Channel mismatch in assignment");
707 size_t s = size(stereo);
708 if(memory != d.memory)
709 memcpy(memory, d.memory, s);
710 return *this;
713 size_t decoder::size(bool stereo)
715 return opus_decoder_get_size(stereo ? 2 : 1);
718 decoder::decoder(const decoder& d)
720 size_t s = size(d.stereo);
721 memory = new char[s];
722 memcpy(memory, d.memory, s);
723 stereo = d.stereo;
724 user = false;
727 decoder::decoder(const decoder& d, char* _memory)
729 size_t s = size(d.stereo);
730 memory = _memory;
731 memcpy(memory, d.memory, s);
732 stereo = d.stereo;
733 user = true;
736 decoder::decoder(void* state, bool _stereo)
738 memory = state;
739 stereo = _stereo;
740 user = true;
743 size_t decoder::decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes, bool decode_fec)
745 return throwex(opus_decode(D(*this), in, insize, out, maxframes, decode_fec ? 1 : 0));
748 size_t decoder::decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes, bool decode_fec)
750 return throwex(opus_decode_float(D(*this), in, insize, out, maxframes, decode_fec ? 1 : 0));
753 uint32_t decoder::get_nb_samples(const unsigned char* buf, size_t bufsize)
755 return throwex(opus_decoder_get_nb_samples(D(*this), buf, bufsize));
758 repacketizer::repacketizer(char* _memory)
760 size_t s = size();
761 memory = _memory ? _memory : new char[s];
762 user = false;
763 opus_repacketizer_init(R(*this));
766 repacketizer::repacketizer(const repacketizer& rp)
768 size_t s = size();
769 memory = new char[s];
770 user = false;
771 memcpy(memory, rp.memory, s);
774 repacketizer::repacketizer(const repacketizer& rp, char* _memory)
776 memory = _memory;
777 user = true;
778 memcpy(memory, rp.memory, size());
781 repacketizer& repacketizer::operator=(const repacketizer& rp)
783 if(memory != rp.memory)
784 memcpy(memory, rp.memory, size());
785 return *this;
788 repacketizer::~repacketizer()
790 if(!user)
791 delete[] reinterpret_cast<char*>(memory);
794 size_t repacketizer::size()
796 return opus_repacketizer_get_size();
799 void repacketizer::cat(const unsigned char* data, size_t len)
801 throwex(opus_repacketizer_cat(R(*this), data, len));
804 size_t repacketizer::out(unsigned char* data, size_t maxlen)
806 return throwex(opus_repacketizer_out(R(*this), data, maxlen));
809 size_t repacketizer::out(unsigned char* data, size_t maxlen, unsigned begin, unsigned end)
811 return throwex(opus_repacketizer_out_range(R(*this), begin, end, data, maxlen));
814 unsigned repacketizer::get_nb_frames()
816 return opus_repacketizer_get_nb_frames(R(*this));
819 encoder& multistream_encoder::operator[](size_t idx)
821 if(idx > (size_t)streams)
822 throw opus::bad_argument();
823 return *reinterpret_cast<encoder*>(substream(idx));
826 size_t multistream_encoder::size(unsigned streams, unsigned coupled_streams)
828 return alignof(encoder) + streams * sizeof(encoder) + opus_multistream_encoder_get_size(streams,
829 coupled_streams);
832 multistream_encoder::~multistream_encoder()
834 if(!user)
835 delete[] memory;
838 multistream_encoder::multistream_encoder(samplerate rate, unsigned _channels, unsigned _streams,
839 unsigned coupled_streams, const unsigned char* mapping, application app, char* _memory)
841 user = (_memory != NULL);
842 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[size(streams, coupled_streams)];
843 try {
844 offset = _streams * sizeof(encoder);
845 throwex(opus_multistream_encoder_init(ME(*this), rate, channels, _streams, coupled_streams, mapping,
846 app));
847 init_structures(_channels, _streams, coupled_streams);
848 } catch(...) {
849 if(!user)
850 delete[] memory;
851 throw;
855 multistream_encoder::multistream_encoder(const multistream_encoder& e)
857 init_copy(e, new char[e.size()]);
858 user = false;
861 multistream_encoder::multistream_encoder(const multistream_encoder& e, char* memory)
863 init_copy(e, memory);
864 user = true;
867 multistream_encoder& multistream_encoder::operator=(const multistream_encoder& e)
869 if(this == &e)
870 return *this;
871 if(streams != e.streams || coupled != e.coupled)
872 throw std::runtime_error("Stream mismatch in assignment");
873 memcpy(ME(*this), e.memory + e.offset, opussize);
874 channels = e.channels;
875 return *this;
878 void multistream_encoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled)
880 channels = _channels;
881 streams = _streams;
882 coupled = _coupled;
883 opussize = opus_multistream_encoder_get_size(streams, coupled);
884 for(int32_t i = 0; i < streams; i++) {
885 OpusEncoder* e;
886 opus_multistream_encoder_ctl(ME(*this), OPUS_MULTISTREAM_GET_ENCODER_STATE(i, &e));
887 new(substream(i)) encoder(e, i < coupled);
891 void multistream_encoder::init_copy(const multistream_encoder& e, char* _memory)
893 user = (_memory != NULL);
894 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[e.size()];
895 offset = e.offset;
896 memcpy(ME(*this), e.memory + e.offset, e.opussize);
897 init_structures(e.channels, e.streams, e.coupled);
900 size_t multistream_encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
902 return throwex(opus_multistream_encode(ME(*this), in, inframes, out, maxout));
905 size_t multistream_encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
907 return throwex(opus_multistream_encode_float(ME(*this), in, inframes, out, maxout));
910 char* multistream_encoder::substream(size_t idx)
912 return memory + idx * sizeof(encoder);
915 encoder& surround_encoder::operator[](size_t idx)
917 if(idx > (size_t)streams)
918 throw opus::bad_argument();
919 return *reinterpret_cast<encoder*>(substream(idx));
922 size_t surround_encoder::size(unsigned channels, unsigned family)
924 //Be conservative with memory, as stream count is not known.
925 #ifdef OPUS_SUPPORTS_SURROUND
926 return alignof(encoder) + channels * sizeof(encoder) + opus_multistream_surround_encoder_get_size(channels,
927 family);
928 #else
929 int streams, coupled;
930 lookup_params(channels, family, streams, coupled);
931 //We use channels and not streams here to keep compatiblity on fallback.
932 return alignof(encoder) + channels * sizeof(encoder) + opus_multistream_encoder_get_size(streams, coupled);
933 #endif
936 surround_encoder::~surround_encoder()
938 if(!user)
939 delete[] memory;
942 surround_encoder::surround_encoder(samplerate rate, unsigned _channels, unsigned _family, application app,
943 stream_format& format, char* _memory)
945 user = (_memory != NULL);
946 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[size(channels, family)];
947 try {
948 int rstreams, rcoupled;
949 unsigned char rmapping[256];
950 offset = _channels * sizeof(encoder); //Conservative.
951 #ifdef OPUS_SUPPORTS_SURROUND
952 throwex(opus_multistream_surround_encoder_init(ME(*this), rate, _channels, _family, &rstreams,
953 &rcoupled, rmapping, app));
954 #else
955 lookup_params(_channels, _family, rstreams, rcoupled);
956 generate_mapping(_channels, _family, rmapping);
957 throwex(opus_multistream_encoder_init(ME(*this), rate, channels, rstreams, rcoupled, rmapping, app));
958 #endif
959 init_structures(_channels, rstreams, rcoupled, _family);
960 format.channels = channels;
961 format.family = family;
962 format.streams = streams;
963 format.coupled = coupled;
964 memcpy(format.mapping, rmapping, channels);
965 } catch(...) {
966 if(!user)
967 delete[] memory;
968 throw;
972 surround_encoder::surround_encoder(const surround_encoder& e)
974 init_copy(e, new char[e.size()]);
975 user = false;
978 surround_encoder::surround_encoder(const surround_encoder& e, char* memory)
980 init_copy(e, memory);
981 user = true;
984 surround_encoder& surround_encoder::operator=(const surround_encoder& e)
986 if(this == &e)
987 return *this;
988 if(channels != e.channels || family != e.family)
989 throw std::runtime_error("Stream mismatch in assignment");
990 memcpy(ME(*this), e.memory + e.offset, opussize);
991 return *this;
994 void surround_encoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled, unsigned _family)
996 channels = _channels;
997 streams = _streams;
998 coupled = _coupled;
999 family = _family;
1000 #if OPUS_SUPPORTS_SURROUND
1001 opussize = opus_multistream_surround_encoder_get_size(_channels, family);
1002 #else
1003 opussize = opus_multistream_encoder_get_size(streams, coupled);
1004 #endif
1005 for(int32_t i = 0; i < streams; i++) {
1006 OpusEncoder* e;
1007 opus_multistream_encoder_ctl(ME(*this), OPUS_MULTISTREAM_GET_ENCODER_STATE(i, &e));
1008 new(substream(i)) encoder(e, i < coupled);
1012 void surround_encoder::init_copy(const surround_encoder& e, char* _memory)
1014 user = (_memory != NULL);
1015 memory = _memory ? alignptr(_memory, alignof(encoder)) : new char[e.size()];
1016 offset = e.offset;
1017 memcpy(ME(*this), e.memory + e.offset, e.opussize);
1018 init_structures(e.channels, e.streams, e.coupled, e.family);
1021 size_t surround_encoder::encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1023 return throwex(opus_multistream_encode(ME(*this), in, inframes, out, maxout));
1026 size_t surround_encoder::encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout)
1028 return throwex(opus_multistream_encode_float(ME(*this), in, inframes, out, maxout));
1031 char* surround_encoder::substream(size_t idx)
1033 return memory + idx * sizeof(encoder);
1036 decoder& multistream_decoder::operator[](size_t idx)
1038 if(idx > (size_t)streams)
1039 throw opus::bad_argument();
1040 return *reinterpret_cast<decoder*>(substream(idx));
1043 size_t multistream_decoder::size(unsigned streams, unsigned coupled_streams)
1045 return streams * sizeof(decoder) + opus_multistream_decoder_get_size(streams,
1046 coupled_streams);
1049 multistream_decoder::multistream_decoder(samplerate rate, unsigned _channels, unsigned _streams,
1050 unsigned coupled_streams, const unsigned char* mapping, char* _memory)
1052 user = (_memory != NULL);
1053 memory = _memory ? alignptr(_memory, alignof(decoder)) : new char[size(streams, coupled_streams)];
1054 try {
1055 throwex(opus_multistream_decoder_init(MD(*this), rate, channels, streams, coupled_streams, mapping));
1056 init_structures(_channels, _streams, coupled_streams);
1057 } catch(...) {
1058 if(!user)
1059 delete[] memory;
1060 throw;
1063 multistream_decoder::multistream_decoder(const multistream_decoder& e)
1065 init_copy(e, NULL);
1068 multistream_decoder::multistream_decoder(const multistream_decoder& e, char* memory)
1070 init_copy(e, memory);
1073 multistream_decoder::~multistream_decoder()
1075 if(!user)
1076 delete[] memory;
1079 multistream_decoder& multistream_decoder::operator=(const multistream_decoder& d)
1081 if(this == &d)
1082 return *this;
1083 if(streams != d.streams || coupled != d.coupled)
1084 throw std::runtime_error("Stream mismatch in assignment");
1085 memcpy(MD(*this), d.memory + d.offset, opussize);
1086 channels = d.channels;
1087 return *this;
1090 void multistream_decoder::init_structures(unsigned _channels, unsigned _streams, unsigned _coupled)
1092 channels = _channels;
1093 streams = _streams;
1094 coupled = _coupled;
1095 opussize = opus_multistream_decoder_get_size(streams, coupled);
1096 for(int32_t i = 0; i < (size_t)streams; i++) {
1097 OpusDecoder* d;
1098 opus_multistream_decoder_ctl(MD(*this), OPUS_MULTISTREAM_GET_DECODER_STATE(i, &d));
1099 new(substream(i)) decoder(d, i < coupled);
1103 void multistream_decoder::init_copy(const multistream_decoder& e, char* _memory)
1105 user = (_memory != NULL);
1106 memory = _memory ? alignptr(_memory, alignof(decoder)) : new char[e.size()];
1107 offset = e.offset;
1108 memcpy(MD(*this), e.memory + e.offset, e.opussize);
1109 init_structures(e.channels, e.streams, e.coupled);
1112 size_t multistream_decoder::decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes,
1113 bool decode_fec)
1115 return throwex(opus_multistream_decode(MD(*this), in, insize, out, maxframes, decode_fec));
1118 size_t multistream_decoder::decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes,
1119 bool decode_fec)
1121 return throwex(opus_multistream_decode_float(MD(*this), in, insize, out, maxframes, decode_fec));
1124 char* multistream_decoder::substream(size_t idx)
1126 return memory + idx * sizeof(encoder);
1129 uint32_t packet_get_nb_frames(const unsigned char* packet, size_t len)
1131 return throwex(opus_packet_get_nb_frames(packet, len));
1134 uint32_t packet_get_samples_per_frame(const unsigned char* data, samplerate fs)
1136 return throwex(opus_packet_get_samples_per_frame(data, fs));
1139 uint32_t packet_get_nb_samples(const unsigned char* packet, size_t len, samplerate fs)
1141 return packet_get_nb_frames(packet, len) * packet_get_samples_per_frame(packet, fs);
1144 uint32_t packet_get_nb_channels(const unsigned char* packet)
1146 return throwex(opus_packet_get_nb_channels(packet));
1149 bandwidth packet_get_bandwidth(const unsigned char* packet)
1151 return bandwidth(throwex(opus_packet_get_bandwidth(packet)));
1154 parsed_packet packet_parse(const unsigned char* packet, size_t len)
1156 parsed_packet p;
1157 unsigned char toc;
1158 const unsigned char* frames[48];
1159 short size[48];
1160 int payload_offset;
1161 uint32_t framecnt = throwex(opus_packet_parse(packet, len, &toc, frames, size, &payload_offset));
1162 p.toc = toc;
1163 p.payload_offset = payload_offset;
1164 p.frames.resize(framecnt);
1165 for(unsigned i = 0; i < framecnt; i++) {
1166 p.frames[i].ptr = frames[i];
1167 p.frames[i].size = size[i];
1169 return p;
1172 std::string version()
1174 return opus_get_version_string();
1178 #endif