fi.helsinki.cs.ohtu.mpeg2
Class VideoStream

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.ElementaryStream
      extended by fi.helsinki.cs.ohtu.mpeg2.VideoStream

public class VideoStream
extends ElementaryStream

Represents a stream of MPEG-2 video. Instances of VideoStream collect together the pieces a MPEG-2 video and provide the data for packetization. This class makes basic consistency checks for the written data. The checking is rather light and cannot be used for validation. In other words, garbage in, garbage out. The checking can be disabled.

For an overall picture and details of MPEG-2 video, please see the ISO 13818-2.


Nested Class Summary
private static class VideoStream.VideoAccessUnit
          Represents an access unit of a video stream.
private static class VideoStream.VideoPacketizer
          Implements the packetizer for video streams.
private static class VideoStream.VideoState
          Video state, used to track video access unit generation
 
Field Summary
private  int blockCount
           
private  int blocks
           
private  int blocksCoded
           
private  boolean checkEnabled
           
private  boolean closed
           
private  boolean discont
           
private  long dtsBase
           
private  boolean firstAu
           
private  SequenceHeader.FrameRateCode frameRate
           
private  int frames
           
private  PictureHeader.CodingType frameType
           
private  boolean mbIntra
           
private  boolean mbMvBackward
           
private  boolean mbMvForward
           
private  boolean mbPattern
           
private  java.util.List<VideoStream.VideoPacketizer> packers
           
private  long ptsBase
           
private  VideoStream.VideoState state
           
private  BitOutputStream videoBits
           
private  java.io.ByteArrayOutputStream videoBytes
           
 
Constructor Summary
VideoStream()
          Creates a new VideoStream instance.
 
Method Summary
 void close()
          Closes this ElementaryStream.
private  void decodeMblkType(int[] mblktype)
          Decodes a variable length code representing a macroblock type.
private  void finishAu()
          Finishes building an access unit and delivers it to the packetizers.
 void flush()
          Flushes buffered data, if any, to the packetizers.
 boolean getChecking()
          Gets whether video structure checking is enabled.
 long getDts()
          Gets the current decoding timestamp.
private  float getFrameDuration(SequenceHeader.FrameRateCode frameRate)
          Returns duration of a single frame for given frame rate.
 AVPacketizer getPacketizer(int vsid)
          Creates a new packetizer for this VideoStream.
 AVPacketizer getPacketizer(int vsid, int decoderBuffer)
          Creates a new packetizer for this VideoStream.
 AVPacketizer getPacketizer(int vsid, int decoderBuffer, boolean alignIFrames)
          Creates a packetizer for this VideoStream.
 long getPts()
          Gets the current presentation timestamp.
 void releasePacketizer(AVPacketizer esp)
          Releases a packetizer for this ElementaryStream.
private  void resetVideoAUBuilder()
          Resets the buffer used to collect data for an access unit.
 void setChecking(boolean checkEnabled)
          Sets whether video structure checking is enabled.
 void setPtsDts(double pts, double dts)
          Sets the presentation and decoding timestamps.
 void setPtsDts(long pts, long dts)
          Sets the presentation and decoding timestamps.
private  int vlcToInt(int[] vlc)
          Converts a variable length coded value to an integer.
 void write(Block block)
          Writes a block.
 void write(MacroblockHeader mblkhdr, int[] mblktype)
          Writes a macroblock header.
 void write(MotionVector mv, MotionVector.Predictors preds)
          Writes a motion vector.
 void write(PictureHeader pichdr, PictureCodingExtensionHeader pcehdr)
          Writes picture headers.
 void write(PictureHeader pichdr, PictureCodingExtensionHeader pcehdr, long pts, long dts)
          Writes picture headers.
 void write(SequenceHeader seqhdr, SequenceExtensionHeader sexhdr)
          Writes sequence headers.
 void write(SliceHeader sheader)
          Writes a slice header.
 void writeCodedBlockPattern(boolean[] blockCoded)
          Writes a pattern for which blocks have been coded.
 void writeEnd()
          Writes a sequence end code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

packers

private final java.util.List<VideoStream.VideoPacketizer> packers

ptsBase

private long ptsBase

dtsBase

private long dtsBase

