From 10f89852c8c868b779e78b433a9e6cf872a01849 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 13 Nov 2024 17:26:11 +0100 Subject: [PATCH] Do not turn on 'TPsot==TNsot detection fix' when TNsot==1, and add a OPJ_DPARAMETERS_DISABLE_TPSOT_FIX flag to disable it Adresses the use case of https://lists.osgeo.org/pipermail/gdal-dev/2024-November/059805.html where Sentinel2 L1C JPEG2000 files are made of a single tile-part per tile. --- src/lib/openjp2/j2k.c | 20 +++++++++++++++++--- src/lib/openjp2/openjpeg.h | 7 ++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index 7dc389fa2..94d775d7c 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -6752,6 +6752,9 @@ void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG); + if (parameters->flags & OPJ_DPARAMETERS_DISABLE_TPSOT_FIX) { + j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; + } #ifdef USE_JPWL j2k->m_cp.correct = parameters->jpwl_correct; j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; @@ -9961,14 +9964,25 @@ OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, } } + printf("p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = %d\n", p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked); if (p_j2k->m_specific_param.m_decoder.m_can_decode && !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { /* Issue 254 */ - OPJ_BOOL l_correction_needed; + OPJ_BOOL l_correction_needed = OPJ_FALSE; p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; - if (!opj_j2k_need_nb_tile_parts_correction(p_stream, - p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { + if (p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts == 1) { + /* Skip opj_j2k_need_nb_tile_parts_correction() if there is + * only a single tile part declared. The + * opj_j2k_need_nb_tile_parts_correction() hack was needed + * for files with 5 declared tileparts (where they were + * actually 6). + * Doing it systematically hurts performance when reading + * Sentinel2 L1C JPEG2000 files as explained in + * https://lists.osgeo.org/pipermail/gdal-dev/2024-November/059805.html + */ + } else if (!opj_j2k_need_nb_tile_parts_correction(p_stream, + p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { opj_event_msg(p_manager, EVT_ERROR, "opj_j2k_apply_nb_tile_parts_correction error\n"); return OPJ_FALSE; diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 113481bbb..05cec0e3c 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -546,7 +546,12 @@ typedef struct opj_cparameters { } opj_cparameters_t; #define OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG 0x0001 -#define OPJ_DPARAMETERS_DUMP_FLAG 0x0002 +#define OPJ_DPARAMETERS_DUMP_FLAG 0x0002 + +/** Disable at runtime the check for invalid TPSOT values added in + * https://github.com/uclouvain/openjpeg/pull/514. + */ +#define OPJ_DPARAMETERS_DISABLE_TPSOT_FIX 0x0004 /** * Decompression parameters