Improvements to error handling and warning fixes

This commit is contained in:
Jean-Marc Valin 2011-05-05 15:30:44 -04:00
parent 1a458862e3
commit 9edde42690
2 changed files with 41 additions and 35 deletions

View file

@ -42,11 +42,11 @@ extern "C" {
#endif #endif
#if defined(__GNUC__) && defined(CELT_BUILD) #if defined(__GNUC__) && defined(CELT_BUILD)
#define EXPORT __attribute__ ((visibility ("default"))) #define CELT_EXPORT __attribute__ ((visibility ("default")))
#elif defined(WIN32) #elif defined(WIN32)
#define EXPORT __declspec(dllexport) #define CELT_EXPORT __declspec(dllexport)
#else #else
#define EXPORT #define CELT_EXPORT
#endif #endif
#define _celt_check_int(x) (((void)((x) == (celt_int32)0)), (celt_int32)(x)) #define _celt_check_int(x) (((void)((x) == (celt_int32)0)), (celt_int32)(x))
@ -157,19 +157,19 @@ typedef struct CELTMode CELTMode;
@param error Returned error code (if NULL, no error will be returned) @param error Returned error code (if NULL, no error will be returned)
@return A newly created mode @return A newly created mode
*/ */
EXPORT CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error); CELT_EXPORT CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error);
/** Destroys a mode struct. Only call this after all encoders and /** Destroys a mode struct. Only call this after all encoders and
decoders using this mode are destroyed as well. decoders using this mode are destroyed as well.
@param mode Mode to be destroyed @param mode Mode to be destroyed
*/ */
EXPORT void celt_mode_destroy(CELTMode *mode); CELT_EXPORT void celt_mode_destroy(CELTMode *mode);
/* Encoder stuff */ /* Encoder stuff */
EXPORT int celt_encoder_get_size(int channels); CELT_EXPORT int celt_encoder_get_size(int channels);
EXPORT int celt_encoder_get_size_custom(const CELTMode *mode, int channels); CELT_EXPORT int celt_encoder_get_size_custom(const CELTMode *mode, int channels);
/** Creates a new encoder state. Each stream needs its own encoder /** Creates a new encoder state. Each stream needs its own encoder
state (can't be shared across simultaneous streams). state (can't be shared across simultaneous streams).
@ -177,7 +177,7 @@ EXPORT int celt_encoder_get_size_custom(const CELTMode *mode, int channels);
@param error Returns an error code @param error Returns an error code
@return Newly created encoder state. @return Newly created encoder state.
*/ */
EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error); CELT_EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error);
/** Creates a new encoder state. Each stream needs its own encoder /** Creates a new encoder state. Each stream needs its own encoder
state (can't be shared across simultaneous streams). state (can't be shared across simultaneous streams).
@ -188,16 +188,16 @@ EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *er
@param error Returns an error code @param error Returns an error code
@return Newly created encoder state. @return Newly created encoder state.
*/ */
EXPORT CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error); CELT_EXPORT CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error);
EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error); CELT_EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error);
EXPORT CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error); CELT_EXPORT CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error);
/** Destroys a an encoder state. /** Destroys a an encoder state.
@param st Encoder state to be destroyed @param st Encoder state to be destroyed
*/ */
EXPORT void celt_encoder_destroy(CELTEncoder *st); CELT_EXPORT void celt_encoder_destroy(CELTEncoder *st);
/** Encodes a frame of audio. /** Encodes a frame of audio.
@param st Encoder state @param st Encoder state
@ -217,7 +217,7 @@ EXPORT void celt_encoder_destroy(CELTEncoder *st);
* the length returned be somehow transmitted to the decoder. Otherwise, no * the length returned be somehow transmitted to the decoder. Otherwise, no
* decoding is possible. * decoding is possible.
*/ */
EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes); CELT_EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
/** Encodes a frame of audio. /** Encodes a frame of audio.
@param st Encoder state @param st Encoder state
@ -233,7 +233,7 @@ EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, int frame_size,
* the length returned be somehow transmitted to the decoder. Otherwise, no * the length returned be somehow transmitted to the decoder. Otherwise, no
* decoding is possible. * decoding is possible.
*/ */
EXPORT int celt_encode(CELTEncoder *st, const celt_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes); CELT_EXPORT int celt_encode(CELTEncoder *st, const celt_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
/** Query and set encoder parameters /** Query and set encoder parameters
@param st Encoder state @param st Encoder state
@ -241,13 +241,13 @@ EXPORT int celt_encode(CELTEncoder *st, const celt_int16 *pcm, int frame_size, u
@param value Pointer to a 32-bit int value @param value Pointer to a 32-bit int value
@return Error code @return Error code
*/ */
EXPORT int celt_encoder_ctl(CELTEncoder * st, int request, ...); CELT_EXPORT int celt_encoder_ctl(CELTEncoder * st, int request, ...);
/* Decoder stuff */ /* Decoder stuff */
EXPORT int celt_decoder_get_size(int channels); CELT_EXPORT int celt_decoder_get_size(int channels);
EXPORT int celt_decoder_get_size_custom(const CELTMode *mode, int channels); CELT_EXPORT int celt_decoder_get_size_custom(const CELTMode *mode, int channels);
/** Creates a new decoder state. Each stream needs its own decoder state (can't /** Creates a new decoder state. Each stream needs its own decoder state (can't
be shared across simultaneous streams). be shared across simultaneous streams).
@ -257,7 +257,7 @@ EXPORT int celt_decoder_get_size_custom(const CELTMode *mode, int channels);
@param error Returns an error code @param error Returns an error code
@return Newly created decoder state. @return Newly created decoder state.
*/ */
EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error); CELT_EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error);
/** Creates a new decoder state. Each stream needs its own decoder state (can't /** Creates a new decoder state. Each stream needs its own decoder state (can't
be shared across simultaneous streams). be shared across simultaneous streams).
@ -267,16 +267,16 @@ EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *er
@param error Returns an error code @param error Returns an error code
@return Newly created decoder state. @return Newly created decoder state.
*/ */
EXPORT CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error); CELT_EXPORT CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error);
EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error); CELT_EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error);
EXPORT CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error); CELT_EXPORT CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error);
/** Destroys a a decoder state. /** Destroys a a decoder state.
@param st Decoder state to be destroyed @param st Decoder state to be destroyed
*/ */
EXPORT void celt_decoder_destroy(CELTDecoder *st); CELT_EXPORT void celt_decoder_destroy(CELTDecoder *st);
/** Decodes a frame of audio. /** Decodes a frame of audio.
@param st Decoder state @param st Decoder state
@ -287,7 +287,7 @@ EXPORT void celt_decoder_destroy(CELTDecoder *st);
returned here in float format. returned here in float format.
@return Error code. @return Error code.
*/ */
EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size); CELT_EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size);
/** Decodes a frame of audio. /** Decodes a frame of audio.
@param st Decoder state @param st Decoder state
@ -298,7 +298,7 @@ EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len
returned here in 16-bit PCM format (native endian). returned here in 16-bit PCM format (native endian).
@return Error code. @return Error code.
*/ */
EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt_int16 *pcm, int frame_size); CELT_EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt_int16 *pcm, int frame_size);
/** Query and set decoder parameters /** Query and set decoder parameters
@param st Decoder state @param st Decoder state
@ -306,14 +306,14 @@ EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt
@param value Pointer to a 32-bit int value @param value Pointer to a 32-bit int value
@return Error code @return Error code
*/ */
EXPORT int celt_decoder_ctl(CELTDecoder * st, int request, ...); CELT_EXPORT int celt_decoder_ctl(CELTDecoder * st, int request, ...);
/** Returns the English string that corresponds to an error code /** Returns the English string that corresponds to an error code
* @param error Error code (negative for an error, 0 for success * @param error Error code (negative for an error, 0 for success
* @return Constant string (must NOT be freed) * @return Constant string (must NOT be freed)
*/ */
EXPORT const char *celt_strerror(int error); CELT_EXPORT const char *celt_strerror(int error);
/* @} */ /* @} */

