Merge commit 'remotes/fml/master'
[paraslash.git] / oggdec.c
index d6c8f1121f0cf11f3289e4ba64cbf713f99950aa..3dced8a0baf58baf6617498c481668944e5ad183 100644 (file)
--- a/oggdec.c
+++ b/oggdec.c
@@ -1,19 +1,7 @@
 /*
- * 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 oggdec.c paraslash's ogg vorbis decoder */
 
 #include "oggdec_filter.cmdline.h"
 #include "list.h"
+#include "sched.h"
 #include "filter.h"
 #include "error.h"
 #include "string.h"
 
 #include <vorbis/vorbisfile.h>
 
-/** \cond some internal constants */
-#define BITS 16
+/** Determine byte sex. */
+#ifdef WORDS_BIGENDIAN
+#define ENDIAN 1
+#else
 #define ENDIAN 0
-#define SIGN 1
-/** \endcond */
+#endif
 
-/** data specific to the oggdec filter */
+/** Data specific to the oggdec filter. */
 struct private_oggdec_data {
-       /** describes an ogg vorbis file */
+       /** Describes an ogg vorbis file. */
        OggVorbis_File *vf;
-       /** the input buffer */
+       /** The input buffer. */
        char *inbuf;
-       /** the length of \a inbuf */
+       /** The length of \a inbuf. */
        size_t inbuf_len;
-       /** the number of bytes consumed from the input buffer */
+       /** The number of bytes consumed from the input buffer. */
        size_t converted;
 };
 
@@ -53,15 +43,15 @@ static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
        size_t ret, have = pod->inbuf_len - pod->converted;
        char *p = pod->inbuf + pod->converted;
 
-       if (*fn->fci->eof)
-               return 0;
 //     PARA_DEBUG_LOG("pod = %p\n", pod);
 //     PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
        if (pod->inbuf_len < size) {
+               if (*fn->fc->input_eof)
+                       return 0;
                errno = EAGAIN;
-               return -1;
+               return (size_t)-1;
        }
-       ret = MIN(nmemb, have / size) * size;
+       ret = PARA_MIN(nmemb, have / size) * size;
        memcpy(buf, p, ret);
        pod->converted += ret;
        return ret;
@@ -72,13 +62,13 @@ static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
  * Since we want the data source to be treated as unseekable at all
  * times, the provided seek callback always returns -1 (failure).
  */
-static int cb_seek(__unused void *datasource, __unused ogg_int64_t offset,
-               __unused int whence)
+static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
+               __a_unused int whence)
 {
        return -1;
 }
 
-static int cb_close(__unused void *datasource)
+static int cb_close(__a_unused void *datasource)
 {
        return 0;
 }
@@ -98,7 +88,7 @@ static void ogg_open(struct filter_node *fn)
 {
        struct private_oggdec_data *pod = para_calloc(
                sizeof(struct private_oggdec_data));
-       struct gengetopt_args_info *conf = fn->conf;
+       struct oggdec_filter_args_info *conf = fn->conf;
 
        fn->private_data = pod;
        fn->bufsize = conf->bufsize_arg * 1024;
@@ -125,7 +115,7 @@ static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
 {
        ssize_t ret;
        struct private_oggdec_data *pod = fn->private_data;
-       struct gengetopt_args_info *conf = fn->conf;
+       struct oggdec_filter_args_info *conf = fn->conf;
        /* make the buffer known to the read callback cb_read() */
        pod->inbuf = inbuffer;
        pod->inbuf_len = len;
@@ -133,9 +123,9 @@ static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
 
        if (!pod->vf) {
                int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
-               if (len <ib && !*fn->fci->eof && !fn->fci->error) {
-                       PARA_INFO_LOG("initial input buffer %zd/%d, waiting for more data\n",
-                               len, ib);
+               if (len <ib && !*fn->fc->input_eof) {
+                       PARA_DEBUG_LOG("initial input buffer %zd/%d, "
+                               "waiting for more data\n", len, ib);
                        return 0;
                }
                pod->vf = para_malloc(sizeof(struct OggVorbis_File));
@@ -154,34 +144,38 @@ static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
                        return -E_OGGDEC_BADHEADER;
                if (ret < 0)
                        return -E_OGGDEC_FAULT;
-               fn->fci->channels = ov_info(pod->vf, 0)->channels;
-               fn->fci->samplerate = ov_info(pod->vf, 0)->rate;
-               PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fci->channels, fn->fci->samplerate);
+               fn->fc->channels = ov_info(pod->vf, 0)->channels;
+               fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
+               PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
+                       fn->fc->samplerate);
        }
-again:
-       ret = ov_read(pod->vf, fn->buf + fn->loaded, fn->bufsize - fn->loaded,
-               ENDIAN, BITS / 8, SIGN, NULL);
-       if (ret == OV_HOLE || !ret) {
-               return pod->converted;
+       while (!*fn->fc->input_eof && fn->loaded < fn->bufsize) {
+               int length = fn->bufsize - fn->loaded;
+               long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
+                       ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
+               if (read_ret == OV_HOLE || !read_ret)
+                       return pod->converted;
+               if (read_ret < 0)
+                       return -E_OGGDEC_BADLINK;
+               fn->loaded += read_ret;
        }
-       if (ret < 0)
-               return -E_OGGDEC_BADLINK;
-       fn->loaded += ret;
-       if (!*fn->fci->eof && !fn->fci->error && fn->loaded < fn->bufsize)
-               goto again;
        return pod->converted;
 }
 
 static void *oggdec_parse_config(int argc, char **argv)
 {
-       struct gengetopt_args_info *ret = para_calloc(sizeof(struct gengetopt_args_info));
+       struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
        if (!oggdec_cmdline_parser(argc, argv, ret))
                return ret;
        free(ret);
        return NULL;
 }
 
-/** the init function of the ogg vorbis decoder */
+/**
+ * The init function of the ogg vorbis decoder.
+ *
+ * \param f Its fields are filled in by the function.
+ */
 void oggdec_init(struct filter *f)
 {
        f->open = ogg_open;