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 struct ogg_datasource
{
39 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
41 struct ogg_datasource
*ods
= datasource
;
42 size_t copy
= PARA_MIN(ods
->numbytes
- ods
->fpos
, size
* nmemb
),
46 memcpy(buf
, ods
->map
+ ods
->fpos
, copy
);
47 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
48 ods
->fpos
+= ret
* size
;
52 static int cb_seek(void *datasource
, ogg_int64_t offset
,
55 struct ogg_datasource
*ods
= datasource
;
58 if (offset
>= 0 && offset
<= ods
->numbytes
) {
66 if (offset
<= 0 && -offset
<= ods
->numbytes
) {
67 ods
->fpos
= ods
->numbytes
+ offset
;
74 if ((offset
>= 0 && offset
+ ods
->fpos
> ods
->numbytes
) ||
75 (offset
< 0 && offset
+ ods
->fpos
< 0)) {
86 /* don't do anything as vss still needs the open filehandle */
87 static int cb_close(__a_unused
void *datasource
)
92 static long cb_tell(void *datasource
)
94 struct ogg_datasource
*ods
= datasource
;
95 return (unsigned long)ods
->fpos
;
98 static int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
100 int ret
= ov_open_callbacks(datasource
, vf
,
101 NULL
, /* no initial buffer */
102 0, /* no initial bytes */
103 c
); /* the ov_open_callbacks */
105 /* FIXME: provide better error codes */
108 if (ret
== OV_ENOTVORBIS
)
110 if (ret
== OV_EVERSION
)
112 if (ret
== OV_EBADHEADER
)
120 static int ogg_compute_header_len(char *map
, size_t numbytes
,
121 struct audio_format_info
*afi
)
124 size_t len
= PARA_MIN(numbytes
, CHUNK_SIZE
);
132 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
133 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
134 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
136 ogg_sync_init(sync_in
);
137 vorbis_info_init(&vi
);
138 vorbis_comment_init(&vc
);
139 buf
= ogg_sync_buffer(sync_in
, (long)len
);
140 memcpy(buf
, map
, len
);
141 ogg_sync_wrote(sync_in
, (long)len
);
142 ret
= -E_SYNC_PAGEOUT
;
143 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
145 serial
= ogg_page_serialno(&page
);
146 ogg_stream_init(stream_in
, serial
);
147 ogg_stream_init(stream_out
, serial
);
148 ret
= ogg_stream_pagein(stream_in
, &page
);
150 ret
= E_STREAM_PAGEIN
;
153 ret
= ogg_stream_packetout(stream_in
, &packet
);
155 ret
= -E_STREAM_PACKETOUT
;
159 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
161 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
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
);
173 ret
= ogg_sync_pageout(sync_in
, &page
);
175 ret
= -E_SYNC_PAGEOUT
;
178 ogg_stream_pagein(stream_in
, &page
);
179 ogg_stream_packetout(stream_in
, &packet
);
180 ogg_stream_packetin(stream_out
, &packet
);
183 while (ogg_stream_flush(stream_out
, &page
))
184 afi
->header_len
+= page
.body_len
+ page
.header_len
;
185 PARA_INFO_LOG("header_len = %d\n", afi
->header_len
);
186 afi
->header_offset
= 0;
189 ogg_stream_destroy(stream_in
);
190 ogg_stream_destroy(stream_out
);
192 ogg_sync_destroy(sync_in
);
193 vorbis_info_clear(&vi
);
194 vorbis_comment_clear(&vc
);
199 * Alloc and fill array table of byte offsets. chunk_table[i] is the
200 * offset in the current input file at which the sample containing time i *
201 * CHUNK_TIME begins. Always successful.
203 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
204 struct audio_format_info
*afi
, long unsigned time_total
)
207 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
208 long unsigned num_chunks
;
212 num
= time_total
/ chunk_time
+ 3;
213 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
215 afi
->chunk_table
= para_malloc((num
+ 1) * sizeof(size_t));
216 afi
->chunk_table
[0] = 0;
218 for (i
= 1; ret
<= num
; i
++) {
220 ret
= ov_time_seek(of
, i
* chunk_time
);
223 pos
= ov_raw_tell(of
);
224 diff
= pos
- old_pos
;
225 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
226 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
227 afi
->chunk_table
[i
] = pos
;
228 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
229 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
230 // "size: %zd\n", i - 1,
231 // i * chunk_time, pos, pos - old_pos);
235 //fi->chunk_table[i] = pos;
236 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
237 num_chunks
, chunk_time
, max_chunk_len
, min
);
242 * Init oggvorbis file and write some tech data to given pointers.
244 static int ogg_get_file_info(char *map
, size_t numbytes
,
245 struct audio_format_info
*afi
)
250 const ov_callbacks ovc
= {
251 .read_func
= cb_read
,
252 .seek_func
= cb_seek
,
253 .close_func
= cb_close
,
256 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
258 ret
= ogg_compute_header_len(map
, numbytes
, afi
);
261 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
265 vi
= ov_info(&of
, 0);
268 afi
->seconds_total
= ov_time_total(&of
, -1);
269 afi
->frequency
= vi
->rate
;
270 afi
->bitrate
= ov_bitrate(&of
, 0);
271 afi
->channels
= vi
->channels
;
272 afi
->chunks_total
= ogg_compute_chunk_table(&of
, afi
, afi
->seconds_total
);
273 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lu, %ukHz, "
274 "%d channels, %ukbps\n"
275 "audio_file_info2: \n"
276 "audio_file_info3: \n",
277 afi
->chunks_total
, (long unsigned) (chunk_time
* 1000 * 1000),
278 afi
->frequency
/ 1000, vi
->channels
, afi
->bitrate
/ 1000
280 afi
->chunk_tv
.tv_sec
= 0;
281 afi
->chunk_tv
.tv_usec
= 250 * 1000;
282 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
285 ov_clear(&of
); /* keeps the file open */
289 static const char* ogg_suffixes
[] = {"ogg", NULL
};
292 * the init function of the ogg vorbis audio format handler
294 * \param afh pointer to the struct to initialize
296 void ogg_init(struct audio_format_handler
*afh
)
298 afh
->get_file_info
= ogg_get_file_info
,
299 afh
->suffixes
= ogg_suffixes
;