Skip to content

Commit 3785a24

Browse files
committed
Merge branch 'feat/add_h264_encoder_decoder_demo' into 'master'
feat(esp_h264): Add esp_h264 enocder and decoder example Closes AUD-6406 See merge request espressif/esp-idf!39676
2 parents f05d47c + fa54de7 commit 3785a24

13 files changed

+971
-0
lines changed

examples/peripherals/.build-test-rules.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ examples/peripherals/gpio/matrix_keyboard:
8787
enable:
8888
- if: IDF_TARGET == "esp32s2"
8989

90+
examples/peripherals/h264:
91+
enable:
92+
- if: IDF_TARGET in ["esp32p4", "esp32s3"]
93+
reason: only supports esp32p4 and esp32s3
94+
depends_components:
95+
- esp_h264
96+
9097
examples/peripherals/i2c/i2c_basic:
9198
disable:
9299
- if: SOC_I2C_SUPPORTED != 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists.txt file.
3+
4+
cmake_minimum_required(VERSION 3.16)
5+
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
project(esp_h264_example)
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
| Supported Targets | ESP32-P4 | ESP32-S3 |
2+
| ----------------- | -------- | -------- |
3+
4+
# H.264 Encoder-Decoder Example
5+
6+
## Overview
7+
8+
This example demonstrates how to use H.264 hardware/software encoder and decoder with visual pattern generation:
9+
10+
- Generate colorful test patterns for video processing
11+
- Encode video frames using H.264 codec (hardware on ESP32-P4, software on ESP32-S3)
12+
- Decode the encoded frames back to original format using software decoder
13+
- Display visual comparison between source and decoded images
14+
15+
The example supports multiple YUV formats and provides side-by-side colorized display in the console. All encoding parameters are configurable through the ESP-IDF menuconfig system.
16+
17+
## Configuration
18+
19+
This example provides comprehensive configuration options through `idf.py menuconfig`:
20+
21+
### H.264 Encoder Type Selection
22+
- **Hardware Encoder**: Available only on ESP32-P4, provides better performance and lower power consumption
23+
- **Software Encoder**: Available on all targets (ESP32-S3, ESP32-P4), uses more CPU resources
24+
25+
### Configurable Parameters
26+
All parameters can be adjusted in "H.264 Example Configuration" menu:
27+
28+
- **Video Width**: 64-1920 pixels (default: 320)
29+
- **Video Height**: 64-1080 pixels (default: 240)
30+
- **Frame Rate**: 1-60 fps (default: 30 for hardware, 15 for software)
31+
- **Bitrate**: 64K-10M bps (default: 512K for hardware, 256K for software)
32+
- **GOP Size**: 1-255 frames (default: 30)
33+
- **QP Value**: 10-51 (default: 26 for hardware, 28 for software)
34+
35+
### Target-Specific Defaults
36+
- **ESP32-P4**: Optimized for hardware encoding with higher performance settings
37+
- **ESP32-S3**: Optimized for software encoding with conservative settings
38+
39+
## How to use example
40+
41+
### Prerequisites Required
42+
43+
This example requires:
44+
- ESP32-P4 development board (for hardware encoding) or ESP32-S3 development board (for software encoding)
45+
- USB cable for programming and power supply
46+
- Terminal that supports ANSI color codes for proper visual output
47+
48+
### Configure the Example
49+
50+
Before building, configure the example parameters:
51+
52+
```bash
53+
idf.py menuconfig
54+
```
55+
56+
Navigate to: `Component config``H.264 Example Configuration`
57+
58+
1. **Select Encoder Type**: Choose between Hardware (ESP32-P4 only) or Software encoder
59+
2. **Adjust Parameters**: Configure video resolution, frame rate, bitrate, etc.
60+
3. **Save and Exit**: Press 'S' to save configuration
61+
62+
### Build and Flash
63+
64+
For ESP32-P4 (with hardware encoding support):
65+
```bash
66+
idf.py set-target esp32p4
67+
idf.py menuconfig # Configure as needed
68+
idf.py build
69+
idf.py -p PORT flash monitor
70+
```
71+
72+
For ESP32-S3 (software encoding only):
73+
```bash
74+
idf.py set-target esp32s3
75+
idf.py menuconfig # Software encoder will be automatically selected
76+
idf.py build
77+
idf.py -p PORT flash monitor
78+
```
79+
80+
(To exit the serial monitor, type ``Ctrl-]``.)
81+
82+
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.
83+
84+
## Example Output
85+
86+
```bash
87+
I (1555) H264_ENC_DEC: H264 Example starting: 320x240 @ 30fps
88+
I (1565) H264_ENC_DEC: Encoder: Hardware, Decoder: Software
89+
I (1575) H264_ENC_DEC: Config: GOP=30, Bitrate=512000 bps, QP=26
90+
I (1585) H264_DEC.SW: tinyh264 version: 1c7f584
91+
I (1585) H264_ENC_DEC: H264 encode-decode loop started (320x240 @ 30fps)
92+
93+
Frame 0: source image | decoded image
94+
[Colorized bar patterns displayed side by side using ANSI colors]
95+
96+
Frame 1: source image | decoded image
97+
[Colorized bar patterns displayed side by side using ANSI colors]
98+
99+
Frame 2: source image | decoded image
100+
[Colorized bar patterns displayed side by side using ANSI colors]
101+
...
102+
103+
I (21465) H264_ENC_DEC: H264 process Completed successfully
104+
I (21475) main_task: Returned from app_main()
105+
```
106+
107+
*Note: The exact values shown will depend on your menuconfig settings.*
108+
109+
## Video Format Support
110+
111+
- **ESP_H264_RAW_FMT_I420**: Planar YUV 4:2:0 format (decoder output, software encoder input)
112+
- **ESP_H264_RAW_FMT_O_UYY_E_VYY**: Interlaced YUV format (hardware encoder input on ESP32-P4)
113+
114+
## Performance Recommendations
115+
116+
### For ESP32-P4 (Hardware Encoding):
117+
- Resolution: Up to 1920x1080 supported
118+
- Frame Rate: 30-60 fps achievable
119+
- Bitrate: 512K-5M bps recommended
120+
- QP: 20-30 for optimal quality/performance balance
121+
122+
### For ESP32-S3 (Software Encoding):
123+
- Resolution: 320x240 or smaller recommended
124+
- Frame Rate: 10-15 fps for stable performance
125+
- Bitrate: 256K-1M bps recommended
126+
- QP: 28-35 for better performance
127+
128+
## Troubleshooting
129+
130+
**Configuration Issues:**
131+
- Use `idf.py menuconfig` to verify H.264 settings before building
132+
- Ensure hardware encoder is only selected for ESP32-P4 target
133+
134+
**Memory allocation failures:**
135+
- Reduce resolution or frame rate in menuconfig
136+
- Ensure sufficient SPIRAM is available
137+
- Check ESP-IDF memory configuration
138+
139+
**Encoding/decoding errors:**
140+
- Verify the correct target is selected (ESP32-P4 for hardware)
141+
- Check that H.264 component is properly configured in menuconfig
142+
- Adjust bitrate settings for your resolution/frame rate combination
143+
144+
**Performance Issues:**
145+
- Lower resolution, frame rate, or bitrate for software encoding
146+
- Use hardware encoder on ESP32-P4 for better performance
147+
- Increase QP value to reduce computational load
148+
149+
**Visual output issues:**
150+
- Ensure your terminal supports ANSI color codes
151+
- Try different terminal applications if colors don't display properly
152+
153+
(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.)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRC_DIRS "./"
2+
INCLUDE_DIRS "./")
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
menu "H.264 Example Configuration"
2+
3+
choice H264_ENCODER_TYPE
4+
prompt "H.264 Encoder Type"
5+
default H264_ENCODER_HARDWARE if IDF_TARGET_ESP32P4
6+
default H264_ENCODER_SOFTWARE
7+
help
8+
Select the H.264 encoder type to use.
9+
Hardware encoder is only available on ESP32P4 and provides
10+
better performance and lower power consumption.
11+
Software encoder is available on all targets but requires
12+
more CPU resources.
13+
14+
config H264_ENCODER_HARDWARE
15+
bool "Hardware Encoder (ESP32P4 only)"
16+
depends on IDF_TARGET_ESP32P4
17+
help
18+
Use hardware H.264 encoder.
19+
This option is only available on ESP32P4 which has
20+
dedicated H.264 hardware encoding capabilities.
21+
Provides better performance and lower power consumption
22+
compared to software encoding.
23+
24+
config H264_ENCODER_SOFTWARE
25+
bool "Software Encoder"
26+
help
27+
Use software H.264 encoder using OpenH264 library.
28+
Available on all supported targets (ESP32S3, ESP32P4)
29+
but requires more CPU resources and power consumption
30+
compared to hardware encoding.
31+
endchoice
32+
33+
menu "H.264 Encoder Parameters"
34+
35+
config H264_ENCODER_WIDTH
36+
int "Video Width"
37+
range 64 1920
38+
default 320
39+
help
40+
Video frame width in pixels.
41+
Must be multiple of 16 for optimal performance.
42+
Recommended values: 128, 160, 320, 640, 1280.
43+
44+
config H264_ENCODER_HEIGHT
45+
int "Video Height"
46+
range 64 1080
47+
default 240
48+
help
49+
Video frame height in pixels.
50+
Must be multiple of 16 for optimal performance.
51+
Recommended values: 96, 120, 240, 480, 720.
52+
53+
config H264_ENCODER_FPS
54+
int "Frame Rate (FPS)"
55+
range 1 60
56+
default 30 if H264_ENCODER_HARDWARE
57+
default 15 if H264_ENCODER_SOFTWARE
58+
help
59+
Video frame rate in frames per second.
60+
Hardware encoder can support higher frame rates.
61+
Software encoder performance depends on CPU capability.
62+
63+
config H264_ENCODER_BITRATE
64+
int "Bitrate (bps)"
65+
range 64000 10000000
66+
default 512000 if H264_ENCODER_HARDWARE
67+
default 256000 if H264_ENCODER_SOFTWARE
68+
help
69+
Video bitrate in bits per second.
70+
Higher bitrate provides better quality but larger file size.
71+
Hardware encoder can handle higher bitrates more efficiently.
72+
Typical values: 256K-1M for low quality, 1M-5M for high quality.
73+
74+
config H264_ENCODER_GOP_SIZE
75+
int "GOP Size"
76+
range 1 255
77+
default 30
78+
help
79+
Group of Pictures size. Determines the frequency
80+
of I-frames in the video stream.
81+
Larger GOP size = better compression, higher latency.
82+
Smaller GOP size = lower compression, lower latency.
83+
84+
config H264_ENCODER_QP_VALUE
85+
int "Quantization Parameter (QP)"
86+
range 10 51
87+
default 26 if H264_ENCODER_HARDWARE
88+
default 28 if H264_ENCODER_SOFTWARE
89+
help
90+
Quantization parameter that controls video quality.
91+
Lower values = higher quality, larger file size.
92+
Higher values = lower quality, smaller file size.
93+
Hardware encoder can handle lower QP values more efficiently.
94+
95+
endmenu
96+
endmenu

0 commit comments

Comments
 (0)