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"
33 /** must be big enough to hold header */
34 #define CHUNK_SIZE 32768
35 static double chunk_time
= 0.25;
37 static OggVorbis_File
*oggvorbis_file
;
39 static int header_len
, inbuf_size
, vi_channels
;
40 static char *header
, *inbuf
;
41 static ssize_t
*chunk_table
, max_chunk_len
, num_chunks
;
42 static struct audio_format_handler
*af
;
43 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)
86 PARA_INFO_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
87 ogg_stream_packetin(stream_out
, &packet
);
89 ret
= ogg_sync_pageout(sync_in
, &page
);
91 ret
= -E_SYNC_PAGEOUT
;
94 ogg_stream_pagein(stream_in
, &page
);
95 ogg_stream_packetout(stream_in
, &packet
);
96 ogg_stream_packetin(stream_out
, &packet
);
98 ret
= ogg_sync_pageout(sync_in
, &page
);
100 ret
= -E_SYNC_PAGEOUT
;
103 ogg_stream_pagein(stream_in
, &page
);
104 ogg_stream_packetout(stream_in
, &packet
);
105 ogg_stream_packetin(stream_out
, &packet
);
108 while (ogg_stream_flush(stream_out
, &page
))
109 header_len
+= page
.body_len
+ page
.header_len
;
111 PARA_INFO_LOG("header_len = %d\n", header_len
);
113 ogg_stream_destroy(stream_in
);
114 ogg_stream_destroy(stream_out
);
116 ogg_sync_destroy(sync_in
);
117 vorbis_info_clear(&vi
);
118 vorbis_comment_clear(&vc
);
122 static void tunetable(void)
124 int i
= 1, j
= -1, lp
= 1;
125 while (i
< num_chunks
) {
126 if (chunk_table
[i
] == chunk_table
[lp
]) {
131 tv_scale(i
, &af
->chunk_tv
, &af
->eof_tv
);
132 for (j
= lp
; j
< i
; j
++)
133 chunk_table
[j
] = chunk_table
[i
];
137 for (i
= 2; i
< num_chunks
; i
++)
138 if (chunk_table
[i
] != chunk_table
[1])
141 for (i
= 2; i
< num_chunks
- lp
; i
++)
142 chunk_table
[i
] = chunk_table
[i
+ lp
];
148 * Alloc and fill array table of byte offsets. chunk_table[i] is the
149 * offset in the current infile at which the sample containing time i *
152 static void ogg_compute_chunk_table(double time_total
)
155 ssize_t pos
= 0, min
= 0, old_pos
;
159 num
= time_total
/ chunk_time
+ 3;
160 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
162 chunk_table
= para_malloc(num
* sizeof(size_t));
166 for (i
= 1; ret
== 0; i
++) {
168 ret
= ov_time_seek(oggvorbis_file
, i
* chunk_time
);
171 pos
= ov_raw_tell(oggvorbis_file
);
172 diff
= pos
- old_pos
;
173 max_chunk_len
= PARA_MAX(max_chunk_len
, diff
);
174 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
175 chunk_table
[i
] = pos
;
176 if (i
< 11 || !((i
- 1) % 1000)|| i
> num
- 11)
177 PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
178 "size: %zd\n", i
- 1,
179 i
* chunk_time
, pos
, pos
- old_pos
);
183 chunk_table
[i
] = pos
;
185 PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
186 num_chunks
, chunk_time
, max_chunk_len
, min
);
190 static void ogg_close_audio_file(void)
192 if (oggvorbis_file
) {
193 PARA_DEBUG_LOG("%s", "ov_clear\n");
194 ov_clear(oggvorbis_file
);
195 free(oggvorbis_file
);
196 oggvorbis_file
= NULL
;
209 static int ogg_save_header(FILE *file
, int len
)
213 header
= para_malloc(len
);
215 ret
= read(fileno(file
), header
, len
);
222 * Init oggvorbis file and write some tech data to given pointers.
224 static int ogg_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
230 ogg_int64_t raw_total
;
234 return -E_OGG_NO_FILE
;
235 ret
= ogg_compute_header_len();
238 ret
= ogg_save_header(file
, header_len
);
242 oggvorbis_file
= para_malloc(sizeof(OggVorbis_File
));
243 ret
= ov_open(file
, oggvorbis_file
, NULL
, 0);
245 free(oggvorbis_file
);
250 vi
= ov_info(oggvorbis_file
, 0);
253 time_total
= ov_time_total(oggvorbis_file
, -1);
254 raw_total
= ov_raw_total(oggvorbis_file
, -1);
255 *seconds
= time_total
;
256 vi_sampling_rate
= vi
->rate
;
257 vi_bitrate
= ov_bitrate(oggvorbis_file
, 0);
258 vi_bitrate_nominal
= vi
->bitrate_nominal
;
259 vi_channels
= vi
->channels
;
260 ogg_compute_chunk_table(time_total
);
261 *frames
= num_chunks
;
262 sprintf(info_str
, "audio_file_info1:%zu x %lu, %ldkHz, %d channels, %ldkbps\n"
263 "audio_file_info2: \n"
264 "audio_file_info3: \n",
265 num_chunks
, (long unsigned) (chunk_time
* 1000 * 1000),
266 vi_sampling_rate
/ 1000, vi_channels
, vi_bitrate
/ 1000
271 ogg_close_audio_file();
275 static char *ogg_read_chunk(long unsigned current_chunk
, ssize_t
*len
)
281 if (current_chunk
>= num_chunks
)
283 *len
= chunk_table
[current_chunk
+ 1] - chunk_table
[current_chunk
];
284 if (!*len
) /* nothing to send for this run */
286 pos
= chunk_table
[current_chunk
];
287 if (inbuf_size
< *len
) {
288 PARA_INFO_LOG("increasing inbuf for chunk #%lu/%zu to %zd bytes\n",
289 current_chunk
, num_chunks
, *len
);
290 inbuf
= para_realloc(inbuf
, *len
);
293 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
295 ret
= fseek(infile
, pos
, SEEK_SET
);
298 ret
= para_fread(inbuf
, *len
, 1, infile
);
301 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
302 // (long unsigned) inbuf[4]);
303 return (char *)inbuf
;
306 static char *ogg_get_header_info(int *len
)
312 static const char* ogg_suffixes
[] = {"ogg", NULL
};
315 * the init function of the ogg vorbis audio format handler
317 * \param p pointer to the struct to initialize
319 void ogg_init(struct audio_format_handler
*p
)
322 af
->get_file_info
= ogg_get_file_info
,
323 af
->read_chunk
= ogg_read_chunk
;
324 af
->close_audio_file
= ogg_close_audio_file
;
325 af
->get_header_info
= ogg_get_header_info
;
326 af
->chunk_tv
.tv_sec
= 0;
327 af
->chunk_tv
.tv_usec
= 250 * 1000;
328 tv_scale(3, &af
->chunk_tv
, &af
->eof_tv
);
329 af
->suffixes
= ogg_suffixes
;