fi.helsinki.cs.ohtu.mpeg2.video
Class VLC

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.video.VLC

public class VLC
extends java.lang.Object

VLC contains methods for writing to a BitOutputStream the various variable length codes used in MPEG-2 encoding.


Nested Class Summary
static class VLC.DCTCoefficientTable
          These are the two variable length code tables used for coding DCT coefficients.
static class VLC.IntraVLCFormat
          These are the two intra VLC format options.
 
Field Summary
private  BitOutputStream output
           
 
Constructor Summary
VLC(BitOutputStream bitStream)
          Class constructor.
 
Method Summary
private  int abs(int a)
           
 VLC.DCTCoefficientTable getIntraTable(VLC.IntraVLCFormat format)
          Returns the variable length code table to use for intra blocks.
 VLC.DCTCoefficientTable getNonIntraTable()
          Returns the variable length code table to use for non intra blocks.
 BitOutputStream getStream()
          Gets the underlying stream the variable length codes are written to.
 void setStream(BitOutputStream stream)
          Sets the stream the variable length codes are written to.
 void writeCodedBlockPattern(boolean[] coded)
           Writes the coded block pattern variable length code to the stream.
 void writeDCTCoefficient(int run, int level, VLC.DCTCoefficientTable tableToUse, boolean dcCoefficient)
          Writes a single variable length code for a given run and level.
private  void writeDCTCoefficientTableOne(int run, int level)
           
private  void writeDCTCoefficientTableZero(int run, int level, boolean dcCoefficient)
           
 void writeDCTDcSizeChrominance(int dcSize)
          Writes the variable length code for a given size of a DC coefficient (in an intra block).
 void writeDCTDcSizeLuminance(int dcSize)
          Writes the variable length code for a given size of a DC coefficient (in an intra block).
 void writeDCTEndOfBlock(VLC.DCTCoefficientTable tableToUse)
           
private  void writeEscapedDCTCoefficient(int run, int level)
           
 void writeMacroblockAddressIncrement(int addressIncrement)
          Writes the variable length code of the given macroblock address increment to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

output

private BitOutputStream output
Constructor Detail

VLC

public VLC(BitOutputStream bitStream)
Class constructor.

Parameters:
bitStream - The stream to write the variable length codes to
Method Detail

getStream

public BitOutputStream getStream()
Gets the underlying stream the variable length codes are written to. This is most useful for writing writeTo methods for things such as macroblocks, some fields of which are VL-coded while others are not. Naturally, both are always written to the same stream, so the stream returned by this function can be used for writing the non-VL-coded parts.

Returns:
The stream.

setStream

public void setStream(BitOutputStream stream)
Sets the stream the variable length codes are written to.

Parameters:
stream - The new stream to write to.

writeMacroblockAddressIncrement

public void writeMacroblockAddressIncrement(int addressIncrement)
                                     throws java.io.IOException
Writes the variable length code of the given macroblock address increment to the stream. This method writes a macroblock escape code floor(addressIncrement/33) times and then writes the variable length code for addressIncrement mod 33.

Parameters:
addressIncrement - The macroblock address increment to write. Must be greater than zero.
Throws:
java.io.IOException - Exception from the underlying stream

writeCodedBlockPattern

public void writeCodedBlockPattern(boolean[] coded)
                            throws java.io.IOException

Writes the coded block pattern variable length code to the stream. The order of the blocks is as follows:

Y:
0 1
2 3
Cb:
4
Cr:
5

Parameters:
coded - Indicates whether a given block is to be marked as coded. If coded[i], where i is a the number of the block, equals true then that block is coded in its macroblock.
Throws:
java.io.IOException - Exception from the underlying stream

writeDCTDcSizeLuminance

public void writeDCTDcSizeLuminance(int dcSize)
                             throws java.io.IOException
Writes the variable length code for a given size of a DC coefficient (in an intra block). This method is used for luminance blocks.

Parameters:
dcSize - Must be in range [0,11]
Throws:
java.io.IOException - Exception from the underlying stream

writeDCTDcSizeChrominance

public void writeDCTDcSizeChrominance(int dcSize)
                               throws java.io.IOException
Writes the variable length code for a given size of a DC coefficient (in an intra block). This method is used for chrominance blocks.

Parameters:
dcSize - Must be in range [0,11]
Throws:
java.io.IOException - Exception from the underlying stream

getIntraTable

public VLC.DCTCoefficientTable getIntraTable(VLC.IntraVLCFormat format)
Returns the variable length code table to use for intra blocks.

Parameters:
format - The intra VLC format to be used.
Returns:
The table that should be used.

getNonIntraTable

public VLC.DCTCoefficientTable getNonIntraTable()
Returns the variable length code table to use for non intra blocks.

Returns:
The table that should be used.

writeDCTCoefficient

public void writeDCTCoefficient(int run,
                                int level,
                                VLC.DCTCoefficientTable tableToUse,
                                boolean dcCoefficient)
                         throws java.io.IOException
Writes a single variable length code for a given run and level. The variable length code table zero uses an alternate code for DC coefficients of level 1.

Parameters:
run - The number of zeros preceding the non-zero coefficient. Must be in range [0,63].
level - The level of the non-zero coefficient. Must be in range [-2047,2047] and shall not be zero.
tableToUse - The variable length code table to use.
dcCoefficient - Whether the coefficient to be written is the DC coefficient of the block.
Throws:
java.io.IOException - Exception from the underlying stream

writeDCTEndOfBlock

public void writeDCTEndOfBlock(VLC.DCTCoefficientTable tableToUse)
                        throws java.io.IOException
Throws:
java.io.IOException

writeDCTCoefficientTableZero

private void writeDCTCoefficientTableZero(int run,
                                          int level,
                                          boolean dcCoefficient)
                                   throws java.io.IOException
Throws:
java.io.IOException

writeDCTCoefficientTableOne

private void writeDCTCoefficientTableOne(int run,
                                         int level)
                                  throws java.io.IOException
Throws:
java.io.IOException

abs

private int abs(int a)

writeEscapedDCTCoefficient

private void writeEscapedDCTCoefficient(int run,
                                        int level)
                                 throws java.io.IOException
Throws:
java.io.IOException