Skip to content

Commit

Permalink
use opus_(re)alloc and opus_free for dnn and DRED related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mklingb committed Dec 15, 2023
1 parent f5a1efd commit 12fbd81
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 21 deletions.
14 changes: 11 additions & 3 deletions celt/os_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@
#include <string.h>
#include <stdlib.h>

/** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
/** Opus wrapper for malloc(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
#ifndef OVERRIDE_OPUS_ALLOC
static OPUS_INLINE void *opus_alloc (size_t size)
{
return malloc(size);
}
#endif

/** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */
#ifndef OVERRIDE_OPUS_REALLOC
static OPUS_INLINE void *opus_realloc (void *ptr, size_t size)
{
return realloc(ptr, size);
}
#endif

/** Used only for non-threadsafe pseudostack.
If desired, this can always return the same area of memory rather than allocating a new one every time. */
#ifndef OVERRIDE_OPUS_ALLOC_SCRATCH
static OPUS_INLINE void *opus_alloc_scratch (size_t size)
{
Expand All @@ -58,7 +66,7 @@ static OPUS_INLINE void *opus_alloc_scratch (size_t size)
}
#endif

/** Opus wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function and opus_alloc */
/** Opus wrapper for free(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
#ifndef OVERRIDE_OPUS_FREE
static OPUS_INLINE void opus_free (void *ptr)
{
Expand Down
2 changes: 1 addition & 1 deletion dnn/fargan.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int fargan_load_model(FARGANState *st, const unsigned char *data, int len) {
int ret;
parse_weights(&list, data, len);
ret = init_fargan(&st->model, list);
free(list);
opus_free(list);
if (ret == 0) return 0;
else return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion dnn/fwgan.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int fwgan_load_model(FWGANState *st, const unsigned char *data, int len) {
int ret;
parse_weights(&list, data, len);
ret = init_fwgan(&st->model, list);
free(list);
opus_free(list);
if (ret == 0) return 0;
else return -1;
}
Expand Down
7 changes: 4 additions & 3 deletions dnn/lpcnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ int lpcnet_load_model(LPCNetState *st, const unsigned char *data, int len) {
int ret;
parse_weights(&list, data, len);
ret = init_lpcnet_model(&st->model, list);
free(list);
opus_free(list);
if (ret == 0) return 0;
else return -1;
}
Expand All @@ -214,14 +214,15 @@ int lpcnet_load_model(LPCNetState *st, const unsigned char *data, int len) {
LPCNetState *lpcnet_create()
{
LPCNetState *lpcnet;
lpcnet = (LPCNetState *)calloc(lpcnet_get_size(), 1);
lpcnet = (LPCNetState *)opus_alloc(lpcnet_get_size(), 1);
OPUS_CLEAR(lpcnet, 1);
lpcnet_init(lpcnet);
return lpcnet;
}

void lpcnet_destroy(LPCNetState *lpcnet)
{
free(lpcnet);
opus_free(lpcnet);
}

void lpcnet_reset_signal(LPCNetState *lpcnet)
Expand Down
4 changes: 2 additions & 2 deletions dnn/lpcnet_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ int lpcnet_encoder_load_model(LPCNetEncState *st, const unsigned char *data, int

LPCNetEncState *lpcnet_encoder_create(void) {
LPCNetEncState *st;
st = malloc(lpcnet_encoder_get_size());
st = opus_alloc(lpcnet_encoder_get_size());
lpcnet_encoder_init(st);
return st;
}

void lpcnet_encoder_destroy(LPCNetEncState *st) {
free(st);
opus_free(st);
}

static void frame_analysis(LPCNetEncState *st, kiss_fft_cpx *X, float *Ex, const float *in) {
Expand Down
2 changes: 1 addition & 1 deletion dnn/lpcnet_plc.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int lpcnet_plc_load_model(LPCNetPLCState *st, const unsigned char *data, int len
int ret;
parse_weights(&list, data, len);
ret = init_plc_model(&st->model, list);
free(list);
opus_free(list);
if (ret == 0) {
ret = lpcnet_encoder_load_model(&st->enc, data, len);
}
Expand Down
9 changes: 5 additions & 4 deletions dnn/parse_lpcnet_weights.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <string.h>
#include <stdlib.h>
#include "nnet.h"
#include "os_support.h"

#define SPARSE_BLOCK_SIZE 32

Expand All @@ -55,7 +56,7 @@ int parse_weights(WeightArray **list, const unsigned char *data, int len)
{
int nb_arrays=0;
int capacity=20;
*list = malloc(capacity*sizeof(WeightArray));
*list = opus_alloc(capacity*sizeof(WeightArray));
while (len > 0) {
int ret;
WeightArray array = {NULL, 0, 0, 0};
Expand All @@ -64,11 +65,11 @@ int parse_weights(WeightArray **list, const unsigned char *data, int len)
if (nb_arrays+1 >= capacity) {
/* Make sure there's room for the ending NULL element too. */
capacity = capacity*3/2;
*list = realloc(*list, capacity*sizeof(WeightArray));
*list = opus_realloc(*list, capacity*sizeof(WeightArray));
}
(*list)[nb_arrays++] = array;
} else {
free(*list);
opus_free(*list);
*list = NULL;
return -1;
}
Expand Down Expand Up @@ -269,7 +270,7 @@ int main()
printf("found %s: size %d\n", list[i].name, list[i].size);
}
printf("%p\n", list[i].name);
free(list);
opus_free(list);
munmap(data, len);
close(fd);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion dnn/pitchdnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int pitchdnn_load_model(PitchDNNState *st, const unsigned char *data, int len) {
int ret;
parse_weights(&list, data, len);
ret = init_pitchdnn(&st->model, list);
free(list);
opus_free(list);
if (ret == 0) return 0;
else return -1;
}
2 changes: 1 addition & 1 deletion silk/dred_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int dred_encoder_load_model(DREDEnc* enc, const unsigned char *data, int len)
int ret;
parse_weights(&list, data, len);
ret = init_rdovaeenc(&enc->model, list);
free(list);
opus_free(list);
if (ret == 0) {
ret = lpcnet_encoder_load_model(&enc->lpcnet_enc_state, data, len);
}
Expand Down
8 changes: 4 additions & 4 deletions src/opus_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ int dred_decoder_load_model(OpusDREDDecoder *dec, const unsigned char *data, int
int ret;
parse_weights(&list, data, len);
ret = init_rdovaedec(&dec->model, list);
free(list);
opus_free(list);
if (ret == 0) dec->loaded = 1;
return (ret == 0) ? OPUS_OK : OPUS_BAD_ARG;
}
Expand Down Expand Up @@ -1233,8 +1233,8 @@ OpusDREDDecoder *opus_dred_decoder_create(int *error)

void opus_dred_decoder_destroy(OpusDREDDecoder *dec)
{
dec->magic = 0xDE57801D;
free(dec);
if (dec) dec->magic = 0xDE57801D;
opus_free(dec);
}

int opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...)
Expand Down Expand Up @@ -1372,7 +1372,7 @@ OpusDRED *opus_dred_alloc(int *error)
void opus_dred_free(OpusDRED *dec)
{
#ifdef ENABLE_DRED
free(dec);
opus_free(dec);
#else
(void)dec;
#endif
Expand Down

0 comments on commit 12fbd81

Please sign in to comment.