FileDocCategorySizeDatePackage
NativeEncoder.javaAPI DocJMF 2.1.1e10392Mon May 12 12:20:48 BST 2003com.sun.media.codec.audio.mpa

NativeEncoder

public class NativeEncoder extends AudioCodec

Fields Summary
private int
nativeData
protected int
INSIZE
protected int
OUTSIZE
byte[]
encData
int
encDataLen
BitRateControl
bra
Control[]
controls
boolean
settingsChanged
private int
numFramesPerOutputBuffer
private int
numFramesInBuffer
private int
outOffset
static Hashtable
bitRateTable
Constructors Summary
public NativeEncoder()


     
	bitRateTable.put(new Integer(32000), new Integer(48000));
	bitRateTable.put(new Integer(44100), new Integer(64000));
	bitRateTable.put(new Integer(48000), new Integer(64000));

	bitRateTable.put(new Integer(16000), new Integer(24000));
	bitRateTable.put(new Integer(22050), new Integer(32000));
	bitRateTable.put(new Integer(24000), new Integer(32000));
    

	inputFormats = new Format[] {

/*
 - crashes the VM.  Disables for 2.1.1 beta.

	    new AudioFormat(AudioFormat.LINEAR,
				16000.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
*/

	    new AudioFormat(AudioFormat.LINEAR,
				22050.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
	    new AudioFormat(AudioFormat.LINEAR,
				24000.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
/*
 - The decoder weren't able to handle the data generated.  
   Probably buggy data.  Disables for 2.1.1 beta.

	    new AudioFormat(AudioFormat.LINEAR,
				32000.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
*/

	    new AudioFormat(AudioFormat.LINEAR,
				44100.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
	    new AudioFormat(AudioFormat.LINEAR,
				48000.,
				16,
			    	Format.NOT_SPECIFIED,
				Arch.isBigEndian()?
				     AudioFormat.BIG_ENDIAN :
				     AudioFormat.LITTLE_ENDIAN,
				AudioFormat.SIGNED),
	};

	outputFormats = new Format[] {
	    /* Disabling MP3 encoding 
	    new AudioFormat(AudioFormat.MPEGLAYER3,
			    Format.NOT_SPECIFIED,
			    Format.NOT_SPECIFIED,
			    Format.NOT_SPECIFIED,
			    AudioFormat.BIG_ENDIAN,
			    Format.NOT_SPECIFIED),
	    */
	    new AudioFormat(AudioFormat.MPEG,
			    Format.NOT_SPECIFIED,
			    Format.NOT_SPECIFIED,
			    Format.NOT_SPECIFIED,
			    AudioFormat.BIG_ENDIAN,
			    Format.NOT_SPECIFIED),
	};

 	bra = new BitRateControl(this, 64000, 24000, 128000);
    
Methods Summary
public synchronized voidclose()

	if (nativeData != 0) {
	    nClose(nativeData);
	    nativeData = 0;
	}
    
private intcomputeBitRate(int sampleRate, int channels)


	Object o = bitRateTable.get(new Integer(sampleRate));

	int bitRate;

	if (o != null) {
	    bitRate = ((Integer)o).intValue();
	    bitRate *= channels;
	} else {
	    bitRate = 64000; // Default bitrate
	}

	return bitRate;
    
public intgetBitRate()

	return bra.getBitRate();
    
public java.lang.Object[]getControls()

	if (controls == null) {
	     controls = new Control[1];
	     controls[0] = bra;
	}
	return (Object[])controls;
    
public java.lang.StringgetName()

	return "MPEG Layer 2 Encoder";
    
public javax.media.Format[]getSupportedOutputFormats(javax.media.Format input)

	if (input == null) {
	    return outputFormats;
	} else if (input instanceof AudioFormat) {
	    AudioFormat af = (AudioFormat) input;
	    Format outputs[] = new Format[] {
		        /* Disabling MP3 encoding
			new AudioFormat (AudioFormat.MPEGLAYER3,
					 af.getSampleRate(),
					 af.getSampleSizeInBits(),
					 af.getChannels(),
					 AudioFormat.BIG_ENDIAN,
					 AudioFormat.SIGNED),
		        */
			new AudioFormat (AudioFormat.MPEG,
					 af.getSampleRate(),
					 af.getSampleSizeInBits(),
					 af.getChannels(),
					 AudioFormat.BIG_ENDIAN,
					 AudioFormat.SIGNED),
		};
	    return outputs;
	}

	return new Format[0];
    
public intisLayer3()

	/* Disabling MP3
	if (outputFormat != null && 
	    outputFormat.getEncoding().equalsIgnoreCase(AudioFormat.MPEGLAYER3)) {
	    return 1;
	} else {
	    return 0;
	}
	*/
	return 0;
    
public native booleannClose(int instance)

public native intnConvert(int instance, byte[] inBuf, int inOffset, int inLen, byte[] outBuf, int outOffset)

public native intnFlush(int instance, byte[] outBuf)

public native intnOpen(javax.media.format.AudioFormat format)

public synchronized voidopen()

	if (nativeData != 0)
	    close();
	
      	try {
            JMFSecurityManager.loadLibrary("jmutil");
            JMFSecurityManager.loadLibrary("jmmpa");
	    nativeData = nOpen((AudioFormat)outputFormat);
	    if (nativeData == 0) {
		throw new ResourceUnavailableException("could not open " + getName());
	    }
	    encData = new byte[INSIZE*numFramesPerOutputBuffer];
	    return;
	} catch (Throwable e) {
	}
	throw new ResourceUnavailableException("could not open " + getName());
    
public synchronized intprocess(javax.media.Buffer in, javax.media.Buffer out)


	if (isEOM(in)) {
	    int len = nFlush(nativeData, (byte [])out.getData());
	    if (len > 0) {
		out.setOffset(0);
		out.setLength(len);
	    } else
		out.setLength(0);

	    out.setFormat(outputFormat);
	    out.setEOM(true);
	    if (settingsChanged) {
		reset();
		settingsChanged = false;
	    }
	    return BUFFER_PROCESSED_OK;
	}

	if (!(in.getData() instanceof byte[])) {
	    return BUFFER_PROCESSED_FAILED;
	}

	int inOffset = in.getOffset();
	int inLength = in.getLength();
	byte inData[] = (byte[])in.getData();
	byte outData[] = (byte[])out.getData();

	// Check the output buffer size.
	if (outData == null || outData.length < (OUTSIZE*numFramesPerOutputBuffer)) {
	    outData = new byte[OUTSIZE*numFramesPerOutputBuffer];
	    out.setData(outData);
	}

	int rtn = 0;

	if (nativeData != 0) {

	    //System.err.println("inOffset = " + inOffset);
	    while ((encDataLen < INSIZE*numFramesPerOutputBuffer) && 
                     (inOffset < inLength)) {
		encData[encDataLen] = inData[inOffset];
		inOffset++; 
		encDataLen++;
	    }

	    //System.err.println("inOffset = " + inOffset + " encDataLen = " + encDataLen + " inLength = " + inLength);
	    if (encDataLen >= INSIZE*numFramesPerOutputBuffer) { 
		// We have enough samples to encode.

		for (int i = 0; i < numFramesPerOutputBuffer; i++) {

		    int returnVal = 
			nConvert(nativeData, (byte[])encData, 
				 (INSIZE*i), ////// inOffset, 
				 INSIZE, (byte[])out.getData(), outOffset);
		    
		    if (returnVal < 0) {
			return BUFFER_PROCESSED_FAILED;
		    }

		    numFramesInBuffer++;
		    outOffset += returnVal;
		}
		encDataLen = 0;
		numFramesInBuffer = 0;
		if (outOffset == 0) {
		    rtn |= OUTPUT_BUFFER_NOT_FILLED;
		} else {
		    out.setOffset(0);
		    out.setLength(outOffset);
		    out.setFormat(outputFormat);
		    outOffset = 0;
		}
	    } else {
 		// We are not done with the output buffer.
 		rtn |= OUTPUT_BUFFER_NOT_FILLED;
 	    }
	}

	if (inOffset < inLength) {
	    // We are not done with the input buffer.
	    in.setOffset(inOffset);
	    rtn |= INPUT_BUFFER_NOT_CONSUMED;
	}




	if (rtn == BUFFER_PROCESSED_OK && settingsChanged) {
	    reset();
	    settingsChanged = false;
	}
	return rtn;
    
public synchronized voidreset()

	if (nativeData != 0) {
	    close();
	    try {
		open();
	    } catch (ResourceUnavailableException rue) {
		System.err.println("MP2 Encoder: " + rue);
	    }
	}
    
public javax.media.FormatsetOutputFormat(javax.media.Format format)

      if (matches(format, getSupportedOutputFormats(inputFormat)) == null)
          return null;
      if (!(format instanceof AudioFormat))
          return null;


      AudioFormat in = (AudioFormat) format;

      double sampleRate = in.getSampleRate();
      int channels = in.getChannels();

      int bitRate = computeBitRate((int) sampleRate, channels);
      bra.setBitRate(bitRate);

      outputFormat = new AudioFormat(
				     in.getEncoding(),
				     sampleRate,
				     in.getSampleSizeInBits(),
				     channels,
				     in.getEndian(),
				     in.getSigned(),
				     8, // in.getFrameSizeInBits(),
				     getBitRate() / 8,// in.getFrameRate(),
				     in.getDataType()
				     );
      return outputFormat;