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

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

public class MotionVector
extends java.lang.Object

Represents a motion vector.

Motion vectors are used in MPEG-2 to specify the reference points for motion compensation. The other input is the motion residual, which conveys the difference between the image data at the reference point in the reference frame and at the point to be reconstructed.

Motion vector values are specified at half-pixel precision. Therefore, a motion vector having values (7, -20) corresponds to an image-space offset of (3.5, -10).

The picture layer F code parameter affects the encoding and allowable values in motion vectors. Motion vector range will be restricted to [-(2^(fCode+3))..2^(fCode+3)-1] for both components. Additionally, constantly using motion vectors in the high end of the range is not space-efficient, but neither is always using a really high F code. The space taken, in bits, by a motion vector's (X or Y) component with value v is given roughly by fCode + v / 2^fCode. However, the encoded size of a zero component is always exactly one bit.


Nested Class Summary
static class MotionVector.Predictors
          Preserves that context of previously written motion vectors to determine correct binary encoding when writing motion vectors.
 
Field Summary
private static int[][] COARSE_VALUE_CODES
           
private static int[][] DIFFERENTIAL_CODES
           
private  int[] differentials
           
private  int fCode
           
private  java.lang.Boolean isBottomField
           
private  int[] values
           
 
Constructor Summary
MotionVector(int fCode)
          Constructs a zero-length motion vector.
MotionVector(int fCode, int horizontalValue, int verticalValue)
          Constructs a motion vector with the given value.
 
Method Summary
 int getFCode()
          Gets the F code the motion vector is subject to.
 int getHorizontalDPDifferential()
          If the motion vector specifies dual-prime differentials, returns the horizontal differential value.
 int getHorizontalValue()
          Gets the horizontal value.
 int getVerticalDPDifferential()
          If the motion vector specifies dual-prime differentials, returns the vertical differential value.
 int getVerticalValue()
          Gets the vertical value.
 boolean hasDPDifferentials()
          Determines if the motion vector specifies dual-prime differentials.
 boolean hasFieldSelect()
          Determines if the motion vector specifies (the parity of) a reference field.
 boolean isFromBottomField()
          If the motion vector specifies the reference field parity, determines whether it selects a bottom field.
 boolean isFromTopField()
          If the motion vector specifies the reference field parity, determines whether it selects a top field.
 void setDPDifferentials(int horizontalDifferential, int verticalDifferential)
          Sets the motion vector to specify dual-prime differentials.
 void setFromBottomField()
          Sets the motion vector to specify that a bottom field should be used as the reference field.
 void setFromTopField()
          Sets the motion vector to specify that a top field should be used as the reference field.
 void setHorizontalValue(int value)
          Sets the horizontal value.
 void setValues(int horizontalValue, int verticalValue)
          Sets both the horizontal and vertical value.
 void setVerticalValue(int value)
          Sets the vertical value.
 void unsetFieldReferenceExtras()
          Makes the motion vector specify neither a dual-prime differential or a reference field parity.
 void writeTo(BitOutputStream stream, MotionVector.Predictors predictors)
          Writes the motion vector to a bit-by-bit output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COARSE_VALUE_CODES

private static final int[][] COARSE_VALUE_CODES

DIFFERENTIAL_CODES

private static final int[][] DIFFERENTIAL_CODES

fCode

private int fCode

values

private int[] values

isBottomField

private java.lang.Boolean isBottomField

differentials

private int[] differentials
Constructor Detail

MotionVector

public MotionVector(int fCode)
Constructs a zero-length motion vector. The motion vector will not have a dual-prime differential or a reference field parity specified.

Parameters:
fCode - The F code corresponding to the motion vector from the picture layer. Must be in range [1..9].

MotionVector

public MotionVector(int fCode,
                    int horizontalValue,
                    int verticalValue)
Constructs a motion vector with the given value. The motion vector will not have a dual-prime differential or a reference field parity specified.

