feat(jpeg_encoder): Add the example support for jpeg encoder

This commit is contained in:
Cao Sen Miao 2024-04-01 20:01:06 +08:00
parent 58acf93da2
commit e8e7166a4c
8 changed files with 318 additions and 0 deletions

View File

@ -126,6 +126,12 @@ examples/peripherals/jpeg/jpeg_decode:
depends_components:
- esp_driver_jpeg
examples/peripherals/jpeg/jpeg_encode:
disable:
- if: SOC_JPEG_ENCODE_SUPPORTED != 1
depends_components:
- esp_driver_jpeg
examples/peripherals/lcd/i2c_oled:
disable:
- if: SOC_I2C_SUPPORTED != 1

View File

@ -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_encode)

View File

@ -0,0 +1,56 @@
| Supported Targets | ESP32-P4 |
| ----------------- | -------- |
# JPEG encode example
## Overview
This example demonstrates how to use the JPEG hardware [encoder](https://docs.espressif.com/projects/esp-idf/en/latest/esp32p4/api-reference/peripherals/jpeg.html) to encode a 1080p picture:
This example makes use of the hardware-based JPEG encoder. If you have multiple pictures that need to be decoded, such as *.rgb -> *.jpg, you can use this example to accelerate encoding.
## How to use example
### Hardware Required
* An Espressif development board based on a chip listed in supported targets
* A USB cable for power supply and serial communication
* Computer with ESP-IDF installed and configured
* The raw picture is the only source that you need to prepare (We have an [esp1080p.rgb](https://github.com/espressif/esp-idf/tree/master/examples/peripherals/jpeg/jpeg_encode/resources/esp1080.rgb) in resources folder, you can also get it from [jpeg_decode](https://github.com/espressif/esp-idf/tree/master/examples/peripherals/jpeg/jpeg_decode) example).
### Build and Flash
Before you start build and flash this example, please put the image `esp1080.rgb` 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 (1114) jpeg.example: Initializing SD card
I (1114) gpio: GPIO[43]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1124) gpio: GPIO[44]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1134) gpio: GPIO[39]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1144) gpio: GPIO[40]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1154) gpio: GPIO[41]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (1164) gpio: GPIO[42]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (1414) 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 (1434) jpeg.example: infile_1080p:/sdcard/esp1080.rgb
I (5174) jpeg.example: outfile:/sdcard/outjpg.jpg
I (5284) jpeg.example: Card unmounted
I (5284) main_task: Returned from app_main()
```
## 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.)

View File

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

View File

@ -0,0 +1,9 @@
menu "JPEG Encode 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

View File

@ -0,0 +1,136 @@
/*
* 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 "driver/jpeg_encode.h"
static const char *TAG = "jpeg.example";
static sdmmc_card_t *s_card;
#define MOUNT_POINT "/sdcard"
const static char s_infile_1080p[] = "/sdcard/esp1080.rgb";
const static char s_outfile_1080p[] = "/sdcard/outjpg.jpg";
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());
uint32_t raw_size_1080p;
uint32_t jpg_size_1080p;
jpeg_encoder_handle_t jpeg_handle;
FILE *file_raw_1080p = fopen(s_infile_1080p, "rb");
ESP_LOGI(TAG, "s_infile_1080p:%s", s_infile_1080p);
if (file_raw_1080p == NULL) {
ESP_LOGE(TAG, "fopen file_raw_1080p error");
return;
}
jpeg_encode_engine_cfg_t encode_eng_cfg = {
.timeout_ms = 70,
};
jpeg_encode_memory_alloc_cfg_t rx_mem_cfg = {
.buffer_direction = JPEG_DEC_ALLOC_OUTPUT_BUFFER,
};
jpeg_encode_memory_alloc_cfg_t tx_mem_cfg = {
.buffer_direction = JPEG_DEC_ALLOC_INPUT_BUFFER,
};
jpeg_new_encoder_engine(&encode_eng_cfg, &jpeg_handle);
// Read 1080p raw picture
fseek(file_raw_1080p, 0, SEEK_END);
raw_size_1080p = ftell(file_raw_1080p);
fseek(file_raw_1080p, 0, SEEK_SET);
size_t tx_buffer_size = 0;
uint8_t *raw_buf_1080p = (uint8_t*)jpeg_alloc_encoder_mem(raw_size_1080p, &tx_mem_cfg, &tx_buffer_size);
if (raw_buf_1080p == NULL) {
ESP_LOGE(TAG, "alloc 1080p tx buffer error");
return;
}
fread(raw_buf_1080p, 1, raw_size_1080p, file_raw_1080p);
fclose(file_raw_1080p);
size_t rx_buffer_size = 0;
uint8_t *jpg_buf_1080p = (uint8_t*)jpeg_alloc_encoder_mem(raw_size_1080p / 10, &rx_mem_cfg, &rx_buffer_size); // Assume that compression ratio of 10 to 1
if (jpg_buf_1080p == NULL) {
ESP_LOGE(TAG, "alloc jpg_buf_1080p error");
return;
}
jpeg_encode_cfg_t enc_config = {
.src_type = JPEG_ENCODE_IN_FORMAT_RGB888,
.sub_sample = JPEG_DOWN_SAMPLING_YUV422,
.image_quality = 80,
.width = 1920,
.height = 1080,
};
jpeg_encoder_process(jpeg_handle, &enc_config, raw_buf_1080p, raw_size_1080p, jpg_buf_1080p, rx_buffer_size, &jpg_size_1080p);
FILE *file_jpg_1080p = fopen(s_outfile_1080p, "wb");
ESP_LOGI(TAG, "outfile:%s", s_outfile_1080p);
if (file_jpg_1080p == NULL) {
ESP_LOGE(TAG, "fopen file_jpg_1080p error");
return;
}
fwrite(jpg_buf_1080p, 1, jpg_size_1080p, file_jpg_1080p);
fclose(file_jpg_1080p);
sdcard_deinit();
ESP_LOGI(TAG, "Card unmounted");
}

File diff suppressed because one or more lines are too long

View File

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