Skip to content

Commit

Permalink
Support: jpeg decoder on esp32c2 (#525)
Browse files Browse the repository at this point in the history
* Extend s/w jpeg decoder functions for chips missing rom tjpgd

Also, disabled camera_init and capture from example for chips not
supporting camera

Signed-off-by: Vikram <vikram.dattu@espressif.com>

* Extend CI build support for C3 and C2

Signed-off-by: Vikram <vikram.dattu@espressif.com>

---------

Signed-off-by: Vikram <vikram.dattu@espressif.com>
  • Loading branch information
vikramdattu authored Apr 25, 2023
1 parent 36b8b4e commit e689c3b
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ jobs:
strategy:
matrix:
idf_ver: ["release-v4.4", "release-v5.0", "release-v5.1", "latest"]
idf_target: ["esp32", "esp32s2", "esp32s3"]
idf_target: ["esp32", "esp32s2", "esp32s3", "esp32c2", "esp32c3"]
exclude:
- idf_ver: "release-v4.4"
idf_target: esp32c2 # ESP32C2 support started with version 5.0
container: espressif/idf:${{ matrix.idf_ver }}
steps:
- uses: actions/checkout@v1
Expand Down
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if(IDF_TARGET STREQUAL "esp32" OR IDF_TARGET STREQUAL "esp32s2" OR IDF_TARGET ST
list(APPEND COMPONENT_SRCS
target/xclk.c
target/esp32s2/ll_cam.c
target/esp32s2/tjpgd.c
target/tjpgd.c
)

list(APPEND COMPONENT_PRIV_INCLUDEDIRS
Expand All @@ -84,4 +84,15 @@ if(IDF_TARGET STREQUAL "esp32" OR IDF_TARGET STREQUAL "esp32s2" OR IDF_TARGET ST

endif()

# CONFIG_ESP_ROM_HAS_JPEG_DECODE is available from IDF v4.4 but
# previous IDF supported chips already support JPEG decoder, hence okay to use this
if(idf_version VERSION_GREATER_EQUAL "4.4" AND NOT CONFIG_ESP_ROM_HAS_JPEG_DECODE)
list(APPEND COMPONENT_SRCS
target/tjpgd.c
)
list(APPEND COMPONENT_PRIV_INCLUDEDIRS
target/jpeg_include/
)
endif()

register_component()
8 changes: 3 additions & 5 deletions conversions/esp_jpg_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
#if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
#include "esp32/rom/tjpgd.h"
#elif CONFIG_IDF_TARGET_ESP32S2
#include "tjpgd.h"
#elif CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rom/tjpgd.h"
#elif CONFIG_IDF_TARGET_ESP32C3
#include "esp32c3/rom/tjpgd.h"
#elif CONFIG_IDF_TARGET_ESP32H2
#include "esp32h2/rom/tjpgd.h"
#elif CONFIG_ESP_ROM_HAS_JPEG_DECODE // available since IDF 4.4
#include "rom/tjpgd.h" // latest IDFs have `rom/` includes available
#else
#error Target CONFIG_IDF_TARGET is not supported
#include "tjpgd.h" // using software decoder
#endif
#else // ESP32 Before IDF 4.0
#include "rom/tjpgd.h"
Expand Down
2 changes: 1 addition & 1 deletion driver/esp_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static esp_err_t camera_probe(const camera_config_t *config, camera_model_t *out

ESP_LOGD(TAG, "Doing SW reset of sensor");
vTaskDelay(10 / portTICK_PERIOD_MS);

return s_state->sensor.reset(&s_state->sensor);
err :
CAMERA_DISABLE_OUT_CLOCK();
Expand Down
18 changes: 12 additions & 6 deletions driver/include/esp_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
#include "sys/time.h"
#include "sdkconfig.h"

/**
* @brief define for if chip supports camera
*/
#define ESP_CAMERA_SUPPORTED (CONFIG_IDF_TARGET_ESP32 | CONFIG_IDF_TARGET_ESP32S3 | \
CONFIG_IDF_TARGET_ESP32S2)

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -85,7 +91,7 @@ typedef enum {
} camera_grab_mode_t;

/**
* @brief Camera frame buffer location
* @brief Camera frame buffer location
*/
typedef enum {
CAMERA_FB_IN_PSRAM, /*!< Frame buffer is placed in external PSRAM */
Expand All @@ -99,7 +105,7 @@ typedef enum {
typedef enum {
CONV_DISABLE,
RGB565_TO_YUV422,

YUV422_TO_RGB565,
YUV422_TO_YUV420
} camera_conv_mode_t;
Expand Down Expand Up @@ -219,15 +225,15 @@ sensor_t * esp_camera_sensor_get(void);

/**
* @brief Save camera settings to non-volatile-storage (NVS)
*
* @param key A unique nvs key name for the camera settings
*
* @param key A unique nvs key name for the camera settings
*/
esp_err_t esp_camera_save_to_nvs(const char *key);

/**
* @brief Load camera settings from non-volatile-storage (NVS)
*
* @param key A unique nvs key name for the camera settings
*
* @param key A unique nvs key name for the camera settings
*/
esp_err_t esp_camera_load_from_nvs(const char *key);

Expand Down
13 changes: 10 additions & 3 deletions examples/main/take_picture.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* 2. Kconfig setup
*
*
* If you have a Kconfig file, copy the content from
* https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
* In case you haven't, copy and paste this Kconfig file inside the src directory.
Expand All @@ -20,9 +20,9 @@

/**
* 3. Enable PSRAM on sdkconfig:
*
*
* CONFIG_ESP32_SPIRAM_SUPPORT=y
*
*
* More info on
* https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
*/
Expand Down Expand Up @@ -95,6 +95,7 @@

static const char *TAG = "example:take_picture";

#if ESP_CAMERA_SUPPORTED
static camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
Expand Down Expand Up @@ -139,9 +140,11 @@ static esp_err_t init_camera(void)

return ESP_OK;
}
#endif

void app_main(void)
{
#if ESP_CAMERA_SUPPORTED
if(ESP_OK != init_camera()) {
return;
}
Expand All @@ -157,4 +160,8 @@ void app_main(void)

vTaskDelay(5000 / portTICK_RATE_MS);
}
#else
ESP_LOGE(TAG, "Camera support is not available for this chip");
return;
#endif
}
99 changes: 99 additions & 0 deletions target/jpeg_include/tjpgd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*----------------------------------------------------------------------------/
/ TJpgDec - Tiny JPEG Decompressor include file (C)ChaN, 2012
/----------------------------------------------------------------------------*/
#ifndef _TJPGDEC
#define _TJPGDEC
/*---------------------------------------------------------------------------*/
/* System Configurations */

#define JD_SZBUF 512 /* Size of stream input buffer */
#define JD_FORMAT 0 /* Output pixel format 0:RGB888 (3 BYTE/pix), 1:RGB565 (1 WORD/pix) */
#define JD_USE_SCALE 1 /* Use descaling feature for output */
#define JD_TBLCLIP 1 /* Use table for saturation (might be a bit faster but increases 1K bytes of code size) */

/*---------------------------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif

/* These types must be 16-bit, 32-bit or larger integer */
typedef int INT;
typedef unsigned int UINT;

/* These types must be 8-bit integer */
typedef char CHAR;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;

/* These types must be 16-bit integer */
typedef short SHORT;
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned short WCHAR;

/* These types must be 32-bit integer */
typedef long LONG;
typedef unsigned long ULONG;
typedef unsigned long DWORD;


/* Error code */
typedef enum {
JDR_OK = 0, /* 0: Succeeded */
JDR_INTR, /* 1: Interrupted by output function */
JDR_INP, /* 2: Device error or wrong termination of input stream */
JDR_MEM1, /* 3: Insufficient memory pool for the image */
JDR_MEM2, /* 4: Insufficient stream input buffer */
JDR_PAR, /* 5: Parameter error */
JDR_FMT1, /* 6: Data format error (may be damaged data) */
JDR_FMT2, /* 7: Right format but not supported */
JDR_FMT3 /* 8: Not supported JPEG standard */
} JRESULT;



/* Rectangular structure */
typedef struct {
WORD left, right, top, bottom;
} JRECT;



/* Decompressor object structure */
typedef struct JDEC JDEC;
struct JDEC {
UINT dctr; /* Number of bytes available in the input buffer */
BYTE* dptr; /* Current data read ptr */
BYTE* inbuf; /* Bit stream input buffer */
BYTE dmsk; /* Current bit in the current read byte */
BYTE scale; /* Output scaling ratio */
BYTE msx, msy; /* MCU size in unit of block (width, height) */
BYTE qtid[3]; /* Quantization table ID of each component */
SHORT dcv[3]; /* Previous DC element of each component */
WORD nrst; /* Restart inverval */
UINT width, height; /* Size of the input image (pixel) */
BYTE* huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */
WORD* huffcode[2][2]; /* Huffman code word tables [id][dcac] */
BYTE* huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */
LONG* qttbl[4]; /* Dequaitizer tables [id] */
void* workbuf; /* Working buffer for IDCT and RGB output */
BYTE* mcubuf; /* Working buffer for the MCU */
void* pool; /* Pointer to available memory pool */
UINT sz_pool; /* Size of momory pool (bytes available) */
UINT (*infunc)(JDEC*, BYTE*, UINT);/* Pointer to jpeg stream input function */
void* device; /* Pointer to I/O device identifiler for the session */
};



/* TJpgDec API functions */
JRESULT jd_prepare (JDEC*, UINT(*)(JDEC*,BYTE*,UINT), void*, UINT, void*);
JRESULT jd_decomp (JDEC*, UINT(*)(JDEC*,void*,JRECT*), BYTE);


#ifdef __cplusplus
}
#endif

#endif /* _TJPGDEC */
File renamed without changes.

0 comments on commit e689c3b

Please sign in to comment.