Parameters:
fCode - The F code corresponding to the motion vector from the picture layer. Must be in range [1..9].
horizontalValue - The horizontal value. Subject to the restrictions of setHorizontalValue().
verticalValue - The vertical value. Subject to the restrictions of setVerticalValue().
Method Detail

getFCode

public int getFCode()
Gets the F code the motion vector is subject to.

Returns:
The F code. Will be in range [1..9].

getHorizontalValue

public int getHorizontalValue()
Gets the horizontal value.

Returns:
The value. Will be in range [-(2^(fCode+3))..2^(fCode+3)-1]

setHorizontalValue

public void setHorizontalValue(int value)
Sets the horizontal value.

Parameters:
value - The value to set. Must be in range [-(2^(fCode+3))..2^(fCode+3)-1]

getVerticalValue

public int getVerticalValue()
Gets the vertical value.

Returns:
The value. Will be in range [-(2^(fCode+3))..2^(fCode+3)-1]

setVerticalValue

public void setVerticalValue(int value)
Sets the vertical value.

Parameters:
value - The value to set. Must be in range [-(2^(fCode+3))..2^(fCode+3)-1]

setValues

public void setValues(int horizontalValue,
                      int verticalValue)
Sets both the horizontal and vertical value.

Parameters:
horizontalValue - The horizontal value. Subject to the restrictions of setHorizontalValue().
verticalValue - The vertical value. Subject to the restrictions of setVerticalValue().

setFromTopField

public void setFromTopField()
Sets the motion vector to specify that a top field should be used as the reference field. Field selection is only allowed with field prediction. It is mutually exclusive with dual-prime prediction, so dual-prime prediction is turned off if this method is used.


setFromBottomField

public void setFromBottomField()
Sets the motion vector to specify that a bottom field should be used as the reference field. Field selection is only allowed with field prediction. It is mutually exclusive with dual-prime prediction, so dual-prime prediction is turned off if this method is used.


hasFieldSelect

public boolean hasFieldSelect()
Determines if the motion vector specifies (the parity of) a reference field.

Returns:
Whether or not the vector specifies (the parity of) a reference field.

isFromTopField

public boolean isFromTopField()
If the motion vector specifies the reference field parity, determines whether it selects a top field.

Returns:
true if a top field is selected.

isFromBottomField

public boolean isFromBottomField()
If the motion vector specifies the reference field parity, determines whether it selects a bottom field.

Returns:
true if a bottom field is selected.

setDPDifferentials

public void setDPDifferentials(int horizontalDifferential,
                               int verticalDifferential)
Sets the motion vector to specify dual-prime differentials. The differentials should only be specified when using dual-prime prediction. As dual-prime prediction references two fields at once, the field selection is automatically turned off when using this method.

Parameters:
horizontalDifferential - The horizontal differential value. Must be in range [-1..1].
verticalDifferential - The vertical differential value. Must be in range [-1..1].

hasDPDifferentials

public boolean hasDPDifferentials()
Determines if the motion vector specifies dual-prime differentials.

Returns:
Whether or not the vector specifies dual-prime differentials.

getHorizontalDPDifferential

public int getHorizontalDPDifferential()
If the motion vector specifies dual-prime differentials, returns the horizontal differential value.

Returns:
The value. Will be in range [-1..1].

getVerticalDPDifferential

public int getVerticalDPDifferential()
If the motion vector specifies dual-prime differentials, returns the vertical differential value.

Returns:
The value. Will be in range [-1..1].

unsetFieldReferenceExtras

public void unsetFieldReferenceExtras()
Makes the motion vector specify neither a dual-prime differential or a reference field parity.


writeTo

public void writeTo(BitOutputStream stream,
                    MotionVector.Predictors predictors)
             throws java.io.IOException
Writes the motion vector to a bit-by-bit output stream.

Parameters:
stream - The stream to write to.
predictors - The Predictors instance to use.
Throws:
java.io.IOException - Propagated from the stream write.