diff options
author | Jean-Marc Valin <jmvalin@amazon.com> | 2023-06-01 20:44:22 +0300 |
---|---|---|
committer | Jean-Marc Valin <jmvalin@amazon.com> | 2023-06-16 20:02:22 +0300 |
commit | 6fb930956f4bc5ee6661d804be0429803aa94ed9 (patch) | |
tree | 489e232c30a4859009925c1996c9a21ca222cfff /src/opus_demo.c | |
parent | d43eb241e353d2a70f7b6786b0ed37ef5bbef67a (diff) |
Add blob loading support to decoder
Diffstat (limited to 'src/opus_demo.c')
-rw-r--r-- | src/opus_demo.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/src/opus_demo.c b/src/opus_demo.c index f1417f7e..7594c5cc 100644 --- a/src/opus_demo.c +++ b/src/opus_demo.c @@ -42,6 +42,50 @@ #define MAX_PACKET 1500 +#ifdef USE_WEIGHTS_FILE +# if __unix__ +# include <fcntl.h> +# include <sys/mman.h> +# include <unistd.h> +# include <sys/stat.h> +/* When available, mmap() is preferable to reading the file, as it leads to + better resource utilization, especially if multiple processes are using the same + file (mapping will be shared in cache). */ +unsigned char *load_blob(const char *filename, int *len) { + int fd; + unsigned char *data; + struct stat st; + stat(filename, &st); + *len = st.st_size; + fd = open(filename, O_RDONLY); + data = mmap(NULL, *len, PROT_READ, MAP_SHARED, fd, 0); + close(fd); + return data; +} +void free_blob(unsigned char *blob, int len) { + munmap(blob, len); +} +# else +unsigned char *load_blob(const char *filename, int *len) { + FILE *file; + unsigned char *data; + file = fopen(filename, "r"); + fseek(file, 0L, SEEK_END); + *len = ftell(file); + fseek(file, 0L, SEEK_SET); + if (*len <= 0) return NULL; + data = malloc(*len); + *len = fread(data, 1, *len, file); + return data; +} +void free_blob(unsigned char *blob, int len) { + free(blob); + (void)len; +} +# endif +#endif + + void print_usage( char* argv[] ) { fprintf(stderr, "Usage: %s [-e] <application> <sampling rate (Hz)> <channels (1/2)> " @@ -270,6 +314,12 @@ int main(int argc, char *argv[]) int lost_count=0; FILE *packet_loss_file=NULL; int dred_duration=0; +#ifdef USE_WEIGHTS_FILE + int blob_len; + unsigned char *blob_data; + const char *filename = "weights_blob.bin"; + blob_data = load_blob(filename, &blob_len); +#endif if (argc < 5 ) { @@ -567,8 +617,9 @@ int main(int argc, char *argv[]) goto failure; } } - - +#ifdef USE_WEIGHTS_FILE + opus_decoder_ctl(dec, OPUS_SET_DNN_BLOB(blob_data, blob_len)); +#endif switch(bandwidth) { case OPUS_BANDWIDTH_NARROWBAND: @@ -928,5 +979,8 @@ failure: free(in); free(out); free(fbytes); +#ifdef USE_WEIGHTS_FILE + free_blob(blob_data, blob_len); +#endif return ret; } |