1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #include "mumble_pch.hpp"
7 
8 #include "CELTCodec.h"
9 
10 #include "Audio.h"
11 #include "Version.h"
12 
CELTCodec(const QString & version)13 CELTCodec::CELTCodec(const QString &version) {
14 	bValid = true;
15 	cmMode = NULL;
16 	qsVersion = version;
17 	iBitstreamVersion = INT_MIN;
18 
19 	this->celt_encoder_destroy = ::celt_encoder_destroy;
20 	this->celt_encoder_ctl = ::celt_encoder_ctl;
21 
22 	this->celt_decoder_destroy = ::celt_decoder_destroy;
23 	this->celt_decoder_ctl = ::celt_decoder_ctl;
24 }
25 
~CELTCodec()26 CELTCodec::~CELTCodec() {
27 	if (cmMode)
28 		::celt_mode_destroy(const_cast<CELTMode *>(cmMode));
29 }
30 
isValid() const31 bool CELTCodec::isValid() const {
32 	return bValid;
33 }
34 
bitstreamVersion() const35 int CELTCodec::bitstreamVersion() const {
36 	if (cmMode && iBitstreamVersion == INT_MIN)
37 		::celt_mode_info(cmMode, CELT_GET_BITSTREAM_VERSION, reinterpret_cast<celt_int32 *>(&iBitstreamVersion));
38 
39 	return iBitstreamVersion;
40 }
41 
version() const42 QString CELTCodec::version() const {
43 	return qsVersion;
44 }
45 
report() const46 void CELTCodec::report() const {
47 	qWarning("CELT bitstream %08x from internal CELT with SBCELT decoding", bitstreamVersion());
48 }
49 
CELTCodecSBCELT()50 CELTCodecSBCELT::CELTCodecSBCELT() : CELTCodec(QLatin1String("0.7.0")) {
51 	if (bValid) {
52 		cmMode = ::celt_mode_create(SAMPLE_RATE, SAMPLE_RATE / 100, NULL);
53 		cmSBCELTMode = ::sbcelt_mode_create(SAMPLE_RATE, SAMPLE_RATE / 100, NULL);
54 
55 		this->celt_decoder_destroy = ::sbcelt_decoder_destroy;
56 		this->celt_decoder_ctl = ::sbcelt_decoder_ctl;
57 	}
58 }
59 
encoderCreate()60 CELTEncoder *CELTCodecSBCELT::encoderCreate() {
61 	return ::celt_encoder_create(cmMode, 1, NULL);
62 }
63 
decoderCreate()64 CELTDecoder *CELTCodecSBCELT::decoderCreate() {
65 	return ::sbcelt_decoder_create(cmSBCELTMode, 1, NULL);
66 }
67 
encode(CELTEncoder * st,const celt_int16 * pcm,unsigned char * compressed,int nbCompressedBytes)68 int CELTCodecSBCELT::encode(CELTEncoder *st, const celt_int16 *pcm, unsigned char *compressed, int nbCompressedBytes) {
69 	return ::celt_encode(st, pcm, NULL, compressed, nbCompressedBytes);
70 }
71 
decode_float(CELTDecoder * st,const unsigned char * data,int len,float * pcm)72 int CELTCodecSBCELT::decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm) {
73 	return ::sbcelt_decode_float(st, data, len, pcm);
74 }