H.264 Codec for ESP32-S3
Loading...
Searching...
No Matches
esp_h264_types.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#pragma once
8
9#include <stdint.h>
10#include <stdbool.h>
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#define ESP_H264_QP_MAX (51) /*<! H264 support maximum quantization parameter (QP) */
17#define ESP_H264_4CC(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
18
19/**
20 * @brief The given code snippet defines a structure called `esp_h264_err_t`
21 * It is an enumeration that represents different error codes for H.264 video encoding operations
22 */
23typedef enum {
24 ESP_H264_ERR_OK = 0, /*<! Succeeded */
25 ESP_H264_ERR_FAIL = -1, /*<! Failed */
26 ESP_H264_ERR_ARG = -2, /*<! Invalid arguments */
27 ESP_H264_ERR_MEM = -3, /*<! Insufficient memory */
28 ESP_H264_ERR_UNSUPPORTED = -5, /*<! Un-supported */
29 ESP_H264_ERR_TIMEOUT = -6, /*<! Timeout */
30 ESP_H264_ERR_OVERFLOW = -7, /*<! Buffer overflow */
31} esp_h264_err_t;
32
33/**
34 * @brief This is an unencoded data format
35 *
36 * @note
37 * |-------------------------------|--------------|--------------|--------------|
38 * | enum | SW encoder | HW encoder | SW decoder |
39 * |-------------------------------|--------------|--------------|--------------|
40 * | ESP_H264_RAW_FMT_BGR888 | un-supported| supported | un-supported |
41 * |-------------------------------|--------------|--------------|--------------|
42 * | ESP_H264_RAW_FMT_RGB565_LE | un-supported| supported | un-supported |
43 * |-------------------------------|--------------|--------------|--------------|
44 * | ESP_H264_RAW_FMT_VUY | un-supported| supported | un-supported |
45 * |-------------------------------|--------------|--------------|--------------|
46 * | ESP_H264_RAW_FMT_UYVY | un-supported| supported | un-supported |
47 * |-------------------------------|--------------|--------------|--------------|
48 * | ESP_H264_RAW_FMT_YUYV | supported | un-supported | un-supported |
49 * |-------------------------------|--------------|--------------|--------------|
50 * | ESP_H264_RAW_FMT_I420 | supported | un-supported | supported |
51 * |-------------------------------|--------------|--------------|--------------|
52 * | ESP_H264_RAW_FMT_O_UYY_E_VYY | un-supported | supported | un-supported |
53 * |-------------------------------|--------------|--------------|--------------|
54 */
55typedef enum {
56 ESP_H264_RAW_FMT_BGR888 = ESP_H264_4CC('B', 'G', 'R', '3'), /*<! BGR888 */
57 ESP_H264_RAW_FMT_RGB565_LE = ESP_H264_4CC('R', 'G', 'B', 'L'), /*<! RGB565 little endian */
58 ESP_H264_RAW_FMT_VUY = ESP_H264_4CC('Y', '3', '0', '8'), /*<! VUY */
59 ESP_H264_RAW_FMT_UYVY = ESP_H264_4CC('U', 'Y', 'V', 'Y'), /*<! UYVY */
60 ESP_H264_RAW_FMT_YUYV = ESP_H264_4CC('Y', 'U', 'Y', 'V'), /*<! The storage format is YUV422 packet. The data order is Y U Y V... for per line */
61 ESP_H264_RAW_FMT_I420 = ESP_H264_4CC('Y', 'U', '1', '2'), /*<! The storage format is YUV420 planar(IYUV). The data order is to store all Y first, then all U, and finally all V */
62 ESP_H264_RAW_FMT_O_UYY_E_VYY = ESP_H264_4CC('O', 'U', 'E', 'V'), /*<! The storage format is YUV420 packet, the data order is as follows
63 |-------------|-------------------------------|
64 | line number | data order |
65 |-------------|-------------------------------|
66 | 1 | u y y u y y u y y... |
67 |-------------|-------------------------------|
68 | 2 | v y y v y y v y y... |
69 |-------------|-------------------------------|
70 | 3 | u y y u y y u y y... |
71 |-------------|-------------------------------|
72 | 4 | v y y v y y v y y... |
73 |-------------|-------------------------------|
74 | ... | ... |
75 |-------------|-------------------------------|
76 */
77} esp_h264_raw_format_t;
78
79/**
80 * @brief Enumerate video frame type
81 */
82typedef enum {
83 ESP_H264_FRAME_TYPE_INVALID = -1, /*<! Encoder not ready or parameters are invalid */
84 ESP_H264_FRAME_TYPE_IDR = 0, /*<! Instantaneous decoding refresh (IDR) frame
85 IDR frames are essentially I-frames and use intra-frame prediction.
86 IDR frames refresh immediately
87 So IDR frames assume the random access function, a new IDR frame starts,
88 can recalculate a new GOP start encoding, the player can always play from an IDR frame,
89 because after it, no frame references the previous frame.
90 If there is no IDR frame in a video, the video cannot be accessed randomly. */
91 ESP_H264_FRAME_TYPE_I = 1, /*<! Intra frame(I frame) type. If output frame type is this,
92 it means this frame is I-frame except IDR frame. */
93 ESP_H264_FRAME_TYPE_P = 2, /*<! Predicted frame (P-frame) type */
94} esp_h264_frame_type_t;
95
96/**
97 * @brief H.264 Data packet
98 */
99typedef struct {
100 uint8_t *buffer; /*<! Data buffer */
101 uint32_t len; /*<! It is buffer length in bytes */
102} esp_h264_pkt_t;
103
104/**
105 * @brief Picture resolution
106 * In the case of a certain display resolution, the smaller the display, the clearer the picture
107 * On the contrary, when the display size is fixed, the higher the display resolution, the clearer the picture
108 */
109typedef struct {
110 uint16_t width; /*<! Width of picture
111 |---------|-----------------|------------------|
112 | | Software encoder| Hardware encoder |
113 |---------|-----------------|------------------|
114 | maximum | infinite | 1920 |
115 |---------|-----------------|------------------|
116 | minimum | 16 | 80 |
117 |---------|-----------------|------------------|
118 */
119 uint16_t height; /*<! Height of picture
120 |---------|-----------------|------------------|
121 | | Software encoder| Hardware encoder |
122 |---------|-----------------|------------------|
123 | maximum | infinite | 2032 |
124 |---------|-----------------|------------------|
125 | minimum | 16 | 80 |
126 |---------|-----------------|------------------|
127 */
128} esp_h264_resolution_t;
129
130/**
131 * @brief Rate control(RC) parameter
132 * The size of the output stream approaching the target stream is controlled
133 * by using the optimal quantization parameter for each macroblock.
134 */
135typedef struct {
136 uint32_t bitrate; /*<! Target bitrate desired
137 Under normal circumstances, the compression rate of H.264 can exceed 100 times.
138 Therefore, the recommended value is `width` * `height` * `pixel` / `compression ratio`.
139 For network transmission, a smaller value is recommended when the network environment is poor.*/
140 uint8_t qp_min; /*<! Minimum range for quantization parameter(QP). The range is [0, 51].
141 Smaller QP values reduce the compression ratio, increasing video quality.*/
142 uint8_t qp_max; /*<! Maximum range for quantization parameter(QP). The range is [0, 51].
143 Larger QP values increase compression.
144 It must be greater than or equal to `qp_min`.*/
145} esp_h264_enc_rc_t;
146
147/**
148 * @brief Encoder configure information
149 */
150typedef struct {
151 esp_h264_raw_format_t pic_type; /*<! Un-encoding data format */
152 uint8_t gop; /*<! Period of Intra frame
153 GOP is usually set to the number of frames per second(FPS) output by the encoder,
154 that is, the FPS, which is generally 25 or 30, but other values can also be set. */
155 uint8_t fps; /*<! Maximum input frames per second
156 The higher the FPS, the more coherent and realistic the video.
157 24 FPS is standard for videos to appear coherent.
158 30 FPS is standard for video games to appear coherent.
159 FPS greater than 75 are generally imperceptible.*/
160 esp_h264_resolution_t res; /*<! Picture resolution */
161 esp_h264_enc_rc_t rc; /*<! RC parameter */
162} esp_h264_enc_cfg_t;
163
164/**
165 * @brief Data stream information after encoding
166 */
167typedef struct {
168 esp_h264_frame_type_t frame_type; /*<! Frame type */
169 esp_h264_pkt_t raw_data; /*<! Encoded data stream */
170 uint32_t length; /*<! Actual length of encoder data in bytes */
171 uint32_t dts; /*<! Decoding time stamp(DTS)
172 The timestamp of the decoder when decoding relative to SCR(system reference time).
173 It mainly identifies when the bit stream read into memory begins to be sent to the decoder for decoding.*/
174 uint32_t pts; /*<! Presentation time stamp(PTS). The timestamp relative to the SCR when the frame is displayed.
175 It mainly measures when the decoded video is displayed.
176 It is time scale. PTS plus time base is actual time.
177 Commonly time base in TS stream is {1, 90000}. So PTS unit is 1/90000 second.
178 If time base is millisecond, PTS unit is 1 / 1000 second .
179 If H.264 encode data has only I-frame and P-frame, the DTS is equal to PTS.
180 */
181} esp_h264_enc_out_frame_t;
182
183/**
184 * @brief Data stream information before encoding
185 */
186typedef struct {
187 esp_h264_pkt_t raw_data; /*<! Unencoded data stream */
188 uint32_t pts; /*<! Presentation time stamp(PTS) */
189} esp_h264_enc_in_frame_t;
190
191/**
192 * @brief Data stream information after encoding
193 */
194typedef struct {
195 esp_h264_frame_type_t frame_type; /*<! Frame type */
196 uint8_t *outbuf; /*<! Decoder data stream */
197 uint32_t out_size; /*<! Actual length of decoder data in bytes */
198 uint32_t dts; /*<! Decoding time stamp(DTS)
199 The timestamp of the decoder when decoding relative to SCR(system reference time).
200 It mainly identifies when the bit stream read into memory begins to be sent to the decoder for decoding.*/
201 uint32_t pts; /*<! Presentation time stamp(PTS).The timestamp relative to the SCR when the frame is displayed.
202 It mainly measures when the decoded video is displayed.
203 It is time scale. PTS plus time base is actual time.
204 Commonly time base in TS stream is {1, 90000}. So PTS unit is 1/90000 second.
205 If time base is millisecond, PTS unit is 1 / 1000 second .
206 If H.264 encode data has only I-frame and P-frame, the DTS is equal to PTS. */
207} esp_h264_dec_out_frame_t;
208
209/**
210 * @brief Data stream information before encoding
211 *
212 * @note User needs to process the `consume` as follows:
213 *
214 * @code{c}
215 * esp_h264_dec_in_frame_t in_frame = {.raw_data.buffer = buffer, .raw_data.len = len};
216 * esp_h264_dec_out_frame_t out_frame;
217 * while (raw_data.len) {
218 * ret = esp_h264_dec_process(dec, &in_frame, &out_frame);
219 * if (ret != ESP_H264_ERR_OK) {
220 * break;
221 * }
222 * in_frame.raw_data.buffer += in_frame.consume;
223 * in_frame.raw_data.len -= in_frame.consume;
224 * }
225 * @endcode
226 */
227typedef struct {
228 esp_h264_pkt_t raw_data; /*<! Encoded data stream */
229 uint32_t consume; /*<! Actual consumed data length in bytes */
230 uint32_t dts; /*<! Decoding time stamp(DTS)
231 The timestamp of the decoder when decoding relative to SCR(system reference time).
232 It mainly identifies when the bit stream read into memory begins to be sent to the decoder for decoding.*/
233 uint32_t pts; /*<! Presentation time stamp(PTS).The timestamp relative to the SCR when the frame is displayed.
234 It mainly measures when the decoded video is displayed.
235 It is time scale. PTS plus time base is actual time.
236 Commonly time base in TS stream is {1, 90000}. So PTS unit is 1/90000 second.
237 If time base is millisecond, PTS unit is 1 / 1000 second.
238 If H.264 encode data has only I-frame and P-frame, the DTS is equal to PTS.
239 */
240} esp_h264_dec_in_frame_t;
241
242/**
243 * @brief Get bytes per pixel (BPP) by picture type
244 * @param pic_type Picture format type
245 * @return Bytes per pixel value
246 */
247#define ESP_H264_GET_BPP_BY_PIC_TYPE(pic_type)
248 ((pic_type) == ESP_H264_RAW_FMT_I420 || (pic_type) == ESP_H264_RAW_FMT_O_UYY_E_VYY ? 1.5f :
249 (pic_type) == ESP_H264_RAW_FMT_RGB565_LE || (pic_type) == ESP_H264_RAW_FMT_UYVY || (pic_type) == ESP_H264_RAW_FMT_YUYV ? 2.0f :
250 3.0f)
251
252#if !defined(CONFIG_ESP_REV_MIN_FULL) || CONFIG_ESP_REV_MIN_FULL < 300
253
254/**
255 * @brief Check if picture type is supported by hardware (HW Version 3)
256 * @note HW Version 3 only supports ESP_H264_RAW_FMT_O_UYY_E_VYY format
257 * @param pic_type Picture format type to check
258 * @return true if supported, false otherwise
259 */
260#define ESP_H264_HW_IS_SUPPORTED_PIC_TYPE(pic_type)
261 ((pic_type) == ESP_H264_RAW_FMT_O_UYY_E_VYY)
262
263#else
264
265/**
266 * @brief Check if picture type is supported by hardware (HW Version 1/2)
267 * @note HW Version 1/2 supports multiple formats including:
268 * - ESP_H264_RAW_FMT_GREY (Grayscale)
269 * - ESP_H264_RAW_FMT_BGR888 (24-bit RGB)
270 * - ESP_H264_RAW_FMT_RGB565_LE (16-bit RGB Little-Endian)
271 * - ESP_H264_RAW_FMT_VUY (YUV 4:4:4)
272 * - ESP_H264_RAW_FMT_UYVY (YUV 4:2:2)
273 * - ESP_H264_RAW_FMT_O_UYY_E_VYY (Optimized YUV 4:2:0)
274 * @param pic_type Picture format type to check
275 * @return true if supported, false otherwise
276 */
277#define ESP_H264_HW_IS_SUPPORTED_PIC_TYPE(pic_type)
278 ((pic_type) == ESP_H264_RAW_FMT_BGR888 ||
279 (pic_type) == ESP_H264_RAW_FMT_RGB565_LE ||
280 (pic_type) == ESP_H264_RAW_FMT_VUY ||
281 (pic_type) == ESP_H264_RAW_FMT_UYVY ||
282 (pic_type) == ESP_H264_RAW_FMT_O_UYY_E_VYY)
283
284#endif /* defined(CONFIG_ESP_REV_MIN_FULL) && CONFIG_ESP_REV_MIN_FULL < 300 */
285
286#ifdef __cplusplus
287}
288#endif
#define ESP_H264_4CC(a, b, c, d)
Definition: esp_h264_types.h:17
@ ESP_H264_RAW_FMT_I420
Definition: esp_h264_types.h:61
@ ESP_H264_RAW_FMT_O_UYY_E_VYY
Definition: esp_h264_types.h:62
@ ESP_H264_RAW_FMT_YUYV
Definition: esp_h264_types.h:60
@ ESP_H264_RAW_FMT_VUY
Definition: esp_h264_types.h:58
@ ESP_H264_RAW_FMT_BGR888
Definition: esp_h264_types.h:56
@ ESP_H264_RAW_FMT_UYVY
Definition: esp_h264_types.h:59
@ ESP_H264_RAW_FMT_RGB565_LE
Definition: esp_h264_types.h:57
@ ESP_H264_FRAME_TYPE_I
Definition: esp_h264_types.h:91
@ ESP_H264_FRAME_TYPE_INVALID
Definition: esp_h264_types.h:83
@ ESP_H264_FRAME_TYPE_P
Definition: esp_h264_types.h:93
@ ESP_H264_FRAME_TYPE_IDR
Definition: esp_h264_types.h:84
@ ESP_H264_ERR_MEM
Definition: esp_h264_types.h:27
@ ESP_H264_ERR_OVERFLOW
Definition: esp_h264_types.h:30
@ ESP_H264_ERR_ARG
Definition: esp_h264_types.h:26
@ ESP_H264_ERR_OK
Definition: esp_h264_types.h:24
@ ESP_H264_ERR_UNSUPPORTED
Definition: esp_h264_types.h:28
@ ESP_H264_ERR_FAIL
Definition: esp_h264_types.h:25
@ ESP_H264_ERR_TIMEOUT
Definition: esp_h264_types.h:29
esp_h264_pkt_t raw_data
Definition: esp_h264_types.h:228
uint32_t pts
Definition: esp_h264_types.h:233
uint32_t dts
Definition: esp_h264_types.h:230
uint32_t consume
Definition: esp_h264_types.h:229
uint32_t out_size
Definition: esp_h264_types.h:197
uint32_t pts
Definition: esp_h264_types.h:201
uint32_t dts
Definition: esp_h264_types.h:198
uint8_t * outbuf
Definition: esp_h264_types.h:196
esp_h264_frame_type_t frame_type
Definition: esp_h264_types.h:195
esp_h264_resolution_t res
Definition: esp_h264_types.h:160
uint8_t fps
Definition: esp_h264_types.h:155
esp_h264_raw_format_t pic_type
Definition: esp_h264_types.h:151
uint8_t gop
Definition: esp_h264_types.h:152
esp_h264_enc_rc_t rc
Definition: esp_h264_types.h:161
esp_h264_pkt_t raw_data
Definition: esp_h264_types.h:187
uint32_t pts
Definition: esp_h264_types.h:188
esp_h264_pkt_t raw_data
Definition: esp_h264_types.h:169
uint32_t pts
Definition: esp_h264_types.h:174
uint32_t dts
Definition: esp_h264_types.h:171
esp_h264_frame_type_t frame_type
Definition: esp_h264_types.h:168
uint32_t length
Definition: esp_h264_types.h:170
uint32_t bitrate
Definition: esp_h264_types.h:136
uint8_t qp_max
Definition: esp_h264_types.h:142
uint8_t qp_min
Definition: esp_h264_types.h:140
uint8_t * buffer
Definition: esp_h264_types.h:100
uint32_t len
Definition: esp_h264_types.h:101
uint16_t height
Definition: esp_h264_types.h:119
uint16_t width
Definition: esp_h264_types.h:110