Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rzeldent committed Feb 17, 2024
1 parent 152c068 commit 6119020
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 70 deletions.
33 changes: 25 additions & 8 deletions lib/micro-rtsp-server/include/micro_rtsp_jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

#include <stdint.h>
#include <stddef.h>
#include <tuple>

class micro_rtsp_jpeg
{
public:
micro_rtsp_jpeg(const uint8_t *jpeg, size_t size);
bool decode_jpeg(const uint8_t *jpg, size_t size);

std::tuple<const uint8_t *, size_t> quantization_table_0_;
std::tuple<const uint8_t *, size_t> quantization_table_1_;
std::tuple<const uint8_t *, size_t> jpeg_data_;
protected:
bool decode_jpeg(uint8_t *jpg, size_t size);
std::tuple<const uint8_t *, size_t> find_jpeg_section(uint8_t **ptr, uint8_t *end, uint8_t flag);
class __attribute__ ((packed)) jpg_section
{
public:
uint8_t flag() const { return section_flag; }
const char *flag_name() const;
uint16_t section_length() const { return section_flag == 0xd8 || section_flag == 0xd9 ? 0 : (section_length_msb << 8) + section_length_lsb; }
const uint8_t *data() const { return reinterpret_cast<const uint8_t *>(&section_data[1]); }
uint16_t data_length() const { return section_length() - 3; }

private:
const uint8_t section_flag;
const uint8_t section_length_msb;
const uint8_t section_length_lsb;
const uint8_t section_data[];
};

const jpg_section *quantization_table_0_;
const jpg_section *quantization_table_1_;

const uint8_t *jpeg_data_start;
const uint8_t *jpeg_data_end;

private:
static const jpg_section *find_jpeg_section(const uint8_t **ptr, const uint8_t *end, uint8_t flag);
};
94 changes: 47 additions & 47 deletions lib/micro-rtsp-server/src/micro_rtsp_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,100 @@

#include "micro_rtsp_jpeg.h"

micro_rtsp_jpeg::micro_rtsp_jpeg(const uint8_t *jpeg, size_t size)
const char *micro_rtsp_jpeg::jpg_section::flag_name() const
{
switch (section_flag)
{
case 0xd8:
return "SOI"; // start of image
case 0xd9:
return "EOI"; // end of image
case 0xe0:
return "APP0";
case 0xdb:
return "DQT"; // define quantization table
case 0xc4:
return "DHT"; // define Huffman table
case 0xc0:
return "SOF"; // start of frame
case 0xda:
return "SOS"; // start of scan
}
return "Unknown";
}

std::tuple<const uint8_t *, size_t> micro_rtsp_jpeg::find_jpeg_section(uint8_t **ptr, uint8_t *end, uint8_t flag)
const micro_rtsp_jpeg::jpg_section *micro_rtsp_jpeg::find_jpeg_section(const uint8_t **ptr, const uint8_t *end, uint8_t flag)
{
size_t len;
log_d("find_jpeg_section 0x%02x", flag);
while (*ptr < end)
{
auto framing = *(*ptr++);
auto framing = *((*ptr)++);
if (framing != 0xff)
{
log_e("Malformed jpeg. Expected framing 0xff but found: 0x%02x", framing);
log_e("Expected framing 0xff but found: 0x%02x", framing);
break;
}

// framing = 0xff, flag, len MSB, len LSB
auto flag_code = *(*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 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)
auto section = reinterpret_cast<const jpg_section *>((*ptr)++);
// Advance pointer section has a length, so not SOI (0xd8) and EOI (0xd9)
*ptr += section->section_length();
if (section->flag() == flag)
{
log_d("Skipping jpeg section flag: 0x%02x, %d bytes", flag_code, len);
ptr += len;
break;
}
default:
log_e("Unexpected jpeg flag: 0x%02x", flag_code);
log_d("Found section 0x%02x (%s), %d bytes", flag, section->flag_name(), section->section_length());
return section;
}

log_d("Skipping section: 0x%02x (%s), %d bytes", section->flag(), section->flag_name(), section->section_length());
}

// Not found
return std::tuple<const uint8_t *, size_t>(nullptr, 0);
return nullptr;
}

// See https://create.stephan-brumme.com/toojpeg/
bool micro_rtsp_jpeg::decode_jpeg(uint8_t *data, size_t size)
bool micro_rtsp_jpeg::decode_jpeg(const uint8_t *data, size_t size)
{
log_d("decode_jpeg");
// Look for start jpeg file (0xd8)
auto ptr = data;
auto end = ptr + size;

// Check for SOI (start of image) 0xff, 0xd8
if (*(ptr++) != 0xff || *(ptr++) != 0xd8)
if (!find_jpeg_section(&ptr, end, 0xd8))
{
log_e("No valid start of image marker found");
return false;
}

// First quantization table
quantization_table_0_ = find_jpeg_section(&ptr, end, 0xdb);
if (std::get<0>(quantization_table_0_) == nullptr)
{
if (!(quantization_table_0_ = find_jpeg_section(&ptr, end, 0xdb)))
log_e("No quantization table 0 section found");
}

// Second quantization table (for color images)
quantization_table_1_ = find_jpeg_section(&ptr, end, 0xdb);
if (std::get<0>(quantization_table_1_) == nullptr)
{
if (!(quantization_table_1_ = find_jpeg_section(&ptr, end, 0xdb)))
log_w("No quantization table 1 section found");
}

// Start of scan
auto sos = find_jpeg_section(&ptr, end, 0xda);
if (std::get<0>(sos) == nullptr)
{
if (!find_jpeg_section(&ptr, end, 0xda))
log_e("No start of scan section found");
}

// Start of the data sections
auto start = ptr;
jpeg_data_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))
while (ptr < end - 1 && (ptr[0] != 0xff || ptr[1] == 0))
ptr++;

// Go back tgo start of section
ptr--;

// Check if marker is an end of image marker
if (*(ptr++) != 0xff || *(ptr++) != 0xd9)
if (!find_jpeg_section(&ptr, end, 0xd9))
{
log_e("No end of image marker found");
return false;
}

jpeg_data_ = std::tuple<const uint8_t *, size_t>(start, size - (ptr - start - 2));
jpeg_data_end = ptr;
log_d("Total jpeg data = %d bytes", jpeg_data_end - jpeg_data_start);

return true;
}
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ default_envs = esp32cam_ai_thinker
[env]
platform = espressif32
framework = arduino
test_framework = unity

#upload_protocol = espota
#upload_port = 192.168.178.223
Expand All @@ -42,7 +43,8 @@ framework = arduino
monitor_speed = 115200
monitor_rts = 0
monitor_dtr = 0
monitor_filters = log2file, time, default, esp32_exception_decoder
#monitor_filters = log2file, time, default, esp32_exception_decoder
monitor_filters = esp32_exception_decoder

build_flags =
-Ofast
Expand Down
62 changes: 48 additions & 14 deletions test/test_main.cpp

Large diffs are not rendered by default.

0 comments on commit 6119020

Please sign in to comment.