ref: ece94a04759369097bdbec44c282d1beffe3b026
parent: 80ed1476636e8fd342346bda19e6cab66ff3f3d0
author: Jean-Marc Valin <[email protected]>
date: Fri Oct 16 03:30:14 EDT 2009
Improved error handling, and implemented celt_strerror()
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -125,7 +125,11 @@
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 @@
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 @@
#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 *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 @@
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 @@
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 @@
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];
+}
+
--- a/libcelt/celt.h
+++ b/libcelt/celt.h
@@ -69,6 +69,8 @@
#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
@@ -264,6 +266,8 @@
/* @} */
+
+const char *celt_strerror(int error);
#ifdef __cplusplus
--- 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"
--- a/libcelt/testcelt.c
+++ b/libcelt/testcelt.c
@@ -108,10 +108,16 @@
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)
{