Unverified Commit bd819fc1 authored by Wolle's avatar Wolle Committed by GitHub

Add files via upload

parent 5f4c5dd4
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "celt.h" #include "celt.h"
#include "opus_decoder.h"
CELTDecoder *cdec; CELTDecoder *cdec;
band_ctx_t s_band_ctx; band_ctx_t s_band_ctx;
...@@ -768,7 +769,7 @@ uint32_t alg_unquant(int16_t *X, int32_t N, int32_t K, int32_t spread, int32_t B ...@@ -768,7 +769,7 @@ uint32_t alg_unquant(int16_t *X, int32_t N, int32_t K, int32_t spread, int32_t B
if(K <= 0) log_e("alg_unquant() needs at least one pulse"); if(K <= 0) log_e("alg_unquant() needs at least one pulse");
if(N <= 1) log_e("alg_unquant() needs at least two dimensions"); if(N <= 1) log_e("alg_unquant() needs at least two dimensions");
int32_t* iy = s_iyBuff; assert(N <= 96); int32_t* iy = s_iyBuff; assert(N <= 176);
Ryy = decode_pulses(iy, N, K); Ryy = decode_pulses(iy, N, K);
normalise_residual(iy, X, N, Ryy, gain); normalise_residual(iy, X, N, Ryy, gain);
exp_rotation(X, N, -1, B, K, spread); exp_rotation(X, N, -1, B, K, spread);
...@@ -1971,15 +1972,12 @@ int32_t celt_decoder_get_size(int32_t channels){ ...@@ -1971,15 +1972,12 @@ int32_t celt_decoder_get_size(int32_t channels){
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
int32_t celt_decoder_init(int32_t channels){ int32_t celt_decoder_init(int32_t channels){
log_e("hier");
// allocate buffers first // allocate buffers first
if (channels < 0 || channels > 2){ if (channels < 0 || channels > 2){
log_e("OPUS_BAD_ARG"); return ERR_OPUS_CHANNELS_OUT_OF_RANGE;
return OPUS_BAD_ARG;
} }
if (cdec == NULL){ if (cdec == NULL){
log_e("cdec is NULL"); return ERR_OPUS_CELT_ALLOC_FAIL;
return OPUS_ALLOC_FAIL;
} }
int n = celt_decoder_get_size(channels); int n = celt_decoder_get_size(channels);
...@@ -2007,11 +2005,9 @@ int32_t celt_decoder_init(int32_t channels){ ...@@ -2007,11 +2005,9 @@ int32_t celt_decoder_init(int32_t channels){
cdec->_decode_mem[0] = 0; cdec->_decode_mem[0] = 0;
cdec->end = cdec->mode->effEBands; // 21 cdec->end = cdec->mode->effEBands; // 21
log_e("hier1");
int ret = celt_decoder_ctl(OPUS_RESET_STATE); int ret = celt_decoder_ctl(OPUS_RESET_STATE);
log_i("ret %i", ret); if(ret < 0) return ret;
log_e("ok"); return ERR_OPUS_NONE;
return OPUS_OK;
} }
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
...@@ -2026,7 +2022,7 @@ bool CELTDecoder_AllocateBuffers(void) { ...@@ -2026,7 +2022,7 @@ bool CELTDecoder_AllocateBuffers(void) {
size_t omd = celt_decoder_get_size(2); size_t omd = celt_decoder_get_size(2);
if(!cdec) {cdec = (CELTDecoder*) __heap_caps_malloc(omd);} if(!cdec) {cdec = (CELTDecoder*) __heap_caps_malloc(omd);}
if(!s_freqBuff) {s_freqBuff = (int32_t*) __heap_caps_malloc(960 * sizeof(int32_t));} if(!s_freqBuff) {s_freqBuff = (int32_t*) __heap_caps_malloc(960 * sizeof(int32_t));}
if(!s_iyBuff) {s_iyBuff = (int32_t*) __heap_caps_malloc(96 * sizeof(int32_t));} if(!s_iyBuff) {s_iyBuff = (int32_t*) __heap_caps_malloc(176 * sizeof(int32_t));}
if(!s_normBuff) {s_normBuff = (int16_t*) __heap_caps_malloc(1248 * sizeof(int16_t));} if(!s_normBuff) {s_normBuff = (int16_t*) __heap_caps_malloc(1248 * sizeof(int16_t));}
if(!s_XBuff) {s_XBuff = (int16_t*) __heap_caps_malloc(1920 * sizeof(int16_t));} if(!s_XBuff) {s_XBuff = (int16_t*) __heap_caps_malloc(1920 * sizeof(int16_t));}
if(!s_bits1Buff) {s_bits1Buff = (int32_t*) __heap_caps_malloc(21 * sizeof(int32_t));} if(!s_bits1Buff) {s_bits1Buff = (int32_t*) __heap_caps_malloc(21 * sizeof(int32_t));}
...@@ -2280,12 +2276,12 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf, ...@@ -2280,12 +2276,12 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf,
{ {
for(LM = 0; LM <= m_CELTMode.maxLM; LM++) // m_CELTMode.maxLM == 3 for(LM = 0; LM <= m_CELTMode.maxLM; LM++) // m_CELTMode.maxLM == 3
if(m_CELTMode.shortMdctSize << LM == frame_size) break; // frame_size == 960 if(m_CELTMode.shortMdctSize << LM == frame_size) break; // frame_size == 960
if(LM > m_CELTMode.maxLM) {log_e("OPUS_BAD_ARG"); return OPUS_BAD_ARG;} if(LM > m_CELTMode.maxLM) {log_e("OPUS_BAD_ARG"); return ERR_OPUS_CELT_BAD_ARG;}
} }
M = 1 << LM; // LM=3 -> M = 8 M = 1 << LM; // LM=3 -> M = 8
if(len < 0 || len > 1275 || outbuf == NULL) {log_e("OPUS_BAD_ARG"); return OPUS_BAD_ARG;} if(len < 0 || len > 1275 || outbuf == NULL) {log_e("OPUS_BAD_ARG"); return ERR_OPUS_CELT_BAD_ARG;}
N = M * m_CELTMode.shortMdctSize; // const m_CELTMode.shortMdctSize == 120, M == 8 -> N = 960 N = M * m_CELTMode.shortMdctSize; // const m_CELTMode.shortMdctSize == 120, M == 8 -> N = 960
...@@ -2295,7 +2291,7 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf, ...@@ -2295,7 +2291,7 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf,
out_syn[c] = decode_mem[c] + DECODE_BUFFER_SIZE - N; out_syn[c] = decode_mem[c] + DECODE_BUFFER_SIZE - N;
} while(++c < CC); } while(++c < CC);
if(len <= 1) {log_e("OPUS_BAD_ARG"); return OPUS_BAD_ARG;} if(len <= 1) {log_e("OPUS_BAD_ARG"); return ERR_OPUS_CELT_BAD_ARG;}
if(C == 1) { if(C == 1) {
for(i = 0; i < nbEBands; i++) oldBandE[i] = max(oldBandE[i], oldBandE[nbEBands + i]); for(i = 0; i < nbEBands; i++) oldBandE[i] = max(oldBandE[i], oldBandE[nbEBands + i]);
...@@ -2477,7 +2473,7 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf, ...@@ -2477,7 +2473,7 @@ int32_t celt_decode_with_ec(const uint8_t *inbuf, int32_t len, int16_t *outbuf,
deemphasis(out_syn, outbuf, N); deemphasis(out_syn, outbuf, N);
if(ec_tell() > 8 * len) return OPUS_INTERNAL_ERROR; if(ec_tell() > 8 * len) return ERR_CELT_OPUS_INTERNAL_ERROR;
if(s_ec.error) cdec->error = 1; if(s_ec.error) cdec->error = 1;
return frame_size; return frame_size;
...@@ -2491,17 +2487,17 @@ int32_t celt_decoder_ctl(int32_t request, ...) { ...@@ -2491,17 +2487,17 @@ int32_t celt_decoder_ctl(int32_t request, ...) {
switch (request) { switch (request) {
case CELT_SET_END_BAND_REQUEST: { case CELT_SET_END_BAND_REQUEST: {
int32_t value = va_arg(ap, int32_t); int32_t value = va_arg(ap, int32_t);
if (value < 1 || value > cdec->mode->nbEBands) goto bad_arg; if (value < 1 || value > cdec->mode->nbEBands) {va_end(ap); return ERR_OPUS_CELT_END_BAND;}
cdec->end = value; cdec->end = value;
} break; } break;
case CELT_SET_CHANNELS_REQUEST: { case CELT_SET_CHANNELS_REQUEST: {
int32_t value = va_arg(ap, int32_t); int32_t value = va_arg(ap, int32_t);
if (value < 1 || value > 2) goto bad_arg; if (value < 1 || value > 2) {va_end(ap); return ERR_OPUS_CELT_SET_CHANNELS;}
cdec->stream_channels = value; cdec->stream_channels = value;
} break; } break;
case CELT_GET_AND_CLEAR_ERROR_REQUEST: { case CELT_GET_AND_CLEAR_ERROR_REQUEST: {
int32_t *value = va_arg(ap, int32_t *); int32_t *value = va_arg(ap, int32_t *);
if (value == NULL) goto bad_arg; if (value == NULL) {va_end(ap); return ERR_OPUS_CELT_CLEAR_REQUEST;}
*value = cdec->error; *value = cdec->error;
cdec->error = 0; cdec->error = 0;
} break; } break;
...@@ -2522,7 +2518,7 @@ int32_t celt_decoder_ctl(int32_t request, ...) { ...@@ -2522,7 +2518,7 @@ int32_t celt_decoder_ctl(int32_t request, ...) {
} break; } break;
case CELT_GET_MODE_REQUEST: { case CELT_GET_MODE_REQUEST: {
const CELTMode **value = va_arg(ap, const CELTMode **); const CELTMode **value = va_arg(ap, const CELTMode **);
if (value == 0) goto bad_arg; if (value == 0){va_end(ap); return ERR_OPUS_CELT_GET_MODE_REQUEST;}
*value = cdec->mode; *value = cdec->mode;
} break; } break;
case CELT_SET_SIGNALLING_REQUEST: { case CELT_SET_SIGNALLING_REQUEST: {
...@@ -2530,17 +2526,12 @@ int32_t celt_decoder_ctl(int32_t request, ...) { ...@@ -2530,17 +2526,12 @@ int32_t celt_decoder_ctl(int32_t request, ...) {
cdec->signalling = value; cdec->signalling = value;
} break; } break;
default: default:
goto bad_request;
}
va_end(ap);
return OPUS_OK;
bad_arg:
va_end(ap); va_end(ap);
log_e("OPUS_BAD_ARG"); return ERR_OPUS_CELT_UNKNOWN_REQUEST;
return OPUS_BAD_ARG; break;
bad_request: }
va_end(ap); va_end(ap);
return OPUS_UNIMPLEMENTED; return ERR_OPUS_NONE;
} }
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
......
...@@ -37,12 +37,6 @@ ...@@ -37,12 +37,6 @@
#include "Arduino.h" #include "Arduino.h"
#define OPUS_OK 0
#define OPUS_BAD_ARG -18
#define OPUS_INTERNAL_ERROR -19
#define OPUS_UNIMPLEMENTED -20
#define OPUS_ALLOC_FAIL -21
#define OPUS_RESET_STATE 4028 #define OPUS_RESET_STATE 4028
#define OPUS_GET_SAMPLE_RATE_REQUEST 4029 #define OPUS_GET_SAMPLE_RATE_REQUEST 4029
......
...@@ -27,6 +27,7 @@ uint16_t *m_segmentTable; ...@@ -27,6 +27,7 @@ uint16_t *m_segmentTable;
uint8_t m_segmentTableSize = 0; uint8_t m_segmentTableSize = 0;
int16_t s_segmentTableRdPtr = -1; int16_t s_segmentTableRdPtr = -1;
int8_t error = 0; int8_t error = 0;
float m_CompressionRatio = 0;
bool OPUSDecoder_AllocateBuffers(){ bool OPUSDecoder_AllocateBuffers(){
const uint32_t CELT_SET_END_BAND_REQUEST = 10012; const uint32_t CELT_SET_END_BAND_REQUEST = 10012;
...@@ -64,6 +65,7 @@ void OPUSsetDefaults(){ ...@@ -64,6 +65,7 @@ void OPUSsetDefaults(){
m_segmentTableSize = 0; m_segmentTableSize = 0;
s_oldmode = 0xFF; s_oldmode = 0xFF;
s_segmentTableRdPtr = -1; s_segmentTableRdPtr = -1;
error = 0; error = 0;
} }
...@@ -77,8 +79,6 @@ int OPUSDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){ ...@@ -77,8 +79,6 @@ int OPUSDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
else return ret; // error else return ret; // error
} }
if(f_m_opusFramePacket){ if(f_m_opusFramePacket){
if(m_segmentTableSize > 0){ if(m_segmentTableSize > 0){
s_segmentTableRdPtr++; s_segmentTableRdPtr++;
...@@ -135,7 +135,10 @@ uint8_t OPUSGetBitsPerSample(){ ...@@ -135,7 +135,10 @@ uint8_t OPUSGetBitsPerSample(){
return 16; return 16;
} }
uint32_t OPUSGetBitRate(){ uint32_t OPUSGetBitRate(){
return 1; if(m_CompressionRatio != 0){
return (16 * 2 * 48000) / m_CompressionRatio; //bitsPerSample * channel* SampleRate/CompressionRatio
}
else return 0;
} }
uint16_t OPUSGetOutputSamps(){ uint16_t OPUSGetOutputSamps(){
return s_validSamples; // 1024 return s_validSamples; // 1024
...@@ -258,7 +261,7 @@ int parseOpusHead(uint8_t *inbuf, int nBytes){ // reference https://wiki.xiph.o ...@@ -258,7 +261,7 @@ int parseOpusHead(uint8_t *inbuf, int nBytes){ // reference https://wiki.xiph.o
outputGain += *(inbuf + 16); outputGain += *(inbuf + 16);
uint8_t channelMap = *(inbuf + 18); uint8_t channelMap = *(inbuf + 18);
if(channelCount == 0 or channelCount >2) return ERR_OPUS_NR_OF_CHANNELS_UNSUPPORTED; if(channelCount == 0 or channelCount >2) return ERR_OPUS_CHANNELS_OUT_OF_RANGE;
m_channels = channelCount; m_channels = channelCount;
if(sampleRate != 48000) return ERR_OPUS_INVALID_SAMPLERATE; if(sampleRate != 48000) return ERR_OPUS_INVALID_SAMPLERATE;
m_samplerate = sampleRate; m_samplerate = sampleRate;
...@@ -319,6 +322,7 @@ int OPUSparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph ...@@ -319,6 +322,7 @@ int OPUSparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph
m_segmentLength += n; m_segmentLength += n;
} }
m_segmentTableSize = segmentTableWrPtr + 1; m_segmentTableSize = segmentTableWrPtr + 1;
m_CompressionRatio = (float)(960 * 2 * pageSegments)/m_segmentLength; // const 960 validBytes out
bool continuedPage = headerType & 0x01; // set: page contains data of a packet continued from the previous page bool continuedPage = headerType & 0x01; // set: page contains data of a packet continued from the previous page
bool firstPage = headerType & 0x02; // set: this is the first page of a logical bitstream (bos) bool firstPage = headerType & 0x02; // set: this is the first page of a logical bitstream (bos)
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
enum : int8_t {OPUS_PARSE_OGG_DONE = 100, enum : int8_t {OPUS_PARSE_OGG_DONE = 100,
ERR_OPUS_NONE = 0, ERR_OPUS_NONE = 0,
ERR_OPUS_NR_OF_CHANNELS_UNSUPPORTED = -1, ERR_OPUS_CHANNELS_OUT_OF_RANGE = -1,
ERR_OPUS_INVALID_SAMPLERATE = -2, ERR_OPUS_INVALID_SAMPLERATE = -2,
ERR_OPUS_EXTRA_CHANNELS_UNSUPPORTED = -3, ERR_OPUS_EXTRA_CHANNELS_UNSUPPORTED = -3,
ERR_OPUS_DECODER_ASYNC = -4, ERR_OPUS_DECODER_ASYNC = -4,
...@@ -17,7 +17,13 @@ enum : int8_t {OPUS_PARSE_OGG_DONE = 100, ...@@ -17,7 +17,13 @@ enum : int8_t {OPUS_PARSE_OGG_DONE = 100,
ERR_OPUS_CELT_BAD_ARG = -18, ERR_OPUS_CELT_BAD_ARG = -18,
ERR_OPUS_CELT_INTERNAL_ERROR = -19, ERR_OPUS_CELT_INTERNAL_ERROR = -19,
ERR_OPUS_CELT_UNIMPLEMENTED = -20, ERR_OPUS_CELT_UNIMPLEMENTED = -20,
ERR_OPUS_CELT_ALLOC_FAIL = -21}; ERR_OPUS_CELT_ALLOC_FAIL = -21,
ERR_OPUS_CELT_UNKNOWN_REQUEST = -22,
ERR_OPUS_CELT_GET_MODE_REQUEST = - 23,
ERR_OPUS_CELT_CLEAR_REQUEST = -24,
ERR_OPUS_CELT_SET_CHANNELS = -25,
ERR_OPUS_CELT_END_BAND = -26,
ERR_CELT_OPUS_INTERNAL_ERROR = -27};
bool OPUSDecoder_AllocateBuffers(); bool OPUSDecoder_AllocateBuffers();
void OPUSDecoder_FreeBuffers(); void OPUSDecoder_FreeBuffers();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment