Chapter 6. Using Compression Library Parameters

The CL has a group of routines for working with a set of state variables called “parameters” that are unique for each instantiation. These routines—clQueryParams(), clGetParams(), clSetParams(), clGetDefault(), clSetDefault()—are similar to a set of routines in the Audio Library. You can get and set parameters, either individually or as a group; however, all of the parameters have reasonable defaults that are algorithm-dependent and need not be set.

This chapter describes how to use the Compression Library parameters:

Compression Library Parameter Definitions

Parameters provide state information about or set frame characteristics, data formats, and algorithms for each compressor/decompressor. This section discusses parameters by category.

Image Frame Dimension Parameters

The CL_IMAGE_WIDTH and CL_IMAGE_HEIGHT parameters provide information about image frame dimensions. For more information on these parameters, see “Using OCTANE Compression Image Formats” in Chapter 4.

Data Format Parameters

These parameters describe data formats:

CL_ORIGINAL_FORMAT 


On compression, this is the format of the original video. On decompression, this is the format that you want after decompression. The value, a symbolic constant, is CL_RGB, CL_RGBX (default), CL_RGBA, CL_RGB332, CL_GRAYSCALE, CL_YUV, CL_YUV422, or CL_YUV422DC.

CL_INTERNAL_FORMAT 


Some video algorithms have several “natural” formats that can be compressed without color-space conversion. This parameter allows the selection of one of these formats. The video default is algorithm-specific.

CL_COMPONENTS 


A read-only value, as determined by CL_ORIGINAL_FORMAT, that indicates the number of components in the data. For example, video is generally 1 for gray-scale, and 3 or 4 for color. The default is 4.

CL_BITS_PER_COMPONENT 


The number of bits per component. For OCTANE Digital Video, this value is always 8.

CL_ORIENTATION 


Specifies the orientation of compressed data, which can be one of the following:

  • CL_TOP_DOWN: for pixels arranged top-to-bottom (default)

  • CL_BOTTOM_UP: for pixels arranged bottom-to-top

  • DM_TOP_TO_BOTTOM for Silicon Graphics movies

The orientation of compressed data is always top down. When compression or decompression is specified, the original format (or final format) of the data can be bottom up. Specify this inversion by setting the CL_ORIENTATION parameter to CL_BOTTOM_UP instead of the default.

Buffer Parameters

These parameters describe buffer sizes and characteristics:

CL_FRAME_BUFFER_SIZE 


The maximum size, in bytes, of the frame buffer. If clDecompress() is called with numberOfFrames larger than 1, this value should be the frame size × numberOfFrames.

CL_COMPRESSED_BUFFER_SIZE 


The maximum size of the compressed data buffer. The default is calculated as the maximum possible size, taking into account all the factors such as algorithm, encoding method, data type, and so on. If you want to use a smaller buffer, you can set this value explicitly. If clCompress() is called with numberOfFrames larger than 1, this value should be the maximum compressed size of one frame × numberOfFrames.

CL_BLOCK_SIZE 


The natural block size of the algorithm in samples. It is most efficient to specify numberOfFrames to be a multiple of the block size when calling clCompress() or clDecompress().

CL_PREROLL 


The number of blocks of frames that must be supplied to clDecompress() before decompressed frames are returned.

CL_FRAME_RATE 


The requested number of frames per second.

CL_FRAME_TYPE 


The decompressor fills in the frame type when it decompresses a frame. Frame type is one of:

CL_KEYFRAME

frame is a keyframe

CL_INTRA

equivalent to CL_KEYFRAME

CL_PREDICTED

frame contains information about its succeeding frames

CL_BIDIRECTIONAL

frame contains information about frames that precede and succeed it


Compression Ratio and Quality Parameters

These parameters control the compression ratio and quality:

CL_ALGORITHM_ID 


A parameter that can be queried to find out the scheme identifier of the algorithm of an open compressor or decompressor.

CL_EXACT_COMPRESSION_RATIO 


