Add missing __printf_2_3 to all para_log functions.
[paraslash.git] / oggdec_filter.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
8
9 #include "para.h"
10
11 #include "oggdec_filter.cmdline.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "ggo.h"
15 #include "filter.h"
16 #include "error.h"
17 #include "string.h"
18
19 #include <vorbis/vorbisfile.h>
20
21 /** Determine byte sex. */
22 #ifdef WORDS_BIGENDIAN
23 #define ENDIAN 1
24 #else
25 #define ENDIAN 0
26 #endif
27
28 /** Data specific to the oggdec filter. */
29 struct private_oggdec_data {
30         /** Describes an ogg vorbis file. */
31         OggVorbis_File *vf;
32         /** The input buffer. */
33         char *inbuf;
34         /** The length of \a inbuf. */
35         size_t inbuf_len;
36         /** The number of bytes consumed from the input buffer. */
37         size_t converted;
38 };
39
40 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
41 {
42         struct filter_node *fn = datasource;
43         struct private_oggdec_data *pod = fn->private_data;
44         size_t ret, have = pod->inbuf_len - pod->converted;
45         char *p = pod->inbuf + pod->converted;
46
47 //      PARA_DEBUG_LOG("pod = %p\n", pod);
48 //      PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
49         if (pod->inbuf_len < size) {
50                 if (*fn->fc->input_error)
51                         return 0;
52                 errno = EAGAIN;
53                 return (size_t)-1;
54         }
55         ret = PARA_MIN(nmemb, have / size) * size;
56         memcpy(buf, p, ret);
57         pod->converted += ret;
58         return ret;
59 }
60
61 /*
62  * Custom data seeking function.
63  *
64  * Since we want the data source to be treated as unseekable at all
65  * times, the provided seek callback always returns -1 (failure).
66  */
67 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
68                 __a_unused int whence)
69 {
70         return -1;
71 }
72
73 static int cb_close(__a_unused void *datasource)
74 {
75         return 0;
76 }
77
78 static const ov_callbacks ovc = {
79         .read_func = cb_read,
80         .seek_func = cb_seek,
81         .close_func = cb_close,
82         /*
83          * The tell function need not be provided if the data IO abstraction is
84          * not seekable
85          */
86         .tell_func = NULL
87 };
88
89 static void ogg_open(struct filter_node *fn)
90 {
91         struct private_oggdec_data *pod = para_calloc(
92                 sizeof(struct private_oggdec_data));
93         struct oggdec_filter_args_info *conf = fn->conf;
94
95         fn->private_data = pod;
96         fn->bufsize = conf->bufsize_arg * 1024;
97         fn->buf = para_malloc(fn->bufsize);
98 }
99
100 static void ogg_close(struct filter_node *fn)
101 {
102         struct private_oggdec_data *pod = fn->private_data;
103         if (pod->vf) {
104                 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
105                 ov_clear(pod->vf);
106                 free(pod->vf);
107                 pod->vf = NULL;
108         } else
109                 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
110         free(fn->buf);
111         fn->buf = NULL;
112         free(fn->private_data);
113         fn->private_data = NULL;
114 }
115
116 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
117 {
118         ssize_t ret;
119         struct private_oggdec_data *pod = fn->private_data;
120         struct oggdec_filter_args_info *conf = fn->conf;
121         /* make the buffer known to the read callback cb_read() */
122         pod->inbuf = inbuffer;
123         pod->inbuf_len = len;
124         pod->converted = 0;
125
126         if (!pod->vf) {
127                 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
128                 if (len <ib && !*fn->fc->input_error) {
129                         PARA_DEBUG_LOG("initial input buffer %zd/%d, "
130                                 "waiting for more data\n", len, ib);
131                         return 0;
132                 }
133                 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
134                 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
135                 ret = ov_open_callbacks(fn, pod->vf,
136                         NULL, /* no initial buffer */
137                         0, /* no initial bytes */
138                         ovc); /* the ov_open_callbacks */
139                 if (ret == OV_EREAD)
140                         return -E_OGGDEC_READ;
141                 if (ret == OV_ENOTVORBIS)
142                         return -E_OGGDEC_NOTVORBIS;
143                 if (ret == OV_EVERSION)
144                         return -E_OGGDEC_VERSION;
145                 if (ret == OV_EBADHEADER)
146                         return -E_OGGDEC_BADHEADER;
147                 if (ret < 0)
148                         return -E_OGGDEC_FAULT;
149                 fn->fc->channels = ov_info(pod->vf, 0)->channels;
150                 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
151                 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
152                         fn->fc->samplerate);
153         }
154         while (fn->loaded < fn->bufsize) {
155                 int length = fn->bufsize - fn->loaded;
156                 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
157                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
158                 if (read_ret == OV_HOLE || !read_ret)
159                         return pod->converted;
160                 if (read_ret < 0)
161                         return -E_OGGDEC_BADLINK;
162                 fn->loaded += read_ret;
163         }
164         return pod->converted;
165 }
166
167 static int oggdec_parse_config(int argc, char **argv, void **config)
168 {
169         int ret;
170         struct oggdec_filter_args_info *ogg_conf;
171
172         ogg_conf = para_calloc(sizeof(*ogg_conf));
173         ret = -E_OGGDEC_SYNTAX;
174         if (oggdec_cmdline_parser(argc, argv, ogg_conf))
175                 goto err;
176         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
177         if (ogg_conf->bufsize_arg < 0)
178                 goto err;
179         if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
180                 goto err;
181         if (ogg_conf->initial_buffer_arg < 0)
182                 goto err;
183         if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
184                 goto err;
185         *config = ogg_conf;
186         return 1;
187 err:
188         free(ogg_conf);
189         return ret;
190 }
191
192 /**
193  * The init function of the ogg vorbis decoder.
194  *
195  * \param f Its fields are filled in by the function.
196  */
197 void oggdec_filter_init(struct filter *f)
198 {
199         struct oggdec_filter_args_info dummy;
200
201         oggdec_cmdline_parser_init(&dummy);
202         f->open = ogg_open;
203         f->close = ogg_close;
204         f->convert = ogg_convert;
205         f->parse_config = oggdec_parse_config;
206         f->help = (struct ggo_help) {
207                 .short_help = oggdec_filter_args_info_help,
208                 .detailed_help = oggdec_filter_args_info_detailed_help
209         };
210 }