Merge commit 'remotes/fml/master'
[paraslash.git] / ogg_afh.c
index cce1eb54f40c02d05e96419c1afd0e849c18c6a4..ba0b64be932e91b7e6c6f56e30058b2687e9496b 100644 (file)
--- a/ogg_afh.c
+++ b/ogg_afh.c
 /*
- * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2004-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 ogg_afh.c para_server's ogg vorbis audio format handler */
 
+#include <inttypes.h>
 #include <ogg/ogg.h>
 #include <vorbis/codec.h>
 #include <vorbis/vorbisfile.h>
 
-#include "server.cmdline.h"
-#include "server.h"
-#include "afs.h"
+#include "para.h"
 #include "afh.h"
 #include "error.h"
 #include "string.h"
+#include "afs.h"
+#include "server.h"
 
-/* must be big enough to hold header */
+/** must be big enough to hold header */
 #define CHUNK_SIZE 32768
 static double chunk_time = 0.25;
 
-static OggVorbis_File *oggvorbis_file;
-static FILE *infile;
-static int header_len, oggbuf_len, vi_channels;
-static char *header, *oggbuf;
-static ssize_t *chunk_table, max_chunk_len;
-static struct audio_format_handler *af;
-static long vi_sampling_rate, vi_bitrate, vi_bitrate_nominal,
-       num_chunks;
+/** describes a memory-mapped ogg vorbis file */
+struct ogg_datasource {
+       /** the memory mapping */
+       char *map;
+       /** this size of the mapping */
+       off_t numbytes;
+       /** the current position in the mapping */
+       off_t fpos;
+};
+
+static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
+{
+       struct ogg_datasource *ods = datasource;
+       size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
+               ret = copy / size;
+       if (!ret)
+               return 0;
+       memcpy(buf, ods->map + ods->fpos, copy);
+//     PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
+       ods->fpos += ret * size;
+       return ret;
+}
+
+static int cb_seek(void *datasource, ogg_int64_t offset,
+               int whence)
+{
+       struct ogg_datasource *ods = datasource;
+       switch (whence) {
+       case SEEK_SET:
+               if (offset >= 0 && offset <= ods->numbytes) {
+                       ods->fpos = offset;
+                       return 0;
+               }
+               errno = EINVAL;
+               return -1;
+               break;
+       case SEEK_END:
+               if (offset <= 0 && -offset <= ods->numbytes) {
+                       ods->fpos = ods->numbytes + offset;
+                       return 0;
+               }
+               errno = EINVAL;
+               return -1;
+               break;
+       case SEEK_CUR:
+               if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
+                               (offset < 0 && offset + ods->fpos < 0)) {
+                       errno = EINVAL;
+                       return -1;
+               }
+               ods->fpos += offset;
+               return 0;
+       }
+       errno = EINVAL;
+       return -1;
+}
+
+/* don't do anything as vss still needs the open filehandle */
+static int cb_close(__a_unused void *datasource)
+{
+       return 0;
+}
+
+static long cb_tell(void *datasource)
+{
+       struct ogg_datasource *ods = datasource;
+       return (unsigned long)ods->fpos;
+}
+
+static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
+{
+       int ret = ov_open_callbacks(datasource, vf,
+               NULL, /* no initial buffer */
+               0, /* no initial bytes */
+               c); /* the ov_open_callbacks */
+
+       if (ret == OV_EREAD)
+               return -E_OGG_READ;
+       if (ret == OV_ENOTVORBIS)
+               return -E_VORBIS;
+       if (ret == OV_EVERSION)
+               return -E_OGG_VERSION;
+       if (ret == OV_EBADHEADER)
+               return -E_OGG_BAD_HEADER;
+       if (ret < 0)
+               return -E_OGG_UNKNOWN_ERROR;
+       return 1;
 
-static int ogg_compute_header_len(void)
+}
+
+static int ogg_compute_header_len(char *map, size_t numbytes,
+               struct afh_info *afhi)
 {
-       int ret, len, in = fileno(infile);
-       unsigned int serial;
+       int ret;
+       size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
+       int serial;
        char *buf;
+
        ogg_page page;
        ogg_packet packet;
        vorbis_comment vc;
@@ -57,21 +130,21 @@ static int ogg_compute_header_len(void)
        ogg_sync_init(sync_in);
        vorbis_info_init(&vi);
        vorbis_comment_init(&vc);
-       buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
-       len = read(in, buf, CHUNK_SIZE);
-       ret = -E_OGG_READ;
-       if (len <= 0)
-               goto err1;
-       ogg_sync_wrote(sync_in, len);
+       buf = ogg_sync_buffer(sync_in, (long)len);
+       memcpy(buf, map, len);
+       ogg_sync_wrote(sync_in, (long)len);
        ret = -E_SYNC_PAGEOUT;
-       if (ogg_sync_pageout(sync_in, &page) <= 0)
+       if (ogg_sync_pageout(sync_in, &page) <= 0) {
+               free(stream_in);
+               free(stream_out);
                goto err1;
+       }
        serial = ogg_page_serialno(&page);
        ogg_stream_init(stream_in, serial);
        ogg_stream_init(stream_out, serial);
        ret = ogg_stream_pagein(stream_in, &page);
        if (ret < 0) {
-               ret = E_STREAM_PAGEIN;
+               ret = -E_STREAM_PAGEIN;
                goto err2;
        }
        ret = ogg_stream_packetout(stream_in, &packet);
@@ -80,11 +153,9 @@ static int ogg_compute_header_len(void)
                goto err2;
        }
        ret = -E_VORBIS;
