the paraslash-0.4.4 release tarball
[paraslash.git] / ogg_afh_common.c
1 /*
2  * Copyright (C) 2004-2010 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
8
9 #include <ogg/ogg.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "string.h"
16 #include "ogg_afh_common.h"
17
18
19 /* Taken from decoder_example.c of libvorbis-1.2.3. */
20 static int process_packets_2_and_3(ogg_sync_state *oss,
21                 ogg_stream_state *stream, struct afh_info *afhi,
22                 struct ogg_afh_callback_info *ci)
23 {
24         ogg_page page;
25         ogg_packet packet;
26         int i = 0;
27
28         while (i < 2) {
29                 while (i < 2) {
30                         int ret = ogg_sync_pageout(oss, &page);
31                         if (ret == 0)
32                                 break; /* Need more data */
33                         if (ret != 1)
34                                 continue;
35                         /*
36                          * We can ignore any errors here as they'll also become
37                          * apparent at packetout.
38                          */
39                         ogg_stream_pagein(stream, &page);
40                         while (i < 2) {
41                                 ret = ogg_stream_packetout(stream, &packet);
42                                 if (ret == 0)
43                                         break;
44                                 if (ret < 0)
45                                         return -E_STREAM_PACKETOUT;
46                                 ret = ci->packet_callback(&packet, i + 1, afhi,
47                                         ci->private_data);
48                                 if (ret < 0)
49                                         return ret;
50                                 if (ret == 0) /* header complete */
51                                         return 1;
52                                 i++;
53                         }
54                 }
55         }
56         return 1;
57 }
58
59 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
60                 struct ogg_afh_callback_info *ci)
61 {
62         ogg_packet packet;
63         ogg_stream_state stream;
64         ogg_page page;
65         int ret;
66
67         ret = -E_SYNC_PAGEOUT;
68         if (ogg_sync_pageout(oss, &page) != 1)
69                 goto out;
70
71         ret = ogg_page_serialno(&page);
72         ogg_stream_init(&stream, ret);
73
74         ret = -E_STREAM_PAGEIN;
75         if (ogg_stream_pagein(&stream, &page) < 0)
76                 goto out;
77
78         ret = -E_STREAM_PACKETOUT;
79         if (ogg_stream_packetout(&stream, &packet) != 1)
80                 goto out;
81         ret = ci->packet_callback(&packet, 0, afhi, ci->private_data);
82         if (ret < 0)
83                 goto out;
84         ret = process_packets_2_and_3(oss, &stream, afhi, ci);
85         if (ret < 0)
86                 goto out;
87         afhi->header_offset = 0;
88         afhi->header_len = oss->returned;
89         ret = 1;
90 out:
91         ogg_stream_clear(&stream);
92         return ret;
93 }
94
95 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
96                 struct timeval *result)
97 {
98         uint64_t x = (uint64_t)num_frames * 1000 * 1000
99                 / frequency / num_chunks;
100
101         result->tv_sec = x / 1000 / 1000;
102         result->tv_usec = x % (1000 * 1000);
103         PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
104                 tv2ms(result));
105 }
106
107 /**
108  * Pass first three ogg packets to callback and build the chunk table.
109  *
110  * This function extracts the first three ogg packets of the audio data
111  * given by \a map and \a numbytes and passes each packet to the callback
112  * defined by \a ci.
113  *
114  * If the packet callback indicates success, the chunk table is built.  Chunk
115  * zero contains the first three ogg packets while all other chunks consist of
116  * exactly one ogg page.
117  *
118  * \param map Audio file data.
119  * \param numbytes The length of \a map.
120  * \param afhi Passed to the packet callback, contains chunk table.
121  * \param ci The callback structure.
122  *
123  * \return Standard.
124  */
125 int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
126                 struct ogg_afh_callback_info *ci)
127 {
128         ogg_sync_state oss;
129         ogg_page op;
130         long len = numbytes;
131         char *buf;
132         int ret, i, j, frames_per_chunk, ct_size;
133         long long unsigned num_frames = 0;
134
135         ogg_sync_init(&oss);
136         ret = -E_OGG_SYNC;
137         buf = ogg_sync_buffer(&oss, len);
138         if (!buf)
139                 goto out;
140         memcpy(buf, map, len);
141         ret = -E_OGG_SYNC;
142         if (ogg_sync_wrote(&oss, len) < 0)
143                 goto out;
144         ret = process_ogg_packets(&oss, afhi, ci);
145         if (ret < 0)
146                 goto out;
147         oss.returned = 0;
148         oss.fill = numbytes;
149         /* count ogg packages and get duration of the file */
150         for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
151                 num_frames = ogg_page_granulepos(&op);
152         PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
153         ret = -E_OGG_EMPTY;
154         if (i == 0)
155                 goto out;
156         afhi->seconds_total = num_frames / afhi->frequency;
157         /* use roughly one page per chunk */
158         frames_per_chunk = num_frames / i;
159         PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
160                 afhi->seconds_total, frames_per_chunk);
161         ct_size = 250;
162         afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
163         afhi->chunk_table[0] = 0;
164         afhi->chunk_table[1] = afhi->header_len;
165         oss.returned = afhi->header_len;
166         oss.fill = numbytes;
167         for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
168                 int granule = ogg_page_granulepos(&op);
169
170                 while (granule > j * frames_per_chunk) {
171                         j++;
172                         if (j >= ct_size) {
173                                 ct_size *= 2;
174                                 afhi->chunk_table = para_realloc(
175                                         afhi->chunk_table,
176                                         ct_size * sizeof(uint32_t));
177                         }
178                         afhi->chunk_table[j] = oss.returned;
179                 }
180         }
181         afhi->chunks_total = j;
182         set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
183         ret = 0;
184 out:
185         ogg_sync_clear(&oss);
186         return ret;
187 }