Skip to content

Commit 4ee590b

Browse files
committed
add CELT_SET_START_BAND_REQUEST
1 parent 2a0eb19 commit 4ee590b

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

src/opus_decoder/celt.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Created on: Sep 01.2022
77
*
8-
* Updated on: Mar 13.2024
8+
* Updated on: Dec 21.2024
99
* Author: Wolle (schreibfaul1)
1010
*/
1111

@@ -50,6 +50,7 @@ int16_t* s_tmpBuff; // mem in deinterleave_hadamard and interlea
5050

5151
const uint32_t CELT_GET_AND_CLEAR_ERROR_REQUEST = 10007;
5252
const uint32_t CELT_SET_CHANNELS_REQUEST = 10008;
53+
const uint32_t CELT_SET_START_BAND_REQUEST = 10010;
5354
const uint32_t CELT_SET_END_BAND_REQUEST = 10012;
5455
const uint32_t CELT_GET_MODE_REQUEST = 10015;
5556
const uint32_t CELT_SET_SIGNALLING_REQUEST = 10016;
@@ -58,6 +59,7 @@ const uint32_t CELT_SET_TONALITY_SLOPE_REQUEST = 10020;
5859
const uint32_t CELT_SET_ANALYSIS_REQUEST = 10022;
5960
const uint32_t OPUS_SET_LFE_REQUEST = 10024;
6061
const uint32_t OPUS_SET_ENERGY_MASK_REQUEST = 10026;
62+
const uint32_t CELT_SET_SILK_INFO_REQUEST = 10028;
6163

