]> granicus.if.org Git - openjpeg/commitdiff
Fixed crash on encoding
authormayeut <mayeut@users.noreply.github.com>
Fri, 16 Oct 2015 23:30:23 +0000 (01:30 +0200)
committermayeut <mayeut@users.noreply.github.com>
Fri, 16 Oct 2015 23:30:23 +0000 (01:30 +0200)
Update #624
Update #625

src/lib/openjp2/j2k.c
src/lib/openjp2/opj_malloc.c
src/lib/openjp2/tcd.c

index c8d8cf1ac89b134e07359f50f4a6f532d08aab7a..a8dc22b96d4c5064afcd67f0f7bd7ee4a7905d4d 100644 (file)
@@ -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;j<p_j2k->m_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;i<l_nb_tiles;++i) {
                 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
                         if (l_current_data) {
@@ -9969,7 +9981,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
                 /* otherwise, allocate the data */
                 for (j=0;j<p_j2k->m_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) {
index 30d892587eb23e076c2d6d19d310c10abaa1d7da..2636f07698aa17796a7b9e1338744249eaeabac9 100644 (file)
@@ -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 */
index a5a5d272796b38f59fa6263b6b8501ede6fc4939..5e20a7a0232b9fbc019d0598804f855da709c44a 100644 (file)
@@ -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;