Update #44 - WebAssembly GC support, fix more WebRTC bugs

This commit is contained in:
lax1dude
2024-12-03 23:38:28 -08:00
parent 919014b4df
commit 70b52bbf7a
216 changed files with 34358 additions and 91 deletions

View File

@ -0,0 +1,293 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
* Copyright (C) 2000 ymnk, JCraft,Inc.
*
* Written by: 2000 ymnk<ymnk@jcraft.com>
*
* Many thanks to
* Monty <monty@xiph.org> and
* The XIPHOPHORUS Company http://www.xiph.org/ .
* JOrbis has been based on their awesome works, Vorbis codec.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.jogg;
public class Buffer {
private static final int BUFFER_INCREMENT = 256;
private static final int[] mask = { 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f,
0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
0x00007fff, 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
0xffffffff };
int ptr = 0;
byte[] buffer = null;
int endbit = 0;
int endbyte = 0;
int storage = 0;
public void writeinit() {
buffer = new byte[BUFFER_INCREMENT];
ptr = 0;
buffer[0] = (byte) '\0';
storage = BUFFER_INCREMENT;
}
public void write(byte[] s) {
for (int i = 0; i < s.length; i++) {
if (s[i] == 0)
break;
write(s[i], 8);
}
}
public void read(byte[] s, int bytes) {
int i = 0;
while (bytes-- != 0) {
s[i++] = (byte) (read(8));
}
}
void reset() {
ptr = 0;
buffer[0] = (byte) '\0';
endbit = endbyte = 0;
}
public void writeclear() {
buffer = null;
}
public void readinit(byte[] buf, int bytes) {
readinit(buf, 0, bytes);
}
public void readinit(byte[] buf, int start, int bytes) {
ptr = start;
buffer = buf;
endbit = endbyte = 0;
storage = bytes;
}
public void write(int value, int bits) {
if (endbyte + 4 >= storage) {
byte[] foo = new byte[storage + BUFFER_INCREMENT];
System.arraycopy(buffer, 0, foo, 0, storage);
buffer = foo;
storage += BUFFER_INCREMENT;
}
value &= mask[bits];
bits += endbit;
buffer[ptr] |= (byte) (value << endbit);
if (bits >= 8) {
buffer[ptr + 1] = (byte) (value >>> (8 - endbit));
if (bits >= 16) {
buffer[ptr + 2] = (byte) (value >>> (16 - endbit));
if (bits >= 24) {
buffer[ptr + 3] = (byte) (value >>> (24 - endbit));
if (bits >= 32) {
if (endbit > 0)
buffer[ptr + 4] = (byte) (value >>> (32 - endbit));
else
buffer[ptr + 4] = 0;
}
}
}
}
endbyte += bits / 8;
ptr += bits / 8;
endbit = bits & 7;
}
public int look(int bits) {
int ret;
int m = mask[bits];
bits += endbit;
if (endbyte + 4 >= storage) {
if (endbyte + (bits - 1) / 8 >= storage)
return (-1);
}
ret = ((buffer[ptr]) & 0xff) >>> endbit;
if (bits > 8) {
ret |= ((buffer[ptr + 1]) & 0xff) << (8 - endbit);
if (bits > 16) {
ret |= ((buffer[ptr + 2]) & 0xff) << (16 - endbit);
if (bits > 24) {
ret |= ((buffer[ptr + 3]) & 0xff) << (24 - endbit);
if (bits > 32 && endbit != 0) {
ret |= ((buffer[ptr + 4]) & 0xff) << (32 - endbit);
}
}
}
}
return (m & ret);
}
public int look1() {
if (endbyte >= storage)
return (-1);
return ((buffer[ptr] >> endbit) & 1);
}
public void adv(int bits) {
bits += endbit;
ptr += bits / 8;
endbyte += bits / 8;
endbit = bits & 7;
}
public void adv1() {
++endbit;
if (endbit > 7) {
endbit = 0;
ptr++;
endbyte++;
}
}
public int read(int bits) {
int ret;
int m = mask[bits];
bits += endbit;
if (endbyte + 4 >= storage) {
ret = -1;
if (endbyte + (bits - 1) / 8 >= storage) {
ptr += bits / 8;
endbyte += bits / 8;
endbit = bits & 7;
return (ret);
}
}
ret = ((buffer[ptr]) & 0xff) >>> endbit;
if (bits > 8) {
ret |= ((buffer[ptr + 1]) & 0xff) << (8 - endbit);
if (bits > 16) {
ret |= ((buffer[ptr + 2]) & 0xff) << (16 - endbit);
if (bits > 24) {
ret |= ((buffer[ptr + 3]) & 0xff) << (24 - endbit);
if (bits > 32 && endbit != 0) {
ret |= ((buffer[ptr + 4]) & 0xff) << (32 - endbit);
}
}
}
}
ret &= m;
ptr += bits / 8;
endbyte += bits / 8;
endbit = bits & 7;
return (ret);
}
public int readB(int bits) {
int ret;
int m = 32 - bits;
bits += endbit;
if (endbyte + 4 >= storage) {
/* not the main path */
ret = -1;
if (endbyte * 8 + bits > storage * 8) {
ptr += bits / 8;
endbyte += bits / 8;
endbit = bits & 7;
return (ret);
}
}
ret = (buffer[ptr] & 0xff) << (24 + endbit);
if (bits > 8) {
ret |= (buffer[ptr + 1] & 0xff) << (16 + endbit);
if (bits > 16) {
ret |= (buffer[ptr + 2] & 0xff) << (8 + endbit);
if (bits > 24) {
ret |= (buffer[ptr + 3] & 0xff) << (endbit);
if (bits > 32 && (endbit != 0))
ret |= (buffer[ptr + 4] & 0xff) >> (8 - endbit);
}
}
}
ret = (ret >>> (m >> 1)) >>> ((m + 1) >> 1);
ptr += bits / 8;
endbyte += bits / 8;
endbit = bits & 7;
return (ret);
}
public int read1() {
int ret;
if (endbyte >= storage) {
ret = -1;
endbit++;
if (endbit > 7) {
endbit = 0;
ptr++;
endbyte++;
}
return (ret);
}
ret = (buffer[ptr] >> endbit) & 1;
endbit++;
if (endbit > 7) {
endbit = 0;
ptr++;
endbyte++;
}
return (ret);
}
public int bytes() {
return (endbyte + (endbit + 7) / 8);
}
public int bits() {
return (endbyte * 8 + endbit);
}
public byte[] buffer() {
return (buffer);
}
public static int ilog(int v) {
int ret = 0;
while (v > 0) {
ret++;
v >>>= 1;
}
return (ret);
}
public static void report(String in) {
System.err.println(in);
System.exit(1);
}
}

