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

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

public class Quantizer
extends java.lang.Object

Contains the quantization functionality used in MPEG-2. All the input and output arrays in this class represent 8*8 matrices in row-major format.


Nested Class Summary
static class Quantizer.ScaleType
          This enumeration contains the two quantizer scale mappings used in MPEG-2.
 
Field Summary
private static int HEIGHT
           
static int[] INTRA_BLOCK_DEFAULT_WEIGHTS
          This is the default weighting matrix for the quantization of intra blocks.
private  int[] intraBlockWeights
           
private  int intraDCMult
           
private  int intraDCPrecision
           
private static int[] LINEAR_SCALE
           
static int[] NON_INTRA_BLOCK_DEFAULT_WEIGHTS
          This is the default weighting matrix for the quantization of non-intra blocks.
private  int[] nonIntraBlockWeights
           
private static int[] NONLINEAR_SCALE
           
private  Quantizer.ScaleType scaleType
           
private static int WIDTH
           
 
Constructor Summary
Quantizer(int[] intraBlockWeights, int[] nonIntraBlockWeights, Quantizer.ScaleType scaleType, int intraDCPrecision)
          Constructs a new quantizer with the given weighting matrices, quantizer scaling type and precision of DC coefficients in intra blocks.
Quantizer(Quantizer.ScaleType scaleType, int intraDCPrecision)
          Constructs a new quantizer with the default weighting matrices and the given quantizer scaling type and precision of the DC coefficients in intra blocks.
 
Method Summary
private  void doMismatchControl(int[] values)
           
 int[] getIntraBlockWeights()
           
 int getIntraDCPrecision()
           
 int[] getNonIntraBlockWeights()
           
 Quantizer.ScaleType getScaleType()
           
 int[] inverseQuantize(int[] input, boolean intra, int scaleCode)
          Inverse quantizes the given block, saturates the values and performs MPEG-2 style mismatch control on the result.
 void inverseQuantize(int[] input, int[] target, boolean intra, int scaleCode)
          Inverse quantizes the given block, saturates the values and performs MPEG-2 style mismatch control on the result.
private  void inverseQuantizeIntra(int[] input, int[] target, int intraDCMult, int scale)
           
private  int inverseQuantizeIntraACCoefficient(int value, int weight, int scale)
           
private  int inverseQuantizeIntraDCCoefficient(int quantizedValue, int intraDCMult)
           
private  void inverseQuantizeNonIntra(int[] input, int[] target, int scale)
           
private  int inverseQuantizeNonIntraCoefficient(int value, int weight, int scale)
           
 int[] quantize(int[] input, boolean intra, int scaleCode)
          Quantizes the given block and saturates the values.
 void quantize(int[] input, int[] target, boolean intra, int scaleCode)
          Quantizes the given block and saturates the values.
private  void quantizeIntra(int[] input, int[] target, int intraDCMult, int scale)
           
private  int quantizeIntraACCoefficient(int value, int weight, int scale)
           
private  int quantizeIntraDCCoefficient(int value, int intraDCMult)
           
private  void quantizeNonIntra(int[] input, int[] target, int scale)
           
private  int quantizeNonIntraCoefficient(int value, int weight, int scale)
           
private  int saturate(int value, int min, int max)
           
private  void saturateIntraForward(int[] input, int[] target)
           
private  void saturateInverse(int[] input, int[] target)
           
private  void saturateNonIntraForward(int[] input, int[] target)
           
 void setIntraBlockWeights(int[] intraBlockWeights)
           
 void setIntraDCPrecision(int intraDCPrecision)
          Sets the precision used in the quantization of DC coefficients in intra blocks.
 void setNonIntraBlockWeights(int[] nonIntraBlockWeights)
           
 void setScaleType(Quantizer.ScaleType scaleType)
           
