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
     
    1010 
    1111import org.sophie2.base.commons.util.Base64; 
    1212import org.sophie2.core.prolib.annot.Immutable; 
     13import org.sophie2.base.commons.util.bindata.FastByteArrayOutputStream; 
    1314 
    1415/** 
    1516 * Immutable binary data. 
     
    115116         */ 
    116117        public RawBinData(InputStream input, int size) throws IOException { 
    117118                // should read exactly size bytes 
    118                 ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     119                FastByteArrayOutputStream os = new FastByteArrayOutputStream(size); 
    119120                transport(input, os, size); 
    120121                this.data = os.toByteArray(); 
    121122        } 
     
    137138 
    138139        @Override 
    139140        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; 
    143145        } 
    144146 
    145147 
  • modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/NativeMediaHandler.java

     
    106106                                // If there are some requests for AudioChunks... 
    107107                                if (!reqQueue.isEmpty()) { 
    108108                                        lastRequest = reqQueue.poll(); 
    109                                         SophieLog.debug("Fetching request for position " + lastRequest); 
     109                                        //SophieLog.debug("Fetching request for position " + lastRequest); 
    110110 
    111111                                        // If the requested chunk is not in the cache retrieve it 
    112112                                        // from the natives and put it in the cache. 
     
    135135                                         
    136136                                        //Prefetch: 
    137137                                        if (nextNeeded != -1) { 
    138                                                 SophieLog.debug("Prefetching: " + nextNeeded); 
     138                                                //SophieLog.debug("Prefetching: " + nextNeeded); 
    139139                                                this.audioCache.put(nextNeeded, getNativeAudio(nextNeeded)); 
    140140                                        } else { 
    141141                                                // If there is no request yet just sleep for a while... 
     
    200200                        MediaInfoResponse response = getVideoBridge().sendCommand(cmd, MediaInfoResponse.class); 
    201201                        ImmSize size = new ImmSize(response.getWidth(), response.getHeight()); 
    202202                        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()); 
    204204                } 
    205205                return this.info; 
    206206 
     
    242242        public ImmImage getFrame(long millis) { 
    243243                assert millis >= 0 && millis <= Integer.MAX_VALUE; 
    244244                // 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) { 
    246248                        assert this.lastFrame != null; 
    247249                        return this.lastFrame; 
    248250                } 
    249251                this.lastFrame = getVideoBridge().getFrame((int) millis % getModLen()); 
    250252                assert this.lastFrame != null; 
    251253                this.lastFrameMillis = millis; 
     254                 
    252255                return this.lastFrame; 
    253256        } 
    254257 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinReader.java

     
    2323public final class BinReader { 
    2424        private final BinData data; 
    2525        private int pos; 
     26        private byte[] bytes; 
    2627 
    2728        /** 
    2829         * Constructor 
     
    3132        public BinReader(BinData data) { 
    3233                this.data = data; 
    3334                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(); 
    3442        } 
    3543 
    3644        /** 
     
    9199        private byte readByte() { 
    92100                assert this.pos < this.data.getSize() : 
    93101                        "pos=" + this.pos + " ,size=" + this.data.getSize(); 
    94                 return this.data.getByte(this.pos++); 
     102                 
     103                return this.bytes[this.pos++]; 
    95104        } 
    96105 
    97106        private byte[] readArray(int len) { 
    98107                // TODO may be written faster if needed 
    99108                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                 
    103113                return res; 
    104114        } 
    105115 
  • modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/MediaInfoResponse.java

     
    1313        private final int millis; 
    1414        private final int width; 
    1515        private final int height; 
     16        private final int approxFrameRate; 
    1617        private final boolean hasAudio; 
    1718 
    1819    /** 
     
    2930            this.height = reader.readInt(); 
    3031            int temp = reader.readInt(); 
    3132            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; 
    3236            reader.assertEnd(); 
    3337    } 
    3438 
     
    6670        public int getHeight() { 
    6771                return this.height; 
    6872        } 
     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         
    6983} 
  • modules/org.sophie2.base.media/src/main/java/org/sophie2/base/media/MediaInfo.java

     
    1616         * Represents an empty media info. 
    1717         */ 
    1818        public static final MediaInfo EMPTY_INFO = new MediaInfo(0, 
    19                         ImmImage.DUMMY_IMAGE.getSize(), false); 
     19                        ImmImage.DUMMY_IMAGE.getSize(), 0, false); 
    2020         
    2121        private final double duration; 
    2222        private final ImmSize dimensions; 
    2323        private final boolean hasAudio; 
     24        private final int frameRate; 
    2425 
    2526        /** 
    2627         * Constructs a media info. 
     
    2930         *            - the duration of the media in seconds. 
    3031         * @param dimensions 
    3132         *            - the dimensions (width and height) of the video content. 
     33         * @param frameRate  
     34         *                        - the frame rate of the video content. 
    3235         * @param hasAudio 
    3336         *            - whether this medias has audio. 
    3437         *  
    3538         */ 
    36         public MediaInfo(double duration, ImmSize dimensions, boolean hasAudio) { 
     39        public MediaInfo(double duration, ImmSize dimensions, int frameRate, boolean hasAudio) { 
    3740                this.duration = duration; 
    3841                this.dimensions = dimensions; 
    3942                this.hasAudio = hasAudio; 
     43                this.frameRate = frameRate; 
    4044        } 
    4145         
    4246        /** 
     
    5761        public double getDuration() { 
    5862                return this.duration; 
    5963        } 
    60  
     64         
    6165        /** 
     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        /** 
    6274         * Gets the dimensions of the video content. This is not the size of the 
    6375         * frame video content displaying this media, but the actual width and 
    6476         * height of the video frames. 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinWriter.java

     
    3737                } 
    3838                this.data[this.size++] = b; 
    3939        } 
     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        } 
    4063 
    4164        /** 
    4265         * Writes an encoded int. 
     
    5679        public void writeBinData(BinData value) { 
    5780                writeInt(value.getSize()); 
    5881                byte[] bytes = value.getBytes();  
    59                 for (int i = 0; i < value.getSize(); ++i) { 
    60                         addByte(bytes[i]); 
    61                 } 
     82                addBytes (bytes); 
    6283        } 
    6384 
    6485        /** 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/FastByteArrayOutputStream.java

     
     1package org.sophie2.base.commons.util.bindata; 
     2 
     3import java.io.OutputStream; 
     4 
     5/** 
     6 * ByteArrayOutputStream implementation that doesn't synchronize methods 
     7 * and doesn't copy the data on toByteArray(). 
     8 */ 
     9public 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

     
    327327                                String textStr = modelText.toString(); 
    328328 
    329329                                List<String> misspelled = new LinkedList<String> (); 
    330                                 //SpellCheckUtility.getMisspelledWords (misspelled, null, textStr); 
     330                                SpellCheckUtility.getMisspelledWords (misspelled, null, textStr, null); 
    331331 
    332332                                DefaultListModel model = (DefaultListModel) listMisspelled.getModel(); 
    333333 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/BinData.java

     
    126126         *             if occurs :) 
    127127         */ 
    128128        public static void transport(InputStream from, OutputStream to) throws IOException { 
    129                 byte[] buffer = new byte[1 << 14]; 
     129                byte[] buffer = new byte[1 << 16]; 
    130130                int read; 
    131131                while ((read = from.read(buffer)) != -1) { 
    132132                        to.write(buffer, 0, read); 
     
    144144         *             if occurs 
    145145         */ 
    146146        public static void transport(Reader reader, Writer writer) throws IOException { 
    147                 char[] buffer = new char[1 << 14]; 
     147                char[] buffer = new char[1 << 16]; 
    148148                int read; 
    149149                while ((read = reader.read(buffer)) != -1) { 
    150150                        writer.write(buffer, 0, read); 
     
    169169         */ 
    170170        protected void transport(InputStream from, OutputStream to, int size) throws IOException { 
    171171                assert size > 0; 
    172                 byte[] buffer = new byte[1 << 14]; 
     172                byte[] buffer = new byte[1 << 16]; 
    173173                int remaining = size; 
    174174                int read; 
    175175                while ((read = from.read(buffer, 0, Math.min(remaining, buffer.length))) != -1