709e7d4b3c5e7278c72ba6fb2451c8798dcab411
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
19
20 #include <inttypes.h>
21 #include <ogg/ogg.h>
22 #include <vorbis/codec.h>
23 #include <vorbis/vorbisfile.h>
24
25 #include "server.cmdline.h"
26 #include "server.h"
27 #include "vss.h"
28 #include "afh.h"
29 #include "error.h"
30 #include "string.h"
31
32 /** must be big enough to hold header */
33 #define CHUNK_SIZE 32768
34 static double chunk_time = 0.25;
35
36 static OggVorbis_File *oggvorbis_file;
37 static int header_len;
38 static char *header;
39 static ssize_t *chunk_table;
40 static struct audio_format_handler *af;
41
42 static int ogg_compute_header_len(FILE *file)
43 {
44         int ret, len, in = fileno(file);
45         unsigned int serial;
46         char *buf;
47         ogg_page page;
48         ogg_packet packet;
49         vorbis_comment vc;
50         vorbis_info vi;
51         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
52         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
53         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
54
55         ogg_sync_init(sync_in);
56         vorbis_info_init(&vi);
57         vorbis_comment_init(&vc);
58         buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
59         len = read(in, buf, CHUNK_SIZE);
60         ret = -E_OGG_READ;
61         if (len <= 0)
62                 goto err1;
63         ogg_sync_wrote(sync_in, len);
64         ret = -E_SYNC_PAGEOUT;
65         if (ogg_sync_pageout(sync_in, &page) <= 0)
66                 goto err1;
67         serial = ogg_page_serialno(&page);
68         ogg_stream_init(stream_in, serial);
69         ogg_stream_init(stream_out, serial);
70         ret = ogg_stream_pagein(stream_in, &page);
71         if (ret < 0) {
72                 ret = E_STREAM_PAGEIN;
73                 goto err2;
74         }
75         ret = ogg_stream_packetout(stream_in, &packet);
76         if (ret != 1) {
77                 ret = -E_STREAM_PACKETOUT;
78                 goto err2;
79         }
80         ret = -E_VORBIS;
81         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
82                 goto err2;
83         PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
84         ogg_stream_packetin(stream_out, &packet);
85
86         ret = ogg_sync_pageout(sync_in, &page);
87         if (ret <= 0) {
88                 ret = -E_SYNC_PAGEOUT;
89                 goto err2;
90         }
91         ogg_stream_pagein(stream_in, &page);
92         ogg_stream_packetout(stream_in, &packet);
93         ogg_stream_packetin(stream_out, &packet);
94
95         ret = ogg_sync_pageout(sync_in, &page);
96         if (ret <= 0) {
97                 ret = -E_SYNC_PAGEOUT;
98                 goto err2;
99         }
100         ogg_stream_pagein(stream_in, &page);
101         ogg_stream_packetout(stream_in, &packet);
102         ogg_stream_packetin(stream_out, &packet);
103
104         header_len = 0;
105         while (ogg_stream_flush(stream_out, &page))
106                 header_len += page.body_len + page.header_len;
107         ret = len;
108         PARA_INFO_LOG("header_len = %d\n", header_len);
109 err2:
110         ogg_stream_destroy(stream_in);
111         ogg_stream_destroy(stream_out);
112 err1:
113         ogg_sync_destroy(sync_in);
114         vorbis_info_clear(&vi);
115         vorbis_comment_clear(&vc);
116         return ret;
117 }
118
119 static void tunetable(long unsigned num_chunks)
120 {
121         int i = 1, j = -1, lp = 1;
122         while (i < num_chunks) {
123                 if (chunk_table[i] == chunk_table[lp]) {
124                         i++;
125                         continue;
126                 }
127                 if (j < 0)
128                         tv_scale(i, &af->chunk_tv, &af->eof_tv);
129                 for (j = lp; j < i; j++)
130                         chunk_table[j] = chunk_table[i];
131                 lp = i;
132         }
133 #if 1
134         for (i = 2; i < num_chunks; i++)
135                 if (chunk_table[i] != chunk_table[1])
136                         break;
137         lp = i;
138         for (i = 2; i < num_chunks - lp; i++)
139                 chunk_table[i] = chunk_table[i + lp];
140 #endif
141 }
142
143
144 /*
145  * Alloc and fill array table of byte offsets. chunk_table[i] is the
146  * offset in the current input file at which the sample containing time i *
147  * CHUNK_TIME begins.
148  */
149 static long unsigned ogg_compute_chunk_table(double time_total)
150 {
151         int i, ret, num;
152         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
153         long unsigned num_chunks;
154
155         old_pos = 0;
156         ret = 0;
157         num = time_total / chunk_time + 3;
158         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
159                 chunk_time, num);
160         chunk_table = para_malloc(num * sizeof(size_t));
161         chunk_table[0] = 0;
162         max_chunk_len = 0;
163         for (i = 1; ret == 0; i++) {
164                 ogg_int64_t diff;
165                 ret = ov_time_seek(oggvorbis_file, i * chunk_time);
166                 if (ret)
167                         break;
168                 pos = ov_raw_tell(oggvorbis_file);
169                 diff = pos - old_pos;
170                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
171                 min = (i == 1)? diff : PARA_MIN(min, diff);
172                 chunk_table[i] = pos;
173                 if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
174                         PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
175                                 "size: %zd\n", i - 1,
176                                 i * chunk_time, pos, pos - old_pos);
177                 old_pos = pos;
178         }
179         num_chunks = i - 1;
180         chunk_table[i] = pos;
181         tunetable(num_chunks);
182         PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
183                 num_chunks, chunk_time, max_chunk_len, min);
184         return num_chunks;
185 }
186
187 static void ogg_close_audio_file(void)
188 {
189         if (oggvorbis_file) {
190                 PARA_DEBUG_LOG("%s", "ov_clear\n");
191                 ov_clear(oggvorbis_file);
192                 free(oggvorbis_file);
193                 oggvorbis_file = NULL;
194         }
195         free(header);
196         header = NULL;
197         header_len = 0;
198         free(chunk_table);
199         chunk_table = NULL;
200 }
201
202 static int ogg_save_header(FILE *file, int len)
203 {
204         int ret;
205
206         header = para_malloc(len);
207         rewind(file);
208         ret = read(fileno(file), header, len);
209         if (ret != len)
210                 return -E_OGG_READ;
211         return 1;
212 }
213
214 /*
215  * Init oggvorbis file and write some tech data to given pointers.
216  */
217 static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
218         int *seconds, size_t **vss_chunk_table)
219 {
220         int ret;
221         double time_total;
222         vorbis_info *vi;
223         ogg_int64_t raw_total;
224         long vi_sampling_rate, vi_bitrate;
225
226         if (!file)
227                 return -E_OGG_NO_FILE;
228         ret = ogg_compute_header_len(file);
229         if (ret < 0)
230                 return ret;
231         ret = ogg_save_header(file, header_len);
232         if (ret < 0)
233                 return ret;
234         rewind(file);
235         oggvorbis_file = para_malloc(sizeof(OggVorbis_File));
236         ret = ov_open(file, oggvorbis_file, NULL, 0);
237         if (ret < 0) {
238                 free(oggvorbis_file);
239                 free(header);
240                 return -E_OGG_OPEN;
241         }
242         ret = -E_OGG_INFO;
243         vi = ov_info(oggvorbis_file, 0);
244         if (!vi)
245                 goto err;
246         time_total = ov_time_total(oggvorbis_file, -1);
247         raw_total = ov_raw_total(oggvorbis_file, -1);
248         *seconds = time_total;
249         vi_sampling_rate = vi->rate;
250         vi_bitrate = ov_bitrate(oggvorbis_file, 0);
251         rewind(file);
252         *frames = ogg_compute_chunk_table(time_total);
253         rewind(file);
254         *vss_chunk_table = chunk_table;
255         sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
256                 "audio_file_info2: \n"
257                 "audio_file_info3: \n",
258                 *frames, (long unsigned) (chunk_time * 1000 * 1000),
259                 vi_sampling_rate / 1000, vi->channels, vi_bitrate / 1000
260                 );
261         rewind(file);
262         return 1;
263 err:
264         ogg_close_audio_file();
265         return ret;
266 }
267
268 static char *ogg_get_header_info(int *len)
269 {
270         *len = header_len;
271         return header;
272 }
273
274 static const char* ogg_suffixes[] = {"ogg", NULL};
275
276 /**
277  * the init function of the ogg vorbis audio format handler
278  *
279  * \param p pointer to the struct to initialize
280  */
281 void ogg_init(struct audio_format_handler *p)
282 {
283         af = p;
284         af->get_file_info = ogg_get_file_info,
285         af->close_audio_file = ogg_close_audio_file;
286         af->get_header_info = ogg_get_header_info;
287         af->chunk_tv.tv_sec = 0;
288         af->chunk_tv.tv_usec = 250 * 1000;
289         tv_scale(3, &af->chunk_tv, &af->eof_tv);
290         af->suffixes = ogg_suffixes;
291 }