Improved error handling, and implemented celt_strerror()
This commit is contained in:
parent
80ed147663
commit
ece94a0475
4 changed files with 56 additions and 3 deletions
|
@ -125,7 +125,11 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error)
|
||||||
CELTEncoder *st;
|
CELTEncoder *st;
|
||||||
|
|
||||||
if (check_mode(mode) != CELT_OK)
|
if (check_mode(mode) != CELT_OK)
|
||||||
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_INVALID_MODE;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
if (channels < 0 || channels > 2)
|
if (channels < 0 || channels > 2)
|
||||||
{
|
{
|
||||||
celt_warning("Only mono and stereo supported");
|
celt_warning("Only mono and stereo supported");
|
||||||
|
@ -139,7 +143,11 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error)
|
||||||
st = celt_alloc(sizeof(CELTEncoder));
|
st = celt_alloc(sizeof(CELTEncoder));
|
||||||
|
|
||||||
if (st==NULL)
|
if (st==NULL)
|
||||||
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_ALLOC_FAIL;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
st->marker = ENCODERPARTIAL;
|
st->marker = ENCODERPARTIAL;
|
||||||
st->mode = mode;
|
st->mode = mode;
|
||||||
st->frame_size = N;
|
st->frame_size = N;
|
||||||
|
@ -175,11 +183,15 @@ CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error)
|
||||||
#endif
|
#endif
|
||||||
&& (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL))
|
&& (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL))
|
||||||
{
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_OK;
|
||||||
st->marker = ENCODERVALID;
|
st->marker = ENCODERVALID;
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
/* If the setup fails for some reason deallocate it. */
|
/* If the setup fails for some reason deallocate it. */
|
||||||
celt_encoder_destroy(st);
|
celt_encoder_destroy(st);
|
||||||
|
if (error)
|
||||||
|
*error = CELT_ALLOC_FAIL;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1046,7 +1058,11 @@ CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error)
|
||||||
CELTDecoder *st;
|
CELTDecoder *st;
|
||||||
|
|
||||||
if (check_mode(mode) != CELT_OK)
|
if (check_mode(mode) != CELT_OK)
|
||||||
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_INVALID_MODE;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
if (channels < 0 || channels > 2)
|
if (channels < 0 || channels > 2)
|
||||||
{
|
{
|
||||||
celt_warning("Only mono and stereo supported");
|
celt_warning("Only mono and stereo supported");
|
||||||
|
@ -1060,7 +1076,11 @@ CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error)
|
||||||
st = celt_alloc(sizeof(CELTDecoder));
|
st = celt_alloc(sizeof(CELTDecoder));
|
||||||
|
|
||||||
if (st==NULL)
|
if (st==NULL)
|
||||||
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_ALLOC_FAIL;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
st->marker = DECODERPARTIAL;
|
st->marker = DECODERPARTIAL;
|
||||||
st->mode = mode;
|
st->mode = mode;
|
||||||
|
@ -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) &&
|
if ((st->decode_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) &&
|
||||||
(st->preemph_memD!=NULL))
|
(st->preemph_memD!=NULL))
|
||||||
{
|
{
|
||||||
|
if (error)
|
||||||
|
*error = CELT_OK;
|
||||||
st->marker = DECODERVALID;
|
st->marker = DECODERVALID;
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
/* If the setup fails for some reason deallocate it. */
|
/* If the setup fails for some reason deallocate it. */
|
||||||
celt_decoder_destroy(st);
|
celt_decoder_destroy(st);
|
||||||
|
if (error)
|
||||||
|
*error = CELT_ALLOC_FAIL;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1441,3 +1465,22 @@ bad_request:
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
return CELT_UNIMPLEMENTED;
|
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
|
#define CELT_UNIMPLEMENTED -5
|
||||||
/** An encoder or decoder structure is invalid or already freed */
|
/** An encoder or decoder structure is invalid or already freed */
|
||||||
#define CELT_INVALID_STATE -6
|
#define CELT_INVALID_STATE -6
|
||||||
|
/** Memory allocation has failed */
|
||||||
|
#define CELT_ALLOC_FAIL -6
|
||||||
|
|
||||||
/* Requests */
|
/* Requests */
|
||||||
#define CELT_GET_MODE_REQUEST 1
|
#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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
#include "psy.h"
|
#include "psy.h"
|
||||||
#include "pitch.h"
|
#include "pitch.h"
|
||||||
|
|
||||||
#define CELT_BITSTREAM_VERSION 0x8000000a
|
#define CELT_BITSTREAM_VERSION 0x8000000b
|
||||||
|
|
||||||
#ifdef STATIC_MODES
|
#ifdef STATIC_MODES
|
||||||
#include "static_modes.h"
|
#include "static_modes.h"
|
||||||
|
|
|
@ -108,10 +108,16 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
enc = celt_encoder_create(mode, channels, &err);
|
enc = celt_encoder_create(mode, channels, &err);
|
||||||
if (err != 0)
|
if (err != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to create the encoder: %s\n", celt_strerror(err));
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
dec = celt_decoder_create(mode, channels, &err);
|
dec = celt_decoder_create(mode, channels, &err);
|
||||||
if (err != 0)
|
if (err != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to create the decoder: %s\n", celt_strerror(err));
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (argc>7)
|
if (argc>7)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue