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
2828enum {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
3232bool s_f_opusParseOgg = false ;
3333bool s_f_newSteamTitle = false ; // streamTitle
@@ -39,7 +39,7 @@ bool s_f_lastPage = false;
3939bool s_f_nextChunk = false ;
4040
4141uint8_t s_opusChannels = 0 ;
42- uint8_t s_mode = 0 ;
42+ uint16_t s_mode = 0 ;
4343uint8_t s_opusCountCode = 0 ;
4444uint8_t s_opusPageNr = 0 ;
4545uint8_t s_frameCount = 0 ;
@@ -64,6 +64,7 @@ uint16_t *s_opusSegmentTable;
6464uint8_t s_opusSegmentTableSize = 0 ;
6565int16_t s_opusSegmentTableRdPtr = -1 ;
6666int8_t s_opusError = 0 ;
67+ int8_t s_prev_mode = 0 ;
6768float s_opusCompressionRatio = 0 ;
6869
6970std::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- // ----------------------------------------------------------------------------------------------------------------------------------------------------
337338int32_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// ----------------------------------------------------------------------------------------------------------------------------------------------------
0 commit comments