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"
32 /** must be big enough to hold header */
33 #define CHUNK_SIZE 32768
34 static double chunk_time
= 0.25;
37 static int header_len
;
39 static ssize_t
*chunk_table
;
40 static struct audio_format_handler
*af
;
42 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
45 return fread(buf
, size
, nmemb
, f
);
48 static int cb_seek(__a_unused
void *datasource
, ogg_int64_t offset
,
52 return fseek(f
, offset
, whence
);
55 /* don't do anything as vss still needs the open filehandle */
56 static int cb_close(__a_unused
void *datasource
)
61 long cb_tell(void *datasource
)
67 int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
69 int ret
= ov_open_callbacks(datasource
, vf
,
70 NULL
, /* no initial buffer */
71 0, /* no initial bytes */
72 c
); /* the ov_open_callbacks */
74 /* FIXME: provide better error codes */
77 if (ret
== OV_ENOTVORBIS
)
79 if (ret
== OV_EVERSION
)
81 if (ret
== OV_EBADHEADER
)
89 static int ogg_compute_header_len(FILE *file
)
91 int ret
, len
, in
= fileno(file
);
98 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
99 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
100 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
102 ogg_sync_init(sync_in
);
103 vorbis_info_init(&vi
);
104 vorbis_comment_init(&vc
);
105 buf
= ogg_sync_buffer(sync_in
, CHUNK_SIZE
);
106 len
= read(in
, buf
, CHUNK_SIZE
);
110 ogg_sync_wrote(sync_in
, len
);
111 ret
= -E_SYNC_PAGEOUT
;
112 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
114 serial
= ogg_page_serialno(&page
);
115 ogg_stream_init(stream_in
, serial
);
116 ogg_stream_init(stream_out
, serial
);
117 ret
= ogg_stream_pagein(stream_in
, &page
);
119 ret
= E_STREAM_PAGEIN
;
122 ret
= ogg_stream_packetout(stream_in
, &packet
);
124 ret
= -E_STREAM_PACKETOUT
;
128 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
130 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
131 ogg_stream_packetin(stream_out
, &packet
);
133 ret
= ogg_sync_pageout(sync_in
, &page
);
135 ret
= -E_SYNC_PAGEOUT
;
138 ogg_stream_pagein(stream_in
, &page
);
139 ogg_stream_packetout(stream_in
, &packet
);
140 ogg_stream_packetin(stream_out
, &packet
);
142 ret
= ogg_sync_pageout(sync_in
, &page
);
144 ret
= -E_SYNC_PAGEOUT
;
147 ogg_stream_pagein(stream_in
, &page
);
148 ogg_stream_packetout(stream_in
, &packet
);
149 ogg_stream_packetin(stream_out
, &packet
);
152 while (ogg_stream_flush(stream_out
, &page
))
153 header_len
+= page
.body_len
+ page
.header_len
;
155 PARA_INFO_LOG("header_len = %d\n", header_len
);
157 ogg_stream_destroy(stream_in
);
158 ogg_stream_destroy(stream_out
);
160 ogg_sync_destroy(sync_in
);
161 vorbis_info_clear(&vi
);
162 vorbis_comment_clear(&vc
);
167 * Alloc and fill array table of byte offsets. chunk_table[i] is the
168 * offset in the current input file at which the sample containing time i *
169 * CHUNK_TIME begins. Always successful.
171 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
175 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
176 long unsigned num_chunks
;
180 num
= time_total
/ chunk_time
+ 3;
181 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
183 chunk_table
= para_malloc(num
* sizeof(size_t));
186 for (i
= 1; ret
== 0; i
++) {
188 ret
= ov_time_seek(of
, i
* chunk_time
);
191 pos
= ov_raw_tell(of
);
192 diff
= pos
- old_pos
;
193 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
194 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
195 chunk_table
[i
] = pos
;
196 if (i
< 11 || !((i
- 1) % 1000)|| i
> num
- 11)
197 PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
198 "size: %zd\n", i
- 1,
199 i
* chunk_time
, pos
, pos
- old_pos
);
203 chunk_table
[i
] = pos
;
204 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
205 num_chunks
, chunk_time
, max_chunk_len
, min
);
209 static void ogg_close_audio_file(void)
216 static int ogg_save_header(FILE *file
, int len
)
220 header
= para_malloc(len
);
222 ret
= read(fileno(file
), header
, len
);
230 * Init oggvorbis file and write some tech data to given pointers.
232 static int ogg_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
233 int *seconds
, size_t **vss_chunk_table
)
238 ogg_int64_t raw_total
;
239 long vi_sampling_rate
, vi_bitrate
;
241 static const ov_callbacks ovc
= {
242 .read_func
= cb_read
,
243 .seek_func
= cb_seek
,
244 .close_func
= cb_close
,
249 return -E_OGG_NO_FILE
;
250 ret
= ogg_compute_header_len(file
);
253 ret
= ogg_save_header(file
, header_len
);
257 ret
= ogg_open_callbacks(file
, &of
, ovc
);
261 vi
= ov_info(&of
, 0);
264 time_total
= ov_time_total(&of
, -1);
265 raw_total
= ov_raw_total(&of
, -1);
266 *seconds
= time_total
;
267 vi_sampling_rate
= vi
->rate
;
268 vi_bitrate
= ov_bitrate(&of
, 0);
270 *frames
= ogg_compute_chunk_table(&of
, time_total
);
272 *vss_chunk_table
= chunk_table
;
273 sprintf(info_str
, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
274 "audio_file_info2: \n"
275 "audio_file_info3: \n",
276 *frames
, (long unsigned) (chunk_time
* 1000 * 1000),
277 vi_sampling_rate
/ 1000, vi
->channels
, vi_bitrate
/ 1000
283 ov_clear(&of
); /* keeps the file open */
289 static char *ogg_get_header_info(int *len
)
295 static const char* ogg_suffixes
[] = {"ogg", NULL
};
298 * the init function of the ogg vorbis audio format handler
300 * \param p pointer to the struct to initialize
302 void ogg_init(struct audio_format_handler
*p
)
305 af
->get_file_info
= ogg_get_file_info
,
306 af
->close_audio_file
= ogg_close_audio_file
;
307 af
->get_header_info
= ogg_get_header_info
;
308 af
->chunk_tv
.tv_sec
= 0;
309 af
->chunk_tv
.tv_usec
= 250 * 1000;
310 tv_scale(3, &af
->chunk_tv
, &af
->eof_tv
);
311 af
->suffixes
= ogg_suffixes
;