Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加pipe支持 #82

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 122 additions & 34 deletions silk/test/Decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ unsigned long GetHighResolutionTime() /* O: time in usec*/
static SKP_int32 rand_seed = 1;

static void print_usage(char* argv[]) {
printf( "\nVersion:20160922 Build By kn007 (kn007.net)");
printf( "\nGithub: https://github.com/kn007/silk-v3-decoder\n");
printf( "\nusage: %s in.bit out.pcm [settings]\n", argv[ 0 ] );
printf( "\nin.bit : Bitstream input to decoder" );
printf( "\nout.pcm : Speech output from decoder" );
printf( "\n settings:" );
printf( "\n-Fs_API <Hz> : Sampling rate of output signal in Hz; default: 24000" );
printf( "\n-loss <perc> : Simulated packet loss percentage (0-100); default: 0" );
printf( "\n-quiet : Print out just some basic values" );
printf( "\n" );
fprintf(stderr, "\nVersion:20160922 Build By kn007 (kn007.net)");
fprintf(stderr, "\nGithub: https://github.com/kn007/silk-v3-decoder\n");
fprintf(stderr, "\nusage: %s in.bit out.pcm [settings]\n", argv[ 0 ] );
fprintf(stderr, "\nin.bit : Bitstream input to decoder" );
fprintf(stderr, "\nout.pcm : Speech output from decoder" );
fprintf(stderr, "\n settings:" );
fprintf(stderr, "\n-Fs_API <Hz> : Sampling rate of output signal in Hz; default: 24000" );
fprintf(stderr, "\n-loss <perc> : Simulated packet loss percentage (0-100); default: 0" );
fprintf(stderr, "\n-quiet : Print out just some basic values" );
fprintf(stderr, "\n" );
}

int main( int argc, char* argv[] )
Expand All @@ -127,7 +127,12 @@ int main( int argc, char* argv[] )
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;
#ifdef _WIN32
HANDLE bitInFile, speechOutFile;
DWORD dwCounter;
#else
FILE *bitInFile, *speechOutFile;
#endif
SKP_int32 packetSize_ms=0, API_Fs_Hz = 0;
SKP_int32 decSizeBytes;
void *psDec;
Expand Down Expand Up @@ -161,53 +166,100 @@ int main( int argc, char* argv[] )
quiet = 1;
args++;
} else {
printf( "Error: unrecognized setting: %s\n\n", argv[ args ] );
fprintf(stderr, "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 );
fprintf(stderr,"********** Silk Decoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version());
fprintf(stderr,"********** Compiled for %d bit cpu *******************************\n", (int)sizeof(void*) * 8 );
fprintf(stderr, "Input: %s\n", bitInFileName );
fprintf(stderr, "Output: %s\n", speechOutFileName );
}

/* Open files */
bitInFile = fopen( bitInFileName, "rb" );
#ifdef _WIN32
if(!SKP_STR_CASEINSENSITIVE_COMPARE(bitInFileName, "-")) {
bitInFile = GetStdHandle(STD_INPUT_HANDLE);
} else {
bitInFile = CreateFileA(bitInFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
}
#else
bitInFile = SKP_STR_CASEINSENSITIVE_COMPARE(bitInFileName, "-") ? fopen( bitInFileName, "rb" ) : stdin;
#endif
if( bitInFile == NULL ) {
printf( "Error: could not open input file %s\n", bitInFileName );
fprintf(stderr, "Error: could not open input file %s\n", bitInFileName );
exit( 0 );
}

/* Check Silk header */
{
char header_buf[ 50 ];
#ifdef _WIN32
ReadFile(bitInFile, header_buf, sizeof(char), NULL, NULL);
#else
fread(header_buf, sizeof(char), 1, bitInFile);
#endif
header_buf[ strlen( "" ) ] = '\0'; /* Terminate with a null character */
if( strcmp( header_buf, "" ) != 0 ) {
#ifdef _WIN32
DWORD dwCounter;
ReadFile(bitInFile, header_buf, sizeof(char)*strlen( "!SILK_V3" ),&dwCounter,NULL);
counter = dwCounter / sizeof(char);
#else
counter = fread( header_buf, sizeof( char ), strlen( "!SILK_V3" ), bitInFile );
#endif
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 );
fprintf(stderr, "Error: Wrong Header %s\n", header_buf );
exit( 0 );
}
} else {
#ifdef _WIN32
ReadFile(bitInFile, header_buf, sizeof(char)*strlen( "#!SILK_V3" ),&dwCounter,NULL);
counter = dwCounter / sizeof(char);
#else
counter = fread( header_buf, sizeof( char ), strlen( "#!SILK_V3" ), bitInFile );
#endif
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 );
fprintf(stderr, "Error: Wrong Header %s\n", header_buf );
exit( 0 );
}
}
}

