virtual bool Transcode(const void * fromPtr, unsigned & fromLen, void * toPtr, unsigned & toLen, unsigned & flags) { SKP_SILK_SDK_DecControlStruct status; status.API_sampleRate = m_definition->sampleRate; SKP_int16 nSamplesOut = toLen/2; SKP_int error = SKP_Silk_SDK_Decode(m_state, &status, false, (const SKP_uint8 *)fromPtr, fromLen, (SKP_int16 *)toPtr, &nSamplesOut); toLen = nSamplesOut*2; if (status.moreInternalDecoderFrames) fromLen = 0; if (error == 0) return true; PTRACE(1, "SILK", "Decoder error " << error); return false; }
/* * Recover lost frame. */ static pj_status_t silk_codec_recover(pjmedia_codec *codec, unsigned output_buf_len, struct pjmedia_frame *output) { struct silk_private *silk; silk = (struct silk_private*) codec->codec_data; SKP_int16 nBytes = output_buf_len; int ret; PJ_ASSERT_RETURN(output, PJ_EINVAL); PJ_LOG(5, (THIS_FILE, "Recover silk frame")); /* Decode */ ret = SKP_Silk_SDK_Decode( silk->psDec, &silk->dec, 1, NULL, 0, output->buf, &nBytes ); if(ret){ PJ_LOG(1, (THIS_FILE, "Failed to recover silk frame %d", ret)); return PJ_EINVAL; } output->size = nBytes; output->type = PJMEDIA_FRAME_TYPE_AUDIO; return PJ_SUCCESS; }
static switch_status_t switch_silk_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { struct silk_context *context = codec->private_info; SKP_int16 ret, len; int16_t *target = decoded_data; *decoded_data_len = 0; do { ret = SKP_Silk_SDK_Decode(context->dec_state, &context->decoder_object, ((*flag & SFF_PLC)), encoded_data, encoded_data_len, target, &len); if (ret) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SKP_Silk_Decode returned %d!\n", ret); } target += len; *decoded_data_len += (len * 2); } while (context->decoder_object.moreInternalDecoderFrames); return SWITCH_STATUS_SUCCESS; }
int SILK_2_Pcm16( unsigned char* out_buf, unsigned char* in_buf, unsigned int size, unsigned int channels, unsigned int rate, long h_codec ) { SILK_state* st = (SILK_state*)h_codec; SKP_int16 ret, len, *outPtr = (SKP_int16*)out_buf; do { /* Decode 20 ms */ ret = SKP_Silk_SDK_Decode( st->psDec, &st->decControl, 0/*lost*/, in_buf, size, outPtr, &len ); if( ret ) { ERROR( "SKP_Silk_SDK_Decode returned %d\n", ret ); return ret; } outPtr += len; if( (unsigned char*)outPtr - out_buf > AUDIO_BUFFER_SIZE ) { /* Oooops!!! buffer overflow !!! */ ERROR("Buffer overflow (size=%li)\n", (unsigned char*)outPtr - out_buf); return -1; // TODO } /* Until last 20 ms frame of packet has been decoded */ } while( st->decControl.moreInternalDecoderFrames ); return (unsigned char*)outPtr - out_buf; }
//decode packet of size bytes in buffer to short samples output_buffer //returns number of samples int SILK8_decode(short *output_buffer, unsigned char *buffer, int size) { int16_t len; int16_t *outPtr; outPtr = output_buffer; do { SKP_Silk_SDK_Decode(psDec, &DecControl, 0, (uint8_t *) buffer, size, outPtr, &len); outPtr += len; } while (DecControl.moreInternalDecoderFrames); return (int)(outPtr - output_buffer); //number of samples }
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK8_decode (JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) { jbyte buffer [MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; jshort output_buffer[( MAX_FRAME_LENGTH << 1 ) * MAX_INPUT_FRAMES ]; // SKP_int16 *outPtr; int ret; SKP_int16 len; // int tot_len,frames; if (!codec_open) return 0; #ifdef DEBUG_SILK8 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "##### BEGIN DECODE ******** decoding frame size: %d\n", size); #endif env->GetByteArrayRegion(encoded, RTP_HDR_SIZE, size, buffer); // outPtr = output_buffer; // tot_len = 0; // frames = 0; // do { ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0,(SKP_uint8 *) buffer, size, output_buffer,&len ); if( ret ) { __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "!!!!!!!! SKP_Silk_SDK_Decode returned: %d\n", ret); Print_Decode_Error_Msg(ret); } #ifdef DEBUG_SILK8 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "##### DECODED length: %d\n\t Frame #: %d", len); #endif // frames++; // outPtr += len; // tot_len += len; // } while( DecControl.moreInternalDecoderFrames ); env->SetShortArrayRegion(lin, 0, len,output_buffer); return (jint)len; }
static int plc(struct audec_state *st, int16_t *sampv, size_t *sampc) { int16_t nsamp = *sampc; int ret; ret = SKP_Silk_SDK_Decode(st->dec, &st->decControl, 1, NULL, 0, sampv, &nsamp); if (ret) return EPROTO; *sampc = nsamp; return 0; }
static int decode(struct audec_state *st, int16_t *sampv, size_t *sampc, const uint8_t *buf, size_t len) { int16_t nsamp = *sampc; int ret; ret = SKP_Silk_SDK_Decode(st->dec, &st->decControl, 0, buf, (int)len, sampv, &nsamp); if (ret) { re_printf("SKP_Silk_SDK_Decode: ret=%d\n", ret); } *sampc = nsamp; return 0; }
static int SILKDecode (const struct PluginCodec_Definition * codec, void * context, const void * fromPtr, unsigned * fromLen, void * toPtr, unsigned * toLen, unsigned int * flag) { SKP_int16 ret=0; SKP_int16 len, tot_len; SKP_int16 *outPtr; struct SILKDecoderControl * ctxt = (struct SILKDecoderControl *)context; WaitAndSignal m(*ctxt->mutex); SKP_int frames; outPtr = (short *)toPtr; frames = 0; tot_len = 0; len = 0; do { ret = SKP_Silk_SDK_Decode( ctxt->context, &ctxt->control, 0, (const unsigned char *)fromPtr, (SKP_uint8)*fromLen, (short *)toPtr, &len ); if( ret ) { printf( "\nSKP_Silk_SDK_Decode returned %d", ret ); } // frames++; // *(short *)toPtr += len; tot_len = len; // if( frames > MAX_INPUT_FRAMES ) { // tot_len = 0; // frames = 0; // } } while( ctxt->control.moreInternalDecoderFrames ); *toLen = tot_len*2; return 1; }
static pj_status_t silk_codec_decode(pjmedia_codec *codec, const struct pjmedia_frame *input, unsigned output_buf_len, struct pjmedia_frame *output) { int ret = 0; struct silk_private *silk; unsigned count; SKP_int16 len; PJ_ASSERT_RETURN(input && output, PJ_EINVAL); silk = (struct silk_private*) codec->codec_data; //For silk parsing need to decode... len = output->size; ret = SKP_Silk_SDK_Decode( silk->psDec, &silk->dec, 0, //not loss frames input->buf, //Packet input->size, //Packet size output->buf, &len ); if(ret){ PJ_LOG(1, (THIS_FILE, "Failed to decode silk frame : %d", ret)); output->type = PJMEDIA_FRAME_TYPE_NONE; output->buf = NULL; output->size = 0; }else{ output->size = len; output->type = PJMEDIA_FRAME_TYPE_AUDIO; output->timestamp = input->timestamp; } return PJ_SUCCESS; }
static switch_status_t switch_silk_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { struct silk_context *context = codec->private_info; SKP_int16 ret, len; int16_t *target = decoded_data; switch_core_session_t *session = codec->session; stfu_instance_t *jb = NULL; SKP_int lost_flag = (*flag & SFF_PLC); stfu_frame_t next_frame; SKP_uint8 recbuff[STFU_DATALEN]; SKP_int16 reclen; int32_t found_frame; switch_bool_t did_lbrr = SWITCH_FALSE; int i; *decoded_data_len = 0; if (lost_flag) { if (session) { jb = switch_core_session_get_jb(session, SWITCH_MEDIA_TYPE_AUDIO); } if (jb && codec && codec->cur_frame) { for (i = 1; i <= MAX_LBRR_DELAY; i++) { found_frame = stfu_n_copy_next_frame(jb, codec->cur_frame->timestamp, codec->cur_frame->seq, i, &next_frame); if (found_frame) { SKP_Silk_SDK_search_for_LBRR(next_frame.data, next_frame.dlen, i, (SKP_uint8*) &recbuff, &reclen); if (reclen) { encoded_data = &recbuff; encoded_data_len = reclen; lost_flag = SKP_FALSE; did_lbrr = SWITCH_TRUE; break; } } } } } do { ret = SKP_Silk_SDK_Decode(context->dec_state, &context->decoder_object, lost_flag, encoded_data, encoded_data_len, target, &len); if (ret){ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SKP_Silk_Decode returned %d!\n", ret); printSilkError(ret); /* if FEC was activated, we can ignore bit errors*/ if (! (ret == SKP_SILK_DEC_PAYLOAD_ERROR && did_lbrr)) return SWITCH_STATUS_FALSE; } target += len; *decoded_data_len += (len * 2); } while (context->decoder_object.moreInternalDecoderFrames); return SWITCH_STATUS_SUCCESS; }
int main( int argc, char* argv[] ) { unsigned long tottime, starttime; double filetime; size_t counter; SKP_int32 args, totPackets, i, k; SKP_int16 ret, len, tot_len; SKP_int16 nBytes; SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; SKP_uint8 *payloadEnd = NULL, *payloadToDec = NULL; SKP_uint8 FECpayload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ], *payloadPtr; SKP_int16 nBytesFEC; SKP_int16 nBytesPerPacket[ MAX_LBRR_DELAY + 1 ], totBytes; SKP_int16 out[ ( ( FRAME_LENGTH_MS * MAX_API_FS_KHZ ) << 1 ) * MAX_INPUT_FRAMES ], *outPtr; char speechOutFileName[ 150 ], bitInFileName[ 150 ]; FILE *bitInFile, *speechOutFile; SKP_int32 packetSize_ms=0, API_Fs_Hz = 0; SKP_int32 decSizeBytes; void *psDec; SKP_float loss_prob; SKP_int32 frames, lost, quiet; SKP_SILK_SDK_DecControlStruct DecControl; if( argc < 3 ) { print_usage( argv ); exit( 0 ); } /* default settings */ quiet = 0; loss_prob = 0.0f; /* get arguments */ args = 1; strcpy( bitInFileName, argv[ args ] ); args++; strcpy( speechOutFileName, argv[ args ] ); args++; while( args < argc ) { if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) { sscanf( argv[ args + 1 ], "%f", &loss_prob ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_API" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &API_Fs_Hz ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-quiet" ) == 0 ) { quiet = 1; args++; } else { printf( "Error: unrecognized setting: %s\n\n", argv[ args ] ); print_usage( argv ); exit( 0 ); } } if( !quiet ) { printf("********** Silk Decoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version()); printf("********** Compiled for %d bit cpu *******************************\n", (int)sizeof(void*) * 8 ); printf( "Input: %s\n", bitInFileName ); printf( "Output: %s\n", speechOutFileName ); } /* Open files */ bitInFile = fopen( bitInFileName, "rb" ); if( bitInFile == NULL ) { printf( "Error: could not open input file %s\n", bitInFileName ); exit( 0 ); } /* Check Silk header */ { char header_buf[ 50 ]; fread(header_buf, sizeof(char), 1, bitInFile); header_buf[ strlen( "" ) ] = '\0'; /* Terminate with a null character */ if( strcmp( header_buf, "" ) != 0 ) { counter = fread( header_buf, sizeof( char ), strlen( "!SILK_V3" ), bitInFile ); header_buf[ strlen( "!SILK_V3" ) ] = '\0'; /* Terminate with a null character */ if( strcmp( header_buf, "!SILK_V3" ) != 0 ) { /* Non-equal strings */ printf( "Error: Wrong Header %s\n", header_buf ); exit( 0 ); } } else { counter = fread( header_buf, sizeof( char ), strlen( "#!SILK_V3" ), bitInFile ); header_buf[ strlen( "#!SILK_V3" ) ] = '\0'; /* Terminate with a null character */ if( strcmp( header_buf, "#!SILK_V3" ) != 0 ) { /* Non-equal strings */ printf( "Error: Wrong Header %s\n", header_buf ); exit( 0 ); } } } speechOutFile = fopen( speechOutFileName, "wb" ); if( speechOutFile == NULL ) { printf( "Error: could not open output file %s\n", speechOutFileName ); exit( 0 ); } /* Set the samplingrate that is requested for the output */ if( API_Fs_Hz == 0 ) { DecControl.API_sampleRate = 24000; } else { DecControl.API_sampleRate = API_Fs_Hz; } /* Initialize to one frame per packet, for proper concealment before first packet arrives */ DecControl.framesPerPacket = 1; /* Create decoder */ ret = SKP_Silk_SDK_Get_Decoder_Size( &decSizeBytes ); if( ret ) { printf( "\nSKP_Silk_SDK_Get_Decoder_Size returned %d", ret ); } psDec = malloc( decSizeBytes ); /* Reset decoder */ ret = SKP_Silk_SDK_InitDecoder( psDec ); if( ret ) { printf( "\nSKP_Silk_InitDecoder returned %d", ret ); } totPackets = 0; tottime = 0; payloadEnd = payload; /* Simulate the jitter buffer holding MAX_FEC_DELAY packets */ for( i = 0; i < MAX_LBRR_DELAY; i++ ) { /* Read payload size */ counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile ); #ifdef _SYSTEM_IS_BIG_ENDIAN swap_endian( &nBytes, 1 ); #endif /* Read payload */ counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile ); if( ( SKP_int16 )counter < nBytes ) { break; } nBytesPerPacket[ i ] = nBytes; payloadEnd += nBytes; totPackets++; } while( 1 ) { /* Read payload size */ counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile ); #ifdef _SYSTEM_IS_BIG_ENDIAN swap_endian( &nBytes, 1 ); #endif if( nBytes < 0 || counter < 1 ) { break; } /* Read payload */ counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile ); if( ( SKP_int16 )counter < nBytes ) { break; } /* Simulate losses */ rand_seed = SKP_RAND( rand_seed ); if( ( ( ( float )( ( rand_seed >> 16 ) + ( 1 << 15 ) ) ) / 65535.0f >= ( loss_prob / 100.0f ) ) && ( counter > 0 ) ) { nBytesPerPacket[ MAX_LBRR_DELAY ] = nBytes; payloadEnd += nBytes; } else { nBytesPerPacket[ MAX_LBRR_DELAY ] = 0; } if( nBytesPerPacket[ 0 ] == 0 ) { /* Indicate lost packet */ lost = 1; /* Packet loss. Search after FEC in next packets. Should be done in the jitter buffer */ payloadPtr = payload; for( i = 0; i < MAX_LBRR_DELAY; i++ ) { if( nBytesPerPacket[ i + 1 ] > 0 ) { starttime = GetHighResolutionTime(); SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], ( i + 1 ), FECpayload, &nBytesFEC ); tottime += GetHighResolutionTime() - starttime; if( nBytesFEC > 0 ) { payloadToDec = FECpayload; nBytes = nBytesFEC; lost = 0; break; } } payloadPtr += nBytesPerPacket[ i + 1 ]; } } else { lost = 0; nBytes = nBytesPerPacket[ 0 ]; payloadToDec = payload; } /* Silk decoder */ outPtr = out; tot_len = 0; starttime = GetHighResolutionTime(); if( lost == 0 ) { /* No Loss: Decode all frames in the packet */ frames = 0; do { /* Decode 20 ms */ ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, payloadToDec, nBytes, outPtr, &len ); if( ret ) { printf( "\nSKP_Silk_SDK_Decode returned %d", ret ); } frames++; outPtr += len; tot_len += len; if( frames > MAX_INPUT_FRAMES ) { /* Hack for corrupt stream that could generate too many frames */ outPtr = out; tot_len = 0; frames = 0; } /* Until last 20 ms frame of packet has been decoded */ } while( DecControl.moreInternalDecoderFrames ); } else { /* Loss: Decode enough frames to cover one packet duration */ for( i = 0; i < DecControl.framesPerPacket; i++ ) { /* Generate 20 ms */ ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len ); if( ret ) { printf( "\nSKP_Silk_Decode returned %d", ret ); } outPtr += len; tot_len += len; } } packetSize_ms = tot_len / ( DecControl.API_sampleRate / 1000 ); tottime += GetHighResolutionTime() - starttime; totPackets++; /* Write output to file */ #ifdef _SYSTEM_IS_BIG_ENDIAN swap_endian( out, tot_len ); #endif fwrite( out, sizeof( SKP_int16 ), tot_len, speechOutFile ); /* Update buffer */ totBytes = 0; for( i = 0; i < MAX_LBRR_DELAY; i++ ) { totBytes += nBytesPerPacket[ i + 1 ]; } SKP_memmove( payload, &payload[ nBytesPerPacket[ 0 ] ], totBytes * sizeof( SKP_uint8 ) ); payloadEnd -= nBytesPerPacket[ 0 ]; SKP_memmove( nBytesPerPacket, &nBytesPerPacket[ 1 ], MAX_LBRR_DELAY * sizeof( SKP_int16 ) ); if( !quiet ) { fprintf( stderr, "\rPackets decoded: %d", totPackets ); } }
static int silktolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) { struct silk_coder_pvt *coder = pvt->pvt; SKP_int ret = 0; SKP_int16 numSamplesOut = 0; SKP_int16 totalSamplesOut = 0; SKP_int16 *dst = (SKP_int16*)pvt->outbuf.i16; SKP_uint8 *src = (SKP_uint8*)f->data.ptr; SKP_int srcBytes = (SKP_int)f->datalen; SKP_int lostFlag = 0; /* assume no loss for now */ int decodeIterations = SILK_MAX_INTERNAL_FRAMES; /* int i = 0; struct ast_frame *nf = NULL; SKP_uint8 FECPayload[SILK_MAX_BYTES_PER_FRAME * SILK_MAX_INTERNAL_FRAMES]; SKP_int16 FECBytes = 0; */ /* If we indicate native PLC in the translator for silktolin * then we may get passed an empty frame (f->datalen = 0), indicating * that we should fill in some PLC data. So when we get f->datalen=0 * we should check to see if we have any next frames (check * f->frame_list.next ?) and look inside them with * SKP_Silk_SDK_search_for_LBRR(). We can search up to 2 frames ahead. * If we find LBRR data, then we should pass that data to the * decoder. If we do not find any LBRR data, then we should * just pass lostFlag=1 to the decoder for normal PLC */ if(srcBytes == 0) { /* Native PLC */ lostFlag = 1; /* search for LBRR data */ /* FIXME: Actually do the lookahead, which I guess will require a jitterbuffer? */ /* if (f->frame_list && f->frame_list.next) { nf = f->frame_list.next; } for(i = 0; i < SILK_MAX_LBRR_DELAY; ++i){ if(nf) { if(nf->datalen) { SKP_Silk_SDK_search_for_LBRR((SKP_uint8*)nf->data.ptr, nf->datalen, i+1, FECPayload, &FECBytes); if(FECBytes > 0){ src = FECPayload; srcBytes = FECBytes; lostFlag = 0; break; } } if (nf->frame_list && nf->frame_list.next) { nf = nf->frame_list.next; } } } */ } if(lostFlag) { /* set the decodeIterations for the do{}while() to be the * number of frames in the last decoded packet */ decodeIterations = coder->decControl.framesPerPacket; ast_log(LOG_NOTICE, "silktolin indicated lost packet - no LBRR"); } do { ret = SKP_Silk_SDK_Decode(coder->psDec, &coder->decControl, lostFlag, src, srcBytes, dst, &numSamplesOut); if(ret) { ast_log(LOG_NOTICE, "SKP_Silk_SDK_Decode returned %d\n", ret); } dst += numSamplesOut; totalSamplesOut += numSamplesOut; /* decrement the number of iterations remaining */ decodeIterations--; /* while we have more data and have not gone too far */ } while (coder->decControl.moreInternalDecoderFrames && decodeIterations > 0); /* okay, we've decoded everything we can */ pvt->samples = totalSamplesOut; pvt->datalen = totalSamplesOut * 2; return 0; }
int VoiceEncoder_Silk::Decompress(const char *pCompressed, int compressedBytes, char *pUncompressed, int maxUncompressedBytes) { int nPayloadSize; // ebp@2 char *pWritePos; // ebx@5 const char *pReadPos; // edx@5 char *pWritePosMax; // [sp+28h] [bp-44h]@4 const char *pReadPosMax; // [sp+3Ch] [bp-30h]@1 const int outSampleRate = 8000; m_decControl.API_sampleRate = outSampleRate; int nSamplesPerFrame = outSampleRate / 50; if (compressedBytes <= 0) { return 0; } pReadPosMax = &pCompressed[compressedBytes]; pReadPos = pCompressed; pWritePos = pUncompressed; pWritePosMax = &pUncompressed[maxUncompressedBytes]; while (pReadPos < pReadPosMax) { if (pReadPosMax - pReadPos < 2) { break; } nPayloadSize = *(uint16 *)pReadPos; pReadPos += sizeof(uint16); if (nPayloadSize == 0xFFFF) { ResetState(); break; } if (nPayloadSize == 0) { //DTX (discontinued transmission) int numEmptySamples = nSamplesPerFrame; short nSamples = (pWritePosMax - pWritePos) / 2; if (nSamples < numEmptySamples) { break; } memset(pWritePos, 0, numEmptySamples * 2); pWritePos += numEmptySamples * 2; continue; } if ((pReadPos + nPayloadSize) > pReadPosMax) { break; } do { short nSamples = (pWritePosMax - pWritePos) / 2; int decodeRes = SKP_Silk_SDK_Decode(m_pDecoder, &m_decControl, 0, (const unsigned char*)pReadPos, nPayloadSize, (__int16 *)pWritePos, &nSamples); if (SKP_SILK_NO_ERROR != decodeRes) { return (pWritePos - pUncompressed) / 2; } pWritePos += nSamples * sizeof(int16); } while (m_decControl.moreInternalDecoderFrames); pReadPos += nPayloadSize; } return (pWritePos - pUncompressed) / 2; }