afs.cmd: Add documentation of the remaining commands.
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
7
8 #include <inttypes.h>
9 #include <ogg/ogg.h>
10 #include <vorbis/codec.h>
11 #include <vorbis/vorbisfile.h>
12
13 #include "para.h"
14 #include "afh.h"
15 #include "server.h"
16 #include "error.h"
17 #include "string.h"
18
19 /** must be big enough to hold header */
20 #define CHUNK_SIZE 32768
21 static double chunk_time = 0.25;
22
23 /** describes a memory-mapped ogg vorbis file */
24 struct ogg_datasource {
25         /** the memory mapping */
26         char *map;
27         /** this size of the mapping */
28         off_t numbytes;
29         /** the current position in the mapping */
30         off_t fpos;
31 };
32
33 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
34 {
35         struct ogg_datasource *ods = datasource;
36         size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
37                 ret = copy / size;
38         if (!ret)
39                 return 0;
40         memcpy(buf, ods->map + ods->fpos, copy);
41 //      PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
42         ods->fpos += ret * size;
43         return ret;
44 }
45
46 static int cb_seek(void *datasource, ogg_int64_t offset,
47                 int whence)
48 {
49         struct ogg_datasource *ods = datasource;
50         switch (whence) {
51         case SEEK_SET:
52                 if (offset >= 0 && offset <= ods->numbytes) {
53                         ods->fpos = offset;
54                         return 0;
55                 }
56                 errno = EINVAL;
57                 return -1;
58                 break;
59         case SEEK_END:
60                 if (offset <= 0 && -offset <= ods->numbytes) {
61                         ods->fpos = ods->numbytes + offset;
62                         return 0;
63                 }
64                 errno = EINVAL;
65                 return -1;
66                 break;
67         case SEEK_CUR:
68                 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
69                                 (offset < 0 && offset + ods->fpos < 0)) {
70                         errno = EINVAL;
71                         return -1;
72                 }
73                 ods->fpos += offset;
74                 return 0;
75         }
76         errno = EINVAL;
77         return -1;
78 }
79
80 /* don't do anything as vss still needs the open filehandle */
81 static int cb_close(__a_unused void *datasource)
82 {
83         return 0;
84 }
85
86 static long cb_tell(void *datasource)
87 {
88         struct ogg_datasource *ods = datasource;
89         return (unsigned long)ods->fpos;
90 }
91
92 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
93 {
94         int ret = ov_open_callbacks(datasource, vf,
95                 NULL, /* no initial buffer */
96                 0, /* no initial bytes */
97                 c); /* the ov_open_callbacks */
98
99         if (ret == OV_EREAD)
100                 return -E_OGG_READ;
101         if (ret == OV_ENOTVORBIS)
102                 return -E_VORBIS;
103         if (ret == OV_EVERSION)
104                 return -E_OGG_VERSION;
105         if (ret == OV_EBADHEADER)
106                 return -E_OGG_BAD_HEADER;
107         if (ret < 0)
108                 return -E_OGG_UNKNOWN_ERROR;
109         return 1;
110
111 }
112
113 static int ogg_compute_header_len(char *map, size_t numbytes,
114                 struct audio_format_info *afi)
115 {
116         int ret;
117         size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
118         int serial;
119         char *buf;
120
121         ogg_page page;
122         ogg_packet packet;
123         vorbis_comment vc;
124         vorbis_info vi;
125         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
126         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
127         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
128
129         ogg_sync_init(sync_in);
130         vorbis_info_init(&vi);
131         vorbis_comment_init(&vc);
132         buf = ogg_sync_buffer(sync_in, (long)len);
133         memcpy(buf, map, len);
134         ogg_sync_wrote(sync_in, (long)len);
135         ret = -E_SYNC_PAGEOUT;
136         if (ogg_sync_pageout(sync_in, &page) <= 0) {
137                 free(stream_in);
138                 free(stream_out);
139                 goto err1;
140         }
141         serial = ogg_page_serialno(&page);
142         ogg_stream_init(stream_in, serial);
143         ogg_stream_init(stream_out, serial);
144         ret = ogg_stream_pagein(stream_in, &page);
145         if (ret < 0) {
146                 ret = -E_STREAM_PAGEIN;
147                 goto err2;
148         }
149         ret = ogg_stream_packetout(stream_in, &packet);
150         if (ret != 1) {
151                 ret = -E_STREAM_PACKETOUT;
152                 goto err2;
153         }
154         ret = -E_VORBIS;
155         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
156                 goto err2;
157         PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
158         ogg_stream_packetin(stream_out, &packet);
159
160         ret = ogg_sync_pageout(sync_in, &page);
161         if (ret <= 0) {
162                 ret = -E_SYNC_PAGEOUT;
163                 goto err2;
164         }
165         ogg_stream_pagein(stream_in, &page);
166         ogg_stream_packetout(stream_in, &packet);
167         ogg_stream_packetin(stream_out, &packet);
168
169         ret = ogg_sync_pageout(sync_in, &page);
170         if (ret <= 0) {
171                 ret = -E_SYNC_PAGEOUT;
172                 goto err2;
173         }
174         ogg_stream_pagein(stream_in, &page);
175         ogg_stream_packetout(stream_in, &packet);
176         ogg_stream_packetin(stream_out, &packet);
177
178         afi->header_len = 0;
179         while (ogg_stream_flush(stream_out, &page))
180                 afi->header_len += page.body_len + page.header_len;
181         PARA_DEBUG_LOG("header_len = %d\n", afi->header_len);
182         afi->header_offset = 0;
183         ret = 1;
184 err2:
185         ogg_stream_destroy(stream_in);
186         ogg_stream_destroy(stream_out);
187 err1:
188         ogg_sync_destroy(sync_in);
189         vorbis_info_clear(&vi);
190         vorbis_comment_clear(&vc);
191         return ret;
192 }
193
194 /*
195  * Alloc and fill array table of byte offsets. chunk_table[i] is the
196  * offset in the current input file at which the sample containing time i *
197  * CHUNK_TIME begins. Always successful.
198  */
199 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
200         struct audio_format_info *afi, long unsigned time_total)
201 {
202         int i, ret, num;
203         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
204         long unsigned num_chunks;
205
206         old_pos = 0;
207         ret = 0;
208         num = time_total / chunk_time + 3;
209         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
210                 chunk_time, num);
211         afi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
212         afi->chunk_table[0] = 0;
213         max_chunk_len = 0;
214         for (i = 1; ret <= num; i++) {
215                 ogg_int64_t diff;
216                 ret = ov_time_seek(of, i * chunk_time);
217                 if (ret)
218                         break;
219                 pos = ov_raw_tell(of);
220                 diff = pos - old_pos;
221                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
222                 min = (i == 1)? diff : PARA_MIN(min, diff);
223                 afi->chunk_table[i] = pos;
224 //              if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
225 //                      PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
226 //                              "size: %zd\n", i - 1,
227 //                              i * chunk_time, pos, pos - old_pos);
228                 old_pos = pos;
229         }
230         num_chunks = i - 1;
231 //fi->chunk_table[i] = pos;
232         PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
233                 num_chunks, chunk_time, max_chunk_len, min);
234         return num_chunks;
235 }
236
237 /*
238  * Init oggvorbis file and write some tech data to given pointers.
239  */
240 static int ogg_get_file_info(char *map, size_t numbytes,
241                 struct audio_format_info *afi)
242 {
243         int ret;
244         vorbis_info *vi;
245         OggVorbis_File of;
246         const ov_callbacks ovc = {
247                 .read_func = cb_read,
248                 .seek_func = cb_seek,
249                 .close_func = cb_close,
250                 .tell_func = cb_tell
251         };
252         struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
253
254         ret = ogg_compute_header_len(map, numbytes, afi);
255         if (ret < 0)
256                 return ret;
257         ret = ogg_open_callbacks(&ods, &of, ovc);
258         if (ret < 0)
259                 goto err;
260         ret = -E_OGG_INFO;
261         vi = ov_info(&of, 0);
262         if (!vi)
263                 goto err;
264         afi->seconds_total = ov_time_total(&of, -1);
265         afi->frequency = vi->rate;
266         afi->bitrate = ov_bitrate(&of, 0) / 1000;
267         afi->channels = vi->channels;
268         afi->chunks_total = ogg_compute_chunk_table(&of, afi, afi->seconds_total);
269         sprintf(afi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
270                 "%d channels, %ukbps\n"
271                 "audio_file_info2: \n"
272                 "audio_file_info3: \n",
273                 afi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
274                 afi->frequency / 1000, vi->channels, afi->bitrate
275                 );
276         afi->chunk_tv.tv_sec = 0;
277         afi->chunk_tv.tv_usec = 250 * 1000;
278         tv_scale(3, &afi->chunk_tv, &afi->eof_tv);
279         ret = 1;
280 err:
281         ov_clear(&of); /* keeps the file open */
282         return ret;
283 }
284
285 static const char* ogg_suffixes[] = {"ogg", NULL};
286
287 /**
288  * the init function of the ogg vorbis audio format handler
289  *
290  * \param afh pointer to the struct to initialize
291  */
292 void ogg_init(struct audio_format_handler *afh)
293 {
294         afh->get_file_info = ogg_get_file_info,
295         afh->suffixes = ogg_suffixes;
296 }