Package javax.media

Examples of javax.media.Buffer


         */
        private boolean contiguous() {
            int expect = 0;    // next expected offset.

            for (int i = 0; i < list.size(); ++i) {
                final Buffer b = (Buffer) list.get(i);
                final JpegRTPHeader jpegRtpHeader = parseJpegRTPHeader(b);
                int otherOffset = 0;
                if (jpegRtpHeader.getType()>63) otherOffset += 4;
                if (jpegRtpHeader.getQ()>=128) {
                    int length = 0;
                    int j = b.getOffset() + JpegRTPHeader.HEADER_SIZE + otherOffset + 2; //the size of the offset to the length of custom tables
                    byte[] data = (byte[]) b.getData();
                    length = data[j++] & 0xFF;
                    length <<= 8;
                    length |= data[j] & 0xFF;
                    length += 4; //add 4 bytes that are not included in the length of custom qtables
                    otherOffset += length;
                }
                if (jpegRtpHeader.getFragmentOffset() != expect)
                    return false;
                expect += b.getLength() - JpegRTPHeader.HEADER_SIZE - otherOffset;

            }
            return true;
        }
View Full Code Here


                throw new IllegalStateException();
            if (list.size() <= 0)
                throw new IllegalStateException();

            // calculate from offset and length of last buffer:
            final Buffer b = (Buffer) list.get(list.size() - 1);
            final JpegRTPHeader jpegRtpHeader = parseJpegRTPHeader(b);
            // Observed: the frame with the marker has valid offset,
            return jpegRtpHeader.getFragmentOffset() + b.getLength() - JpegRTPHeader.HEADER_SIZE;

        }
View Full Code Here

                throw new IllegalStateException();

            // TODO: perhaps what we should do is copy the header if there is not one.
            // The test samples coming from JMStudio had headers in them, so the JPEG could not be
            // parsed if (another) header was prepended.
            final Buffer bFirst = (Buffer) list.get(0);
            final boolean prependHeader = !hasJPEGHeaders((byte[]) bFirst.getData(), bFirst.getOffset() + JpegRTPHeader.HEADER_SIZE, bFirst.getLength() - JpegRTPHeader.HEADER_SIZE);
            final int MAX_HEADER = prependHeader ? 1024 : 0;    // TODO: what is actual max size for the header?  Seems to be fixed size, 600-700 bytes.
            final int MAX_TRAILER = 2;
            final int frameLength = frameLength();
            final byte[] data;
            int inputOffset = bFirst.getOffset();
            int dri = 0;
            byte[] lqt = null;
            byte[] cqt = null;
            byte[] inputData = (byte[]) bFirst.getData();
            if (bDest.getData() != null && ((byte[]) bDest.getData()).length >= (frameLength + MAX_HEADER + MAX_TRAILER)) {
                data = (byte[]) bDest.getData();    // reuse existing byte array
                // zero out:
                zeroData(data);

            } else {
                data = new byte[frameLength + MAX_HEADER + MAX_TRAILER];    // allocate new one - existing one not there or too short
            }
