Ticket #2496: videoPerformance_java_2.patch
File videoPerformance_java_2.patch, 14.6 KB (added by boyanl, 15 years ago) |
---|
-
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/RawBinData.java
### Eclipse Workspace Patch 1.0 #P sophie
10 10 11 11 import org.sophie2.base.commons.util.Base64; 12 12 import org.sophie2.core.prolib.annot.Immutable; 13 import org.sophie2.base.commons.util.bindata.FastByteArrayOutputStream; 13 14 14 15 /** 15 16 * Immutable binary data. … … 115 116 */ 116 117 public RawBinData(InputStream input, int size) throws IOException { 117 118 // should read exactly size bytes 118 ByteArrayOutputStream os = new ByteArrayOutputStream();119 FastByteArrayOutputStream os = new FastByteArrayOutputStream(size); 119 120 transport(input, os, size); 120 121 this.data = os.toByteArray(); 121 122 } … … 137 138 138 139 @Override 139 140 public byte[] getBytes() { 140 byte[] copyOfData = new byte[this.data.length]; 141 System.arraycopy(this.data, 0, copyOfData, 0, this.data.length); 142 return copyOfData; 141 /*byte[] copyOfData = new byte[this.data.length]; 142 System.arraycopy(this.data, 0, copyOfData, 0, this.data.length); 143 return copyOfData;*/ 144 return this.data; 143 145 } 144 146 145 147 -
modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/NativeMediaHandler.java
106 106 // If there are some requests for AudioChunks... 107 107 if (!reqQueue.isEmpty()) { 108 108 lastRequest = reqQueue.poll(); 109 SophieLog.debug("Fetching request for position " + lastRequest);109 //SophieLog.debug("Fetching request for position " + lastRequest); 110 110 111 111 // If the requested chunk is not in the cache retrieve it 112 112 // from the natives and put it in the cache. … … 135 135 136 136 //Prefetch: 137 137 if (nextNeeded != -1) { 138 SophieLog.debug("Prefetching: " + nextNeeded);138 //SophieLog.debug("Prefetching: " + nextNeeded); 139 139 this.audioCache.put(nextNeeded, getNativeAudio(nextNeeded)); 140 140 } else { 141 141 // If there is no request yet just sleep for a while... … … 200 200 MediaInfoResponse response = getVideoBridge().sendCommand(cmd, MediaInfoResponse.class); 201 201 ImmSize size = new ImmSize(response.getWidth(), response.getHeight()); 202 202 double duration = response.getDurationInMillis() / 1000.0; 203 this.info = new MediaInfo(duration, size, response. hasAudio());203 this.info = new MediaInfo(duration, size, response.getFrameRate(), response.hasAudio()); 204 204 } 205 205 return this.info; 206 206 … … 242 242 public ImmImage getFrame(long millis) { 243 243 assert millis >= 0 && millis <= Integer.MAX_VALUE; 244 244 // TODO: temporary just loop 245 if(Math.abs(millis - this.lastFrameMillis) < 25) { 245 int fr = getInfo().getFrameRate(); 246 int delta = (fr > 0 ? 1000/fr : 25); 247 if(Math.abs(millis - this.lastFrameMillis) <= delta) { 246 248 assert this.lastFrame != null; 247 249 return this.lastFrame; 248 250 } 249 251 this.lastFrame = getVideoBridge().getFrame((int) millis % getModLen()); 250 252 assert this.lastFrame != null; 251 253 this.lastFrameMillis = millis; 254 252 255 return this.lastFrame; 253 256 } 254 257 -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinReader.java
23 23 public final class BinReader { 24 24 private final BinData data; 25 25 private int pos; 26 private byte[] bytes; 26 27 27 28 /** 28 29 * Constructor … … 31 32 public BinReader(BinData data) { 32 33 this.data = data; 33 34 this.pos = 0; 35 36 /* 37 * Small optimization - instead of performing a virtual call to read each byte, 38 * fetch them all at once at construction and then simply index 39 * We force the data to be kept in memory but RawBinData is stored in main memory anyways 40 */ 41 this.bytes = data.getBytes(); 34 42 } 35 43 36 44 /** … … 91 99 private byte readByte() { 92 100 assert this.pos < this.data.getSize() : 93 101 "pos=" + this.pos + " ,size=" + this.data.getSize(); 94 return this.data.getByte(this.pos++); 102 103 return this.bytes[this.pos++]; 95 104 } 96 105 97 106 private byte[] readArray(int len) { 98 107 // TODO may be written faster if needed 99 108 byte[] res = new byte[len]; 100 for (int i = 0; i < len; ++i) { 101 res[i] = readByte(); 102 } 109 //copy bytes [pos, pos+len) 110 System.arraycopy(this.bytes, this.pos, res, 0, len); 111 this.pos += len; 112 103 113 return res; 104 114 } 105 115 -
modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/MediaInfoResponse.java
13 13 private final int millis; 14 14 private final int width; 15 15 private final int height; 16 private final int approxFrameRate; 16 17 private final boolean hasAudio; 17 18 18 19 /** … … 29 30 this.height = reader.readInt(); 30 31 int temp = reader.readInt(); 31 32 this.hasAudio = (temp == 0) ? false : true; 33 int frameRate_num = reader.readInt (); 34 int frameRate_den = reader.readInt (); 35 this.approxFrameRate = frameRate_num/frameRate_den; 32 36 reader.assertEnd(); 33 37 } 34 38 … … 66 70 public int getHeight() { 67 71 return this.height; 68 72 } 73 74 /** 75 * Gets the framerate of the video in frames per second. 76 * @return 77 * The framerate of the video. 78 */ 79 public int getFrameRate () { 80 return this.approxFrameRate; 81 } 82 69 83 } -
modules/org.sophie2.base.media/src/main/java/org/sophie2/base/media/MediaInfo.java
16 16 * Represents an empty media info. 17 17 */ 18 18 public static final MediaInfo EMPTY_INFO = new MediaInfo(0, 19 ImmImage.DUMMY_IMAGE.getSize(), false);19 ImmImage.DUMMY_IMAGE.getSize(), 0, false); 20 20 21 21 private final double duration; 22 22 private final ImmSize dimensions; 23 23 private final boolean hasAudio; 24 private final int frameRate; 24 25 25 26 /** 26 27 * Constructs a media info. … … 29 30 * - the duration of the media in seconds. 30 31 * @param dimensions 31 32 * - the dimensions (width and height) of the video content. 33 * @param frameRate 34 * - the frame rate of the video content. 32 35 * @param hasAudio 33 36 * - whether this medias has audio. 34 37 * 35 38 */ 36 public MediaInfo(double duration, ImmSize dimensions, boolean hasAudio) {39 public MediaInfo(double duration, ImmSize dimensions, int frameRate, boolean hasAudio) { 37 40 this.duration = duration; 38 41 this.dimensions = dimensions; 39 42 this.hasAudio = hasAudio; 43 this.frameRate = frameRate; 40 44 } 41 45 42 46 /** … … 57 61 public double getDuration() { 58 62 return this.duration; 59 63 } 60 64 61 65 /** 66 * Gets the frame rate of the media in frames per second 67 * @return 68 * The frame rate 69 */ 70 public int getFrameRate () { 71 return this.frameRate; 72 } 73 /** 62 74 * Gets the dimensions of the video content. This is not the size of the 63 75 * frame video content displaying this media, but the actual width and 64 76 * height of the video frames. -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinWriter.java
37 37 } 38 38 this.data[this.size++] = b; 39 39 } 40 41 private void addBytes(byte[] b) { 42 if (this.size + b.length > this.data.length) { 43 /* 44 * We want to set the new size to the closest power of 2, which is >= this.size + this.length 45 * Instead of looping through the powers and checking we do it via some bitwise operations 46 */ 47 int newLen = this.size + b.length - 1; 48 newLen |= newLen >> 1; 49 newLen |= newLen >> 2; 50 newLen |= newLen >> 4; 51 newLen |= newLen >> 8; 52 newLen |= newLen >> 16; 53 ++newLen; 54 55 byte[] oldData = this.data; 56 byte[] newData = new byte[newLen]; 57 System.arraycopy(oldData, 0, newData, 0, this.size); 58 this.data = newData; 59 } 60 System.arraycopy (b, 0, this.data, this.size, b.length); 61 this.size += b.length; 62 } 40 63 41 64 /** 42 65 * Writes an encoded int. … … 56 79 public void writeBinData(BinData value) { 57 80 writeInt(value.getSize()); 58 81 byte[] bytes = value.getBytes(); 59 for (int i = 0; i < value.getSize(); ++i) { 60 addByte(bytes[i]); 61 } 82 addBytes (bytes); 62 83 } 63 84 64 85 /** -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/FastByteArrayOutputStream.java
1 package org.sophie2.base.commons.util.bindata; 2 3 import java.io.OutputStream; 4 5 /** 6 * ByteArrayOutputStream implementation that doesn't synchronize methods 7 * and doesn't copy the data on toByteArray(). 8 */ 9 public class FastByteArrayOutputStream extends OutputStream { 10 /** 11 * The buffer 12 */ 13 protected byte[] buf = null; 14 /** 15 * The size of actual data written in the buffer 16 */ 17 protected int size = 0; 18 19 /** 20 * Constructs a stream with buffer capacity size 8K 21 */ 22 public FastByteArrayOutputStream() { 23 this(8 * 1024); 24 } 25 26 /** 27 * Constructs a stream with the given initial size 28 * @param initSize 29 * The given initial size 30 */ 31 public FastByteArrayOutputStream(int initSize) { 32 this.size = 0; 33 this.buf = new byte[initSize]; 34 } 35 36 /** 37 * Ensures that we have a large enough buffer for the given size. 38 * Marked final for easier inlining 39 */ 40 private final void verifyBufferSize(int sz) { 41 if (sz > this.buf.length) { 42 byte[] old = this.buf; 43 this.buf = new byte[Math.max(sz, 2 * this.buf.length )]; 44 System.arraycopy(old, 0, this.buf, 0, old.length); 45 old = null; 46 } 47 } 48 49 /** 50 * @return 51 * Returns the size of the written data. 52 */ 53 public int getSize() { 54 return this.size; 55 } 56 57 /** 58 * @return 59 * Returns the byte array containing the written data. Note that this 60 * array will almost always be larger than the amount of data actually 61 * written. Thus you should use the [0, size) interval of the returned array 62 */ 63 public byte[] toByteArray() { 64 return this.buf; 65 } 66 67 @Override 68 public final void write(byte b[]) { 69 verifyBufferSize(this.size + b.length); 70 System.arraycopy(b, 0, this.buf, this.size, b.length); 71 this.size += b.length; 72 } 73 74 @Override 75 public final void write(byte b[], int off, int len) { 76 verifyBufferSize(this.size + len); 77 System.arraycopy(b, off, this.buf, this.size, len); 78 this.size += len; 79 } 80 81 @Override 82 public final void write(int b) { 83 verifyBufferSize(this.size + 1); 84 this.buf[this.size++] = (byte) b; 85 } 86 87 /** 88 * Resets the size counter of the data, effectively writing over the old data when you 89 * perform writes. 90 */ 91 public void reset() { 92 this.size = 0; 93 } 94 } -
modules/org.sophie2.extra.func.spellcheck/src/test/java/org/sophie2/extra/func/spellcheck/SpellCheckDemoTest.java
327 327 String textStr = modelText.toString(); 328 328 329 329 List<String> misspelled = new LinkedList<String> (); 330 //SpellCheckUtility.getMisspelledWords (misspelled, null, textStr);330 SpellCheckUtility.getMisspelledWords (misspelled, null, textStr, null); 331 331 332 332 DefaultListModel model = (DefaultListModel) listMisspelled.getModel(); 333 333 -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/BinData.java
126 126 * if occurs :) 127 127 */ 128 128 public static void transport(InputStream from, OutputStream to) throws IOException { 129 byte[] buffer = new byte[1 << 1 4];129 byte[] buffer = new byte[1 << 16]; 130 130 int read; 131 131 while ((read = from.read(buffer)) != -1) { 132 132 to.write(buffer, 0, read); … … 144 144 * if occurs 145 145 */ 146 146 public static void transport(Reader reader, Writer writer) throws IOException { 147 char[] buffer = new char[1 << 1 4];147 char[] buffer = new char[1 << 16]; 148 148 int read; 149 149 while ((read = reader.read(buffer)) != -1) { 150 150 writer.write(buffer, 0, read); … … 169 169 */ 170 170 protected void transport(InputStream from, OutputStream to, int size) throws IOException { 171 171 assert size > 0; 172 byte[] buffer = new byte[1 << 1 4];172 byte[] buffer = new byte[1 << 16]; 173 173 int remaining = size; 174 174 int read; 175 175 while ((read = from.read(buffer, 0, Math.min(remaining, buffer.length))) != -1