2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
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.
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.
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.
18 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
22 #include <vorbis/codec.h>
23 #include <vorbis/vorbisfile.h>
25 #include "server.cmdline.h"
33 /** must be big enough to hold header */
34 #define CHUNK_SIZE 32768
35 static double chunk_time = 0.25;
37 static OggVorbis_File *oggvorbis_file;
38 static int header_len;
40 static ssize_t *chunk_table;
41 static struct audio_format_handler *af;
43 static int ogg_compute_header_len(FILE *file)
45 int ret, len, in = fileno(file);
52 ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
53 ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
54 ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
56 ogg_sync_init(sync_in);
57 vorbis_info_init(&vi);
58 vorbis_comment_init(&vc);
59 buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
60 len = read(in, buf, CHUNK_SIZE);
64 ogg_sync_wrote(sync_in, len);
65 ret = -E_SYNC_PAGEOUT;
66 if (ogg_sync_pageout(sync_in, &page) <= 0)
68 serial = ogg_page_serialno(&page);
69 ogg_stream_init(stream_in, serial);
70 ogg_stream_init(stream_out, serial);
71 ret = ogg_stream_pagein(stream_in, &page);
73 ret = E_STREAM_PAGEIN;
76 ret = ogg_stream_packetout(stream_in, &packet);
78 ret = -E_STREAM_PACKETOUT;
82 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
84 PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
85 ogg_stream_packetin(stream_out, &packet);
87 ret = ogg_sync_pageout(sync_in, &page);
89 ret = -E_SYNC_PAGEOUT;
92 ogg_stream_pagein(stream_in, &page);
93 ogg_stream_packetout(stream_in, &packet);
94 ogg_stream_packetin(stream_out, &packet);
96 ret = ogg_sync_pageout(sync_in, &page);
98 ret = -E_SYNC_PAGEOUT;
101 ogg_stream_pagein(stream_in, &page);
102 ogg_stream_packetout(stream_in, &packet);
103 ogg_stream_packetin(stream_out, &packet);
106 while (ogg_stream_flush(stream_out, &page))
107 header_len += page.body_len + page.header_len;
109 PARA_INFO_LOG("header_len = %d\n", header_len);
111 ogg_stream_destroy(stream_in);
112 ogg_stream_destroy(stream_out);
114 ogg_sync_destroy(sync_in);
115 vorbis_info_clear(&vi);
116 vorbis_comment_clear(&vc);
120 static void tunetable(long unsigned num_chunks)
122 int i = 1, j = -1, lp = 1;
123 while (i < num_chunks) {
124 if (chunk_table[i] == chunk_table[lp]) {
129 tv_scale(i, &af->chunk_tv, &af->eof_tv);
130 for (j = lp; j < i; j++)
131 chunk_table[j] = chunk_table[i];
135 for (i = 2; i < num_chunks; i++)
136 if (chunk_table[i] != chunk_table[1])
139 for (i = 2; i < num_chunks - lp; i++)
140 chunk_table[i] = chunk_table[i + lp];
146 * Alloc and fill array table of byte offsets. chunk_table[i] is the
147 * offset in the current input file at which the sample containing time i *
150 static long unsigned ogg_compute_chunk_table(double time_total)
153 ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
154 long unsigned num_chunks;
158 num = time_total / chunk_time + 3;
159 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
161 chunk_table = para_malloc(num * sizeof(size_t));
164 for (i = 1; ret == 0; i++) {
166 ret = ov_time_seek(oggvorbis_file, i * chunk_time);
169 pos = ov_raw_tell(oggvorbis_file);
170 diff = pos - old_pos;
171 max_chunk_len = PARA_MAX(max_chunk_len, diff);
172 min = (i == 1)? diff : PARA_MIN(min, diff);
173 chunk_table[i] = pos;
174 if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
175 PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
176 "size: %zd\n", i - 1,
177 i * chunk_time, pos, pos - old_pos);
181 chunk_table[i] = pos;
182 tunetable(num_chunks);
183 PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
184 num_chunks, chunk_time, max_chunk_len, min);
188 static void ogg_close_audio_file(void)
190 if (oggvorbis_file) {
191 PARA_DEBUG_LOG("%s", "ov_clear\n");
192 ov_clear(oggvorbis_file);
193 free(oggvorbis_file);
194 oggvorbis_file = NULL;
203 static int ogg_save_header(FILE *file, int len)
207 header = para_malloc(len);
209 ret = read(fileno(file), header, len);
216 * Init oggvorbis file and write some tech data to given pointers.
218 static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
219 int *seconds, size_t **vss_chunk_table)
224 ogg_int64_t raw_total;
225 long vi_sampling_rate, vi_bitrate;
228 return -E_OGG_NO_FILE;
229 ret = ogg_compute_header_len(file);
232 ret = ogg_save_header(file, header_len);
236 oggvorbis_file = para_malloc(sizeof(OggVorbis_File));
237 ret = ov_open(file, oggvorbis_file, NULL, 0);
239 free(oggvorbis_file);
244 vi = ov_info(oggvorbis_file, 0);
247 time_total = ov_time_total(oggvorbis_file, -1);
248 raw_total = ov_raw_total(oggvorbis_file, -1);
249 *seconds = time_total;
250 vi_sampling_rate = vi->rate;
251 vi_bitrate = ov_bitrate(oggvorbis_file, 0);
253 *frames = ogg_compute_chunk_table(time_total);
255 *vss_chunk_table = chunk_table;
256 sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
257 "audio_file_info2: \n"
258 "audio_file_info3: \n",
259 *frames, (long unsigned) (chunk_time * 1000 * 1000),
260 vi_sampling_rate / 1000, vi->channels, vi_bitrate / 1000
265 ogg_close_audio_file();
269 static char *ogg_get_header_info(int *len)
275 static const char* ogg_suffixes[] = {"ogg", NULL};
278 * the init function of the ogg vorbis audio format handler
280 * \param p pointer to the struct to initialize
282 void ogg_init(struct audio_format_handler *p)
285 af->get_file_info = ogg_get_file_info,
286 af->close_audio_file = ogg_close_audio_file;
287 af->get_header_info = ogg_get_header_info;
288 af->chunk_tv.tv_sec = 0;
289 af->chunk_tv.tv_usec = 250 * 1000;
290 tv_scale(3, &af->chunk_tv, &af->eof_tv);
291 af->suffixes = ogg_suffixes;