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"
32 /** must be big enough to hold header */
33 #define CHUNK_SIZE 32768
34 static double chunk_time
= 0.25;
36 static OggVorbis_File
*oggvorbis_file
;
38 static int header_len
, oggbuf_len
, vi_channels
;
39 static char *header
, *oggbuf
;
40 static ssize_t
*chunk_table
, max_chunk_len
;
41 static struct audio_format_handler
*af
;
42 static long vi_sampling_rate
, vi_bitrate
, vi_bitrate_nominal
,
45 static int ogg_compute_header_len(void)
47 int ret
, len
, in
= fileno(infile
);
54 ogg_stream_state
*stream_in
= para_malloc(sizeof(ogg_stream_state
));
55 ogg_stream_state
*stream_out
= para_malloc(sizeof(ogg_stream_state
));
56 ogg_sync_state
*sync_in
= para_malloc(sizeof(ogg_sync_state
));
58 ogg_sync_init(sync_in
);
59 vorbis_info_init(&vi
);
60 vorbis_comment_init(&vc
);
61 buf
= ogg_sync_buffer(sync_in
, CHUNK_SIZE
);
62 len
= read(in
, buf
, CHUNK_SIZE
);
66 ogg_sync_wrote(sync_in
, len
);
67 ret
= -E_SYNC_PAGEOUT
;
68 if (ogg_sync_pageout(sync_in
, &page
) <= 0)
70 serial
= ogg_page_serialno(&page
);
71 ogg_stream_init(stream_in
, serial
);
72 ogg_stream_init(stream_out
, serial
);
73 ret
= ogg_stream_pagein(stream_in
, &page
);
75 ret
= E_STREAM_PAGEIN
;
78 ret
= ogg_stream_packetout(stream_in
, &packet
);
80 ret
= -E_STREAM_PACKETOUT
;
84 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0) {
87 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
,
89 ogg_stream_packetin(stream_out
, &packet
);
91 ret
= ogg_sync_pageout(sync_in
, &page
);
93 ret
= -E_SYNC_PAGEOUT
;
96 ogg_stream_pagein(stream_in
, &page
);
97 ogg_stream_packetout(stream_in
, &packet
);
98 ogg_stream_packetin(stream_out
, &packet
);
100 ret
= ogg_sync_pageout(sync_in
, &page
);
102 ret
= -E_SYNC_PAGEOUT
;
105 ogg_stream_pagein(stream_in
, &page
);
106 ogg_stream_packetout(stream_in
, &packet
);
107 ogg_stream_packetin(stream_out
, &packet
);
110 while (ogg_stream_flush(stream_out
, &page
))
111 header_len
+= page
.body_len
+ page
.header_len
;
113 PARA_INFO_LOG("header_len = %d\n", header_len
);
115 ogg_stream_destroy(stream_in
);
116 ogg_stream_destroy(stream_out
);
118 ogg_sync_destroy(sync_in
);
119 vorbis_info_clear(&vi
);
120 vorbis_comment_clear(&vc
);
124 static void tunetable(void)
126 int i
= 1, j
= -1, lp
= 1;
127 while (i
< num_chunks
) {
128 if (chunk_table
[i
] == chunk_table
[lp
]) {
133 tv_scale(i
, &af
->chunk_tv
, &af
->eof_tv
);
134 for (j
= lp
; j
< i
; j
++)
135 chunk_table
[j
] = chunk_table
[i
];
139 for (i
= 2; i
< num_chunks
; i
++)
140 if (chunk_table
[i
] != chunk_table
[1])
143 for (i
= 2; i
< num_chunks
- lp
; i
++)
144 chunk_table
[i
] = chunk_table
[i
+ lp
];
150 * Alloc and fill array table of byte offsets. chunk_table[i] is the
151 * offset in the current infile at which the sample containing time i *
154 static void ogg_compute_chunk_table(double time_total
)
157 ssize_t pos
= 0, min
= 0, old_pos
;
161 num
= time_total
/ chunk_time
+ 3;
162 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
164 chunk_table
= para_malloc(num
* sizeof(ogg_int64_t
));
168 for (i
= 1; ret
== 0; i
++) {
170 ret
= ov_time_seek(oggvorbis_file
, i
* chunk_time
);
173 pos
= ov_raw_tell(oggvorbis_file
);
174 diff
= pos
- old_pos
;
175 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
176 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
177 chunk_table
[i
] = pos
;
178 if (i
< 11 || !((i
- 1) % 1000)|| i
> num
- 11)
179 PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
180 "size: %zd\n", i
- 1,
181 i
* chunk_time
, pos
, pos
- old_pos
);
185 chunk_table
[i
] = pos
;
187 PARA_INFO_LOG("%li chunks (%fs), max chunk: %zd, min chunk: %zd\n",
188 num_chunks
, chunk_time
, max_chunk_len
, min
);
192 static void ogg_close_audio_file(void)
194 if (oggvorbis_file
) {
195 PARA_DEBUG_LOG("%s", "ov_clear\n");
196 ov_clear(oggvorbis_file
);
197 free(oggvorbis_file
);
198 oggvorbis_file
= NULL
;
211 static int ogg_save_header(FILE *file
, int len
)
215 header
= para_malloc(len
);
217 ret
= read(fileno(file
), header
, len
);
224 * Init oggvorbis file and write some tech data to given pointers.
226 static int ogg_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
232 ogg_int64_t raw_total
;
236 return -E_OGG_NO_FILE
;
237 ret
= ogg_compute_header_len();
240 ret
= ogg_save_header(file
, header_len
);
244 oggvorbis_file
= para_malloc(sizeof(OggVorbis_File
));
245 ret
= ov_open(file
, oggvorbis_file
, NULL
, 0);
247 free(oggvorbis_file
);
252 vi
= ov_info(oggvorbis_file
, 0);
255 time_total
= ov_time_total(oggvorbis_file
, -1);
256 raw_total
= ov_raw_total(oggvorbis_file
, -1);
257 *seconds
= time_total
;
258 vi_sampling_rate
= vi
->rate
;
259 vi_bitrate
= ov_bitrate(oggvorbis_file
, 0);
260 vi_bitrate_nominal
= vi
->bitrate_nominal
;
261 vi_channels
= vi
->channels
;
262 ogg_compute_chunk_table(time_total
);
263 *frames
= num_chunks
;
264 sprintf(info_str
, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
265 "audio_file_info2: \n"
266 "audio_file_info3: \n",
267 num_chunks
, (long unsigned) (chunk_time
* 1000 * 1000),
268 vi_sampling_rate
/ 1000, vi_channels
, vi_bitrate
/ 1000
273 ogg_close_audio_file();
278 * Simple stream reposition routine
280 static int ogg_reposition_stream(long unsigned request
)
285 seek
= chunk_table
[request
];
286 ret
= fseek(infile
, seek
, SEEK_SET
);
287 PARA_DEBUG_LOG("seek to %li returned %d. offset:%li\n", seek
,
289 return ret
< 0? -E_OGG_REPOS
: 1;
292 static ogg_int64_t
get_chunk_size(long unsigned chunk_num
)
295 if (chunk_num
>= num_chunks
)
297 ret
= chunk_table
[chunk_num
+ 1] - chunk_table
[chunk_num
];
301 PARA_DEBUG_LOG("chunk %d: %lli-%lli (%lli bytes)\n",
303 chunk_table
[chunk_num
],
304 chunk_table
[chunk_num
+ 1] - 1,
310 static char *ogg_read_chunk(long unsigned current_chunk
, ssize_t
*len
)
313 ogg_int64_t cs
= get_chunk_size(current_chunk
);
314 if (!cs
) { /* nothing to send for this run */
318 if (cs
< 0) { /* eof */
323 if (!oggbuf
|| oggbuf_len
< *len
) {
324 PARA_INFO_LOG("increasing ogg buffer size (%d -> %zu)\n",
326 oggbuf
= para_realloc(oggbuf
, *len
);
329 ret
= read(fileno(infile
), oggbuf
, *len
);
339 PARA_WARNING_LOG("short read (%d/%zd)\n", ret
, *len
);
344 static char *ogg_get_header_info(int *len
)
350 static const char* ogg_suffixes
[] = {"ogg", NULL
};
353 * the init function of the ogg vorbis audio format handler
355 * \param p pointer to the struct to initialize
357 void ogg_init(struct audio_format_handler
*p
)
360 af
->reposition_stream
= ogg_reposition_stream
;
361 af
->get_file_info
= ogg_get_file_info
,
362 af
->read_chunk
= ogg_read_chunk
;
363 af
->close_audio_file
= ogg_close_audio_file
;
364 af
->get_header_info
= ogg_get_header_info
;
365 af
->chunk_tv
.tv_sec
= 0;
366 af
->chunk_tv
.tv_usec
= 250 * 1000;
367 tv_scale(3, &af
->chunk_tv
, &af
->eof_tv
);
368 af
->suffixes
= ogg_suffixes
;