View file

@ -38,14 +38,15 @@
#include "modes.h" #include "modes.h"
#include "SKP_Silk_SDK_API.h" #include "SKP_Silk_SDK_API.h"
/* Transition table for the voice mode */ /* Transition tables for the voice and audio modes. First column is the
middle (memoriless) threshold. The second column is the hysteresis
(difference with the middle) */
static const int voice_bandwidth_thresholds[10] = { static const int voice_bandwidth_thresholds[10] = {
11500, 1500, /* NB<->MB */ 11500, 1500, /* NB<->MB */
14500, 1500, /* MB<->WB */ 14500, 1500, /* MB<->WB */
21000, 2000, /* WB<->SWB */ 21000, 2000, /* WB<->SWB */
29000, 2000, /* SWB<->FB */ 29000, 2000, /* SWB<->FB */
}; };
/* Transition table for the audio mode */
static const int audio_bandwidth_thresholds[10] = { static const int audio_bandwidth_thresholds[10] = {
30000, 0, /* MB not allowed */ 30000, 0, /* MB not allowed */
20000, 2000, /* MB<->WB */ 20000, 2000, /* MB<->WB */
@ -63,11 +64,12 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
/* Create SILK encoder */ /* Create SILK encoder */
ret = SKP_Silk_SDK_Get_Encoder_Size( &silkEncSizeBytes ); ret = SKP_Silk_SDK_Get_Encoder_Size( &silkEncSizeBytes );
if( ret ) { if( ret )
/* Handle error */ return NULL;
}
celtEncSizeBytes = celt_encoder_get_size(channels); celtEncSizeBytes = celt_encoder_get_size(channels);
raw_state = calloc(sizeof(OpusEncoder)+silkEncSizeBytes+celtEncSizeBytes, 1); raw_state = calloc(sizeof(OpusEncoder)+silkEncSizeBytes+celtEncSizeBytes, 1);
if (raw_state == NULL)
return NULL;
st = (OpusEncoder*)raw_state; st = (OpusEncoder*)raw_state;
st->silk_enc = (void*)(raw_state+sizeof(OpusEncoder)); st->silk_enc = (void*)(raw_state+sizeof(OpusEncoder));
st->celt_enc = (CELTEncoder*)(raw_state+sizeof(OpusEncoder)+silkEncSizeBytes); st->celt_enc = (CELTEncoder*)(raw_state+sizeof(OpusEncoder)+silkEncSizeBytes);
@ -76,9 +78,8 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
st->Fs = Fs; st->Fs = Fs;
ret = SKP_Silk_SDK_InitEncoder( st->silk_enc, &st->silk_mode ); ret = SKP_Silk_SDK_InitEncoder( st->silk_enc, &st->silk_mode );
if( ret ) { if( ret )
/* Handle error */ goto failure;
}
/* default SILK parameters */ /* default SILK parameters */
st->silk_mode.API_sampleRate = st->Fs; st->silk_mode.API_sampleRate = st->Fs;
@ -94,6 +95,8 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
/* Create CELT encoder */ /* Create CELT encoder */
/* Initialize CELT encoder */ /* Initialize CELT encoder */
st->celt_enc = celt_encoder_init(st->celt_enc, Fs, channels, &err); st->celt_enc = celt_encoder_init(st->celt_enc, Fs, channels, &err);
if (err != CELT_OK)
goto failure;
celt_encoder_ctl(st->celt_enc, CELT_SET_SIGNALLING(0)); celt_encoder_ctl(st->celt_enc, CELT_SET_SIGNALLING(0));
st->mode = MODE_HYBRID; st->mode = MODE_HYBRID;
@ -110,6 +113,9 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
if (st->Fs > 16000) if (st->Fs > 16000)
st->delay_compensation += 10; st->delay_compensation += 10;
return st; return st;
failure:
free(st);
return NULL;
} }
int opus_encode(OpusEncoder *st, const short *pcm, int frame_size, int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
@ -120,7 +126,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
SKP_int32 nBytes; SKP_int32 nBytes;
ec_enc enc; ec_enc enc;
int framerate, period; int framerate, period;
int silk_internal_bandwidth; int silk_internal_bandwidth=-1;
int bytes_target; int bytes_target;
int prefill=0; int prefill=0;
int start_band = 0; int start_band = 0;