//            System.out.println(Arrays.toString(inputData));
//            System.out.println("Offset:"+inputOffset);

            /*System.out.println("InputData");
            for (byte b:inputData){
                String s = Integer.toHexString(b&0xFF);
                System.out.print((s.length()<2?"0"+s:s)+"-");
            }
            System.out.println("");*/
           
            int offsetAfterHeaders = 0;
            if (prependHeader) {
                // put initial SOI marker manually, we tell RFC2035.MakeHeaders not to do it:
                data[offsetAfterHeaders++] = (byte) 0xff;
                data[offsetAfterHeaders++] = (byte) 0xd8;

                // part of the header with "JFIF" in it, not generated by the code in RFC2035.
                offsetAfterHeaders = buildJFIFHeader(data, offsetAfterHeaders);

                final JpegRTPHeader jpegRtpHeaderFirst = parseJpegRTPHeader(bFirst);
                inputOffset += JpegRTPHeader.HEADER_SIZE;

                if (jpegRtpHeaderFirst.getType() >= 64 && jpegRtpHeaderFirst.getType() <= 127) { //For types 64-127 there has to be restart header

                    dri = inputData[inputOffset++] & 0xFF;
                    dri <<= 8;
                    dri |= inputData[inputOffset++] & 0xFF;
                    inputOffset += 2; //skip rest of the Restart Marker header (2 bytes)
                }
                if (jpegRtpHeaderFirst.getQ() > 127) {
                    inputOffset += 2; //skip MBZ and Precision fields
                    int length = inputData[inputOffset++] & 0xFF;
                    length <<= 8;
                    length |= inputData[inputOffset++] & 0xFF;
                    //Most usual qtable size is 64 bytes, so length is usually 128, for other sizes we would put half to lqt and second half to cqt..

                    // lqt = Arrays.copyOfRange(inputData, inputOffset, inputOffset + length / 2); //Java6 only code
                    lqt = ArrayUtility.copyOfRange(inputData, inputOffset, inputOffset + length /2);
                    inputOffset += length / 2; //TODO what if the length is odd?
                    // (It should not happen though)
                    //cqt = Arrays.copyOfRange(inputData, inputOffset, inputOffset+ length / 2); //Java6 only code
                    cqt = ArrayUtility.copyOfRange(inputData, inputOffset, inputOffset + length /2);
                    inputOffset += length / 2;
                }
                //TODO add special handling if q>128, but first we should handle Reset Header
                offsetAfterHeaders = RFC2035.MakeHeaders(false, data, offsetAfterHeaders, jpegRtpHeaderFirst.getType(), jpegRtpHeaderFirst.getQ(), jpegRtpHeaderFirst.getWidthInBlocks(), jpegRtpHeaderFirst.getHeightInBlocks(), lqt, cqt, dri);

                /*System.out.println("OutputData");
              for (byte b:data){
                  String s = Integer.toHexString(b&0xFF);
                  System.out.print((s.length()<2?"0"+s:s)+"-");
              }
              System.out.println("");*/
            }
            if (TRACE) System.out.println("offsetAfterHeaders=" + offsetAfterHeaders);
            for (int i = 0; i < list.size(); ++i) {
                final Buffer b = (Buffer) list.get(i);
                final JpegRTPHeader jpegRtpHeader = parseJpegRTPHeader(b);

//        if (TRACE) System.out.println("Copying, length=" + (b.getLength() - JpegRTPHeader.HEADER_SIZE) + ":");
//        if (TRACE) System.out.println(dump((byte[]) b.getData(), b.getOffset() + JpegRTPHeader.HEADER_SIZE, (b.getLength() - JpegRTPHeader.HEADER_SIZE) > MAX_DUMP_SIZE ? MAX_DUMP_SIZE : (b.getLength() - JpegRTPHeader.HEADER_SIZE)));
//        if (TRACE) System.out.println("End copying.");

                System.arraycopy(b.getData(), b.getOffset() + JpegRTPHeader.HEADER_SIZE, data, offsetAfterHeaders + jpegRtpHeader.getFragmentOffset(), b.getLength() - JpegRTPHeader.HEADER_SIZE);
            }

            final boolean appendEOI = !hasJPEGTrailer(data, offsetAfterHeaders + frameLength, MAX_TRAILER);    // no need to append if it is already there.
            int trailing = 0;
            if (appendEOI) {
View Full Code Here

      final int result = filterGraph.process(null, trackNumber, -1, flags);
      if ((result & PlugIn.BUFFER_PROCESSED_FAILED) != 0)
      {  throw new RuntimeException("BUFFER_PROCESSED_FAILED")// TODO: what is the right thing to do?
      }
     
      final Buffer b = filterGraph.getRoot().getOutputBuffer(trackNumber);
      if (b == null)
        throw new NullPointerException();
      return b;
     
    }