A flag determines whether the compression ratio is a target or must be exact. Some algorithm implementations, such as for JPEG, can be only approximated and can never be exact. For algorithms that do support it, it is generally kept within a small range that over time is guaranteed to average out to the specified compression ratio.

JPEG and MPEG Parameters

JPEG has the following additional parameters:

CL_JPEG_COMPONENT_TABLES 


Specifies the IDs of the AC Huffman table, DC Huffman table, and quantization table to be used for each component. This parameter cannot be changed directly; rather, it is set up automatically for processing the selected CL_INTERNAL_FORMAT.

YUV formats use AC Huffman table 0, DC Huffman table 0, and quantization table 0 for component 0; AC Huffman table 1, DC Huffman table 1, and quantization table 1 for components 1 and 2. RGB formats use tables AC table 0, DC table 0, and quantization table 0 for all components.

CL_JPEG_QUANTIZATION_TABLES  


Sets or gets the quantization tables to be used. For more information, see “Defining and Using Custom JPEG Quantization Tables” in Chapter 7.

CL_JPEG_QUALITY_FACTOR 


A JPEG quantization table scale factor that represents a rough percentage of the image detail preservation. For more information, see “Defining and Using Custom JPEG Quantization Tables” in Chapter 7.

MPEG_VIDEO has the following additional parameter:

CL_END_OF_SEQUENCE 


An end-of-sequence flag. When the decompressor arrives at the end of the sequence, it sets this flag. The default is FALSE (0).

For a summary of parameters and their types, ranges, and defaults, see Table A-1 in Appendix A, “Video Library Controls and Compression Library Parameters for the OCTANE Compression Option.”

Setting and Querying Compression Library Parameters

After a compressor or decompressor is opened, thus specifying the compression scheme to use, various parameters can be modified using clSetParams(). All of these parameters have reasonable defaults that are algorithm-dependent and need not be set. Some parameters, such as CL_IMAGE_WIDTH and CL_IMAGE_HEIGHT for video compression, should be set, but setting them is not required.

Getting a List of Parameters and Parameter Types

Use clQueryParams() to get a list of valid parameters and their types for a specified a compressor or decompressor. The compressor being queried is identified by its handle. Its function prototype is:

int clQueryParams(CLhandle handle,int *paramValuebuffer, int maxLength)

where

handle 

is the handle to a compressor or decompressor.

paramValuebuffer 


is a pointer to an array of ints into which clQueryParams() can write parameter identifier/parameter type pairs for each parameter associated with the compressor or decompressor. The even (0,2,4,...) entries receive a string that is the parameter identifier. The odd entries (1,3,5,...) receive the parameter type. Parameter type is one of four values:

  • CL_RANGE_VALUE, meaning that a parameter can assume a range of values in which the relative magnitude of the value is meaningful—that is, increasing values indicate increasing quantities of whatever this parameter controls, and vice versa.

  • CL_ENUM_VALUE, meaning that a parameter assumes values from an enumerated type. The values have a limited range, but there is no inherent relationship between the range values.

  • CL_FLOATING_RANGE_VALUE, meaning that a parameter can assume a range of floating point values, in which the relative magnitude of the value is meaningful—that is, increasing values indicate increasing quantities of whatever this parameter controls, and vice versa.

  • CL_FLOATING_ENUM_VALUE, meaning that a parameter assumes values from an enumerated type. The values have a limited floating point range, but there is no inherent relationship between the range values.

maxLength 

is the length of the buffer, in ints, pointed to by paramValuebuffer. If maxLength is zero, then paramValuebuffer is ignored and only the return value is valid.

clQueryParams() returns the size of the buffer, in ints, needed to hold all the parameter identifier/parameter type pairs for the compressor or decompressor identified by handle. The parameters are returned in the even locations of paramValuebuffer, and their types are returned in the odd locations.

If the size of the paramValuebuffer is smaller than the returned value, a partial list of the parameter identifier/parameter type pairs is returned, making it necessary to enlarge the paramValuebuffer in order to receive a complete list. To avoid this situation, you can obtain the correct size of the buffer by calling clQueryParams() with a NULL buffer pointer and a maxLength of 0 to return the actual buffer length without writing any data.

