This commit is contained in:
Rene Zeldenthuis
2024-02-13 19:15:27 +01:00
parent 26d1af2f28
commit 03582b83cb

View File

@@ -20,23 +20,20 @@ std::tuple<const uint8_t *, size_t> micro_rtsp_jpeg::find_jpeg_section(uint8_t *
// framing = 0xff, flag, len MSB, len LSB // framing = 0xff, flag, len MSB, len LSB
auto flag_code = *(*ptr++); auto flag_code = *(*ptr++);
// SOI and EOI have no length // Length of section
len = flag_code == 0xd8 || flag_code == 0xd9 ? 0 : *(*ptr++) * 256 + *(*ptr++); len = *(*ptr++) * 256 + *(*ptr++);
if (flag_code == flag) if (flag_code == flag)
return std::tuple<const uint8_t *, size_t>(*ptr, len); return std::tuple<const uint8_t *, size_t>(*ptr, len);
// Skip the section // Skip the section
switch (flag_code) switch (flag_code)
{ {
case 0xd8: // SOI (start of image)
case 0xd9: // EOI (end of image)
case 0xe0: // APP00 case 0xe0: // APP00
case 0xdb: // DQT (define quantization table) case 0xdb: // DQT (define quantization table)
case 0xc4: // DHT (define Huffman table) case 0xc4: // DHT (define Huffman table)
case 0xc0: // SOF0 (start of frame) case 0xc0: // SOF0 (start of frame)
case 0xda: // SOS (start of scan) case 0xda: // SOS (start of scan)
{ {
// length of section
log_d("Skipping jpeg section flag: 0x%02x, %d bytes", flag_code, len); log_d("Skipping jpeg section flag: 0x%02x, %d bytes", flag_code, len);
ptr += len; ptr += len;
break; break;
@@ -58,8 +55,7 @@ bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
auto end = ptr + size; auto end = ptr + size;
// Check for SOI (start of image) 0xff, 0xd8 // Check for SOI (start of image) 0xff, 0xd8
auto soi = find_jpeg_section(&ptr, end, 0xd8); if (*(ptr++) != 0xff || *(ptr++) != 0xd8)
if (std::get<0>(soi) == nullptr)
{ {
log_e("No valid start of image marker found"); log_e("No valid start of image marker found");
return false; return false;
@@ -76,7 +72,7 @@ bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
quantization_table_1_ = find_jpeg_section(&ptr, end, 0xdb); quantization_table_1_ = find_jpeg_section(&ptr, end, 0xdb);
if (std::get<0>(quantization_table_1_) == nullptr) if (std::get<0>(quantization_table_1_) == nullptr)
{ {
log_e("No quantization table 1 section found"); log_w("No quantization table 1 section found");
} }
// Start of scan // Start of scan
auto sos = find_jpeg_section(&ptr, end, 0xda); auto sos = find_jpeg_section(&ptr, end, 0xda);
@@ -85,23 +81,21 @@ bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
log_e("No start of scan section found"); log_e("No start of scan section found");
} }
// Start of the data sections
// the scan data uses byte stuffing to guarantee anything that starts with 0xff auto start = ptr;
// followed by something not zero, is a new section. Look for that marker and return the ptr // Scan over all the sections. 0xff followed by not zero, is a new section
// pointing there
// Skip the scan
while (ptr < end - 1 && (*ptr != 0xff || ptr[1] == 0)) while (ptr < end - 1 && (*ptr != 0xff || ptr[1] == 0))
ptr++; ptr++;
ptr -= 2; // Go back to the 0xff (marker) // Go back tgo start of section
ptr--;
auto eoi = find_jpeg_section(&ptr, end, 0xd9); // Check if marker is an end of image marker
if (std::get<0>(eoi) == nullptr) if (*(ptr++) != 0xff || *(ptr++) != 0xd9)
{ {
log_e("No end of image marker found"); log_e("No end of image marker found");
} }
jpeg_data_ = std::tuple<const uint8_t *, size_t>(ptr, size - (ptr - data)); jpeg_data_ = std::tuple<const uint8_t *, size_t>(start, size - (ptr - start - 2));
return true; return true;
} }