View Full Code Here

              flags = firstFlags;  // if firstFlags == FilterGraph.SUPPRESS_TRACK_READ, first frame has already been read, only needs redisplay
            else if (inputEOM)
              flags = FilterGraph.SUPPRESS_TRACK_READ;
            else
              flags = FilterGraph.PROCESS_DEFAULT;
            Buffer b = readAndProcessNextFrame(flags);
            if (isClosing())
              return;
            if (first)
            {  first = false;
            }
            if (b.isEOM())
              inputEOM = true// we continue processing, however, until the final buffer in the chain gets EOM.
                        // this is needed to handle the case where there is residual data in the codecs.
                        //TODO: how close is this to what JMF does?  How does JMF handle this?
                        // if we don't do this, the effect is that codecs that are not 1:1 in terms of buffers will have any
                        // data buffered up in the codecs cut off. 
           
            if (b.isDiscard())
              continue;
           

           
            if (!(penultimateNode instanceof RendererNode) && !(penultimateNode instanceof MuxNode))
            {
              // we need to sleep based on the time in the processed data, not the input data. 
              // if the graph goes demux->renderer, then the output buffer of the demux,
              // which is set to b above, is what we use to check.
              b = penultimateNode.getOutputBuffer(0);
              // TODO: this can cause a NPE below if b is null (not sure how it could be null)
 
              if (b == null)
                continue// this can be null if it has not been set yet, if codecs earlier in the chain have been
                      // returning INPUT_BUFFER_NOT_CONSUMED or OUTPUT_BUFFER_NOT_FILLED.
            }
           
            if (b.isEOM())  // TODO: is there data in an eom buffer?
              break;
            if (b.isDiscard())
              continue;
           
                  //  Update the video time
                  nanoseconds = b.getTimeStamp();
          }
         
          eom = true;
          checkAllTracksEOM();
        }
View Full Code Here

    {
      // TODO: this is very inefficient - it allocates a new byte array (or more) every time
      final ByteArrayInputStream is = new ByteArrayInputStream((byte[]) input.getData(), input.getOffset(), input.getLength());
      final BufferedImage image = ImageIO.read(is);
      is.close();
      final Buffer b = ImageToBuffer.createBuffer(image, ((VideoFormat) outputFormat).getFrameRate());
     
      output.setData(b.getData());
      output.setOffset(b.getOffset());
      output.setLength(b.getLength());
      output.setFormat(b.getFormat())// TODO: this is a bit hacky, this format will be more specific than the actual set output format, because now we know what ImageIO gave us for a BufferedImage as far as pixel masks, etc.
     
      return BUFFER_PROCESSED_OK;

    }
    catch (IOException e)