View File

@ -0,0 +1,45 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
* Copyright (C) 2000 ymnk, JCraft,Inc.
*
* Written by: 2000 ymnk<ymnk@jcraft.com>
*
* Many thanks to
* Monty <monty@xiph.org> and
* The XIPHOPHORUS Company http://www.xiph.org/ .
* JOrbis has been based on their awesome works, Vorbis codec.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.jogg;
public class Packet {
public byte[] packet_base;
public int packet;
public int bytes;
public int b_o_s;
public int e_o_s;
public long granulepos;
/**
* sequence number for decode; the framing knows where there's a hole in the
* data, but we need coupling so that the codec (which is in a seperate
* abstraction layer) also knows about the gap
*/
public long packetno;
}

View File

@ -0,0 +1,130 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
* Copyright (C) 2000 ymnk, JCraft,Inc.
*
* Written by: 2000 ymnk<ymnk@jcraft.com>
*
* Many thanks to
* Monty <monty@xiph.org> and
* The XIPHOPHORUS Company http://www.xiph.org/ .
* JOrbis has been based on their awesome works, Vorbis codec.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.jogg;
public class Page {
private static int[] crc_lookup = new int[256];
static {
for (int i = 0; i < crc_lookup.length; i++) {
crc_lookup[i] = crc_entry(i);
}
}
private static int crc_entry(int index) {
int r = index << 24;
for (int i = 0; i < 8; i++) {
if ((r & 0x80000000) != 0) {
r = (r << 1) ^ 0x04c11db7; /*
* The same as the ethernet generator polynomial, although we use an
* unreflected alg and an init/final of 0, not 0xffffffff
*/
} else {
r <<= 1;
}
}
return (r & 0xffffffff);
}
public byte[] header_base;
public int header;
public int header_len;
public byte[] body_base;
public int body;
public int body_len;
int version() {
return header_base[header + 4] & 0xff;
}
int continued() {
return (header_base[header + 5] & 0x01);
}
public int bos() {
return (header_base[header + 5] & 0x02);
}
public int eos() {
return (header_base[header + 5] & 0x04);
}
public long granulepos() {
long foo = header_base[header + 13] & 0xff;
foo = (foo << 8) | (header_base[header + 12] & 0xff);
foo = (foo << 8) | (header_base[header + 11] & 0xff);
foo = (foo << 8) | (header_base[header + 10] & 0xff);
foo = (foo << 8) | (header_base[header + 9] & 0xff);
foo = (foo << 8) | (header_base[header + 8] & 0xff);
foo = (foo << 8) | (header_base[header + 7] & 0xff);
foo = (foo << 8) | (header_base[header + 6] & 0xff);
return (foo);
}
public int serialno() {
return (header_base[header + 14] & 0xff) | ((header_base[header + 15] & 0xff) << 8)
| ((header_base[header + 16] & 0xff) << 16) | ((header_base[header + 17] & 0xff) << 24);
}
int pageno() {
return (header_base[header + 18] & 0xff) | ((header_base[header + 19] & 0xff) << 8)
| ((header_base[header + 20] & 0xff) << 16) | ((header_base[header + 21] & 0xff) << 24);
}
void checksum() {
int crc_reg = 0;
for (int i = 0; i < header_len; i++) {
crc_reg = (crc_reg << 8) ^ crc_lookup[((crc_reg >>> 24) & 0xff) ^ (header_base[header + i] & 0xff)];
}
for (int i = 0; i < body_len; i++) {
crc_reg = (crc_reg << 8) ^ crc_lookup[((crc_reg >>> 24) & 0xff) ^ (body_base[body + i] & 0xff)];
}
header_base[header + 22] = (byte) crc_reg;
header_base[header + 23] = (byte) (crc_reg >>> 8);
header_base[header + 24] = (byte) (crc_reg >>> 16);
header_base[header + 25] = (byte) (crc_reg >>> 24);
}
public Page copy() {
return copy(new Page());
}
public Page copy(Page p) {
byte[] tmp = new byte[header_len];
System.arraycopy(header_base, header, tmp, 0, header_len);
p.header_len = header_len;
p.header_base = tmp;
p.header = 0;
tmp = new byte[body_len];
System.arraycopy(body_base, body, tmp, 0, body_len);
p.body_len = body_len;
p.body_base = tmp;
p.body = 0;
return p;
}
}

