ad7da3ee89a7cb7e2c896b4bb9909e7c722afe39
[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 #include "fd.h"
32
33 /** must be big enough to hold header */
34 #define CHUNK_SIZE 32768
35 static double chunk_time = 0.25;
36
37 static OggVorbis_File *oggvorbis_file;
38 static int header_len;
39 static char *header;
40 static ssize_t *chunk_table, max_chunk_len, num_chunks;
41 static struct audio_format_handler *af;
42 static long vi_sampling_rate, vi_bitrate, vi_bitrate_nominal;
43
44 static int ogg_compute_header_len(FILE *file)
45 {
46         int ret, len, in = fileno(file);
47         unsigned int serial;
48         char *buf;
49         ogg_page page;
50         ogg_packet packet;
51         vorbis_comment vc;
52         vorbis_info vi;
53         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
54         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
55         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
56
57         ogg_sync_init(sync_in);
58         vorbis_info_init(&vi);
59         vorbis_comment_init(&vc);
60         buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
61         len = read(in, buf, CHUNK_SIZE);
62         ret = -E_OGG_READ;
63         if (len <= 0)
64                 goto err1;
65         ogg_sync_wrote(sync_in, len);
66         ret = -E_SYNC_PAGEOUT;
67         if (ogg_sync_pageout(sync_in, &page) <= 0)
68                 goto err1;
69         serial = ogg_page_serialno(&page);
70         ogg_stream_init(stream_in, serial);
71         ogg_stream_init(stream_out, serial);
72         ret = ogg_stream_pagein(stream_in, &page);
73         if (ret < 0) {
74                 ret = E_STREAM_PAGEIN;
75                 goto err2;
76         }
77         ret = ogg_stream_packetout(stream_in, &packet);
78         if (ret != 1) {
79                 ret = -E_STREAM_PACKETOUT;
80                 goto err2;
81         }
82         ret = -E_VORBIS;
83         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
84                 goto err2;
85         PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
86         ogg_stream_packetin(stream_out, &packet);
87
88         ret = ogg_sync_pageout(sync_in, &page);
89         if (ret <= 0) {
90                 ret = -E_SYNC_PAGEOUT;
91                 goto err2;
92         }
93         ogg_stream_pagein(stream_in, &page);
94         ogg_stream_packetout(stream_in, &packet);
95         ogg_stream_packetin(stream_out, &packet);
96
97         ret = ogg_sync_pageout(sync_in, &page);
98         if (ret <= 0) {
99                 ret = -E_SYNC_PAGEOUT;
100                 goto err2;
101         }
102         ogg_stream_pagein(stream_in, &page);
103         ogg_stream_packetout(stream_in, &packet);
104         ogg_stream_packetin(stream_out, &packet);
105
106         header_len = 0;
107         while (ogg_stream_flush(stream_out, &page))
108                 header_len += page.body_len + page.header_len;
109         ret = len;
110         PARA_INFO_LOG("header_len = %d\n", header_len);
111 err2:
112         ogg_stream_destroy(stream_in);
113         ogg_stream_destroy(stream_out);
114 err1:
115         ogg_sync_destroy(sync_in);
116         vorbis_info_clear(&vi);
117         vorbis_comment_clear(&vc);
118         return ret;
119 }
120
121 static void tunetable(void)
122 {
123         int i = 1, j = -1, lp = 1;
124         while (i < num_chunks) {
125                 if (chunk_table[i] == chunk_table[lp]) {
126                         i++;
127                         continue;
128                 }
129                 if (j < 0)
130                         tv_scale(i, &af->chunk_tv, &af->eof_tv);
131                 for (j = lp; j < i; j++)
132                         chunk_table[j] = chunk_table[i];
133                 lp = i;
134         }
135 #if 1
136         for (i = 2; i < num_chunks; i++)
137                 if (chunk_table[i] != chunk_table[1])
138                         break;
139         lp = i;
140         for (i = 2; i < num_chunks - lp; i++)
141                 chunk_table[i] = chunk_table[i + lp];
142 #endif
143 }
144
145
146 /*
147  * Alloc and fill array table of byte offsets. chunk_table[i] is the
148  * offset in the current input file at which the sample containing time i *
149  * CHUNK_TIME begins.
150  */
151 static void ogg_compute_chunk_table(FILE *file, double time_total)
152 {
153         int i, ret, num;
154         ssize_t pos = 0, min = 0, old_pos;
155
156         old_pos = 0;
157         ret = 0;
158         num = time_total / chunk_time + 3;
159         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
160                 chunk_time, num);
161         chunk_table = para_malloc(num * sizeof(size_t));
162         chunk_table[0] = 0;
163         max_chunk_len = 0;
164         rewind(file);
165         for (i = 1; ret == 0; i++) {
166                 ogg_int64_t diff;
167                 ret = ov_time_seek(oggvorbis_file, i * chunk_time);
168                 if (ret)
169                         break;
170                 pos = ov_raw_tell(oggvorbis_file);
171                 diff = pos - old_pos;
172                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
173                 min = (i == 1)? diff : PARA_MIN(min, diff);
174                 chunk_table[i] = pos;
175                 if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
176                         PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
177                                 "size: %zd\n", i - 1,
178                                 i * chunk_time, pos, pos - old_pos);
179                 old_pos = pos;
180         }
181         num_chunks = i - 1;
182         chunk_table[i] = pos;
183         tunetable();
184         PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
185                 num_chunks, chunk_time, max_chunk_len, min);
186         rewind(file);
187 }
188
189 static void ogg_close_audio_file(void)
190 {
191         if (oggvorbis_file) {
192                 PARA_DEBUG_LOG("%s", "ov_clear\n");
193                 ov_clear(oggvorbis_file);
194                 free(oggvorbis_file);
195                 oggvorbis_file = NULL;
196         }
197         free(header);
198         header = NULL;
199         header_len = 0;
200         free(chunk_table);
201         chunk_table = NULL;
202         num_chunks = 0;
203 }
204
205 static int ogg_save_header(FILE *file, int len)
206 {
207         int ret;
208
209         header = para_malloc(len);
210         rewind(file);
211         ret = read(fileno(file), header, len);
212         if (ret != len)
213                 return -E_OGG_READ;
214         return 1;
215 }
216
217 /*
218  * Init oggvorbis file and write some tech data to given pointers.
219  */
220 static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
221         int *seconds, size_t **vss_chunk_table)
222 {
223         int ret;
224         double time_total;
225         vorbis_info *vi;
226         ogg_int64_t raw_total;
227
228         if (!file)
229                 return -E_OGG_NO_FILE;
230         ret = ogg_compute_header_len(file);
231         if (ret < 0)
232                 return ret;
233         ret = ogg_save_header(file, header_len);
234         if (ret < 0)
235                 return ret;
236         rewind(file);
237         oggvorbis_file = para_malloc(sizeof(OggVorbis_File));
238         ret = ov_open(file, oggvorbis_file, NULL, 0);
239         if (ret < 0) {
240                 free(oggvorbis_file);
241                 free(header);
242                 return -E_OGG_OPEN;
243         }
244         ret = -E_OGG_INFO;
245         vi = ov_info(oggvorbis_file, 0);
246         if (!vi)
247                 goto err;
248         time_total = ov_time_total(oggvorbis_file, -1);
249         raw_total = ov_raw_total(oggvorbis_file, -1);
250         *seconds = time_total;
251         vi_sampling_rate = vi->rate;
252         vi_bitrate = ov_bitrate(oggvorbis_file, 0);
253         vi_bitrate_nominal = vi->bitrate_nominal;
254         ogg_compute_chunk_table(file, time_total);
255         *frames = num_chunks;
256         *vss_chunk_table = chunk_table;
257         sprintf(info_str, "audio_file_info1:%zu x %lu, %ldkHz, %d channels, %ldkbps\n"
258                 "audio_file_info2: \n"
259                 "audio_file_info3: \n",
260                 num_chunks, (long unsigned) (chunk_time * 1000 * 1000),
261                 vi_sampling_rate / 1000, vi->channels, vi_bitrate / 1000
262                 );
263         rewind(file);
264         return 1;
265 err:
266         ogg_close_audio_file();
267         return ret;
268 }
269
270 static char *ogg_get_header_info(int *len)
271 {
272         *len = header_len;
273         return header;
274 }
275
276 static const char* ogg_suffixes[] = {"ogg", NULL};
277
278 /**
279  * the init function of the ogg vorbis audio format handler
280  *
281  * \param p pointer to the struct to initialize
282  */
283 void ogg_init(struct audio_format_handler *p)
284 {
285         af = p;
286         af->get_file_info = ogg_get_file_info,
287         af->close_audio_file = ogg_close_audio_file;
288         af->get_header_info = ogg_get_header_info;
289         af->chunk_tv.tv_sec = 0;
290         af->chunk_tv.tv_usec = 250 * 1000;
291         tv_scale(3, &af->chunk_tv, &af->eof_tv);
292         af->suffixes = ogg_suffixes;
293 }