View Full Code Here

  {
    final MyBasicPlugIn p = new MyBasicPlugIn();
   
    // test empty buffer:
    {
      final Buffer b = new Buffer();
      assertEquals(b.getData(), null);
      assertEquals(b.getLength(), 0);
      assertEquals(b.getOffset(), 0);
     
      for (int i = 0; i < 10; ++i)
      {
        final int[] ba = p.doValidateIntArraySize(b, i);
        assertEquals(ba.length, i);
        for (int j = 0; j < i; ++j)
        {  assertEquals(ba[j], 0);
        }
        assertEquals(b.getData(), ba);
        assertEquals(b.getLength(), 0);
        assertEquals(b.getOffset(), 0);
      }
    }
   
    // buf of len 5 with length set to 5:
    {
      final Buffer b = new Buffer();
      final int[] bBuf = new int[5];
      b.setData(bBuf);
      b.setLength(bBuf.length);
      assertTrue(b.getData() == bBuf);
      assertEquals(b.getLength(), bBuf.length);
      assertEquals(b.getOffset(), 0);
     
      for (int i = 0; i < 10; ++i)
      {
        final int[] ba = p.doValidateIntArraySize(b, i);
        if (i >  bBuf.length)
          assertTrue(ba != bBuf);
        else
          assertTrue(ba == bBuf);
        final int max = i > bBuf.length ? i : bBuf.length;
        assertEquals(ba.length, max);
        for (int j = 0; j < i; ++j)
        {  assertEquals(ba[j], 0);
        }
        assertEquals(b.getData(), ba);
        assertEquals(b.getLength(), bBuf.length);
        assertEquals(b.getOffset(), 0);
      }
    }
   
    // buf of len 5 with length set to 0:
    {
      final Buffer b = new Buffer();
      final int[] bBuf = new int[5];
      b.setData(bBuf);
      b.setLength(0);
      assertTrue(b.getData() == bBuf);
      assertEquals(b.getLength(), 0);
      assertEquals(b.getOffset(), 0);
     
      for (int i = 0; i < 10; ++i)
      {
        final int[] ba = p.doValidateIntArraySize(b, i);
        if (i >  bBuf.length)
          assertTrue(ba != bBuf);
        else
          assertTrue(ba == bBuf);
        final int max = i > bBuf.length ? i : bBuf.length;
        assertEquals(ba.length, max);
        for (int j = 0; j < i; ++j)
        {  assertEquals(ba[j], 0);
        }
        assertEquals(b.getData(), ba);
        assertEquals(b.getLength(), 0);
        assertEquals(b.getOffset(), 0);
      }
    }
   
    // it appears that getLength/setLength has nothing to do with doValidateIntArraySize.
    // doValidateIntArraySize looks like it checks the size of the buf, and reallocates it
    // if too small.
   
    // try with a non-intarray
    {
      final Buffer b = new Buffer();
      final short[] bBuf = new short[5];
      b.setData(bBuf);
      b.setLength(0);
      assertTrue(b.getData() == bBuf);
      assertEquals(b.getLength(), 0);
      assertEquals(b.getOffset(), 0);
     
      for (int i = 0; i < 10; ++i)
      {
        final int[] ba = p.doValidateIntArraySize(b, i);
//        if (i >  bBuf.length)
//          assertTrue(ba != bBuf);
//        else
//          assertTrue(ba == bBuf);
        assertEquals(ba.length, i);
        for (int j = 0; j < i; ++j)
        {  assertEquals(ba[j], 0);
        }
        assertEquals(b.getData(), ba);
        assertEquals(b.getLength(), 0);
        assertEquals(b.getOffset(), 0);
      }
    }
   
    // appears to simply reallocate if not a int array.
    // See if it copies existing data:
    {
      final Buffer b = new Buffer();
      final int[] bBuf = new int[] {0, 1, 2, 3, 4};
      b.setData(bBuf);
      b.setLength(0);
      assertTrue(b.getData() == bBuf);
      assertEquals(b.getLength(), 0);
      assertEquals(b.getOffset(), 0);
     
      for (int i = 0; i < 10; ++i)
      {
        final int[] ba = p.doValidateIntArraySize(b, i);
        if (i >  bBuf.length)
          assertTrue(ba != bBuf);
        else
          assertTrue(ba == bBuf);
        final int max = i > bBuf.length ? i : bBuf.length;
        assertEquals(ba.length, max);
        for (int j = 0; j < i; ++j)
        { 
          if (j < bBuf.length)
            assertEquals(ba[j], bBuf[j]);
          else
            assertEquals(ba[j], 0);
        }
        assertEquals(b.getData(), ba);
        assertEquals(b.getLength(), 0);
        assertEquals(b.getOffset(), 0);
      }
    }
   
    // it does copy existing data.
View Full Code Here

    }
   
  }
  public void testBuffer(Buffer b1)
  {
    final Buffer b2 = (Buffer) b1.clone();
    assertTrue(b1.getData() != b2.getData());
    assertTrue(b2.getData() != null);
    assertTrue(b1.getFlags() == b2.getFlags());
    assertTrue(b1.getFormat() == b2.getFormat());
    if (b1.getHeader() != null)
      assertTrue(b1.getHeader() != b2.getHeader());
    else
      assertTrue(null == b2.getHeader());
     
    //assertTrue(b2.getFormat() == null);
   
    final Buffer b3 = new Buffer();
    b3.copy(b1);
    assertTrue(b1.getData() == b3.getData());
    assertTrue(b1.getFlags() == b3.getFlags());
    assertTrue(b1.getFormat() == b3.getFormat());
    if (b1.getHeader() != null)
      assertTrue(b1.getHeader() == b3.getHeader());
    else
      assertTrue(null == b3.getHeader());
     
    final Buffer b4 = new Buffer();
    b4.copy(b1, true);
    assertTrue(b1.getData() != b4.getData());
    assertTrue(b4.getData() != null);
    assertTrue(b1.getFlags() == b4.getFlags());
    assertTrue(b1.getFormat() == b4.getFormat());
    if (b1.getHeader() != null)
      assertTrue(b1.getHeader() == b4.getHeader());
    else
      assertTrue(null == b4.getHeader());

 
    final Buffer b5 = new Buffer();
    b5.copy(b1, false);
    assertTrue(b1.getData() == b5.getData());
    assertTrue(b1.getFlags() == b5.getFlags());
    assertTrue(b1.getFormat() == b5.getFormat());
    if (b1.getHeader() != null)
      assertTrue(b1.getHeader() == b5.getHeader());
    else
      assertTrue(null == b5.getHeader());

   
  }
