Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/OggDecoder.java
1461 views
1
package org.newdawn.slick.openal;
2
3
import java.io.ByteArrayOutputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.nio.ByteBuffer;
7
8
/**
9
* Decode an OGG file to PCM data
10
*
11
* @author Kevin Glass
12
*/
13
public class OggDecoder {
14
/** The conversion buffer size */
15
private int convsize = 4096 * 4;
16
/** The buffer used to read OGG file */
17
private byte[] convbuffer = new byte[convsize]; // take 8k out of the data segment, not the stack
18
19
/**
20
* Create a new OGG decoder
21
*/
22
public OggDecoder() {
23
}
24
25
/**
26
* Get the data out of an OGG file
27
*
28
* @param input The input stream from which to read the OGG file
29
* @return The data describing the OGG thats been read
30
* @throws IOException Indicaites a failure to read the OGG file
31
*/
32
public OggData getData(InputStream input) throws IOException {
33
if (input == null) {
34
throw new IOException("Failed to read OGG, source does not exist?");
35
}
36
ByteArrayOutputStream dataout = new ByteArrayOutputStream();
37
38
// SyncState oy = new SyncState(); // sync and verify incoming physical bitstream
39
// StreamState os = new StreamState(); // take physical pages, weld into a logical stream of packets
40
// Page og = new Page(); // one Ogg bitstream page. Vorbis packets are inside
41
// Packet op = new Packet(); // one raw packet of data for decode
42
//
43
// Info vi = new Info(); // struct that stores all the static vorbis bitstream settings
44
// Comment vc = new Comment(); // struct that stores all the bitstream user comments
45
// DspState vd = new DspState(); // central working state for the packet->PCM decoder
46
// Block vb = new Block(vd); // local working space for packet->PCM decode
47
//
48
// byte[] buffer;
49
// int bytes = 0;
50
//
51
// boolean bigEndian = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN);
52
// // Decode setup
53
//
54
// oy.init(); // Now we can read pages
55
//
56
// while (true) { // we repeat if the bitstream is chained
57
// int eos = 0;
58
//
59
// // grab some data at the head of the stream. We want the first page
60
// // (which is guaranteed to be small and only contain the Vorbis
61
// // stream initial header) We need the first page to get the stream
62
// // serialno.
63
//
64
// // submit a 4k block to libvorbis' Ogg layer
65
// int index = oy.buffer(4096);
66
//
67
// buffer = oy.data;
68
// try {
69
// bytes = input.read(buffer, index, 4096);
70
// } catch (Exception e) {
71
// Log.error("Failure reading in vorbis");
72
// Log.error(e);
73
// System.exit(0);
74
// }
75
// oy.wrote(bytes);
76
//
77
// // Get the first page.
78
// if (oy.pageout(og) != 1) {
79
// // have we simply run out of data? If so, we're done.
80
// if (bytes < 4096)
81
// break;
82
//
83
// // error case. Must not be Vorbis data
84
// Log.error("Input does not appear to be an Ogg bitstream.");
85
// System.exit(0);
86
// }
87
//
88
// // Get the serial number and set up the rest of decode.
89
// // serialno first; use it to set up a logical stream
90
// os.init(og.serialno());
91
//
92
// // extract the initial header from the first page and verify that the
93
// // Ogg bitstream is in fact Vorbis data
94
//
95
// // I handle the initial header first instead of just having the code
96
// // read all three Vorbis headers at once because reading the initial
97
// // header is an easy way to identify a Vorbis bitstream and it's
98
// // useful to see that functionality seperated out.
99
//
100
// vi.init();
101
// vc.init();
102
// if (os.pagein(og) < 0) {
103
// // error; stream version mismatch perhaps
104
// Log.error("Error reading first page of Ogg bitstream data.");
105
// System.exit(0);
106
// }
107
//
108
// if (os.packetout(op) != 1) {
109
// // no page? must not be vorbis
110
// Log.error("Error reading initial header packet.");
111
// System.exit(0);
112
// }
113
//
114
// if (vi.synthesis_headerin(vc, op) < 0) {
115
// // error case; not a vorbis header
116
// Log.error("This Ogg bitstream does not contain Vorbis audio data.");
117
// System.exit(0);
118
// }
119
//
120
// // At this point, we're sure we're Vorbis. We've set up the logical
121
// // (Ogg) bitstream decoder. Get the comment and codebook headers and
122
// // set up the Vorbis decoder
123
//
124
// // The next two packets in order are the comment and codebook headers.
125
// // They're likely large and may span multiple pages. Thus we reead
126
// // and submit data until we get our two pacakets, watching that no
127
// // pages are missing. If a page is missing, error out; losing a
128
// // header page is the only place where missing data is fatal. */
129
//
130
// int i = 0;
131
// while (i < 2) {
132
// while (i < 2) {
133
//
134
// int result = oy.pageout(og);
135
// if (result == 0)
136
// break; // Need more data
137
// // Don't complain about missing or corrupt data yet. We'll
138
// // catch it at the packet output phase
139
//
140
// if (result == 1) {
141
// os.pagein(og); // we can ignore any errors here
142
// // as they'll also become apparent
143
// // at packetout
144
// while (i < 2) {
145
// result = os.packetout(op);
146
// if (result == 0)
147
// break;
148
// if (result == -1) {
149
// // Uh oh; data at some point was corrupted or missing!
150
// // We can't tolerate that in a header. Die.
151
// Log.error("Corrupt secondary header. Exiting.");
152
// System.exit(0);
153
// }
154
// vi.synthesis_headerin(vc, op);
155
// i++;
156
// }
157
// }
158
// }
159
// // no harm in not checking before adding more
160
// index = oy.buffer(4096);
161
// buffer = oy.data;
162
// try {
163
// bytes = input.read(buffer, index, 4096);
164
// } catch (Exception e) {
165
// Log.error("Failed to read Vorbis: ");
166
// Log.error(e);
167
// System.exit(0);
168
// }
169
// if (bytes == 0 && i < 2) {
170
// Log.error("End of file before finding all Vorbis headers!");
171
// System.exit(0);
172
// }
173
// oy.wrote(bytes);
174
// }
175
//
176
// convsize = 4096 / vi.channels;
177
//
178
// // OK, got and parsed all three headers. Initialize the Vorbis
179
// // packet->PCM decoder.
180
// vd.synthesis_init(vi); // central decode state
181
// vb.init(vd); // local state for most of the decode
182
// // so multiple block decodes can
183
// // proceed in parallel. We could init
184
// // multiple vorbis_block structures
185
// // for vd here
186
//
187
// float[][][] _pcm = new float[1][][];
188
// int[] _index = new int[vi.channels];
189
// // The rest is just a straight decode loop until end of stream
190
// while (eos == 0) {
191
// while (eos == 0) {
192
//
193
// int result = oy.pageout(og);
194
// if (result == 0)
195
// break; // need more data
196
// if (result == -1) { // missing or corrupt data at this page position
197
// Log.error("Corrupt or missing data in bitstream; continuing...");
198
// } else {
199
// os.pagein(og); // can safely ignore errors at
200
// // this point
201
// while (true) {
202
// result = os.packetout(op);
203
//
204
// if (result == 0)
205
// break; // need more data
206
// if (result == -1) { // missing or corrupt data at this page position
207
// // no reason to complain; already complained above
208
// } else {
209
// // we have a packet. Decode it
210
// int samples;
211
// if (vb.synthesis(op) == 0) { // test for success!
212
// vd.synthesis_blockin(vb);
213
// }
214
//
215
// // **pcm is a multichannel float vector. In stereo, for
216
// // example, pcm[0] is left, and pcm[1] is right. samples is
217
// // the size of each channel. Convert the float values
218
// // (-1.<=range<=1.) to whatever PCM format and write it out
219
//
220
// while ((samples = vd.synthesis_pcmout(_pcm,
221
// _index)) > 0) {
222
// float[][] pcm = _pcm[0];
223
// //boolean clipflag = false;
224
// int bout = (samples < convsize ? samples
225
// : convsize);
226
//
227
// // convert floats to 16 bit signed ints (host order) and
228
// // interleave
229
// for (i = 0; i < vi.channels; i++) {
230
// int ptr = i * 2;
231
// //int ptr=i;
232
// int mono = _index[i];
233
// for (int j = 0; j < bout; j++) {
234
// int val = (int) (pcm[i][mono + j] * 32767.);
235
// // short val=(short)(pcm[i][mono+j]*32767.);
236
// // int val=(int)Math.round(pcm[i][mono+j]*32767.);
237
// // might as well guard against clipping
238
// if (val > 32767) {
239
// val = 32767;
240
// //clipflag = true;
241
// }
242
// if (val < -32768) {
243
// val = -32768;
244
// //clipflag = true;
245
// }
246
// if (val < 0)
247
// val = val | 0x8000;
248
//
249
// if (bigEndian) {
250
// convbuffer[ptr] = (byte) (val >>> 8);
251
// convbuffer[ptr + 1] = (byte) (val);
252
// } else {
253
// convbuffer[ptr] = (byte) (val);
254
// convbuffer[ptr + 1] = (byte) (val >>> 8);
255
// }
256
// ptr += 2 * (vi.channels);
257
// }
258
// }
259
//
260
// dataout.write(convbuffer, 0, 2 * vi.channels * bout);
261
//
262
// vd.synthesis_read(bout); // tell libvorbis how
263
// // many samples we
264
// // actually consumed
265
// }
266
// }
267
// }
268
// if (og.eos() != 0)
269
// eos = 1;
270
// }
271
// }
272
// if (eos == 0) {
273
// index = oy.buffer(4096);
274
// if (index >= 0) {
275
// buffer = oy.data;
276
// try {
277
// bytes = input.read(buffer, index, 4096);
278
// } catch (Exception e) {
279
// Log.error("Failure during vorbis decoding");
280
// Log.error(e);
281
// return null;
282
// }
283
// } else {
284
// bytes = 0;
285
// }
286
// oy.wrote(bytes);
287
// if (bytes == 0)
288
// eos = 1;
289
// }
290
// }
291
//
292
// // clean up this logical bitstream; before exit we see if we're
293
// // followed by another [chained]
294
//
295
// os.clear();
296
//
297
// // ogg_page and ogg_packet structs always point to storage in
298
// // libvorbis. They're never freed or manipulated directly
299
//
300
// vb.clear();
301
// vd.clear();
302
// vi.clear(); // must be called last
303
// }
304
//
305
// // OK, clean up the framer
306
// oy.clear();
307
// OggData ogg = new OggData();
308
// ogg.channels = vi.channels;
309
// ogg.rate = vi.rate;
310
311
OggInputStream oggInput = new OggInputStream(input);
312
313
boolean done = false;
314
while (!oggInput.atEnd()) {
315
dataout.write(oggInput.read());
316
}
317
318
OggData ogg = new OggData();
319
ogg.channels = oggInput.getChannels();
320
ogg.rate = oggInput.getRate();
321
322
byte[] data = dataout.toByteArray();
323
ogg.data = ByteBuffer.allocateDirect(data.length);
324
ogg.data.put(data);
325
ogg.data.rewind();
326
327
return ogg;
328
}
329
}
330
331