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>
29 /** must be big enough to hold header */
30 #define CHUNK_SIZE 32768
31 static double chunk_time
= 0.25;
33 /** describes a memory-mapped ogg vorbis file */
34 struct ogg_datasource
{
35 /** the memory mapping */
37 /** this size of the mapping */
39 /** the current position in the mapping */
43 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
45 struct ogg_datasource
*ods
= datasource
;
46 size_t copy
= PARA_MIN(ods
->numbytes
- ods
->fpos
, size
* nmemb
),
50 memcpy(buf
, ods
->map
+ ods
->fpos
, copy
);
51 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
52 ods
->fpos
+= ret
* size
;
56 static int cb_seek(void *datasource
, ogg_int64_t offset
,
59 struct ogg_datasource
*ods
= datasource
;
62 if (offset
>= 0 && offset
<= ods
->numbytes
) {
70 if (offset
<= 0 && -offset
<= ods
->numbytes
) {
71 ods
->fpos
= ods
->numbytes
+ offset
;
78 if ((offset
>= 0 && offset
+ ods
->fpos
> ods
->numbytes
) ||
79 (offset
< 0 && offset
+ ods
->fpos
< 0)) {
90 /* don't do anything as vss still needs the open filehandle */
91 static int cb_close(__a_unused
void *datasource
)
96 static long cb_tell(void *datasource
)
98 struct ogg_datasource
*ods
= datasource
;
99 return (unsigned long)ods
->fpos
;
102 static int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
104 int ret
= ov_open_callbacks(datasource
, vf
,
105 NULL
, /* no initial buffer */
106 0, /* no initial bytes */
107 c
); /* the ov_open_callbacks */
111 if (ret
== OV_ENOTVORBIS
)
113 if (ret
== OV_EVERSION
)
114 return -E_OGG_VERSION
;
115 if (ret
== OV_EBADHEADER
)
116 return -E_OGG_BAD_HEADER
;
118 return -E_OGG_UNKNOWN_ERROR
;
123 static int ogg_compute_header_len(char *map
, size_t numbytes
,
124 struct audio_format_info
*afi
)
127 size_t len
= PARA_MIN(numbytes
, CHUNK_SIZE
);
135 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
136 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
137 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
139 ogg_sync_init(sync_in
);
140 vorbis_info_init(&vi
);
141 vorbis_comment_init(&vc
);
142 buf
= ogg_sync_buffer(sync_in
, (long)len
);
143 memcpy(buf
, map
, len
);
144 ogg_sync_wrote(sync_in
, (long)len
);
145 ret
= -E_SYNC_PAGEOUT
;
146 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
148 serial
= ogg_page_serialno(&page
);
149 ogg_stream_init(stream_in
, serial
);
150 ogg_stream_init(stream_out
, serial
);
151 ret
= ogg_stream_pagein(stream_in
, &page
);
153 ret
= -E_STREAM_PAGEIN
;
156 ret
= ogg_stream_packetout(stream_in
, &packet
);
158 ret
= -E_STREAM_PACKETOUT
;
162 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
164 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
165 ogg_stream_packetin(stream_out
, &packet
);
167 ret
= ogg_sync_pageout(sync_in
, &page
);
169 ret
= -E_SYNC_PAGEOUT
;
172 ogg_stream_pagein(stream_in
, &page
);
173 ogg_stream_packetout(stream_in
, &packet
);
174 ogg_stream_packetin(stream_out
, &packet
);
176 ret
= ogg_sync_pageout(sync_in
, &page
);
178 ret
= -E_SYNC_PAGEOUT
;
181 ogg_stream_pagein(stream_in
, &page
);
182 ogg_stream_packetout(stream_in
, &packet
);
183 ogg_stream_packetin(stream_out
, &packet
);
186 while (ogg_stream_flush(stream_out
, &page
))
187 afi
->header_len
+= page
.body_len
+ page
.header_len
;
188 PARA_INFO_LOG("header_len = %d\n", afi
->header_len
);
189 afi
->header_offset
= 0;
192 ogg_stream_destroy(stream_in
);
193 ogg_stream_destroy(stream_out
);
195 ogg_sync_destroy(sync_in
);
196 vorbis_info_clear(&vi
);
197 vorbis_comment_clear(&vc
);
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.
206 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
207 struct audio_format_info
*afi
, long unsigned time_total
)
210 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
211 long unsigned num_chunks
;
215 num
= time_total
/ chunk_time
+ 3;
216 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
218 afi
->chunk_table
= para_malloc((num
+ 1) * sizeof(size_t));
219 afi
->chunk_table
[0] = 0;
221 for (i
= 1; ret
<= num
; i
++) {
223 ret
= ov_time_seek(of
, i
* chunk_time
);
226 pos
= ov_raw_tell(of
);
227 diff
= pos
- old_pos
;
228 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
229 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
230 afi
->chunk_table
[i
] = pos
;
231 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
232 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
233 // "size: %zd\n", i - 1,
234 // i * chunk_time, pos, pos - old_pos);
238 //fi->chunk_table[i] = pos;
239 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
240 num_chunks
, chunk_time
, max_chunk_len
, min
);
245 * Init oggvorbis file and write some tech data to given pointers.
247 static int ogg_get_file_info(char *map
, size_t numbytes
,
248 struct audio_format_info
*afi
)
253 const ov_callbacks ovc
= {
254 .read_func
= cb_read
,
255 .seek_func
= cb_seek
,
256 .close_func
= cb_close
,
259 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
261 ret
= ogg_compute_header_len(map
, numbytes
, afi
);
264 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
268 vi
= ov_info(&of
, 0);
271 afi
->seconds_total
= ov_time_total(&of
, -1);
272 afi
->frequency
= vi
->rate
;
273 afi
->bitrate
= ov_bitrate(&of
, 0);
274 afi
->channels
= vi
->channels
;
275 afi
->chunks_total
= ogg_compute_chunk_table(&of
, afi
, afi
->seconds_total
);
276 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lu, %ukHz, "
277 "%d channels, %ukbps\n"
278 "audio_file_info2: \n"
279 "audio_file_info3: \n",
280 afi
->chunks_total
, (long unsigned) (chunk_time
* 1000 * 1000),
281 afi
->frequency
/ 1000, vi
->channels
, afi
->bitrate
/ 1000
283 afi
->chunk_tv
.tv_sec
= 0;
284 afi
->chunk_tv
.tv_usec
= 250 * 1000;
285 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
288 ov_clear(&of
); /* keeps the file open */
292 static const char* ogg_suffixes
[] = {"ogg", NULL
};
295 * the init function of the ogg vorbis audio format handler
297 * \param afh pointer to the struct to initialize
299 void ogg_init(struct audio_format_handler
*afh
)
301 afh
->get_file_info
= ogg_get_file_info
,
302 afh
->suffixes
= ogg_suffixes
;