net.h: add missing __printf_2_3 attribute to send_va_buffer()
[paraslash.git] / mp3dec.c
index 764fc2665efee91301611b1a0f903f3ac1bd8090..5c6bab339e599152ccbba81d4873500fd8af7e3a 100644 (file)
--- a/mp3dec.c
+++ b/mp3dec.c
@@ -1,26 +1,14 @@
 /*
 /*
- * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
  *
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file mp3dec.c paraslash's mp3 decoder */
 
 #include "para.h"
  */
 
 /** \file mp3dec.c paraslash's mp3 decoder */
 
 #include "para.h"
-
 #include "list.h"
 #include "list.h"
+#include "sched.h"
 #include "filter.h"
 #include "error.h"
 #include <mad.h>
 #include "filter.h"
 #include "error.h"
 #include <mad.h>
@@ -30,7 +18,7 @@
 #define MP3_OUTBUF_SIZE 128 * 1024
 
 /** \cond a helper macro */
 #define MP3_OUTBUF_SIZE 128 * 1024
 
 /** \cond a helper macro */
-#define MAD_TO_SHORT(f) (f) >= MAD_F_ONE?  SHRT_MAX :\
+#define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
        (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
 /** \endcond */
 
        (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
 /** \endcond */
 
@@ -59,6 +47,11 @@ static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
        mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
        pmd->stream.error = 0;
 next_frame:
        mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
        pmd->stream.error = 0;
 next_frame:
+       ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
+       if (ret < 0)
+               goto out;
+       fn->fc->samplerate = pmd->frame.header.samplerate;
+       fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
        ret = mad_frame_decode(&pmd->frame, &pmd->stream);
        if (ret) {
                if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
        ret = mad_frame_decode(&pmd->frame, &pmd->stream);
        if (ret) {
                if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
@@ -67,18 +60,15 @@ next_frame:
                return -E_MAD_FRAME_DECODE;
        }
        mad_synth_frame(&pmd->synth, &pmd->frame);
                return -E_MAD_FRAME_DECODE;
        }
        mad_synth_frame(&pmd->synth, &pmd->frame);
-       fn->fci->samplerate = pmd->frame.header.samplerate;
-       fn->fci->channels = MAD_NCHANNELS(&pmd->frame.header);
 
        for (i = 0; i < pmd->synth.pcm.length; i++) {
 
        for (i = 0; i < pmd->synth.pcm.length; i++) {
-               /* output format: unsigned 16 bit little endian */
-               signed short s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
-               fn->buf[fn->loaded++] = s & 0xff;
-               fn->buf[fn->loaded++] = s >> 8;
+               int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
+               write_int16_host_endian(fn->buf + fn->loaded, s);
+               fn->loaded += 2;
                if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
                        s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
                if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
                        s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
-                       fn->buf[fn->loaded++] = s & 0xff;
-                       fn->buf[fn->loaded++] = s >> 8;
+                       write_int16_host_endian(fn->buf + fn->loaded, s);
+                       fn->loaded += 2;
                }
                if (fn->loaded != fn->bufsize) /* output buffer not full */
                        continue;
                }
                if (fn->loaded != fn->bufsize) /* output buffer not full */
                        continue;
@@ -90,8 +80,8 @@ next_frame:
 out:
        if (pmd->stream.next_frame) { /* we still have some data */
                size_t off = pmd->stream.bufend - pmd->stream.next_frame;
 out:
        if (pmd->stream.next_frame) { /* we still have some data */
                size_t off = pmd->stream.bufend - pmd->stream.next_frame;
-               PARA_DEBUG_LOG("converted %zd, %zd input bytes, %zd output bytes\n",
-                       len - off, off, fn->loaded);
+//             PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
+//                     fn->fc->samplerate, copy - off);
                return copy - off;
        }
        return copy;
                return copy - off;
        }
        return copy;
@@ -123,9 +113,12 @@ static void mp3dec_open(struct filter_node *fn)
        fn->bufsize = MP3_OUTBUF_SIZE;
        fn->buf = para_calloc(fn->bufsize);
 }
        fn->bufsize = MP3_OUTBUF_SIZE;
        fn->buf = para_calloc(fn->bufsize);
 }
+
 /**
  * the init function of the mp3dec filter
  *
 /**
  * the init function of the mp3dec filter
  *
+ * \param f pointer to the filter struct to initialize
+ *
  * \sa filter::init
  */
 void mp3dec_init(struct filter *f)
  * \sa filter::init
  */
 void mp3dec_init(struct filter *f)