closed

private boolean closed

checkEnabled

private boolean checkEnabled

state

private VideoStream.VideoState state

videoBytes

private java.io.ByteArrayOutputStream videoBytes

videoBits

private BitOutputStream videoBits

discont

private boolean discont

firstAu

private boolean firstAu

blockCount

private int blockCount

blocks

private int blocks

blocksCoded

private int blocksCoded

mbPattern

private boolean mbPattern

mbMvForward

private boolean mbMvForward

mbMvBackward

private boolean mbMvBackward

mbIntra

private boolean mbIntra

frameRate

private SequenceHeader.FrameRateCode frameRate

frames

private int frames

frameType

private PictureHeader.CodingType frameType
Constructor Detail

VideoStream

public VideoStream()
Creates a new VideoStream instance. Stream checking is enabled by default. The first allowed write operation is writing of sequence headers.

Method Detail

getPts

public long getPts()
Gets the current presentation timestamp.

Returns:
the current presentation timestamp, in ticks of the 90kHz clock

getDts

public long getDts()
Gets the current decoding timestamp.

Returns:
the current decoding timestamp, in ticks of the 90kHz clock

getChecking

public boolean getChecking()
Gets whether video structure checking is enabled.

Returns:
true if checking is enabled

setChecking

public void setChecking(boolean checkEnabled)
Sets whether video structure checking is enabled.

Parameters:
checkEnabled - true if checking is enabled

setPtsDts

public void setPtsDts(long pts,
                      long dts)
Sets the presentation and decoding timestamps.

Parameters:
pts - the PTS, in ticks of the 90kHz clock
dts - the DTS, in ticks of the 90kHz clock

setPtsDts

public void setPtsDts(double pts,
                      double dts)
Sets the presentation and decoding timestamps.

Parameters:
pts - the PTS, in seconds
dts - the DTS, in seconds

write

public void write(SequenceHeader seqhdr,
                  SequenceExtensionHeader sexhdr)
Writes sequence headers. If stream checking is enabled, calling this method is allowed at the start of the stream and after writing complete picture data.

Parameters:
seqhdr - the sequence header to write
sexhdr - the sequence extension header to write
Throws:
IllegalStateExpection - if sequence headers were not expected

write

public void write(PictureHeader pichdr,
                  PictureCodingExtensionHeader pcehdr)
Writes picture headers. If stream checking is enabled, calling this method is allowed straight after writing sequence headers or complete picture data.

Parameters:
pichdr - the picture header to write
pcehdr - the picture coding extension header to write
Throws:
java.lang.IllegalStateException - if picture headers were not expected

write

public void write(PictureHeader pichdr,
                  PictureCodingExtensionHeader pcehdr,
                  long pts,
                  long dts)
Writes picture headers.

Parameters:
pichdr - the picture header to write
pcehdr - the picture coding extension header to write
pts - the presentation timestamp for the picture
dts - the decoding timestamp for the picture
See Also:
write(PictureHeader, PictureCodingExtensionHeader)

write

public void write(SliceHeader sheader)
Writes a slice header. When stream checking is enabled, this call is allowed straight after writing picture headers or a completed slice.

Parameters:
sheader - the slice header to write

write

public void write(MacroblockHeader mblkhdr,
                  int[] mblktype)
Writes a macroblock header. If stream checking is enabled, calling this method is allowed straight after writing a slice header or a complete macroblock. Completing the macroblock allows writing another picture header, sequence header or sequence end code.

Parameters:
mblkhdr - the macroblock header to write
mblktype - the type of the macroblock
Throws:
java.lang.IllegalStateException - if a macroblock header was not expected
See Also:
write(Block), write(MotionVector, MotionVector.Predictors), writeCodedBlockPattern(boolean[])

write

public void write(Block block)
Writes a block. When stream checking is enabled, calling this method is allowed after writing a macroblock header and any motion vectors and coded block pattern required by the macroblock type. This method has to be called as many times as there are coded blocks in the current macroblock. Writing the correct number of blocks completes a macroblock.

Parameters:
block - the block to write
Throws:
java.lang.IllegalStateException - if a block was not expected

write

public void write(MotionVector mv,
                  MotionVector.Predictors preds)
