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 return decoder_execute(cmd
, pod
->sample_rate
, pod
->channels
, result
);
127 static int ogg_init(struct filter_node
*fn
)
129 struct private_oggdec_data
*pod
= fn
->private_data
;
130 struct btr_node
*btrn
= fn
->btrn
;
134 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
135 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
136 btr_get_input_queue_size(btrn
), fn
->min_iqs
);
138 oret
= ov_open_callbacks(fn
, pod
->vf
,
139 NULL
, /* no initial buffer */
140 0, /* no initial bytes */
141 ovc
); /* the ov_open_callbacks */
142 if (oret
== OV_ENOTVORBIS
|| oret
== OV_EBADHEADER
) {
143 /* this might be due to the input buffer being too small */
144 if (!btr_no_parent(btrn
)) {
146 iqs
= btr_get_input_queue_size(btrn
);
148 if (iqs
< fn
->min_iqs
)
150 PARA_CRIT_LOG("iqs: %zu\n", iqs
);
151 btr_merge(btrn
, fn
->min_iqs
);
155 ret
= (oret
== OV_ENOTVORBIS
)?
156 -E_OGGDEC_NOTVORBIS
: -E_OGGDEC_BADHEADER
;
159 ret
= -E_OGGDEC_READ
;
160 if (oret
== OV_EREAD
)
162 ret
= -E_OGGDEC_VERSION
;
163 if (oret
== OV_EVERSION
)
165 ret
= -E_OGGDEC_FAULT
;
168 pod
->channels
= ov_info(pod
->vf
, 0)->channels
;
169 pod
->sample_rate
= ov_info(pod
->vf
, 0)->rate
;
170 tv_add(now
, &(struct timeval
)EMBRACE(0, 300 * 1000), &pod
->stream_start
);
171 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod
->channels
,
179 btr_consume(btrn
, pod
->converted
);
186 static void ogg_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
188 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
189 struct private_oggdec_data
*pod
= fn
->private_data
;
193 ret
= btr_node_status(fn
->btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
197 sched_request_barrier(&pod
->stream_start
, s
);
200 static void ogg_post_select(__a_unused
struct sched
*s
, struct task
*t
)
202 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
203 struct private_oggdec_data
*pod
= fn
->private_data
;
204 struct btr_node
*btrn
= fn
->btrn
;
207 if (tv_diff(&pod
->stream_start
, now
, NULL
) > 0)
211 ret
= ns
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
215 btr_merge(btrn
, fn
->min_iqs
);
221 char *out
= para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE
);
222 ssize_t read_ret
= ov_read(pod
->vf
, out
, OGGDEC_OUTPUT_CHUNK_SIZE
,
223 ENDIAN
, 2 /* 16 bit */, 1 /* signed */, NULL
);
224 btr_consume(btrn
, pod
->converted
);
229 if (read_ret
== 0 || read_ret
== OV_HOLE
)
231 ret
= -E_OGGDEC_BADLINK
;
234 btr_add_output(out
, read_ret
, btrn
);
235 if (btr_get_output_queue_size(btrn
) > 128 * 1024)
236 return; /* enough data for the moment */
241 btr_remove_node(btrn
);
246 * The init function of the ogg vorbis decoder.
248 * \param f Its fields are filled in by the function.
250 void oggdec_filter_init(struct filter
*f
)
253 f
->close
= ogg_close
;
254 f
->pre_select
= ogg_pre_select
;
255 f
->post_select
= ogg_post_select
;
256 f
->execute
= oggdec_execute
;