diff options
author | Jean-Marc Valin <jean-marc.valin@usherbrooke.ca> | 2009-10-16 15:30:14 +0400 |
---|---|---|
committer | Jean-Marc Valin <jean-marc.valin@usherbrooke.ca> | 2009-10-16 15:31:28 +0400 |
commit | ece94a04759369097bdbec44c282d1beffe3b026 (patch) | |
tree | 11c683186fa31b1fc21b3d18362e91fbac1ada76 | |
parent | 80ed1476636e8fd342346bda19e6cab66ff3f3d0 (diff) |
Improved error handling, and implemented celt_strerror()
-rw-r--r-- | libcelt/celt.c | 47 | ||||
-rw-r--r-- | libcelt/celt.h | 4 | ||||
-rw-r--r-- | libcelt/modes.h | 2 | ||||
-rw-r--r-- | libcelt/testcelt.c | 6 |
4 files changed, 56 insertions, 3 deletions
diff --git a/libcelt/celt.c b/libcelt/celt.c index 395da31..af99728 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -125,7 +125,11 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) CELTEncoder *st; if (check_mode(mode) != CELT_OK) + { + if (error) + *error = CELT_INVALID_MODE; return NULL; + } if (channels < 0 || channels > 2) { celt_warning("Only mono and stereo supported"); @@ -138,8 +142,12 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) C = channels; st = celt_alloc(sizeof(CELTEncoder)); - if (st==NULL) + if (st==NULL) + { + if (error) + *error = CELT_ALLOC_FAIL; return NULL; + } st->marker = ENCODERPARTIAL; st->mode = mode; st->frame_size = N; @@ -175,11 +183,15 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) #endif && (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL)) { + if (error) + *error = CELT_OK; st->marker = ENCODERVALID; return st; } /* If the setup fails for some reason deallocate it. */ celt_encoder_destroy(st); + if (error) + *error = CELT_ALLOC_FAIL; return NULL; } @@ -1046,7 +1058,11 @@ CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) CELTDecoder *st; if (check_mode(mode) != CELT_OK) + { + if (error) + *error = CELT_INVALID_MODE; return NULL; + } if (channels < 0 || channels > 2) { celt_warning("Only mono and stereo supported"); @@ -1060,8 +1076,12 @@ CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) st = celt_alloc(sizeof(CELTDecoder)); if (st==NULL) + { + if (error) + *error = CELT_ALLOC_FAIL; return NULL; - + } + st->marker = DECODERPARTIAL; st->mode = mode; st->frame_size = N; @@ -1081,11 +1101,15 @@ CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) if ((st->decode_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) && (st->preemph_memD!=NULL)) { + if (error) + *error = CELT_OK; st->marker = DECODERVALID; return st; } /* If the setup fails for some reason deallocate it. */ celt_decoder_destroy(st); + if (error) + *error = CELT_ALLOC_FAIL; return NULL; } @@ -1441,3 +1465,22 @@ bad_request: va_end(ap); return CELT_UNIMPLEMENTED; } + +const char *celt_strerror(int error) +{ + static const char *error_strings[8] = { + "success", + "invalid argument", + "invalid mode", + "internal error", + "corrupted stream", + "request not implemented", + "invalid state", + "memory allocation failed" + }; + if (error > 0 || error < -7) + return "unknown error"; + else + return error_strings[-error]; +} + diff --git a/libcelt/celt.h b/libcelt/celt.h index a0d7d60..6076ce3 100644 --- a/libcelt/celt.h +++ b/libcelt/celt.h @@ -69,6 +69,8 @@ extern "C" { #define CELT_UNIMPLEMENTED -5 /** An encoder or decoder structure is invalid or already freed */ #define CELT_INVALID_STATE -6 +/** Memory allocation has failed */ +#define CELT_ALLOC_FAIL -6 /* Requests */ #define CELT_GET_MODE_REQUEST 1 @@ -265,6 +267,8 @@ EXPORT int celt_decoder_ctl(CELTDecoder * st, int request, ...); /* @} */ +const char *celt_strerror(int error); + #ifdef __cplusplus } diff --git a/libcelt/modes.h b/libcelt/modes.h index 3a30c1a..173a389 100644 --- a/libcelt/modes.h +++ b/libcelt/modes.h @@ -39,7 +39,7 @@ #include "psy.h" #include "pitch.h" -#define CELT_BITSTREAM_VERSION 0x8000000a +#define CELT_BITSTREAM_VERSION 0x8000000b #ifdef STATIC_MODES #include "static_modes.h" diff --git a/libcelt/testcelt.c b/libcelt/testcelt.c index 88d0608..9e6c2d1 100644 --- a/libcelt/testcelt.c +++ b/libcelt/testcelt.c @@ -108,10 +108,16 @@ int main(int argc, char *argv[]) enc = celt_encoder_create(mode, channels, &err); if (err != 0) + { + fprintf(stderr, "Failed to create the encoder: %s\n", celt_strerror(err)); return 1; + } dec = celt_decoder_create(mode, channels, &err); if (err != 0) + { + fprintf(stderr, "Failed to create the decoder: %s\n", celt_strerror(err)); return 1; + } if (argc>7) { |