Commit 09d53eea authored by Manuel Stoeckl's avatar Manuel Stoeckl
Browse files

Revert to simpler LZ4 and Zstd APIs

As adding frame headers to compressed data is no longer needed, it
is safe to revert from the LZ4F API to the simpler LZ4 and LZ4HC APIs
for fast and slow LZ4 compression modes.

Similarly, as fine grained control of the Zstd compression parameters
is neither needed nor used, the use of the very new ZSTD_compress2
function is replaced with the much older and equivalent ZSTD_compressCCtx
function.
parent 23446300
Pipeline #53390 passed with stages
in 1 minute and 28 seconds
......@@ -49,8 +49,8 @@ Requirements:
* meson (build, >= 0.47. with dependencies `ninja`, `pkg-config`, `python3`)
* wayland (build, >= 1.10 for the `wl_surface::damage_buffer` request)
* wayland-protocols (build, >= 1.12, for the xdg-shell protocol, and others)
* liblz4 (optional)
* libzstd (optional, >= 1.4.0)
* liblz4 (optional, >=1.7.0)
* libzstd (optional, >= 0.4.6)
* libgbm (optional, to support programs using OpenGL via DMABUFs)
* libdrm (optional, same as for libgbm)
* ffmpeg (optional, >=3.1, needs avcodec/avutil/swscale for lossy video encoding)
......
......@@ -48,11 +48,11 @@ is_darwin = host_machine.system() == 'darwin'
if (is_linux or is_darwin) and cc.has_header('sys/sdt.h')
config_data.set('HAS_USDT', 1, description: 'Enable static trace probes')
endif
liblz4 = dependency('liblz4', required: get_option('with_lz4'))
liblz4 = dependency('liblz4', version: '>=1.7.0', required: get_option('with_lz4'))
if liblz4.found()
config_data.set('HAS_LZ4', 1, description: 'Enable LZ4 compression')
endif
libzstd = dependency('libzstd', version: '>=1.4.0', required: get_option('with_zstd'))
libzstd = dependency('libzstd', version: '>=0.4.6', required: get_option('with_zstd'))
if libzstd.found()
config_data.set('HAS_ZSTD', 1, description: 'Enable Zstd compression')
endif
......
......@@ -42,7 +42,8 @@
#include <unistd.h>
#ifdef HAS_LZ4
#include <lz4frame.h>
#include <lz4.h>
#include <lz4hc.h>
#endif
#ifdef HAS_ZSTD
#include <zstd.h>
......@@ -116,7 +117,7 @@ static void cleanup_thread_local(struct thread_data *data)
ZSTD_freeDCtx(data->comp_ctx.zstd_dcontext);
#endif
#ifdef HAS_LZ4
LZ4F_freeDecompressionContext(data->comp_ctx.lz4f_dcontext);
free(data->comp_ctx.lz4_extstate);
#endif
free(data->tmp_buf);
}
......@@ -127,14 +128,16 @@ static void setup_thread_local(struct thread_data *data,
struct comp_ctx *ctx = &data->comp_ctx;
ctx->zstd_ccontext = NULL;
ctx->zstd_dcontext = NULL;
ctx->lz4f_dcontext = NULL;
ctx->lz4_extstate = NULL;
#ifdef HAS_LZ4
if (mode == COMP_LZ4) {
LZ4F_errorCode_t err = LZ4F_createDecompressionContext(
&ctx->lz4f_dcontext, LZ4F_VERSION);
if (LZ4F_isError(err)) {
wp_error("Failed to created LZ4F decompression context: %s",
LZ4F_getErrorName(err));
/* Like LZ4Frame, integer codes indicate compression level.
* Negative numbers are acceleration, positive use the HC
* routines */
if (compression_level <= 0) {
ctx->lz4_extstate = malloc((size_t)LZ4_sizeofState());
} else {
ctx->lz4_extstate = malloc((size_t)LZ4_sizeofStateHC());
}
}
#endif
......@@ -142,8 +145,6 @@ static void setup_thread_local(struct thread_data *data,
if (mode == COMP_ZSTD) {
ctx->zstd_ccontext = ZSTD_createCCtx();
ctx->zstd_dcontext = ZSTD_createDCtx();
ZSTD_CCtx_setParameter(ctx->zstd_ccontext,
ZSTD_c_compressionLevel, compression_level);
}
#endif
(void)mode;
......@@ -379,7 +380,8 @@ static size_t compress_bufsize(struct thread_pool *pool, size_t max_input)
return 0;
#ifdef HAS_LZ4
case COMP_LZ4:
return (size_t)LZ4F_compressFrameBound((int)max_input, NULL);
/* This bound applies for both LZ4 and LZ4HC compressors */
return (size_t)LZ4_compressBound((int)max_input);
#endif
#ifdef HAS_ZSTD
case COMP_ZSTD:
......@@ -414,15 +416,19 @@ static void compress_buffer(struct thread_pool *pool, struct comp_ctx *ctx,
break;
#ifdef HAS_LZ4
case COMP_LZ4: {
LZ4F_preferences_t prefs;
memset(&prefs, 0, sizeof(prefs));
prefs.compressionLevel = pool->compression_level;
size_t ws = LZ4F_compressFrame(
mbuf, msize, ibuf, isize, &prefs);
if (LZ4F_isError(ws)) {
wp_error("Lz4 compression failed for %d bytes in %d of space: %s",
(int)isize, (int)msize,
LZ4F_getErrorName(ws));
size_t ws;
if (pool->compression_level <= 0) {
ws = LZ4_compress_fast_extState(ctx->lz4_extstate, ibuf,
mbuf, isize, msize,
-pool->compression_level);
} else {
ws = LZ4_compress_HC_extStateHC(ctx->lz4_extstate, ibuf,
mbuf, isize, msize,
pool->compression_level);
}
if (ws == 0) {
wp_error("LZ4 compression failed for %d bytes in %d of space",
(int)isize, (int)msize);
}
dst->size = (size_t)ws;
dst->data = (char *)mbuf;
......@@ -431,8 +437,8 @@ static void compress_buffer(struct thread_pool *pool, struct comp_ctx *ctx,
#endif
#ifdef HAS_ZSTD
case COMP_ZSTD: {
size_t ws = ZSTD_compress2(
ctx->zstd_ccontext, mbuf, msize, ibuf, isize);
size_t ws = ZSTD_compressCCtx(ctx->zstd_ccontext, mbuf, msize,
ibuf, isize, pool->compression_level);
if (ZSTD_isError(ws)) {
wp_error("Zstd compression failed for %d bytes in %d of space: %s",
(int)isize, (int)msize,
......@@ -471,24 +477,13 @@ static void uncompress_buffer(struct thread_pool *pool, struct comp_ctx *ctx,
break;
#ifdef HAS_LZ4
case COMP_LZ4: {
size_t total = 0;
size_t read = 0;
while (read < isize) {
size_t dst_remaining = msize - total;
size_t src_remaining = isize - read;
size_t hint = LZ4F_decompress(ctx->lz4f_dcontext,
&mbuf[total], &dst_remaining,
&ibuf[read], &src_remaining, NULL);
read += src_remaining;
total += dst_remaining;
if (LZ4F_isError(hint)) {
wp_error("Lz4 decomp. failed with %d bytes and %d space remaining: %s",
isize - read, msize - total,
LZ4F_getErrorName(hint));
break;
}
int ws = LZ4_decompress_safe(
ibuf, mbuf, (int)isize, (int)msize);
if (ws < 0 || (size_t)ws != msize) {
wp_error("Lz4 decompression failed for %d bytes to %d of space, used %d",
(int)isize, (int)msize, ws);
}
*wsize = total;
*wsize = (size_t)ws;
*wbuf = mbuf;
break;
}
......@@ -503,7 +498,7 @@ static void uncompress_buffer(struct thread_pool *pool, struct comp_ctx *ctx,
ZSTD_getErrorName(ws));
ws = 0;
}
*wsize = (size_t)ws;
*wsize = ws;
*wbuf = mbuf;
break;
}
......
......@@ -150,11 +150,10 @@ struct render_data {
VAConfigID av_copy_config;
};
typedef struct LZ4F_dctx_s LZ4F_dctx;
typedef struct ZSTD_CCtx_s ZSTD_CCtx;
typedef struct ZSTD_DCtx_s ZSTD_DCtx;
struct comp_ctx {
LZ4F_dctx *lz4f_dcontext;
void *lz4_extstate;
ZSTD_CCtx *zstd_ccontext;
ZSTD_DCtx *zstd_dcontext;
};
......
......@@ -406,7 +406,7 @@ int main(int argc, char **argv)
} else if (!strncmp(optarg, "lz4", 3) &&
parse_level_choice(optarg + 3,
&config.compression_level,
1)) {
-1)) {
#ifdef HAS_LZ4
config.compression = COMP_LZ4;
#else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment