Skip to content

Commit 38e8569

Browse files
avoid extract malloc
1 parent 174edcc commit 38e8569

File tree

3 files changed

+220
-251
lines changed

3 files changed

+220
-251
lines changed

examples/ImgViewer/ImgViewerMjpeg_ESP32P4/MjpegClass.h

Lines changed: 73 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,17 @@
77
#pragma once
88

99
#if defined(ESP32)
10-
11-
#define READ_BUFFER_SIZE 1024
12-
1310
#include <driver/jpeg_decode.h>
1411

12+
#define READ_BATCH_SIZE 1024
13+
1514
class MjpegClass
1615
{
1716
public:
1817
bool setup(const char *path)
1918
{
2019
_input = fopen(path, "r");
21-
_inputindex = 0;
22-
23-
if (!_read_buf)
24-
{
25-
_read_buf = (uint8_t *)malloc(READ_BUFFER_SIZE);
26-
}
27-
28-
if (!_read_buf)
29-
{
30-
return false;
31-
}
20+
_read = 0;
3221

3322
jpeg_decode_engine_cfg_t decode_eng_cfg = {
3423
.intr_priority = 0,
@@ -62,106 +51,112 @@ class MjpegClass
6251

6352
bool readMjpegBuf()
6453
{
65-
if (_inputindex == 0)
54+
if (_read == 0)
6655
{
67-
_buf_read = fread(_read_buf, 1, READ_BUFFER_SIZE, _input);
68-
_inputindex += _buf_read;
56+
// _mjpeg_buf empty
57+
_read = fread(_mjpeg_buf, 1, READ_BATCH_SIZE, _input);
6958
}
70-
_mjpeg_buf_offset = 0;
71-
int i = 0;
59+
else
60+
{
61+
// pad previous remain data to the start of _mjpeg_buf
62+
memcpy(_mjpeg_buf, _p, _read);
63+
}
64+
7265
bool found_FFD8 = false;
73-
while ((_buf_read > 0) && (!found_FFD8))
66+
_p = _mjpeg_buf;
67+
while ((_read > 0) && (!found_FFD8))
7468
{
75-
i = 0;
76-
while ((i < _buf_read) && (!found_FFD8))
69+
while ((_read > 1) && (!found_FFD8))
7770
{
78-
if ((_read_buf[i] == 0xFF) && (_read_buf[i + 1] == 0xD8)) // JPEG header
71+
--_read;
72+
if ((*_p++ == 0xFF) && (*_p == 0xD8)) // JPEG header
7973
{
8074
// Serial.printf("Found FFD8 at: %d.\n", i);
8175
found_FFD8 = true;
8276
}
83-
++i;
8477
}
85-
if (found_FFD8)
78+
if (!found_FFD8)
8679
{
87-
--i;
88-
}
89-
else
90-
{
91-
_buf_read = fread(_read_buf, 1, READ_BUFFER_SIZE, _input);
80+
if (*_p == 0xFF)
81+
{
82+
_mjpeg_buf[0] = 0xFF;
83+
_read = fread(_mjpeg_buf + 1, 1, READ_BATCH_SIZE, _input) + 1;
84+
}
85+
else
86+
{
87+
_read = fread(_mjpeg_buf, 1, READ_BATCH_SIZE, _input);
88+
}
89+
_p = _mjpeg_buf;
9290
}
9391
}
94-
uint8_t *_p = _read_buf + i;
95-
_buf_read -= i;
92+
93+
if (!found_FFD8)
94+
{
95+
return false;
96+
}
97+
98+
// rewind 1 byte
99+
--_p;
100+
++_read;
101+
102+
// pad JPEG header to the start of _mjpeg_buf
103+
if (_p > _mjpeg_buf)
104+
{
105+
Serial.println("(_p > _mjpeg_buf)");
106+
memcpy(_mjpeg_buf, _p, _read);
107+
}
108+
109+
// skip JPEG header
110+
_p += 2;
111+
_read -= 2;
112+
113+
if (_read == 0)
114+
{
115+
_read = fread(_p, 1, READ_BATCH_SIZE, _input);
116+
}
117+
96118
bool found_FFD9 = false;
97-
if (_buf_read > 0)
119+
while ((_read > 0) && (!found_FFD9))
98120
{
99-
i = 3;
100-
while ((_buf_read > 0) && (!found_FFD9))
121+
while ((_read > 1) && (!found_FFD9))
101122
{
102-
if ((_mjpeg_buf_offset > 0) && (_mjpeg_buf[_mjpeg_buf_offset - 1] == 0xFF) && (_p[0] == 0xD9)) // JPEG trailer
123+
--_read;
124+
if ((*_p++ == 0xFF) && (*_p == 0xD9)) // JPEG trailer
103125
{
104126
// Serial.printf("Found FFD9 at: %d.\n", i);
105127
found_FFD9 = true;
106128
}
107-
else
108-
{
109-
while ((i < _buf_read) && (!found_FFD9))
110-
{
111-
if ((_p[i] == 0xFF) && (_p[i + 1] == 0xD9)) // JPEG trailer
112-
{
113-
found_FFD9 = true;
114-
++i;
115-
}
116-
++i;
117-
}
118-
}
119-
120-
// Serial.printf("i: %d\n", i);
121-
memcpy(_mjpeg_buf + _mjpeg_buf_offset, _p, i);
122-
_mjpeg_buf_offset += i;
123-
size_t o = _buf_read - i;
124-
if (o > 0)
125-
{
126-
// Serial.printf("o: %d\n", o);
127-
memcpy(_read_buf, _p + i, o);
128-
_buf_read = fread(_read_buf + o, 1, READ_BUFFER_SIZE - o, _input);
129-
_p = _read_buf;
130-
_inputindex += _buf_read;
131-
_buf_read += o;
132-
// Serial.printf("_buf_read: %d\n", _buf_read);
133-
}
134-
else
135-
{
136-
_buf_read = fread(_read_buf, 1, READ_BUFFER_SIZE, _input);
137-
_p = _read_buf;
138-
_inputindex += _buf_read;
139-
}
140-
i = 0;
141129
}
142-
if (found_FFD9)
130+
131+
if (!found_FFD9)
143132
{
144-
return true;
133+
_read += fread(_p + _read, 1, READ_BATCH_SIZE, _input);
134+
// Serial.printf("_read: %d\n", _read - 1);
145135
}
146136
}
147137

138+
if (found_FFD9)
139+
{
140+
++_p;
141+
--_read;
142+
return true;
143+
}
144+
148145
return false;
149146
}
150147

151148
bool decodeJpg()
152149
{
153-
_remain = _mjpeg_buf_offset;
154-
155150
jpeg_decode_picture_info_t header_info;
156-
ESP_ERROR_CHECK(jpeg_decoder_get_info(_mjpeg_buf, _remain, &header_info));
151+
ESP_ERROR_CHECK(jpeg_decoder_get_info(_mjpeg_buf, _p - _mjpeg_buf, &header_info));
157152
_w = header_info.width;
158153
_h = header_info.height;
159154
uint32_t out_size;
160155
jpeg_decode_cfg_t decode_cfg_rgb = {
161156
.output_format = JPEG_DECODE_OUT_FORMAT_RGB565,
162157
.rgb_order = JPEG_DEC_RGB_ELEMENT_ORDER_BGR,
163158
};
164-
ESP_ERROR_CHECK(jpeg_decoder_process(_decoder_engine, &decode_cfg_rgb, (const uint8_t *)_mjpeg_buf, _remain, (uint8_t *)_output_buf, MJPEG_OUTPUT_SIZE, &out_size));
159+
ESP_ERROR_CHECK(jpeg_decoder_process(_decoder_engine, &decode_cfg_rgb, (const uint8_t *)_mjpeg_buf, _p - _mjpeg_buf, (uint8_t *)_output_buf, MJPEG_OUTPUT_SIZE, &out_size));
165160

166161
return true;
167162
}
@@ -191,16 +186,11 @@ class MjpegClass
191186
uint8_t *_mjpeg_buf;
192187
uint16_t *_output_buf;
193188

194-
uint8_t *_read_buf;
195-
int32_t _mjpeg_buf_offset = 0;
196-
197189
jpeg_decoder_handle_t _decoder_engine;
198-
199190
int16_t _w = 0, _h = 0;
200191

201-
int32_t _inputindex = 0;
202-
int32_t _buf_read;
203-
int32_t _remain = 0;
192+
uint8_t *_p;
193+
int32_t _read;
204194
};
205195

206196
#endif // defined(ESP32)

0 commit comments

Comments
 (0)