speechOutFile = fopen( speechOutFileName, "wb" );
#ifdef _WIN32
if (!SKP_STR_CASEINSENSITIVE_COMPARE(speechOutFileName, "-"))
{
speechOutFile = GetStdHandle(STD_OUTPUT_HANDLE);
} else {
speechOutFile = CreateFileA(speechOutFileName,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
}

#else
speechOutFile = SKP_STR_CASEINSENSITIVE_COMPARE(bitInFileName, "-") ? fopen( speechOutFileName, "wb" ) : stdout;
#endif
if( speechOutFile == NULL ) {
printf( "Error: could not open output file %s\n", speechOutFileName );
fprintf(stderr, "Error: could not open output file %s\n", speechOutFileName );
exit( 0 );
}

Expand All @@ -224,14 +276,14 @@ int main( int argc, char* argv[] )
/* Create decoder */
ret = SKP_Silk_SDK_Get_Decoder_Size( &decSizeBytes );
if( ret ) {
printf( "\nSKP_Silk_SDK_Get_Decoder_Size returned %d", ret );
fprintf(stderr, "\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 );
fprintf(stderr, "\nSKP_Silk_InitDecoder returned %d", ret );
}

totPackets = 0;
Expand All @@ -241,13 +293,24 @@ int main( int argc, char* argv[] )
/* Simulate the jitter buffer holding MAX_FEC_DELAY packets */
for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
/* Read payload size */
#ifdef _WIN32
ReadFile(bitInFile, &nBytes, sizeof(SKP_int16), &dwCounter, NULL);
counter = dwCounter / sizeof(SKP_int16);
#else
counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile );
#endif

#ifdef _SYSTEM_IS_BIG_ENDIAN
swap_endian( &nBytes, 1 );
#endif
/* Read payload */
#ifdef _WIN32
ReadFile(bitInFile, payloadEnd, sizeof(SKP_uint8)*nBytes, &dwCounter, NULL);
counter = dwCounter / sizeof(SKP_uint8);
#else
counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile );

#endif

if( ( SKP_int16 )counter < nBytes ) {
break;
}
Expand All @@ -258,7 +321,13 @@ int main( int argc, char* argv[] )

while( 1 ) {
/* Read payload size */
#ifdef _WIN32
ReadFile(bitInFile,&nBytes,sizeof(SKP_int16),&dwCounter,NULL);
counter = dwCounter / sizeof(SKP_int16);
#else
counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile );
#endif

#ifdef _SYSTEM_IS_BIG_ENDIAN
swap_endian( &nBytes, 1 );
#endif
Expand All @@ -267,7 +336,13 @@ int main( int argc, char* argv[] )
}

/* Read payload */
#ifdef _WIN32
ReadFile(bitInFile,payloadEnd,sizeof(SKP_uint8)*nBytes,&dwCounter,NULL);
counter = dwCounter / sizeof(SKP_uint8);
#else
counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile );
#endif

if( ( SKP_int16 )counter < nBytes ) {
break;
}
Expand Down Expand Up @@ -319,7 +394,7 @@ int main( int argc, char* argv[] )
/* 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 );
fprintf(stderr, "\nSKP_Silk_SDK_Decode returned %d", ret );
}

frames++;
Expand All @@ -339,7 +414,7 @@ int main( int argc, char* argv[] )
/* Generate 20 ms */
ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
if( ret ) {
printf( "\nSKP_Silk_Decode returned %d", ret );
fprintf(stderr, "\nSKP_Silk_Decode returned %d", ret );
}
outPtr += len;
tot_len += len;
Expand All @@ -354,7 +429,11 @@ int main( int argc, char* argv[] )
#ifdef _SYSTEM_IS_BIG_ENDIAN
swap_endian( out, tot_len );
#endif
#ifdef _WIN32
WriteFile(speechOutFile, out, sizeof(SKP_int16)*tot_len,NULL,NULL);
#else
fwrite( out, sizeof( SKP_int16 ), tot_len, speechOutFile );
#endif

/* Update buffer */
totBytes = 0;
Expand Down Expand Up @@ -416,7 +495,7 @@ int main( int argc, char* argv[] )
/* 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 );
fprintf(stderr, "\nSKP_Silk_SDK_Decode returned %d", ret );
}

frames++;
Expand All @@ -437,7 +516,7 @@ int main( int argc, char* argv[] )
for( i = 0; i < DecControl.framesPerPacket; i++ ) {
ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
if( ret ) {
printf( "\nSKP_Silk_Decode returned %d", ret );
fprintf(stderr, "\nSKP_Silk_Decode returned %d", ret );
}
outPtr += len;
tot_len += len;
Expand All @@ -452,7 +531,11 @@ int main( int argc, char* argv[] )
#ifdef _SYSTEM_IS_BIG_ENDIAN
swap_endian( out, tot_len );
#endif
#ifdef _WIN32
WriteFile(speechOutFile, out, sizeof(SKP_int16)*tot_len,NULL,NULL);
#else
fwrite( out, sizeof( SKP_int16 ), tot_len, speechOutFile );
#endif

/* Update Buffer */
totBytes = 0;
Expand All @@ -478,24 +561,29 @@ int main( int argc, char* argv[] )
}

if( !quiet ) {
printf( "\nDecoding Finished \n" );
fprintf(stderr, "\nDecoding Finished \n" );
}

/* Free decoder */
free( psDec );

/* Close files */
#ifdef _WIN32
CloseHandle(speechOutFile);
CloseHandle(bitInFile);
#else
fclose( speechOutFile );
fclose( bitInFile );
#endif

filetime = totPackets * 1e-3 * packetSize_ms;
if( !quiet ) {
printf("\nFile length: %.3f s", filetime);
printf("\nTime for decoding: %.3f s (%.3f%% of realtime)", 1e-6 * tottime, 1e-4 * tottime / filetime);
printf("\n\n");
fprintf(stderr,"\nFile length: %.3f s", filetime);
fprintf(stderr,"\nTime for decoding: %.3f s (%.3f%% of realtime)", 1e-6 * tottime, 1e-4 * tottime / filetime);
fprintf(stderr,"\n\n");
} else {
/* print time and % of realtime */
printf( "%.3f %.3f %d\n", 1e-6 * tottime, 1e-4 * tottime / filetime, totPackets );
fprintf(stderr, "%.3f %.3f %d\n", 1e-6 * tottime, 1e-4 * tottime / filetime, totPackets );
}
return 0;
}
Loading