1 #ifndef _library_opus_hpp_included_
2 #define _library_opus_hpp_included_
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(); };
32 class multistream_encoder
;
33 class multistream_decoder
;
34 class surround_encoder
;
36 template<typename T
> struct generic_eget
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
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
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;
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); }
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 {}
115 bitrate(int32_t _b
) { b
= _b
; }
116 static bitrate _auto
;
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 {}
131 vbr(bool _v
) { v
= _v
; }
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 {}
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 {}
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 {}
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 {}
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 {}
217 signal(int32_t _s
) { s
= _s
; }
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 {}
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 {}
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);}
262 extern _lookahead lookahead
;
266 fec(bool _f
) { f
= _f
; }
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 {}
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 {}
296 dtx(bool _d
) { d
= _d
; }
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 {}
312 lsbdepth(uint32_t _depth
) { depth
= _depth
; };
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 {}
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); }*/
340 extern _pktduration pktduration
;
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 {}
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); }
371 extern _finalrange finalrange
;
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); }
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); }
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 { }
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 { }
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); }
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 {}
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; }
489 encoder(samplerate rate
, bool stereo
, application app
, char* memory
= NULL
);
490 encoder(void* state
, bool _stereo
);
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
; }
515 decoder(samplerate rate
, bool stereo
, char* memory
= NULL
);
516 decoder(void* state
, bool _stereo
);
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
; }
544 repacketizer(char* memory
= NULL
);
545 repacketizer(const repacketizer
& rp
);
546 repacketizer(const repacketizer
& rp
, char* memory
);
547 repacketizer
& operator=(const repacketizer
& rp
);
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
; }
560 class multistream_encoder
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
; }
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
);
595 class surround_encoder
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
);
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
; }
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
);
639 class multistream_decoder
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
; }
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
);
678 const unsigned char* ptr
;
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
);