Fix some compilation errors on Mac OS X
[lsnes.git] / include / library / opus.hpp
blobb6ce2676e4d1fce7011e532bee30b6f4c61ac72f
1 #ifndef _library_opus_hpp_included_
2 #define _library_opus_hpp_included_
4 #include <cstdint>
5 #include <cstdlib>
6 #include <stdexcept>
7 #include <vector>
8 #include <functional>
10 namespace loadlib
12 class module;
15 namespace opus
17 void load_libopus(const loadlib::module& lib);
18 bool libopus_loaded();
19 size_t add_callback(std::function<void()> fun);
20 void cancel_callback(size_t handle);
22 struct bad_argument : public std::runtime_error { bad_argument(); };
23 struct buffer_too_small : public std::runtime_error { buffer_too_small(); };
24 struct internal_error : public std::runtime_error { internal_error(); };
25 struct invalid_packet : public std::runtime_error { invalid_packet(); };
26 struct unimplemented : public std::runtime_error { unimplemented(); };
27 struct invalid_state : public std::runtime_error { invalid_state(); };
28 struct not_loaded : public std::runtime_error { not_loaded(); };
30 class encoder;
31 class decoder;
32 class multistream_encoder;
33 class multistream_decoder;
34 class surround_encoder;
36 template<typename T> struct generic_eget
38 typedef T erettype;
39 T operator()(encoder& e) const;
40 T errordefault() const;
43 template<typename T> struct generic_meget : public generic_eget<T>
45 using generic_eget<T>::operator();
46 T operator()(multistream_encoder& e) const;
47 T operator()(surround_encoder& e) const;
50 template<typename T> struct generic_get
52 typedef T drettype;
53 typedef T erettype;
54 T operator()(decoder& e) const;
55 T operator()(encoder& e) const;
56 T errordefault() const;
59 template<typename T> struct generic_mget : public generic_get<T>
61 using generic_get<T>::operator();
62 T operator()(multistream_decoder& e) const;
63 T operator()(multistream_encoder& e) const;
64 T operator()(surround_encoder& e) const;
67 template<typename T> struct generic_dget
69 typedef T drettype;
70 T operator()(decoder& e) const;
71 T errordefault() const;
74 template<typename T> struct generic_mdget : public generic_dget<T>
76 using generic_dget<T>::operator();
77 T operator()(multistream_decoder& e) const;
80 struct samplerate
82 samplerate(int32_t _fs) { fs = _fs; }
83 static samplerate r8k;
84 static samplerate r12k;
85 static samplerate r16k;
86 static samplerate r24k;
87 static samplerate r48k;
88 operator int32_t() { return fs; }
89 typedef samplerate erettype;
90 samplerate operator()(encoder& e) const;
91 samplerate operator()(multistream_encoder& e) const;
92 samplerate operator()(decoder& e) const;
93 samplerate operator()(multistream_decoder& e) const;
94 samplerate errordefault() const { return samplerate(0); }
95 private:
96 int32_t fs;
99 struct complexity
101 complexity(int32_t _c) { c = _c; }
102 operator int32_t() { return c; }
103 static generic_meget<complexity> get;
104 typedef void erettype;
105 void operator()(encoder& e) const;
106 void operator()(multistream_encoder& e) const;
107 void operator()(surround_encoder& e) const;
108 void errordefault() const {}
109 private:
110 int32_t c;
113 struct bitrate
115 bitrate(int32_t _b) { b = _b; }
116 static bitrate _auto;
117 static bitrate max;
118 operator int32_t() { return b; }
119 static generic_meget<bitrate> get;
120 typedef void erettype;
121 void operator()(encoder& e) const;
122 void operator()(multistream_encoder& e) const;
123 void operator()(surround_encoder& e) const;
124 void errordefault() const {}
125 private:
126 int32_t b;
129 struct vbr
131 vbr(bool _v) { v = _v; }
132 static vbr cbr;
133 static vbr _vbr;
134 operator bool() { return v; }
135 static generic_meget<vbr> get;
136 typedef void erettype;
137 void operator()(encoder& e) const;
138 void operator()(multistream_encoder& e) const;
139 void operator()(surround_encoder& e) const;
140 void errordefault() const {}
141 private:
142 bool v;
145 struct vbr_constraint
147 vbr_constraint(bool _c) { c = _c; }
148 static vbr_constraint unconstrained;
149 static vbr_constraint constrained;
150 operator bool() { return c; }
151 static generic_meget<vbr_constraint> get;
152 typedef void erettype;
153 void operator()(encoder& e) const;
154 void operator()(multistream_encoder& e) const;
155 void operator()(surround_encoder& e) const;
156 void errordefault() const {}
157 private:
158 bool c;
161 struct force_channels
163 force_channels(int32_t _f) { f = _f; }
164 static force_channels _auto;
165 static force_channels mono;
166 static force_channels stereo;
167 operator int32_t() { return f; }
168 static generic_meget<force_channels> get;
169 typedef void erettype;
170 void operator()(encoder& e) const;
171 void operator()(multistream_encoder& e) const;
172 void operator()(surround_encoder& e) const;
173 void errordefault() const {}
174 private:
175 int32_t f;
178 struct max_bandwidth
180 max_bandwidth(int32_t _bw) { bw = _bw; }
181 static max_bandwidth narrow;
182 static max_bandwidth medium;
183 static max_bandwidth wide;
184 static max_bandwidth superwide;
185 static max_bandwidth full;
186 operator int32_t() { return bw; }
187 static generic_eget<max_bandwidth> get;
188 typedef void erettype;
189 void operator()(encoder& e) const;
190 void errordefault() const {}
191 private:
192 int32_t bw;
195 struct bandwidth
197 bandwidth(int32_t _bw) { bw = _bw; }
198 static bandwidth _auto;
199 static bandwidth narrow;
200 static bandwidth medium;
201 static bandwidth wide;
202 static bandwidth superwide;
203 static bandwidth full;
204 operator int32_t() { return bw; }
205 static generic_mget<bandwidth> get;
206 typedef void erettype;
207 void operator()(encoder& e) const;
208 void operator()(multistream_encoder& e) const;
209 void operator()(surround_encoder& e) const;
210 void errordefault() const {}
211 private:
212 int32_t bw;
215 struct signal
217 signal(int32_t _s) { s = _s; }
218 static signal _auto;
219 static signal music;
220 static signal voice;
221 operator int32_t() { return s; }
222 static generic_meget<signal> get;
223 typedef void erettype;
224 void operator()(encoder& e) const;
225 void operator()(multistream_encoder& e) const;
226 void operator()(surround_encoder& e) const;
227 void errordefault() const {}
228 private:
229 int32_t s;
232 struct application
234 application(int32_t _app) { app = _app; };
235 static application audio;
236 static application voice;
237 static application lowdelay;
238 operator int32_t() { return app; }
239 static generic_meget<application> get;
240 typedef void erettype;
241 void operator()(encoder& e) const;
242 void operator()(multistream_encoder& e) const;
243 void operator()(surround_encoder& e) const;
244 void errordefault() const {}
245 private:
246 int32_t app;
249 struct _lookahead
251 _lookahead() { l = 0; }
252 _lookahead(uint32_t _l) { l = _l; }
253 operator uint32_t() { return l; }
254 typedef _lookahead erettype;
255 _lookahead operator()(encoder& e) const;
256 _lookahead operator()(multistream_encoder& e) const;
257 void operator()(surround_encoder& e) const;
258 _lookahead errordefault() const { return _lookahead(0);}
259 private:
260 uint32_t l;
262 extern _lookahead lookahead;
264 struct fec
266 fec(bool _f) { f = _f; }
267 static fec disabled;
268 static fec enabled;
269 operator bool() { return f; }
270 static generic_meget<fec> get;
271 typedef void erettype;
272 void operator()(encoder& e) const;
273 void operator()(multistream_encoder& e) const;
274 void operator()(surround_encoder& e) const;
275 void errordefault() const {}
276 private:
277 bool f;
280 struct lossperc
282 lossperc(uint32_t _loss) { loss = _loss; };
283 operator uint32_t() { return loss; }
284 static generic_meget<lossperc> get;
285 typedef void erettype;
286 void operator()(encoder& e) const;
287 void operator()(multistream_encoder& e) const;
288 void operator()(surround_encoder& e) const;
289 void errordefault() const {}
290 private:
291 uint32_t loss;
294 struct dtx
296 dtx(bool _d) { d = _d; }
297 static dtx disabled;
298 static dtx enabled;
299 operator bool() { return d; }
300 static generic_meget<dtx> get;
301 typedef void erettype;
302 void operator()(encoder& e) const;
303 void operator()(multistream_encoder& e) const;
304 void operator()(surround_encoder& e) const;
305 void errordefault() const {}
306 private:
307 bool d;
310 struct lsbdepth
312 lsbdepth(uint32_t _depth) { depth = _depth; };
313 static lsbdepth d8;
314 static lsbdepth d16;
315 static lsbdepth d24;
316 operator uint32_t() { return depth; }
317 static generic_meget<lsbdepth> get;
318 typedef void erettype;
319 void operator()(encoder& e) const;
320 void operator()(multistream_encoder& e) const;
321 void operator()(surround_encoder& e) const;
322 void errordefault() const {}
323 private:
324 uint32_t depth;
327 struct _pktduration
329 _pktduration() { d = 0; }
330 _pktduration(uint32_t _d) { d = _d; }
331 operator uint32_t() { return d; }
332 typedef _pktduration drettype;
333 _pktduration operator()(decoder& e) const;
334 _pktduration operator()(multistream_decoder& e) const;
335 void operator()(surround_encoder& e) const;
336 _pktduration errordefault() const; /*{ return _pktduration(0); }*/
337 private:
338 uint32_t d;
340 extern _pktduration pktduration;
342 struct _reset
344 typedef void drettype;
345 typedef void erettype;
346 void operator()(decoder& e) const;
347 void operator()(encoder& e) const;
348 void operator()(multistream_decoder& e) const;
349 void operator()(multistream_encoder& e) const;
350 void operator()(surround_encoder& e) const;
351 void errordefault() const {}
353 extern _reset reset;
355 struct _finalrange
357 _finalrange() { f = 0; }
358 _finalrange(uint32_t _f) { f = _f; }
359 operator uint32_t() { return f; }
360 typedef _finalrange drettype;
361 typedef _finalrange erettype;
362 _finalrange operator()(decoder& e) const;
363 _finalrange operator()(encoder& e) const;
364 _finalrange operator()(multistream_decoder& e) const;
365 _finalrange operator()(multistream_encoder& e) const;
366 _finalrange operator()(surround_encoder& e) const;
367 _finalrange errordefault() const { return _finalrange(0); }
368 private:
369 uint32_t f;
371 extern _finalrange finalrange;
373 struct _pitch
375 _pitch() { p = 0; }
376 _pitch(uint32_t _p) { p = _p; }
377 operator uint32_t() { return p; }
378 typedef _pitch drettype;
379 typedef _pitch erettype;
380 _pitch operator()(encoder& e) const;
381 _pitch operator()(decoder& e) const;
382 _pitch errordefault() const { return _pitch(0); }
383 private:
384 uint32_t p;
386 extern _pitch pitch;
388 struct gain
390 gain(int32_t _g) { g = _g; };
391 operator int32_t() { return g; }
392 static generic_mdget<gain> get;
393 typedef void drettype;
394 void operator()(decoder& d) const;
395 void operator()(multistream_decoder& d) const;
396 gain errordefault() const { return gain(0); }
397 private:
398 int32_t g;
401 struct prediction_disabled
403 prediction_disabled(bool _d) { d = _d; }
404 static prediction_disabled disabled;
405 static prediction_disabled enabled;
406 operator bool() { return d; }
407 static generic_meget<prediction_disabled> get;
408 typedef void erettype;
409 void operator()(encoder& e) const;
410 void operator()(multistream_encoder& e) const;
411 void operator()(surround_encoder& e) const;
412 void errordefault() const { }
413 private:
414 bool d;
417 struct frame_duration
419 frame_duration(int32_t _v) { v = _v; }
420 static frame_duration argument;
421 static frame_duration variable;
422 static frame_duration l2ms; //Really 2.5ms.
423 static frame_duration l5ms;
424 static frame_duration l10ms;
425 static frame_duration l20ms;
426 static frame_duration l40ms;
427 static frame_duration l60ms;
428 operator int32_t() { return v; }
429 static generic_meget<frame_duration> get;
430 typedef void erettype;
431 void operator()(encoder& e) const;
432 void operator()(multistream_encoder& e) const;
433 void operator()(surround_encoder& e) const;
434 void errordefault() const { }
435 private:
436 int32_t v;
439 struct _last_packet_duration
441 _last_packet_duration() { p = 0; }
442 _last_packet_duration(uint32_t _p) { p = _p; }
443 operator uint32_t() { return p; }
444 typedef _last_packet_duration drettype;
445 _last_packet_duration operator()(decoder& e) const;
446 _last_packet_duration operator()(multistream_decoder& e) const;
447 _last_packet_duration errordefault() const { return _last_packet_duration(0); }
448 private:
449 uint32_t p;
451 extern _last_packet_duration last_packet_duration;
455 struct set_control_int
457 set_control_int(int32_t _ctl, int32_t _val) { ctl = _ctl; val = _val; }
458 typedef void drettype;
459 typedef void erettype;
460 void operator()(encoder& e) const;
461 void operator()(decoder& e) const;
462 void operator()(multistream_encoder& e) const;
463 void operator()(multistream_decoder& e) const;
464 void operator()(surround_encoder& e) const;
465 void errordefault() const {}
466 private:
467 int32_t ctl;
468 int32_t val;
471 struct get_control_int
473 get_control_int(int32_t _ctl) { ctl = _ctl; }
474 typedef int32_t drettype;
475 typedef int32_t erettype;
476 int32_t operator()(encoder& e) const;
477 int32_t operator()(decoder& e) const;
478 int32_t operator()(multistream_encoder& e) const;
479 int32_t operator()(multistream_decoder& e) const;
480 int32_t operator()(surround_encoder& e) const;
481 int32_t errordefault() const { return -1; }
482 private:
483 int32_t ctl;
486 class encoder
488 public:
489 encoder(samplerate rate, bool stereo, application app, char* memory = NULL);
490 encoder(void* state, bool _stereo);
491 ~encoder();
492 encoder(const encoder& e);
493 encoder(const encoder& e, char* memory);
494 static size_t size(bool stereo);
495 size_t size() const { return size(stereo); }
496 encoder& operator=(const encoder& e);
497 template<typename T> typename T::erettype ctl(const T& c) { return c(*this); }
498 template<typename T> typename T::erettype ctl_quiet(const T& c)
500 try { return c(*this); } catch(...) { return c.errordefault(); }
502 size_t encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
503 size_t encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
504 bool is_stereo() { return stereo; }
505 void* getmem() { return memory; }
506 private:
507 void* memory;
508 bool stereo;
509 bool user;
512 class decoder
514 public:
515 decoder(samplerate rate, bool stereo, char* memory = NULL);
516 decoder(void* state, bool _stereo);
517 ~decoder();
518 decoder(const decoder& e);
519 decoder(const decoder& e, char* memory);
520 static size_t size(bool stereo);
521 size_t size() const { return size(stereo); }
522 decoder& operator=(const decoder& e);
523 template<typename T> typename T::drettype ctl(const T& c) { return c(*this); }
524 template<typename T> typename T::drettype ctl_quiet(const T& c)
526 try { return c(*this); } catch(...) { return c.errordefault(); }
528 size_t decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes,
529 bool decode_fec = false);
530 size_t decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes,
531 bool decode_fec = false);
532 uint32_t get_nb_samples(const unsigned char* buf, size_t bufsize);
533 bool is_stereo() { return stereo; }
534 void* getmem() { return memory; }
535 private:
536 void* memory;
537 bool stereo;
538 bool user;
541 class repacketizer
543 public:
544 repacketizer(char* memory = NULL);
545 repacketizer(const repacketizer& rp);
546 repacketizer(const repacketizer& rp, char* memory);
547 repacketizer& operator=(const repacketizer& rp);
548 ~repacketizer();
549 static size_t size();
550 void cat(const unsigned char* data, size_t len);
551 size_t out(unsigned char* data, size_t maxlen);
552 size_t out(unsigned char* data, size_t maxlen, unsigned begin, unsigned end);
553 unsigned get_nb_frames();
554 void* getmem() { return memory; }
555 private:
556 void* memory;
557 bool user;
560 class multistream_encoder
562 public:
563 multistream_encoder(samplerate rate, unsigned channels, unsigned streams,
564 unsigned coupled_streams, const unsigned char* mapping, application app, char* memory = NULL);
565 static size_t size(unsigned streams, unsigned coupled_streams);
566 size_t size() const { return size(streams, coupled); }
567 multistream_encoder(const multistream_encoder& e);
568 multistream_encoder(const multistream_encoder& e, char* memory);
569 multistream_encoder& operator=(const multistream_encoder& e);
570 ~multistream_encoder();
571 encoder& operator[](size_t idx);
572 size_t encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
573 size_t encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
574 template<typename T> typename T::erettype ctl(const T& c) { return c(*this); }
575 template<typename T> typename T::erettype ctl_quiet(const T& c)
577 try { return c(*this); } catch(...) { return c.errordefault(); }
579 uint8_t get_channels() { return channels; }
580 uint8_t get_streams() { return streams; }
581 void* getmem() { return memory + offset; }
582 private:
583 void init_copy(const multistream_encoder& e, char* memory);
584 char* substream(size_t idx);
585 void init_structures(unsigned _channels, unsigned _streams, unsigned _coupled);
586 char* memory;
587 bool user;
588 uint8_t channels;
589 uint8_t streams;
590 uint8_t coupled;
591 size_t offset;
592 size_t opussize;
595 class surround_encoder
597 public:
598 struct stream_format
600 unsigned family;
601 unsigned channels;
602 unsigned streams;
603 unsigned coupled;
604 unsigned char mapping[256];
606 surround_encoder(samplerate rate, unsigned channels, unsigned family, application app,
607 stream_format& format, char* memory = NULL);
608 static size_t size(unsigned channels, unsigned family);
609 size_t size() const { return size(channels, family); }
610 surround_encoder(const surround_encoder& e);
611 surround_encoder(const surround_encoder& e, char* memory);
612 surround_encoder& operator=(const surround_encoder& e);
613 ~surround_encoder();
614 encoder& operator[](size_t idx);
615 size_t encode(const int16_t* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
616 size_t encode(const float* in, uint32_t inframes, unsigned char* out, uint32_t maxout);
617 template<typename T> typename T::erettype ctl(const T& c) { return c(*this); }
618 template<typename T> typename T::erettype ctl_quiet(const T& c)
620 try { return c(*this); } catch(...) { return c.errordefault(); }
622 uint8_t get_channels() { return channels; }
623 uint8_t get_streams() { return streams; }
624 void* getmem() { return memory + offset; }
625 private:
626 void init_copy(const surround_encoder& e, char* memory);
627 char* substream(size_t idx);
628 void init_structures(unsigned _channels, unsigned _streams, unsigned _coupled, unsigned _family);
629 char* memory;
630 bool user;
631 uint8_t channels;
632 uint8_t streams;
633 uint8_t coupled;
634 uint8_t family;
635 size_t offset;
636 size_t opussize;
639 class multistream_decoder
641 public:
642 multistream_decoder(samplerate rate, unsigned channels, unsigned streams,
643 unsigned coupled_streams, const unsigned char* mapping, char* memory = NULL);
644 static size_t size(unsigned streams, unsigned coupled_streams);
645 size_t size() const { return size(streams, coupled); }
646 multistream_decoder(const multistream_decoder& e);
647 multistream_decoder(const multistream_decoder& e, char* memory);
648 multistream_decoder& operator=(const multistream_decoder& e);
649 ~multistream_decoder();
650 decoder& operator[](size_t idx);
651 template<typename T> typename T::drettype ctl(const T& c) { return c(*this); }
652 template<typename T> typename T::drettype ctl_quiet(const T& c)
654 try { return c(*this); } catch(...) { return c.errordefault(); }
656 size_t decode(const unsigned char* in, uint32_t insize, int16_t* out, uint32_t maxframes,
657 bool decode_fec = false);
658 size_t decode(const unsigned char* in, uint32_t insize, float* out, uint32_t maxframes,
659 bool decode_fec = false);
660 uint8_t get_channels() { return channels; }
661 uint8_t get_streams() { return streams; }
662 void* getmem() { return memory + offset; }
663 private:
664 void init_copy(const multistream_decoder& e, char* memory);
665 char* substream(size_t idx);
666 void init_structures(unsigned _channels, unsigned _streams, unsigned _coupled);
667 char* memory;
668 bool user;
669 uint8_t channels;
670 uint8_t streams;
671 uint8_t coupled;
672 size_t offset;
673 size_t opussize;
676 struct parsed_frame
678 const unsigned char* ptr;
679 short size;
682 struct parsed_packet
684 unsigned char toc;
685 std::vector<parsed_frame> frames;
686 uint32_t payload_offset;
689 uint32_t packet_get_nb_frames(const unsigned char* packet, size_t len);
690 uint32_t packet_get_samples_per_frame(const unsigned char* data, samplerate fs);
691 uint32_t packet_get_nb_samples(const unsigned char* packet, size_t len, samplerate fs);
692 uint32_t packet_get_nb_channels(const unsigned char* packet);
693 bandwidth packet_get_bandwidth(const unsigned char* packet);
694 parsed_packet packet_parse(const unsigned char* packet, size_t len);
695 std::string version();
698 * Get tick (2.5ms) count from opus packet.
700 * Parameter packet: The packet data.
701 * Parameter packetsize: The size of packet.
703 * Note: Guaranteed not to read more than 2 bytes from the packet.
705 uint8_t packet_tick_count(const uint8_t* packet, size_t packetsize);
707 #endif