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 number of bytes consumed from the input buffer. */
36 /** When to start producing output. */
37 struct timeval stream_start
;
38 /** The number of channels of the current stream. */
39 unsigned int channels
;
40 /** Current sample rate in Hz. */
41 unsigned int samplerate
;
44 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
46 struct filter_node
*fn
= datasource
;
47 struct private_oggdec_data
*pod
= fn
->private_data
;
48 struct btr_node
*btrn
= fn
->btrn
;
50 size_t nbytes
= btr_next_buffer(btrn
, &btr_buf
), tmp
;
53 * oggvorbis always uses size == 1. Other sizes would complicate the code
54 * for no real gain. So we simply don't support size != 1.
57 assert(pod
->converted
<= nbytes
);
58 tmp
= nbytes
- pod
->converted
;
59 PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb
, tmp
);
60 tmp
= PARA_MIN(tmp
, nmemb
);
63 memcpy(buf
, btr_buf
+ pod
->converted
, tmp
);
64 pod
->converted
+= tmp
;
69 * Custom data seeking function.
71 * Since we want the data source to be treated as unseekable at all
72 * times, the provided seek callback always returns -1 (failure).
74 static int cb_seek(__a_unused
void *datasource
, __a_unused ogg_int64_t offset
,
75 __a_unused
int whence
)
80 static int cb_close(__a_unused
void *datasource
)
85 static const ov_callbacks ovc
= {
88 .close_func
= cb_close
,
90 * The tell function need not be provided if the data IO abstraction is
96 static void ogg_open(struct filter_node
*fn
)
98 struct private_oggdec_data
*pod
= para_calloc(
99 sizeof(struct private_oggdec_data
));
100 struct oggdec_filter_args_info
*conf
= fn
->conf
;
102 fn
->private_data
= pod
;
103 fn
->bufsize
= conf
->bufsize_arg
* 1024;
104 fn
->buf
= para_malloc(fn
->bufsize
);
108 static void ogg_close(struct filter_node
*fn
)
110 struct private_oggdec_data
*pod
= fn
->private_data
;
112 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod
->vf
, pod
);
117 PARA_DEBUG_LOG("nothing to close\n");
120 free(fn
->private_data
);
121 fn
->private_data
= NULL
;
124 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
126 static int oggdec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
128 struct filter_node
*fn
= btr_context(btrn
);
129 struct private_oggdec_data
*pod
= fn
->private_data
;
131 if (!strcmp(cmd
, "samplerate")) {
132 if (pod
->samplerate
== 0)
133 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
134 *result
= make_message("%u", pod
->samplerate
);
137 if (!strcmp(cmd
, "channels")) {
138 if (pod
->channels
== 0)
139 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
140 *result
= make_message("%u", pod
->channels
);
143 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);
146 static int ogg_init(struct filter_node
*fn
)
148 struct private_oggdec_data
*pod
= fn
->private_data
;
149 struct btr_node
*btrn
= fn
->btrn
;
152 struct timeval delay
= {0, 500 * 1000};
154 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
155 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
156 btr_get_input_queue_size(btrn
), fn
->min_iqs
);
158 oret
= ov_open_callbacks(fn
, pod
->vf
,
159 NULL
, /* no initial buffer */
160 0, /* no initial bytes */
161 ovc
); /* the ov_open_callbacks */
162 if (oret
== OV_ENOTVORBIS
|| oret
== OV_EBADHEADER
) {
163 /* this might be due to the input buffer being too small */
164 if (!btr_no_parent(btrn
)) {
166 iqs
= btr_get_input_queue_size(btrn
);
168 if (iqs
< fn
->min_iqs
)
170 PARA_CRIT_LOG("iqs: %zu\n", iqs
);
171 btr_merge(btrn
, fn
->min_iqs
);
175 ret
= (oret
== OV_ENOTVORBIS
)?
176 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
179 ret
= -E_OGGDEC_READ
;
180 if (oret
== OV_EREAD
)
182 ret
= -E_OGGDEC_VERSION
;
183 if (oret
== OV_EVERSION
)
185 ret
= -E_OGGDEC_FAULT
;
188 pod
->channels
= ov_info(pod
->vf
, 0)->channels
;
189 pod
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
190 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
192 /* wait a bit to avoid buffer underruns */
193 tv_add(now
, &delay
, &pod
->stream_start
);
200 btr_consume(btrn
, pod
->converted
);
207 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
209 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
210 struct private_oggdec_data
*pod
= fn
->private_data
;
211 struct btr_node
*btrn
= fn
->btrn
;
218 ret
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
221 btr_merge(btrn
, fn
->min_iqs
);
222 len
= btr_next_buffer(btrn
, &in
);
223 iqs
= btr_get_input_queue_size(btrn
);
230 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
231 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
232 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
233 btr_consume(btrn
, pod
->converted
);
238 if (btr_no_parent(btrn
))
245 if (read_ret
== OV_HOLE
)
247 ret
= -E_OGGDEC_BADLINK
;
250 btr_add_output(out
, read_ret
, btrn
);
251 if (btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
) == 0)
252 return; /* enough data for the moment */
257 btr_remove_node(btrn
);
261 static int oggdec_parse_config(int argc
, char **argv
, void **config
)
264 struct oggdec_filter_args_info
*ogg_conf
;
266 ogg_conf
= para_calloc(sizeof(*ogg_conf
));
267 ret
= -E_OGGDEC_SYNTAX
;
268 if (oggdec_cmdline_parser(argc
, argv
, ogg_conf
))
270 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
271 if (ogg_conf
->bufsize_arg
< 0)
273 if (ogg_conf
->bufsize_arg
>= INT_MAX
/ 1024)
275 if (ogg_conf
->initial_buffer_arg
< 0)
277 if (ogg_conf
->initial_buffer_arg
>= INT_MAX
/ 1024)
286 static void oggdec_free_config(void *conf
)
288 oggdec_cmdline_parser_free(conf
);
292 * The init function of the ogg vorbis decoder.
294 * \param f Its fields are filled in by the function.
296 void oggdec_filter_init(struct filter
*f
)
298 struct oggdec_filter_args_info dummy
;
300 oggdec_cmdline_parser_init(&dummy
);
302 f
->close
= ogg_close
;
303 f
->pre_select
= generic_filter_pre_select
;
304 f
->post_select
= ogg_post_select
;
305 f
->parse_config
= oggdec_parse_config
;
306 f
->free_config
= oggdec_free_config
;
307 f
->execute
= oggdec_execute
;
308 f
->help
= (struct ggo_help
) {
309 .short_help
= oggdec_filter_args_info_help
,
310 .detailed_help
= oggdec_filter_args_info_detailed_help