From acf206a62a64a2e7087555909498ceb49c4b9742 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Fri, 30 Apr 2021 16:51:03 +0300 Subject: [PATCH] Add initial ESP32-S2 support --- CMakeLists.txt | 21 +- conversions/esp_jpg_decode.c | 6 +- conversions/include/img_converters.h | 3 + conversions/to_bmp.c | 67 ++ conversions/to_jpg.cpp | 8 +- driver/camera.c | 2 +- driver/esp_camera.c | 328 +++++++++ driver/include/sensor.h | 53 +- driver/private_include/cam_hal.h | 58 ++ driver/private_include/camera_common.h | 2 + driver/sccb.c | 15 +- driver/sensor.c | 9 + driver/xclk.c | 12 +- idf_component.yml | 3 - sensors/ov3660.c | 8 +- target/esp32s2/cam_hal.c | 701 ++++++++++++++++++ target/esp32s2/private_include/tjpgd.h | 99 +++ target/esp32s2/tjpgd.c | 970 +++++++++++++++++++++++++ 18 files changed, 2336 insertions(+), 29 deletions(-) create mode 100644 driver/esp_camera.c create mode 100644 driver/private_include/cam_hal.h delete mode 100644 idf_component.yml create mode 100644 target/esp32s2/cam_hal.c create mode 100644 target/esp32s2/private_include/tjpgd.h create mode 100755 target/esp32s2/tjpgd.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 489aeba..e43906c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,5 @@ -if(IDF_TARGET STREQUAL "esp32") +if(IDF_TARGET STREQUAL "esp32" OR IDF_TARGET STREQUAL "esp32s2") set(COMPONENT_SRCS - driver/camera.c driver/sccb.c driver/sensor.c driver/xclk.c @@ -28,6 +27,24 @@ if(IDF_TARGET STREQUAL "esp32") conversions/private_include ) + if(IDF_TARGET STREQUAL "esp32") + list(APPEND COMPONENT_SRCS + driver/camera.c + ) + endif() + + if(IDF_TARGET STREQUAL "esp32s2") + list(APPEND COMPONENT_SRCS + driver/esp_camera.c + target/esp32s2/cam_hal.c + target/esp32s2/tjpgd.c + ) + + list(APPEND COMPONENT_PRIV_INCLUDEDIRS + target/esp32s2/private_include + ) + endif() + set(COMPONENT_REQUIRES driver) set(COMPONENT_PRIV_REQUIRES freertos nvs_flash) diff --git a/conversions/esp_jpg_decode.c b/conversions/esp_jpg_decode.c index d42794f..a9615e3 100644 --- a/conversions/esp_jpg_decode.c +++ b/conversions/esp_jpg_decode.c @@ -17,7 +17,11 @@ #if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+ #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "esp32/rom/tjpgd.h" -#else +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "tjpgd.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/tjpgd.h" +#else #error Target CONFIG_IDF_TARGET is not supported #endif #else // ESP32 Before IDF 4.0 diff --git a/conversions/include/img_converters.h b/conversions/include/img_converters.h index 330f8db..f736200 100644 --- a/conversions/include/img_converters.h +++ b/conversions/include/img_converters.h @@ -22,6 +22,7 @@ extern "C" { #include #include #include "esp_camera.h" +#include "esp_jpg_decode.h" typedef size_t (* jpg_out_cb)(void * arg, size_t index, const void* data, size_t len); @@ -120,6 +121,8 @@ bool frame2bmp(camera_fb_t * fb, uint8_t ** out, size_t * out_len); */ bool fmt2rgb888(const uint8_t *src_buf, size_t src_len, pixformat_t format, uint8_t * rgb_buf); +bool jpg2rgb565(const uint8_t *src, size_t src_len, uint8_t * out, jpg_scale_t scale); + #ifdef __cplusplus } #endif diff --git a/conversions/to_bmp.c b/conversions/to_bmp.c index 85f9c88..5a54bdb 100644 --- a/conversions/to_bmp.c +++ b/conversions/to_bmp.c @@ -24,6 +24,10 @@ #if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+ #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "esp32/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/spiram.h" #else #error Target CONFIG_IDF_TARGET is not supported #endif @@ -115,6 +119,54 @@ static bool _rgb_write(void * arg, uint16_t x, uint16_t y, uint16_t w, uint16_t return true; } +static bool _rgb565_write(void * arg, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t *data) +{ + rgb_jpg_decoder * jpeg = (rgb_jpg_decoder *)arg; + if(!data){ + if(x == 0 && y == 0){ + //write start + jpeg->width = w; + jpeg->height = h; + //if output is null, this is BMP + if(!jpeg->output){ + jpeg->output = (uint8_t *)_malloc((w*h*3)+jpeg->data_offset); + if(!jpeg->output){ + return false; + } + } + } else { + //write end + } + return true; + } + + size_t jw = jpeg->width*3; + size_t jw2 = jpeg->width*2; + size_t t = y * jw; + size_t t2 = y * jw2; + size_t b = t + (h * jw); + size_t l = x * 2; + uint8_t *out = jpeg->output+jpeg->data_offset; + uint8_t *o = out; + size_t iy, iy2, ix, ix2; + + w = w * 3; + + for(iy=t, iy2=t2; iy> 3); + o[ix2+1] = c>>8; + o[ix2] = c&0xff; + } + data+=w; + } + return true; +} + //input buffer static uint32_t _jpg_read(void * arg, size_t index, uint8_t *buf, size_t len) { @@ -140,6 +192,21 @@ static bool jpg2rgb888(const uint8_t *src, size_t src_len, uint8_t * out, jpg_sc return true; } +bool jpg2rgb565(const uint8_t *src, size_t src_len, uint8_t * out, jpg_scale_t scale) +{ + rgb_jpg_decoder jpeg; + jpeg.width = 0; + jpeg.height = 0; + jpeg.input = src; + jpeg.output = out; + jpeg.data_offset = 0; + + if(esp_jpg_decode(src_len, scale, _jpg_read, _rgb565_write, (void*)&jpeg) != ESP_OK){ + return false; + } + return true; +} + bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_len) { diff --git a/conversions/to_jpg.cpp b/conversions/to_jpg.cpp index f8987a8..9b8905a 100644 --- a/conversions/to_jpg.cpp +++ b/conversions/to_jpg.cpp @@ -25,6 +25,10 @@ #if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+ #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "esp32/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/spiram.h" #else #error Target CONFIG_IDF_TARGET is not supported #endif @@ -195,7 +199,7 @@ public: return true; } if ((size_t)len > (max_len - index)) { - ESP_LOGW(TAG, "JPG output overflow: %d bytes", len - (max_len - index)); + //ESP_LOGW(TAG, "JPG output overflow: %d bytes (%d,%d,%d)", len - (max_len - index), len, index, max_len); len = max_len - index; } if (len) { @@ -215,7 +219,7 @@ bool fmt2jpg(uint8_t *src, size_t src_len, uint16_t width, uint16_t height, pixf { //todo: allocate proper buffer for holding JPEG data //this should be enough for CIF frame size - int jpg_buf_len = 64*1024; + int jpg_buf_len = 128*1024; uint8_t * jpg_buf = (uint8_t *)_malloc(jpg_buf_len); diff --git a/driver/camera.c b/driver/camera.c index 9e6a716..b89dd37 100755 --- a/driver/camera.c +++ b/driver/camera.c @@ -1321,7 +1321,7 @@ esp_err_t camera_init(const camera_config_t* config) } vsync_intr_disable(); - err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM); + err = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1 | ESP_INTR_FLAG_IRAM); if (err != ESP_OK) { if (err != ESP_ERR_INVALID_STATE) { ESP_LOGE(TAG, "gpio_install_isr_service failed (%x)", err); diff --git a/driver/esp_camera.c b/driver/esp_camera.c new file mode 100644 index 0000000..88b4e8b --- /dev/null +++ b/driver/esp_camera.c @@ -0,0 +1,328 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include +#include +#include +#include "time.h" +#include "sys/time.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "soc/soc.h" +#include "soc/gpio_sig_map.h" +#include "soc/i2s_reg.h" +#include "soc/i2s_struct.h" +#include "soc/io_mux_reg.h" +#include "driver/gpio.h" +#include "driver/rtc_io.h" +#include "driver/periph_ctrl.h" +#include "esp_intr_alloc.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "nvs.h" +#include "sensor.h" +#include "sccb.h" +#include "cam_hal.h" +#include "esp_camera.h" +// #include "camera_common.h" +#include "xclk.h" +#if CONFIG_OV2640_SUPPORT +#include "ov2640.h" +#endif +#if CONFIG_OV7725_SUPPORT +#include "ov7725.h" +#endif +#if CONFIG_OV3660_SUPPORT +#include "ov3660.h" +#endif +#if CONFIG_OV5640_SUPPORT +#include "ov5640.h" +#endif +#if CONFIG_NT99141_SUPPORT +#include "nt99141.h" +#endif +#if CONFIG_OV7670_SUPPORT +#include "ov7670.h" +#endif + + +#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG) +#include "esp32-hal-log.h" +#define TAG "" +#else +#include "esp_log.h" +static const char *TAG = "camera"; +#endif + +typedef struct { + sensor_t sensor; + camera_fb_t fb; +} camera_state_t; + +camera_state_t *s_state = NULL; + +#if CONFIG_IDF_TARGET_ESP32S3 // LCD_CAM module of ESP32-S3 will generate xclk +#define CAMERA_ENABLE_OUT_CLOCK(v) +#define CAMERA_DISABLE_OUT_CLOCK() +#else +#define CAMERA_ENABLE_OUT_CLOCK(v) camera_enable_out_clock((v)) +#define CAMERA_DISABLE_OUT_CLOCK() camera_disable_out_clock() +#endif + +static esp_err_t camera_probe(const camera_config_t *config, camera_model_t *out_camera_model) +{ + *out_camera_model = CAMERA_NONE; + if (s_state != NULL) { + return ESP_ERR_INVALID_STATE; + } + + s_state = (camera_state_t *) calloc(sizeof(*s_state), 1); + if (!s_state) { + return ESP_ERR_NO_MEM; + } + + if (config->pin_xclk >= 0) { + ESP_LOGD(TAG, "Enabling XCLK output"); + CAMERA_ENABLE_OUT_CLOCK(config); + } + + if (config->pin_sscb_sda != -1) { + ESP_LOGD(TAG, "Initializing SSCB"); + SCCB_Init(config->pin_sscb_sda, config->pin_sscb_scl); + } + + if (config->pin_pwdn >= 0) { + ESP_LOGD(TAG, "Resetting camera by power down line"); + gpio_config_t conf = { 0 }; + conf.pin_bit_mask = 1LL << config->pin_pwdn; + conf.mode = GPIO_MODE_OUTPUT; + gpio_config(&conf); + + // carefull, logic is inverted compared to reset pin + gpio_set_level(config->pin_pwdn, 1); + vTaskDelay(10 / portTICK_PERIOD_MS); + gpio_set_level(config->pin_pwdn, 0); + vTaskDelay(10 / portTICK_PERIOD_MS); + } + + if (config->pin_reset >= 0) { + ESP_LOGD(TAG, "Resetting camera"); + gpio_config_t conf = { 0 }; + conf.pin_bit_mask = 1LL << config->pin_reset; + conf.mode = GPIO_MODE_OUTPUT; + gpio_config(&conf); + + gpio_set_level(config->pin_reset, 0); + vTaskDelay(10 / portTICK_PERIOD_MS); + gpio_set_level(config->pin_reset, 1); + vTaskDelay(10 / portTICK_PERIOD_MS); + } + + + ESP_LOGD(TAG, "Searching for camera address"); + vTaskDelay(10 / portTICK_PERIOD_MS); + + uint8_t slv_addr = SCCB_Probe(); + + if (slv_addr == 0) { + CAMERA_DISABLE_OUT_CLOCK(); + return ESP_ERR_CAMERA_NOT_DETECTED; + } + + ESP_LOGI(TAG, "Detected camera at address=0x%02x", slv_addr); + s_state->sensor.slv_addr = slv_addr; + s_state->sensor.xclk_freq_hz = config->xclk_freq_hz; + + /** + * Read sensor ID + */ + sensor_id_t *id = &s_state->sensor.id; + + if (slv_addr == OV2640_SCCB_ADDR) { + SCCB_Write(slv_addr, 0xFF, 0x01);//bank sensor + id->PID = SCCB_Read(slv_addr, REG_PID); + id->VER = SCCB_Read(slv_addr, REG_VER); + id->MIDL = SCCB_Read(slv_addr, REG_MIDL); + id->MIDH = SCCB_Read(slv_addr, REG_MIDH); + } else if (slv_addr == OV5640_SCCB_ADDR || slv_addr == OV3660_SCCB_ADDR) { + id->PID = SCCB_Read16(slv_addr, REG16_CHIDH); + id->VER = SCCB_Read16(slv_addr, REG16_CHIDL); + } else if (slv_addr == NT99141_SCCB_ADDR) { + SCCB_Write16(slv_addr, 0x3008, 0x01);//bank sensor + id->PID = SCCB_Read16(slv_addr, 0x3000); + id->VER = SCCB_Read16(slv_addr, 0x3001); + if (config->xclk_freq_hz > 10000000) { + ESP_LOGE(TAG, "NT99141: only XCLK under 10MHz is supported, and XCLK is now set to 10M"); + s_state->sensor.xclk_freq_hz = 10000000; + } + } + vTaskDelay(10 / portTICK_PERIOD_MS); + ESP_LOGI(TAG, "Camera PID=0x%02x VER=0x%02x MIDL=0x%02x MIDH=0x%02x", + id->PID, id->VER, id->MIDH, id->MIDL); + + /** + * Initialize sensor according to sensor ID + */ + switch (id->PID) { +#if CONFIG_OV2640_SUPPORT + case OV2640_PID: + *out_camera_model = CAMERA_OV2640; + ov2640_init(&s_state->sensor); + break; +#endif +#if CONFIG_OV7725_SUPPORT + case OV7725_PID: + *out_camera_model = CAMERA_OV7725; + ov7725_init(&s_state->sensor); + break; +#endif +#if CONFIG_OV3660_SUPPORT + case OV3660_PID: + *out_camera_model = CAMERA_OV3660; + ov3660_init(&s_state->sensor); + break; +#endif +#if CONFIG_OV5640_SUPPORT + case OV5640_PID: + *out_camera_model = CAMERA_OV5640; + ov5640_init(&s_state->sensor); + break; +#endif +#if CONFIG_OV7670_SUPPORT + case OV7670_PID: + *out_camera_model = CAMERA_OV7670; + ov7670_init(&s_state->sensor); + break; +#endif +#if CONFIG_NT99141_SUPPORT + case NT99141_PID: + *out_camera_model = CAMERA_NT99141; + NT99141_init(&s_state->sensor); + break; +#endif + default: + id->PID = 0; + CAMERA_DISABLE_OUT_CLOCK(); + ESP_LOGE(TAG, "Detected camera not supported."); + return ESP_ERR_CAMERA_NOT_SUPPORTED; + } + + ESP_LOGD(TAG, "Doing SW reset of sensor"); + s_state->sensor.reset(&s_state->sensor); + + return ESP_OK; +} + + +esp_err_t esp_camera_init(const camera_config_t *config) +{ + esp_err_t err; + err = cam_init(config); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Camera init failed with error 0x%x", err); + return err; + } + + camera_model_t camera_model = CAMERA_NONE; + err = camera_probe(config, &camera_model); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Camera probe failed with error 0x%x", err); + goto fail; + } + if (camera_model == CAMERA_OV7725) { + ESP_LOGI(TAG, "Detected OV7725 camera"); + if (config->pixel_format == PIXFORMAT_JPEG) { + ESP_LOGE(TAG, "Camera does not support JPEG"); + err = ESP_ERR_CAMERA_NOT_SUPPORTED; + goto fail; + } + } else if (camera_model == CAMERA_OV2640) { + ESP_LOGI(TAG, "Detected OV2640 camera"); + } else if (camera_model == CAMERA_OV3660) { + ESP_LOGI(TAG, "Detected OV3660 camera"); + } else if (camera_model == CAMERA_OV5640) { + ESP_LOGI(TAG, "Detected OV5640 camera"); + } else if (camera_model == CAMERA_OV7670) { + ESP_LOGI(TAG, "Detected OV7670 camera"); + } else if (camera_model == CAMERA_NT99141) { + ESP_LOGI(TAG, "Detected NT99141 camera"); + } else { + ESP_LOGI(TAG, "Camera not supported"); + err = ESP_ERR_CAMERA_NOT_SUPPORTED; + goto fail; + } + + framesize_t frame_size = (framesize_t) config->frame_size; + pixformat_t pix_format = (pixformat_t) config->pixel_format; + s_state->sensor.status.framesize = frame_size; + s_state->sensor.pixformat = pix_format; + // ESP_LOGD(TAG, "Setting frame size to %dx%d", s_state->width, s_state->height); + if (s_state->sensor.set_framesize(&s_state->sensor, frame_size) != 0) { + ESP_LOGE(TAG, "Failed to set frame size"); + err = ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE; + goto fail; + } + s_state->sensor.set_pixformat(&s_state->sensor, pix_format); + + if (s_state->sensor.id.PID == OV2640_PID) { + s_state->sensor.set_gainceiling(&s_state->sensor, GAINCEILING_2X); + s_state->sensor.set_bpc(&s_state->sensor, false); + s_state->sensor.set_wpc(&s_state->sensor, true); + s_state->sensor.set_lenc(&s_state->sensor, true); + } + + if (pix_format == PIXFORMAT_JPEG) { + (*s_state->sensor.set_quality)(&s_state->sensor, config->jpeg_quality); + } + s_state->sensor.init_status(&s_state->sensor); + + cam_start(); + + return ESP_OK; + +fail: + free(s_state); + s_state = NULL; + CAMERA_DISABLE_OUT_CLOCK(); + return err; +} + +camera_fb_t *esp_camera_fb_get() +{ + if (s_state == NULL) { + return NULL; + } + camera_fb_t *fb = cam_take(); + //set the frame properties + fb->width = resolution[s_state->sensor.status.framesize].width; + fb->height = resolution[s_state->sensor.status.framesize].height; + fb->format = s_state->sensor.pixformat; + return fb; +} + +void esp_camera_fb_return(camera_fb_t *fb) +{ + if (s_state == NULL) { + return; + } + cam_give(fb); +} + +sensor_t *esp_camera_sensor_get() +{ + if (s_state == NULL) { + return NULL; + } + return &s_state->sensor; +} diff --git a/driver/include/sensor.h b/driver/include/sensor.h index ad6cd89..acc29e8 100755 --- a/driver/include/sensor.h +++ b/driver/include/sensor.h @@ -11,13 +11,50 @@ #include #include -#define NT99141_PID (0x14) -#define OV9650_PID (0x96) -#define OV7725_PID (0x77) -#define OV2640_PID (0x26) -#define OV3660_PID (0x36) -#define OV5640_PID (0x56) -#define OV7670_PID (0x76) +// Chip ID Registers +#define REG_PID 0x0A +#define REG_VER 0x0B +#define REG_MIDH 0x1C +#define REG_MIDL 0x1D + +#define REG16_CHIDH 0x300A +#define REG16_CHIDL 0x300B + +typedef enum { + OV9650_PID = 0x96, + OV7725_PID = 0x77, + OV2640_PID = 0x26, + OV3660_PID = 0x36, + OV5640_PID = 0x56, + OV7670_PID = 0x76, + NT99141_PID = 0x14 +} camera_pid_t; + +typedef enum { + CAMERA_OV7725, + CAMERA_OV2640, + CAMERA_OV3660, + CAMERA_OV5640, + CAMERA_OV7670, + CAMERA_NT99141, + CAMERA_MODEL_MAX, + CAMERA_NONE, +} camera_model_t; + +typedef enum { + OV2640_SCCB_ADDR = 0x30, + OV5640_SCCB_ADDR = 0x3C, + OV3660_SCCB_ADDR = 0x3C, + OV7725_SCCB_ADDR = 0x21, + OV7670_SCCB_ADDR = 0x21, + NT99141_SCCB_ADDR = 0x2A, +} camera_sccb_addr_t; + +typedef struct { + const camera_model_t model; + const camera_sccb_addr_t sccb_addr; + const camera_pid_t pid; +} camera_sensor_info_t; typedef enum { PIXFORMAT_RGB565, // 2BPP/RGB565 @@ -101,6 +138,8 @@ typedef struct { // Resolution table (in sensor.c) extern const resolution_info_t resolution[]; +// camera sensor table (in sensor.c) +extern const camera_sensor_info_t camera_sensor[]; typedef struct { uint8_t MIDH; diff --git a/driver/private_include/cam_hal.h b/driver/private_include/cam_hal.h new file mode 100644 index 0000000..96d6b7d --- /dev/null +++ b/driver/private_include/cam_hal.h @@ -0,0 +1,58 @@ +// Copyright 2010-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "esp_camera.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Uninitialize the lcd_cam module + * + * @param handle Provide handle pointer to release resources + * + * @return + * - ESP_OK Success + * - ESP_FAIL Uninitialize fail + */ +esp_err_t cam_deinit(void); + +/** + * @brief Initialize the lcd_cam module + * + * @param config Configurations - see lcd_cam_config_t struct + * + * @return + * - ESP_OK Success + * - ESP_ERR_INVALID_ARG Parameter error + * - ESP_ERR_NO_MEM No memory to initialize lcd_cam + * - ESP_FAIL Initialize fail + */ +esp_err_t cam_init(const camera_config_t *config); + +void cam_stop(void); + +void cam_start(void); + +camera_fb_t *cam_take(void); + +void cam_give(camera_fb_t *dma_buffer); + +#ifdef __cplusplus +} +#endif diff --git a/driver/private_include/camera_common.h b/driver/private_include/camera_common.h index 8a2db5c..205e5fa 100755 --- a/driver/private_include/camera_common.h +++ b/driver/private_include/camera_common.h @@ -15,6 +15,8 @@ #if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+ #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "esp32/rom/lldesc.h" +#elif CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2 +#include "esp32s2/rom/lldesc.h" #else #error Target CONFIG_IDF_TARGET is not supported #endif diff --git a/driver/sccb.c b/driver/sccb.c index cb615bb..7398c02 100755 --- a/driver/sccb.c +++ b/driver/sccb.c @@ -11,6 +11,7 @@ #include #include #include "sccb.h" +#include "sensor.h" #include #include "sdkconfig.h" #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG) @@ -36,12 +37,10 @@ const int SCCB_I2C_PORT = 1; #else const int SCCB_I2C_PORT = 0; #endif -static uint8_t ESP_SLAVE_ADDR = 0x3c; int SCCB_Init(int pin_sda, int pin_scl) { ESP_LOGI(TAG, "pin_sda %d pin_scl %d\n", pin_sda, pin_scl); - //log_i("SCCB_Init start"); i2c_config_t conf; memset(&conf, 0, sizeof(i2c_config_t)); conf.mode = I2C_MODE_MASTER; @@ -59,7 +58,11 @@ int SCCB_Init(int pin_sda, int pin_scl) uint8_t SCCB_Probe() { uint8_t slave_addr = 0x0; - while(slave_addr < 0x7f) { + for (size_t i = 0; i < CAMERA_MODEL_MAX; i++) { + if (slave_addr == camera_sensor[i].sccb_addr) { + continue; + } + slave_addr = camera_sensor[i].sccb_addr; i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); i2c_master_write_byte(cmd, ( slave_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN); @@ -67,12 +70,10 @@ uint8_t SCCB_Probe() esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); if( ret == ESP_OK) { - ESP_SLAVE_ADDR = slave_addr; - return ESP_SLAVE_ADDR; + return slave_addr; } - slave_addr++; } - return ESP_SLAVE_ADDR; + return 0; } uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg) diff --git a/driver/sensor.c b/driver/sensor.c index 2e6d111..942106a 100644 --- a/driver/sensor.c +++ b/driver/sensor.c @@ -1,5 +1,14 @@ #include "sensor.h" +const camera_sensor_info_t camera_sensor[CAMERA_MODEL_MAX] = { + {CAMERA_OV7725, OV7725_SCCB_ADDR, OV7725_PID}, + {CAMERA_OV2640, OV2640_SCCB_ADDR, OV2640_PID}, + {CAMERA_OV3660, OV3660_SCCB_ADDR, OV3660_PID}, + {CAMERA_OV5640, OV5640_SCCB_ADDR, OV5640_PID}, + {CAMERA_OV7670, OV7670_SCCB_ADDR, OV7670_PID}, + {CAMERA_NT99141, NT99141_SCCB_ADDR, NT99141_PID}, +}; + const resolution_info_t resolution[FRAMESIZE_INVALID] = { { 96, 96, ASPECT_RATIO_1X1 }, /* 96x96 */ { 160, 120, ASPECT_RATIO_4X3 }, /* QQVGA */ diff --git a/driver/xclk.c b/driver/xclk.c index f58747d..872067b 100755 --- a/driver/xclk.c +++ b/driver/xclk.c @@ -15,9 +15,13 @@ static const char* TAG = "camera_xclk"; esp_err_t xclk_timer_conf(int ledc_timer, int xclk_freq_hz) { ledc_timer_config_t timer_conf; - timer_conf.duty_resolution = 2; + timer_conf.duty_resolution = LEDC_TIMER_1_BIT; timer_conf.freq_hz = xclk_freq_hz; +#if CONFIG_IDF_TARGET_ESP32 timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE; +#else + timer_conf.speed_mode = LEDC_LOW_SPEED_MODE; +#endif #if ESP_IDF_VERSION_MAJOR >= 4 timer_conf.clk_cfg = LEDC_AUTO_CLK; #endif @@ -41,11 +45,15 @@ esp_err_t camera_enable_out_clock(camera_config_t* config) ledc_channel_config_t ch_conf; ch_conf.gpio_num = config->pin_xclk; +#if CONFIG_IDF_TARGET_ESP32 ch_conf.speed_mode = LEDC_HIGH_SPEED_MODE; +#else + ch_conf.speed_mode = LEDC_LOW_SPEED_MODE; +#endif ch_conf.channel = config->ledc_channel; ch_conf.intr_type = LEDC_INTR_DISABLE; ch_conf.timer_sel = config->ledc_timer; - ch_conf.duty = 2; + ch_conf.duty = 1; ch_conf.hpoint = 0; err = ledc_channel_config(&ch_conf); if (err != ESP_OK) { diff --git a/idf_component.yml b/idf_component.yml deleted file mode 100644 index d8dc8e1..0000000 --- a/idf_component.yml +++ /dev/null @@ -1,3 +0,0 @@ -version: "1.0.0" -description: This package hosts ESP32 compatible driver for OV2640 image sensors. Additionally it provides a few tools, which allow converting the captured frame data to the more common BMP and JPEG formats. -url: https://github.com/espressif/esp32-camera diff --git a/sensors/ov3660.c b/sensors/ov3660.c index 723ec5c..2de3e8c 100755 --- a/sensors/ov3660.c +++ b/sensors/ov3660.c @@ -310,13 +310,13 @@ static int set_image_options(sensor_t *sensor) static int set_framesize(sensor_t *sensor, framesize_t framesize) { int ret = 0; - framesize_t old_framesize = sensor->status.framesize; - sensor->status.framesize = framesize; if(framesize > FRAMESIZE_QXGA){ - ESP_LOGE(TAG, "Invalid framesize: %u", framesize); - return -1; + ESP_LOGW(TAG, "Invalid framesize: %u", framesize); + framesize = FRAMESIZE_QXGA; } + framesize_t old_framesize = sensor->status.framesize; + sensor->status.framesize = framesize; uint16_t w = resolution[framesize].width; uint16_t h = resolution[framesize].height; aspect_ratio_t ratio = resolution[sensor->status.framesize].aspect_ratio; diff --git a/target/esp32s2/cam_hal.c b/target/esp32s2/cam_hal.c new file mode 100644 index 0000000..5da01e5 --- /dev/null +++ b/target/esp32s2/cam_hal.c @@ -0,0 +1,701 @@ +// Copyright 2010-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "esp_heap_caps.h" +#include "esp32s2/rom/lldesc.h" +#include "soc/system_reg.h" +#include "soc/i2s_struct.h" +#include "soc/spi_struct.h" +#include "xclk.h" +#include "cam_hal.h" +#include "hal/gpio_ll.h" +#include "esp_log.h" + +static const char *TAG = "lcd_cam_s2"; + +#define CAM_CHECK(a, str, ret) if (!(a)) { \ + ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \ + return (ret); \ + } + +#define CAM_CHECK_GOTO(a, str, lab) if (!(a)) { \ + ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \ + goto lab; \ + } + +#define LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE (4092) + +typedef enum { + CAM_IN_SUC_EOF_EVENT = 0, + CAM_VSYNC_EVENT +} cam_event_t; + +typedef enum { + CAM_STATE_IDLE = 0, + CAM_STATE_READ_BUF = 1, +} cam_state_t; + +typedef struct { + camera_fb_t fb; + uint8_t en; + //for RGB/YUV modes + lldesc_t *dma; + size_t fb_offset; +} cam_frame_t; + +typedef struct { + uint32_t dma_buffer_size; + uint32_t dma_half_buffer_size; + uint32_t dma_half_buffer_cnt; + uint32_t dma_node_buffer_size; + uint32_t dma_node_cnt; + uint32_t frame_copy_cnt; + + //for JPEG mode + lldesc_t *dma; + uint8_t *dma_buffer; + + cam_frame_t *frames; + + QueueHandle_t event_queue; + QueueHandle_t frame_buffer_queue; + TaskHandle_t task_handle; + intr_handle_t cam_intr_handle; + + uint8_t jpeg_mode; + uint8_t vsync_pin; + uint8_t vsync_invert; + uint32_t frame_cnt; + uint32_t recv_size; + bool swap_data; + + //for RGB/YUV modes + uint16_t width; + uint16_t height; + + cam_state_t state; +} cam_obj_t; + +static cam_obj_t *cam_obj = NULL; + +#define I2S_ISR_ENABLE(i) {I2S0.int_clr.i = 1;I2S0.int_ena.i = 1;} +#define I2S_ISR_DISABLE(i) {I2S0.int_ena.i = 0;I2S0.int_clr.i = 1;} + +static void IRAM_ATTR ll_cam_vsync_isr(void *arg) +{ + cam_event_t cam_event = CAM_VSYNC_EVENT; + BaseType_t HPTaskAwoken = pdFALSE; + // filter + esp_rom_delay_us(1); + if (gpio_ll_get_level(&GPIO, cam_obj->vsync_pin) == !cam_obj->vsync_invert) { + xQueueSendFromISR(cam_obj->event_queue, (void *)&cam_event, &HPTaskAwoken); + } + + if (HPTaskAwoken == pdTRUE) { + portYIELD_FROM_ISR(); + } +} + +static void IRAM_ATTR ll_cam_i2s_isr(void *arg) +{ + cam_event_t cam_event = CAM_IN_SUC_EOF_EVENT; + BaseType_t HPTaskAwoken = pdFALSE; + typeof(I2S0.int_st) status = I2S0.int_st; + I2S0.int_clr.val = status.val; + if (status.val == 0) { + return; + } + + if (status.in_suc_eof) { + xQueueSendFromISR(cam_obj->event_queue, (void *)&cam_event, &HPTaskAwoken); + //ets_printf("s"); + } + + //if (status.in_done) ets_printf("d"); + //if (status.in_err_eof) ets_printf("E"); + //if (status.in_dscr_err) ets_printf("D"); + //if (status.in_dscr_empty) ets_printf("e"); + //if (status.v_sync) ets_printf("v"); + + if (HPTaskAwoken == pdTRUE) { + portYIELD_FROM_ISR(); + } +} + +static bool ll_cam_stop(void) +{ + if (!cam_obj->jpeg_mode || I2S0.int_ena.in_suc_eof == 1) { + I2S0.conf.rx_start = 0; + + if (cam_obj->jpeg_mode) { + I2S_ISR_DISABLE(in_suc_eof); + } + //I2S_ISR_DISABLE(v_sync); + //I2S_ISR_DISABLE(in_done); + //I2S_ISR_DISABLE(in_err_eof); + //I2S_ISR_DISABLE(in_dscr_err); + //I2S_ISR_DISABLE(in_dscr_empty); + + I2S0.in_link.stop = 1; + return true; + } + return false; +} + +static bool ll_cam_start(int frame_pos) +{ + if (!cam_obj->jpeg_mode || I2S0.int_ena.in_suc_eof == 0) { + I2S0.conf.rx_start = 0; + + if (cam_obj->jpeg_mode) { + I2S_ISR_ENABLE(in_suc_eof); + } + //I2S_ISR_ENABLE(v_sync); + //I2S_ISR_ENABLE(in_done); + //I2S_ISR_ENABLE(in_err_eof); + //I2S_ISR_ENABLE(in_dscr_err); + //I2S_ISR_ENABLE(in_dscr_empty); + + I2S0.conf.rx_reset = 1; + I2S0.conf.rx_reset = 0; + I2S0.conf.rx_fifo_reset = 1; + I2S0.conf.rx_fifo_reset = 0; + I2S0.lc_conf.in_rst = 1; + I2S0.lc_conf.in_rst = 0; + I2S0.lc_conf.ahbm_fifo_rst = 1; + I2S0.lc_conf.ahbm_fifo_rst = 0; + I2S0.lc_conf.ahbm_rst = 1; + I2S0.lc_conf.ahbm_rst = 0; + + I2S0.rx_eof_num = cam_obj->dma_half_buffer_size; // Ping pong operation + if (cam_obj->jpeg_mode) { + I2S0.in_link.addr = ((uint32_t)&cam_obj->dma[0]) & 0xfffff; + } else { + I2S0.in_link.addr = ((uint32_t)&cam_obj->frames[frame_pos].dma[0]) & 0xfffff; + } + + I2S0.in_link.start = 1; + I2S0.conf.rx_start = 1; + return true; + } + return false; +} + +static esp_err_t ll_cam_config() +{ + // Configure the clock + I2S0.clkm_conf.clkm_div_num = 2; // 160MHz / 2 = 80MHz + I2S0.clkm_conf.clkm_div_b = 0; + I2S0.clkm_conf.clkm_div_a = 0; + I2S0.clkm_conf.clk_sel = 2; + I2S0.clkm_conf.clk_en = 1; + + + I2S0.conf.val = 0; + I2S0.fifo_conf.val = 0; + I2S0.fifo_conf.dscr_en = 1; + + I2S0.lc_conf.ahbm_fifo_rst = 1; + I2S0.lc_conf.ahbm_fifo_rst = 0; + I2S0.lc_conf.ahbm_rst = 1; + I2S0.lc_conf.ahbm_rst = 0; + I2S0.lc_conf.check_owner = 0; + //I2S0.lc_conf.indscr_burst_en = 1; + //I2S0.lc_conf.ext_mem_bk_size = 0; // DMA access external memory block size. 0: 16 bytes, 1: 32 bytes, 2:64 bytes, 3:reserved + + I2S0.timing.val = 0; + + I2S0.int_ena.val = 0; + I2S0.int_clr.val = ~0; + + I2S0.conf2.lcd_en = 1; + I2S0.conf2.camera_en = 1; + + // Configuration data format + I2S0.conf.rx_slave_mod = 1; + I2S0.conf.rx_right_first = 0; + I2S0.conf.rx_msb_right = cam_obj->swap_data; + I2S0.conf.rx_short_sync = 0; + I2S0.conf.rx_mono = 0; + I2S0.conf.rx_msb_shift = 0; + I2S0.conf.rx_dma_equal = 1; + + // Configure sampling rate + I2S0.sample_rate_conf.rx_bck_div_num = 1; + I2S0.sample_rate_conf.rx_bits_mod = 8; + + I2S0.conf1.rx_pcm_bypass = 1; + + I2S0.conf2.i_v_sync_filter_en = 1; + I2S0.conf2.i_v_sync_filter_thres = 4; + I2S0.conf2.cam_sync_fifo_reset = 1; + I2S0.conf2.cam_sync_fifo_reset = 0; + + I2S0.conf_chan.rx_chan_mod = 1; + + I2S0.fifo_conf.rx_fifo_mod_force_en = 1; + I2S0.fifo_conf.rx_data_num = 32; + I2S0.fifo_conf.rx_fifo_mod = 2; + + I2S0.lc_conf.in_rst = 1; + I2S0.lc_conf.in_rst = 0; + + I2S0.conf.rx_start = 1; + + return ESP_OK; +} + +static void ll_cam_vsync_intr_enable(bool en) +{ + if (en) { + gpio_intr_enable(cam_obj->vsync_pin); + } else { + gpio_intr_disable(cam_obj->vsync_pin); + } +} + +static const uint16_t JPEG_SOI_MARKER = 0xD8FF; // written in little-endian for esp32 +static const uint16_t JPEG_EOI_MARKER = 0xD9FF; // written in little-endian for esp32 + +static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length) +{ + for (size_t i = 0; i < length; i++) { + uint16_t sig = *((uint16_t *)inbuf); + if (JPEG_SOI_MARKER == sig) { + return i; + } + inbuf++; + } + return -1; +} + +static int cam_verify_jpeg_eoi(const uint8_t *inbuf, uint32_t length) +{ + int offset = -1; + uint8_t *dptr = (uint8_t *)inbuf + length - 2; + while (dptr > inbuf) { + uint16_t sig = *((uint16_t *)dptr); + if (JPEG_EOI_MARKER == sig) { + offset = dptr - inbuf; + return offset; + } + dptr--; + } + return -1; +} + +static bool cam_get_next_frame(int * frame_pos) +{ + if(!cam_obj->frames[*frame_pos].en){ + for (int x = 0; x < cam_obj->frame_cnt; x++) { + if (cam_obj->frames[x].en) { + *frame_pos = x; + return true; + } + } + } else { + return true; + } + return false; +} + +static bool cam_start_frame(int * frame_pos) +{ + if (cam_get_next_frame(frame_pos)) { + if(ll_cam_start(*frame_pos)){ + // Vsync the frame manually + ll_cam_vsync_intr_enable(false); + gpio_matrix_in(cam_obj->vsync_pin, I2S0I_V_SYNC_IDX, !cam_obj->vsync_invert); + esp_rom_delay_us(10); + gpio_matrix_in(cam_obj->vsync_pin, I2S0I_V_SYNC_IDX, cam_obj->vsync_invert); + ll_cam_vsync_intr_enable(true); + return true; + } + } + return false; +} + +//Copy fram from DMA dma_buffer to fram dma_buffer +static void cam_task(void *arg) +{ + int cnt = 0; + int frame_pos = 0; + cam_obj->state = CAM_STATE_IDLE; + cam_event_t cam_event = 0; + + xQueueReset(cam_obj->event_queue); + + while (1) { + xQueueReceive(cam_obj->event_queue, (void *)&cam_event, portMAX_DELAY); + switch (cam_obj->state) { + + case CAM_STATE_IDLE: { + if (cam_event == CAM_VSYNC_EVENT) { + if(cam_start_frame(&frame_pos)){ + cam_obj->state = CAM_STATE_READ_BUF; + } + cnt = 0; + } + } + break; + + case CAM_STATE_READ_BUF: { + if (cam_event == CAM_IN_SUC_EOF_EVENT) { + if(cam_obj->jpeg_mode){ + memcpy(&cam_obj->frames[frame_pos].fb.buf[cnt * cam_obj->dma_half_buffer_size], &cam_obj->dma_buffer[(cnt % cam_obj->dma_half_buffer_cnt) * cam_obj->dma_half_buffer_size], cam_obj->dma_half_buffer_size); + } + cnt++; + + } else if (cam_event == CAM_VSYNC_EVENT) { + ll_cam_stop(); + + int offset_e = -1; + if (cam_obj->jpeg_mode) { + // check for the end marker for JPEG in the last buffer + offset_e = cam_verify_jpeg_eoi(&cam_obj->frames[frame_pos].fb.buf[(cnt - 1) * cam_obj->dma_half_buffer_size], cam_obj->dma_half_buffer_size); + if (offset_e < 0) { + memcpy(&cam_obj->frames[frame_pos].fb.buf[cnt * cam_obj->dma_half_buffer_size], &cam_obj->dma_buffer[(cnt % cam_obj->dma_half_buffer_cnt) * cam_obj->dma_half_buffer_size], cam_obj->dma_half_buffer_size); + cnt++; + } else { + offset_e += ((cnt - 1) * cam_obj->dma_half_buffer_size); + } + } + + + camera_fb_t * frame_buffer_event = &cam_obj->frames[frame_pos].fb; + cam_obj->frames[frame_pos].en = 0; + + if (cam_obj->jpeg_mode) { + frame_buffer_event->len = cnt * cam_obj->dma_half_buffer_size; + // find the start marker for JPEG. + int offset_s = cam_verify_jpeg_soi(frame_buffer_event->buf, 1); + if (offset_s == 0) { + // find the end marker for JPEG. Data after that can be discarded + if (offset_e < 0) { + offset_e = cam_verify_jpeg_eoi(frame_buffer_event->buf, frame_buffer_event->len); + } + if (offset_e >= 0) { + // adjust buffer length + frame_buffer_event->len = offset_e + sizeof(JPEG_EOI_MARKER); + //send frame + if (xQueueSend(cam_obj->frame_buffer_queue, (void *)&frame_buffer_event, 0) != pdTRUE) { + cam_obj->frames[frame_pos].en = 1; + ESP_LOGE(TAG, "fbqueue_send_fail\n"); + } + } else { + cam_obj->frames[frame_pos].en = 1; + //ets_printf("no eoi: %u\n", frame_buffer_event->len); + } + } else { + cam_obj->frames[frame_pos].en = 1; + //ets_printf("no soi\n"); + } + } else { + frame_buffer_event->len = cam_obj->recv_size; + //send frame + if (xQueueSend(cam_obj->frame_buffer_queue, (void *)&frame_buffer_event, 0) != pdTRUE) { + cam_obj->frames[frame_pos].en = 1; + ESP_LOGE(TAG, "fbqueue_send_fail\n"); + } + } + + if(!cam_start_frame(&frame_pos)){ + cam_obj->state = CAM_STATE_IDLE; + } + cnt = 0; + } + } + break; + } + } +} + +static lldesc_t * allocate_dma_descriptors(uint32_t count, uint16_t size, uint8_t * buffer) +{ + lldesc_t *dma = (lldesc_t *)heap_caps_malloc(count * sizeof(lldesc_t), MALLOC_CAP_DMA); + if (dma == NULL) { + return dma; + } + + for (int x = 0; x < count; x++) { + dma[x].size = size; + dma[x].length = 0; + dma[x].eof = 0; + dma[x].owner = 1; + dma[x].buf = (buffer + size * x); + dma[x].empty = (uint32_t)&dma[(x + 1) % count]; + } + return dma; +} + +static esp_err_t cam_dma_config() +{ + int cnt = 0; + + if (cam_obj->jpeg_mode) { + cam_obj->dma_half_buffer_cnt = 16; + cam_obj->dma_buffer_size = cam_obj->dma_half_buffer_cnt * 1024; + cam_obj->dma_half_buffer_size = cam_obj->dma_buffer_size / cam_obj->dma_half_buffer_cnt; + cam_obj->dma_node_buffer_size = cam_obj->dma_half_buffer_size; + } else { + for (cnt = 0; cnt < cam_obj->recv_size; cnt++) { // Find a buffer size that can be divisible by + if (cam_obj->recv_size % (cam_obj->recv_size - cnt) == 0) { + break; + } + } + cam_obj->dma_buffer_size = cam_obj->recv_size - cnt; + cam_obj->dma_half_buffer_cnt = 2; + cam_obj->dma_half_buffer_size = cam_obj->dma_buffer_size / cam_obj->dma_half_buffer_cnt; + + for (cnt = 0; cnt < LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE; cnt++) { // Find a divisible dma size + if ((cam_obj->dma_half_buffer_size) % (LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE - cnt) == 0) { + break; + } + } + cam_obj->dma_node_buffer_size = LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE - cnt; + } + + cam_obj->dma_node_cnt = (cam_obj->dma_buffer_size) / cam_obj->dma_node_buffer_size; // Number of DMA nodes + cam_obj->frame_copy_cnt = cam_obj->recv_size / cam_obj->dma_half_buffer_size; // Number of interrupted copies, ping-pong copy + + ESP_LOGI(TAG, "buffer_size: %d, half_buffer_size: %d, node_buffer_size: %d, node_cnt: %d, total_cnt: %d\n", cam_obj->dma_buffer_size, cam_obj->dma_half_buffer_size, cam_obj->dma_node_buffer_size, cam_obj->dma_node_cnt, cam_obj->frame_copy_cnt); + + cam_obj->dma_buffer = NULL; + cam_obj->dma = NULL; + + cam_obj->frames = (cam_frame_t *)heap_caps_malloc(cam_obj->frame_cnt * sizeof(cam_frame_t), MALLOC_CAP_DEFAULT); + CAM_CHECK(cam_obj->frames != NULL, "frames malloc failed", ESP_FAIL); + + uint8_t dma_align = 0; + if (!cam_obj->jpeg_mode) { + dma_align = 16 << I2S0.lc_conf.ext_mem_bk_size; + } + for (int x = 0; x < cam_obj->frame_cnt; x++) { + cam_obj->frames[x].dma = NULL; + cam_obj->frames[x].fb_offset = 0; + cam_obj->frames[x].en = 0; + cam_obj->frames[x].fb.buf = (uint8_t *)heap_caps_malloc(cam_obj->recv_size * sizeof(uint8_t) + dma_align, MALLOC_CAP_SPIRAM); + CAM_CHECK(cam_obj->frames[x].fb.buf != NULL, "frame buffer malloc failed", ESP_FAIL); + if (!cam_obj->jpeg_mode) { + //align PSRAM buffer. TODO: save the offset so proper address can be freed later + cam_obj->frames[x].fb_offset = dma_align - ((uint32_t)cam_obj->frames[x].fb.buf & (dma_align - 1)); + cam_obj->frames[x].fb.buf += cam_obj->frames[x].fb_offset; + ESP_LOGI(TAG, "Frame[%d]: Offset: %u, Addr: 0x%08X", x, cam_obj->frames[x].fb_offset, (uint32_t)cam_obj->frames[x].fb.buf); + cam_obj->frames[x].dma = allocate_dma_descriptors(cam_obj->dma_node_cnt, cam_obj->dma_node_buffer_size, cam_obj->frames[x].fb.buf); + CAM_CHECK(cam_obj->frames[x].dma != NULL, "frame dma malloc failed", ESP_FAIL); + } + cam_obj->frames[x].en = 1; + } + + if (cam_obj->jpeg_mode) { + cam_obj->dma_buffer = (uint8_t *)heap_caps_malloc(cam_obj->dma_buffer_size * sizeof(uint8_t), MALLOC_CAP_DMA); + CAM_CHECK(cam_obj->dma_buffer != NULL, "dma_buffer malloc failed", ESP_FAIL); + + cam_obj->dma = allocate_dma_descriptors(cam_obj->dma_node_cnt, cam_obj->dma_node_buffer_size, cam_obj->dma_buffer); + CAM_CHECK(cam_obj->dma != NULL, "dma malloc failed", ESP_FAIL); + } + + return ESP_OK; +} + + + + + + + + + + + + + + + + + + + +static esp_err_t cam_set_pin(const camera_config_t *config) +{ + gpio_config_t io_conf = {0}; + io_conf.intr_type = cam_obj->vsync_invert ? GPIO_PIN_INTR_NEGEDGE : GPIO_PIN_INTR_POSEDGE; + io_conf.pin_bit_mask = 1ULL << config->pin_vsync; + io_conf.mode = GPIO_MODE_INPUT; + io_conf.pull_up_en = 1; + io_conf.pull_down_en = 0; + gpio_config(&io_conf); + gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM); + gpio_isr_handler_add(config->pin_vsync, ll_cam_vsync_isr, NULL); + gpio_intr_disable(config->pin_vsync); + + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_pclk], PIN_FUNC_GPIO); + gpio_set_direction(config->pin_pclk, GPIO_MODE_INPUT); + gpio_set_pull_mode(config->pin_pclk, GPIO_FLOATING); + gpio_matrix_in(config->pin_pclk, I2S0I_WS_IN_IDX, false); + + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_vsync], PIN_FUNC_GPIO); + gpio_set_direction(config->pin_vsync, GPIO_MODE_INPUT); + gpio_set_pull_mode(config->pin_vsync, GPIO_FLOATING); + gpio_matrix_in(config->pin_vsync, I2S0I_V_SYNC_IDX, cam_obj->vsync_invert); + + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_href], PIN_FUNC_GPIO); + gpio_set_direction(config->pin_href, GPIO_MODE_INPUT); + gpio_set_pull_mode(config->pin_href, GPIO_FLOATING); + gpio_matrix_in(config->pin_href, I2S0I_H_SYNC_IDX, false); + + int data_pins[8] = { + config->pin_d0, config->pin_d1, config->pin_d2, config->pin_d3, config->pin_d4, config->pin_d5, config->pin_d6, config->pin_d7, + }; + for (int i = 0; i < 8; i++) { + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[data_pins[i]], PIN_FUNC_GPIO); + gpio_set_direction(data_pins[i], GPIO_MODE_INPUT); + gpio_set_pull_mode(data_pins[i], GPIO_FLOATING); + // High bit alignment, IN16 is always the highest bit + // fifo accesses data by bit, when rx_bits_mod is 8, the data needs to be aligned by 8 bits + gpio_matrix_in(data_pins[i], I2S0I_DATA_IN0_IDX + 8 + i, false); + } + + esp_err_t err = camera_enable_out_clock(config); + if(err != ESP_OK) { + return err; + } + + gpio_matrix_in(0x38, I2S0I_H_ENABLE_IDX, false); + + return ESP_OK; +} + +esp_err_t cam_init(const camera_config_t *config) +{ + CAM_CHECK(NULL != config, "config pointer is invalid", ESP_ERR_INVALID_ARG); + + esp_err_t ret = ESP_OK; + cam_obj = (cam_obj_t *)heap_caps_calloc(1, sizeof(cam_obj_t), MALLOC_CAP_DMA); + CAM_CHECK(NULL != cam_obj, "lcd_cam object malloc error", ESP_ERR_NO_MEM); + + cam_obj->jpeg_mode = config->pixel_format == PIXFORMAT_JPEG; + cam_obj->vsync_pin = config->pin_vsync; + cam_obj->vsync_invert = true; + cam_obj->frame_cnt = config->fb_count; + cam_obj->width = resolution[config->frame_size].width; + cam_obj->height = resolution[config->frame_size].height; + cam_obj->swap_data = 0; + + if(cam_obj->jpeg_mode){ + cam_obj->recv_size = cam_obj->width * cam_obj->height / 5; + } else { + cam_obj->recv_size = cam_obj->width * cam_obj->height * 2; + } + + cam_obj->event_queue = xQueueCreate(16, sizeof(cam_event_t)); + CAM_CHECK_GOTO(cam_obj->event_queue != NULL, "event_queue create failed", err); + + cam_obj->frame_buffer_queue = xQueueCreate(cam_obj->frame_cnt, sizeof(camera_fb_t)); + CAM_CHECK_GOTO(cam_obj->frame_buffer_queue != NULL, "frame_buffer_queue create failed", err); + + cam_set_pin(config); + periph_module_enable(PERIPH_I2S0_MODULE); + ll_cam_config(); + + ret = esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, ll_cam_i2s_isr, NULL, &cam_obj->cam_intr_handle); + CAM_CHECK_GOTO(ret == ESP_OK, "cam intr alloc failed", err); + + ret = cam_dma_config(); + CAM_CHECK_GOTO(ret == ESP_OK, "cam_dma_config failed", err); + + xTaskCreate(cam_task, "cam_task", 1024, NULL, configMAX_PRIORITIES / 2, &cam_obj->task_handle); + + ESP_LOGI(TAG, "cam init ok"); + return ESP_OK; + +err: + cam_deinit(); + return ESP_FAIL; +} + +esp_err_t cam_deinit(void) +{ + if (!cam_obj) { + return ESP_FAIL; + } + + cam_stop(); + gpio_isr_handler_remove(cam_obj->vsync_pin); + if (cam_obj->task_handle) { + vTaskDelete(cam_obj->task_handle); + } + if (cam_obj->event_queue) { + vQueueDelete(cam_obj->event_queue); + } + if (cam_obj->frame_buffer_queue) { + vQueueDelete(cam_obj->frame_buffer_queue); + } + if (cam_obj->dma) { + free(cam_obj->dma); + } + if (cam_obj->dma_buffer) { + free(cam_obj->dma_buffer); + } + if (cam_obj->frames) { + for (int x = 0; x < cam_obj->frame_cnt; x++) { + free(cam_obj->frames[x].fb.buf - cam_obj->frames[x].fb_offset); + if (cam_obj->frames[x].dma) { + free(cam_obj->frames[x].dma); + } + } + free(cam_obj->frames); + } + + if (cam_obj->cam_intr_handle) { + esp_intr_free(cam_obj->cam_intr_handle); + } + + free(cam_obj); + cam_obj = NULL; + return ESP_OK; +} + +void cam_stop(void) +{ + ll_cam_vsync_intr_enable(false); + ll_cam_stop(); +} + +void cam_start(void) +{ + ll_cam_vsync_intr_enable(true); +} + +camera_fb_t *cam_take(void) +{ + camera_fb_t *dma_buffer; + xQueueReceive(cam_obj->frame_buffer_queue, (void *)&dma_buffer, portMAX_DELAY); + return dma_buffer; +} + +void cam_give(camera_fb_t *dma_buffer) +{ + for (int x = 0; x < cam_obj->frame_cnt; x++) { + if (&cam_obj->frames[x].fb == dma_buffer) { + cam_obj->frames[x].en = 1; + break; + } + } +} diff --git a/target/esp32s2/private_include/tjpgd.h b/target/esp32s2/private_include/tjpgd.h new file mode 100644 index 0000000..2e1a810 --- /dev/null +++ b/target/esp32s2/private_include/tjpgd.h @@ -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 1 /* 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 */ diff --git a/target/esp32s2/tjpgd.c b/target/esp32s2/tjpgd.c new file mode 100755 index 0000000..5a983c4 --- /dev/null +++ b/target/esp32s2/tjpgd.c @@ -0,0 +1,970 @@ +/*----------------------------------------------------------------------------/ +/ TJpgDec - Tiny JPEG Decompressor R0.01b (C)ChaN, 2012 +/-----------------------------------------------------------------------------/ +/ The TJpgDec is a generic JPEG decompressor module for tiny embedded systems. +/ This is a free software that opened for education, research and commercial +/ developments under license policy of following terms. +/ +/ Copyright (C) 2012, ChaN, all right reserved. +/ +/ * The TJpgDec module is a free software and there is NO WARRANTY. +/ * No restriction on use. You can use, modify and redistribute it for +/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. +/ * Redistributions of source code must retain the above copyright notice. +/ +/-----------------------------------------------------------------------------/ +/ Oct 04,'11 R0.01 First release. +/ Feb 19,'12 R0.01a Fixed decompression fails when scan starts with an escape seq. +/ Sep 03,'12 R0.01b Added JD_TBLCLIP option. +/----------------------------------------------------------------------------*/ + +#include "tjpgd.h" + +#define SUPPORT_JPEG 1 + +#ifdef SUPPORT_JPEG +/*-----------------------------------------------*/ +/* Zigzag-order to raster-order conversion table */ +/*-----------------------------------------------*/ + +#define ZIG(n) Zig[n] + +static +const BYTE Zig[64] = { /* Zigzag-order to raster-order conversion table */ + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 +}; + + + +/*-------------------------------------------------*/ +/* Input scale factor of Arai algorithm */ +/* (scaled up 16 bits for fixed point operations) */ +/*-------------------------------------------------*/ + +#define IPSF(n) Ipsf[n] + +static +const WORD Ipsf[64] = { /* See also aa_idct.png */ + (WORD)(1.00000*8192), (WORD)(1.38704*8192), (WORD)(1.30656*8192), (WORD)(1.17588*8192), (WORD)(1.00000*8192), (WORD)(0.78570*8192), (WORD)(0.54120*8192), (WORD)(0.27590*8192), + (WORD)(1.38704*8192), (WORD)(1.92388*8192), (WORD)(1.81226*8192), (WORD)(1.63099*8192), (WORD)(1.38704*8192), (WORD)(1.08979*8192), (WORD)(0.75066*8192), (WORD)(0.38268*8192), + (WORD)(1.30656*8192), (WORD)(1.81226*8192), (WORD)(1.70711*8192), (WORD)(1.53636*8192), (WORD)(1.30656*8192), (WORD)(1.02656*8192), (WORD)(0.70711*8192), (WORD)(0.36048*8192), + (WORD)(1.17588*8192), (WORD)(1.63099*8192), (WORD)(1.53636*8192), (WORD)(1.38268*8192), (WORD)(1.17588*8192), (WORD)(0.92388*8192), (WORD)(0.63638*8192), (WORD)(0.32442*8192), + (WORD)(1.00000*8192), (WORD)(1.38704*8192), (WORD)(1.30656*8192), (WORD)(1.17588*8192), (WORD)(1.00000*8192), (WORD)(0.78570*8192), (WORD)(0.54120*8192), (WORD)(0.27590*8192), + (WORD)(0.78570*8192), (WORD)(1.08979*8192), (WORD)(1.02656*8192), (WORD)(0.92388*8192), (WORD)(0.78570*8192), (WORD)(0.61732*8192), (WORD)(0.42522*8192), (WORD)(0.21677*8192), + (WORD)(0.54120*8192), (WORD)(0.75066*8192), (WORD)(0.70711*8192), (WORD)(0.63638*8192), (WORD)(0.54120*8192), (WORD)(0.42522*8192), (WORD)(0.29290*8192), (WORD)(0.14932*8192), + (WORD)(0.27590*8192), (WORD)(0.38268*8192), (WORD)(0.36048*8192), (WORD)(0.32442*8192), (WORD)(0.27590*8192), (WORD)(0.21678*8192), (WORD)(0.14932*8192), (WORD)(0.07612*8192) +}; + + + +/*---------------------------------------------*/ +/* Conversion table for fast clipping process */ +/*---------------------------------------------*/ + +#if JD_TBLCLIP + +#define BYTECLIP(v) Clip8[(UINT)(v) & 0x3FF] + +static +const BYTE Clip8[1024] = { + /* 0..255 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, + /* 256..511 */ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + /* -512..-257 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* -256..-1 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +#else /* JD_TBLCLIP */ + +inline +BYTE BYTECLIP ( + INT val +) +{ + if (val < 0) val = 0; + if (val > 255) val = 255; + + return (BYTE)val; +} + +#endif + + + +/*-----------------------------------------------------------------------*/ +/* Allocate a memory block from memory pool */ +/*-----------------------------------------------------------------------*/ + +static +void* alloc_pool ( /* Pointer to allocated memory block (NULL:no memory available) */ + JDEC* jd, /* Pointer to the decompressor object */ + UINT nd /* Number of bytes to allocate */ +) +{ + char *rp = 0; + + + nd = (nd + 3) & ~3; /* Align block size to the word boundary */ + + if (jd->sz_pool >= nd) { + jd->sz_pool -= nd; + rp = (char*)jd->pool; /* Get start of available memory pool */ + jd->pool = (void*)(rp + nd); /* Allocate requierd bytes */ + } + + return (void*)rp; /* Return allocated memory block (NULL:no memory to allocate) */ +} + + + + +/*-----------------------------------------------------------------------*/ +/* Create de-quantization and prescaling tables with a DQT segment */ +/*-----------------------------------------------------------------------*/ + +static +UINT create_qt_tbl ( /* 0:OK, !0:Failed */ + JDEC* jd, /* Pointer to the decompressor object */ + const BYTE* data, /* Pointer to the quantizer tables */ + UINT ndata /* Size of input data */ +) +{ + UINT i; + BYTE d, z; + LONG *pb; + + + while (ndata) { /* Process all tables in the segment */ + if (ndata < 65) return JDR_FMT1; /* Err: table size is unaligned */ + ndata -= 65; + d = *data++; /* Get table property */ + if (d & 0xF0) return JDR_FMT1; /* Err: not 8-bit resolution */ + i = d & 3; /* Get table ID */ + pb = alloc_pool(jd, 64 * sizeof (LONG));/* Allocate a memory block for the table */ + if (!pb) return JDR_MEM1; /* Err: not enough memory */ + jd->qttbl[i] = pb; /* Register the table */ + for (i = 0; i < 64; i++) { /* Load the table */ + z = ZIG(i); /* Zigzag-order to raster-order conversion */ + pb[z] = (LONG)((DWORD)*data++ * IPSF(z)); /* Apply scale factor of Arai algorithm to the de-quantizers */ + } + } + + return JDR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Create huffman code tables with a DHT segment */ +/*-----------------------------------------------------------------------*/ + +static +UINT create_huffman_tbl ( /* 0:OK, !0:Failed */ + JDEC* jd, /* Pointer to the decompressor object */ + const BYTE* data, /* Pointer to the packed huffman tables */ + UINT ndata /* Size of input data */ +) +{ + UINT i, j, b, np, cls, num; + BYTE d, *pb, *pd; + WORD hc, *ph; + + + while (ndata) { /* Process all tables in the segment */ + if (ndata < 17) return JDR_FMT1; /* Err: wrong data size */ + ndata -= 17; + d = *data++; /* Get table number and class */ + cls = (d >> 4); num = d & 0x0F; /* class = dc(0)/ac(1), table number = 0/1 */ + if (d & 0xEE) return JDR_FMT1; /* Err: invalid class/number */ + pb = alloc_pool(jd, 16); /* Allocate a memory block for the bit distribution table */ + if (!pb) return JDR_MEM1; /* Err: not enough memory */ + jd->huffbits[num][cls] = pb; + for (np = i = 0; i < 16; i++) { /* Load number of patterns for 1 to 16-bit code */ + pb[i] = b = *data++; + np += b; /* Get sum of code words for each code */ + } + + ph = alloc_pool(jd, np * sizeof (WORD));/* Allocate a memory block for the code word table */ + if (!ph) return JDR_MEM1; /* Err: not enough memory */ + jd->huffcode[num][cls] = ph; + hc = 0; + for (j = i = 0; i < 16; i++) { /* Re-build huffman code word table */ + b = pb[i]; + while (b--) ph[j++] = hc++; + hc <<= 1; + } + + if (ndata < np) return JDR_FMT1; /* Err: wrong data size */ + ndata -= np; + pd = alloc_pool(jd, np); /* Allocate a memory block for the decoded data */ + if (!pd) return JDR_MEM1; /* Err: not enough memory */ + jd->huffdata[num][cls] = pd; + for (i = 0; i < np; i++) { /* Load decoded data corresponds to each code ward */ + d = *data++; + if (!cls && d > 11) return JDR_FMT1; + *pd++ = d; + } + } + + return JDR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Extract N bits from input stream */ +/*-----------------------------------------------------------------------*/ + +static +INT bitext ( /* >=0: extracted data, <0: error code */ + JDEC* jd, /* Pointer to the decompressor object */ + UINT nbit /* Number of bits to extract (1 to 11) */ +) +{ + BYTE msk, s, *dp; + UINT dc, v, f; + + + msk = jd->dmsk; dc = jd->dctr; dp = jd->dptr; /* Bit mask, number of data available, read ptr */ + s = *dp; v = f = 0; + do { + if (!msk) { /* Next byte? */ + if (!dc) { /* No input data is available, re-fill input buffer */ + dp = jd->inbuf; /* Top of input buffer */ + dc = jd->infunc(jd, dp, JD_SZBUF); + if (!dc) return 0 - (INT)JDR_INP; /* Err: read error or wrong stream termination */ + } else { + dp++; /* Next data ptr */ + } + dc--; /* Decrement number of available bytes */ + if (f) { /* In flag sequence? */ + f = 0; /* Exit flag sequence */ + if (*dp != 0) return 0 - (INT)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */ + *dp = s = 0xFF; /* The flag is a data 0xFF */ + } else { + s = *dp; /* Get next data byte */ + if (s == 0xFF) { /* Is start of flag sequence? */ + f = 1; continue; /* Enter flag sequence */ + } + } + msk = 0x80; /* Read from MSB */ + } + v <<= 1; /* Get a bit */ + if (s & msk) v++; + msk >>= 1; + nbit--; + } while (nbit); + jd->dmsk = msk; jd->dctr = dc; jd->dptr = dp; + + return (INT)v; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Extract a huffman decoded data from input stream */ +/*-----------------------------------------------------------------------*/ + +static +INT huffext ( /* >=0: decoded data, <0: error code */ + JDEC* jd, /* Pointer to the decompressor object */ + const BYTE* hbits, /* Pointer to the bit distribution table */ + const WORD* hcode, /* Pointer to the code word table */ + const BYTE* hdata /* Pointer to the data table */ +) +{ + BYTE msk, s, *dp; + UINT dc, v, f, bl, nd; + + + msk = jd->dmsk; dc = jd->dctr; dp = jd->dptr; /* Bit mask, number of data available, read ptr */ + s = *dp; v = f = 0; + bl = 16; /* Max code length */ + do { + if (!msk) { /* Next byte? */ + if (!dc) { /* No input data is available, re-fill input buffer */ + dp = jd->inbuf; /* Top of input buffer */ + dc = jd->infunc(jd, dp, JD_SZBUF); + if (!dc) return 0 - (INT)JDR_INP; /* Err: read error or wrong stream termination */ + } else { + dp++; /* Next data ptr */ + } + dc--; /* Decrement number of available bytes */ + if (f) { /* In flag sequence? */ + f = 0; /* Exit flag sequence */ + if (*dp != 0) + return 0 - (INT)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */ + *dp = s = 0xFF; /* The flag is a data 0xFF */ + } else { + s = *dp; /* Get next data byte */ + if (s == 0xFF) { /* Is start of flag sequence? */ + f = 1; continue; /* Enter flag sequence, get trailing byte */ + } + } + msk = 0x80; /* Read from MSB */ + } + v <<= 1; /* Get a bit */ + if (s & msk) v++; + msk >>= 1; + + for (nd = *hbits++; nd; nd--) { /* Search the code word in this bit length */ + if (v == *hcode++) { /* Matched? */ + jd->dmsk = msk; jd->dctr = dc; jd->dptr = dp; + return *hdata; /* Return the decoded data */ + } + hdata++; + } + bl--; + } while (bl); + + return 0 - (INT)JDR_FMT1; /* Err: code not found (may be collapted data) */ +} + + + + +/*-----------------------------------------------------------------------*/ +/* Apply Inverse-DCT in Arai Algorithm (see also aa_idct.png) */ +/*-----------------------------------------------------------------------*/ + +static +void block_idct ( + LONG* src, /* Input block data (de-quantized and pre-scaled for Arai Algorithm) */ + BYTE* dst /* Pointer to the destination to store the block as byte array */ +) +{ + const LONG M13 = (LONG)(1.41421*4096), M2 = (LONG)(1.08239*4096), M4 = (LONG)(2.61313*4096), M5 = (LONG)(1.84776*4096); + LONG v0, v1, v2, v3, v4, v5, v6, v7; + LONG t10, t11, t12, t13; + UINT i; + + /* Process columns */ + for (i = 0; i < 8; i++) { + v0 = src[8 * 0]; /* Get even elements */ + v1 = src[8 * 2]; + v2 = src[8 * 4]; + v3 = src[8 * 6]; + + t10 = v0 + v2; /* Process the even elements */ + t12 = v0 - v2; + t11 = (v1 - v3) * M13 >> 12; + v3 += v1; + t11 -= v3; + v0 = t10 + v3; + v3 = t10 - v3; + v1 = t11 + t12; + v2 = t12 - t11; + + v4 = src[8 * 7]; /* Get odd elements */ + v5 = src[8 * 1]; + v6 = src[8 * 5]; + v7 = src[8 * 3]; + + t10 = v5 - v4; /* Process the odd elements */ + t11 = v5 + v4; + t12 = v6 - v7; + v7 += v6; + v5 = (t11 - v7) * M13 >> 12; + v7 += t11; + t13 = (t10 + t12) * M5 >> 12; + v4 = t13 - (t10 * M2 >> 12); + v6 = t13 - (t12 * M4 >> 12) - v7; + v5 -= v6; + v4 -= v5; + + src[8 * 0] = v0 + v7; /* Write-back transformed values */ + src[8 * 7] = v0 - v7; + src[8 * 1] = v1 + v6; + src[8 * 6] = v1 - v6; + src[8 * 2] = v2 + v5; + src[8 * 5] = v2 - v5; + src[8 * 3] = v3 + v4; + src[8 * 4] = v3 - v4; + + src++; /* Next column */ + } + + /* Process rows */ + src -= 8; + for (i = 0; i < 8; i++) { + v0 = src[0] + (128L << 8); /* Get even elements (remove DC offset (-128) here) */ + v1 = src[2]; + v2 = src[4]; + v3 = src[6]; + + t10 = v0 + v2; /* Process the even elements */ + t12 = v0 - v2; + t11 = (v1 - v3) * M13 >> 12; + v3 += v1; + t11 -= v3; + v0 = t10 + v3; + v3 = t10 - v3; + v1 = t11 + t12; + v2 = t12 - t11; + + v4 = src[7]; /* Get odd elements */ + v5 = src[1]; + v6 = src[5]; + v7 = src[3]; + + t10 = v5 - v4; /* Process the odd elements */ + t11 = v5 + v4; + t12 = v6 - v7; + v7 += v6; + v5 = (t11 - v7) * M13 >> 12; + v7 += t11; + t13 = (t10 + t12) * M5 >> 12; + v4 = t13 - (t10 * M2 >> 12); + v6 = t13 - (t12 * M4 >> 12) - v7; + v5 -= v6; + v4 -= v5; + + dst[0] = BYTECLIP((v0 + v7) >> 8); /* Descale the transformed values 8 bits and output */ + dst[7] = BYTECLIP((v0 - v7) >> 8); + dst[1] = BYTECLIP((v1 + v6) >> 8); + dst[6] = BYTECLIP((v1 - v6) >> 8); + dst[2] = BYTECLIP((v2 + v5) >> 8); + dst[5] = BYTECLIP((v2 - v5) >> 8); + dst[3] = BYTECLIP((v3 + v4) >> 8); + dst[4] = BYTECLIP((v3 - v4) >> 8); + dst += 8; + + src += 8; /* Next row */ + } +} + + + + +/*-----------------------------------------------------------------------*/ +/* Load all blocks in the MCU into working buffer */ +/*-----------------------------------------------------------------------*/ + +static +JRESULT mcu_load ( + JDEC* jd /* Pointer to the decompressor object */ +) +{ + LONG *tmp = (LONG*)jd->workbuf; /* Block working buffer for de-quantize and IDCT */ + UINT blk, nby, nbc, i, z, id, cmp; + INT b, d, e; + BYTE *bp; + const BYTE *hb, *hd; + const WORD *hc; + const LONG *dqf; + + + nby = jd->msx * jd->msy; /* Number of Y blocks (1, 2 or 4) */ + nbc = 2; /* Number of C blocks (2) */ + bp = jd->mcubuf; /* Pointer to the first block */ + + for (blk = 0; blk < nby + nbc; blk++) { + cmp = (blk < nby) ? 0 : blk - nby + 1; /* Component number 0:Y, 1:Cb, 2:Cr */ + id = cmp ? 1 : 0; /* Huffman table ID of the component */ + + /* Extract a DC element from input stream */ + hb = jd->huffbits[id][0]; /* Huffman table for the DC element */ + hc = jd->huffcode[id][0]; + hd = jd->huffdata[id][0]; + b = huffext(jd, hb, hc, hd); /* Extract a huffman coded data (bit length) */ + if (b < 0) return 0 - b; /* Err: invalid code or input */ + d = jd->dcv[cmp]; /* DC value of previous block */ + if (b) { /* If there is any difference from previous block */ + e = bitext(jd, b); /* Extract data bits */ + if (e < 0) return 0 - e; /* Err: input */ + b = 1 << (b - 1); /* MSB position */ + if (!(e & b)) e -= (b << 1) - 1; /* Restore sign if needed */ + d += e; /* Get current value */ + jd->dcv[cmp] = (SHORT)d; /* Save current DC value for next block */ + } + dqf = jd->qttbl[jd->qtid[cmp]]; /* De-quantizer table ID for this component */ + tmp[0] = d * dqf[0] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */ + + /* Extract following 63 AC elements from input stream */ + for (i = 1; i < 64; i++) tmp[i] = 0; /* Clear rest of elements */ + hb = jd->huffbits[id][1]; /* Huffman table for the AC elements */ + hc = jd->huffcode[id][1]; + hd = jd->huffdata[id][1]; + i = 1; /* Top of the AC elements */ + do { + b = huffext(jd, hb, hc, hd); /* Extract a huffman coded value (zero runs and bit length) */ + if (b == 0) break; /* EOB? */ + if (b < 0) return 0 - b; /* Err: invalid code or input error */ + z = (UINT)b >> 4; /* Number of leading zero elements */ + if (z) { + i += z; /* Skip zero elements */ + if (i >= 64) return JDR_FMT1; /* Too long zero run */ + } + if (b &= 0x0F) { /* Bit length */ + d = bitext(jd, b); /* Extract data bits */ + if (d < 0) return 0 - d; /* Err: input device */ + b = 1 << (b - 1); /* MSB position */ + if (!(d & b)) d -= (b << 1) - 1;/* Restore negative value if needed */ + z = ZIG(i); /* Zigzag-order to raster-order converted index */ + tmp[z] = d * dqf[z] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */ + } + } while (++i < 64); /* Next AC element */ + + if (JD_USE_SCALE && jd->scale == 3) + *bp = (*tmp / 256) + 128; /* If scale ratio is 1/8, IDCT can be ommited and only DC element is used */ + else + block_idct(tmp, bp); /* Apply IDCT and store the block to the MCU buffer */ + + bp += 64; /* Next block */ + } + + return JDR_OK; /* All blocks have been loaded successfully */ +} + + + + +/*-----------------------------------------------------------------------*/ +/* Output an MCU: Convert YCrCb to RGB and output it in RGB form */ +/*-----------------------------------------------------------------------*/ + +static +JRESULT mcu_output ( + JDEC* jd, /* Pointer to the decompressor object */ + UINT (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */ + UINT x, /* MCU position in the image (left of the MCU) */ + UINT y /* MCU position in the image (top of the MCU) */ +) +{ + const INT CVACC = (sizeof (INT) > 2) ? 1024 : 128; + UINT ix, iy, mx, my, rx, ry; + INT yy, cb, cr; + BYTE *py, *pc, *rgb24; + JRECT rect; + + + mx = jd->msx * 8; my = jd->msy * 8; /* MCU size (pixel) */ + rx = (x + mx <= jd->width) ? mx : jd->width - x; /* Output rectangular size (it may be clipped at right/bottom end) */ + ry = (y + my <= jd->height) ? my : jd->height - y; + if (JD_USE_SCALE) { + rx >>= jd->scale; ry >>= jd->scale; + if (!rx || !ry) return JDR_OK; /* Skip this MCU if all pixel is to be rounded off */ + x >>= jd->scale; y >>= jd->scale; + } + rect.left = x; rect.right = x + rx - 1; /* Rectangular area in the frame buffer */ + rect.top = y; rect.bottom = y + ry - 1; + + + if (!JD_USE_SCALE || jd->scale != 3) { /* Not for 1/8 scaling */ + + /* Build an RGB MCU from discrete comopnents */ + rgb24 = (BYTE*)jd->workbuf; + for (iy = 0; iy < my; iy++) { + pc = jd->mcubuf; + py = pc + iy * 8; + if (my == 16) { /* Double block height? */ + pc += 64 * 4 + (iy >> 1) * 8; + if (iy >= 8) py += 64; + } else { /* Single block height */ + pc += mx * 8 + iy * 8; + } + for (ix = 0; ix < mx; ix++) { + cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */ + cr = pc[64] - 128; + if (mx == 16) { /* Double block width? */ + if (ix == 8) py += 64 - 8; /* Jump to next block if double block heigt */ + pc += ix & 1; /* Increase chroma pointer every two pixels */ + } else { /* Single block width */ + pc++; /* Increase chroma pointer every pixel */ + } + yy = *py++; /* Get Y component */ + + /* Convert YCbCr to RGB */ + *rgb24++ = /* R */ BYTECLIP(yy + ((INT)(1.402 * CVACC) * cr) / CVACC); + *rgb24++ = /* G */ BYTECLIP(yy - ((INT)(0.344 * CVACC) * cb + (INT)(0.714 * CVACC) * cr) / CVACC); + *rgb24++ = /* B */ BYTECLIP(yy + ((INT)(1.772 * CVACC) * cb) / CVACC); + } + } + + /* Descale the MCU rectangular if needed */ + if (JD_USE_SCALE && jd->scale) { + UINT x, y, r, g, b, s, w, a; + BYTE *op; + + /* Get averaged RGB value of each square correcponds to a pixel */ + s = jd->scale * 2; /* Bumber of shifts for averaging */ + w = 1 << jd->scale; /* Width of square */ + a = (mx - w) * 3; /* Bytes to skip for next line in the square */ + op = (BYTE*)jd->workbuf; + for (iy = 0; iy < my; iy += w) { + for (ix = 0; ix < mx; ix += w) { + rgb24 = (BYTE*)jd->workbuf + (iy * mx + ix) * 3; + r = g = b = 0; + for (y = 0; y < w; y++) { /* Accumulate RGB value in the square */ + for (x = 0; x < w; x++) { + r += *rgb24++; + g += *rgb24++; + b += *rgb24++; + } + rgb24 += a; + } /* Put the averaged RGB value as a pixel */ + *op++ = (BYTE)(r >> s); + *op++ = (BYTE)(g >> s); + *op++ = (BYTE)(b >> s); + } + } + } + + } else { /* For only 1/8 scaling (left-top pixel in each block are the DC value of the block) */ + + /* Build a 1/8 descaled RGB MCU from discrete comopnents */ + rgb24 = (BYTE*)jd->workbuf; + pc = jd->mcubuf + mx * my; + cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */ + cr = pc[64] - 128; + for (iy = 0; iy < my; iy += 8) { + py = jd->mcubuf; + if (iy == 8) py += 64 * 2; + for (ix = 0; ix < mx; ix += 8) { + yy = *py; /* Get Y component */ + py += 64; + + /* Convert YCbCr to RGB */ + *rgb24++ = /* R */ BYTECLIP(yy + ((INT)(1.402 * CVACC) * cr / CVACC)); + *rgb24++ = /* G */ BYTECLIP(yy - ((INT)(0.344 * CVACC) * cb + (INT)(0.714 * CVACC) * cr) / CVACC); + *rgb24++ = /* B */ BYTECLIP(yy + ((INT)(1.772 * CVACC) * cb / CVACC)); + } + } + } + + /* Squeeze up pixel table if a part of MCU is to be truncated */ + mx >>= jd->scale; + if (rx < mx) { + BYTE *s, *d; + UINT x, y; + + s = d = (BYTE*)jd->workbuf; + for (y = 0; y < ry; y++) { + for (x = 0; x < rx; x++) { /* Copy effective pixels */ + *d++ = *s++; + *d++ = *s++; + *d++ = *s++; + } + s += (mx - rx) * 3; /* Skip truncated pixels */ + } + } + + /* Convert RGB888 to RGB565 if needed */ + if (JD_FORMAT == 1) { + BYTE *s = (BYTE*)jd->workbuf; + WORD w, *d = (WORD*)s; + UINT n = rx * ry; + + do { + w = (*s++ & 0xF8) << 8; /* RRRRR----------- */ + w |= (*s++ & 0xFC) << 3; /* -----GGGGGG----- */ + w |= *s++ >> 3; /* -----------BBBBB */ + *d++ = w; + } while (--n); + } + + /* Output the RGB rectangular */ + return outfunc(jd, jd->workbuf, &rect) ? JDR_OK : JDR_INTR; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Process restart interval */ +/*-----------------------------------------------------------------------*/ + +static +JRESULT restart ( + JDEC* jd, /* Pointer to the decompressor object */ + WORD rstn /* Expected restert sequense number */ +) +{ + UINT i, dc; + WORD d; + BYTE *dp; + + + /* Discard padding bits and get two bytes from the input stream */ + dp = jd->dptr; dc = jd->dctr; + d = 0; + for (i = 0; i < 2; i++) { + if (!dc) { /* No input data is available, re-fill input buffer */ + dp = jd->inbuf; + dc = jd->infunc(jd, dp, JD_SZBUF); + if (!dc) return JDR_INP; + } else { + dp++; + } + dc--; + d = (d << 8) | *dp; /* Get a byte */ + } + jd->dptr = dp; jd->dctr = dc; jd->dmsk = 0; + + /* Check the marker */ + if ((d & 0xFFD8) != 0xFFD0 || (d & 7) != (rstn & 7)) + return JDR_FMT1; /* Err: expected RSTn marker is not detected (may be collapted data) */ + + /* Reset DC offset */ + jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; + + return JDR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Analyze the JPEG image and Initialize decompressor object */ +/*-----------------------------------------------------------------------*/ + +#define LDB_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr))<<8)|(WORD)*(BYTE*)((ptr)+1)) + + +JRESULT jd_prepare ( + JDEC* jd, /* Blank decompressor object */ + UINT (*infunc)(JDEC*, BYTE*, UINT), /* JPEG strem input function */ + void* pool, /* Working buffer for the decompression session */ + UINT sz_pool, /* Size of working buffer */ + void* dev /* I/O device identifier for the session */ +) +{ + BYTE *seg, b; + WORD marker; + DWORD ofs; + UINT n, i, j, len; + JRESULT rc; + + + if (!pool) return JDR_PAR; + + jd->pool = pool; /* Work memroy */ + jd->sz_pool = sz_pool; /* Size of given work memory */ + jd->infunc = infunc; /* Stream input function */ + jd->device = dev; /* I/O device identifier */ + jd->nrst = 0; /* No restart interval (default) */ + + for (i = 0; i < 2; i++) { /* Nulls pointers */ + for (j = 0; j < 2; j++) { + jd->huffbits[i][j] = 0; + jd->huffcode[i][j] = 0; + jd->huffdata[i][j] = 0; + } + } + for (i = 0; i < 4; i++) jd->qttbl[i] = 0; + + jd->inbuf = seg = alloc_pool(jd, JD_SZBUF); /* Allocate stream input buffer */ + if (!seg) return JDR_MEM1; + + if (jd->infunc(jd, seg, 2) != 2) return JDR_INP;/* Check SOI marker */ + if (LDB_WORD(seg) != 0xFFD8) return JDR_FMT1; /* Err: SOI is not detected */ + ofs = 2; + + for (;;) { + /* Get a JPEG marker */ + if (jd->infunc(jd, seg, 4) != 4) return JDR_INP; + marker = LDB_WORD(seg); /* Marker */ + len = LDB_WORD(seg + 2); /* Length field */ + if (len <= 2 || (marker >> 8) != 0xFF) return JDR_FMT1; + len -= 2; /* Content size excluding length field */ + ofs += 4 + len; /* Number of bytes loaded */ + + switch (marker & 0xFF) { + case 0xC0: /* SOF0 (baseline JPEG) */ + /* Load segment data */ + if (len > JD_SZBUF) return JDR_MEM2; + if (jd->infunc(jd, seg, len) != len) return JDR_INP; + + jd->width = LDB_WORD(seg+3); /* Image width in unit of pixel */ + jd->height = LDB_WORD(seg+1); /* Image height in unit of pixel */ + if (seg[5] != 3) return JDR_FMT3; /* Err: Supports only Y/Cb/Cr format */ + + /* Check three image components */ + for (i = 0; i < 3; i++) { + b = seg[7 + 3 * i]; /* Get sampling factor */ + if (!i) { /* Y component */ + if (b != 0x11 && b != 0x22 && b != 0x21)/* Check sampling factor */ + return JDR_FMT3; /* Err: Supports only 4:4:4, 4:2:0 or 4:2:2 */ + jd->msx = b >> 4; jd->msy = b & 15; /* Size of MCU [blocks] */ + } else { /* Cb/Cr component */ + if (b != 0x11) return JDR_FMT3; /* Err: Sampling factor of Cr/Cb must be 1 */ + } + b = seg[8 + 3 * i]; /* Get dequantizer table ID for this component */ + if (b > 3) return JDR_FMT3; /* Err: Invalid ID */ + jd->qtid[i] = b; + } + break; + + case 0xDD: /* DRI */ + /* Load segment data */ + if (len > JD_SZBUF) return JDR_MEM2; + if (jd->infunc(jd, seg, len) != len) return JDR_INP; + + /* Get restart interval (MCUs) */ + jd->nrst = LDB_WORD(seg); + break; + + case 0xC4: /* DHT */ + /* Load segment data */ + if (len > JD_SZBUF) return JDR_MEM2; + if (jd->infunc(jd, seg, len) != len) return JDR_INP; + + /* Create huffman tables */ + rc = create_huffman_tbl(jd, seg, len); + if (rc) return rc; + break; + + case 0xDB: /* DQT */ + /* Load segment data */ + if (len > JD_SZBUF) return JDR_MEM2; + if (jd->infunc(jd, seg, len) != len) return JDR_INP; + + /* Create de-quantizer tables */ + rc = create_qt_tbl(jd, seg, len); + if (rc) return rc; + break; + + case 0xDA: /* SOS */ + /* Load segment data */ + if (len > JD_SZBUF) return JDR_MEM2; + if (jd->infunc(jd, seg, len) != len) return JDR_INP; + + if (!jd->width || !jd->height) return JDR_FMT1; /* Err: Invalid image size */ + + if (seg[0] != 3) return JDR_FMT3; /* Err: Supports only three color components format */ + + /* Check if all tables corresponding to each components have been loaded */ + for (i = 0; i < 3; i++) { + b = seg[2 + 2 * i]; /* Get huffman table ID */ + if (b != 0x00 && b != 0x11) return JDR_FMT3; /* Err: Different table number for DC/AC element */ + b = i ? 1 : 0; + if (!jd->huffbits[b][0] || !jd->huffbits[b][1]) /* Check huffman table for this component */ + return JDR_FMT1; /* Err: Huffman table not loaded */ + if (!jd->qttbl[jd->qtid[i]]) return JDR_FMT1; /* Err: Dequantizer table not loaded */ + } + + /* Allocate working buffer for MCU and RGB */ + n = jd->msy * jd->msx; /* Number of Y blocks in the MCU */ + if (!n) return JDR_FMT1; /* Err: SOF0 has not been loaded */ + len = n * 64 * 2 + 64; /* Allocate buffer for IDCT and RGB output */ + if (len < 256) len = 256; /* but at least 256 byte is required for IDCT */ + jd->workbuf = alloc_pool(jd, len); /* and it may occupy a part of following MCU working buffer for RGB output */ + if (!jd->workbuf) return JDR_MEM1; /* Err: not enough memory */ + jd->mcubuf = alloc_pool(jd, (n + 2) * 64); /* Allocate MCU working buffer */ + if (!jd->mcubuf) return JDR_MEM1; /* Err: not enough memory */ + + /* Pre-load the JPEG data to extract it from the bit stream */ + jd->dptr = seg; jd->dctr = 0; jd->dmsk = 0; /* Prepare to read bit stream */ + if (ofs %= JD_SZBUF) { /* Align read offset to JD_SZBUF */ + jd->dctr = jd->infunc(jd, seg + ofs, JD_SZBUF - (UINT)ofs); + jd->dptr = seg + ofs - 1; + } + + return JDR_OK; /* Initialization succeeded. Ready to decompress the JPEG image. */ + + case 0xC1: /* SOF1 */ + case 0xC2: /* SOF2 */ + case 0xC3: /* SOF3 */ + case 0xC5: /* SOF5 */ + case 0xC6: /* SOF6 */ + case 0xC7: /* SOF7 */ + case 0xC9: /* SOF9 */ + case 0xCA: /* SOF10 */ + case 0xCB: /* SOF11 */ + case 0xCD: /* SOF13 */ + case 0xCE: /* SOF14 */ + case 0xCF: /* SOF15 */ + case 0xD9: /* EOI */ + return JDR_FMT3; /* Unsuppoted JPEG standard (may be progressive JPEG) */ + + default: /* Unknown segment (comment, exif or etc..) */ + /* Skip segment data */ + if (jd->infunc(jd, 0, len) != len) /* Null pointer specifies to skip bytes of stream */ + return JDR_INP; + } + } +} + + + + +/*-----------------------------------------------------------------------*/ +/* Start to decompress the JPEG picture */ +/*-----------------------------------------------------------------------*/ + +JRESULT jd_decomp ( + JDEC* jd, /* Initialized decompression object */ + UINT (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */ + BYTE scale /* Output de-scaling factor (0 to 3) */ +) +{ + UINT x, y, mx, my; + WORD rst, rsc; + JRESULT rc; + + + if (scale > (JD_USE_SCALE ? 3 : 0)) return JDR_PAR; + jd->scale = scale; + + mx = jd->msx * 8; my = jd->msy * 8; /* Size of the MCU (pixel) */ + + jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; /* Initialize DC values */ + rst = rsc = 0; + + rc = JDR_OK; + for (y = 0; y < jd->height; y += my) { /* Vertical loop of MCUs */ + for (x = 0; x < jd->width; x += mx) { /* Horizontal loop of MCUs */ + if (jd->nrst && rst++ == jd->nrst) { /* Process restart interval if enabled */ + rc = restart(jd, rsc++); + if (rc != JDR_OK) return rc; + rst = 1; + } + rc = mcu_load(jd); /* Load an MCU (decompress huffman coded stream and apply IDCT) */ + if (rc != JDR_OK) return rc; + rc = mcu_output(jd, outfunc, x, y); /* Output the MCU (color space conversion, scaling and output) */ + if (rc != JDR_OK) return rc; + } + } + + return rc; +} +#endif//SUPPORT_JPEG + +