clQueryParams() also reports whether the parameter is one of a set of enumerated types, any integer number within a specific range, or any floating point number within a specific range. In each case, the values are numbers within the range returned by clGetMinMax() and have the defaults returned by clGetDefault().

Example 6-1 demonstrates how to get a list of parameters for a specified compressor/decompressor.

Example 6-1. Getting a List of Parameters for a Compressor/Decompressor

#include <dmedia/cl.h>
#include <malloc.h>

/*
* Get a buffer containing all the parameters for a specified
* compressor or decompressor.
*/

int *buf, bufferLength;
bufferLength = clQueryParams(handle, 0, 0);
buf = (int *)malloc(bufferLength * sizeof(int));
clQueryParams(handle, buf, bufferLength);


Getting the Parameter ID that Corresponds to a Parameter Name

If you know the name of a parameter, but not its identifier, you can use clGetParamID() to get the identifier of a parameter from its name.

Its function prototype is:

int clGetParamID(CLhandle handle, char *name)

Getting and Setting Parameter Values

You can get or set parameter values as a group or individually.

Use clGetParams() to return the current values for the parameters referenced in the paramValuebuffer array. The values are written into the odd locations of paramValuebuffer immediately after the corresponding parameters.

Use clSetParams() to set the current state of the parameters referenced in the paramValuebuffer array.

To change a state parameter:

  1. Call clQueryParams() to find out which parameters are available.

  2. Call clGetParams() to find out the current state.

  3. Fill in the even entries of the paramValuebuffer array corresponding to the parameters to be changed and then call clSetParams().

The function prototypes are:

void clGetParams ( CLhandle handle, int *paramValuebuffer,
                   int bufferLength )
void clSetParams ( CLhandle handle, int *paramValuebuffer,
                   int bufferLength )

where

handle 

is a handle that identifies a compressor or decompressor.

paramValuebuffer 


is a pointer to an array of pairs of ints. The even elements of this array select the parameters to be read or changed. The subsequent odd elements are the current or new values of these parameters.

bufferLength 

is the number of ints in the buffer pointed to by paramValuebuffer.

Alternatively, parameters can be changed individually with clSetParam() and clGetParam(). clGetParam() returns the current value of the parameter. clSetParam() returns the previous value of the parameter.

The function prototypes are:

int clGetParam(CLhandle handle, int paramID)
int clSetParam(CLhandle handle, int paramID, int value)

where

handle 

is a handle that identifies a compressor or decompressor.

paramID 

is the identifier of the parameter to get or set.

value 

is the new value of the parameter.

Example 6-2 demonstrates how to extract the current value of specific parameters from a list of parameters returned as a group. In this case, the current block size and preroll values are obtained from the list of parameters that are returned in paramValuebuffer from clGetParams().

Example 6-2. Getting the Current Values of Selected Parameters

#include <dmedia/cl.h>
...
/* Get the block size and preroll */
int paramValueBuffer[][2] = {
CL_BLOCK_SIZE, 0,
CL_PREROLL, 0
};
clGetParams(handle, (int *)paramValueBuffer,
sizeof(paramValueBuffer) / sizeof(int));
/* paramValueBuffer[0][1] is the block size */
/* paramValueBuffer[1][1] is the preroll */


Getting or Setting the Value of a Floating Point Parameter

Some parameters, such as CL_FRAME_RATE, are floating point values. You don't have to cast expressions involving floating point values, because macros are provided within libcl that handle the conversions for you; even though a value is a float you can cast to an int. To set a floating point value, use the macro CL_TypeIsInt(); to retrieve a floating point value, use the macro CL_TypeIsFloat().

The argument must be a variable, because the type definitions in /usr/include/dmedia/cl.h are

float 

*(float *) &value

int 

*(int *) &value

Example 6-3 demonstrates how to use the libcl macros to get/set a floating point parameter value.

Example 6-3. Using Macros to Get or Set the Value of a Floating Point Parameter

