paraslash 0.7.3
[paraslash.git] / spxdec_filter.c
index 4c7a773b51f4d7491c5304a325b09dfdb29cdccd..08eac02a557b2d503646666edd7ffb35e1c5c5f7 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Copyright (C) 2002-2006 Jean-Marc Valin
- * Copyright (C) 2010-2014 Andre Noll <maan@tuebingen.mpg.de>
+ * Copyright (C) 2010 Andre Noll <maan@tuebingen.mpg.de>
  *
- * Licensed under the GPL v2. For licencing details see COPYING.
+ * Licensed under the GPL v2, see file COPYING.
  */
 
 /** \file spxdec_filter.c Paraslash's ogg/speex decoder. */
@@ -48,9 +48,9 @@
 #include <speex/speex_callbacks.h>
 
 #include "para.h"
+#include "portable_io.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
@@ -75,13 +75,13 @@ struct private_spxdec_data {
        int lookahead;
        /** The state information about the current stream. */
        ogg_stream_state os;
-       /** Whether \a os initialized. */
+       /** Whether \a os is initialized. */
        bool stream_init;
 };
 
 static void spxdec_open(struct filter_node *fn)
 {
-       struct private_spxdec_data *psd = para_calloc(sizeof(*psd));
+       struct private_spxdec_data *psd = zalloc(sizeof(*psd));
 
        fn->private_data = psd;
        fn->min_iqs = 200;
@@ -122,13 +122,14 @@ static int speexdec_init(struct filter_node *fn)
        return 1;
 }
 
-#if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
-#define le_short(s) ((short) ((unsigned short) (s) << 8) | ((unsigned short) (s) >> 8))
-#else
-#define le_short(s) ((short) (s))
-#endif
-
+/**
+ * Size of the output buffer.
+ *
+ * Valid streams have frame sizes in the range from 160 to 640. To avoid buffer
+ * overflows, we bail out if the decoder reports a value bigger than this.
+ */
 #define MAX_FRAME_SIZE 2000
+
 /* Copy Ogg packet to Speex bitstream */
 static int speexdec_write_frames(int packet_no,
                struct private_spxdec_data *psd, int skip_samples,
@@ -139,7 +140,14 @@ static int speexdec_write_frames(int packet_no,
        for (j = 0; j != psd->shi.nframes; j++) {
                short output[MAX_FRAME_SIZE], *btr_output;
                int skip = skip_samples + psd->lookahead, skip_idx = 0;
-               int samples, new_frame_size = psd->shi.frame_size;
+               int samples, this_frame_size,
+                       new_frame_size = psd->shi.frame_size;
+
+               if (speex_decoder_ctl(psd->shi.state, SPEEX_GET_FRAME_SIZE,
+                               &this_frame_size) == 0) {
+                       if (this_frame_size > MAX_FRAME_SIZE)
+                               return -E_SPX_DECODE_OVERFLOW;
+               };
 
                if (speex_decode_int(psd->shi.state, &psd->bits, output) < 0)
                        return -E_SPX_DECODE;
@@ -163,9 +171,9 @@ static int speexdec_write_frames(int packet_no,
                if (new_frame_size <= 0)
                        continue;
                samples = new_frame_size * psd->shi.channels;
-               btr_output = para_malloc(2 * samples);
+               btr_output = arr_alloc(samples, 2);
                for (i = 0; i < samples; i++)
-                       btr_output[i] = le_short(output[i + skip_idx]);
+                       btr_output[i] = read_u16(output + i + skip_idx);
                btr_add_output((char *)btr_output, samples * 2, btrn);
        }
        return 1;
@@ -238,7 +246,7 @@ static int compute_skip_samples(ogg_page *og, struct private_spxdec_data *psd)
        return ret;
 }
 
-static int speexdec_post_select(__a_unused struct sched *s, void *context)
+static int speexdec_post_monitor(__a_unused struct sched *s, void *context)
 {
        struct filter_node *fn = context;
        struct private_spxdec_data *psd = fn->private_data;
@@ -294,16 +302,10 @@ fail:
        return ret;
 }
 
-/**
- * The init function of the ogg/speex decoder.
- *
- * \param f Its fields are filled in by the function.
- */
-void spxdec_filter_init(struct filter *f)
-{
-       f->open = spxdec_open;
-       f->close = speexdec_close;
-       f->pre_select = generic_filter_pre_select;
-       f->post_select = speexdec_post_select;
-       f->execute = speexdec_execute;
-}
+const struct filter lsg_filter_cmd_com_spxdec_user_data = {
+       .open = spxdec_open,
+       .close = speexdec_close,
+       .pre_monitor = generic_filter_pre_monitor,
+       .post_monitor = speexdec_post_monitor,
+       .execute = speexdec_execute,
+};