|
| 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.) |
0 commit comments