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"
31 /** must be big enough to hold header */
32 #define CHUNK_SIZE 32768
33 static double chunk_time
= 0.25;
35 struct ogg_datasource
{
41 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
43 struct ogg_datasource
*ods
= datasource
;
44 size_t copy
= PARA_MIN(ods
->numbytes
- ods
->fpos
, size
* nmemb
),
48 memcpy(buf
, ods
->map
+ ods
->fpos
, copy
);
49 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
50 ods
->fpos
+= ret
* size
;
54 static int cb_seek(void *datasource
, ogg_int64_t offset
,
57 struct ogg_datasource
*ods
= datasource
;
60 if (offset
>= 0 && offset
<= ods
->numbytes
) {
68 if (offset
<= 0 && -offset
<= ods
->numbytes
) {
69 ods
->fpos
= ods
->numbytes
+ offset
;
76 if ((offset
>= 0 && offset
+ ods
->fpos
> ods
->numbytes
) ||
77 (offset
< 0 && offset
+ ods
->fpos
< 0)) {
88 /* don't do anything as vss still needs the open filehandle */
89 static int cb_close(__a_unused
void *datasource
)
94 static long cb_tell(void *datasource
)
96 struct ogg_datasource
*ods
= datasource
;
97 return (unsigned long)ods
->fpos
;
100 static int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
102 int ret
= ov_open_callbacks(datasource
, vf
,
103 NULL
, /* no initial buffer */
104 0, /* no initial bytes */
105 c
); /* the ov_open_callbacks */
107 /* FIXME: provide better error codes */
110 if (ret
== OV_ENOTVORBIS
)
112 if (ret
== OV_EVERSION
)
114 if (ret
== OV_EBADHEADER
)
122 static void ogg_save_header(char *map
, struct audio_format_info
*afi
)
124 afi
->header
= para_malloc(afi
->header_len
);
125 memcpy(afi
->header
, map
, afi
->header_len
);
128 static int ogg_compute_header_len(char *map
, off_t numbytes
,
129 struct audio_format_info
*afi
)
131 int ret
, len
= PARA_MIN(numbytes
, CHUNK_SIZE
);
138 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
139 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
140 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
142 ogg_sync_init(sync_in
);
143 vorbis_info_init(&vi
);
144 vorbis_comment_init(&vc
);
145 buf
= ogg_sync_buffer(sync_in
, len
);
146 memcpy(buf
, map
, len
);
147 ogg_sync_wrote(sync_in
, len
);
148 ret
= -E_SYNC_PAGEOUT
;
149 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
151 serial
= ogg_page_serialno(&page
);
152 ogg_stream_init(stream_in
, serial
);
153 ogg_stream_init(stream_out
, serial
);
154 ret
= ogg_stream_pagein(stream_in
, &page
);
156 ret
= E_STREAM_PAGEIN
;
159 ret
= ogg_stream_packetout(stream_in
, &packet
);
161 ret
= -E_STREAM_PACKETOUT
;
165 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
167 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
168 ogg_stream_packetin(stream_out
, &packet
);
170 ret
= ogg_sync_pageout(sync_in
, &page
);
172 ret
= -E_SYNC_PAGEOUT
;
175 ogg_stream_pagein(stream_in
, &page
);
176 ogg_stream_packetout(stream_in
, &packet
);
177 ogg_stream_packetin(stream_out
, &packet
);
179 ret
= ogg_sync_pageout(sync_in
, &page
);
181 ret
= -E_SYNC_PAGEOUT
;
184 ogg_stream_pagein(stream_in
, &page
);
185 ogg_stream_packetout(stream_in
, &packet
);
186 ogg_stream_packetin(stream_out
, &packet
);
189 while (ogg_stream_flush(stream_out
, &page
))
190 afi
->header_len
+= page
.body_len
+ page
.header_len
;
191 PARA_INFO_LOG("header_len = %d\n", afi
->header_len
);
192 ogg_save_header(map
, afi
);
195 ogg_stream_destroy(stream_in
);
196 ogg_stream_destroy(stream_out
);
198 ogg_sync_destroy(sync_in
);
199 vorbis_info_clear(&vi
);
200 vorbis_comment_clear(&vc
);
205 * Alloc and fill array table of byte offsets. chunk_table[i] is the
206 * offset in the current input file at which the sample containing time i *
207 * CHUNK_TIME begins. Always successful.
209 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
210 struct audio_format_info
*afi
, double time_total
)
213 ssize_t max_chunk_len
, pos
= 0, min
= 0, old_pos
;
214 long unsigned num_chunks
;
218 num
= time_total
/ chunk_time
+ 3;
219 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
221 afi
->chunk_table
= para_malloc(num
* sizeof(size_t));
222 afi
->chunk_table
[0] = 0;
224 for (i
= 1; ret
== 0; i
++) {
226 ret
= ov_time_seek(of
, i
* chunk_time
);
229 pos
= ov_raw_tell(of
);
230 diff
= pos
- old_pos
;
231 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
232 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
233 afi
->chunk_table
[i
] = pos
;
234 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
235 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
236 // "size: %zd\n", i - 1,
237 // i * chunk_time, pos, pos - old_pos);
241 afi
->chunk_table
[i
] = pos
;
242 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
243 num_chunks
, chunk_time
, max_chunk_len
, min
);
248 * Init oggvorbis file and write some tech data to given pointers.
250 static int ogg_get_file_info(FILE *file
, char *map
, off_t numbytes
,
251 struct audio_format_info
*afi
)
256 ogg_int64_t raw_total
;
257 long vi_sampling_rate
, vi_bitrate
;
259 const ov_callbacks ovc
= {
260 .read_func
= cb_read
,
261 .seek_func
= cb_seek
,
262 .close_func
= cb_close
,
265 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
267 ret
= ogg_compute_header_len(map
, numbytes
, afi
);
270 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
274 vi
= ov_info(&of
, 0);
277 time_total
= ov_time_total(&of
, -1);
278 raw_total
= ov_raw_total(&of
, -1);
279 afi
->seconds_total
= time_total
;
280 vi_sampling_rate
= vi
->rate
;
281 vi_bitrate
= ov_bitrate(&of
, 0);
282 afi
->chunks_total
= ogg_compute_chunk_table(&of
, afi
, time_total
);
283 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lu, %ldkHz, "
284 "%d channels, %ldkbps\n"
285 "audio_file_info2: \n"
286 "audio_file_info3: \n",
287 afi
->chunks_total
, (long unsigned) (chunk_time
* 1000 * 1000),
288 vi_sampling_rate
/ 1000, vi
->channels
, vi_bitrate
/ 1000
290 afi
->chunk_tv
.tv_sec
= 0;
291 afi
->chunk_tv
.tv_usec
= 250 * 1000;
292 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
295 ov_clear(&of
); /* keeps the file open */
301 static const char* ogg_suffixes
[] = {"ogg", NULL
};
304 * the init function of the ogg vorbis audio format handler
306 * \param afh pointer to the struct to initialize
308 void ogg_init(struct audio_format_handler
*afh
)
310 afh
->get_file_info
= ogg_get_file_info
,
311 afh
->suffixes
= ogg_suffixes
;