add some code
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// Copyright 2024 Espressif Systems (Shanghai) CO., LTD.
|
||||
// All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/**
|
||||
* @brief JPEG error code
|
||||
*/
|
||||
typedef enum {
|
||||
JPEG_ERR_OK = 0, /*!< Succeeded */
|
||||
JPEG_ERR_FAIL = -1, /*!< Device error or wrong termination of input stream */
|
||||
JPEG_ERR_NO_MEM = -2, /*!< Insufficient memory for the image */
|
||||
JPEG_ERR_NO_MORE_DATA = -3, /*!< Input data is not enough */
|
||||
JPEG_ERR_INVALID_PARAM = -4, /*!< Parameter error */
|
||||
JPEG_ERR_BAD_DATA = -5, /*!< Data format error (may be damaged data) */
|
||||
JPEG_ERR_UNSUPPORT_FMT = -6, /*!< Right format but not supported */
|
||||
JPEG_ERR_UNSUPPORT_STD = -7, /*!< Not supported JPEG standard */
|
||||
} jpeg_error_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG pixel format
|
||||
*/
|
||||
typedef enum {
|
||||
JPEG_PIXEL_FORMAT_GRAY = 0, /*!< Grayscale. 1-byte luminance component stored in memory for each pixel.
|
||||
Encoder supported. Decoder un-supported. */
|
||||
JPEG_PIXEL_FORMAT_RGB888 = 1, /*!< RGB888. 3-bytes red, green and blue components stored in memory from low to high address for each pixel.
|
||||
Encoder supported. Decoder supported. */
|
||||
JPEG_PIXEL_FORMAT_RGBA = 2, /*!< RGBA. 4-bytes red, green, blue and alpha components stored in memory from low to high address for each pixel.
|
||||
Encoder supported. Decoder un-supported. */
|
||||
JPEG_PIXEL_FORMAT_YCbYCr = 3, /*!< YCbYCr (belongs to packed yuv422). 4-bytes Y, Cb, Y and Cr components stored in memory from low to high address for each 2 pixels.
|
||||
Encoder supported. Decoder un-supported. */
|
||||
JPEG_PIXEL_FORMAT_YCbY2YCrY2 = 4, /*!< YCbY2YCrY2 (belongs to packed yuv420). 12-bytes Y, Cb, Y, Y, Cb, Y, Y, Cr, Y, Y, Cr and Y components stored in memory from low to high address for each 8 pixels.
|
||||
Encoder supported. Decoder un-supported. */
|
||||
JPEG_PIXEL_FORMAT_RGB565_BE = 5, /*!< RGB565. 2-bytes RGB565 big-endian data stored in memory for each pixel.
|
||||
Encoder un-supported. Decoder supported. */
|
||||
JPEG_PIXEL_FORMAT_RGB565_LE = 6, /*!< RGB565. 2-bytes RGB565 little-endian data stored in memory for each pixel.
|
||||
Encoder un-supported. Decoder supported. */
|
||||
JPEG_PIXEL_FORMAT_CbYCrY = 7, /*!< CbYCrY (belongs to packed yuv422). 4-bytes Cb, Y, Cr and Y components stored in memory from low to high address for each 2 pixels.
|
||||
Encoder un-supported. Decoder supported. */
|
||||
} jpeg_pixel_format_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG chrominance subsampling type
|
||||
*/
|
||||
typedef enum {
|
||||
JPEG_SUBSAMPLE_GRAY = 0, /*!< Grayscale, no chrominance components */
|
||||
JPEG_SUBSAMPLE_444 = 1, /*!< 4:4:4 chrominance subsampling, one chroma component for every pixel in the source image */
|
||||
JPEG_SUBSAMPLE_422 = 2, /*!< 4:2:2 chrominance subsampling, one chroma component for every 2x1 block of pixels in the source image */
|
||||
JPEG_SUBSAMPLE_420 = 3, /*!< 4:2:0 chrominance subsampling, one chroma component for every 2x2 block of pixels in the source image */
|
||||
} jpeg_subsampling_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG rotation type
|
||||
*/
|
||||
typedef enum {
|
||||
JPEG_ROTATE_0D = 0, /*!< Source image rotates clockwise 0 degree before encoding */
|
||||
JPEG_ROTATE_90D = 1, /*!< Source image rotates clockwise 90 degree before encoding */
|
||||
JPEG_ROTATE_180D = 2, /*!< Source image rotates clockwise 180 degree before encoding */
|
||||
JPEG_ROTATE_270D = 3, /*!< Source image rotates clockwise 270 degree before encoding */
|
||||
} jpeg_rotate_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG resize resolution parameter
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t width; /*!< Image width */
|
||||
uint16_t height; /*!< Image height */
|
||||
} jpeg_resolution_t;
|
||||
|
||||
/**
|
||||
* @brief Allocate buffer. The buffer address will be aligned.
|
||||
*
|
||||
* @param[in] size Allocate buffer size
|
||||
* @param[in] aligned Aligned byte
|
||||
*
|
||||
* @return
|
||||
* - NULL Failed
|
||||
* - Others Allocated buffer address
|
||||
*/
|
||||
void *jpeg_calloc_align(size_t size, int aligned);
|
||||
|
||||
/**
|
||||
* @brief Free buffer. The buffer address come from `jpeg_calloc_align`
|
||||
*
|
||||
* @param[in] buf Buffer address
|
||||
*/
|
||||
void jpeg_free_align(void *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
@@ -0,0 +1,162 @@
|
||||
// Copyright 2024 Espressif Systems (Shanghai) CO., LTD.
|
||||
// All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "esp_jpeg_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define DEFAULT_JPEG_DEC_CONFIG() { \
|
||||
.output_type = JPEG_PIXEL_FORMAT_RGB888, \
|
||||
.scale = {.width = 0, .height = 0}, \
|
||||
.clipper = {.width = 0, .height = 0}, \
|
||||
.rotate = JPEG_ROTATE_0D, \
|
||||
.block_enable = false, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief JPEG decoder handle
|
||||
*/
|
||||
typedef void *jpeg_dec_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Configuration for JPEG decoder
|
||||
*
|
||||
* @note Feature supported and limitations
|
||||
* - support scale. Maximum scale ratio is 1/8. Scale.height and scale.width require integer multiples of 8.
|
||||
* - support clipper. Clipper.height and clipper.width require integer multiples of 8.
|
||||
* - support 0, 90, 180 and 270 clockwise rotation. Width and height require integer multiples of 8.
|
||||
* - support block decoder mode.
|
||||
*
|
||||
* The general flow of decoder
|
||||
* image data ----> decoding (required) ----> scale (optional) ----> clipper (optional) ----> rotation (optional)
|
||||
*
|
||||
* Each case of general flow:
|
||||
* image -> decoding: The variety of width and height supported.
|
||||
* image -> decoding -> scale: The scale.height and scale.width require integer multiples of 8.
|
||||
* image -> decoding -> clipper: The clipper.height and clipper.width require integer multiples of 8.
|
||||
* image -> decoding -> rotation: The height and width of image require integer multiples of 8.
|
||||
* image -> decoding -> scale -> clipper: The scale.height, scale.width, clipper.height and clipper.width require integer multiples of 8. The resolution of clipper should be less or equal than scale.
|
||||
* image -> decoding -> scale -> rotation: The scale.height and scale.width require integer multiples of 8.
|
||||
* image -> decoding -> clipper -> rotation: The clipper.height and clipper.width require integer multiples of 8.
|
||||
* image -> decoding -> scale -> clipper -> rotation: The scale.height, scale.width, clipper.height and clipper.width require integer multiples of 8. The resolution of clipper should be less or equal than scale.
|
||||
*
|
||||
* Flow of block decoder mode
|
||||
* image data ----> decoding
|
||||
*/
|
||||
typedef struct {
|
||||
jpeg_pixel_format_t output_type; /*!< Output pixel format. Support RGB888 RGB565_BE RGB565_LE CbYCrY, see jpeg_pixel_format_t enumeration. */
|
||||
jpeg_resolution_t scale; /*!< Resolution of scale. scale.width = 0 means disable the width scale. scale.height = 0 means disable the height scale.
|
||||
If enable this mode, scale.width and scale.height require integer multiples of 8. */
|
||||
jpeg_resolution_t clipper; /*!< Resolution of clipper. clipper.width = 0 means disable the width clipper. clipper.height = 0 means disable the height clipper.
|
||||
If enable this mode, clipper.width and clipper.height require integer multiples of 8. */
|
||||
jpeg_rotate_t rotate; /*!< Support 0 90 180 270 degree clockwise rotation.
|
||||
Only support when both width and height are multiples of 8. Otherwise unsupported. */
|
||||
bool block_enable; /*!< Configured to true to enable block mode, each time output 8 or 16 line data depending on the height of MCU(Minimum Coded Unit).
|
||||
If enabled this mode, scale and clipper are not supported, the width and height require integer multiples of 8 and rotate require JPEG_ROTATE_0D.
|
||||
Configured to false to disable block mode. */
|
||||
} jpeg_dec_config_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG decoder output infomation
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t width; /*!< Image width */
|
||||
uint16_t height; /*!< Image height */
|
||||
} jpeg_dec_header_info_t;
|
||||
|
||||
/**
|
||||
* @brief JPEG decoder io control
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t *inbuf; /*!< The input buffer pointer */
|
||||
int inbuf_len; /*!< The length of the input buffer in bytes */
|
||||
int inbuf_remain; /*!< Not used length of the input buffer */
|
||||
uint8_t *outbuf; /*!< The buffer to store decoded data. The buffer must be aligned 16 byte. */
|
||||
int out_size; /*!< Output size of current block when block mode is enabled.
|
||||
Size of entire image when block mode is disabled. */
|
||||
} jpeg_dec_io_t;
|
||||
|
||||
/**
|
||||
* @brief Create a JPEG decoder handle, set user configuration infomation to decoder handle
|
||||
*
|
||||
* @param[in] config Pointer to configuration information
|
||||
* @param[out] jpeg_dec Pointer to JPEG decoder handle
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to create handle
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_open(jpeg_dec_config_t *config, jpeg_dec_handle_t *jpeg_dec);
|
||||
|
||||
/**
|
||||
* @brief Parse JPEG header, and output infomation to user
|
||||
*
|
||||
* @param[in] jpeg_dec JPEG decoder handle
|
||||
* @param[in] io Pointer to io control struct
|
||||
* @param[out] out_info Pointer to output infomation struct
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to parse JPEG header
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_parse_header(jpeg_dec_handle_t jpeg_dec, jpeg_dec_io_t *io, jpeg_dec_header_info_t *out_info);
|
||||
|
||||
/**
|
||||
* @brief Get image buffer size
|
||||
*
|
||||
* @note User buffer must align to 16 bytes, user can malloc it through helper function `jpeg_calloc_align`.
|
||||
*
|
||||
* @param[in] jpeg_dec JPEG decoder handle
|
||||
* @param[out] outbuf_len Image buffer size
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to get output buffer size
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_get_outbuf_len(jpeg_dec_handle_t jpeg_dec, int *outbuf_len);
|
||||
|
||||
/**
|
||||
* @brief Get the number of times to call `jpeg_dec_process`
|
||||
*
|
||||
* @note If disable block decoder mode, the process_count always be 1.
|
||||
*
|
||||
* @param[in] jpeg_dec JPEG decoder handle
|
||||
* @param[out] process_count Number of times
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to get process times
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_get_process_count(jpeg_dec_handle_t jpeg_dec, int *process_count);
|
||||
|
||||
/**
|
||||
* @brief Decode JPEG picture
|
||||
*
|
||||
* @param[in] jpeg_dec JPEG decoder handle
|
||||
* @param[in] io Pointer to io control struct
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to decode JPEG picture
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_process(jpeg_dec_handle_t jpeg_dec, jpeg_dec_io_t *io);
|
||||
|
||||
/**
|
||||
* @brief Close JPEG decoder
|
||||
*
|
||||
* @param[in] jpeg_dec JPEG decoder handle
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK On success
|
||||
* - Others Fail to close JPEG decoder
|
||||
*/
|
||||
jpeg_error_t jpeg_dec_close(jpeg_dec_handle_t jpeg_dec);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
@@ -0,0 +1,129 @@
|
||||
// Copyright 2024 Espressif Systems (Shanghai) CO., LTD.
|
||||
// All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_jpeg_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define DEFAULT_JPEG_ENC_CONFIG() { \
|
||||
.width = 320, \
|
||||
.height = 240, \
|
||||
.src_type = JPEG_PIXEL_FORMAT_YCbYCr, \
|
||||
.subsampling = JPEG_SUBSAMPLE_420, \
|
||||
.quality = 40, \
|
||||
.rotate = JPEG_ROTATE_0D, \
|
||||
.task_enable = false, \
|
||||
.hfm_task_priority = 13, \
|
||||
.hfm_task_core = 1, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief JPEG encoder handle
|
||||
*/
|
||||
typedef void *jpeg_enc_handle_t;
|
||||
|
||||
/* JPEG configure Configuration */
|
||||
typedef struct jpeg_info {
|
||||
int width; /*!< Image width */
|
||||
int height; /*!< Image height */
|
||||
jpeg_pixel_format_t src_type; /*!< Input image type */
|
||||
jpeg_subsampling_t subsampling; /*!< JPEG chroma subsampling factors. If `src_type` is JPEG_PIXEL_FORMAT_YCbY2YCrY2, this must be `JPEG_SUBSAMPLE_GRAY` or `JPEG_SUBSAMPLE_420`. Others is un-support */
|
||||
uint8_t quality; /*!< Quality: 1-100, higher is better. Typical values are around 40 - 100. */
|
||||
jpeg_rotate_t rotate; /*!< Supports 0, 90 180 270 degree clockwise rotation.
|
||||
Under src_type = JPEG_PIXEL_FORMAT_YCbYCr, subsampling = JPEG_SUBSAMPLE_420, width % 16 == 0. height % 16 = 0 conditions, rotation enabled.
|
||||
Otherwise unsupported */
|
||||
bool task_enable; /*!< True: `jpeg_enc_open` would create task to finish part of encoding work. false: no task help the encoder encode */
|
||||
uint8_t hfm_task_priority; /*!< Task priority. If task_enable is true, this must be set */
|
||||
uint8_t hfm_task_core; /*!< Task core. If task_enable is true, this must be set */
|
||||
} jpeg_enc_config_t;
|
||||
|
||||
/**
|
||||
* @brief Open JPEG encoder
|
||||
*
|
||||
* @param[in] info The configuration information
|
||||
* @param[out] jpeg_enc The JPEG encoder handle
|
||||
*
|
||||
* @return
|
||||
* - positive value JPEG encoder handle
|
||||
* - NULL refer to `jpeg_error_t`
|
||||
*/
|
||||
jpeg_error_t jpeg_enc_open(jpeg_enc_config_t *info, jpeg_enc_handle_t *jpeg_enc);
|
||||
|
||||
/**
|
||||
* @brief Encode one image
|
||||
*
|
||||
* @param[in] jpeg_enc The JPEG encoder handle. It gained from `jpeg_enc_open`
|
||||
* @param[in] in_buf The input buffer, It needs a completed image. The buffer must be aligned 16 byte.
|
||||
* @param[in] inbuf_size The size of `in_buf`. The value must be size of a completed image.
|
||||
* @param[out] out_buf The output buffer
|
||||
* @param[out] outbuf_size The size of output buffer
|
||||
* @param[out] out_size The size of JPEG image
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK It has finished to encode one image.
|
||||
* - other values refer to `jpeg_error_t`
|
||||
*/
|
||||
jpeg_error_t jpeg_enc_process(const jpeg_enc_handle_t jpeg_enc, const uint8_t *in_buf, int inbuf_size, uint8_t *out_buf, int outbuf_size, int *out_size);
|
||||
|
||||
/**
|
||||
* @brief Get block size. Block size is minimum process unit.
|
||||
*
|
||||
* @param[in] jpeg_enc The JPEG encoder handle. It gained from `jpeg_enc_open`
|
||||
*
|
||||
* @return
|
||||
* - positive value block size
|
||||
* - other values not valid data
|
||||
*/
|
||||
int jpeg_enc_get_block_size(const jpeg_enc_handle_t jpeg_enc);
|
||||
|
||||
/**
|
||||
* @brief Encode block size image. Get block size from `jpeg_enc_get_block_size`
|
||||
*
|
||||
* @param[in] jpeg_enc The JPEG encoder handle. It gained from `jpeg_enc_open`
|
||||
* @param[in] in_buf The input buffer, It needs a completed image.
|
||||
* @param[in] inbuf_size The size of `in_buf`. Get block size from `jpeg_enc_get_block_size`
|
||||
* @param[out] out_buf The output buffer, it saves a completed JPEG image.
|
||||
* @param[out] outbuf_size The size of output buffer
|
||||
* @param[out] out_size The size of JPEG image
|
||||
*
|
||||
* @return
|
||||
* - block size It has finished to encode current block size image.
|
||||
* - JPEG_ERR_OK It has finished to encode one image.
|
||||
* - JPEG_ERR_FAIL Encoder failed.
|
||||
*/
|
||||
jpeg_error_t jpeg_enc_process_with_block(const jpeg_enc_handle_t jpeg_enc, const uint8_t *in_buf, int inbuf_size, uint8_t *out_buf, int outbuf_size, int *out_size);
|
||||
|
||||
/**
|
||||
* @brief Reset quality.
|
||||
*
|
||||
* @note After encoding an entire image, user can call this function to change the quality value.
|
||||
*
|
||||
* @param[in] jpeg_enc The JPEG encoder handle. It gained from `jpeg_enc_open`
|
||||
* @param[in] q Quality: 1-100, higher is better. Typical values are around 40 - 100.
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK succeed
|
||||
* - others values refer to `jpeg_error_t`
|
||||
*/
|
||||
jpeg_error_t jpeg_enc_set_quality(const jpeg_enc_handle_t jpeg_enc, uint8_t q);
|
||||
|
||||
/**
|
||||
* @brief Close JPEG encoder
|
||||
*
|
||||
* @param[in] jpeg_enc The JPEG encoder handle. It gained from `jpeg_enc_open`
|
||||
*
|
||||
* @return
|
||||
* - JPEG_ERR_OK It has finished to deinit.
|
||||
* - other values refer to `jpeg_error_t`
|
||||
*/
|
||||
jpeg_error_t jpeg_enc_close(jpeg_enc_handle_t jpeg_enc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2024 Espressif Systems (Shanghai) CO., LTD.
|
||||
// All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define ESP_JPEG_VERION "0.5.1"
|
||||
|
||||
/**
|
||||
* @version 0.5.0:
|
||||
* Features:
|
||||
* Encoder:
|
||||
* - Support variety of width and height to encoder
|
||||
* - Support RGB888 RGBA YCbYCr YCbY2YCrY2 GRAY pixel format
|
||||
* - Support YUV444 YUV422 YUV420 subsampling
|
||||
* - Support quality(1-100)
|
||||
* - Support 0 90 180 270 degree clockwise rotation, under src_type = JPEG_PIXEL_FORMAT_YCbYCr,
|
||||
* subsampling = JPEG_SUBSAMPLE_420, width and height are multiply of 16 and
|
||||
* src_type = JPEG_PIXEL_FORMAT_YCbYCr, subsampling = JPEG_SUBSAMPLE_GRAY, width and height are multiply of 8
|
||||
* - Support mono-task and dual-task
|
||||
* - Support two mode encoder, respectively one image encoder and block encoder
|
||||
* Decoder:
|
||||
* - Support variety of width and height to decoder
|
||||
* - Support one and three channels decoder
|
||||
* - Support RGB888 RGB565(big endian) RGB565(little endian) CbYCrY pixel format output
|
||||
* - Support 0 90 180 270 degree clockwise rotation, under width and height are multiply of 8
|
||||
* - Support clipper and scale, under width and height are multiply of 8
|
||||
* - Support two mode decoder, respectively one image decoder and block decoder
|
||||
*
|
||||
* @note The encoder/decoder do ASM optimization in ESP32S3. Frame rate performs better than the others chips.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Get JPEG version string
|
||||
*
|
||||
* @return
|
||||
* - JPEG codec version
|
||||
*/
|
||||
const char *esp_jpeg_get_version();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
Reference in New Issue
Block a user