diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index c8d8cf1a..a8dc22b9 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -9947,6 +9947,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, OPJ_UINT32 l_nb_tiles; OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size; OPJ_BYTE * l_current_data = 00; + OPJ_BOOL l_reuse_data = OPJ_FALSE; opj_tcd_t* p_tcd = 00; /* preconditions */ @@ -9957,6 +9958,17 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, p_tcd = p_j2k->m_tcd; l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + if (l_nb_tiles == 1) { + l_reuse_data = OPJ_TRUE; +#ifdef __SSE__ + for (j=0;jm_tcd->image->numcomps;++j) { + opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; + if (((size_t)l_img_comp->data & 0xFU) != 0U) { /* tile data shall be aligned on 16 bytes */ + l_reuse_data = OPJ_FALSE; + } + } +#endif + } for (i=0;im_tcd->image->numcomps;++j) { opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; - if (l_nb_tiles == 1) { + if (l_reuse_data) { opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; l_tilec->data = l_img_comp->data; l_tilec->ownsData = OPJ_FALSE; @@ -9984,7 +9996,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, } } l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); - if (l_nb_tiles > 1) { + if (!l_reuse_data) { if (l_current_tile_size > l_max_tile_size) { OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size); if (! l_new_current_data) { diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 30d89258..2636f076 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -79,8 +79,7 @@ static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t si * allocated array (eg. _msize on Windows, malloc_size on MacOS, * malloc_usable_size on systems with glibc) */ a_ptr = opj_aligned_alloc_n(alignment, size); - /* memory may overlap, do not use memcpy */ - memmove(a_ptr, r_ptr, size); + memcpy(a_ptr, r_ptr, size); free( r_ptr ); return a_ptr; /* _MSC_VER */ diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index a5a5d272..5e20a7a0 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -635,18 +635,15 @@ OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec) l_tilec->ownsData = OPJ_TRUE; } else if (l_tilec->data_size_needed > l_tilec->data_size) { - OPJ_INT32 * new_data = (OPJ_INT32 *) opj_aligned_realloc(l_tilec->data, l_tilec->data_size_needed); - /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */ - /* fprintf(stderr, "Not enough memory to handle tile data"); */ - if (! new_data) { - opj_aligned_free(l_tilec->data); - l_tilec->data = NULL; + /* We don't need to keep old data */ + opj_aligned_free(l_tilec->data); + l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed); + if (! l_tilec->data ) { l_tilec->data_size = 0; l_tilec->data_size_needed = 0; l_tilec->ownsData = OPJ_FALSE; return OPJ_FALSE; } - l_tilec->data = new_data; /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/ l_tilec->data_size = l_tilec->data_size_needed; l_tilec->ownsData = OPJ_TRUE; @@ -1521,7 +1518,7 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) } if (l_tile_comp->ownsData && l_tile_comp->data) { - opj_free(l_tile_comp->data); + opj_aligned_free(l_tile_comp->data); l_tile_comp->data = 00; l_tile_comp->ownsData = 0; l_tile_comp->data_size = 0;