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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
* $ProjectName$
* $ProjectRevision$
* -----------------------------------------------------------
* $Id$
* -----------------------------------------------------------
*
* $Author$
*
* Description:
*
* Copyright 2002-2003 Tor-Einar Jarnbjo
* -----------------------------------------------------------
*
* Change History
* -----------------------------------------------------------
* $Log$
* Revision 1.1 2005/07/11 15:42:36 hcl
* Songdb java version, source. only 1.5 compatible
*
* Revision 1.1.1.1 2004/04/04 22:09:12 shred
* First Import
*
* Revision 1.3 2003/04/10 19:48:22 jarnbjo
* no message
*
* Revision 1.2 2003/03/31 00:23:04 jarnbjo
* no message
*
* Revision 1.1 2003/03/03 21:02:20 jarnbjo
* no message
*
*/
package de.jarnbjo.ogg;
import java.io.*;
import de.jarnbjo.util.io.*;
/**
* <p>An instance of this class represents an ogg page read from an ogg file
* or network stream. It has no public constructor, but instances can be
* created by the <code>create</code> methods, supplying a JMF stream or
* a <code>RandomAccessFile</code>
* which is positioned at the beginning of an Ogg page.</p>
*
* <p>Furtheron, the class provides methods for accessing the raw page data,
* as well as data attributes like segmenting information, sequence number,
* stream serial number, chechsum and wether this page is the beginning or
* end of a logical bitstream (BOS, EOS) and if the page data starts with a
* continued packet or a fresh data packet.</p>
*/
public class OggPage {
private int version;
private boolean continued, bos, eos;
private long absoluteGranulePosition;
private int streamSerialNumber, pageSequenceNumber, pageCheckSum;
private int[] segmentOffsets;
private int[] segmentLengths;
private int totalLength;
private byte[] header, segmentTable, data;
protected OggPage() {
}
private OggPage(
int version,
boolean continued,
boolean bos,
boolean eos,
long absoluteGranulePosition,
int streamSerialNumber,
int pageSequenceNumber,
int pageCheckSum,
int[] segmentOffsets,
int[] segmentLengths,
int totalLength,
byte[] header,
byte[] segmentTable,
byte[] data) {
this.version=version;
this.continued=continued;
this.bos=bos;
this.eos=eos;
this.absoluteGranulePosition=absoluteGranulePosition;
this.streamSerialNumber=streamSerialNumber;
this.pageSequenceNumber=pageSequenceNumber;
this.pageCheckSum=pageCheckSum;
this.segmentOffsets=segmentOffsets;
this.segmentLengths=segmentLengths;
this.totalLength=totalLength;
this.header=header;
this.segmentTable=segmentTable;
this.data=data;
}
/**
* this method equals to create(RandomAccessFile source, false)
*
* @see #create(RandomAccessFile, boolean)
*/
public static OggPage create(RandomAccessFile source) throws IOException, EndOfOggStreamException, OggFormatException {
return create(source, false);
}
/**
* This method is called to read data from the current position in the
* specified RandomAccessFile and create a new OggPage instance based on the data
* read. If the parameter <code>skipData</code> is set to <code>true</code>,
* the actual page segments (page data) is skipped and not read into
* memory. This mode is useful when scanning through an ogg file to build
* a seek table.
*
* @param source the source from which the ogg page is generated
* @param skipData if set to <code>true</code>, the actual page data is not read into memory
* @return an ogg page created by reading data from the specified source, starting at the current position
* @throws FormatException if the data read from the specified source is not matching the specification for an ogg page
* @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source
* @throws IOException if some other I/O error is detected when reading from the source
*
* @see #create(RandomAccessFile)
*/
public static OggPage create(RandomAccessFile source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException {
return create((Object)source, skipData);
}
/**
* this method equals to create(InputStream source, false)
*
* @see #create(InputStream, boolean)
*/
public static OggPage create(InputStream source) throws IOException, EndOfOggStreamException, OggFormatException {
return create(source, false);
}
/**
* This method is called to read data from the current position in the
* specified InpuStream and create a new OggPage instance based on the data
* read. If the parameter <code>skipData</code> is set to <code>true</code>,
* the actual page segments (page data) is skipped and not read into
* memory. This mode is useful when scanning through an ogg file to build
* a seek table.
*
* @param source the source from which the ogg page is generated
* @param skipData if set to <code>true</code>, the actual page data is not read into memory
* @return an ogg page created by reading data from the specified source, starting at the current position
* @throws FormatException if the data read from the specified source is not matching the specification for an ogg page
* @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source
* @throws IOException if some other I/O error is detected when reading from the source
*
* @see #create(InputStream)
*/
public static OggPage create(InputStream source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException {
return create((Object)source, skipData);
}
/**
* this method equals to create(byte[] source, false)
*
* @see #create(byte[], boolean)
*/
public static OggPage create(byte[] source) throws IOException, EndOfOggStreamException, OggFormatException {
return create(source, false);
}
/**
* This method is called to
* create a new OggPage instance based on the specified byte array.
*
* @param source the source from which the ogg page is generated
* @param skipData if set to <code>true</code>, the actual page data is not read into memory
* @return an ogg page created by reading data from the specified source, starting at the current position
* @throws FormatException if the data read from the specified source is not matching the specification for an ogg page
* @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source
* @throws IOException if some other I/O error is detected when reading from the source
*
* @see #create(byte[])
*/
public static OggPage create(byte[] source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException {
return create((Object)source, skipData);
}
private static OggPage create(Object source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException {
try {
int sourceOffset=27;
byte[] header=new byte[27];
if(source instanceof RandomAccessFile) {
RandomAccessFile raf=(RandomAccessFile)source;
if(raf.getFilePointer()==raf.length()) {
return null;
}
raf.readFully(header);
}
else if(source instanceof InputStream) {
readFully((InputStream)source, header);
}
else if(source instanceof byte[]) {
System.arraycopy((byte[])source, 0, header, 0, 27);
}
BitInputStream bdSource=new ByteArrayBitInputStream(header);
int capture=bdSource.getInt(32);
if(capture!=0x5367674f) {
//throw new FormatException("Ogg page does not start with 'OggS' (0x4f676753)");
/*
** This condition is IMHO an error, but older Ogg files often contain
** pages with a different capture than OggS. I am not sure how to
** manage these pages, but the decoder seems to work properly, if
** the incorrect capture is simply ignored.
*/
String cs=Integer.toHexString(capture);
while(cs.length()<8) {
cs="0"+cs;
}
cs=cs.substring(6, 8)+cs.substring(4, 6)+cs.substring(2, 4)+cs.substring(0, 2);
char c1=(char)(Integer.valueOf(cs.substring(0, 2), 16).intValue());
char c2=(char)(Integer.valueOf(cs.substring(2, 4), 16).intValue());
char c3=(char)(Integer.valueOf(cs.substring(4, 6), 16).intValue());
char c4=(char)(Integer.valueOf(cs.substring(6, 8), 16).intValue());
System.out.println("Ogg packet header is 0x"+cs+" ("+c1+c2+c3+c4+"), should be 0x4f676753 (OggS)");
}
int version=bdSource.getInt(8);
byte tmp=(byte)bdSource.getInt(8);
boolean bf1=(tmp&1)!=0;
boolean bos=(tmp&2)!=0;
boolean eos=(tmp&4)!=0;
long absoluteGranulePosition=bdSource.getLong(64);
int streamSerialNumber=bdSource.getInt(32);
int pageSequenceNumber=bdSource.getInt(32);
int pageCheckSum=bdSource.getInt(32);
int pageSegments=bdSource.getInt(8);
//System.out.println("OggPage: "+streamSerialNumber+" / "+absoluteGranulePosition+" / "+pageSequenceNumber);
int[] segmentOffsets=new int[pageSegments];
int[] segmentLengths=new int[pageSegments];
int totalLength=0;
byte[] segmentTable=new byte[pageSegments];
byte[] tmpBuf=new byte[1];
for(int i=0; i<pageSegments; i++) {
int l=0;
if(source instanceof RandomAccessFile) {
l=((int)((RandomAccessFile)source).readByte()&0xff);
}
else if(source instanceof InputStream) {
l=(int)((InputStream)source).read();
}
else if(source instanceof byte[]) {
l=(int)((byte[])source)[sourceOffset++];
l&=255;
}
segmentTable[i]=(byte)l;
segmentLengths[i]=l;
segmentOffsets[i]=totalLength;
totalLength+=l;
}
byte[] data=null;
if(!skipData) {
//System.out.println("createPage: "+absoluteGranulePosition*1000/44100);
data=new byte[totalLength];
//source.read(data, 0, totalLength);
if(source instanceof RandomAccessFile) {
((RandomAccessFile)source).readFully(data);
}
else if(source instanceof InputStream) {
readFully((InputStream)source, data);
}
else if(source instanceof byte[]) {
System.arraycopy(source, sourceOffset, data, 0, totalLength);
}
}
return new OggPage(version, bf1, bos, eos, absoluteGranulePosition, streamSerialNumber, pageSequenceNumber, pageCheckSum, segmentOffsets, segmentLengths, totalLength, header, segmentTable, data);
}
catch(EOFException e) {
throw new EndOfOggStreamException();
}
}
private static void readFully(InputStream source, byte[] buffer) throws IOException {
int total=0;
while(total<buffer.length) {
int read=source.read(buffer, total, buffer.length-total);
if(read==-1) {
throw new EndOfOggStreamException();
}
total+=read;
}
}
/**
* Returns the absolute granule position of the last complete
* packet contained in this Ogg page, or -1 if the page contains a single
* packet, which is not completed on this page. For pages containing Vorbis
* data, this value is the sample index within the Vorbis stream. The Vorbis
* stream does not necessarily start with sample index 0.
*
* @return the absolute granule position of the last packet completed on
* this page
*/
public long getAbsoluteGranulePosition() {
return absoluteGranulePosition;
}
/**
* Returns the stream serial number of this ogg page.
*
* @return this page's serial number
*/
public int getStreamSerialNumber() {
return streamSerialNumber;
}
/**
* Return the sequnce number of this ogg page.
*
* @return this page's sequence number
*/
public int getPageSequenceNumber() {
return pageSequenceNumber;
}
/**
* Return the check sum of this ogg page.
*
* @return this page's check sum
*/
public int getPageCheckSum() {
return pageCheckSum;
}
/**
* @return the total number of bytes in the page data
*/
public int getTotalLength() {
if(data!=null) {
return 27+segmentTable.length+data.length;
}
else {
return totalLength;
}
}
/**
* @return a ByteBuffer containing the page data
*/
public byte[] getData() {
return data;
}
public byte[] getHeader() {
return header;
}
public byte[] getSegmentTable() {
return segmentTable;
}
public int[] getSegmentOffsets() {
return segmentOffsets;
}
public int[] getSegmentLengths() {
return segmentLengths;
}
/**
* @return <code>true</code> if this page begins with a continued packet
*/
public boolean isContinued() {
return continued;
}
/**
* @return <code>true</code> if this page begins with a fresh packet
*/
public boolean isFresh() {
return !continued;
}
/**
* @return <code>true</code> if this page is the beginning of a logical stream
*/
public boolean isBos() {
return bos;
}
/**
* @return <code>true</code> if this page is the end of a logical stream
*/
public boolean isEos() {
return eos;
}
}
|