2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
10 #include <vorbis/vorbisfile.h>
14 #include "oggdec_filter.cmdline.h"
18 #include "buffer_tree.h"
23 /** Determine byte sex. */
24 #ifdef WORDS_BIGENDIAN
30 /** Data specific to the oggdec filter. */
31 struct private_oggdec_data
{
32 /** Describes an ogg vorbis file. */
34 /** The input buffer. */
36 /** The length of \a inbuf. */
38 /** The number of bytes consumed from the input buffer. */
40 /** When to start producing output. */
41 struct timeval stream_start
;
42 /** The number of channels of the current stream. */
43 unsigned int channels
;
44 /** Current sample rate in Hz. */
45 unsigned int samplerate
;
48 static size_t cb_read_nobtr(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
50 struct filter_node
*fn
= datasource
;
51 struct private_oggdec_data
*pod
= fn
->private_data
;
52 size_t ret
, have
= pod
->inbuf_len
- pod
->converted
;
53 char *p
= pod
->inbuf
+ pod
->converted
;
55 // PARA_DEBUG_LOG("pod = %p\n", pod);
56 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
57 if (pod
->inbuf_len
< size
) {
58 if (*fn
->fc
->input_error
)
63 ret
= PARA_MIN(nmemb
, have
/ size
) * size
;
65 pod
->converted
+= ret
;
69 static size_t cb_read_btr(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
71 struct filter_node
*fn
= datasource
;
72 struct btr_node
*btrn
= fn
->btrn
;
76 * oggvorbis always uses size == 1. Other sizes would complicate the code
77 * for no real gain. So we simply don't support size != 1.
80 //PARA_DEBUG_LOG("vorbis requests %zu x %zu = %zu bytes\n", size, nmemb, size * nmemb);
84 size_t nbytes
= btr_next_buffer(btrn
, &btr_buf
);
87 nbytes
= PARA_MIN(nmemb
- copied
, nbytes
);
88 memcpy(buf
+ copied
, btr_buf
, nbytes
);
90 btr_consume(btrn
, nbytes
);
97 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
99 struct filter_node
*fn
= datasource
;
102 return cb_read_btr(buf
, size
, nmemb
, datasource
);
104 return cb_read_nobtr(buf
, size
, nmemb
, datasource
);
108 * Custom data seeking function.
110 * Since we want the data source to be treated as unseekable at all
111 * times, the provided seek callback always returns -1 (failure).
113 static int cb_seek(__a_unused
void *datasource
, __a_unused ogg_int64_t offset
,
114 __a_unused
int whence
)
119 static int cb_close(__a_unused
void *datasource
)
124 static const ov_callbacks ovc
= {
125 .read_func
= cb_read
,
126 .seek_func
= cb_seek
,
127 .close_func
= cb_close
,
129 * The tell function need not be provided if the data IO abstraction is
135 static void ogg_open(struct filter_node
*fn
)
137 struct private_oggdec_data
*pod
= para_calloc(
138 sizeof(struct private_oggdec_data
));
139 struct oggdec_filter_args_info
*conf
= fn
->conf
;
141 fn
->private_data
= pod
;
142 fn
->bufsize
= conf
->bufsize_arg
* 1024;
143 fn
->buf
= para_malloc(fn
->bufsize
);
146 static void ogg_close(struct filter_node
*fn
)
148 struct private_oggdec_data
*pod
= fn
->private_data
;
150 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod
->vf
, pod
);
155 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod
->vf
, pod
);
158 free(fn
->private_data
);
159 fn
->private_data
= NULL
;
162 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
164 static int oggdec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
166 struct filter_node
*fn
= btr_context(btrn
);
167 struct private_oggdec_data
*pod
= fn
->private_data
;
169 if (!strcmp(cmd
, "samplerate")) {
170 if (pod
->samplerate
== 0)
171 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
172 *result
= make_message("%u", pod
->samplerate
);
175 if (!strcmp(cmd
, "channels")) {
176 if (pod
->channels
== 0)
177 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
178 *result
= make_message("%u", pod
->channels
);
181 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);
184 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
186 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
187 struct private_oggdec_data
*pod
= fn
->private_data
;
188 struct btr_node
*btrn
= fn
->btrn
;
194 ret
= btr_node_status(btrn
, fn
->min_iqs
);
199 btr_merge(btrn
, fn
->min_iqs
);
200 len
= btr_next_buffer(btrn
, &in
);
201 iqs
= btr_get_input_queue_size(btrn
);
205 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
206 PARA_NOTICE_LOG("input queue: %zu, opening ov callbacks\n", iqs
);
207 oret
= ov_open_callbacks(fn
, pod
->vf
,
208 NULL
, /* no initial buffer */
209 0, /* no initial bytes */
210 ovc
); /* the ov_open_callbacks */
211 if (oret
== OV_ENOTVORBIS
|| oret
== OV_EBADHEADER
) {
212 /* this might be due to the input buffer being too small */
213 if (!btr_no_parent(btrn
)) {
216 fn
->min_iqs
= iqs
+ 1;
219 ret
= (oret
== OV_ENOTVORBIS
)?
220 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
223 ret
= -E_OGGDEC_READ
;
224 if (oret
== OV_EREAD
)
226 ret
= -E_OGGDEC_VERSION
;
227 if (oret
== OV_EVERSION
)
229 ret
= -E_OGGDEC_FAULT
;
232 pod
->channels
= ov_info(pod
->vf
, 0)->channels
;
233 pod
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
234 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
236 ///* wait a bit to avoid buffer underruns */
237 //tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
241 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
242 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
243 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
248 if (btr_no_parent(btrn
))
252 if (read_ret
== OV_HOLE
)
255 ret
= -E_OGGDEC_BADLINK
;
258 btr_add_output(out
, read_ret
, btrn
);
268 static ssize_t
ogg_convert(char *inbuffer
, size_t len
, struct filter_node
*fn
)
271 struct private_oggdec_data
*pod
= fn
->private_data
;
272 struct oggdec_filter_args_info
*conf
= fn
->conf
;
273 /* make the buffer known to the read callback cb_read() */
274 pod
->inbuf
= inbuffer
;
275 pod
->inbuf_len
= len
;
279 if (*fn
->fc
->input_error
< 0)
280 return *fn
->fc
->input_error
;
283 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
284 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len
);
285 ret
= ov_open_callbacks(fn
, pod
->vf
,
286 NULL
, /* no initial buffer */
287 0, /* no initial bytes */
288 ovc
); /* the ov_open_callbacks */
289 if (ret
== OV_ENOTVORBIS
|| ret
== OV_EBADHEADER
) {
290 /* this might be due to the input buffer being too small */
291 int ib
= 1024 * conf
->initial_buffer_arg
; /* initial buffer */
293 PARA_INFO_LOG("initial input buffer %zd/%d, "
294 "waiting for more data\n", len
, ib
);
299 return ret
== OV_ENOTVORBIS
?
300 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
303 return -E_OGGDEC_READ
;
304 if (ret
== OV_EVERSION
)
305 return -E_OGGDEC_VERSION
;
307 return -E_OGGDEC_FAULT
;
308 fn
->fc
->channels
= ov_info(pod
->vf
, 0)->channels
;
309 fn
->fc
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
310 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn
->fc
->channels
,
312 /* wait a bit to avoid buffer underruns */
313 tv_add(now
, &(struct timeval
){0, 500 * 1000}, &pod
->stream_start
);
314 return pod
->converted
;
316 if (tv_diff(now
, &pod
->stream_start
, NULL
) < 0) {
317 PARA_DEBUG_LOG("initial delay..\n");
320 while (fn
->loaded
< fn
->bufsize
) {
321 int length
= fn
->bufsize
- fn
->loaded
;
322 long read_ret
= ov_read(pod
->vf
, fn
->buf
+ fn
->loaded
, length
,
323 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
325 return pod
->converted
;
326 if (read_ret
== OV_HOLE
) {
328 PARA_INFO_LOG("hole, delaying playback\n");
329 tv_add(now
, &(struct timeval
){0, 500 * 1000}, &pod
->stream_start
);
331 return pod
->converted
;
334 return -E_OGGDEC_BADLINK
;
335 fn
->loaded
+= read_ret
;
337 return pod
->converted
;
340 static int oggdec_parse_config(int argc
, char **argv
, void **config
)
343 struct oggdec_filter_args_info
*ogg_conf
;
345 ogg_conf
= para_calloc(sizeof(*ogg_conf
));
346 ret
= -E_OGGDEC_SYNTAX
;
347 if (oggdec_cmdline_parser(argc
, argv
, ogg_conf
))
349 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
350 if (ogg_conf
->bufsize_arg
< 0)
352 if (ogg_conf
->bufsize_arg
>= INT_MAX
/ 1024)
354 if (ogg_conf
->initial_buffer_arg
< 0)
356 if (ogg_conf
->initial_buffer_arg
>= INT_MAX
/ 1024)
366 * The init function of the ogg vorbis decoder.
368 * \param f Its fields are filled in by the function.
370 void oggdec_filter_init(struct filter
*f
)
372 struct oggdec_filter_args_info dummy
;
374 oggdec_cmdline_parser_init(&dummy
);
376 f
->close
= ogg_close
;
377 f
->convert
= ogg_convert
;
378 f
->pre_select
= generic_filter_pre_select
;
379 f
->post_select
= ogg_post_select
;
380 f
->parse_config
= oggdec_parse_config
;
381 f
->execute
= oggdec_execute
;
382 f
->help
= (struct ggo_help
) {
383 .short_help
= oggdec_filter_args_info_help
,
384 .detailed_help
= oggdec_filter_args_info_detailed_help