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
));
101 fn
->private_data
= pod
;
105 static void ogg_close(struct filter_node
*fn
)
107 struct private_oggdec_data
*pod
= fn
->private_data
;
109 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod
->vf
, pod
);
114 PARA_DEBUG_LOG("nothing to close\n");
115 free(fn
->private_data
);
116 fn
->private_data
= NULL
;
119 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
121 static int oggdec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
123 struct filter_node
*fn
= btr_context(btrn
);
124 struct private_oggdec_data
*pod
= fn
->private_data
;
126 if (!strcmp(cmd
, "samplerate")) {
127 if (pod
->samplerate
== 0)
128 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
129 *result
= make_message("%u", pod
->samplerate
);
132 if (!strcmp(cmd
, "channels")) {
133 if (pod
->channels
== 0)
134 return -ERRNO_TO_PARA_ERROR(ENAVAIL
);
135 *result
= make_message("%u", pod
->channels
);
138 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);
141 static int ogg_init(struct filter_node
*fn
)
143 struct private_oggdec_data
*pod
= fn
->private_data
;
144 struct btr_node
*btrn
= fn
->btrn
;
147 struct timeval delay
= {0, 500 * 1000};
149 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
150 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
151 btr_get_input_queue_size(btrn
), fn
->min_iqs
);
153 oret
= ov_open_callbacks(fn
, pod
->vf
,
154 NULL
, /* no initial buffer */
155 0, /* no initial bytes */
156 ovc
); /* the ov_open_callbacks */
157 if (oret
== OV_ENOTVORBIS
|| oret
== OV_EBADHEADER
) {
158 /* this might be due to the input buffer being too small */
159 if (!btr_no_parent(btrn
)) {
161 iqs
= btr_get_input_queue_size(btrn
);
163 if (iqs
< fn
->min_iqs
)
165 PARA_CRIT_LOG("iqs: %zu\n", iqs
);
166 btr_merge(btrn
, fn
->min_iqs
);
170 ret
= (oret
== OV_ENOTVORBIS
)?
171 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
174 ret
= -E_OGGDEC_READ
;
175 if (oret
== OV_EREAD
)
177 ret
= -E_OGGDEC_VERSION
;
178 if (oret
== OV_EVERSION
)
180 ret
= -E_OGGDEC_FAULT
;
183 pod
->channels
= ov_info(pod
->vf
, 0)->channels
;
184 pod
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
185 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
187 /* wait a bit to avoid buffer underruns */
188 tv_add(now
, &delay
, &pod
->stream_start
);
195 btr_consume(btrn
, pod
->converted
);
202 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
204 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
205 struct private_oggdec_data
*pod
= fn
->private_data
;
206 struct btr_node
*btrn
= fn
->btrn
;
213 ret
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
216 btr_merge(btrn
, fn
->min_iqs
);
217 len
= btr_next_buffer(btrn
, &in
);
218 iqs
= btr_get_input_queue_size(btrn
);
225 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
226 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
227 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
228 btr_consume(btrn
, pod
->converted
);
233 if (btr_no_parent(btrn
))
240 if (read_ret
== OV_HOLE
)
242 ret
= -E_OGGDEC_BADLINK
;
245 btr_add_output(out
, read_ret
, btrn
);
246 if (btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
) == 0)
247 return; /* enough data for the moment */
252 btr_remove_node(btrn
);
256 static int oggdec_parse_config(int argc
, char **argv
, void **config
)
259 struct oggdec_filter_args_info
*ogg_conf
;
261 ogg_conf
= para_calloc(sizeof(*ogg_conf
));
262 ret
= -E_OGGDEC_SYNTAX
;
263 if (oggdec_cmdline_parser(argc
, argv
, ogg_conf
))
265 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
266 if (ogg_conf
->bufsize_arg
< 0)
268 if (ogg_conf
->bufsize_arg
>= INT_MAX
/ 1024)
270 if (ogg_conf
->initial_buffer_arg
< 0)
272 if (ogg_conf
->initial_buffer_arg
>= INT_MAX
/ 1024)
281 static void oggdec_free_config(void *conf
)
283 oggdec_cmdline_parser_free(conf
);
287 * The init function of the ogg vorbis decoder.
289 * \param f Its fields are filled in by the function.
291 void oggdec_filter_init(struct filter
*f
)
293 struct oggdec_filter_args_info dummy
;
295 oggdec_cmdline_parser_init(&dummy
);
297 f
->close
= ogg_close
;
298 f
->pre_select
= generic_filter_pre_select
;
299 f
->post_select
= ogg_post_select
;
300 f
->parse_config
= oggdec_parse_config
;
301 f
->free_config
= oggdec_free_config
;
302 f
->execute
= oggdec_execute
;
303 f
->help
= (struct ggo_help
) {
304 .short_help
= oggdec_filter_args_info_help
,
305 .detailed_help
= oggdec_filter_args_info_detailed_help