From ffdb902b029226c448f1287eb84f11c354e9957e Mon Sep 17 00:00:00 2001 From: Jannis Achstetter Date: Tue, 8 Jun 2021 21:17:30 +0200 Subject: [PATCH] Add zlib compress (which is surprisingly fast) --- .gitmodules | 9 +++++++++ lib/snappy | 1 + lib/zlib | 1 + lib/zstd | 1 + src/CMakeLists.txt | 9 +++++++++ src/algo-heatshrink.c | 2 +- src/algo-zlib.c | 20 ++++++++++++++++++++ src/algo-zlib.h | 10 ++++++++++ src/main.c | 9 +++++++++ 9 files changed, 61 insertions(+), 1 deletion(-) create mode 160000 lib/snappy create mode 160000 lib/zlib create mode 160000 lib/zstd create mode 100644 src/algo-zlib.c create mode 100644 src/algo-zlib.h diff --git a/.gitmodules b/.gitmodules index 4fb59e2..d04a4a3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,12 @@ [submodule "lib/heatshrink"] path = lib/heatshrink url = https://github.com/kripton/heatshrink.git +[submodule "lib/snappy"] + path = lib/snappy + url = https://github.com/kripton/snappy.git +[submodule "lib/zstd"] + path = lib/zstd + url = https://github.com/kripton/zstd.git +[submodule "lib/zlib"] + path = lib/zlib + url = https://github.com/kripton/zlib.git diff --git a/lib/snappy b/lib/snappy new file mode 160000 index 0000000..b638ebe --- /dev/null +++ b/lib/snappy @@ -0,0 +1 @@ +Subproject commit b638ebe5d95ec4559921a72f8c2bbc4b1b5a2fd0 diff --git a/lib/zlib b/lib/zlib new file mode 160000 index 0000000..4773d80 --- /dev/null +++ b/lib/zlib @@ -0,0 +1 @@ +Subproject commit 4773d8033b53126cf4bf48bebc41338c90a78332 diff --git a/lib/zstd b/lib/zstd new file mode 160000 index 0000000..8a3bdfa --- /dev/null +++ b/lib/zstd @@ -0,0 +1 @@ +Subproject commit 8a3bdfaa7bb2944dbed1ce9dede39f512d6da3f9 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f5a54b4..91834a7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,12 +12,14 @@ pico_sdk_init() ## Include the CMakeLists-files of (non-pico-sdk)-libraries we will be using include(../lib/heatshrink/CMakeLists.txt) +include(../lib/zlib/interfaceLibForPicoSDK.cmake) ## Add our own C/C++ files here ## Sorted alphabetically add_executable(${CMAKE_PROJECT_NAME} ${TINYUSB_LIBNETWORKING_SOURCES} algo-heatshrink.c + algo-zlib.c main.c ) @@ -29,6 +31,12 @@ target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC HEATSHRINK_STATIC_LOOKAHEAD_BITS=6 ) +## Config for the zlib library to reduce memory usage +target_compile_definitions(${CMAKE_PROJECT_NAME} + PUBLIC MAX_WBITS=9 + PUBLIC MAX_MEM_LEVEL=4 +) + ## Needed so tusb_config.h is found by the tinyUSB-library included in the pico-sdk target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR} @@ -39,6 +47,7 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE target_link_libraries(${CMAKE_PROJECT_NAME} heatshrink pico_stdlib + zlib ) ## enable usb output, disable uart output diff --git a/src/algo-heatshrink.c b/src/algo-heatshrink.c index aacf3f5..d2381ea 100644 --- a/src/algo-heatshrink.c +++ b/src/algo-heatshrink.c @@ -71,4 +71,4 @@ void heatshrink_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_ } *outsize = polled; -} \ No newline at end of file +} diff --git a/src/algo-zlib.c b/src/algo-zlib.c new file mode 100644 index 0000000..6aa223d --- /dev/null +++ b/src/algo-zlib.c @@ -0,0 +1,20 @@ +#include "algo-zlib.h" + +#include "../lib/zlib/zlib.h" + +void zlib_init() +{ +} + +void zlib_compress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize) +{ + long unsigned int outsize2 = *outsize; + compress2(output, &outsize2, input, insize, 4); + *outsize = outsize2; +} + +void zlib_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize) +{ + long unsigned int outsize2 = *outsize; + uncompress(output, &outsize2, input, insize); +} diff --git a/src/algo-zlib.h b/src/algo-zlib.h new file mode 100644 index 0000000..c1618b8 --- /dev/null +++ b/src/algo-zlib.h @@ -0,0 +1,10 @@ +#ifndef __ALGO_ZLIB_H__ +#define __ALGO_ZLIB_H__ + +#include "pico/stdlib.h" + +void zlib_init(); +void zlib_compress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize); +void zlib_uncompress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize); + +#endif // __ALGO_ZLIB_H__ diff --git a/src/main.c b/src/main.c index cee8dd1..717023f 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ #include "picotool_binary_information.h" #include "algo-heatshrink.h" +#include "algo-zlib.h" // TEST VECTORS @@ -50,6 +51,7 @@ struct algo { enum { ALGO_HEATSHRINK, + ALGO_ZLIB, // Add more HERE ALGO_NUM_TOTAL }; @@ -180,6 +182,13 @@ int main() { algos[ALGO_HEATSHRINK].uncompress = heatshrink_uncompress; algos[ALGO_HEATSHRINK].init(); + // Algo 0: zlib + sprintf(algos[ALGO_ZLIB].name, "ZLIB"); + algos[ALGO_ZLIB].init = zlib_init; + algos[ALGO_ZLIB].compress = zlib_compress; + algos[ALGO_ZLIB].uncompress = zlib_uncompress; + algos[ALGO_ZLIB].init(); + finish_time = time_us_32(); elapsed_time = finish_time - start_time; printf("%luµs\n", elapsed_time);