View File

@ -0,0 +1,538 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
* Copyright (C) 2000 ymnk, JCraft,Inc.
*
* Written by: 2000 ymnk<ymnk@jcraft.com>
*
* Many thanks to
* Monty <monty@xiph.org> and
* The XIPHOPHORUS Company http://www.xiph.org/ .
* JOrbis has been based on their awesome works, Vorbis codec.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.jogg;
public class StreamState {
byte[] body_data; /* bytes from packet bodies */
int body_storage; /* storage elements allocated */
int body_fill; /* elements stored; fill mark */
private int body_returned; /* elements of fill returned */
int[] lacing_vals; /* The values that will go to the segment table */
long[] granule_vals; /*
* pcm_pos values for headers. Not compact this way, but it is simple coupled to
* the lacing fifo
*/
int lacing_storage;
int lacing_fill;
int lacing_packet;
int lacing_returned;
byte[] header = new byte[282]; /* working space for header encode */
int header_fill;
public int e_o_s; /*
* set when we have buffered the last packet in the logical bitstream
*/
int b_o_s; /*
* set after we've written the initial page of a logical bitstream
*/
int serialno;
int pageno;
long packetno; /*
* sequence number for decode; the framing knows where there's a hole in the
* data, but we need coupling so that the codec (which is in a seperate
* abstraction layer) also knows about the gap
*/
long granulepos;
public StreamState() {
init();
}
StreamState(int serialno) {
this();
init(serialno);
}
void init() {
body_storage = 16 * 1024;
body_data = new byte[body_storage];
lacing_storage = 1024;
lacing_vals = new int[lacing_storage];
granule_vals = new long[lacing_storage];
}
public void init(int serialno) {
if (body_data == null) {
init();
} else {
for (int i = 0; i < body_data.length; i++)
body_data[i] = 0;
for (int i = 0; i < lacing_vals.length; i++)
lacing_vals[i] = 0;
for (int i = 0; i < granule_vals.length; i++)
granule_vals[i] = 0;
}
this.serialno = serialno;
}
public void clear() {
body_data = null;
lacing_vals = null;
granule_vals = null;
}
void destroy() {
clear();
}
void body_expand(int needed) {
if (body_storage <= body_fill + needed) {
body_storage += (needed + 1024);
byte[] foo = new byte[body_storage];
System.arraycopy(body_data, 0, foo, 0, body_data.length);
body_data = foo;
}
}
void lacing_expand(int needed) {
if (lacing_storage <= lacing_fill + needed) {
lacing_storage += (needed + 32);
int[] foo = new int[lacing_storage];
System.arraycopy(lacing_vals, 0, foo, 0, lacing_vals.length);
lacing_vals = foo;
long[] bar = new long[lacing_storage];
System.arraycopy(granule_vals, 0, bar, 0, granule_vals.length);
granule_vals = bar;
}
}
/* submit data to the internal buffer of the framing engine */
public int packetin(Packet op) {
int lacing_val = op.bytes / 255 + 1;
if (body_returned != 0) {
/*
* advance packet data according to the body_returned pointer. We had to keep it
* around to return a pointer into the buffer last call
*/
body_fill -= body_returned;
if (body_fill != 0) {
System.arraycopy(body_data, body_returned, body_data, 0, body_fill);
}
body_returned = 0;
}
/* make sure we have the buffer storage */
body_expand(op.bytes);
lacing_expand(lacing_val);
/*
* Copy in the submitted packet. Yes, the copy is a waste; this is the liability
* of overly clean abstraction for the time being. It will actually be fairly
* easy to eliminate the extra copy in the future
*/
System.arraycopy(op.packet_base, op.packet, body_data, body_fill, op.bytes);
body_fill += op.bytes;
/* Store lacing vals for this packet */
int j;
for (j = 0; j < lacing_val - 1; j++) {
lacing_vals[lacing_fill + j] = 255;
granule_vals[lacing_fill + j] = granulepos;
}
lacing_vals[lacing_fill + j] = (op.bytes) % 255;
granulepos = granule_vals[lacing_fill + j] = op.granulepos;
/* flag the first segment as the beginning of the packet */
lacing_vals[lacing_fill] |= 0x100;
lacing_fill += lacing_val;
/* for the sake of completeness */
packetno++;
if (op.e_o_s != 0)
e_o_s = 1;
return (0);
}
public int packetout(Packet op) {
/*
* The last part of decode. We have the stream broken into packet segments. Now
* we need to group them into packets (or return the out of sync markers)
*/
int ptr = lacing_returned;
if (lacing_packet <= ptr) {
return (0);
}
if ((lacing_vals[ptr] & 0x400) != 0) {
/* We lost sync here; let the app know */
lacing_returned++;
/*
* we need to tell the codec there's a gap; it might need to handle previous
* packet dependencies.
*/
packetno++;
return (-1);
}
/* Gather the whole packet. We'll have no holes or a partial packet */
{
int size = lacing_vals[ptr] & 0xff;
int bytes = 0;
op.packet_base = body_data;
op.packet = body_returned;
op.e_o_s = lacing_vals[ptr] & 0x200; /* last packet of the stream? */
op.b_o_s = lacing_vals[ptr] & 0x100; /* first packet of the stream? */
bytes += size;
while (size == 255) {
int val = lacing_vals[++ptr];
size = val & 0xff;
if ((val & 0x200) != 0)
op.e_o_s = 0x200;
bytes += size;
}
op.packetno = packetno;
op.granulepos = granule_vals[ptr];
op.bytes = bytes;
body_returned += bytes;
lacing_returned = ptr + 1;
}
packetno++;
return (1);
}
// add the incoming page to the stream state; we decompose the page
// into packet segments here as well.
public int pagein(Page og) {
byte[] header_base = og.header_base;
int header = og.header;
byte[] body_base = og.body_base;
int body = og.body;
int bodysize = og.body_len;
int segptr = 0;
int version = og.version();
int continued = og.continued();
int bos = og.bos();
int eos = og.eos();
long granulepos = og.granulepos();
int _serialno = og.serialno();
int _pageno = og.pageno();
int segments = header_base[header + 26] & 0xff;
// clean up 'returned data'
{
int lr = lacing_returned;
int br = body_returned;
// body data
if (br != 0) {
body_fill -= br;
if (body_fill != 0) {
System.arraycopy(body_data, br, body_data, 0, body_fill);
}
body_returned = 0;
}
if (lr != 0) {
// segment table
if ((lacing_fill - lr) != 0) {
System.arraycopy(lacing_vals, lr, lacing_vals, 0, lacing_fill - lr);
System.arraycopy(granule_vals, lr, granule_vals, 0, lacing_fill - lr);
}
lacing_fill -= lr;
lacing_packet -= lr;
lacing_returned = 0;
}
}
// check the serial number
if (_serialno != serialno)
return (-1);
if (version > 0)
return (-1);
lacing_expand(segments + 1);
// are we in sequence?
if (_pageno != pageno) {
int i;
// unroll previous partial packet (if any)
for (i = lacing_packet; i < lacing_fill; i++) {
body_fill -= lacing_vals[i] & 0xff;
// System.out.println("??");
}
lacing_fill = lacing_packet;
// make a note of dropped data in segment table
if (pageno != -1) {
lacing_vals[lacing_fill++] = 0x400;
lacing_packet++;
}
// are we a 'continued packet' page? If so, we'll need to skip
// some segments
if (continued != 0) {
bos = 0;
for (; segptr < segments; segptr++) {
int val = (header_base[header + 27 + segptr] & 0xff);
body += val;
bodysize -= val;
if (val < 255) {
segptr++;
break;
}
}
}
}
if (bodysize != 0) {
body_expand(bodysize);
System.arraycopy(body_base, body, body_data, body_fill, bodysize);
body_fill += bodysize;
}
{
int saved = -1;
while (segptr < segments) {
int val = (header_base[header + 27 + segptr] & 0xff);
lacing_vals[lacing_fill] = val;
granule_vals[lacing_fill] = -1;
if (bos != 0) {
lacing_vals[lacing_fill] |= 0x100;
bos = 0;
}
if (val < 255)
saved = lacing_fill;
lacing_fill++;
segptr++;
if (val < 255)
lacing_packet = lacing_fill;
}
/* set the granulepos on the last pcmval of the last full packet */
if (saved != -1) {
granule_vals[saved] = granulepos;
}
}
if (eos != 0) {
e_o_s = 1;
if (lacing_fill > 0)
lacing_vals[lacing_fill - 1] |= 0x200;
}
pageno = _pageno + 1;
return (0);
}
/*
* This will flush remaining packets into a page (returning nonzero), even if
* there is not enough data to trigger a flush normally (undersized page). If
* there are no packets or partial packets to flush, ogg_stream_flush returns 0.
* Note that ogg_stream_flush will try to flush a normal sized page like
* ogg_stream_pageout; a call to ogg_stream_flush does not gurantee that all
* packets have flushed. Only a return value of 0 from ogg_stream_flush
* indicates all packet data is flushed into pages.
*
* ogg_stream_page will flush the last page in a stream even if it's undersized;
* you almost certainly want to use ogg_stream_pageout (and *not*
* ogg_stream_flush) unless you need to flush an undersized page in the middle
* of a stream for some reason.
*/
public int flush(Page og) {
int i;
int vals = 0;
int maxvals = (lacing_fill > 255 ? 255 : lacing_fill);
int bytes = 0;
int acc = 0;
long granule_pos = granule_vals[0];
if (maxvals == 0)
return (0);
/* construct a page */
/* decide how many segments to include */
/*
* If this is the initial header case, the first page must only include the
* initial header packet
*/
if (b_o_s == 0) { /* 'initial header page' case */
granule_pos = 0;
for (vals = 0; vals < maxvals; vals++) {
if ((lacing_vals[vals] & 0x0ff) < 255) {
vals++;
break;
}
}
} else {
for (vals = 0; vals < maxvals; vals++) {
if (acc > 4096)
break;
acc += (lacing_vals[vals] & 0x0ff);
granule_pos = granule_vals[vals];
}
}
/* construct the header in temp storage */
System.arraycopy("OggS".getBytes(), 0, header, 0, 4);
/* stream structure version */
header[4] = 0x00;
/* continued packet flag? */
header[5] = 0x00;
if ((lacing_vals[0] & 0x100) == 0)
header[5] |= 0x01;
/* first page flag? */
if (b_o_s == 0)
header[5] |= 0x02;
/* last page flag? */
if (e_o_s != 0 && lacing_fill == vals)
header[5] |= 0x04;
b_o_s = 1;
/* 64 bits of PCM position */
for (i = 6; i < 14; i++) {
header[i] = (byte) granule_pos;
granule_pos >>>= 8;
}
/* 32 bits of stream serial number */
{
int _serialno = serialno;
for (i = 14; i < 18; i++) {
header[i] = (byte) _serialno;
_serialno >>>= 8;
}
}
/*
* 32 bits of page counter (we have both counter and page header because this
* val can roll over)
*/
if (pageno == -1)
pageno = 0; /*
* because someone called stream_reset; this would be a strange thing to do in
* an encode stream, but it has plausible uses
*/
{
int _pageno = pageno++;
for (i = 18; i < 22; i++) {
header[i] = (byte) _pageno;
_pageno >>>= 8;
}
}
/* zero for computation; filled in later */
header[22] = 0;
header[23] = 0;
header[24] = 0;
header[25] = 0;
/* segment table */
header[26] = (byte) vals;
for (i = 0; i < vals; i++) {
header[i + 27] = (byte) lacing_vals[i];
bytes += (header[i + 27] & 0xff);
}
/* set pointers in the ogg_page struct */
og.header_base = header;
og.header = 0;
og.header_len = header_fill = vals + 27;
og.body_base = body_data;
og.body = body_returned;
og.body_len = bytes;
/* advance the lacing data and set the body_returned pointer */
lacing_fill -= vals;
System.arraycopy(lacing_vals, vals, lacing_vals, 0, lacing_fill * 4);
System.arraycopy(granule_vals, vals, granule_vals, 0, lacing_fill * 8);
body_returned += bytes;
/* calculate the checksum */
og.checksum();
/* done */
return (1);
}
/*
* This constructs pages from buffered packet segments. The pointers returned
* are to static buffers; do not free. The returned buffers are good only until
* the next call (using the same ogg_stream_state)
*/
public int pageout(Page og) {
if ((e_o_s != 0 && lacing_fill != 0) || /* 'were done, now flush' case */
body_fill - body_returned > 4096 || /* 'page nominal size' case */
lacing_fill >= 255 || /* 'segment table full' case */
(lacing_fill != 0 && b_o_s == 0)) { /* 'initial header page' case */
return flush(og);
}
return 0;
}
public int eof() {
return e_o_s;
}
public int reset() {
body_fill = 0;
body_returned = 0;
lacing_fill = 0;
lacing_packet = 0;
lacing_returned = 0;
header_fill = 0;
e_o_s = 0;
b_o_s = 0;
pageno = -1;
packetno = 0;
granulepos = 0;
return (0);
}
}