-       if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0) {
+       if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
                goto err2;
-       } else
-               PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels,
-                       vi.rate);
+       PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
        ogg_stream_packetin(stream_out, &packet);
 
        ret = ogg_sync_pageout(sync_in, &page);
@@ -105,258 +176,121 @@ static int ogg_compute_header_len(void)
        ogg_stream_packetout(stream_in, &packet);
        ogg_stream_packetin(stream_out, &packet);
 
-       header_len = 0;
+       afhi->header_len = 0;
        while (ogg_stream_flush(stream_out, &page))
-               header_len += page.body_len + page.header_len;
-       ret = len;
-       PARA_INFO_LOG("header_len = %d\n", header_len);
+               afhi->header_len += page.body_len + page.header_len;
+       PARA_DEBUG_LOG("header_len = %d\n", afhi->header_len);
+       afhi->header_offset = 0;
+       ret = 1;
 err2:
        ogg_stream_destroy(stream_in);
        ogg_stream_destroy(stream_out);
 err1:
        ogg_sync_destroy(sync_in);
        vorbis_info_clear(&vi);
-        vorbis_comment_clear(&vc);
+       vorbis_comment_clear(&vc);
        return ret;
 }
 
-static void tunetable(void)
-{
-       int i = 1, j = -1, lp = 1;
-       while (i < num_chunks) {
-               if (chunk_table[i] == chunk_table[lp]) {
-                       i++;
-                       continue;
-               }
-               if (j < 0)
-                       tv_scale(i, &af->chunk_tv, &af->eof_tv);
-               for (j = lp; j < i; j++)
-                       chunk_table[j] = chunk_table[i];
-               lp = i;
-       }
-#if 1
-       for (i = 2; i < num_chunks; i++)
-               if (chunk_table[i] != chunk_table[1])
-                       break;
-       lp = i;
-       for (i = 2; i < num_chunks - lp; i++)
-               chunk_table[i] = chunk_table[i + lp];
-#endif 
-}
-
-
 /*
  * Alloc and fill array table of byte offsets. chunk_table[i] is the
- * offset in the current infile at which the sample containing time i *
- * CHUNK_TIME begins.
+ * offset in the current input file at which the sample containing time i *
+ * CHUNK_TIME begins. Always successful.
  */
-static void ogg_compute_chunk_table(double time_total)
+static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
+       struct afh_info *afhi, long unsigned time_total)
 {
        int i, ret, num;
-       ssize_t pos = 0, min = 0, old_pos;
+       ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
+       long unsigned num_chunks;
 
        old_pos = 0;
        ret = 0;
        num = time_total / chunk_time + 3;
        PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
                chunk_time, num);
-       chunk_table = para_malloc(num * sizeof(ogg_int64_t));
-       chunk_table[0] = 0;
+       afhi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
+       afhi->chunk_table[0] = 0;
        max_chunk_len = 0;
