feat(jpeg): Add jpeg decoder example for esp32p4

pull/13309/head
Cao Sen Miao 2024-02-28 15:15:03 +08:00
rodzic abc2971d95
commit e3a12a9ad6
11 zmienionych plików z 349 dodań i 0 usunięć

Wyświetl plik

@ -116,6 +116,12 @@ examples/peripherals/i2s/i2s_recorder:
- esp_driver_spi
- esp_driver_i2s
examples/peripherals/jpeg/jpeg_decode:
disable:
- if: SOC_JPEG_CODEC_SUPPORTED != 1
depends_components:
- esp_driver_jpeg
examples/peripherals/lcd/i2c_oled:
disable:
- if: SOC_I2C_SUPPORTED != 1

Wyświetl plik

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(jpeg_decode)

Wyświetl plik

@ -0,0 +1,58 @@
| Supported Targets | ESP32-P4 |
| ----------------- | -------- |
# JPEG decode example
## Overview
This example demonstrates how to use the JPEG hardware decoder to decode a 1080p and a 720p picture:
If you have a bunch of big JPEG picture need to be decoded, such as `*.jpg` -> `*.rgb`, and this example uses hardware JPEG decoder to accelerate the decoding.
## How to use example
### Prerequisites Required
This example demonstrates the flexibility of decoding pictures by decoding two different sizes: one in 1080p and another in 720p. It showcases how you can easily modify the code to meet your specific requirements, such as only decoding 1080p photos.
### Build and Flash
Before you start build and flash this example, please put the image `esp720.jpg` and `esp1080.jpg` in your sdcard.
Enter `idf.py -p PORT flash monitor` to build, flash and monitor the project.
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Example Output
```bash
I (1116) jpeg.example: Initializing SD card
I (1116) gpio: GPIO[43]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1126) gpio: GPIO[44]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1136) gpio: GPIO[39]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1146) gpio: GPIO[40]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1156) gpio: GPIO[41]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1166) gpio: GPIO[42]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (1416) gpio: GPIO[42]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
Name: SD64G
Type: SDHC/SDXC
Speed: 40.00 MHz (limit: 40.00 MHz)
Size: 60906MB
CSD: ver=2, sector_size=512, capacity=124735488 read_bl_len=9
SSR: bus_width=4
I (1436) jpeg.example: jpg_file_1080:/sdcard/esp1080.jpg
I (1696) jpeg.example: jpg_file_1080:/sdcard/esp720.jpg
I (1796) jpeg.example: header parsed, width is 1920, height is 1080
I (1846) jpeg.example: raw_file_1080:/sdcard/out.rgb
I (11836) jpeg.example: raw_file_720:/sdcard/out2.rgb
I (13336) jpeg.example: Card unmounted
I (13336) main_task: Returned from app_main()
```
Moreover, we provided a helper script called `open_rgb.py`, which can help you easily see the outputs on your computer. For requirements component you need, you can call `pip install -r requirements.txt` under `examples/peripheral/jpeg/jpeg_decode` folder.
## Troubleshooting
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)

Wyświetl plik

@ -0,0 +1,2 @@
idf_component_register(SRCS "jpeg_decode_main.c"
INCLUDE_DIRS ".")

Wyświetl plik

@ -0,0 +1,9 @@
menu "JPEG Decode Example menu"
config EXAMPLE_FORMAT_IF_MOUNT_FAILED
bool "Format the card if mount failed"
default n
help
If this config item is set, format_if_mount_failed will be set to true and the card will be formatted if
the mount has failed.
endmenu

Wyświetl plik

