5adcc25218377e57c8b038e30e325268aeb6e86f
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
, off_t numbytes
,
121 struct audio_format_info
*afi
)
123 int ret
, len
= PARA_MIN(numbytes
, CHUNK_SIZE
);
130 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
131 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
132 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
134 ogg_sync_init(sync_in
);
135 vorbis_info_init(&vi
);
136 vorbis_comment_init(&vc
);
137 buf
= ogg_sync_buffer(sync_in
, len
);
138 memcpy(buf
, map
, len
);
139 ogg_sync_wrote(sync_in
, len
);
140 ret
= -E_SYNC_PAGEOUT
;
141 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
143 serial
= ogg_page_serialno(&page
);
144 ogg_stream_init(stream_in
, serial
);
145 ogg_stream_init(stream_out
, serial
);
146 ret
= ogg_stream_pagein(stream_in
, &page
);
148 ret
= E_STREAM_PAGEIN
;
151 ret
= ogg_stream_packetout(stream_in
, &packet
);
153 ret
= -E_STREAM_PACKETOUT
;
157 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
159 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
160 ogg_stream_packetin(stream_out
, &packet
);
162 ret
= ogg_sync_pageout(sync_in
, &page
);
164 ret
= -E_SYNC_PAGEOUT
;
167 ogg_stream_pagein(stream_in
, &page
);
168 ogg_stream_packetout(stream_in
, &packet
);
169 ogg_stream_packetin(stream_out
, &packet
);
171 ret
= ogg_sync_pageout(sync_in
, &page
);
173 ret
= -E_SYNC_PAGEOUT
;
176 ogg_stream_pagein(stream_in
, &page
);
177 ogg_stream_packetout(stream_in
, &packet
);
178 ogg_stream_packetin(stream_out
, &packet
);
181 while (ogg_stream_flush(stream_out
, &page
))
182 afi
->header_len
+= page
.body_len
+ page
.header_len
;
183 PARA_INFO_LOG("header_len = %d\n", afi
->header_len
);
184 afi
->header_offset
= 0;
187 ogg_stream_destroy(stream_in
);
188 ogg_stream_destroy(stream_out
);
190 ogg_sync_destroy(sync_in
);
191 vorbis_info_clear(&vi
);
192 vorbis_comment_clear(&vc
);
197 * Alloc and fill array table of byte offsets. chunk_table[i] is the
198 * offset in the current input file at which the sample containing time i *
199 * CHUNK_TIME begins. Always successful.
201 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
202 struct audio_format_info
*afi
, double time_total
)
205 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
206 long unsigned num_chunks
;
210 num
= time_total
/ chunk_time
+ 3;
211 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
213 afi
->chunk_table
= para_malloc((num
+ 1) * sizeof(size_t));
214 afi
->chunk_table
[0] = 0;
216 for (i
= 1; ret
<= num
; i
++) {
218 ret
= ov_time_seek(of
, i
* chunk_time
);
221 pos
= ov_raw_tell(of
);
222 diff
= pos
- old_pos
;
223 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
224 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
225 afi
->chunk_table
[i
] = pos
;
226 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
227 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
228 // "size: %zd\n", i - 1,
229 // i * chunk_time, pos, pos - old_pos);
233 //fi->chunk_table[i] = pos;
234 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
235 num_chunks
, chunk_time
, max_chunk_len
, min
);
240 * Init oggvorbis file and write some tech data to given pointers.
242 static int ogg_get_file_info(char *map
, off_t numbytes
,
243 struct audio_format_info
*afi
)
248 const ov_callbacks ovc
= {
249 .read_func
= cb_read
,
250 .seek_func
= cb_seek
,
251 .close_func
= cb_close
,
254 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
256 ret
= ogg_compute_header_len(map
, numbytes
, afi
);
259 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
263 vi
= ov_info(&of
, 0);
266 afi
->seconds_total
= ov_time_total(&of
, -1);
267 afi
->frequency
= vi
->rate
;
268 afi
->bitrate
= ov_bitrate(&of
, 0);
269 afi
->channels
= vi
->channels
;
270 afi
->chunks_total
= ogg_compute_chunk_table(&of
, afi
, afi
->seconds_total
);
271 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lu, %ukHz, "
272 "%d channels, %ukbps\n"
273 "audio_file_info2: \n"
274 "audio_file_info3: \n",
275 afi
->chunks_total
, (long unsigned) (chunk_time
* 1000 * 1000),
276 afi
->frequency
/ 1000, vi
->channels
, afi
->bitrate
/ 1000
278 afi
->chunk_tv
.tv_sec
= 0;
279 afi
->chunk_tv
.tv_usec
= 250 * 1000;
280 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
283 ov_clear(&of
); /* keeps the file open */
287 static const char* ogg_suffixes
[] = {"ogg", NULL
};
290 * the init function of the ogg vorbis audio format handler
292 * \param afh pointer to the struct to initialize
294 void ogg_init(struct audio_format_handler
*afh
)
296 afh
->get_file_info
= ogg_get_file_info
,
297 afh
->suffixes
= ogg_suffixes
;