HAL JPEG How to Use

group JPEG_How_To_Use

How to use the HAL JPEG driver

The HAL JPEG driver can be used as follows:

  • Initialize the JPEG handle by calling the HAL_JPEG_Init() API that performs these operations:

    • Associate instance to the handle

    • Enable the JPEG clock interface (When USE_HAL_JPEG_CLK_ENABLE_MODEL compilation flag is set to HAL_CLK_ENABLE_PERIPH_ONLY or HAL_CLK_ENABLE_PERIPH_PWR_SYSTEM in the stm32u5xx_hal_conf.h module)

    • Initialize of the handle state to the HAL_JPEG_STATE_IDLE.

  • For JPEG encoding, use the HAL_JPEG_SetConfigEncoding() function to set the encoding parameters. This step is mandatory before calling the encoding function. The application can adjust the image_quality parameter, ranging from 1 to 100, to balance visual quality against the original raw image and the resulting JPEG file size. Higher values result in better visual quality but larger file sizes, while lower values produce smaller file sizes with reduced visual quality.

  • Note that for decoding operation the JPEG peripheral output data are organized in YCbCr blocks called MCU (Minimum Coded Unit) as defined in the JPEG specification ISO/IEC 10918-1 standard. It is up to the application to transform these YCbCr blocks to RGB data that can be display.

    Respectively, for Encoding operation the JPEG peripheral input must be organized in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr MCU blocks transformation before feeding the JPEG peripheral with data.

  • Use the HAL_JPEG_Encode() and HAL_JPEG_Decode() functions to start respectively a JPEG encoding/decoding operation in polling method (blocking).

  • Use the HAL_JPEG_Encode_IT() and HAL_JPEG_Decode_IT() functions to start respectively a JPEG encoding/decoding operation with Interrupt method (non blocking).

  • Use the HAL_JPEG_Encode_DMA() and HAL_JPEG_Decode_DMA() functions to start respectively a JPEG encoding/decoding operation with DMA method (non blocking).

  • Use the HAL_JPEG_GetState() function to get the HAL JPEG global_state.

  • Use the HAL_JPEG_GetEncoderState() function to get the HAL JPEG encode_config_state.

  • Use the HAL_JPEG_GetHeaderProcessingState() function to get the HAL JPEG header_processing_state.

Pause Resume feature usage

Pause Resume InputBuffer usage

Pause Resume OutputBuffer usage

Header parsing and generation feature

  • The Header parsing and generation feature is enabled by default within the HAL JPEG driver.

  • User can disable this feature by calling HAL_JPEG_DisableHeaderProcessing() .

  • User can enable this feature by calling HAL_JPEG_EnableHeaderProcessing() .

  • User can check whether the header processing feature is Enabled/Disabled by calling HAL_JPEG_GetHeaderProcessingState() .

  • For decoding with header processing disabled the user have to provide the decoding parameter to the CODEC (as they are no longer filled by the hardware ).

  • For encoding with Header generation disabled the encoder produces a raw JPEG bitstream. This bitstream does not include the standard JPEG headers that are typically found in a complete JPEG file. Instead, it contains only the compressed image data.

Custom quantization tables usage

  • By default the HAL JPEG driver uses the default quantization tables as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding. User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTableX() Note that for decoding the quantization tables are automatically extracted from the JPEG header.

  1. Declare a 64 value quantization table for color component x .

  2. Override the default table(s) by calling HAL_JPEG_SetUserQuantTableX().

  3. Call the HAL_JPEG_SetConfigEncoding() to set the encoding parameters .

  4. Start encoding operation by calling one of : HAL_JPEG_Encode() / HAL_JPEG_Encode_IT() / HAL_JPEG_Encode_DMA() .

Callback definition

  • The HAL_JPEG_InfoReadyCallback() callback is invoked during JPEG decoding operations to provide the application with JPEG image parameters. This callback is triggered when the JPEG peripheral successfully parses the JPEG header.

  • The HAL_JPEG_GetDataCallback() callback is invoked during both encoding and decoding operations. It notifies the application that the input buffer has been consumed by the peripheral and requests a new data chunk if the operation (encoding or decoding) is not yet complete.

  • The HAL_JPEG_DataReadyCallback() callback is invoked when the HAL JPEG driver has filled the provided output buffer with the specified size.

    • This callback must be implemented on the application side. It must call the HAL_JPEG_UpdateOutputBuffer() function to provide the HAL JPEG driver with the new output buffer location and size for storing the next data chunk. If the application is not ready to provide the output buffer location, it can call the HAL_JPEG_PauseOutputBuffer() function to inform the JPEG HAL driver to pause output data. Once the application is ready to receive the new data chunk (i.e., the output buffer location is free or available), it must call the HAL_JPEG_UpdateOutputBuffer() function to provide the HAL JPEG driver with the new output buffer location and size. Then, it must call HAL_JPEG_ResumeOutputBuffer() to inform the HAL to resume outputting data into the provided output buffer.

  • The HAL_JPEG_EncodeCpltCallback() callback is invoked when the HAL JPEG driver has completed the current JPEG encoding operation and all output data has been transmitted to the application.

  • The HAL_JPEG_DecodeCpltCallback() callback is invoked when the HAL JPEG driver has completed the current JPEG decoding operation and all output data has been transmitted to the application.

  • The HAL_JPEG_ErrorCallback() callback is invoked when an error occurs during the current operation. The application can call the HAL_JPEG_GetLastErrorCodes() function to retrieve the error codes.

Callback registration

By default, after the HAL_JPEG_Init() and when the global_state is HAL_JPEG_STATE_IDLE all callbacks are set to the corresponding weak functions.

Callbacks can be registered in HAL_JPEG_STATE_IDLE global_state only. When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration feature is not available and all callbacks are set to the corresponding weak functions.

Interrupt functions