@ -0,0 +1,169 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <string.h>
#include "esp_heap_caps.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "esp_attr.h"
#include "driver/jpeg_decode.h"
static const char *TAG = "jpeg.example";
static sdmmc_card_t *s_card;
#define MOUNT_POINT "/sdcard"
const static char jpg_file_1080[] = "/sdcard/esp1080.jpg";
const static char raw_file_1080[] = "/sdcard/out.rgb";
const static char jpg_file_720[] = "/sdcard/esp720.jpg";
const static char raw_file_720[] = "/sdcard/out2.rgb";
static esp_err_t sdcard_init(void)
{
esp_err_t ret = ESP_OK;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
#ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
.format_if_mount_failed = true,
#else
.format_if_mount_failed = false,
#endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
const char mount_point[] = MOUNT_POINT;
ESP_LOGI(TAG, "Initializing SD card");
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
slot_config.width = 4;
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &s_card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount filesystem. "
"If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
} else {
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
}
return ret;
}
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, s_card);
return ret;
}
static void sdcard_deinit(void)
{
const char mount_point[] = MOUNT_POINT;
esp_vfs_fat_sdcard_unmount(mount_point, s_card);
}
void app_main(void)
{
ESP_ERROR_CHECK(sdcard_init());
jpeg_decoder_handle_t jpgd_handle;
jpeg_decode_engine_cfg_t decode_eng_cfg = {
};
ESP_ERROR_CHECK(jpeg_new_decoder_engine(&decode_eng_cfg, &jpgd_handle));
jpeg_decode_cfg_t decode_cfg_rgb = {
.output_format = JPEG_DECODE_OUT_FORMAT_RGB888,
.rgb_order = JPEG_DEC_RGB_ELEMENT_ORDER_BGR,
};
jpeg_decode_cfg_t decode_cfg_gray = {
.output_format = JPEG_DECODE_OUT_FORMAT_GRAY,
};
FILE *file_jpg_1080p = fopen(jpg_file_1080, "rb");
ESP_LOGI(TAG, "jpg_file_1080:%s", jpg_file_1080);
if (file_jpg_1080p == NULL) {
ESP_LOGE(TAG, "fopen file_jpg_1080p error");
return;
}
fseek(file_jpg_1080p, 0, SEEK_END);
int jpeg_size_1080p = ftell(file_jpg_1080p);
fseek(file_jpg_1080p, 0, SEEK_SET);
uint8_t *tx_buf_1080p = (uint8_t*)jpeg_alloc_decoder_mem(jpeg_size_1080p);
if (tx_buf_1080p == NULL) {
ESP_LOGE(TAG, "alloc 1080p tx buffer error");
return;
}
fread(tx_buf_1080p, 1, jpeg_size_1080p, file_jpg_1080p);
fclose(file_jpg_1080p);
FILE *file_jpg_720p = fopen(jpg_file_720, "rb");
ESP_LOGI(TAG, "jpg_file_1080:%s", jpg_file_720);
if (file_jpg_720p == NULL) {
ESP_LOGE(TAG, "fopen file_jpg_720p error");
return;
}
fseek(file_jpg_720p, 0, SEEK_END);
int jpeg_size_720p = ftell(file_jpg_720p);
fseek(file_jpg_720p, 0, SEEK_SET);
uint8_t *tx_buf_720p = (uint8_t*)jpeg_alloc_decoder_mem(jpeg_size_720p);
if (tx_buf_720p == NULL) {
ESP_LOGE(TAG, "alloc 720p tx buffer error");
return;
}
fread(tx_buf_720p, 1, jpeg_size_720p, file_jpg_720p);
fclose(file_jpg_720p);
uint8_t *rx_buf_1080p = (uint8_t*)jpeg_alloc_decoder_mem(1920 * 1080 * 3);
uint8_t *rx_buf_720p = (uint8_t*)jpeg_alloc_decoder_mem(720 * 1280);
if (rx_buf_1080p == NULL) {
ESP_LOGE(TAG, "alloc 1080p rx buffer error");
return;
}
if (rx_buf_720p == NULL) {
ESP_LOGE(TAG, "alloc 720p rx buffer error");
return;
}
// Get the jpg header information (This step is optional)
jpeg_decode_picture_info_t header_info;
ESP_ERROR_CHECK(jpeg_decoder_get_info(tx_buf_1080p, jpeg_size_1080p, &header_info));
ESP_LOGI(TAG, "header parsed, width is %" PRId32 ", height is %" PRId32, header_info.width, header_info.height);
uint32_t out_size_1080p = 0;
uint32_t out_size_720p = 0;
ESP_ERROR_CHECK(jpeg_decoder_process(jpgd_handle, &decode_cfg_rgb, tx_buf_1080p, jpeg_size_1080p, rx_buf_1080p, &out_size_1080p));
ESP_ERROR_CHECK(jpeg_decoder_process(jpgd_handle, &decode_cfg_gray, tx_buf_720p, jpeg_size_720p, rx_buf_720p, &out_size_720p));
// Write two pictures.
FILE *file_rgb_1080p = fopen(raw_file_1080, "wb");
ESP_LOGI(TAG, "raw_file_1080:%s", raw_file_1080);
if (file_rgb_1080p == NULL) {
ESP_LOGE(TAG, "fopen file_rgb_1080p error");
return;
}
fwrite(rx_buf_1080p, 1, out_size_1080p, file_rgb_1080p);
fclose(file_rgb_1080p);
FILE *file_rgb_720p = fopen(raw_file_720, "wb");
ESP_LOGI(TAG, "raw_file_720:%s", raw_file_720);
if (file_rgb_720p == NULL) {
ESP_LOGE(TAG, "fopen file_rgb_720p error");
return;
}
fwrite(rx_buf_720p, 1, out_size_720p, file_rgb_720p);
fclose(file_rgb_720p);
sdcard_deinit();
ESP_LOGI(TAG, "Card unmounted");
}

