From e3d2be1ec0a2b7bb4beebc4c2a3cbede4bbbf436 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Mon, 2 Jun 2025 00:08:31 +0200 Subject: [PATCH WIP v2 06/10] ffbuild/bin2c: Allow to use aligned reads Possible if one aligns the buffer. Signed-off-by: Andreas Rheinhardt --- ffbuild/bin2c.c | 10 ++++++---- fftools/resources/resman.c | 4 ++-- libavfilter/cuda/load_helper.c | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ffbuild/bin2c.c b/ffbuild/bin2c.c index e8643ca792..c380a0ac43 100644 --- a/ffbuild/bin2c.c +++ b/ffbuild/bin2c.c @@ -115,11 +115,14 @@ static void write_u32(FILE *output, uint32_t num) (num >> (HAVE_BIGENDIAN ? (24 - 8 * i) : 8 * i)) & 0xff); } -static int handle_compressed_file(FILE *input, FILE *output) +static int handle_compressed_file(FILE *input, FILE *output, const char *name) { unsigned char *compressed_data; uint32_t compressed_size, uncompressed_size; + fprintf(output, "#include \"libavutil/mem_internal.h\"\n"); + fprintf(output, "DECLARE_ALIGNED_4(const unsigned char, ff_%s_data)[] = { ", name); + int err = read_file_and_compress(&compressed_data, &compressed_size, &uncompressed_size, input); if (err) @@ -194,11 +197,9 @@ int main(int argc, char **argv) } } - fprintf(output, "const unsigned char ff_%s_data[] = { ", name); - #if CONFIG_PTX_COMPRESSION || CONFIG_RESOURCE_COMPRESSION if (compression) { - int err = handle_compressed_file(input, output); + int err = handle_compressed_file(input, output, name); if (err) { fclose(input); fclose(output); @@ -210,6 +211,7 @@ int main(int argc, char **argv) { unsigned int length = 0; + fprintf(output, "const unsigned char ff_%s_data[] = { ", name); while (fread(&data, 1, 1, input) > 0) { fprintf(output, "0x%02x, ", data); length++; diff --git a/fftools/resources/resman.c b/fftools/resources/resman.c index fc00465775..0094ecd4c5 100644 --- a/fftools/resources/resman.c +++ b/fftools/resources/resman.c @@ -64,14 +64,14 @@ static ResourceManagerContext resman_ctx = { .class = &resman_class }; static int decompress_zlib(ResourceManagerContext *ctx, const uint8_t *in, char **out) { // Allocate output buffer with extra byte for null termination - uint32_t uncompressed_size = AV_RN32(in); + uint32_t uncompressed_size = AV_RN32A(in); uint8_t *buf = av_malloc(uncompressed_size + 1); if (!buf) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate decompression buffer\n"); return AVERROR(ENOMEM); } uLongf buf_size = uncompressed_size; - int ret = uncompress(buf, &buf_size, in + 8, AV_RN32(in + 4)); + int ret = uncompress(buf, &buf_size, in + 8, AV_RN32A(in + 4)); if (ret != Z_OK || uncompressed_size != buf_size) { av_log(ctx, AV_LOG_ERROR, "Error uncompressing resource. zlib returned %d\n", ret); av_free(buf); diff --git a/libavfilter/cuda/load_helper.c b/libavfilter/cuda/load_helper.c index e47f0421ef..79ec37c7cd 100644 --- a/libavfilter/cuda/load_helper.c +++ b/libavfilter/cuda/load_helper.c @@ -38,13 +38,13 @@ int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_mo CudaFunctions *cu = hwctx->internal->cuda_dl; #if CONFIG_PTX_COMPRESSION - uint32_t uncompressed_size = AV_RN32(data); + uint32_t uncompressed_size = AV_RN32A(data); uint8_t *buf = av_realloc(NULL, uncompressed_size + 1); if (!buf) return AVERROR(ENOMEM); uLongf buf_size = uncompressed_size; - int ret = uncompress(buf, &buf_size, data + 8, AV_RN32(data + 4)); + int ret = uncompress(buf, &buf_size, data + 8, AV_RN32A(data + 4)); if (ret != Z_OK || uncompressed_size != buf_size) { av_log(avctx, AV_LOG_ERROR, "Error uncompressing cuda code. zlib returned %d\n", ret); ret = AVERROR_EXTERNAL; -- 2.45.2