2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
10 #include <vorbis/codec.h>
11 #include <vorbis/vorbisfile.h>
17 /** must be big enough to hold header */
18 #define CHUNK_SIZE 32768
19 static double chunk_time
= 0.25;
21 /** describes a memory-mapped ogg vorbis file */
22 struct ogg_datasource
{
23 /** the memory mapping */
25 /** this size of the mapping */
27 /** the current position in the mapping */
31 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
33 struct ogg_datasource
*ods
= datasource
;
34 size_t copy
= PARA_MIN(ods
->numbytes
- ods
->fpos
, size
* nmemb
),
38 memcpy(buf
, ods
->map
+ ods
->fpos
, copy
);
39 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
40 ods
->fpos
+= ret
* size
;
44 static int cb_seek(void *datasource
, ogg_int64_t offset
,
47 struct ogg_datasource
*ods
= datasource
;
50 if (offset
>= 0 && offset
<= ods
->numbytes
) {
58 if (offset
<= 0 && -offset
<= ods
->numbytes
) {
59 ods
->fpos
= ods
->numbytes
+ offset
;
66 if ((offset
>= 0 && offset
+ ods
->fpos
> ods
->numbytes
) ||
67 (offset
< 0 && offset
+ ods
->fpos
< 0)) {
78 /* don't do anything as vss still needs the open filehandle */
79 static int cb_close(__a_unused
void *datasource
)
84 static long cb_tell(void *datasource
)
86 struct ogg_datasource
*ods
= datasource
;
87 return (unsigned long)ods
->fpos
;
90 static int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
92 int ret
= ov_open_callbacks(datasource
, vf
,
93 NULL
, /* no initial buffer */
94 0, /* no initial bytes */
95 c
); /* the ov_open_callbacks */
99 if (ret
== OV_ENOTVORBIS
)
101 if (ret
== OV_EVERSION
)
102 return -E_OGG_VERSION
;
103 if (ret
== OV_EBADHEADER
)
104 return -E_OGG_BAD_HEADER
;
106 return -E_OGG_UNKNOWN_ERROR
;
111 static int ogg_compute_header_len(char *map
, size_t numbytes
,
112 struct audio_format_info
*afi
)
115 size_t len
= PARA_MIN(numbytes
, CHUNK_SIZE
);
123 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
124 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
125 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
127 ogg_sync_init(sync_in
);
128 vorbis_info_init(&vi
);
129 vorbis_comment_init(&vc
);
130 buf
= ogg_sync_buffer(sync_in
, (long)len
);
131 memcpy(buf
, map
, len
);
132 ogg_sync_wrote(sync_in
, (long)len
);
133 ret
= -E_SYNC_PAGEOUT
;
134 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
136 serial
= ogg_page_serialno(&page
);
137 ogg_stream_init(stream_in
, serial
);
138 ogg_stream_init(stream_out
, serial
);
139 ret
= ogg_stream_pagein(stream_in
, &page
);
141 ret
= -E_STREAM_PAGEIN
;
144 ret
= ogg_stream_packetout(stream_in
, &packet
);
146 ret
= -E_STREAM_PACKETOUT
;
150 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
152 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
153 ogg_stream_packetin(stream_out
, &packet
);
155 ret
= ogg_sync_pageout(sync_in
, &page
);
157 ret
= -E_SYNC_PAGEOUT
;
160 ogg_stream_pagein(stream_in
, &page
);
161 ogg_stream_packetout(stream_in
, &packet
);
162 ogg_stream_packetin(stream_out
, &packet
);
164 ret
= ogg_sync_pageout(sync_in
, &page
);
166 ret
= -E_SYNC_PAGEOUT
;
169 ogg_stream_pagein(stream_in
, &page
);
170 ogg_stream_packetout(stream_in
, &packet
);
171 ogg_stream_packetin(stream_out
, &packet
);
174 while (ogg_stream_flush(stream_out
, &page
))
175 afi
->header_len
+= page
.body_len
+ page
.header_len
;
176 PARA_INFO_LOG("header_len = %d\n", afi
->header_len
);
177 afi
->header_offset
= 0;
180 ogg_stream_destroy(stream_in
);
181 ogg_stream_destroy(stream_out
);
183 ogg_sync_destroy(sync_in
);
184 vorbis_info_clear(&vi
);
185 vorbis_comment_clear(&vc
);
190 * Alloc and fill array table of byte offsets. chunk_table[i] is the
191 * offset in the current input file at which the sample containing time i *
192 * CHUNK_TIME begins. Always successful.
194 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
195 struct audio_format_info
*afi
, long unsigned time_total
)
198 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
199 long unsigned num_chunks
;
203 num
= time_total
/ chunk_time
+ 3;
204 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
206 afi
->chunk_table
= para_malloc((num
+ 1) * sizeof(size_t));
207 afi
->chunk_table
[0] = 0;
209 for (i
= 1; ret
<= num
; i
++) {
211 ret
= ov_time_seek(of
, i
* chunk_time
);
214 pos
= ov_raw_tell(of
);
215 diff
= pos
- old_pos
;
216 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
217 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
218 afi
->chunk_table
[i
] = pos
;
219 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
220 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
221 // "size: %zd\n", i - 1,
222 // i * chunk_time, pos, pos - old_pos);
226 //fi->chunk_table[i] = pos;
227 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
228 num_chunks
, chunk_time
, max_chunk_len
, min
);
233 * Init oggvorbis file and write some tech data to given pointers.
235 static int ogg_get_file_info(char *map
, size_t numbytes
,
236 struct audio_format_info
*afi
)
241 const ov_callbacks ovc
= {
242 .read_func
= cb_read
,
243 .seek_func
= cb_seek
,
244 .close_func
= cb_close
,
247 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
249 ret
= ogg_compute_header_len(map
, numbytes
, afi
);
252 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
256 vi
= ov_info(&of
, 0);
259 afi
->seconds_total
= ov_time_total(&of
, -1);
260 afi
->frequency
= vi
->rate
;
261 afi
->bitrate
= ov_bitrate(&of
, 0);
262 afi
->channels
= vi
->channels
;
263 afi
->chunks_total
= ogg_compute_chunk_table(&of
, afi
, afi
->seconds_total
);
264 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lu, %ukHz, "
265 "%d channels, %ukbps\n"
266 "audio_file_info2: \n"
267 "audio_file_info3: \n",
268 afi
->chunks_total
, (long unsigned) (chunk_time
* 1000 * 1000),
269 afi
->frequency
/ 1000, vi
->channels
, afi
->bitrate
/ 1000
271 afi
->chunk_tv
.tv_sec
= 0;
272 afi
->chunk_tv
.tv_usec
= 250 * 1000;
273 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
276 ov_clear(&of
); /* keeps the file open */
280 static const char* ogg_suffixes
[] = {"ogg", NULL
};
283 * the init function of the ogg vorbis audio format handler
285 * \param afh pointer to the struct to initialize
287 void ogg_init(struct audio_format_handler
*afh
)
289 afh
->get_file_info
= ogg_get_file_info
,
290 afh
->suffixes
= ogg_suffixes
;