6264
const uint8_t EPSILON = 1;
6365
const uint8_t BITRES = 3;
@@ -2483,6 +2485,11 @@ int32_t celt_decoder_ctl(int32_t request, ...) {
24832485

24842486
va_start(ap, request);
24852487
switch (request) {
2488+
case CELT_SET_START_BAND_REQUEST: {
2489+
int32_t value = va_arg(ap, int32_t);
2490+
if (value < 1 || value > s_celtDec->mode->nbEBands) {va_end(ap); return ERR_OPUS_CELT_START_BAND;}
2491+
s_celtDec->start = value;
2492+
} break;
24862493
case CELT_SET_END_BAND_REQUEST: {
24872494
int32_t value = va_arg(ap, int32_t);
24882495
if (value < 1 || value > s_celtDec->mode->nbEBands) {va_end(ap); return ERR_OPUS_CELT_END_BAND;}

src/opus_decoder/opus_decoder.cpp

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* based on Xiph.Org Foundation celt decoder
44
*
55
* Created on: 26.01.2023
6-
* Updated on: 20.12.2024
6+
* Updated on: 21.12.2024
77
*/
88
//----------------------------------------------------------------------------------------------------------------------
99
// O G G / O P U S I M P L.
@@ -27,7 +27,7 @@ const uint32_t CELT_GET_AND_CLEAR_ERROR_REQUEST = 10007;
2727

2828
enum {OPUS_BANDWIDTH_NARROWBAND = 1101, OPUS_BANDWIDTH_MEDIUMBAND = 1102, OPUS_BANDWIDTH_WIDEBAND = 1103,
2929
OPUS_BANDWIDTH_SUPERWIDEBAND = 1104, OPUS_BANDWIDTH_FULLBAND = 1105};
30-
enum {MODE_CELT_ONLY, MODE_SILK_ONLY, MODE_HYBRID};
30+
enum {MODE_NONE = 0, MODE_SILK_ONLY = 1000, MODE_HYBRID = 1001, MODE_CELT_ONLY = 1002};
3131

3232
bool s_f_opusParseOgg = false;
3333
bool s_f_newSteamTitle = false; // streamTitle
@@ -39,7 +39,7 @@ bool s_f_lastPage = false;
3939
bool s_f_nextChunk = false;
4040

4141
uint8_t s_opusChannels = 0;
42-
uint8_t s_mode = 0;
42+
uint16_t s_mode = 0;
4343
uint8_t s_opusCountCode = 0;
4444
uint8_t s_opusPageNr = 0;
4545
uint8_t s_frameCount = 0;
@@ -64,6 +64,7 @@ uint16_t *s_opusSegmentTable;
6464
uint8_t s_opusSegmentTableSize = 0;
6565
int16_t s_opusSegmentTableRdPtr = -1;
6666
int8_t s_opusError = 0;
67+
int8_t s_prev_mode = 0;
6768
float s_opusCompressionRatio = 0;
6869

6970
std::vector <uint32_t>s_opusBlockPicItem;
@@ -145,6 +146,7 @@ void OPUSsetDefaults(){
145146
s_opusPageNr = 0;
146147
s_opusError = 0;
147148
s_endband = 0;
149+
s_prev_mode = 0;
148150
s_opusBlockPicItem.clear(); s_opusBlockPicItem.shrink_to_fit();
149151
}
150152

@@ -333,41 +335,16 @@ int32_t opusDecodePage3(uint8_t* inbuf, int32_t* bytesLeft, uint32_t segmentLeng
333335
return ret;
334336
}
335337
//----------------------------------------------------------------------------------------------------------------------------------------------------
336-
//----------------------------------------------------------------------------------------------------------------------------------------------------
337338
int32_t opus_decode_frame(uint8_t *inbuf, int16_t *outbuf, int32_t packetLen, uint16_t samplesPerFrame) {
338339

339-
// int pcm_transition_silk_size;
340-
// int16_t pcm_transition_silk;
341-
// int pcm_transition_celt_size;
342-
// int16_t pcm_transition_celt;
343-
344340
int32_t ret = 0;
345-
// int32_t silk_frame_size;
346-
347-
if(s_bandWidth) {
348-
int endband = 21;
349-
switch(s_bandWidth) {
350-
case OPUS_BANDWIDTH_NARROWBAND: endband = 13; break;
351-
case OPUS_BANDWIDTH_MEDIUMBAND:
352-
case OPUS_BANDWIDTH_WIDEBAND: endband = 17; break;
353-
case OPUS_BANDWIDTH_SUPERWIDEBAND: endband = 19; break;
354-
case OPUS_BANDWIDTH_FULLBAND: endband = 21; break;
355-
default: break;
356-
}
357-
celt_decoder_ctl(endband);
358-
}
359341

360342
if (s_mode == MODE_CELT_ONLY){
361343
celt_decoder_ctl(CELT_SET_END_BAND_REQUEST, s_endband);
362344
ec_dec_init((uint8_t *)inbuf, packetLen);
363345
ret = celt_decode_with_ec((int16_t*)outbuf, samplesPerFrame);
364346
}
365347

366-
if(s_mode == MODE_HYBRID){
367-
log_w("Hybrid mode not yet supported");
368-
ret = samplesPerFrame;
369-
}
370-
371348
if(s_mode == MODE_SILK_ONLY) {
372349
int decodedSamples = 0;
373350
int32_t silk_frame_size;
@@ -379,27 +356,51 @@ int32_t opus_decode_frame(uint8_t *inbuf, int16_t *outbuf, int32_t packetLen, ui
379356
ec_dec_init((uint8_t *)inbuf, packetLen);
380357
uint8_t APIchannels = 2;
381358
silk_setRawParams(s_opusChannels, APIchannels, payloadSize_ms, s_internalSampleRate, 48000);
382-
// log_w("payloadSize_ms %i, s_internalSampleRate %i", payloadSize_ms, s_internalSampleRate);
383-
static bool silkInit = false;
384-
if(!silkInit){
385-
silkInit = true;
386-
silk_InitDecoder();
387-
}
388-
// silk_InitDecoder();
389-
390359
do{
391360
/* Call SILK decoder */
392361
int lost_flag = 0;
393362
int first_frame = decodedSamples == 0;
394363
int silk_ret = silk_Decode(lost_flag, first_frame, (int16_t*)outbuf + decodedSamples, &silk_frame_size);
395364
if(silk_ret)log_w("silk_ret %i", silk_ret);
396365
decodedSamples += silk_frame_size;
397-
// log_w("decodedSamples %i, samplesPerFrame %i", decodedSamples, samplesPerFrame);
398366
} while(decodedSamples < samplesPerFrame);
399-
400-
return decodedSamples;
367+
ret = decodedSamples;
401368
}
402369

370+
if(s_mode == MODE_HYBRID){
371+
log_w("Hybrid mode not yet supported");
372+
return samplesPerFrame;
373+
int decodedSamples = 0;
374+
int32_t silk_frame_size;
375+
int F2_5, F5, F10, F20;
376+
F20 = packetLen / 50;
377+
F10 = F20 >> 1;
378+
F5 = F10 >> 1;
379+
F2_5 = F5 >> 1;
380+
if(packetLen < F2_5) { return ERR_OPUS_BUFFER_TOO_SMALL; }
381+
ec_dec_init((uint8_t *)inbuf, packetLen);
382+
s_internalSampleRate = 16000;
383+
uint8_t APIchannels = 2;
384+
uint16_t payloadSize_ms = max(10, 1000 * samplesPerFrame / 48000);
385+
int lost_flag = 0;
386+
int first_frame = decodedSamples == 0;
387+
silk_setRawParams(s_opusChannels, APIchannels, payloadSize_ms, s_internalSampleRate, 48000);
388+
silk_Decode(lost_flag, first_frame, (int16_t*)outbuf + decodedSamples, &silk_frame_size);
389+
if(s_bandWidth) {
390+
s_endband = 21;
391+
switch(s_bandWidth) {
392+
case OPUS_BANDWIDTH_NARROWBAND: s_endband = 13; break;
393+
case OPUS_BANDWIDTH_MEDIUMBAND:
394+
case OPUS_BANDWIDTH_WIDEBAND: s_endband = 17; break;
395+
case OPUS_BANDWIDTH_SUPERWIDEBAND: s_endband = 19; break;
396+
case OPUS_BANDWIDTH_FULLBAND: s_endband = 21; break;
397+
default: break;
398+
}
399+
}
400+
celt_decoder_ctl(CELT_SET_START_BAND_REQUEST, s_endband);
401+
celt_decoder_ctl(CELT_SET_END_BAND_REQUEST, s_endband);
402+
ret = celt_decode_with_ec((int16_t*)outbuf, samplesPerFrame);
403+
}
403404
return ret;
404405
}
405406
//----------------------------------------------------------------------------------------------------------------------------------------------------

src/opus_decoder/opus_decoder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum : int8_t {OPUS_CONTINUE = 110,
2121
ERR_OPUS_WIDE_BAND_UNSUPPORTED = -8,
2222
ERR_OPUS_SUPER_WIDE_BAND_UNSUPPORTED = -9,
2323
ERR_OPUS_OGG_SYNC_NOT_FOUND = - 10,
24+
ERR_OPUS_BUFFER_TOO_SMALL = -11,
2425
ERR_OPUS_CELT_BAD_ARG = -18,
2526
ERR_OPUS_CELT_INTERNAL_ERROR = -19,
2627
ERR_OPUS_CELT_UNIMPLEMENTED = -20,
@@ -30,7 +31,8 @@ enum : int8_t {OPUS_CONTINUE = 110,
3031
ERR_OPUS_CELT_CLEAR_REQUEST = -24,
3132
ERR_OPUS_CELT_SET_CHANNELS = -25,
3233
ERR_OPUS_CELT_END_BAND = -26,
33-
ERR_CELT_OPUS_INTERNAL_ERROR = -27};
34+
ERR_OPUS_CELT_START_BAND = -27,
35+
ERR_CELT_OPUS_INTERNAL_ERROR = -28};
3436

3537
bool OPUSDecoder_AllocateBuffers();
3638
void OPUSDecoder_FreeBuffers();

0 commit comments

Comments
 (0)