Writes a motion vector. If stream checking is enabled, calling of this method is allowed after writing a macroblock header and if the macroblock type requires a motion vector. Some macroblock types require two motion vectors.

Parameters:
mv - the motion vector to write
preds - the motion vector predictors to update
Throws:
java.lang.IllegalStateException - if a motion vector was not expected

writeCodedBlockPattern

public void writeCodedBlockPattern(boolean[] blockCoded)
Writes a pattern for which blocks have been coded. When stream checking is enabled, calling this method is allowed after writing a macroblock header and any motion vectors required by the macroblock type and if the type requires a coded block pattern.

Parameters:
blockCoded - the pattern of coded blocks
Throws:
java.lang.IllegalStateException - if a coded block pattern was not expected

writeEnd

public void writeEnd()
Writes a sequence end code. Calling this method is allowed only straight after picture data, if stream checking is enabled.

Throws:
java.lang.IllegalStateException - if not called after writing picture data

getPacketizer

public AVPacketizer getPacketizer(int vsid,
                                  int decoderBuffer,
                                  boolean alignIFrames)
Creates a packetizer for this VideoStream. Video stream number, vsid, defines the stream id for video packets. decoderBuffer defines how much video data can be waiting to be decoded at the video decoder at any moment. A lower number means lower resource demands at the decoder but it makes it more difficult to maintain smooth playback. Besides, an access unit has to completely fit into the buffer. alignIFrames specifies whether access units containing I-frames should start at the start of a new packet. Some formats require it.

Parameters:
vsid - the stream number for the packetized stream, in range [0, 16]
decoderBuffer - the size of the video decoder buffer, in bytes; has to be at least 32768
alignIFrames - true if I-frames should be packet-aligned
Returns:
the packetizer

getPacketizer

public AVPacketizer getPacketizer(int vsid,
                                  int decoderBuffer)
Creates a new packetizer for this VideoStream. I-frame packet alignment is disabled.

Parameters:
vsid - the stream number for the packetized video stream, in range [0, 16]
decoderBuffer - the size of the video decoder buffer, in bytes; has to be at least 32768
Returns:
the packetizer
See Also:
getPacketizer(int, int, boolean)

getPacketizer

public AVPacketizer getPacketizer(int vsid)
Creates a new packetizer for this VideoStream. The video decoder buffer defaults to 46KiB. I-frames are not packet aligned.

Specified by:
getPacketizer in class ElementaryStream
Parameters:
vsid - the stream number for the packetized video stream, in range [0, 16]
Returns:
the packetizer
See Also:
getPacketizer(int, int, boolean)

releasePacketizer

public void releasePacketizer(AVPacketizer esp)
Description copied from class: ElementaryStream
Releases a packetizer for this ElementaryStream. After releasing the packetizer will not receive more data from this stream.

Specified by:
releasePacketizer in class ElementaryStream
Parameters:
esp - the packetizer to release

flush

public void flush()
Description copied from class: ElementaryStream
Flushes buffered data, if any, to the packetizers.

Specified by:
flush in class ElementaryStream

close

public void close()
Description copied from class: ElementaryStream
Closes this ElementaryStream. After this call no more data shall be written into this stream.

Overrides:
close in class ElementaryStream

finishAu

private void finishAu()
Finishes building an access unit and delivers it to the packetizers. After that, the access unit building buffer is reset.


resetVideoAUBuilder

private void resetVideoAUBuilder()
Resets the buffer used to collect data for an access unit.


getFrameDuration

private float getFrameDuration(SequenceHeader.FrameRateCode frameRate)
Returns duration of a single frame for given frame rate.

Parameters:
frameRate - the frame rate
Returns:
the duration, in seconds

vlcToInt

private int vlcToInt(int[] vlc)
Converts a variable length coded value to an integer. The bit order is reversed in the return value. Otherwise different vlc values could not be sorted out.

Parameters:
vlc - the variable length code to be converted
Returns:
the integer value of the variable length code

decodeMblkType

private void decodeMblkType(int[] mblktype)
Decodes a variable length code representing a macroblock type. Updates the state of this VideoStream instance accordingly.

Parameters:
mblktype - the macroblock type in variable length code