2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file oggdec.c paraslash's ogg vorbis decoder */
23 #include "oggdec_filter.cmdline.h"
29 #include <vorbis/vorbisfile.h>
31 /** \cond some internal constants */
37 /** data specific to the oggdec filter */
38 struct private_oggdec_data
{
39 /** describes an ogg vorbis file */
41 /** the input buffer */
43 /** the length of \a inbuf */
45 /** the number of bytes consumed from the input buffer */
49 static size_t cb_read(void *buf
, size_t size
, size_t nmemb
, void *datasource
)
51 struct filter_node
*fn
= datasource
;
52 struct private_oggdec_data
*pod
= fn
->private_data
;
53 size_t ret
, have
= pod
->inbuf_len
- pod
->converted
;
54 char *p
= pod
->inbuf
+ pod
->converted
;
58 // PARA_DEBUG_LOG("pod = %p\n", pod);
59 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
60 if (pod
->inbuf_len
< size
) {
64 ret
= MIN(nmemb
, have
/ size
) * size
;
66 pod
->converted
+= ret
;
71 * cb_seek -- custom data seeking function
72 * Since we want the data source to be treated as unseekable at all
73 * times, the provided seek callback always returns -1 (failure).
75 static int cb_seek(__unused
void *datasource
, __unused ogg_int64_t offset
,
81 static int cb_close(__unused
void *datasource
)
86 static const ov_callbacks ovc
= {
89 .close_func
= cb_close
,
91 * The tell function need not be provided if
92 * the data IO abstraction is not seekable
97 static void ogg_open(struct filter_node
*fn
)
99 struct private_oggdec_data
*pod
= para_calloc(
100 sizeof(struct private_oggdec_data
));
101 struct oggdec_filter_args_info
*conf
= fn
->conf
;
103 fn
->private_data
= pod
;
104 fn
->bufsize
= conf
->bufsize_arg
* 1024;
105 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 in fc %p, pod = %p\n", pod
->vf
, pod
);
120 free(fn
->private_data
);
121 fn
->private_data
= NULL
;
124 static ssize_t
ogg_convert(char *inbuffer
, size_t len
, struct filter_node
*fn
)
127 struct private_oggdec_data
*pod
= fn
->private_data
;
128 struct oggdec_filter_args_info
*conf
= fn
->conf
;
129 /* make the buffer known to the read callback cb_read() */
130 pod
->inbuf
= inbuffer
;
131 pod
->inbuf_len
= len
;
135 int ib
= 1024 * conf
->initial_buffer_arg
; /* initial buffer */
136 if (len
<ib
&& !*fn
->fci
->eof
&& !fn
->fci
->error
) {
137 PARA_INFO_LOG("initial input buffer %zd/%d, waiting for more data\n",
141 pod
->vf
= para_malloc(sizeof(struct OggVorbis_File
));
142 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len
);
143 ret
= ov_open_callbacks(fn
, pod
->vf
,
144 NULL
, /* no initial buffer */
145 0, /* no initial bytes */
146 ovc
); /* the ov_open_callbacks */
148 return -E_OGGDEC_READ
;
149 if (ret
== OV_ENOTVORBIS
)
150 return -E_OGGDEC_NOTVORBIS
;
151 if (ret
== OV_EVERSION
)
152 return -E_OGGDEC_VERSION
;
153 if (ret
== OV_EBADHEADER
)
154 return -E_OGGDEC_BADHEADER
;
156 return -E_OGGDEC_FAULT
;
157 fn
->fci
->channels
= ov_info(pod
->vf
, 0)->channels
;
158 fn
->fci
->samplerate
= ov_info(pod
->vf
, 0)->rate
;
159 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn
->fci
->channels
, fn
->fci
->samplerate
);
162 ret
= ov_read(pod
->vf
, fn
->buf
+ fn
->loaded
, fn
->bufsize
- fn
->loaded
,
163 ENDIAN
, BITS
/ 8, SIGN
, NULL
);
164 if (ret
== OV_HOLE
|| !ret
) {
165 return pod
->converted
;
168 return -E_OGGDEC_BADLINK
;
170 if (!*fn
->fci
->eof
&& !fn
->fci
->error
&& fn
->loaded
< fn
->bufsize
)
172 return pod
->converted
;
175 static void *oggdec_parse_config(int argc
, char **argv
)
177 struct oggdec_filter_args_info
*ret
= para_calloc(sizeof(struct oggdec_filter_args_info
));
178 if (!oggdec_cmdline_parser(argc
, argv
, ret
))
184 /** the init function of the ogg vorbis decoder */
185 void oggdec_init(struct filter
*f
)
188 f
->close
= ogg_close
;
189 f
->convert
= ogg_convert
;
190 f
->print_help
= oggdec_cmdline_parser_print_help
;
191 f
->parse_config
= oggdec_parse_config
;