Wyświetl plik

@ -0,0 +1,91 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import argparse
import cv2 as cv
import numpy as np
def open_picture(path): # type: (str) -> list[int]
with open(path, 'rb') as f:
data = f.read()
f.close()
new_data = [int(x) for x in data]
return new_data
def picture_show_rgb888(data, h, w): # type: (list[int], int, int) -> None
data = np.array(data).reshape(h, w, 3).astype(np.uint8)
cv.imshow('data', data)
cv.waitKey()
def picture_show_rgb565(data, h, w): # type: (list[int], int, int) -> None
new_data = [0] * ((len(data) // 2) * 3)
for i in range(len(data)):
if i % 2 != 0:
new_data[3 * (i - 1) // 2 + 2] = (data[i] & 0xf8)
new_data[3 * (i - 1) // 2 + 1] |= (data[i] & 0x7) << 5
else:
new_data[3 * i // 2] = (data[i] & 0x1f) << 3
new_data[3 * i // 2 + 1] |= (data[i] & 0xe0) >> 3
new_data = np.array(new_data).reshape(h, w, 3).astype(np.uint8)
cv.imshow('data', new_data)
cv.waitKey()
def picture_show_gray(data, h, w): # type: (list[int], int, int) -> None
new_data = np.array(data).reshape(h, w, 1).astype(np.uint8)
cv.imshow('data', new_data)
cv.waitKey()
def main(): # type: () -> None
parser = argparse.ArgumentParser(description='which mode need to show')
parser.add_argument(
'--pic_path',
type=str,
help='What is the path of your picture',
required=True)
parser.add_argument(
'--pic_type',
type=str,
help='What type you want to show',
required=True,
choices=['rgb565', 'rgb888', 'gray'])
parser.add_argument(
'--hight',
type=int,
help='the picture hight',
default=480)
parser.add_argument(
'--width',
type=int,
help='the picture width',
default=640)
args = parser.parse_args()
hight = args.hight
width = args.width
data = open_picture(args.pic_path)
if (args.pic_type == 'rgb565'):
picture_show_rgb565(data, hight, width)
elif (args.pic_type == 'rgb888'):
picture_show_rgb888(data, hight, width)
elif (args.pic_type == 'gray'):
picture_show_gray(data, hight, width)
else:
print('This type is not supported in this script!')
if __name__ == '__main__':
main()

Wyświetl plik

@ -0,0 +1,2 @@
opencv-python
numpy

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 48 KiB

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 24 KiB

Wyświetl plik

@ -0,0 +1,6 @@
# SPIRAM configurations
CONFIG_IDF_EXPERIMENTAL_FEATURES=y
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_HEX=y
CONFIG_SPIRAM_SPEED_200M=y