View Full Code Here

   
    public void read(Buffer buffer) throws IOException
    {
      if (TRACE) System.out.println(getClass().getSimpleName() + " read");
     
      Buffer next = null;
     
      try
      {
                next = (Buffer)jitterBuffer.get();
      }
      catch ( Exception ex )
      {
      }
     
      if ( (next == null) || next.isEOM() )
      {
        eos = true;
      }
     
      if (next != null)
      {
        if (buffer.getData() == null)
          buffer.copy(next, false)
        else
        {
          // according to the API, if the caller sets the
          // data in the buffer, we should not allocate it.
          // See http://java.sun.com/products/java-media/jmf/2.1.1/apidocs/javax/media/protocol/PushBufferStream.html
         
          // we use the original offset in the buffer in this case, since that is what is required
          // by FMJ's RTPSendStream.transferData(PushBufferStream stream).  This feature does not appear to be
          // defined in the API.
         
          final Object originalData = buffer.getData();
          final int originalOffset = buffer.getOffset();
          final int originalLength =  arrayLength(buffer.getData());
          buffer.copy(next, false);
          buffer.setData(originalData);
          buffer.setOffset(originalOffset);
          // length is set in copy
         
          // mgodehardt: will measure thruput in bits per second for the BitRateControl
          // dont know if this the right place to measure the thruput
          long currentTimestamp = System.nanoTime();
          if ( -1 == lastTimestamp )
          {
            lastTimestamp = currentTimestamp;
          }
         
          bytesProcessed += buffer.getLength();
         
          if ( (buffer.getFlags() & Buffer.FLAG_RTP_MARKER) > 0 )
          {
            framesProcessed++;
          }
         
          if ( (currentTimestamp - lastTimestamp) > 1000000000L )
          {
            bitsPerSecond = bytesProcessed << 3;
           
            float diffTime = (float)(currentTimestamp - lastTimestamp) / 1000000L;
            frameRate = (float)framesProcessed * (1000.0f / diffTime);
           
            bytesProcessed = 0;
            framesProcessed = 0;
            lastTimestamp = currentTimestamp;
          }
         
          if (next.getLength() > 0)
          { 
            // TODO: what if original data is not big enough?
            if (next.getLength() > originalLength)
            { 
              logger.warning("Buffer passed in has length: " + originalLength + "; needs to be at least: " + next.getLength() + "; not able to re-use passed in buffer data");
              buffer.copy(next, false)
            }
            else
            {
              System.arraycopy(next.getData(), next.getOffset(), originalData, originalOffset, next.getLength());
            }
          }
        }
      }
      else
View Full Code Here

    final double scaleY = ((double) outputSize.height) / ((double) inputSize.height);
   
    final BufferedImage scaled = scale(image, scaleX, scaleY)// TODO: is the size exact?  what about rounding errors?
   
    System.out.println("scaled: " + scaled.getWidth() + "x" + scaled.getHeight());
    final Buffer b = ImageToBuffer.createBuffer(scaled, ((VideoFormat) outputFormat).getFrameRate());
    output.setData(b.getData());
    output.setLength(b.getLength());
    output.setOffset(b.getOffset());
    output.setFormat(b.getFormat());
    // TODO: what about format?
 
    return BUFFER_PROCESSED_OK;

   
View Full Code Here

TOP

Related Classes of javax.media.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.