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