From c64b321e7a564c673ab0322e4df895be51ec29a2 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Tue, 23 May 2023 18:31:45 -0400 Subject: [PATCH] DRED API update output() renamed to decode(), dred objects using alloc() and free(), OpusDRED now passed as cost for decoding. --- include/opus.h | 8 ++++---- silk/dred_decoder.c | 22 ---------------------- src/opus_decoder.c | 41 ++++++++++++++++++++++++++++++++++++++--- src/opus_demo.c | 6 +++--- src/opus_private.h | 2 +- 5 files changed, 46 insertions(+), 33 deletions(-) diff --git a/include/opus.h b/include/opus.h index 01668a8b..41244f23 100644 --- a/include/opus.h +++ b/include/opus.h @@ -557,12 +557,12 @@ OPUS_EXPORT int opus_dred_get_size(void); /** Allocates and initializes a DRED state. * @param [out] error int*: #OPUS_OK Success or @ref opus_errorcodes */ -OPUS_EXPORT OpusDRED *opus_dred_create(int *error); +OPUS_EXPORT OpusDRED *opus_dred_alloc(int *error); /** Frees an OpusDRED allocated by opus_dred_create(). * @param[in] st OpusDRED*: State to be freed. */ -OPUS_EXPORT void opus_dred_destroy(OpusDRED *dec); +OPUS_EXPORT void opus_dred_free(OpusDRED *dec); /** Decode an Opus DRED packet. * @param [in] dred OpusDRED*: DRED state @@ -592,7 +592,7 @@ OPUS_EXPORT int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src * frame_size must be a multiple of 2.5 ms. * @returns Number of decoded samples or @ref opus_errorcodes */ -OPUS_EXPORT int opus_decoder_dred_output(OpusDecoder *st, OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size); +OPUS_EXPORT int opus_decoder_dred_decode(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size); /** Decode audio from an Opus DRED packet with floating point output. * @param [in] st OpusDecoder*: Decoder state @@ -604,7 +604,7 @@ OPUS_EXPORT int opus_decoder_dred_output(OpusDecoder *st, OpusDRED *dred, opus_i * frame_size must be a multiple of 2.5 ms. * @returns Number of decoded samples or @ref opus_errorcodes */ -OPUS_EXPORT int opus_decoder_dred_output_float(OpusDecoder *st, OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size); +OPUS_EXPORT int opus_decoder_dred_decode_float(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size); /** Parse an opus packet into one or more frames. diff --git a/silk/dred_decoder.c b/silk/dred_decoder.c index 5eecb42e..d010f91a 100644 --- a/silk/dred_decoder.c +++ b/silk/dred_decoder.c @@ -37,29 +37,7 @@ #include "celt/entdec.h" -int opus_dred_get_size(void) -{ - return sizeof(OpusDRED); -} -OpusDRED *opus_dred_create(int *error) -{ - OpusDRED *dec; - dec = (OpusDRED *)opus_alloc(opus_dred_get_size()); - if (dec == NULL) - { - if (error) - *error = OPUS_ALLOC_FAIL; - return NULL; - } - return dec; - -} - -void opus_dred_destroy(OpusDRED *dec) -{ - free(dec); -} int dred_ec_decode(OpusDRED *dec, const opus_uint8 *bytes, int num_bytes, int min_feature_frames) { diff --git a/src/opus_decoder.c b/src/opus_decoder.c index 5d5d9494..7e1f2d32 100644 --- a/src/opus_decoder.c +++ b/src/opus_decoder.c @@ -634,7 +634,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, int opus_decode_native(OpusDecoder *st, const unsigned char *data, opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, - int self_delimited, opus_int32 *packet_offset, int soft_clip, OpusDRED *dred, opus_int32 dred_offset) + int self_delimited, opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset) { int i, nb_samples; int count, offset; @@ -1207,6 +1207,41 @@ static int dred_find_payload(const unsigned char *data, opus_int32 len, const un } #endif +int opus_dred_get_size(void) +{ +#ifdef ENABLE_NEURAL_FEC + return sizeof(OpusDRED); +#else + return 0; +#endif +} + +OpusDRED *opus_dred_alloc(int *error) +{ +#ifdef ENABLE_NEURAL_FEC + OpusDRED *dec; + dec = (OpusDRED *)opus_alloc(opus_dred_get_size()); + if (dec == NULL) + { + if (error) + *error = OPUS_ALLOC_FAIL; + return NULL; + } + return dec; +#else + if (error) + *error = OPUS_UNIMPLEMENTED; + return NULL; +#endif +} + +void opus_dred_free(OpusDRED *dec) +{ +#ifdef ENABLE_NEURAL_FEC + free(dec); +#endif +} + int opus_dred_parse(OpusDREDDecoder *dred_dec, OpusDRED *dred, const unsigned char *data, opus_int32 len, opus_int32 max_dred_samples, opus_int32 sampling_rate, int defer_processing) { #ifdef ENABLE_NEURAL_FEC @@ -1261,7 +1296,7 @@ int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src, OpusDRED * #endif } -int opus_decoder_dred_output(OpusDecoder *st, OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size) +int opus_decoder_dred_decode(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size) { #ifdef ENABLE_NEURAL_FEC VARDECL(float, out); @@ -1295,7 +1330,7 @@ int opus_decoder_dred_output(OpusDecoder *st, OpusDRED *dred, opus_int32 dred_of #endif } -int opus_decoder_dred_output_float(OpusDecoder *st, OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size) +int opus_decoder_dred_decode_float(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size) { #ifdef ENABLE_NEURAL_FEC if(frame_size<=0) diff --git a/src/opus_demo.c b/src/opus_demo.c index 7992050e..f1417f7e 100644 --- a/src/opus_demo.c +++ b/src/opus_demo.c @@ -632,7 +632,7 @@ int main(int argc, char *argv[]) frame_size = 2*48000; } dred_dec = opus_dred_decoder_create(&err); - dred = opus_dred_create(&err); + dred = opus_dred_alloc(&err); while (!stop) { if (delayed_celt) @@ -815,7 +815,7 @@ int main(int argc, char *argv[]) output_samples = opus_decode(dec, data, len, out, output_samples, 1); } else if (fr < lost_count) { opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples)); - output_samples = opus_decoder_dred_output(dec, dred, (lost_count-fr)*sampling_rate/100, out, output_samples); + output_samples = opus_decoder_dred_decode(dec, dred, (lost_count-fr)*sampling_rate/100, out, output_samples); } else { output_samples = max_frame_size; output_samples = opus_decode(dec, data, len, out, output_samples, 0); @@ -918,7 +918,7 @@ int main(int argc, char *argv[]) failure: opus_encoder_destroy(enc); opus_decoder_destroy(dec); - opus_dred_destroy(dred); + opus_dred_free(dred); opus_dred_decoder_destroy(dred_dec); free(data); if (fin) diff --git a/src/opus_private.h b/src/opus_private.h index 478ea1a6..65195a92 100644 --- a/src/opus_private.h +++ b/src/opus_private.h @@ -155,7 +155,7 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ int opus_decode_native(OpusDecoder *st, const unsigned char *data, opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, int self_delimited, - opus_int32 *packet_offset, int soft_clip, OpusDRED *dred, opus_int32 dred_offset); + opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset); /* Make sure everything is properly aligned. */ static OPUS_INLINE int align(int i)