Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rzeldent committed Feb 13, 2024
1 parent 26d1af2 commit 03582b8
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
auto flag_code = *(*ptr++);
// SOI and EOI have no length
len = flag_code == 0xd8 || flag_code == 0xd9 ? 0 : *(*ptr++) * 256 + *(*ptr++);
// Length of section
len = *(*ptr++) * 256 + *(*ptr++);
if (flag_code == flag)
return std::tuple<const uint8_t *, size_t>(*ptr, len);

// Skip the section
switch (flag_code)
{
case 0xd8: // SOI (start of image)
case 0xd9: // EOI (end of image)
case 0xe0: // APP00
case 0xdb: // DQT (define quantization table)
case 0xc4: // DHT (define Huffman table)
case 0xc0: // SOF0 (start of frame)
case 0xda: // SOS (start of scan)
{
// length of section
log_d("Skipping jpeg section flag: 0x%02x, %d bytes", flag_code, len);
ptr += len;
break;
Expand All @@ -58,8 +55,7 @@ bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
auto end = ptr + size;

// Check for SOI (start of image) 0xff, 0xd8
auto soi = find_jpeg_section(&ptr, end, 0xd8);
if (std::get<0>(soi) == nullptr)
if (*(ptr++) != 0xff || *(ptr++) != 0xd8)
{
log_e("No valid start of image marker found");
return false;
Expand All @@ -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);
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
auto sos = find_jpeg_section(&ptr, end, 0xda);
Expand All @@ -85,23 +81,21 @@ bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
log_e("No start of scan section found");
}


// the scan data uses byte stuffing to guarantee anything that starts with 0xff
// followed by something not zero, is a new section. Look for that marker and return the ptr
// pointing there

// Skip the scan
// Start of the data sections
auto start = ptr;
// Scan over all the sections. 0xff followed by not zero, is a new section
while (ptr < end - 1 && (*ptr != 0xff || ptr[1] == 0))
ptr++;

ptr -= 2; // Go back to the 0xff (marker)
// Go back tgo start of section
ptr--;

auto eoi = find_jpeg_section(&ptr, end, 0xd9);
if (std::get<0>(eoi) == nullptr)
// Check if marker is an end of image marker
if (*(ptr++) != 0xff || *(ptr++) != 0xd9)
{
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;
}

0 comments on commit 03582b8

Please sign in to comment.