diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index 02169c959b..bfd5053ae5 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -257,7 +257,7 @@ endif() option(OPENEXR_FORCE_INTERNAL_OPENJPH "Force downloading OpenJPH from a git repo" OFF) set(OPENEXR_OPENJPH_REPO "https://github.com/aous72/OpenJPH.git" CACHE STRING "OpenJPH git repo URI") -set(OPENEXR_OPENJPH_TAG "0.21.5" CACHE STRING "OpenJPH git repo tag") +set(OPENEXR_OPENJPH_TAG "restart" CACHE STRING "OpenJPH git repo tag") if (NOT OPENEXR_FORCE_INTERNAL_OPENJPH) find_package(openjph 0.21 CONFIG QUIET) diff --git a/src/lib/OpenEXRCore/internal_ht.cpp b/src/lib/OpenEXRCore/internal_ht.cpp index 0875c8970b..3ae64a9b5b 100644 --- a/src/lib/OpenEXRCore/internal_ht.cpp +++ b/src/lib/OpenEXRCore/internal_ht.cpp @@ -14,6 +14,14 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif +#include "internal_memory.h" +#ifdef __cplusplus +} +#endif + #include "openexr_decode.h" #include "openexr_encode.h" #include "internal_ht_common.h" @@ -157,6 +165,17 @@ class staticmem_outfile : public ojph::outfile_base ojph::ui8 *cur_ptr; }; +void* alloc_fn (exr_transcoding_pipeline_buffer_id_t buf, size_t sz) { + return internal_exr_alloc(sz); +} + +void +free_fn (exr_transcoding_pipeline_buffer_id_t id, void* buf) +{ + if (id == EXR_TRANSCODE_BUFFER_SCRATCH1) { delete (ojph::codestream*) buf; } + else { internal_exr_free (buf); } +} + extern "C" exr_result_t internal_exr_undo_ht ( exr_decode_pipeline_t* decode, @@ -196,11 +215,19 @@ internal_exr_undo_ht ( reinterpret_cast (compressed_data) + header_sz, comp_buf_size - header_sz); - ojph::codestream cs; - cs.read_headers (&infile); + if (decode->scratch_buffer_1 == NULL) { + decode->alloc_fn = alloc_fn; + decode->free_fn = free_fn; + decode->scratch_buffer_1 = new ojph::codestream(); + } else { + ((ojph::codestream*) decode->scratch_buffer_1)->restart(); + } + ojph::codestream* cs = (ojph::codestream*)decode->scratch_buffer_1; - ojph::param_siz siz = cs.access_siz (); - ojph::param_nlt nlt = cs.access_nlt (); + cs->read_headers (&infile); + + ojph::param_siz siz = cs->access_siz (); + ojph::param_nlt nlt = cs->access_nlt (); ojph::ui32 image_width = siz.get_image_extent ().x - siz.get_image_offset ().x; @@ -217,19 +244,19 @@ internal_exr_undo_ht ( decode->channels[c].y_samples > 1) { is_planar = true; } } - cs.set_planar (is_planar); + cs->set_planar (is_planar); assert (decode->chunk.width == image_width); assert (decode->chunk.height == image_height); assert (decode->channel_count == siz.get_num_components ()); - cs.create (); + cs->create (); assert (sizeof (uint16_t) == 2); assert (sizeof (uint32_t) == 4); ojph::ui32 next_comp = 0; ojph::line_buf* cur_line; - if (cs.is_planar ()) + if (cs->is_planar ()) { for (uint32_t c = 0; c < decode->channel_count; c++) { @@ -253,7 +280,7 @@ internal_exr_undo_ht ( if (line_c == file_c) { - cur_line = cs.pull (next_comp); + cur_line = cs->pull (next_comp); assert (next_comp == c); if (decode->channels[file_c].data_type == @@ -296,7 +323,7 @@ internal_exr_undo_ht ( for (uint32_t c = 0; c < decode->channel_count; c++) { int file_c = cs_to_file_ch[c].file_index; - cur_line = cs.pull (next_comp); + cur_line = cs->pull (next_comp); assert (next_comp == c); if (decode->channels[file_c].data_type == EXR_PIXEL_HALF) { @@ -340,10 +367,17 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode) int image_height = encode->chunk.height; int image_width = encode->chunk.width; - ojph::codestream cs; + if (encode->scratch_buffer_1 == NULL) { + encode->alloc_fn = alloc_fn; + encode->free_fn = free_fn; + encode->scratch_buffer_1 = new ojph::codestream(); + } else { + ((ojph::codestream*) encode->scratch_buffer_1)->restart(); + } + ojph::codestream* cs = (ojph::codestream*)encode->scratch_buffer_1; - ojph::param_siz siz = cs.access_siz (); - ojph::param_nlt nlt = cs.access_nlt (); + ojph::param_siz siz = cs->access_siz (); + ojph::param_nlt nlt = cs->access_nlt (); bool isPlanar = false; siz.set_num_components (encode->channel_count); @@ -371,17 +405,17 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode) encode->channels[file_c].width; } - cs.set_planar (isPlanar); + cs->set_planar (isPlanar); siz.set_image_offset (ojph::point (0, 0)); siz.set_image_extent (ojph::point (image_width, image_height)); - ojph::param_cod cod = cs.access_cod (); + ojph::param_cod cod = cs->access_cod (); cod.set_color_transform (isRGB && !isPlanar); cod.set_reversible (true); - cod.set_block_dims (128, 32); - cod.set_num_decomposition (5); + cod.set_block_dims (256, 16); + cod.set_num_decomposition (4); try { @@ -395,12 +429,12 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode) staticmem_outfile output; output.open ( ((uint8_t*) encode->compressed_buffer) + header_sz, encode->packed_bytes - header_sz); - cs.write_headers (&output); + cs->write_headers (&output); ojph::ui32 next_comp = 0; - ojph::line_buf* cur_line = cs.exchange (NULL, next_comp); + ojph::line_buf* cur_line = cs->exchange (NULL, next_comp); - if (cs.is_planar ()) + if (cs->is_planar ()) { for (ojph::ui32 c = 0; c < encode->channel_count; c++) { @@ -445,7 +479,7 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode) } assert (next_comp == c); - cur_line = cs.exchange (cur_line, next_comp); + cur_line = cs->exchange (cur_line, next_comp); } line_pixels += encode->channels[line_c].bytes_per_element * @@ -488,13 +522,13 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode) } } assert (next_comp == c); - cur_line = cs.exchange (cur_line, next_comp); + cur_line = cs->exchange (cur_line, next_comp); } line_pixels += bpl; } } - cs.flush (); + cs->flush (); assert (output.get_size () >= 0); encode->compressed_bytes = output.get_size () + header_sz;