float number;
number = 3.0;
...
clSetParam(handle, CL_COMPRESSION_RATIO, CL_TypeIsInt(number));
number = CL_TypeIsFloat(clGetParam(handle,CL_COMPRESSION_RATIO));


Getting or Setting Individual Parameter Attributes

You can query parameters individually to get the name, defaults, and range of valid values, given the parameter identifier and a handle.

Use clGetName() to return a pointer to a null-terminated string that supplies the English name of a parameter. Its function prototype is

char* clGetName(CLhandle handle, int param)

where

handle 

is a handle that identifies a compressor or decompressor.

param 

is a parameter identifier.

Use clGetDefault() to return the default value of the parameter specified by param. Use clSetDefault() to set the default value. Setting the default value is particularly useful when an algorithm has been added and new defaults need to be set.

The function prototypes are

int clGetDefault(CLhandle handle, int param)
int clSetDefault(int scheme, int paramID, int value)

where

handle 

is a handle that identifies a compressor or decompressor.

paramID 

is a parameter identifier.

scheme 

is the identifier of the scheme for which to set the defaults.

value 

is the new default value associated with param.

Example 6-4 demonstrates how to get and set defaults for a parameter. In this case, the default for the CL_ORIGINAL_FORMAT parameter is set to CL_RGBX for the specified decompressor.

Example 6-4. Getting and Setting Parameter Defaults

#include <dmedia/cl.h>
int default;
...
clOpenDecompressor(scheme, &handle);
...
default = clGetDefault(handle, CL_ORIGINAL_FORMAT);
clSetDefault(scheme, CL_ORIGINAL_FORMAT, CL_RGBX);
...

Use clGetMinMax() to get the maximum and minimum values for a parameter. Use clSetMin() and clSetMax() to set new minimum and maximum parameter values, or to establish the minimum and maximum values when adding a new algorithm.

The function prototypes are

int clGetMinMax ( CLhandle handle, int param, int *minParam,
                  int *maxParam)

int clSetMin(int scheme, int paramID, int min)
int clSetMax(int scheme, int paramID, int max)

where

handle 

is a handle that identifies a compressor or decompressor.

paramID 

is a parameter identifier.

minParam 

is a pointer to the parameter into which clGetMinMax() can write the minimum value associated with paramID.

maxParam 

is a pointer to the parameter into which clGetMinMax() can write the maximum value associated with paramID.

scheme 

is the identifier of the scheme that is to have its minimum or maximum value changed.

min 

is the new minimum value associated with paramID.

max 

is the new maximum value associated with paramID.

Example 6-5 demonstrates how to get and set the minimum and maximum values of a particular parameter for the specified compressor or decompressor.

Example 6-5. Getting and Setting Minimum and Maximum Parameter Values

#include <dmedia/cl.h>
int oldMin, oldMax;
...
clOpenDecompressor(scheme, &handle);
6
...
clGetMinMax(handle, CL_ORIGINAL_FORMAT, &oldMin, &oldMax);
clSetMin(scheme, CL_ORIGINAL_FORMAT, CL_RGB);
clSetMax(scheme, CL_ORIGINAL_FORMAT, CL_RGB332);
...


Using Frame Type Parameters

Some compression algorithms do not allow direct compression or decompression of an arbitrary frame. These algorithms—MPEG, CCITT H.261, and so on—have blocks of frames, where each frame can be decompressed only if all previous frames in the block have been decompressed. The frame at the beginning of the block is called a keyframe.

A frame can be queried for its status as a keyframe by using the CL_FRAME_TYPE state parameter. Legal values are CL_KEYFRAME (or CL_INTRA), CL_PREDICTED, and CL_BIDIRECTIONAL. Predicted frames use information from a previous keyframe, bidirectional frames use information from both previous and future reference frames, where a reference frame is either of the other two types—CL_KEYFRAME or CL_PREDICTED. The Compression Library interface allows keyframe control from the application.

Some algorithms contain only keyframes, such as JPEG, MVC1, RTR, RLE, G.711, and so on. MPEG Video is the only algorithm currently supported that has all three types of frames.