Commit ece94a04 authored by Jean-Marc Valin's avatar Jean-Marc Valin
Browse files

Improved error handling, and implemented celt_strerror()

parent 80ed1476
......@@ -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];
}
......@@ -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
}
......
......@@ -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"
......
......@@ -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)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment