Skip to content

Commit

Permalink
JPEGXL: Hold shallow pointers to load_options/save_options
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Nov 11, 2023
1 parent 112ee12 commit 28ba681
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions src/sail-codecs/jpegxl/jpegxl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
*/
struct jpegxl_state {
struct sail_io *io;
struct sail_load_options *load_options;
struct sail_save_options *save_options;
const struct sail_load_options *load_options;
const struct sail_save_options *save_options;

struct sail_source_image *source_image;

Expand All @@ -58,26 +58,50 @@ struct jpegxl_state {
size_t buffer_size;
};

static sail_status_t alloc_jpegxl_state(struct jpegxl_state **jpegxl_state) {
static sail_status_t alloc_jpegxl_state(struct sail_io *io,
const struct sail_load_options *load_options,
const struct sail_save_options *save_options,
struct jpegxl_state **jpegxl_state) {

void *ptr;
SAIL_TRY(sail_malloc(sizeof(struct jpegxl_state), &ptr));
*jpegxl_state = ptr;

(*jpegxl_state)->io = NULL;
(*jpegxl_state)->load_options = NULL;
(*jpegxl_state)->save_options = NULL;
/* JxlMemoryManager */
SAIL_TRY(sail_malloc(sizeof(JxlMemoryManager), &ptr));
JxlMemoryManager *memory_manager = ptr;

*memory_manager = (JxlMemoryManager) {
.opaque = NULL,
.alloc = jpegxl_private_alloc_func,
.free = jpegxl_private_free_func,
};

(*jpegxl_state)->source_image = NULL;
/* buffer */
const size_t buffer_size = 8192;
void *buffer;
SAIL_TRY_OR_CLEANUP(sail_malloc(buffer_size, &buffer),
/* on error */ sail_free(memory_manager));

(*jpegxl_state)->libjxl_success = false;
(*jpegxl_state)->frame_header_seen = false;
(*jpegxl_state)->basic_info = NULL;
(*jpegxl_state)->memory_manager = NULL;
(*jpegxl_state)->runner = NULL;
(*jpegxl_state)->decoder = NULL;
(*jpegxl_state)->buffer = NULL;
(*jpegxl_state)->buffer_size = 8192;
/* jpegxl_state */
SAIL_TRY_OR_CLEANUP(sail_malloc(sizeof(struct jpegxl_state), &ptr),
/* on error */ sail_free(buffer), sail_free(memory_manager));
*jpegxl_state = ptr;

**jpegxl_state = (struct jpegxl_state) {
.io = io,
.load_options = load_options,
.save_options = save_options,

.source_image = NULL,

.libjxl_success = false,
.frame_header_seen = false,
.basic_info = NULL,
.memory_manager = memory_manager,
.runner = NULL,
.decoder = NULL,
.buffer = buffer,
.buffer_size = buffer_size,
};

return SAIL_OK;
}
Expand All @@ -88,9 +112,6 @@ static void destroy_jpegxl_state(struct jpegxl_state *jpegxl_state) {
return;
}

sail_destroy_load_options(jpegxl_state->load_options);
sail_destroy_save_options(jpegxl_state->save_options);

sail_destroy_source_image(jpegxl_state->source_image);

sail_free(jpegxl_state->basic_info);
Expand All @@ -114,29 +135,13 @@ SAIL_EXPORT sail_status_t sail_codec_load_init_v8_jpegxl(struct sail_io *io, con

/* Allocate a new state. */
struct jpegxl_state *jpegxl_state;
SAIL_TRY(alloc_jpegxl_state(&jpegxl_state));
SAIL_TRY(alloc_jpegxl_state(io, load_options, NULL, &jpegxl_state));
*state = jpegxl_state;

/* Save I/O for further operations. */
jpegxl_state->io = io;

/* Deep copy load options. */
SAIL_TRY(sail_copy_load_options(load_options, &jpegxl_state->load_options));

/* Init decoder. */
void *ptr;
SAIL_TRY(sail_malloc(sizeof(JxlMemoryManager), &ptr));
jpegxl_state->memory_manager = ptr;
jpegxl_state->memory_manager->opaque = NULL,
jpegxl_state->memory_manager->alloc = jpegxl_private_alloc_func,
jpegxl_state->memory_manager->free = jpegxl_private_free_func,

jpegxl_state->runner = JxlResizableParallelRunnerCreate(jpegxl_state->memory_manager);
jpegxl_state->decoder = JxlDecoderCreate(jpegxl_state->memory_manager);

SAIL_TRY(sail_malloc(jpegxl_state->buffer_size, &ptr));
jpegxl_state->buffer = ptr;

if (JxlDecoderSetCoalescing(jpegxl_state->decoder, JXL_TRUE) != JXL_DEC_SUCCESS) {
SAIL_LOG_ERROR("JPEGXL: Failed to set coalescing");
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNDERLYING_CODEC);
Expand Down

0 comments on commit 28ba681

Please sign in to comment.