-       rewind(infile);
-       for (i = 1; ret == 0; i++) {
+       for (i = 1; ret <= num; i++) {
                ogg_int64_t diff;
-               ret = ov_time_seek(oggvorbis_file, i * chunk_time);
+               ret = ov_time_seek(of, i * chunk_time);
                if (ret)
                        break;
-               pos = ov_raw_tell(oggvorbis_file);
+               pos = ov_raw_tell(of);
                diff = pos - old_pos;
                max_chunk_len = PARA_MAX(max_chunk_len, diff);
                min = (i == 1)? diff : PARA_MIN(min, diff);
-               chunk_table[i] = pos;
-               if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
-                       PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
-                               "size: %zd\n", i - 1,
-                               i * chunk_time, pos, pos - old_pos);
+               afhi->chunk_table[i] = pos;
+//             if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
+//                     PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
+//                             "size: %zd\n", i - 1,
+//                             i * chunk_time, pos, pos - old_pos);
                old_pos = pos;
        }
        num_chunks = i - 1;
-       chunk_table[i] = pos;
-       tunetable();
-       PARA_INFO_LOG("%li chunks (%fs), max chunk: %zd, min chunk: %zd\n",
+//fi->chunk_table[i] = pos;
+       PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
                num_chunks, chunk_time, max_chunk_len, min);
-       rewind(infile);
-}
-
-static void ogg_close_audio_file(void)
-{
-       if (oggvorbis_file) {
-               PARA_DEBUG_LOG("%s", "ov_clear\n");
-               ov_clear(oggvorbis_file);
-               free(oggvorbis_file);
-               oggvorbis_file = NULL;
-       }
-       free(header);
-       header = NULL;
-       header_len = 0;
-       free(chunk_table);
-       chunk_table = NULL;
-       num_chunks = 0;
-       free(oggbuf);
-       oggbuf = NULL;
-       oggbuf_len = 0;
-}
-
-static int ogg_save_header(FILE *file, int len)
-{
-       int ret;
-
-       header = para_malloc(len);
-       rewind(file);
-       ret = read(fileno(file), header, len);
-       if (ret != len)
-               return -E_OGG_READ;
-       return 1;
+       return num_chunks;
 }
 
 /*
  * Init oggvorbis file and write some tech data to given pointers.
  */
-static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
-       int *seconds)
+static int ogg_get_file_info(char *map, size_t numbytes,
+               struct afh_info *afhi)
 {
        int ret;
-       double time_total;
        vorbis_info *vi;
-       ogg_int64_t raw_total;
+       OggVorbis_File of;
+       const ov_callbacks ovc = {
+               .read_func = cb_read,
+               .seek_func = cb_seek,
+               .close_func = cb_close,
+               .tell_func = cb_tell
+       };
+       struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
 
-       infile = file;
-       if (!file)
-               return -E_OGG_NO_FILE;
-       ret = ogg_compute_header_len();
+       ret = ogg_compute_header_len(map, numbytes, afhi);
        if (ret < 0)
                return ret;
-       ret = ogg_save_header(file, header_len);
+       ret = ogg_open_callbacks(&ods, &of, ovc);
        if (ret < 0)
-               return ret;
-       rewind(file);
-       oggvorbis_file = para_malloc(sizeof(OggVorbis_File));
-       ret = ov_open(file, oggvorbis_file, NULL, 0);
-       if (ret < 0) {
-               free(oggvorbis_file);
-               free(header);
-               return -E_OGG_OPEN;
-       }
+               goto err;
        ret = -E_OGG_INFO;
-       vi = ov_info(oggvorbis_file, 0);
+       vi = ov_info(&of, 0);
        if (!vi)
                goto err;
-       time_total = ov_time_total(oggvorbis_file, -1);
-       raw_total = ov_raw_total(oggvorbis_file, -1);
-       *seconds = time_total;
-       vi_sampling_rate = vi->rate;
-       vi_bitrate = ov_bitrate(oggvorbis_file, 0);
-       vi_bitrate_nominal = vi->bitrate_nominal;
-       vi_channels = vi->channels;
-       ogg_compute_chunk_table(time_total);
-       *frames = num_chunks;
-       sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
-               "audio_file_info2: \n"
-               "audio_file_info3: \n",
-               num_chunks, (long unsigned) (chunk_time * 1000 * 1000),
-               vi_sampling_rate / 1000, vi_channels, vi_bitrate / 1000
-               );
-       rewind(file);
-       return 1;
+       afhi->seconds_total = ov_time_total(&of, -1);
+       afhi->frequency = vi->rate;
+       afhi->bitrate = ov_bitrate(&of, 0) / 1000;
+       afhi->channels = vi->channels;
+       afhi->chunks_total = ogg_compute_chunk_table(&of, afhi, afhi->seconds_total);
+       afhi->info_string[0] = '\0';
+       sprintf(afhi->info_string, "%s:\n%s:\n%s:\n",
+               status_item_list[SI_AUDIO_FILE_INFO],
+               status_item_list[SI_TAGINFO1],
+               status_item_list[SI_TAGINFO2]
+       );
+       afhi->chunk_tv.tv_sec = 0;
+       afhi->chunk_tv.tv_usec = 250 * 1000;
+       tv_scale(10 / afhi->channels, &afhi->chunk_tv, &afhi->eof_tv);
+       ret = 1;
 err:
