[xiph-cvs] cvs commit: speex/src speexdec.c
Jean-Marc Valin
jm at xiph.org
Wed Jan 8 13:59:00 PST 2003
jm 03/01/08 16:59:00
Modified: libspeex bits.c misc.c misc.h modes.c nb_celp.c nb_celp.h
sb_celp.c sb_celp.h speex.h speex_bits.h
src speexdec.c
Log:
Added a return value (error) to the *ctl functions, added re-allocation
to SpeexBits when buffer is too small.
Revision Changes Path
1.24 +25 -6 speex/libspeex/bits.c
Index: bits.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/bits.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- bits.c 7 Jan 2003 04:11:04 -0000 1.23
+++ bits.c 8 Jan 2003 21:58:59 -0000 1.24
@@ -39,8 +39,9 @@
{
int i;
bits->bytes = (char*)speex_alloc(MAX_BYTES_PER_FRAME);
+ bits->buf_size = MAX_BYTES_PER_FRAME;
- for (i=0;i<MAX_BYTES_PER_FRAME;i++)
+ for (i=0;i<bits->buf_size;i++)
bits->bytes[i]=0;
bits->nbBits=0;
bits->bytePtr=0;
@@ -49,12 +50,13 @@
bits->overflow=0;
}
-void speex_bits_init_buffer(SpeexBits *bits, void *buff)
+void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size)
{
int i;
bits->bytes = (char*)buff;
+ bits->buf_size = buf_size;
- for (i=0;i<MAX_BYTES_PER_FRAME;i++)
+ for (i=0;i<buf_size;i++)
bits->bytes[i]=0;
bits->nbBits=0;
bits->bytePtr=0;
@@ -73,7 +75,7 @@
void speex_bits_reset(SpeexBits *bits)
{
int i;
- for (i=0;i<MAX_BYTES_PER_FRAME;i++)
+ for (i=0;i<bits->buf_size;i++)
bits->bytes[i]=0;
bits->nbBits=0;
bits->bytePtr=0;
@@ -91,9 +93,24 @@
void speex_bits_read_from(SpeexBits *bits, char *bytes, int len)
{
int i;
- if (len > MAX_BYTES_PER_FRAME)
+ if (len > bits->buf_size)
{
- speex_error ("Trying to init frame with too many bits");
+ speex_warning_int("Packet if larger than allocated buffer: ", len);
+ if (bits->owner)
+ {
+ char *tmp = speex_realloc(bits->bytes, len);
+ if (tmp)
+ {
+ bits->buf_size=len;
+ bits->bytes=tmp;
+ } else {
+ len=bits->buf_size;
+ speex_warning("Could not resize input buffer: truncating input");
+ }
+ } else {
+ speex_warning("Do not own input buffer: truncating input");
+ len=bits->buf_size;
+ }
}
for (i=0;i<len;i++)
bits->bytes[i]=bytes[i];
@@ -118,6 +135,7 @@
void speex_bits_read_whole_bytes(SpeexBits *bits, char *bytes, int len)
{
int i,pos;
+ /*FIXME: check for overflow*/
speex_bits_flush(bits);
pos=bits->nbBits>>3;
for (i=0;i<len;i++)
@@ -158,6 +176,7 @@
void speex_bits_pack(SpeexBits *bits, int data, int nbBits)
{
unsigned int d=data;
+ /*FIXME: check for overflow*/
while(nbBits)
{
int bit;
<p><p>1.8 +5 -0 speex/libspeex/misc.c
Index: misc.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/misc.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- misc.c 7 Jan 2003 04:30:25 -0000 1.7
+++ misc.c 8 Jan 2003 21:58:59 -0000 1.8
@@ -95,6 +95,11 @@
return calloc(size,1);
}
+void *speex_realloc (void *ptr, int size)
+{
+ return realloc(ptr, size);
+}
+
void speex_free (void *ptr)
{
free(ptr);
<p><p>1.14 +5 -2 speex/libspeex/misc.h
Index: misc.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/misc.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- misc.h 7 Jan 2003 04:30:25 -0000 1.13
+++ misc.h 8 Jan 2003 21:58:59 -0000 1.14
@@ -50,10 +50,13 @@
unsigned short be_short(unsigned short s);
unsigned short le_short(unsigned short s);
-/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function and speex_free */
+/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_free */
void *speex_alloc (int size);
-/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function and speex_alloc */
+/** Speex wrapper for realloc. To do your own dynamic allocation, all you need to do is replace this function, speex_alloc and speex_free */
+void *speex_realloc (void *ptr, int size);
+
+/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_alloc */
void speex_free (void *ptr);
/** Speex wrapper for mem_move */
<p><p>1.96 +14 -11 speex/libspeex/modes.c
Index: modes.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/modes.c,v
retrieving revision 1.95
retrieving revision 1.96
diff -u -r1.95 -r1.96
--- modes.c 6 Jan 2003 20:43:48 -0000 1.95
+++ modes.c 8 Jan 2003 21:58:59 -0000 1.96
@@ -58,8 +58,8 @@
extern float exc_10_16_table[];
extern float hexc_10_32_table[];
-static void nb_mode_query(void *mode, int request, void *ptr);
-static void wb_mode_query(void *mode, int request, void *ptr);
+static int nb_mode_query(void *mode, int request, void *ptr);
+static int wb_mode_query(void *mode, int request, void *ptr);
/* Parameters for Long-Term Prediction (LTP)*/
static ltp_params ltp_params_nb = {
@@ -549,19 +549,19 @@
}
-void speex_encoder_ctl(void *state, int request, void *ptr)
+int speex_encoder_ctl(void *state, int request, void *ptr)
{
- (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
+ return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
}
-void speex_decoder_ctl(void *state, int request, void *ptr)
+int speex_decoder_ctl(void *state, int request, void *ptr)
{
- (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
+ return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
}
-static void nb_mode_query(void *mode, int request, void *ptr)
+static int nb_mode_query(void *mode, int request, void *ptr)
{
SpeexNBMode *m = (SpeexNBMode*)mode;
@@ -580,11 +580,12 @@
break;
default:
speex_warning_int("Unknown nb_mode_query request: ", request);
+ return -1;
}
-
+ return 0;
}
-static void wb_mode_query(void *mode, int request, void *ptr)
+static int wb_mode_query(void *mode, int request, void *ptr)
{
SpeexSBMode *m = (SpeexSBMode*)mode;
@@ -603,11 +604,13 @@
break;
default:
speex_warning_int("Unknown wb_mode_query request: ", request);
+ return -1;
}
+ return 0;
}
-void speex_mode_query(SpeexMode *mode, int request, void *ptr)
+int speex_mode_query(SpeexMode *mode, int request, void *ptr)
{
- mode->query(mode->mode, request, ptr);
+ return mode->query(mode->mode, request, ptr);
}
<p><p>1.109 +7 -2 speex/libspeex/nb_celp.c
Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.108
retrieving revision 1.109
diff -u -r1.108 -r1.109
--- nb_celp.c 8 Jan 2003 06:57:01 -0000 1.108
+++ nb_celp.c 8 Jan 2003 21:58:59 -0000 1.109
@@ -1086,6 +1086,7 @@
return ret;
} else if (m>7) /* Invalid mode */
{
+ speex_warning("Invalid mode encountered: corrupted stream?");
return -2;
}
@@ -1458,7 +1459,7 @@
return 0;
}
-void nb_encoder_ctl(void *state, int request, void *ptr)
+int nb_encoder_ctl(void *state, int request, void *ptr)
{
EncState *st;
st=(EncState*)state;
@@ -1613,10 +1614,12 @@
break;
default:
speex_warning_int("Unknown nb_ctl request: ", request);
+ return -1;
}
+ return 0;
}
-void nb_decoder_ctl(void *state, int request, void *ptr)
+int nb_decoder_ctl(void *state, int request, void *ptr)
{
DecState *st;
st=(DecState*)state;
@@ -1701,5 +1704,7 @@
break;
default:
speex_warning_int("Unknown nb_ctl request: ", request);
+ return -1;
}
+ return 0;
}
<p><p>1.47 +2 -2 speex/libspeex/nb_celp.h
Index: nb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.h,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -r1.46 -r1.47
--- nb_celp.h 6 Jan 2003 20:43:48 -0000 1.46
+++ nb_celp.h 8 Jan 2003 21:58:59 -0000 1.47
@@ -193,10 +193,10 @@
int nb_decode(void *state, SpeexBits *bits, float *out);
/** ioctl-like function for controlling a narrowband encoder */
-void nb_encoder_ctl(void *state, int request, void *ptr);
+int nb_encoder_ctl(void *state, int request, void *ptr);
/** ioctl-like function for controlling a narrowband decoder */
-void nb_decoder_ctl(void *state, int request, void *ptr);
+int nb_decoder_ctl(void *state, int request, void *ptr);
#endif
<p><p>1.115 +11 -5 speex/libspeex/sb_celp.c
Index: sb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/sb_celp.c,v
retrieving revision 1.114
retrieving revision 1.115
diff -u -r1.114 -r1.115
--- sb_celp.c 8 Jan 2003 06:57:01 -0000 1.114
+++ sb_celp.c 8 Jan 2003 21:58:59 -0000 1.115
@@ -844,7 +844,11 @@
/*Was a narrowband frame, set "null submode"*/
st->submodeID = 0;
}
- /*FIXME: Check for valid submodeID */
+ if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
+ {
+ speex_warning("Invalid mode encountered: corrupted stream?");
+ return -2;
+ }
/* If null mode (no transmission), just set a couple things to zero*/
if (st->submodes[st->submodeID] == NULL)
@@ -1043,7 +1047,7 @@
}
-void sb_encoder_ctl(void *state, int request, void *ptr)
+int sb_encoder_ctl(void *state, int request, void *ptr)
{
SBEncState *st;
st=(SBEncState*)state;
@@ -1230,11 +1234,12 @@
break;
default:
speex_warning_int("Unknown nb_ctl request: ", request);
+ return -1;
}
-
+ return 0;
}
-void sb_decoder_ctl(void *state, int request, void *ptr)
+int sb_decoder_ctl(void *state, int request, void *ptr)
{
SBDecState *st;
st=(SBDecState*)state;
@@ -1316,6 +1321,7 @@
break;
default:
speex_warning_int("Unknown nb_ctl request: ", request);
+ return -1;
}
-
+ return 0;
}
<p><p>1.38 +2 -2 speex/libspeex/sb_celp.h
Index: sb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/sb_celp.h,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- sb_celp.h 6 Jan 2003 08:35:48 -0000 1.37
+++ sb_celp.h 8 Jan 2003 21:58:59 -0000 1.38
@@ -160,8 +160,8 @@
/**Decodes one frame*/
int sb_decode(void *state, SpeexBits *bits, float *out);
-void sb_encoder_ctl(void *state, int request, void *ptr);
+int sb_encoder_ctl(void *state, int request, void *ptr);
-void sb_decoder_ctl(void *state, int request, void *ptr);
+int sb_decoder_ctl(void *state, int request, void *ptr);
#endif
<p><p>1.74 +6 -6 speex/libspeex/speex.h
Index: speex.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/speex.h,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -r1.73 -r1.74
--- speex.h 8 Jan 2003 06:57:01 -0000 1.73
+++ speex.h 8 Jan 2003 21:58:59 -0000 1.74
@@ -171,7 +171,7 @@
typedef int (*encode_func)(void *state, float *in, SpeexBits *bits);
/** Function for controlling the encoder options */
-typedef void (*encoder_ctl_func)(void *state, int request, void *ptr);
+typedef int (*encoder_ctl_func)(void *state, int request, void *ptr);
/** Decoder state initialization function */
typedef void *(*decoder_init_func)(struct SpeexMode *mode);
@@ -183,11 +183,11 @@
typedef int (*decode_func)(void *state, SpeexBits *bits, float *out);
/** Function for controlling the decoder options */
-typedef void (*decoder_ctl_func)(void *state, int request, void *ptr);
+typedef int (*decoder_ctl_func)(void *state, int request, void *ptr);
/** Query function for a mode */
-typedef void (*mode_query_func)(void *mode, int request, void *ptr);
+typedef int (*mode_query_func)(void *mode, int request, void *ptr);
/** Struct defining a Speex mode */
typedef struct SpeexMode {
@@ -263,7 +263,7 @@
* @param ptr Data exchanged to-from function
* @return 0 if frame needs not be transmitted (DTX only), 1 otherwise
*/
-void speex_encoder_ctl(void *state, int request, void *ptr);
+int speex_encoder_ctl(void *state, int request, void *ptr);
/** Returns a handle to a newly created decoder state structure. For now,
@@ -299,7 +299,7 @@
* @param ptr Data exchanged to-from function
* @return 0 for no error, 1 if a terminator is reached, 2 for another error
*/
-void speex_decoder_ctl(void *state, int request, void *ptr);
+int speex_decoder_ctl(void *state, int request, void *ptr);
/** Query function for mode information
@@ -308,7 +308,7 @@
* @param request ioctl-type request (one of the SPEEX_* macros)
* @param ptr Data exchanged to-from function
*/
-void speex_mode_query(SpeexMode *mode, int request, void *ptr);
+int speex_mode_query(SpeexMode *mode, int request, void *ptr);
/** Default narrowband mode */
<p><p>1.18 +2 -1 speex/libspeex/speex_bits.h
Index: speex_bits.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/speex_bits.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- speex_bits.h 7 Jan 2003 04:11:04 -0000 1.17
+++ speex_bits.h 8 Jan 2003 21:58:59 -0000 1.18
@@ -51,13 +51,14 @@
int bitPtr; /**< Position of the bit "cursor" within the current byte */
int owner; /**< Does the struct "own" the "raw" buffer (member "bytes") */
int overflow;/**< Set to one if we try to read past the valid data */
+ int buf_size;/**< Allocated size for buffer */
} SpeexBits;
/** Initializes and allocates resources for a SpeexBits struct */
void speex_bits_init(SpeexBits *bits);
/** Initializes SpeexBits struct using a pre-allocated buffer*/
-void speex_bits_init_buffer(SpeexBits *bits, void *buff);
+void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size);
/** Frees all resources assiociated to a SpeexBits struct. Right now this does nothing since no resources are allocated, but this could change in the future.*/
void speex_bits_destroy(SpeexBits *bits);
<p><p>1.70 +8 -3 speex/src/speexdec.c
Index: speexdec.c
===================================================================
RCS file: /usr/local/cvsroot/speex/src/speexdec.c,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- speexdec.c 7 Jan 2003 04:11:04 -0000 1.69
+++ speexdec.c 8 Jan 2003 21:59:00 -0000 1.70
@@ -272,7 +272,7 @@
}
if (header->mode >= SPEEX_NB_MODES)
{
- fprintf (stderr, "Mode number %d does not (any longer) exist in this version\n",
+ fprintf (stderr, "Mode number %d does not (yet/any longer) exist in this version\n",
header->mode);
return NULL;
}
@@ -325,10 +325,15 @@
fprintf (stderr, "Decoding %d Hz audio using %s mode",
*rate, mode->modeName);
+ if (*channels==1)
+ fprintf (stderr, " (mono");
+ else
+ fprintf (stderr, " (stereo");
+
if (header->vbr)
- fprintf (stderr, " (VBR)\n");
+ fprintf (stderr, ", VBR)\n");
else
- fprintf(stderr, "\n");
+ fprintf(stderr, ")\n");
/*fprintf (stderr, "Decoding %d Hz audio at %d bps using %s mode\n",
*rate, mode->bitrate, mode->modeName);*/
<p><p>--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list