View File

@ -0,0 +1,273 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
* Copyright (C) 2000 ymnk, JCraft,Inc.
*
* Written by: 2000 ymnk<ymnk@jcraft.com>
*
* Many thanks to
* Monty <monty@xiph.org> and
* The XIPHOPHORUS Company http://www.xiph.org/ .
* JOrbis has been based on their awesome works, Vorbis codec.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.jogg;
// DECODING PRIMITIVES: packet streaming layer
// This has two layers to place more of the multi-serialno and paging
// control in the application's hands. First, we expose a data buffer
// using ogg_decode_buffer(). The app either copies into the
// buffer, or passes it directly to read(), etc. We then call
// ogg_decode_wrote() to tell how many bytes we just added.
//
// Pages are returned (pointers into the buffer in ogg_sync_state)
// by ogg_decode_stream(). The page is then submitted to
// ogg_decode_page() along with the appropriate
// ogg_stream_state* (ie, matching serialno). We then get raw
// packets out calling ogg_stream_packet() with a
// ogg_stream_state. See the 'frame-prog.txt' docs for details and
// example code.
public class SyncState {
public byte[] data;
int storage;
int fill;
int returned;
int unsynced;
int headerbytes;
int bodybytes;
public int clear() {
data = null;
return (0);
}
public int buffer(int size) {
// first, clear out any space that has been previously returned
if (returned != 0) {
fill -= returned;
if (fill > 0) {
System.arraycopy(data, returned, data, 0, fill);
}
returned = 0;
}
if (size > storage - fill) {
// We need to extend the internal buffer
int newsize = size + fill + 4096; // an extra page to be nice
if (data != null) {
byte[] foo = new byte[newsize];
System.arraycopy(data, 0, foo, 0, data.length);
data = foo;
} else {
data = new byte[newsize];
}
storage = newsize;
}
return (fill);
}
public int wrote(int bytes) {
if (fill + bytes > storage)
return (-1);
fill += bytes;
return (0);
}
// sync the stream. This is meant to be useful for finding page
// boundaries.
//
// return values for this:
// -n) skipped n bytes
// 0) page not ready; more data (no bytes skipped)
// n) page synced at current location; page length n bytes
private Page pageseek = new Page();
private byte[] chksum = new byte[4];
public int pageseek(Page og) {
int page = returned;
int next;
int bytes = fill - returned;
if (headerbytes == 0) {
int _headerbytes, i;
if (bytes < 27)
return (0); // not enough for a header
/* verify capture pattern */
if (data[page] != 'O' || data[page + 1] != 'g' || data[page + 2] != 'g' || data[page + 3] != 'S') {
headerbytes = 0;
bodybytes = 0;
// search for possible capture
next = 0;
for (int ii = 0; ii < bytes - 1; ii++) {
if (data[page + 1 + ii] == 'O') {
next = page + 1 + ii;
break;
}
}
// next=memchr(page+1,'O',bytes-1);
if (next == 0)
next = fill;
returned = next;
return (-(next - page));
}
_headerbytes = (data[page + 26] & 0xff) + 27;
if (bytes < _headerbytes)
return (0); // not enough for header + seg table
// count up body length in the segment table
for (i = 0; i < (data[page + 26] & 0xff); i++) {
bodybytes += (data[page + 27 + i] & 0xff);
}
headerbytes = _headerbytes;
}
if (bodybytes + headerbytes > bytes)
return (0);
// The whole test page is buffered. Verify the checksum
synchronized (chksum) {
// Grab the checksum bytes, set the header field to zero
System.arraycopy(data, page + 22, chksum, 0, 4);
data[page + 22] = 0;
data[page + 23] = 0;
data[page + 24] = 0;
data[page + 25] = 0;
// set up a temp page struct and recompute the checksum
Page log = pageseek;
log.header_base = data;
log.header = page;
log.header_len = headerbytes;
log.body_base = data;
log.body = page + headerbytes;
log.body_len = bodybytes;
log.checksum();
// Compare
if (chksum[0] != data[page + 22] || chksum[1] != data[page + 23] || chksum[2] != data[page + 24]
|| chksum[3] != data[page + 25]) {
// D'oh. Mismatch! Corrupt page (or miscapture and not a page at all)
// replace the computed checksum with the one actually read in
System.arraycopy(chksum, 0, data, page + 22, 4);
// Bad checksum. Lose sync */
headerbytes = 0;
bodybytes = 0;
// search for possible capture
next = 0;
for (int ii = 0; ii < bytes - 1; ii++) {
if (data[page + 1 + ii] == 'O') {
next = page + 1 + ii;
break;
}
}
// next=memchr(page+1,'O',bytes-1);
if (next == 0)
next = fill;
returned = next;
return (-(next - page));
}
}
// yes, have a whole page all ready to go
{
page = returned;
if (og != null) {
og.header_base = data;
og.header = page;
og.header_len = headerbytes;
og.body_base = data;
og.body = page + headerbytes;
og.body_len = bodybytes;
}
unsynced = 0;
returned += (bytes = headerbytes + bodybytes);
headerbytes = 0;
bodybytes = 0;
return (bytes);
}
}
// sync the stream and get a page. Keep trying until we find a page.
// Supress 'sync errors' after reporting the first.
//
// return values:
// -1) recapture (hole in data)
// 0) need more data
// 1) page returned
//
// Returns pointers into buffered data; invalidated by next call to
// _stream, _clear, _init, or _buffer
public int pageout(Page og) {
// all we need to do is verify a page at the head of the stream
// buffer. If it doesn't verify, we look for the next potential
// frame
while (true) {
int ret = pageseek(og);
if (ret > 0) {
// have a page
return (1);
}
if (ret == 0) {
// need more data
return (0);
}
// head did not start a synced page... skipped some bytes
if (unsynced == 0) {
unsynced = 1;
return (-1);
}
// loop. keep looking
}
}
// clear things to an initial state. Good to call, eg, before seeking
public int reset() {
fill = 0;
returned = 0;
unsynced = 0;
headerbytes = 0;
bodybytes = 0;
return (0);
}
public void init() {
}
public int getDataOffset() {
return returned;
}
public int getBufferOffset() {
return fill;
}
}