diff --git a/lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp b/lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp index 2391d16..707899e 100644 --- a/lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp +++ b/lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp @@ -20,23 +20,20 @@ std::tuple 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(*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; @@ -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; @@ -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); @@ -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(ptr, size - (ptr - data)); + jpeg_data_ = std::tuple(start, size - (ptr - start - 2)); return true; }