2 * Copyright (C) 2005-2010 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 /** The number of channels of the current stream. */
37 unsigned int channels
;
38 /** Current sample rate in Hz. */
39 unsigned int samplerate
;
42 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
44 struct filter_node
*fn
= datasource
;
45 struct private_oggdec_data
*pod
= fn
->private_data
;
46 struct btr_node
*btrn
= fn
->btrn
;
48 size_t nbytes
= btr_next_buffer(btrn
, &btr_buf
), tmp
;
51 * oggvorbis always uses size == 1. Other sizes would complicate the code
52 * for no real gain. So we simply don't support size != 1.
55 assert(pod
->converted
<= nbytes
);
56 tmp
= nbytes
- pod
->converted
;
57 PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb
, tmp
);
58 tmp
= PARA_MIN(tmp
, nmemb
);
61 memcpy(buf
, btr_buf
+ pod
->converted
, tmp
);
62 pod
->converted
+= tmp
;
67 * Custom data seeking function.
69 * Since we want the data source to be treated as unseekable at all
70 * times, the provided seek callback always returns -1 (failure).
72 static int cb_seek(__a_unused
void *datasource
, __a_unused ogg_int64_t offset
,
73 __a_unused
int whence
)
78 static int cb_close(__a_unused
void *datasource
)
83 static const ov_callbacks ovc
= {
86 .close_func
= cb_close
,
88 * The tell function need not be provided if the data IO abstraction is
94 static void ogg_open(struct filter_node
*fn
)
96 struct private_oggdec_data
*pod
= para_calloc(
97 sizeof(struct private_oggdec_data
));
99 fn
->private_data
= pod
;
103 static void ogg_close(struct filter_node
*fn
)
105 struct private_oggdec_data
*pod
= fn
->private_data
;
107 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod
->vf
, pod
);
112 PARA_DEBUG_LOG("nothing to close\n");
113 free(fn
->private_data
);
114 fn
->private_data
= NULL
;
117 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
119 static int oggdec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
121 struct filter_node
*fn
= btr_context(btrn
);
122 struct private_oggdec_data
*pod
= fn
->private_data
;
124 if (!strcmp(cmd
, "samplerate")) {
125 if (pod
->samplerate
== 0)
126 return -E_BTR_NAVAIL
;
127 *result
= make_message("%u", pod
->samplerate
);
130 if (!strcmp(cmd
, "channels")) {
131 if (pod
->channels
== 0)
132 return -E_BTR_NAVAIL
;
133 *result
= make_message("%u", pod
->channels
);
136 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);
139 static int ogg_init(struct filter_node
*fn
)
141 struct private_oggdec_data
*pod
= fn
->private_data
;
142 struct btr_node
*btrn
= fn
->btrn
;
146 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
147 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
148 btr_get_input_queue_size(btrn
), fn
->min_iqs
);
150 oret
= ov_open_callbacks(fn
, pod
->vf
,
151 NULL
, /* no initial buffer */
152 0, /* no initial bytes */
153 ovc
); /* the ov_open_callbacks */
154 if (oret
== OV_ENOTVORBIS
|| oret
== OV_EBADHEADER
) {
155 /* this might be due to the input buffer being too small */
156 if (!btr_no_parent(btrn
)) {
158 iqs
= btr_get_input_queue_size(btrn
);
160 if (iqs
< fn
->min_iqs
)
162 PARA_CRIT_LOG("iqs: %zu\n", iqs
);
163 btr_merge(btrn
, fn
->min_iqs
);
167 ret
= (oret
== OV_ENOTVORBIS
)?
168 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
171 ret
= -E_OGGDEC_READ
;
172 if (oret
== OV_EREAD
)
174 ret
= -E_OGGDEC_VERSION
;
175 if (oret
== OV_EVERSION
)
177 ret
= -E_OGGDEC_FAULT
;
180 pod
->channels
= ov_info(pod
->vf
, 0)->channels
;
181 pod
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
182 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
190 btr_consume(btrn
, pod
->converted
);
197 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
199 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
200 struct private_oggdec_data
*pod
= fn
->private_data
;
201 struct btr_node
*btrn
= fn
->btrn
;
206 ret
= ns
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
210 btr_merge(btrn
, fn
->min_iqs
);
216 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
217 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
218 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
219 btr_consume(btrn
, pod
->converted
);
224 if (read_ret
== 0 || read_ret
== OV_HOLE
)
226 ret
= -E_OGGDEC_BADLINK
;
229 btr_add_output(out
, read_ret
, btrn
);
230 if (btr_get_output_queue_size(btrn
) > 128 * 1024)
231 return; /* enough data for the moment */
236 btr_remove_node(btrn
);
240 static int oggdec_parse_config(int argc
, char **argv
, void **config
)
243 struct oggdec_filter_args_info
*ogg_conf
;
245 ogg_conf
= para_calloc(sizeof(*ogg_conf
));
246 ret
= -E_OGGDEC_SYNTAX
;
247 if (oggdec_cmdline_parser(argc
, argv
, ogg_conf
))
249 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
250 if (ogg_conf
->bufsize_arg
< 0)
252 if (ogg_conf
->bufsize_arg
>= INT_MAX
/ 1024)
254 if (ogg_conf
->initial_buffer_arg
< 0)
256 if (ogg_conf
->initial_buffer_arg
>= INT_MAX
/ 1024)
265 static void oggdec_free_config(void *conf
)
267 oggdec_cmdline_parser_free(conf
);
271 * The init function of the ogg vorbis decoder.
273 * \param f Its fields are filled in by the function.
275 void oggdec_filter_init(struct filter
*f
)
277 struct oggdec_filter_args_info dummy
;
279 oggdec_cmdline_parser_init(&dummy
);
281 f
->close
= ogg_close
;
282 f
->pre_select
= generic_filter_pre_select
;
283 f
->post_select
= ogg_post_select
;
284 f
->parse_config
= oggdec_parse_config
;
285 f
->free_config
= oggdec_free_config
;
286 f
->execute
= oggdec_execute
;
287 f
->help
= (struct ggo_help
) {
288 .short_help
= oggdec_filter_args_info_help
,
289 .detailed_help
= oggdec_filter_args_info_detailed_help