private  int signum(int a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINEAR_SCALE

private static final int[] LINEAR_SCALE

NONLINEAR_SCALE

private static final int[] NONLINEAR_SCALE

INTRA_BLOCK_DEFAULT_WEIGHTS

public static final int[] INTRA_BLOCK_DEFAULT_WEIGHTS
This is the default weighting matrix for the quantization of intra blocks. This is a 8*8 matrix in row-major format.


NON_INTRA_BLOCK_DEFAULT_WEIGHTS

public static final int[] NON_INTRA_BLOCK_DEFAULT_WEIGHTS
This is the default weighting matrix for the quantization of non-intra blocks. This is a 8*8 matrix in row-major format.


WIDTH

private static final int WIDTH
See Also:
Constant Field Values

HEIGHT

private static final int HEIGHT
See Also:
Constant Field Values

intraBlockWeights

private int[] intraBlockWeights

nonIntraBlockWeights

private int[] nonIntraBlockWeights

scaleType

private Quantizer.ScaleType scaleType

intraDCPrecision

private int intraDCPrecision

intraDCMult

private int intraDCMult
Constructor Detail

Quantizer

public Quantizer(int[] intraBlockWeights,
                 int[] nonIntraBlockWeights,
                 Quantizer.ScaleType scaleType,
                 int intraDCPrecision)
Constructs a new quantizer with the given weighting matrices, quantizer scaling type and precision of DC coefficients in intra blocks.

Parameters:
intraBlockWeights - Matrix of the weights for quantizing intra blocks. This is a 8*8 two dimensional array in row-major format.
nonIntraBlockWeights - Matrix of the weights for quantizing non-intra blocks. This is a 8*8 two dimensional array in row-major format.
scaleType - The quantizer scaling mapping type to use.
intraDCPrecision - The precision of the DC coefficients in intra blocks, in bits. Must lie in range [8, 11].

Quantizer

public Quantizer(Quantizer.ScaleType scaleType,
                 int intraDCPrecision)
Constructs a new quantizer with the default weighting matrices and the given quantizer scaling type and precision of the DC coefficients in intra blocks.

Parameters:
scaleType - The quantizer scaling mapping type to use.
intraDCPrecision - The precision of the DC coefficients in intra blocks, in bits. Must lie in range [8, 11].
Method Detail

quantize

public void quantize(int[] input,
                     int[] target,
                     boolean intra,
                     int scaleCode)
Quantizes the given block and saturates the values. The result is stored in the given array.

The DC coefficient in intra blocks will be in the range [0, 2^intra_dc_precision]. Other coefficients will be in the range [-2047, 2047].

Coefficients in non-intra blocks will be in the range [-2047, 2047].

Parameters:
input - The block to quantize.
target - Indicates where to put the result.
intra - Whether the input block is an intra block.
scaleCode - The quantizer_scale_code used in the containing macroblock.

quantize

public int[] quantize(int[] input,
                      boolean intra,
                      int scaleCode)
Quantizes the given block and saturates the values. The result is returned in a new array.

The DC coefficient in intra blocks will be in the range [0, 2^intra_dc_precision]. Other coefficients will be in the range [-2047, 2047].

Coefficients in non-intra blocks will be in the range [-2047, 2047].

Parameters:
input - The block to quantize.
intra - Whether the input block is an intra block.
scaleCode - The quantizer_scale_code used in the containing macroblock.
Returns:
The result in a new array.

quantizeIntra

private void quantizeIntra(int[] input,
                           int[] target,
                           int intraDCMult,
                           int scale)

quantizeNonIntra

private void quantizeNonIntra(int[] input,
                              int[] target,
                              int scale)

quantizeIntraDCCoefficient

private int quantizeIntraDCCoefficient(int value,
                                       int intraDCMult)

quantizeIntraACCoefficient

private int quantizeIntraACCoefficient(int value,
                                       int weight,
                                       int scale)

quantizeNonIntraCoefficient

private int quantizeNonIntraCoefficient(int value,
                                        int weight,
                                        int scale)

inverseQuantize

public void inverseQuantize(int[] input,
                            int[] target,
                            boolean intra,
                            int scaleCode)
Inverse quantizes the given block, saturates the values and performs MPEG-2 style mismatch control on the result. The result is stored in the given array. The resulting values will be in the range [-2048, 2047].

Parameters:
input - The block to inverse quantize.
target - Indicates where to put the result.
intra - Whether the input block is an intra block.
scaleCode - The quantizer_scale_code used in the containing macroblock.

inverseQuantize

public int[] inverseQuantize(int[] input,
                             boolean intra,
                             int scaleCode)
Inverse quantizes the given block, saturates the values and performs MPEG-2 style mismatch control on the result. The result is returned in a new array. The resulting values will be in the range [-2048, 2047].

Parameters:
input - The block to inverse quantize.
intra - Whether the input block is an intra block.
scaleCode - The quantizer_scale_code used in the containing macroblock.
Returns:
The result in a new array.

inverseQuantizeIntra

private void inverseQuantizeIntra(int[] input,
                                  int[] target,
                                  int intraDCMult,
                                  int scale)

inverseQuantizeNonIntra

private void inverseQuantizeNonIntra(int[] input,
                                     int[] target,
                                     int scale)

inverseQuantizeIntraDCCoefficient

private int inverseQuantizeIntraDCCoefficient(int quantizedValue,
                                              int intraDCMult)

inverseQuantizeIntraACCoefficient

private int inverseQuantizeIntraACCoefficient(int value,
                                              int weight,
                                              int scale)

inverseQuantizeNonIntraCoefficient

private int inverseQuantizeNonIntraCoefficient(int value,
                                               int weight,
                                               int scale)

signum

private int signum(int a)

saturateIntraForward

private void saturateIntraForward(int[] input,
                                  int[] target)

saturateNonIntraForward

private void saturateNonIntraForward(int[] input,
                                     int[] target)

saturateInverse

private void saturateInverse(int[] input,
                             int[] target)

saturate

private int saturate(int value,
                     int min,
                     int max)

doMismatchControl

private void doMismatchControl(int[] values)

setIntraDCPrecision

public void setIntraDCPrecision(int intraDCPrecision)
Sets the precision used in the quantization of DC coefficients in intra blocks.

Parameters:
intraDCPrecision - The precision in bits. Must be in range [8,11].

getIntraDCPrecision

public int getIntraDCPrecision()
Returns:
The precision of intra DC coefficients currently used. Is an integer in the range [8,11].

setIntraBlockWeights

public void setIntraBlockWeights(int[] intraBlockWeights)
Parameters:
intraBlockWeights - The weighting matrix to be used for intra blocks

getIntraBlockWeights

public int[] getIntraBlockWeights()
Returns:
The current weighting matrix for intra blocks. This is a reference to the internal array.

setNonIntraBlockWeights

public void setNonIntraBlockWeights(int[] nonIntraBlockWeights)
Parameters:
nonIntraBlockWeights - The weighting matrix to be used for non-intra blocks

getNonIntraBlockWeights

public int[] getNonIntraBlockWeights()
Returns:
The current weighting matrix for non-intra blocks. This is a reference to the internal array.

setScaleType

public void setScaleType(Quantizer.ScaleType scaleType)
Parameters:
scaleType - The new scale type

getScaleType

public Quantizer.ScaleType getScaleType()
Returns:
The current scale type