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>
17 #include "buffer_tree.h"
22 /** Determine byte sex. */
23 #ifdef WORDS_BIGENDIAN
29 /** Data specific to the oggdec filter. */
30 struct private_oggdec_data
{
31 /** Describes an ogg vorbis file. */
33 /** The number of bytes consumed from the input buffer. */
35 /** The number of channels of the current stream. */
36 unsigned int channels
;
37 /** Current sample rate in Hz. */
38 struct timeval stream_start
;
39 unsigned int sample_rate
;
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
, "sample_rate")) {
125 if (pod
->sample_rate
== 0)
126 return -E_BTR_NAVAIL
;
127 *result
= make_message("%u", pod
->sample_rate
);
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
->sample_rate
= ov_info(pod
->vf
, 0)->rate
;
182 tv_add(now
, &(struct timeval
)EMBRACE(0, 300 * 1000), &pod
->stream_start
);
183 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
191 btr_consume(btrn
, pod
->converted
);
198 static void ogg_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
200 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
201 struct private_oggdec_data
*pod
= fn
->private_data
;
205 ret
= btr_node_status(fn
->btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
209 sched_request_barrier(&pod
->stream_start
, s
);
212 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
214 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
215 struct private_oggdec_data
*pod
= fn
->private_data
;
216 struct btr_node
*btrn
= fn
->btrn
;
219 if (tv_diff(&pod
->stream_start
, now
, NULL
) > 0)
223 ret
= ns
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
227 btr_merge(btrn
, fn
->min_iqs
);
233 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
234 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
235 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
236 btr_consume(btrn
, pod
->converted
);
241 if (read_ret
== 0 || read_ret
== OV_HOLE
)
243 ret
= -E_OGGDEC_BADLINK
;
246 btr_add_output(out
, read_ret
, btrn
);
247 if (btr_get_output_queue_size(btrn
) > 128 * 1024)
248 return; /* enough data for the moment */
253 btr_remove_node(btrn
);
258 * The init function of the ogg vorbis decoder.
260 * \param f Its fields are filled in by the function.
262 void oggdec_filter_init(struct filter
*f
)
265 f
->close
= ogg_close
;
266 f
->pre_select
= ogg_pre_select
;
267 f
->post_select
= ogg_post_select
;
268 f
->execute
= oggdec_execute
;