Skip to content

Commit 1296374

Browse files
committed
prepare for SILK next part
1 parent 6d129fd commit 1296374

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

src/opus_decoder/opus_decoder.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ int32_t opusDecodePage3(uint8_t* inbuf, int32_t* bytesLeft, uint32_t segmentLeng
331331
//----------------------------------------------------------------------------------------------------------------------------------------------------
332332
int32_t opus_decode_frame(uint8_t *inbuf, int16_t *outbuf, int32_t packetLen, uint16_t samplesPerFrame) {
333333

334+
// int pcm_transition_silk_size;
335+
// int16_t pcm_transition_silk;
336+
// int pcm_transition_celt_size;
337+
// int16_t pcm_transition_celt;
338+
334339
int32_t ret = 0;
335340
// int32_t silk_frame_size;
336341

@@ -358,30 +363,36 @@ int32_t opus_decode_frame(uint8_t *inbuf, int16_t *outbuf, int32_t packetLen, ui
358363
ret = samplesPerFrame;
359364
}
360365

361-
if (s_mode == MODE_SILK_ONLY){
366+
if(s_mode == MODE_SILK_ONLY) {
362367
log_w("Silk mode not yet supported");
363368
ret = samplesPerFrame;
369+
370+
371+
372+
int decodedSamples = 0;
373+
int32_t silk_frame_size;
374+
uint16_t payloadSize_ms = max(10, 1000 * samplesPerFrame / 48000);
375+
if(s_bandWidth == OPUS_BANDWIDTH_NARROWBAND) { s_internalSampleRate = 8000; }
376+
else if(s_bandWidth == OPUS_BANDWIDTH_MEDIUMBAND) { s_internalSampleRate = 12000; }
377+
else if(s_bandWidth == OPUS_BANDWIDTH_WIDEBAND) { s_internalSampleRate = 16000; }
378+
else { s_internalSampleRate = 16000; }
379+
ec_dec_init((uint8_t *)inbuf, packetLen);
380+
silk_setRawParams(2, 2, payloadSize_ms, s_internalSampleRate, 48000);
381+
// log_w("payloadSize_ms %i, s_internalSampleRate %i", payloadSize_ms, s_internalSampleRate);
382+
silk_InitDecoder();
383+
384+
do{
385+
/* Call SILK decoder */
386+
int first_frame = decodedSamples == 0;
387+
int silk_ret = silk_Decode(0, first_frame, (int16_t*)outbuf + decodedSamples, &silk_frame_size);
388+
if(silk_ret)log_w("silk_ret %i", silk_ret);
389+
decodedSamples += silk_frame_size;
390+
// log_w("decodedSamples %i, samplesPerFrame %i", decodedSamples, samplesPerFrame);
391+
} while(decodedSamples < samplesPerFrame);
392+
393+
return decodedSamples;
364394
}
365395

366-
// if(s_mode == MODE_SILK_ONLY) {
367-
// uint16_t payloadSize_ms = max(10, 1000 * samplesPerFrame / 48000);
368-
// if(s_bandWidth == OPUS_BANDWIDTH_NARROWBAND) { s_internalSampleRate = 8000; }
369-
// else if(s_bandWidth == OPUS_BANDWIDTH_MEDIUMBAND) { s_internalSampleRate = 12000; }
370-
// else if(s_bandWidth == OPUS_BANDWIDTH_WIDEBAND) { s_internalSampleRate = 16000; }
371-
// else { s_internalSampleRate = 16000; }
372-
// //log_w("samplesPerFrame %i", samplesPerFrame);
373-
// // int spf = opus_packet_get_samples_per_frame(inbuf, samplesPerFrame); // todo
374-
// //log_w("spf %i", packetLen);
375-
// ec_dec_init((uint8_t *)inbuf, packetLen);
376-
// silk_InitDecoder();
377-
// //log_w("payloadSize_ms %i, s_internalSampleRate %i", payloadSize_ms, s_internalSampleRate);
378-
// silk_setRawParams(1, 2, payloadSize_ms, s_internalSampleRate, 48000);
379-
// int silk_ret = silk_Decode(0, 1, (int16_t*)outbuf, &silk_frame_size);
380-
// if(silk_ret)log_w("silk_ret %i", silk_ret);
381-
// //log_w("silk_frame_size %i", silk_frame_size);
382-
// if(samplesPerFrame != silk_frame_size)log_w("samplesPerFrame %i silk_frame_size %i",samplesPerFrame ,silk_frame_size);
383-
// return silk_frame_size;
384-
// }
385396
return ret;
386397
}
387398
//----------------------------------------------------------------------------------------------------------------------------------------------------
@@ -702,11 +713,10 @@ int8_t opus_FramePacking_Code3(uint8_t *inbuf, int32_t *bytesLeft, int16_t *outb
702713
*bytesLeft -= idx;
703714
}
704715
if(*frameCount > 0){
705-
if(v){ret = opus_decode_frame(inbuf + idx, outbuf, vfs[M - (*frameCount)], spf);}
706-
else{ ret = opus_decode_frame(inbuf + idx, outbuf, fs, spf);}
716+
if(v){ret = opus_decode_frame(inbuf + idx, outbuf, vfs[M - (*frameCount)], spf); *bytesLeft -= vfs[M - (*frameCount)]; /* log_e("code 3, vfs[M - (*frameCount)], spf) %i", vfs[M - (*frameCount)], spf); */ }
717+
else{ ret = opus_decode_frame(inbuf + idx, outbuf, fs, spf); *bytesLeft -= fs; /* log_e("code 3, fs, spf) %i", fs, spf); */ }
707718
// log_w("code 3, ret %i", ret);
708719
*frameCount -= 1;
709-
*bytesLeft -= fs;
710720
s_opusValidSamples = ret;
711721
if(*frameCount > 0) return OPUS_CONTINUE;
712722
}
@@ -743,7 +753,7 @@ uint8_t OPUSGetChannels(){
743753
return s_opusChannels;
744754
}
745755
uint32_t OPUSGetSampRate(){
746-
return 48000; // s_opusSamplerate;
756+
return 48000;
747757
}
748758
uint8_t OPUSGetBitsPerSample(){
749759
return 16;
@@ -919,7 +929,8 @@ int32_t parseOpusHead(uint8_t *inbuf, int32_t nBytes){ // reference https://wik
919929

920930
if(channelCount == 0 || channelCount >2) return ERR_OPUS_CHANNELS_OUT_OF_RANGE;
921931
s_opusChannels = channelCount;
922-
if(sampleRate != 48000 && sampleRate != 44100) return ERR_OPUS_INVALID_SAMPLERATE;
932+
// log_e("sampleRate %i", sampleRate);
933+
// if(sampleRate != 48000 && sampleRate != 44100) return ERR_OPUS_INVALID_SAMPLERATE;
923934
s_opusSamplerate = sampleRate;
924935
if(channelMap > 1) return ERR_OPUS_EXTRA_CHANNELS_UNSUPPORTED;
925936

src/opus_decoder/silk.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,23 +1831,33 @@ int32_t silk_Decode(
18311831

18321832
/* Set up pointers to temp buffers */
18331833
ALLOC(samplesOut2_tmp, s_API_channels == 2 ? *nSamplesOut : ALLOC_NONE, int16_t);
1834-
if(s_API_channels == 2) { resample_out_ptr = samplesOut2_tmp; }
1835-
else { resample_out_ptr = samplesOut; }
1834+
if (s_API_channels == 2) {
1835+
resample_out_ptr = samplesOut2_tmp;
1836+
} else {
1837+
resample_out_ptr = samplesOut;
1838+
}
18361839

1837-
ALLOC(samplesOut1_tmp_storage2, delay_stack_alloc ? s_channelsInternal * (s_channel_state[0].frame_length + 2) : ALLOC_NONE, int16_t);
1838-
if(delay_stack_alloc) {
1839-
#define OPUS_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
1840-
OPUS_COPY(samplesOut1_tmp_storage2, samplesOut, s_channelsInternal * (s_channel_state[0].frame_length + 2));
1840+
ALLOC(samplesOut1_tmp_storage2,
1841+
delay_stack_alloc ? s_channelsInternal * (s_channel_state[0].frame_length + 2) : ALLOC_NONE,
1842+
int16_t);
1843+
1844+
if (delay_stack_alloc) {
1845+
size_t val2 = sizeof(samplesOut1_tmp_storage2);
1846+
memcpy(samplesOut1_tmp_storage2, samplesOut, val2);
18411847
samplesOut1_tmp[0] = samplesOut1_tmp_storage2;
18421848
samplesOut1_tmp[1] = samplesOut1_tmp_storage2 + s_channel_state[0].frame_length + 2;
18431849
}
1844-
for(n = 0; n < silk_min(s_API_channels, s_channelsInternal); n++) {
1850+
for (n = 0; n < silk_min(s_API_channels, s_channelsInternal); n++) {
1851+
18451852
/* Resample decoded signal to API_sampleRate */
1846-
ret += silk_resampler(&s_channel_state[n].resampler_state, resample_out_ptr, &samplesOut1_tmp[n][1], nSamplesOutDec);
1853+
ret +=
1854+
silk_resampler(&s_channel_state[n].resampler_state, resample_out_ptr, &samplesOut1_tmp[n][1], nSamplesOutDec);
18471855

18481856
/* Interleave if stereo output and stereo stream */
1849-
if(s_API_channels == 2) {
1850-
for(i = 0; i < *nSamplesOut; i++) { samplesOut[n + 2 * i] = resample_out_ptr[i]; }
1857+
if (s_API_channels == 2) {
1858+
for (i = 0; i < *nSamplesOut; i++) {
1859+
samplesOut[n + 2 * i] = resample_out_ptr[i];
1860+
}
18511861
}
18521862
}
18531863

0 commit comments

Comments
 (0)