1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/***
This file is part of libcanberra.
Copyright 2008 Lennart Poettering
libcanberra is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 2.1 of the
License, or (at your option) any later version.
libcanberra is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with libcanberra. If not, If not, see
<http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vorbis/vorbisfile.h>
#include <vorbis/codec.h>
#include "canberra.h"
#include "read-vorbis.h"
#include "macro.h"
#include "malloc.h"
#define FILE_SIZE_MAX (64U*1024U*1024U)
struct ca_vorbis {
OggVorbis_File ovf;
size_t size;
};
static int convert_error(int or) {
switch (or) {
case OV_ENOSEEK:
case OV_EBADPACKET:
case OV_EBADLINK:
case OV_EFAULT:
case OV_EREAD:
case OV_HOLE:
return CA_ERROR_IO;
case OV_EIMPL:
case OV_EVERSION:
case OV_ENOTAUDIO:
return CA_ERROR_NOTSUPPORTED;
case OV_ENOTVORBIS:
case OV_EBADHEADER:
case OV_EOF:
return CA_ERROR_CORRUPT;
case OV_EINVAL:
return CA_ERROR_INVALID;
default:
return CA_ERROR_IO;
}
}
int ca_vorbis_open(ca_vorbis **_v, FILE *f) {
int ret, or;
ca_vorbis *v;
int64_t n;
ca_return_val_if_fail(_v, CA_ERROR_INVALID);
ca_return_val_if_fail(f, CA_ERROR_INVALID);
if (!(v = ca_new0(ca_vorbis, 1)))
return CA_ERROR_OOM;
if ((or = ov_open(f, &v->ovf, NULL, 0)) < 0) {
ret = convert_error(or);
goto fail;
}
if ((n = ov_pcm_total(&v->ovf, -1)) < 0) {
ret = convert_error(or);
ov_clear(&v->ovf);
goto fail;
}
if (n * sizeof(int16_t) > FILE_SIZE_MAX) {
ret = CA_ERROR_TOOBIG;
ov_clear(&v->ovf);
goto fail;
}
v->size = n * sizeof(int16_t) * ca_vorbis_get_nchannels(v);
*_v = v;
return CA_SUCCESS;
fail:
ca_free(v);
return ret;
}
void ca_vorbis_close(ca_vorbis *v) {
ca_assert(v);
ov_clear(&v->ovf);
ca_free(v);
}
unsigned ca_vorbis_get_nchannels(ca_vorbis *v) {
const vorbis_info *vi;
ca_assert(v);
ca_assert_se(vi = ov_info(&v->ovf, -1));
return vi->channels;
}
unsigned ca_vorbis_get_rate(ca_vorbis *v) {
const vorbis_info *vi;
ca_assert(v);
ca_assert_se(vi = ov_info(&v->ovf, -1));
return (unsigned) vi->rate;
}
int ca_vorbis_read_s16ne(ca_vorbis *v, int16_t *d, unsigned *n){
long r;
int section;
int length;
size_t n_read = 0;
ca_return_val_if_fail(v, CA_ERROR_INVALID);
ca_return_val_if_fail(d, CA_ERROR_INVALID);
ca_return_val_if_fail(n, CA_ERROR_INVALID);
ca_return_val_if_fail(*n > 0, CA_ERROR_INVALID);
length = *n * sizeof(int16_t);
do {
r = ov_read(&v->ovf, (char*) d, length,
#ifdef WORDS_BIGENDIAN
1,
#else
0,
#endif
2, 1, §ion);
if (r < 0)
return convert_error(r);
if (r == 0)
break;
/* We only read the first section */
if (section != 0)
break;
length -= r;
d += r/sizeof(int16_t);
n_read += r;
} while (length >= 4096);
ca_assert(v->size >= n_read);
v->size -= n_read;
*n = (unsigned) n_read/sizeof(int16_t);
return CA_SUCCESS;
}
size_t ca_vorbis_get_size(ca_vorbis *v) {
ca_return_val_if_fail(v, CA_ERROR_INVALID);
return v->size;
}
|