+/*
+ * Copyright (C) 2004-2010 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
+
+#include <ogg/ogg.h>
+#include <regex.h>
+
+#include "para.h"
+#include "afh.h"
+#include "error.h"
+#include "string.h"
+#include "ogg_afh_common.h"
+
+
+/* Taken from decoder_example.c of libvorbis-1.2.3. */
+static int process_packets_2_and_3(ogg_sync_state *oss,
+ ogg_stream_state *stream, struct afh_info *afhi,
+ struct ogg_afh_callback_info *ci)
+{
+ ogg_page page;
+ ogg_packet packet;
+ int i = 0;
+
+ while (i < 2) {
+ while (i < 2) {
+ int ret = ogg_sync_pageout(oss, &page);
+ if (ret == 0)
+ break; /* Need more data */
+ if (ret != 1)
+ continue;
+ /*
+ * We can ignore any errors here as they'll also become
+ * apparent at packetout.
+ */
+ ogg_stream_pagein(stream, &page);
+ while (i < 2) {
+ ret = ogg_stream_packetout(stream, &packet);
+ if (ret == 0)
+ break;
+ if (ret < 0)
+ return -E_STREAM_PACKETOUT;
+ ret = ci->packet_callback(&packet, i + 1, afhi,
+ ci->private_data);
+ if (ret < 0)
+ return ret;
+ if (ret == 0) /* header complete */
+ return 1;
+ i++;
+ }
+ }
+ }
+ return 1;
+}
+
+static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
+ struct ogg_afh_callback_info *ci)
+{
+ ogg_packet packet;
+ ogg_stream_state stream;
+ ogg_page page;
+ int ret;
+
+ ret = -E_SYNC_PAGEOUT;
+ if (ogg_sync_pageout(oss, &page) != 1)
+ goto out;
+
+ ret = ogg_page_serialno(&page);
+ ogg_stream_init(&stream, ret);
+
+ ret = -E_STREAM_PAGEIN;
+ if (ogg_stream_pagein(&stream, &page) < 0)
+ goto out;
+
+ ret = -E_STREAM_PACKETOUT;
+ if (ogg_stream_packetout(&stream, &packet) != 1)
+ goto out;
+ ret = ci->packet_callback(&packet, 0, afhi, ci->private_data);
+ if (ret < 0)
+ goto out;
+ ret = process_packets_2_and_3(oss, &stream, afhi, ci);
+ if (ret < 0)
+ goto out;
+ afhi->header_offset = 0;
+ afhi->header_len = oss->returned;
+ ret = 1;
+out:
+ ogg_stream_clear(&stream);
+ return ret;
+}
+
+static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
+ struct timeval *result)
+{
+ uint64_t x = (uint64_t)num_frames * 1000 * 1000
+ / frequency / num_chunks;
+
+ result->tv_sec = x / 1000 / 1000;
+ result->tv_usec = x % (1000 * 1000);
+ PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
+ tv2ms(result));
+}
+
+/**
+ * Pass first three ogg packets to callback and build the chunk table.
+ *
+ * This function extracts the first three ogg packets of the audio data
+ * given by \a map and \a numbytes and passes each packet to the callback
+ * defined by \a ci.
+ *
+ * If the packet callback indicates success, the chunk table is built. Chunk
+ * zero contains the first three ogg packets while all other chunks consist of
+ * exactly one ogg page.
+ *
+ * \param map Audio file data.
+ * \param numbytes The length of \a map.
+ * \param afhi Passed to the packet callback, contains chunk table.
+ * \param ci The callback structure.
+ *
+ * \return Standard.
+ */
+int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
+ struct ogg_afh_callback_info *ci)
+{
+ ogg_sync_state oss;
+ ogg_page op;
+ long len = numbytes;
+ char *buf;
+ int ret, i, j, frames_per_chunk, ct_size;
+ long long unsigned num_frames = 0;
+
+ ogg_sync_init(&oss);
+ ret = -E_OGG_SYNC;
+ buf = ogg_sync_buffer(&oss, len);
+ if (!buf)
+ goto out;
+ memcpy(buf, map, len);
+ ret = -E_OGG_SYNC;
+ if (ogg_sync_wrote(&oss, len) < 0)
+ goto out;
+ ret = process_ogg_packets(&oss, afhi, ci);
+ if (ret < 0)
+ goto out;
+ oss.returned = 0;
+ oss.fill = numbytes;
+ /* count ogg packages and get duration of the file */
+ for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
+ num_frames = ogg_page_granulepos(&op);
+ PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
+ ret = -E_OGG_EMPTY;
+ if (i == 0)
+ goto out;
+ afhi->seconds_total = num_frames / afhi->frequency;
+ /* use roughly one page per chunk */
+ frames_per_chunk = num_frames / i;
+ PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
+ afhi->seconds_total, frames_per_chunk);
+ ct_size = 250;
+ afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
+ afhi->chunk_table[0] = 0;
+ afhi->chunk_table[1] = afhi->header_len;
+ oss.returned = afhi->header_len;
+ oss.fill = numbytes;
+ for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
+ int granule = ogg_page_granulepos(&op);
+
+ while (granule > j * frames_per_chunk) {
+ j++;
+ if (j >= ct_size) {
+ ct_size *= 2;
+ afhi->chunk_table = para_realloc(
+ afhi->chunk_table,
+ ct_size * sizeof(uint32_t));
+ }
+ afhi->chunk_table[j] = oss.returned;
+ }
+ }
+ afhi->chunks_total = j;
+ set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
+ ret = 0;
+out:
+ ogg_sync_clear(&oss);
+ return ret;
+}