From a88ae3a8a3c49ab655d0405f275de219f73c99da Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Tue, 19 Feb 2013 23:43:10 +0200 Subject: [PATCH] Opus support: Fix all sorts of mistakes --- include/library/opus.hpp | 82 ++++++++++++++-------------- src/library/opus.cpp | 115 +++++++++++++++++++++++++++++++++------ 2 files changed, 138 insertions(+), 59 deletions(-) diff --git a/include/library/opus.hpp b/include/library/opus.hpp index 947e24fa..cdb59d56 100644 --- a/include/library/opus.hpp +++ b/include/library/opus.hpp @@ -22,7 +22,7 @@ template struct generic_eget { typedef T erettype; T operator()(encoder& e) const; - T errordefault(); + T errordefault() const; }; template struct generic_get @@ -31,14 +31,14 @@ template struct generic_get typedef T erettype; T operator()(decoder& e) const; T operator()(encoder& e) const; - T errordefault(); + T errordefault() const; }; template struct generic_dget { typedef T drettype; T operator()(decoder& e) const; - static T errordefault; + T errordefault() const; }; struct samplerate @@ -52,19 +52,19 @@ struct samplerate operator int32_t() { return fs; } typedef samplerate erettype; samplerate operator()(encoder& e) const; - samplerate errordefault() { return samplerate(0); } + samplerate errordefault() const { return samplerate(0); } private: int32_t fs; }; -struct compexity +struct complexity { - compexity(int32_t _c) { c = _c; } + complexity(int32_t _c) { c = _c; } operator int32_t() { return c; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t c; }; @@ -75,10 +75,10 @@ struct bitrate static bitrate _auto; static bitrate max; operator int32_t() { return b; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t b; }; @@ -89,10 +89,10 @@ struct vbr static vbr cbr; static vbr _vbr; operator bool() { return v; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: bool v; }; @@ -103,10 +103,10 @@ struct vbr_constraint static vbr_constraint unconstrained; static vbr_constraint constrained; operator bool() { return c; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: bool c; }; @@ -118,10 +118,10 @@ struct force_channels static force_channels mono; static force_channels stereo; operator int32_t() { return f; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t f; }; @@ -135,10 +135,10 @@ struct max_bandwidth static max_bandwidth superwide; static max_bandwidth full; operator int32_t() { return bw; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t bw; }; @@ -153,10 +153,10 @@ struct bandwidth static bandwidth superwide; static bandwidth full; operator int32_t() { return bw; } - typedef generic_get get; + static generic_get get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t bw; }; @@ -168,10 +168,10 @@ struct signal static signal music; static signal voice; operator int32_t() { return s; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t s; }; @@ -183,10 +183,10 @@ struct application static application voice; static application lowdelay; operator int32_t() { return app; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t app; }; @@ -198,7 +198,7 @@ struct _lookahead operator int32_t() { return l; } typedef _lookahead erettype; _lookahead operator()(encoder& e) const; - _lookahead errordefault() { return _lookahead(0);} + _lookahead errordefault() const { return _lookahead(0);} private: int32_t l; }; @@ -210,10 +210,10 @@ struct fec static fec disabled; static fec enabled; operator bool() { return f; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: bool f; }; @@ -222,10 +222,10 @@ struct lossperc { lossperc(int32_t _loss) { loss = _loss; }; operator int32_t() { return loss; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t loss; }; @@ -236,10 +236,10 @@ struct dtx static dtx disabled; static dtx enabled; operator bool() { return d; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: bool d; }; @@ -251,10 +251,10 @@ struct lsbdepth static lsbdepth d16; static lsbdepth d24; operator int32_t() { return depth; } - typedef generic_eget get; + static generic_eget get; typedef void erettype; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t depth; }; @@ -266,7 +266,7 @@ struct _pktduration operator int32_t() { return d; } typedef _pktduration erettype; _pktduration operator()(encoder& e) const; - _pktduration errordefault() { return _pktduration(0); } + _pktduration errordefault() const { return _pktduration(0); } private: int32_t d; }; @@ -278,7 +278,7 @@ struct _reset typedef void erettype; void operator()(decoder& e) const; void operator()(encoder& e) const; - void errordefault() {} + void errordefault() const {} }; extern _reset reset; @@ -291,7 +291,7 @@ struct _finalrange typedef _finalrange erettype; _finalrange operator()(decoder& e) const; _finalrange operator()(encoder& e) const; - _finalrange errordefault() { return _finalrange(0); } + _finalrange errordefault() const { return _finalrange(0); } private: uint32_t f; }; @@ -306,7 +306,7 @@ struct _pitch typedef _pitch erettype; _pitch operator()(encoder& e) const; _pitch operator()(decoder& e) const; - _pitch errordefault() { return _pitch(0); } + _pitch errordefault() const { return _pitch(0); } private: int32_t p; }; @@ -316,10 +316,10 @@ struct gain { gain(int32_t _g) { g = _g; }; operator int32_t() { return g; } - typedef generic_dget get; + static generic_dget get; typedef void drettype; void operator()(decoder& d) const; - gain errordefault() { return gain(0); } + gain errordefault() const { return gain(0); } private: int32_t g; }; @@ -331,7 +331,7 @@ struct set_control_int typedef void erettype; void operator()(encoder& e) const; void operator()(decoder& e) const; - void errordefault() {} + void errordefault() const {} private: int32_t ctl; int32_t val; @@ -344,7 +344,7 @@ struct get_control_int typedef int32_t erettype; int32_t operator()(encoder& e) const; int32_t operator()(decoder& e) const; - int32_t errordefault() { return -1; } + int32_t errordefault() const { return -1; } private: int32_t ctl; }; diff --git a/src/library/opus.cpp b/src/library/opus.cpp index 1b343510..13971f76 100644 --- a/src/library/opus.cpp +++ b/src/library/opus.cpp @@ -110,9 +110,9 @@ int32_t throwex(int32_t ret) throw std::runtime_error(s.str()); } -template struct get_ctlnum { const static int32_t num; }; +template struct get_ctlnum { const static int32_t num; static T errordefault(); }; -template<> const int32_t get_ctlnum::num = OPUS_GET_COMPLEXITY_REQUEST; +template<> const int32_t get_ctlnum::num = OPUS_GET_COMPLEXITY_REQUEST; template<> const int32_t get_ctlnum::num = OPUS_GET_BITRATE_REQUEST; template<> const int32_t get_ctlnum::num = OPUS_GET_VBR_REQUEST; template<> const int32_t get_ctlnum::num = OPUS_GET_VBR_CONSTRAINT_REQUEST; @@ -127,6 +127,22 @@ template<> const int32_t get_ctlnum::num = OPUS_GET_DTX_REQUEST; template<> const int32_t get_ctlnum::num = OPUS_GET_LSB_DEPTH_REQUEST; template<> const int32_t get_ctlnum::num = OPUS_GET_GAIN_REQUEST; +template<> complexity get_ctlnum::errordefault() { return complexity(10); } +template<> bitrate get_ctlnum::errordefault() { return bitrate::_auto; } +template<> vbr get_ctlnum::errordefault() { return vbr::_vbr; } +template<> vbr_constraint get_ctlnum::errordefault() { return vbr_constraint::unconstrained; } +template<> force_channels get_ctlnum::errordefault() { return force_channels::_auto; } +template<> max_bandwidth get_ctlnum::errordefault() { return max_bandwidth::full; } +template<> bandwidth get_ctlnum::errordefault() { return bandwidth::_auto; } +template<> signal get_ctlnum::errordefault() { return signal::_auto; } +template<> application get_ctlnum::errordefault() { return application::audio; } +template<> fec get_ctlnum::errordefault() { return fec::disabled; } +template<> lossperc get_ctlnum::errordefault() { return lossperc(0); } +template<> dtx get_ctlnum::errordefault() { return dtx::disabled; } +template<> lsbdepth get_ctlnum::errordefault() { return lsbdepth(24); } +template<> gain get_ctlnum::errordefault() { return gain(0); } + + OpusEncoder* E(encoder& e) { return reinterpret_cast(e.getmem()); } OpusDecoder* D(decoder& d) { return reinterpret_cast(d.getmem()); } OpusRepacketizer* R(repacketizer& r) { return reinterpret_cast(r.getmem()); } @@ -186,56 +202,90 @@ template T generic_get::operator()(encoder& e) const return do_generic_get(e); } +template T generic_eget::errordefault() const +{ + return get_ctlnum::errordefault(); +} + +template T generic_dget::errordefault() const +{ + return get_ctlnum::errordefault(); +} + +template T generic_get::errordefault() const +{ + return get_ctlnum::errordefault(); +} + + samplerate samplerate::operator()(encoder& e) const { return samplerate(generic_ctl(e, OPUS_GET_SAMPLE_RATE_REQUEST)); } -void compexity::operator()(encoder& e) const +void complexity::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_COMPLEXITY_REQUEST, c); } +generic_eget complexity::get; + void bitrate::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_BITRATE_REQUEST, b); } +generic_eget bitrate::get; + void vbr::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_VBR_REQUEST, v ? 1 : 0); } +generic_eget vbr::get; + void vbr_constraint::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_VBR_CONSTRAINT_REQUEST, c ? 1 : 0); } +generic_eget vbr_constraint::get; + void force_channels::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_FORCE_CHANNELS_REQUEST, f); } +generic_eget force_channels::get; + void max_bandwidth::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_MAX_BANDWIDTH_REQUEST, bw); } +generic_eget max_bandwidth::get; + void bandwidth::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_BANDWIDTH_REQUEST, bw); } +generic_get bandwidth::get; + void signal::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_SIGNAL_REQUEST, s); } +generic_eget signal::get; + void application::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_APPLICATION_REQUEST, app); } +generic_eget application::get; + _lookahead _lookahead::operator()(encoder& e) const { return _lookahead(generic_ctl(e, OPUS_GET_LOOKAHEAD_REQUEST)); @@ -246,21 +296,29 @@ void fec::operator()(encoder& e) const generic_ctl(e, OPUS_SET_INBAND_FEC_REQUEST, f ? 1 : 0); } +generic_eget fec::get; + void lossperc::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_PACKET_LOSS_PERC_REQUEST, loss); } +generic_eget lossperc::get; + void dtx::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_DTX_REQUEST, d ? 1 : 0); } +generic_eget dtx::get; + void lsbdepth::operator()(encoder& e) const { generic_ctl(e, OPUS_SET_LSB_DEPTH_REQUEST, depth); } +generic_eget lsbdepth::get; + _pktduration _pktduration::operator()(encoder& e) const { return _pktduration(generic_ctl(e, OPUS_GET_LAST_PACKET_DURATION_REQUEST)); @@ -301,6 +359,8 @@ void gain::operator()(decoder& d) const generic_ctl(d, OPUS_SET_GAIN_REQUEST, g); } +generic_dget gain::get; + void set_control_int::operator()(encoder& e) const { generic_ctl(e, ctl, val); @@ -325,21 +385,40 @@ void force_instantiate() { encoder e(samplerate::r48k, true, application::audio); decoder d(samplerate::r48k, true); - compexity::get()(e); - bitrate::get()(e); - vbr::get()(e); - vbr_constraint::get()(e); - force_channels::get()(e); - max_bandwidth::get()(e); - bandwidth::get()(e); - bandwidth::get()(d); - signal::get()(e); - application::get()(e); - fec::get()(e); - lossperc::get()(e); - dtx::get()(e); - lsbdepth::get()(e); - gain::get()(d); + complexity::get(e); + bitrate::get(e); + vbr::get(e); + vbr_constraint::get(e); + force_channels::get(e); + max_bandwidth::get(e); + bandwidth::get(e); + bandwidth::get(d); + signal::get(e); + application::get(e); + fec::get(e); + lossperc::get(e); + dtx::get(e); + lsbdepth::get(e); + gain::get(d); + + complexity::get.errordefault(); + bitrate::get.errordefault(); + vbr::get.errordefault(); + vbr_constraint::get.errordefault(); + force_channels::get.errordefault(); + max_bandwidth::get.errordefault(); + bandwidth::get.errordefault(); + signal::get.errordefault(); + application::get.errordefault(); + fec::get.errordefault(); + lossperc::get.errordefault(); + dtx::get.errordefault(); + lsbdepth::get.errordefault(); + gain::get.errordefault(); + + e.ctl_quiet(opus::reset); + e.ctl_quiet(opus::finalrange); + e.ctl_quiet(opus::signal::get); } encoder::~encoder()