-       ogg_close_audio_file();
+       ov_clear(&of); /* keeps the file open */
        return ret;
 }
 
-/*
- * Simple stream reposition routine
- */
-static int ogg_reposition_stream(long unsigned request)
-{
-       int ret;
-       long seek;
-
-       seek = chunk_table[request];
-       ret = fseek(infile, seek, SEEK_SET);
-       PARA_DEBUG_LOG("seek to %li returned %d. offset:%li\n", seek,
-               ret, ftell(infile));
-       return ret < 0? -E_OGG_REPOS : 1;
-}
-
-static ogg_int64_t get_chunk_size(long unsigned chunk_num)
-{
-       ogg_int64_t ret;
-       if (chunk_num >= num_chunks)
-               return -1;
-       ret = chunk_table[chunk_num + 1] - chunk_table[chunk_num];
-       if (!ret)
-               return ret;
-#if 0
-       PARA_DEBUG_LOG("chunk %d: %lli-%lli (%lli bytes)\n",
-               chunk_num,
-               chunk_table[chunk_num],
-               chunk_table[chunk_num + 1] - 1,
-               ret);
-#endif
-       return ret;
-}
-
-char *ogg_read_chunk(long unsigned current_chunk, ssize_t *len)
-{
-       int ret;
-       ogg_int64_t cs = get_chunk_size(current_chunk);
-       if (!cs) { /* nothing to send for this run */
-               *len = 0;
-               return oggbuf;
-       }
-       if (cs < 0) { /* eof */
-               *len = 0;
-               return NULL;
-       }
-       *len = cs;
-       if (!oggbuf || oggbuf_len < *len) {
-               PARA_INFO_LOG("increasing ogg buffer size (%d -> %zu)\n",
-                       oggbuf_len, *len);
-               oggbuf = para_realloc(oggbuf, *len);
-               oggbuf_len = *len;
-       }
-       ret = read(fileno(infile), oggbuf, *len);
-       if (!ret) {
-               *len = 0;
-               return NULL;
-       }
-       if (ret < 0) {
-               *len = -E_OGG_READ;
-               return NULL;
-       }
-       if (ret != *len)
-               PARA_WARNING_LOG("short read (%d/%zd)\n", ret, *len);
-       *len = ret;
-       return oggbuf;
-}
-
-static char *ogg_get_header_info(int *len)
-{
-       *len = header_len;
-       return header;
-}
-
 static const char* ogg_suffixes[] = {"ogg", NULL};
-void ogg_init(struct audio_format_handler *p)
+
+/**
+ * the init function of the ogg vorbis audio format handler
+ *
+ * \param afh pointer to the struct to initialize
+ */
+void ogg_init(struct audio_format_handler *afh)
 {
-       af = p;
-       af->reposition_stream = ogg_reposition_stream;
-       af->get_file_info = ogg_get_file_info,
-       af->read_chunk = ogg_read_chunk;
-       af->close_audio_file = ogg_close_audio_file;
-       af->get_header_info = ogg_get_header_info;
-       af->chunk_tv.tv_sec = 0;
-       af->chunk_tv.tv_usec = 250 * 1000;
-       tv_scale(3, &af->chunk_tv, &af->eof_tv);
-       af->suffixes = ogg_suffixes;
+       afh->get_file_info = ogg_get_file_info,
+       afh->suffixes = ogg_suffixes;
 }