RA Flexible Software Package Documentation
Release v5.7.0
|
|
Driver for the JPEG peripheral on RA MCUs. This module implements the JPEG Codec Interface.
The JPEG Codec is a hardware block providing accelerated JPEG image encode and decode functionality independent of the CPU. Images can optionally be partially processed facilitating streaming applications.
The JPEG Codec provides a number of options useful in a variety of applications:
The specifications for the codec are as follows:
Feature | Options |
---|---|
Decompression input formats | Baseline JPEG Y'CbCr 4:4:4, 4:2:2, 4:2:0 and 4:1:1 |
Decompression output formats | ARGB8888, RGB565 |
Compression input formats | Raw Y'CbCr 4:2:2 only |
Compression output formats | Baseline JPEG Y'CbCr 4:2:2 only |
Byte reordering | Byte, halfword and/or word swapping on input and output |
Interrupt sources | Image size acquired, input/output data pause, decode complete, error |
Compatible image sizes | See Minimum Coded Unit (MCU) below |
Configuration | Options | Default | Description |
---|---|---|---|
Parameter Checking |
| Default (BSP) | If selected, code for parameter checking is included in the build. |
Decode Support |
| Enabled | If selected, code for decoding JPEG images is included in the build. |
Encode Support |
| Disabled | If selected, code for encoding JPEG images is included in the build. |
Configuration | Options | Default | Description |
---|---|---|---|
General | |||
Name | Name must be a valid C symbol | g_jpeg0 | Module name. |
Default mode |
| Decode | Set the mode to use when calling R_JPEG_Open. This parameter is only used when both Encode and Decode support are enabled. |
Decode | |||
Input byte order | MCU Specific Options | Select the byte order of the input data for decoding. | |
Output byte order | MCU Specific Options | Select the byte order of the output data for decoding. | |
Output color format |
| RGB565 (16-bit) | Select the output pixel format for decode operations. |
Output alpha (ARGB8888 only) | Value must be an 8-bit integer (0-255) | 255 | Specify the alpha value to apply to each output pixel when ARGB8888 format is chosen. |
Callback | Name must be a valid C symbol | NULL | If a callback function is provided it will be called from the interrupt service routine (ISR) each time a related IRQ triggers. |
Encode | |||
Horizontal resolution | Value cannot be greater than 65535 and must be a non-negative integer divisible by 16 | 480 | Horizontal resolution of the raw image (in pixels). This value can be configured at runtime via R_JPEG_ImageSizeSet. |
Vertical resolution | Value cannot be greater than 65535 and must be a non-negative integer divisible by 8 | 272 | Vertical resolution of the raw image. This value can be configured at runtime via R_JPEG_ImageSizeSet. |
Horizontal stride | Value cannot be greater than 65535 and must be a non-negative integer | 480 | Horizontal stride of the raw image buffer (in pixels). This value can be configured at runtime via R_JPEG_ImageSizeSet. |
Input byte order | MCU Specific Options | Select the byte order of the input data for encoding. | |
Output byte order | MCU Specific Options | Select the byte order of the output data for encoding. | |
Reset interval | Value cannot be greater than 65535 and must be a non-negative integer | 512 | Set the number of MCUs between RST markers. A value of 0 will disable DRI and RST marker output. |
Quality factor | Value must be between 1 and 100 and must be an integer | 50 | Set the quality factor for encoding (1-100). Lower values produce smaller images at the cost of image quality. |
Callback | Name must be a valid C symbol | NULL | If a callback function is provided it will be called from the interrupt service routine (ISR) each time a related IRQ triggers. |
Interrupts | |||
Decode Process Interrupt Priority | MCU Specific Options | Select the decompression interrupt priority. | |
Data Transfer Interrupt Priority | MCU Specific Options | Select the data transfer interrupt priority. |
The peripheral clock for this module is PCLKA. No clocks are provided by this module.
This module does not have any input or output pin connections.
The JPEG Codec contains both decode and encode hardware. While these two functions are largely independent in configuration only one can be used at a time.
To switch from decode to encode mode (or vice versa) use R_JPEG_ModeSet while the JPEG Codec is idle.
The status value (jpeg_status_t) provided by the callback and by R_JPEG_StatusGet is a bitfield that encompasses all potential status indication conditions. One or more statuses can be set simultaneously.
JPEG decoding can be performed in several ways depending on the application:
The flowchart below illustrates the steps necessary to handle any decode operation. The statuses given in blue are part of jpeg_status_t with the JPEG_DECODE_STATUS prefix omitted.
As compared to decoding, encoding is fairly straightforward. The only option available is to stream input data if desired. The flowchart below details the steps needed to compress an image.
If an encode or decode operation fails or times out while the codec is running, the peripheral must be reset before it is used again. To reset the JPEG Codec simply close and re-open the module by calling R_JPEG_Close followed by R_JPEG_Open.
Developers should be aware of the following limitations when using the JPEG API.
The JPEG Codec can only correctly process images that are an even increment of minimum coded units (MCUs). In other words, depending on the format the width and height of an image to be encoded or decoded must be divisible by the following:
Format | Horizontal | Vertical |
---|---|---|
Y'CbCr 4:4:4 | 8 pixels | 8 lines |
Y'CbCr 4:2:2 | 16 pixels | 8 lines |
Y'CbCr 4:1:1 | 32 pixels | 8 lines |
Y'CbCr 4:2:0 | 16 pixels | 16 lines |
The encoding unit only supports Y'CbCr 4:2:2 input. Raw RGB888 data can be converted to this format as follows:
While these equations are mathematically simple they do use the floating-point unit. To speed things up we can multiply the coefficients by 256 and divide the sum by 256...
...which allows the formulas to be calculated entirely with shifts and addition (coefficients rounded to the nearest integer):
To compose the final Y'CbCr 4:2:2 data the chroma of every two pixels must be averaged. In addition, the JPEG Codec expects chrominance values to be in the range -127..127 instead of the standard 1..255.
Finally, the below equation composes two 4:2:2 output pixels at a time with standard byte order (JPEG_DATA_ORDER_NORMAL
):
This is a basic example showing the minimum code required to initialize the JPEG Codec and decode an image.
In this example JPEG data is read in 512-byte chunks. Decoding is paused when a chunk is read and once the JPEG header is decoded. The image is decoded 16 lines at a time.
This is a basic example showing the minimum code required to initialize the JPEG Codec and encode an image.
In this example the raw input data is provided in smaller chunks. This can help significantly reduce buffer size and improve throughput when streaming in raw data from an outside source.
The below function is provided as a reference for how to convert RGB values to Y'CbCr for use with the JPEG Codec.
Data Structures | |
struct | jpeg_instance_ctrl_t |
struct jpeg_instance_ctrl_t |
JPEG Codec module control block. DO NOT INITIALIZE. Initialization occurs when jpep_api_t::open is called.
Data Fields | ||
---|---|---|
uint32_t | open | JPEG Codec driver status. |
jpeg_status_t | status | JPEG Codec operational status. |
fsp_err_t | error_code | JPEG Codec error code (if any). |
jpeg_mode_t | mode | Current mode (decode or encode). |
uint32_t | horizontal_stride_bytes | Horizontal Stride settings. |
uint32_t | output_buffer_size | Output buffer size. |
jpeg_cfg_t const * | p_cfg | JPEG Decode configuration struct. |
void const * | p_extend | JPEG Codec hardware dependent configuration */. |
jpeg_decode_pixel_format_t | pixel_format | Pixel format. |
uint16_t | total_lines_decoded | Track the number of lines decoded so far. |
jpeg_decode_subsample_t | horizontal_subsample | Horizontal sub-sample setting. |
uint16_t | lines_to_encode | Number of lines to encode. |
uint16_t | vertical_resolution | vertical size |
uint16_t | total_lines_encoded | Number of lines encoded. |
fsp_err_t R_JPEG_Open | ( | jpeg_ctrl_t *const | p_api_ctrl, |
jpeg_cfg_t const *const | p_cfg | ||
) |
Initialize the JPEG Codec module.
FSP_SUCCESS | JPEG Codec module is properly configured and is ready to take input data. |
FSP_ERR_ALREADY_OPEN | JPEG Codec is already open. |
FSP_ERR_ASSERTION | Pointer to the control block or the configuration structure is NULL. |
FSP_ERR_IRQ_BSP_DISABLED | JEDI interrupt does not have an IRQ number. |
FSP_ERR_INVALID_ARGUMENT | (Encode only) Quality factor, horizontal resolution and/or vertical resolution are invalid. |
FSP_ERR_INVALID_ALIGNMENT | (Encode only) The horizontal resolution (at 16bpp) is not divisible by 8 bytes. |
fsp_err_t R_JPEG_OutputBufferSet | ( | jpeg_ctrl_t * | p_api_ctrl, |
void * | p_output_buffer, | ||
uint32_t | output_buffer_size | ||
) |
Assign a buffer to the JPEG Codec for storing output data.
The JPEG decoding operation automatically starts after both the input buffer and the output buffer are set and the output buffer is big enough to hold at least eight lines of decoded image data.
FSP_SUCCESS | The output buffer is properly assigned to JPEG codec device. |
FSP_ERR_ASSERTION | Pointer to the control block or output_buffer is NULL or output_buffer_size is 0. |
FSP_ERR_INVALID_ALIGNMENT | Buffer starting address is not 8-byte aligned. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
FSP_ERR_JPEG_UNSUPPORTED_IMAGE_SIZE | The number of horizontal pixels exceeds horizontal memory stride. |
FSP_ERR_JPEG_BUFFERSIZE_NOT_ENOUGH | Invalid buffer size. |
FSP_ERR_IN_USE | The output buffer cannot be changed during codec operation. |
fsp_err_t R_JPEG_InputBufferSet | ( | jpeg_ctrl_t *const | p_api_ctrl, |
void * | p_data_buffer, | ||
uint32_t | data_buffer_size | ||
) |
Assign an input data buffer to the JPEG codec for processing.
If zero is provided for the decode data buffer size the JPEG Codec will never pause for more input data and will continue to read until either an image has been fully decoded or an error condition occurs.
FSP_SUCCESS | The input data buffer is properly assigned to JPEG Codec device. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL, or the pointer to the input_buffer is NULL, or the input_buffer_size is 0. |
FSP_ERR_INVALID_ALIGNMENT | Buffer starting address is not 8-byte aligned. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
FSP_ERR_IN_USE | The input buffer cannot be changed while the codec is running. |
FSP_ERR_INVALID_CALL | In encode mode the output buffer must be set first. |
FSP_ERR_JPEG_IMAGE_SIZE_ERROR | The buffer size is smaller than the minimum coded unit (MCU). |
fsp_err_t R_JPEG_DecodeLinesDecodedGet | ( | jpeg_ctrl_t * | p_api_ctrl, |
uint32_t * | p_lines | ||
) |
Returns the number of lines decoded into the output buffer.
FSP_SUCCESS | Line count successfully returned. |
FSP_ERR_ASSERTION | Pointer to the control block or p_lines is NULL. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
fsp_err_t R_JPEG_DecodeImageSubsampleSet | ( | jpeg_ctrl_t *const | p_api_ctrl, |
jpeg_decode_subsample_t | horizontal_subsample, | ||
jpeg_decode_subsample_t | vertical_subsample | ||
) |
Configure horizontal and vertical subsampling.
FSP_SUCCESS | Horizontal subsample value is properly configured. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
fsp_err_t R_JPEG_DecodeHorizontalStrideSet | ( | jpeg_ctrl_t * | p_api_ctrl, |
uint32_t | horizontal_stride | ||
) |
Configure horizontal stride setting for decode operations.
FSP_SUCCESS | Horizontal stride value is properly configured. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL. |
FSP_ERR_INVALID_ALIGNMENT | Horizontal stride is zero or is not 8-byte aligned. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
fsp_err_t R_JPEG_DecodeImageSizeGet | ( | jpeg_ctrl_t * | p_api_ctrl, |
uint16_t * | p_horizontal_size, | ||
uint16_t * | p_vertical_size | ||
) |
Obtain the size of an image being decoded.
FSP_SUCCESS | The image size is available and the horizontal and vertical values are stored in the memory pointed to by p_horizontal_size and p_vertical_size. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL and/or size is not ready. |
FSP_ERR_NOT_OPEN | JPEG is not opened. |
fsp_err_t R_JPEG_DecodePixelFormatGet | ( | jpeg_ctrl_t * | p_api_ctrl, |
jpeg_color_space_t * | p_color_space | ||
) |
Get the color format of the JPEG being decoded.
FSP_SUCCESS | The color format was successfully retrieved. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL. |
FSP_ERR_NOT_OPEN | JPEG is not opened. |
fsp_err_t R_JPEG_EncodeImageSizeSet | ( | jpeg_ctrl_t *const | p_api_ctrl, |
jpeg_encode_image_size_t * | p_image_size | ||
) |
Set the image dimensions for an encode operation.
FSP_SUCCESS | Image size was successfully written to the JPEG Codec. |
FSP_ERR_ASSERTION | Pointer to the control block or p_image_size is NULL. |
FSP_ERR_INVALID_ALIGNMENT | Horizontal stride is not 8-byte aligned. |
FSP_ERR_INVALID_ARGUMENT | Horizontal or vertical resolution is invalid or zero. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
FSP_ERR_IN_USE | Image parameters cannot be changed while the codec is running. |
fsp_err_t R_JPEG_ModeSet | ( | jpeg_ctrl_t *const | p_api_ctrl, |
jpeg_mode_t | mode | ||
) |
Switch between encode and decode mode (or vice-versa).
FSP_SUCCESS | Mode changed successfully. |
FSP_ERR_ASSERTION | p_api_ctrl is NULL. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_IN_USE | JPEG Codec is currently in use. |
FSP_ERR_INVALID_ARGUMENT | (Encode only) Quality factor, horizontal resolution and/or vertical resolution are invalid. |
FSP_ERR_INVALID_ALIGNMENT | (Encode only) The horizontal resolution (at 16bpp) is not divisible by 8 bytes. |
fsp_err_t R_JPEG_Close | ( | jpeg_ctrl_t * | p_api_ctrl | ) |
Cancel an outstanding JPEG codec operation and close the device.
FSP_SUCCESS | The JPEG unit is stopped and the driver is closed. |
FSP_ERR_ASSERTION | Pointer to the control block is NULL. |
FSP_ERR_NOT_OPEN | JPEG not opened. |
fsp_err_t R_JPEG_StatusGet | ( | jpeg_ctrl_t * | p_api_ctrl, |
jpeg_status_t * | p_status | ||
) |
Get the status of the JPEG codec. This function can also be used to poll the device.
FSP_SUCCESS | The status information is successfully retrieved. |
FSP_ERR_ASSERTION | Pointer to the control block or p_status is NULL. |
FSP_ERR_NOT_OPEN | JPEG is not opened. |