FileDocCategorySizeDatePackage
JavaEncoder.javaAPI DocJMF 2.1.1e6122Mon May 12 12:21:02 BST 2003com.ibm.media.codec.audio.dvi

JavaEncoder

public class JavaEncoder extends AudioPacketizer
Implements an DVI codec which uses Java methods to do the decoding.

Fields Summary
private Buffer
pcmBuffer
private DVIState
dviState
private long
currentSeq
Constructors Summary
public JavaEncoder()


    ////////////////////////////////////////////////////////////////////////////
    // Methods

      

        packetSize=240;
       	supportedInputFormats = new AudioFormat[] {
	        new AudioFormat(
                    AudioFormat.LINEAR,
                    Format.NOT_SPECIFIED,
                    16,
		    1,
                    AudioFormat.LITTLE_ENDIAN,
                    AudioFormat.SIGNED,
      	            AudioFormat.NOT_SPECIFIED,
                    Format.NOT_SPECIFIED,
                    Format.byteArray
                )
        } ;
        defaultOutputFormats  = new AudioFormat[] {
	  new AudioFormat(AudioFormat.DVI_RTP,
			  AudioFormat.NOT_SPECIFIED,
			  4, // 4 bits per sample
			  1,
			  AudioFormat.NOT_SPECIFIED,        //isBigEndian(),
			  AudioFormat.NOT_SPECIFIED,        //isSigned());
			  AudioFormat.NOT_SPECIFIED,
			  AudioFormat.NOT_SPECIFIED,
			  Format.byteArray)
	      };

        PLUGIN_NAME="DVI Encoder";


    
Methods Summary
public java.lang.Object[]getControls()

        if (controls==null) {
             controls=new Control[2];
             controls[0]=new PacketSizeAdapter(this,packetSize,true);
             controls[1]=new com.sun.media.controls.SilenceSuppressionAdapter(this,false,false);
	}
        return (Object[])controls;
    
protected javax.media.Format[]getMatchingOutputFormats(javax.media.Format in)

      AudioFormat af =(AudioFormat) in;

      supportedOutputFormats = new AudioFormat[] {
	  new AudioFormat(AudioFormat.DVI_RTP,
			  af.getSampleRate(),
			  4, // 4 bits per sample
			  1,
			  AudioFormat.NOT_SPECIFIED,        //isBigEndian(),
			  AudioFormat.NOT_SPECIFIED,        //isSigned());
			  AudioFormat.NOT_SPECIFIED,
			  AudioFormat.NOT_SPECIFIED,
			  Format.byteArray)
	      };
      return  supportedOutputFormats;

    
public voidopen()

        dviState=new DVIState();

        setPacketSize(packetSize);
        reset();
    
public intprocess(javax.media.Buffer inputBuffer, javax.media.Buffer outputBuffer)
decode the buffer


       int rc = super.process(inputBuffer,pcmBuffer);
       if ( (rc & OUTPUT_BUFFER_NOT_FILLED) != 0)
          return rc;

       byte[] pcmData =(byte[]) pcmBuffer.getData();
       int inpLength =  pcmBuffer.getLength();


       int outLength = pcmBuffer.getLength()/4;
       byte[] outData = validateByteArraySize(outputBuffer,outLength + 4);

       // dump the DVI flags from dviState into the first 4 bytes of
       // every packet sent out. The 4 bytes comes from the size of
       // the dvi payload header in case of RTP
       outData[0] = (byte) (dviState.valprev >> 8);
       outData[1] = (byte) dviState.valprev;
       outData[2] = (byte) dviState.index;
       outData[3] = 0;

       DVI.encode(pcmData,
		  0,
		  outData,
		  4,
		  inpLength >> 1,
		  dviState);

       pcmBuffer.setOffset(0);
       pcmBuffer.setLength(0);

       outputBuffer.setSequenceNumber(currentSeq++);
       outputBuffer.setTimeStamp(pcmBuffer.getTimeStamp());

       updateOutput(outputBuffer,
		    outputFormat,
		    outLength+4,
		    0);

       return rc;
    
public voidreset()

        super.reset();
        dviState.valprev=0;
        dviState.index=0;
        pcmBuffer.setOffset(0);
        pcmBuffer.setLength(0);

    
public javax.media.FormatsetOutputFormat(javax.media.Format out)

    // depending on the freq of output format, we must set the packet
    // size to be equivalent to 60ms (will be changed to 40 or 20ms) 
    Format f = super.setOutputFormat(out);
    AudioFormat af = (AudioFormat)f;
    if (af.getSampleRate() == 8000)
      packetSize = 240;
    else if (af.getSampleRate() == 11025)
      packetSize = 330;
    else if (af.getSampleRate() == 22050)
      packetSize = 660;
    else if (af.getSampleRate() == 44100)
      packetSize = 1320;
    return f;
  
public synchronized voidsetPacketSize(int newPacketSize)

        packetSize=newPacketSize*4;  // this is pcmBuffer packetSize

        sample_count = packetSize/2;

        if (history==null) {
            history=new byte[packetSize];
            return;
        }

        if (packetSize > history.length ) {
            byte[] newHistory=new byte[packetSize];
            System.arraycopy(history,0,newHistory,0,historyLength);
            history=newHistory;
        }