2 * Copyright (C) 2004-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
11 #include <vorbis/codec.h>
12 #include <vorbis/vorbisfile.h>
19 /** must be big enough to hold header */
20 #define CHUNK_SIZE 32768
21 static double chunk_time
= 0.25;
23 /** describes a memory-mapped ogg vorbis file */
24 struct ogg_datasource
{
25 /** the memory mapping */
27 /** this size of the mapping */
29 /** the current position in the mapping */
33 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
35 struct ogg_datasource
*ods
= datasource
;
41 assert(ods
->numbytes
>= ods
->fpos
);
42 ret
= ods
->numbytes
- ods
->fpos
;
43 copy
= PARA_MIN(ret
, size
* nmemb
);
47 memcpy(buf
, ods
->map
+ ods
->fpos
, copy
);
48 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
49 ods
->fpos
+= ret
* size
;
53 static int cb_seek(void *datasource
, ogg_int64_t offset
,
56 struct ogg_datasource
*ods
= datasource
;
59 if (offset
>= 0 && offset
<= ods
->numbytes
) {
67 if (offset
<= 0 && -offset
<= ods
->numbytes
) {
68 ods
->fpos
= ods
->numbytes
+ offset
;
75 if ((offset
>= 0 && offset
+ ods
->fpos
> ods
->numbytes
) ||
76 (offset
< 0 && offset
+ ods
->fpos
< 0)) {
87 /* don't do anything as vss still needs the open filehandle */
88 static int cb_close(__a_unused
void *datasource
)
93 static long cb_tell(void *datasource
)
95 struct ogg_datasource
*ods
= datasource
;
96 return (unsigned long)ods
->fpos
;
99 static int ogg_open_callbacks(void *datasource
, OggVorbis_File
*vf
, ov_callbacks c
)
101 int ret
= ov_open_callbacks(datasource
, vf
,
102 NULL
, /* no initial buffer */
103 0, /* no initial bytes */
104 c
); /* the ov_open_callbacks */
108 if (ret
== OV_ENOTVORBIS
)
110 if (ret
== OV_EVERSION
)
111 return -E_OGG_VERSION
;
112 if (ret
== OV_EBADHEADER
)
113 return -E_OGG_BAD_HEADER
;
115 return -E_OGG_UNKNOWN_ERROR
;
120 static int ogg_compute_header_len(char *map
, size_t numbytes
,
121 struct afh_info
*afhi
)
124 size_t len
= PARA_MIN(numbytes
, (size_t)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) {
148 serial
= ogg_page_serialno(&page
);
149 ogg_stream_init(stream_in
, serial
);
150 ogg_stream_init(stream_out
, serial
);
151 ret
= ogg_stream_pagein(stream_in
, &page
);
153 ret
= -E_STREAM_PAGEIN
;
156 ret
= ogg_stream_packetout(stream_in
, &packet
);
158 ret
= -E_STREAM_PACKETOUT
;
162 if (vorbis_synthesis_headerin(&vi
, &vc
, &packet
) < 0)
164 PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi
.channels
, vi
.rate
);
165 ogg_stream_packetin(stream_out
, &packet
);
166 ret
= ogg_sync_pageout(sync_in
, &page
);
168 ret
= -E_SYNC_PAGEOUT
;
171 ogg_stream_pagein(stream_in
, &page
);
172 ogg_stream_packetout(stream_in
, &packet
);
173 ogg_stream_packetin(stream_out
, &packet
);
175 ret
= ogg_sync_pageout(sync_in
, &page
);
177 ret
= -E_SYNC_PAGEOUT
;
180 ogg_stream_pagein(stream_in
, &page
);
181 ogg_stream_packetout(stream_in
, &packet
);
182 ogg_stream_packetin(stream_out
, &packet
);
184 afhi
->header_len
= 0;
185 while (ogg_stream_flush(stream_out
, &page
))
186 afhi
->header_len
+= page
.body_len
+ page
.header_len
;
187 PARA_DEBUG_LOG("header_len = %d\n", afhi
->header_len
);
188 afhi
->header_offset
= 0;
191 ogg_stream_destroy(stream_in
);
192 ogg_stream_destroy(stream_out
);
194 ogg_sync_destroy(sync_in
);
195 vorbis_info_clear(&vi
);
196 vorbis_comment_clear(&vc
);
201 * Alloc and fill array table of byte offsets. chunk_table[i] is the
202 * offset in the current input file at which the sample containing time i *
203 * CHUNK_TIME begins. Always successful.
205 static long unsigned ogg_compute_chunk_table(OggVorbis_File
*of
,
206 struct afh_info
*afhi
, long unsigned time_total
)
209 long unsigned num_chunks
;
210 ogg_int64_t max
= 0, min
= 0, old_pos
= 0;
212 num
= time_total
/ chunk_time
+ 3;
213 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
215 afhi
->chunk_table
= para_malloc((num
+ 1) * sizeof(size_t));
216 afhi
->chunk_table
[0] = 0;
217 for (i
= 1; i
<= num
; i
++) {
218 ogg_int64_t diff
, pos
;
219 ret
= ov_time_seek(of
, i
* chunk_time
);
222 pos
= ov_raw_tell(of
);
223 diff
= pos
- old_pos
;
224 max
= PARA_MAX(max
, diff
);
225 min
= (i
== 1)? diff
: PARA_MIN(min
, diff
);
226 afhi
->chunk_table
[i
] = pos
;
230 PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %lld, min chunk: %lld\n",
231 num_chunks
, chunk_time
, (long long)max
, (long long)min
);
235 static void ogg_get_vorbis_comments(OggVorbis_File
*vf
, struct afh_info
*afhi
)
237 vorbis_comment
*vc
= ov_comment(vf
,-1);
241 afhi
->tags
.artist
= para_strdup(vorbis_comment_query(vc
, "artist", 0));
242 afhi
->tags
.title
= para_strdup(vorbis_comment_query(vc
, "title", 0));
243 afhi
->tags
.album
= para_strdup(vorbis_comment_query(vc
, "album", 0));
244 afhi
->tags
.year
= para_strdup(vorbis_comment_query(vc
, "year", 0));
245 afhi
->tags
.comment
= para_strdup(vorbis_comment_query(vc
, "comment", 0));
249 * Init oggvorbis file and write some tech data to given pointers.
251 static int ogg_get_file_info(char *map
, size_t numbytes
, __a_unused
int fd
,
252 struct afh_info
*afhi
)
257 const ov_callbacks ovc
= {
258 .read_func
= cb_read
,
259 .seek_func
= cb_seek
,
260 .close_func
= cb_close
,
263 struct ogg_datasource ods
= {.map
= map
, .numbytes
= numbytes
, .fpos
= 0};
265 ret
= ogg_compute_header_len(map
, numbytes
, afhi
);
268 ret
= ogg_open_callbacks(&ods
, &of
, ovc
);
272 vi
= ov_info(&of
, 0);
275 afhi
->seconds_total
= ov_time_total(&of
, -1);
276 afhi
->frequency
= vi
->rate
;
277 afhi
->bitrate
= ov_bitrate(&of
, 0) / 1000;
278 afhi
->channels
= vi
->channels
;
279 afhi
->chunks_total
= ogg_compute_chunk_table(&of
, afhi
, afhi
->seconds_total
);
280 afhi
->chunk_tv
.tv_sec
= 0;
281 afhi
->chunk_tv
.tv_usec
= 250 * 1000;
282 ogg_get_vorbis_comments(&of
, afhi
);
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
;