fi.helsinki.cs.ohtu.mpeg2
Class AVPacketizer

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.AVPacketizer
Direct Known Subclasses:
VideoStream.VideoPacketizer

public class AVPacketizer
extends java.lang.Object

Implements a packetizer for elementary streams of audio and video. Instances of this class are intended to be used with a multiplexer. The multiplexer pulls data from the packetizer instance.

Data is written into the packetizer with the write method an AccessUnit at a time. Conceptually the written access units are placed one after another to form a stream of bytes. The stream is then broken into packets by the packetize method. By default access units are not aligned to packet boundaries. Thus an access unit can be split into two (or even more) packets. In some cases it desirable to have some control on the splitting. The method needPacketAlign is consulted on every access unit by the method packetize. When it returns true, the access unit will be placed to start at a packet boundary.

A sub-class of this class can insert a payload prefix by overriding the writePayloadPrefix method. The precise length of the prefix has to be returned by the method getPayloadPrefixLength. The prefix might be useful in implementing sub-stream headers, for instance.

Along with packetization, the class operates a decoder buffer model. It is used by the isMuxable method to determine when a decoder is able to accept more input. The method packetize appends packetized data into the decoder buffer (only virtually). The data are removed by the method isMuxable when they are declared to have been decoded. The buffer must not overflow as it would cause data to be lost (whether data-loss would really happen is a different thing).


Nested Class Summary
static class AVPacketizer.State
          Represents the state of a packetizer.
 
Field Summary
private  int auBytesLeft
           
private  java.util.List<AccessUnit> aus
           
private  boolean closed
           
private  DecoderBuffer decbuf
           
private  PacketizerListener listener
           
private  java.lang.Long prevPts
           
private  StreamID sid
           
private  long tboff
           
private static int TS_INTERVAL
          Number of clock ticks between forced PES timestamps
 
Constructor Summary
AVPacketizer(StreamID sid, int bufsize)
          Creates a new packetizer.
 
Method Summary
 void close()
          Closes this packetizer.
 void flush()
          Flushes this packetizer, that is, notifies the data listener if data is available.
 int getBufferSize()
          Gets the size of the decoder buffer.
 int getDesiredBuffering()
          Returns the desired number of bytes in the decoding buffer for smooth playback and operation.
 long getNextDts()
          Returns the decoding timestamp in the next generated packet.
protected  int getPayloadPrefixLength()
          Returns the length for a payload prefix.
 AVPacketizer.State getState()
          Returns the state of this packetizer.
 StreamID getStreamId()
          Gets the stream id for the PES stream.
 boolean isMuxable(long pcr, int maxSize)
          Updates the decoder buffer model and returns whether the stream from this packetizer can be multiplexed.
protected  boolean needPacketAlign(AccessUnit au)
          Gets whether the given access unit needs to be packet aligned.
 AudioVideoPESPacket packetize(int maxSize)
          Generates a PES packet.
 void setPacketizerListener(PacketizerListener listener)
          Sets the data availability listener for this packetizer.
 void setTimebaseOffset(long tboff)
          Sets the timebase offset for this packetizer.
 void write(AccessUnit au)
          Writes an access unit into this packetizer.
protected  void writePayloadPrefix(AudioVideoPESPacket avpkt, int payloadSize)
          Writes a payload prefix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TS_INTERVAL

private static final int TS_INTERVAL
Number of clock ticks between forced PES timestamps

See Also:
Constant Field Values

sid

private final StreamID sid

decbuf

private final DecoderBuffer decbuf

aus

private final java.util.List<AccessUnit> aus

listener

private PacketizerListener listener

tboff

private long tboff

auBytesLeft

private int auBytesLeft

prevPts

private java.lang.Long prevPts

closed

private boolean closed
Constructor Detail

AVPacketizer

public AVPacketizer(StreamID sid,
                    int bufsize)
Creates a new packetizer.

Parameters:
sid - the stream id for the generated PES packets
bufsize - the size of the decoder buffer, in bytes
Method Detail

needPacketAlign

protected boolean needPacketAlign(AccessUnit au)
Gets whether the given access unit needs to be packet aligned. If packet alignment is required, the access unit will be placed at the start of a packet.

This packetizer uses this method to get hints when to start a new packet. The default implementation returns true when an access unit is marked as discontinuous.

Parameters:
au - the access unit for which packet alignment is checked
Returns:
true if packet aligned is needed

getPayloadPrefixLength

protected int getPayloadPrefixLength()
Returns the length for a payload prefix.

Returns:
the payload prefix length, in bytes

writePayloadPrefix

protected void writePayloadPrefix(AudioVideoPESPacket avpkt,
                                  int payloadSize)
Writes a payload prefix. This method is called just before the actual payload data. Sub-classes can use this method to implement sub-stream headers, for example. Length of the prefix must be equal to the length returned by getPayloadPrefixLength.

Parameters:
avpkt - the packet where to write the prefix
payloadSize - the size of the payload data, in bytes

packetize

public AudioVideoPESPacket packetize(int maxSize)
Generates a PES packet.

Parameters:
maxSize - the maximum number of bytes in the packet

getNextDts

public long getNextDts()
Returns the decoding timestamp in the next generated packet.

Returns:
the DTS in the next generated packet, in ticks of the 90kHz clock

isMuxable

public boolean isMuxable(long pcr,
                         int maxSize)
Updates the decoder buffer model and returns whether the stream from this packetizer can be multiplexed.

Parameters:
pcr - the current program clock reference
maxSize - the maximum number of data bytes in a data packet
Returns:
true if the data can be multiplexed

getState

public AVPacketizer.State getState()
Returns the state of this packetizer.

Returns:
the state of this packetizer

setTimebaseOffset

public void setTimebaseOffset(long tboff)
Sets the timebase offset for this packetizer. The packetizer will add the offset to every PTS and DTS present during the packetization.

Parameters:
tboff - the timebase offset, in ticks of the 90kHz clock

getStreamId

public StreamID getStreamId()
Gets the stream id for the PES stream.

Returns:
the stream id

getBufferSize

public int getBufferSize()
Gets the size of the decoder buffer.

Returns:
the size of the decoder buffer, in bytes

getDesiredBuffering

public int getDesiredBuffering()
Returns the desired number of bytes in the decoding buffer for smooth playback and operation.

Returns:
the desired number of bytes

setPacketizerListener

public void setPacketizerListener(PacketizerListener listener)
Sets the data availability listener for this packetizer. The listener is notified at some point after new data is available. Setting the listener to null disables data availability notifications.

Parameters:
listener - the listener, or null to disable notifications

write

public void write(AccessUnit au)
Writes an access unit into this packetizer.

Parameters:
au - the access unit to write
Throws:
java.lang.IllegalStateException - if the packetizer is closed

flush

public void flush()
Flushes this packetizer, that is, notifies the data listener if data is available.


close

public void close()
Closes this packetizer. After closing no more access units can be written.