Remove the likely/unlikely macros.
[paraslash.git] / oggdec_filter.c
1 /*
2  * Copyright (C) 2005-2009 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 <regex.h>
10 #include <vorbis/vorbisfile.h>
11
12 #include "para.h"
13 #include "oggdec_filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.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 (*fn->fc->input_error < 0)
129                         return *fn->fc->input_error;
130                 if (len < ib) {
131                         PARA_DEBUG_LOG("initial input buffer %zd/%d, "
132                                 "waiting for more data\n", len, ib);
133                         return 0;
134                 }
135                 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
136                 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
137                 ret = ov_open_callbacks(fn, pod->vf,
138                         NULL, /* no initial buffer */
139                         0, /* no initial bytes */
140                         ovc); /* the ov_open_callbacks */
141                 if (ret == OV_EREAD)
142                         return -E_OGGDEC_READ;
143                 if (ret == OV_ENOTVORBIS)
144                         return -E_OGGDEC_NOTVORBIS;
145                 if (ret == OV_EVERSION)
146                         return -E_OGGDEC_VERSION;
147                 if (ret == OV_EBADHEADER)
148                         return -E_OGGDEC_BADHEADER;
149                 if (ret < 0)
150                         return -E_OGGDEC_FAULT;
151                 fn->fc->channels = ov_info(pod->vf, 0)->channels;
152                 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
153                 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
154                         fn->fc->samplerate);
155         }
156         while (fn->loaded < fn->bufsize) {
157                 int length = fn->bufsize - fn->loaded;
158                 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
159                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
160                 if (read_ret == OV_HOLE || !read_ret)
161                         return pod->converted;
162                 if (read_ret < 0)
163                         return -E_OGGDEC_BADLINK;
164                 fn->loaded += read_ret;
165         }
166         return pod->converted;
167 }
168
169 static int oggdec_parse_config(int argc, char **argv, void **config)
170 {
171         int ret;
172         struct oggdec_filter_args_info *ogg_conf;
173
174         ogg_conf = para_calloc(sizeof(*ogg_conf));
175         ret = -E_OGGDEC_SYNTAX;
176         if (oggdec_cmdline_parser(argc, argv, ogg_conf))
177                 goto err;
178         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
179         if (ogg_conf->bufsize_arg < 0)
180                 goto err;
181         if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
182                 goto err;
183         if (ogg_conf->initial_buffer_arg < 0)
184                 goto err;
185         if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
186                 goto err;
187         *config = ogg_conf;
188         return 1;
189 err:
190         free(ogg_conf);
191         return ret;
192 }
193
194 /**
195  * The init function of the ogg vorbis decoder.
196  *
197  * \param f Its fields are filled in by the function.
198  */
199 void oggdec_filter_init(struct filter *f)
200 {
201         struct oggdec_filter_args_info dummy;
202
203         oggdec_cmdline_parser_init(&dummy);
204         f->open = ogg_open;
205         f->close = ogg_close;
206         f->convert = ogg_convert;
207         f->parse_config = oggdec_parse_config;
208         f->help = (struct ggo_help) {
209                 .short_help = oggdec_filter_args_info_help,
210                 .detailed_help = oggdec_filter_args_info_detailed_help
211         };
212 }