Patch from Callum Lewick. Memset patch. See ChangeLog for more details. Thanks Callum !
authorFrancois-Olivier Devaux <fodevaux@users.noreply.github.com>
Thu, 18 Oct 2007 12:26:11 +0000 (12:26 +0000)
committerFrancois-Olivier Devaux <fodevaux@users.noreply.github.com>
Thu, 18 Oct 2007 12:26:11 +0000 (12:26 +0000)
20 files changed:
ChangeLog
LibOpenJPEG.vcproj
Makefile
libopenjpeg/image.c
libopenjpeg/j2k.c
libopenjpeg/j2k_lib.c
libopenjpeg/j2k_lib.h
libopenjpeg/jp2.c
libopenjpeg/opj_includes.h
libopenjpeg/opj_malloc.h [new file with mode: 0644]
libopenjpeg/pi.c
libopenjpeg/t1.c
libopenjpeg/tcd.c
libopenjpeg/tgt.c
mj2/extract_j2k_from_mj2.c
mj2/frames_to_mj2.c
mj2/frames_to_mj2.vcproj
mj2/mj2.c
mj2/mj2_to_frames.c
mj2/wrap_j2k_in_mj2.c

index 9df723ea27f5600c944142f1b6f80ab59d7da81b..a5c2a87bbeb2c413b722b96e1a101dd7ded322f3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,18 @@ What's New for OpenJPEG
 
 October 18, 2007
 * [FOD] Changed the ROI parameter in the image_to_j2k codec to make it correspond to the documentation (i.e. -ROI c=0,U=25)
+* [FOD] Patch from Callum Lewick. Memset patch. 
+               The main idea of the patch is that currently opj_malloc clears all allocations, which unnecessarily
+               dirties up the cache and eats bandwidth. This patch makes it no longer do so, and I've painstakingly determined which allocations actually need
+               to be cleared and changed them to use opj_calloc() instead. I previously tried to just get rid of the opj_*alloc wrappers but that
+               idea was nixed, so this time I'm trying it with macros. I also put in a gcc pragma that helps enforce their use. Which got messy.  :)  It caught a
+               few places they weren't used but it also revealed that the mj2 tools are not very cleanly separated from the library. It includes all the
+               internal headers, but it wasn't using the malloc wrappers. I figured the binaries should be "external" and have minimal knowledge of the
+               internals of the library. I patched them to not include opj_includes.h, and include only the internal headers they actually need. However,
+               they're using the opj_clock() function, which is in with the malloc wrappers. So I decided to move the malloc wrappers to their own header. 
+               But mj2.c seems to want to be "internal", so I patched it to use the wrappers. Note that this patch changes the semantics of opj_malloc, it no longer
+               clears the memory it allocates. If you need it to be cleared, you must use opj_calloc instead, or memset it yourself. It is also somewhat
+               invasive, please test it extensively. I've been pounding on it all summer with my test suite, Second Life, and valgrind, and it checks out clean.
 
 October 12, 2007
 * [FOD] Changed the way the image structure is allocated when the decoding parameters include some resolutions to discard. 
index 505f89b2f02870c2535fa60b96956e48968590c1..8d217d12accd00ff3f6ac3ce73ccd837260d6e10 100644 (file)
@@ -3,7 +3,7 @@
        ProjectType="Visual C++"\r
        Version="8,00"\r
        Name="LibOpenJPEG"\r
-       ProjectGUID="{BDB8C37B-824E-4617-827C-B13E2F015EFE}"\r
+       ProjectGUID="{4F27AA53-4181-4A1A-8238-3931B0A41048}"\r
        >\r
        <Platforms>\r
                <Platform\r
                                RelativePath="libopenjpeg\opj_includes.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\libopenjpeg\opj_malloc.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="libopenjpeg\pi.h"\r
                                >\r
index eb97b9405fddb08ebd112a92874f08b2ed2ae768..f75649845ad59b6000e25db40d2a9469e7fb6c83 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ VER_MAJOR = 2
 VER_MINOR = 1.2.0
 
 SRCS = ./libopenjpeg/bio.c ./libopenjpeg/cio.c ./libopenjpeg/dwt.c ./libopenjpeg/event.c ./libopenjpeg/image.c ./libopenjpeg/j2k.c ./libopenjpeg/j2k_lib.c ./libopenjpeg/jp2.c ./libopenjpeg/jpt.c ./libopenjpeg/mct.c ./libopenjpeg/mqc.c ./libopenjpeg/openjpeg.c ./libopenjpeg/pi.c ./libopenjpeg/raw.c ./libopenjpeg/t1.c ./libopenjpeg/t2.c ./libopenjpeg/tcd.c ./libopenjpeg/tgt.c
-INCLS = ./libopenjpeg/bio.h ./libopenjpeg/cio.h ./libopenjpeg/dwt.h ./libopenjpeg/event.h ./libopenjpeg/fix.h ./libopenjpeg/image.h ./libopenjpeg/int.h ./libopenjpeg/j2k.h ./libopenjpeg/j2k_lib.h ./libopenjpeg/jp2.h ./libopenjpeg/jpt.h ./libopenjpeg/mct.h ./libopenjpeg/mqc.h ./libopenjpeg/openjpeg.h ./libopenjpeg/pi.h ./libopenjpeg/raw.h ./libopenjpeg/t1.h ./libopenjpeg/t2.h ./libopenjpeg/tcd.h ./libopenjpeg/tgt.h ./libopenjpeg/opj_includes.h
+INCLS = ./libopenjpeg/bio.h ./libopenjpeg/cio.h ./libopenjpeg/dwt.h ./libopenjpeg/event.h ./libopenjpeg/fix.h ./libopenjpeg/image.h ./libopenjpeg/int.h ./libopenjpeg/j2k.h ./libopenjpeg/j2k_lib.h ./libopenjpeg/jp2.h ./libopenjpeg/jpt.h ./libopenjpeg/mct.h ./libopenjpeg/mqc.h ./libopenjpeg/openjpeg.h ./libopenjpeg/pi.h ./libopenjpeg/raw.h ./libopenjpeg/t1.h ./libopenjpeg/t2.h ./libopenjpeg/tcd.h ./libopenjpeg/tgt.h ./libopenjpeg/opj_malloc.h ./libopenjpeg/opj_includes.h 
 INCLUDE = -Ilibopenjpeg
 
 # General configuration variables:
index 6698f4590337ca1189f13f9abba080cc45f18a4f..ea8e59ea547ab91d12b538d1ff8bf889e3ce241d 100644 (file)
@@ -35,7 +35,7 @@ opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *c
        int compno;
        opj_image_t *image = NULL;
 
-       image = (opj_image_t*)opj_malloc(sizeof(opj_image_t));
+       image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
        if(image) {
                image->color_space = clrspc;
                image->numcomps = numcmpts;
@@ -58,7 +58,7 @@ opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *c
                        comp->prec = cmptparms[compno].prec;
                        comp->bpp = cmptparms[compno].bpp;
                        comp->sgnd = cmptparms[compno].sgnd;
-                       comp->data = (int*)opj_malloc(comp->w * comp->h * sizeof(int));
+                       comp->data = (int*) opj_calloc(comp->w * comp->h, sizeof(int));
                        if(!comp->data) {
                                fprintf(stderr,"Unable to allocate memory for image.\n");
                                opj_image_destroy(image);
index 042b864329a6cd0c6d7287b708c5770ce00fa710..114ee9ecdf73d44955ad580defa7e7eb131ca89e 100644 (file)
@@ -259,13 +259,13 @@ char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
 }
 
 static void j2k_check_poc_val(opj_cparameters_t *parameters, int numcomps, int numlayers){
+       int* packet_array;
        int index, resno, compno, layno, i;
-       char loss = 0;
        int step_c = 1;
        int step_r = numcomps * step_c;
        int step_l = parameters->numresolution * step_r;
-       int array_size = step_l * numlayers * sizeof(int);
-       int *packet_array = (int *) opj_malloc(array_size);
+       bool loss = false;
+       packet_array = (int*) opj_calloc(step_l * numlayers, sizeof(int));
        
        for (i = 0; i < parameters->numpocs ; i++) {
                int layno0 = 0;
@@ -285,12 +285,12 @@ static void j2k_check_poc_val(opj_cparameters_t *parameters, int numcomps, int n
                        for (layno = 0; layno < numlayers ; layno++) {
                                index = step_r * resno + step_c * compno + step_l * layno;
                                if(!(   packet_array[index]== 1)){
-                                       loss = 1;
+                                       loss = true;
                                }
                        }
                }
        }
-       if(loss == 1)
+       if(loss)
                fprintf(stdout,"Missing packets possible loss of data\n");
        opj_free(packet_array);
 }
@@ -536,7 +536,7 @@ static void j2k_read_siz(opj_j2k_t *j2k) {
        }
 #endif /* USE_JPWL */
 
-       image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
+       image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
        for (i = 0; i < image->numcomps; i++) {
                int tmp, w, h;
                tmp = cio_read(cio, 1);         /* Ssiz_i */
@@ -625,8 +625,8 @@ static void j2k_read_siz(opj_j2k_t *j2k) {
        }
 #endif /* USE_JPWL */
 
-       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
-       cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
+       cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
+       cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
        cp->tileno_size = 0;
        
 #ifdef USE_JPWL
@@ -654,13 +654,13 @@ static void j2k_read_siz(opj_j2k_t *j2k) {
        cp->ppm_data_first = NULL;
        cp->ppm_previous = 0;
        cp->ppm_store = 0;
-       
-       j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
+
+       j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
        for (i = 0; i < cp->tw * cp->th; i++) {
-               cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
+               cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
        }       
-       j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *));
-       j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
+       j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
+       j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
        j2k->state = J2K_STATE_MH;
 
        /* Index */
@@ -675,7 +675,7 @@ static void j2k_read_siz(opj_j2k_t *j2k) {
                cstr_info->tile_y = cp->tdy;    
                cstr_info->tile_Ox = cp->tx0;   
                cstr_info->tile_Oy = cp->ty0;                   
-               cstr_info->tile = (opj_tile_info_t*) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));             
+               cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
        }
 }
 
@@ -824,7 +824,7 @@ static void j2k_read_cod(opj_j2k_t *j2k) {
                opj_codestream_info_t *cstr_info = j2k->cstr_info;
                cstr_info->prog = tcp->prg;
                cstr_info->numlayers = tcp->numlayers;
-               cstr_info->numdecompos = (int*) malloc (image->numcomps * sizeof(int));
+               cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
                for (i = 0; i < image->numcomps; i++) {
                        cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
                }
@@ -1713,15 +1713,19 @@ static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
 /* ----------------------------------------------------------------------- */
 
 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
-       opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
-       if(j2k) {
-               j2k->cinfo = cinfo;
-               j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
-               if(!j2k->default_tcp) {
-                       opj_free(j2k);
-                       return NULL;
-               }
+       opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
+       if(!j2k)
+               return NULL;
+
+       j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
+       if(!j2k->default_tcp) {
+               opj_free(j2k);
+               return NULL;
        }
+
+       j2k->cinfo = cinfo;
+       j2k->tile_data = NULL;
+
        return j2k;
 }
 
@@ -1775,7 +1779,7 @@ void j2k_destroy_decompress(opj_j2k_t *j2k) {
 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
        if(j2k && parameters) {
                /* create and initialize the coding parameters structure */
-               opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
+               opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
                cp->reduce = parameters->cp_reduce;     
                cp->layer = parameters->cp_layer;
                cp->limit_decoding = parameters->cp_limit_decoding;
@@ -1977,7 +1981,7 @@ opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestre
 /* ----------------------------------------------------------------------- */
 
 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
-       opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
+       opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
        if(j2k) {
                j2k->cinfo = cinfo;
        }
@@ -2016,7 +2020,7 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
        }
 
        /* create and initialize the coding parameters structure */
-       cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
+       cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
 
        /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
        j2k->cp = cp;
@@ -2140,7 +2144,7 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
 
        /* initialize the mutiple tiles */
        /* ---------------------------- */
-       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
+       cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
 
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
                opj_tcp_t *tcp = &cp->tcps[tileno];
@@ -2187,8 +2191,8 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
                        tcp->numpocs = 0;
                }
 
-               tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
-               
+               tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
+
                for (i = 0; i < image->numcomps; i++) {
                        opj_tccp_t *tccp = &tcp->tccps[i];
                        tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
@@ -2299,11 +2303,11 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
                cstr_info->tile_Oy = cp->ty0;   /* new version parser */
                cstr_info->numcomps = image->numcomps;
                cstr_info->numlayers = (&cp->tcps[0])->numlayers;
-               cstr_info->numdecompos = (int*) malloc (image->numcomps * sizeof(int));
+               cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
                for (compno=0; compno < image->numcomps; compno++) {
                        cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
                }
-               cstr_info->D_max = 0;           /* ADD Marcela */
+               cstr_info->D_max = 0.0;         /* ADD Marcela */
                cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
        }
        /* << INDEX */
@@ -2450,7 +2454,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
        tcd_free_encode(tcd);
        tcd_destroy(tcd);
 
-       free(j2k->cur_totnum_tp);
+       opj_free(j2k->cur_totnum_tp);
 
        j2k_write_eoc(j2k);
 
index 6277432051bd56e718421c9ba82c20c48df82ff1..91aee0071521556bdc4edcbf19c280ceb92fe5f3 100644 (file)
@@ -57,20 +57,3 @@ double opj_clock(void) {
 #endif
 }
 
-void* opj_malloc( size_t size ) {
-       void *memblock = malloc(size);
-       if(memblock) {
-               memset(memblock, 0, size);
-       }
-       return memblock;
-}
-
-void* opj_realloc( void *memblock, size_t size ) {
-       return realloc(memblock, size);
-}
-
-void opj_free( void *memblock ) {
-       free(memblock); 
-}
-
-
index 99f061962ec62693f385460453fe3c198d420bb0..7df4d367757186e518befee2f7b165ac00ef10a9 100644 (file)
 @file j2k_lib.h
 @brief Internal functions
 
-The functions in J2K_LIB.C are internal utilities mainly used for memory management.
+The functions in J2K_LIB.C are internal utilities mainly used for timing.
 */
 
-#ifndef __GNUC__
-#define __attribute__(x) /* */
-#endif
-
 /** @defgroup MISC MISC - Miscellaneous internal functions */
 /*@{*/
 
@@ -49,85 +45,6 @@ Difference in successive opj_clock() calls tells you the elapsed time
 */
 double opj_clock(void);
 
-/**
-Allocate a memory block with elements initialized to 0
-@param size Bytes to allocate
-@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
-*/
-void* __attribute__ ((malloc)) opj_malloc( size_t size );
-
-/**
-Allocate memory aligned to a 16 byte boundry
-@param size Bytes to allocate
-@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
-*/
-/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
-#ifdef WIN32
-       /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
-       #ifdef __GNUC__
-               #include <mm_malloc.h>
-               #define HAVE_MM_MALLOC
-       #else /* MSVC, Intel C++ */
-               #include <malloc.h>
-               #ifdef _mm_malloc
-                       #define HAVE_MM_MALLOC
-               #endif
-       #endif
-#else /* Not WIN32 */
-       #if defined(__sun)
-               #define HAVE_MEMALIGN
-       /* Linux x86_64 and OSX always align allocations to 16 bytes */
-       #elif !defined(__amd64__) && !defined(__APPLE__)
-               /* FIXME: Yes, this is a big assumption */
-               #define HAVE_POSIX_MEMALIGN
-       #endif
-#endif
-
-#define opj_aligned_malloc(size) malloc(size)
-#define opj_aligned_free(m) free(m)
-
-#ifdef HAVE_MM_MALLOC
-       #undef opj_aligned_malloc
-       #define opj_aligned_malloc(size) _mm_malloc(size, 16)
-       #undef opj_aligned_free
-       #define opj_aligned_free(m) _mm_free(m)
-#endif
-
-#ifdef HAVE_MEMALIGN
-       extern void* memalign(size_t, size_t);
-       #undef opj_aligned_malloc
-       #define opj_aligned_malloc(size) memalign(16, (size))
-       #undef opj_aligned_free
-       #define opj_aligned_free(m) free(m)
-#endif
-
-#ifdef HAVE_POSIX_MEMALIGN
-       #undef opj_aligned_malloc
-       extern int posix_memalign(void**, size_t, size_t);
-
-       static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
-               void* mem = NULL;
-               posix_memalign(&mem, 16, size);
-               return mem;
-       }
-       #undef opj_aligned_free
-       #define opj_aligned_free(m) free(m)
-#endif
-
-/**
-Reallocate memory blocks.
-@param memblock Pointer to previously allocated memory block
-@param size New size in bytes
-@return Returns a void pointer to the reallocated (and possibly moved) memory block
-*/
-void* __attribute__ ((malloc)) opj_realloc( void *memblock, size_t size );
-
-/**
-Deallocates or frees a memory block.
-@param memblock Previously allocated memory block to be freed
-*/
-void opj_free( void *memblock );
-
 /* ----------------------------------------------------------------------- */
 /*@}*/
 
index 92abf2e131dd0a198392a006874a5be395acb900..14f9493c4015ed41d331b304ddb7642916bc55e6 100644 (file)
@@ -507,7 +507,7 @@ static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
 /* ----------------------------------------------------------------------- */
 
 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
-       opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
+       opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
        if(jp2) {
                jp2->cinfo = cinfo;
                /* create the J2K codec */
index 72152a5ea4b6c08323c3ab73cc022ddf575e47e2..7599a71f6c4c13b810edec0de305bce0cf62e4d6 100644 (file)
  ==========================================================
 */
 
+/* Ignore GCC attributes if this is not GCC */
+#ifndef __GNUC__
+       #define __attribute__(x) /* __attribute__(x) */
+#endif
+
 /*
 The inline keyword is supported by C99 but not by C90. 
 Most compilers implement their own version of this keyword ... 
@@ -72,6 +77,7 @@ Most compilers implement their own version of this keyword ...
 #endif /* INLINE */
 
 #include "j2k_lib.h"
+#include "opj_malloc.h"
 #include "event.h"
 #include "cio.h"
 
diff --git a/libopenjpeg/opj_malloc.h b/libopenjpeg/opj_malloc.h
new file mode 100644 (file)
index 0000000..9b7c895
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2005, Herv� Drolon, FreeImage Team
+ * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __OPJ_MALLOC_H
+#define __OPJ_MALLOC_H
+/**
+@file opj_malloc.h
+@brief Internal functions
+
+The functions in opj_malloc.h are internal utilities used for memory management.
+*/
+
+/** @defgroup MISC MISC - Miscellaneous internal functions */
+/*@{*/
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Allocate an uninitialized memory block
+@param size Bytes to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+#define opj_malloc(size) malloc(size)
+
+/**
+Allocate a memory block with elements initialized to 0
+@param num Blocks to allocate
+@param size Bytes per block to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+#define opj_calloc(num, size) calloc(num, size)
+
+/**
+Allocate memory aligned to a 16 byte boundry
+@param size Bytes to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
+#ifdef WIN32
+       /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
+       #ifdef __GNUC__
+               #include <mm_malloc.h>
+               #define HAVE_MM_MALLOC
+       #else /* MSVC, Intel C++ */
+               #include <malloc.h>
+               #ifdef _mm_malloc
+                       #define HAVE_MM_MALLOC
+               #endif
+       #endif
+#else /* Not WIN32 */
+       #if defined(__sun)
+               #define HAVE_MEMALIGN
+       /* Linux x86_64 and OSX always align allocations to 16 bytes */
+       #elif !defined(__amd64__) && !defined(__APPLE__)
+               /* FIXME: Yes, this is a big assumption */
+               #define HAVE_POSIX_MEMALIGN
+       #endif
+#endif
+
+#define opj_aligned_malloc(size) malloc(size)
+#define opj_aligned_free(m) free(m)
+
+#ifdef HAVE_MM_MALLOC
+       #undef opj_aligned_malloc
+       #define opj_aligned_malloc(size) _mm_malloc(size, 16)
+       #undef opj_aligned_free
+       #define opj_aligned_free(m) _mm_free(m)
+#endif
+
+#ifdef HAVE_MEMALIGN
+       extern void* memalign(size_t, size_t);
+       #undef opj_aligned_malloc
+       #define opj_aligned_malloc(size) memalign(16, (size))
+       #undef opj_aligned_free
+       #define opj_aligned_free(m) free(m)
+#endif
+
+#ifdef HAVE_POSIX_MEMALIGN
+       #undef opj_aligned_malloc
+       extern int posix_memalign(void**, size_t, size_t);
+
+       static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
+               void* mem = NULL;
+               posix_memalign(&mem, 16, size);
+               return mem;
+       }
+       #undef opj_aligned_free
+       #define opj_aligned_free(m) free(m)
+#endif
+
+/**
+Reallocate memory blocks.
+@param memblock Pointer to previously allocated memory block
+@param size New size in bytes
+@return Returns a void pointer to the reallocated (and possibly moved) memory block
+*/
+#define opj_realloc(m, s) realloc(m, s)
+
+/**
+Deallocates or frees a memory block.
+@param memblock Previously allocated memory block to be freed
+*/
+#define opj_free(m) free(m)
+
+#ifdef __GNUC__
+#pragma GCC poison malloc calloc realloc free
+#endif
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __OPJ_MALLOC_H */
+
index e43be817bec94739a85d07039d7bdd9ca37ad648..b5e8656d1bc7716a3c16a4d68f1e4c2106f71d82 100644 (file)
@@ -413,17 +413,15 @@ opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno
        opj_pi_iterator_t *pi = NULL;
        opj_tcp_t *tcp = NULL;
        opj_tccp_t *tccp = NULL;
-       size_t array_size;
-       
+
        tcp = &cp->tcps[tileno];
 
-       array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
-       pi = (opj_pi_iterator_t *) opj_malloc(array_size);
+       pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
        if(!pi) {
                /* TODO: throw an error */
                return NULL;
        }
-       
+
        for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
                int maxres = 0;
                int maxprec = 0;
@@ -436,14 +434,12 @@ opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno
                pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
                pi[pino].numcomps = image->numcomps;
 
-               array_size = image->numcomps * sizeof(opj_pi_comp_t);
-               pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
+               pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
                if(!pi[pino].comps) {
                        /* TODO: throw an error */
                        pi_destroy(pi, cp, tileno);
                        return NULL;
                }
-               memset(pi[pino].comps, 0, array_size);
                
                for (compno = 0; compno < pi->numcomps; compno++) {
                        int tcx0, tcy0, tcx1, tcy1;
@@ -453,8 +449,7 @@ opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno
                        comp->dy = image->comps[compno].dy;
                        comp->numresolutions = tccp->numresolutions;
 
-                       array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
-                       comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);
+                       comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
                        if(!comp->resolutions) {
                                /* TODO: throw an error */
                                pi_destroy(pi, cp, tileno);
@@ -507,8 +502,7 @@ opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno
                pi[pino].step_l = maxres * pi[pino].step_r;
                
                if (pino == 0) {
-                       array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
-                       pi[pino].include = (short int *) opj_malloc(array_size);
+                       pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
                        if(!pi[pino].include) {
                                /* TODO: throw an error */
                                pi_destroy(pi, cp, tileno);
@@ -554,15 +548,13 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti
        opj_pi_iterator_t *pi = NULL;
        opj_tcp_t *tcp = NULL;
        opj_tccp_t *tccp = NULL;
-       size_t array_size;
        
        tcp = &cp->tcps[tileno];
 
-       array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
-       pi = (opj_pi_iterator_t *) opj_malloc(array_size);
+       pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
        if(!pi) {       return NULL;}
        pi->tp_on = cp->tp_on;
-       
+
        for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
                p = tileno % cp->tw;
                q = tileno / cp->tw;
@@ -573,13 +565,11 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti
                pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
                pi[pino].numcomps = image->numcomps;
                
-               array_size = image->numcomps * sizeof(opj_pi_comp_t);
-               pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
+               pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
                if(!pi[pino].comps) {
                        pi_destroy(pi, cp, tileno);
                        return NULL;
                }
-               memset(pi[pino].comps, 0, array_size);
                
                for (compno = 0; compno < pi[pino].numcomps; compno++) {
                        int tcx0, tcy0, tcx1, tcy1;
@@ -589,8 +579,7 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti
                        comp->dy = image->comps[compno].dy;
                        comp->numresolutions = tccp->numresolutions;
 
-                       array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
-                       comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);
+                       comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
                        if(!comp->resolutions) {
                                pi_destroy(pi, cp, tileno);
                                return NULL;
@@ -653,8 +642,7 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti
                }
 
                if (pino == 0) {
-                       array_size = tcp->numlayers * pi[pino].step_l * sizeof(short int);
-                       pi[pino].include = (short int *) opj_malloc(array_size);
+                       pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
                        if(!pi[pino].include) {
                                pi_destroy(pi, cp, tileno);
                                return NULL;
index c05d05c025c68feccd1c44e51ddc0cca7bd0ef09..d359b09372cd3e09e09580e94d8c8f16c7d690f0 100644 (file)
@@ -762,7 +762,7 @@ static bool allocate_buffers(
 
        if(datasize > t1->datasize){
                opj_aligned_free(t1->data);
-               t1->data=opj_aligned_malloc(datasize * sizeof(int));
+               t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
                if(!t1->data){
                        return false;
                }
@@ -775,7 +775,7 @@ static bool allocate_buffers(
 
        if(flagssize > t1->flagssize){
                opj_aligned_free(t1->flags);
-               t1->flags=opj_aligned_malloc(flagssize * sizeof(flag_t));
+               t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
                if(!t1->flags){
                        return false;
                }
@@ -998,15 +998,15 @@ opj_t1_t* t1_create(opj_common_ptr cinfo) {
        if(!t1)
                return NULL;
 
-               t1->cinfo = cinfo;
-               /* create MQC and RAW handles */
-               t1->mqc = mqc_create();
-               t1->raw = raw_create();
+       t1->cinfo = cinfo;
+       /* create MQC and RAW handles */
+       t1->mqc = mqc_create();
+       t1->raw = raw_create();
 
-       t1->datasize=0;
        t1->data=NULL;
-       t1->flagssize=0;
        t1->flags=NULL;
+       t1->datasize=0;
+       t1->flagssize=0;
 
        return t1;
 }
@@ -1018,7 +1018,7 @@ void t1_destroy(opj_t1_t *t1) {
                raw_destroy(t1->raw);
                opj_aligned_free(t1->data);
                opj_aligned_free(t1->flags);
-               free(t1);
+               opj_free(t1);
        }
 }
 
index 8d91b851813a3313222d51db7eedcb201e79ab83..2a8a62235bd6c8aae80697eebe7229174b64cb05 100644 (file)
@@ -313,7 +313,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                                                prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
                                                prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
 
-                                               prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
+                                               prc->cblks = (opj_tcd_cblk_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_t));
                                                prc->incltree = tgt_create(prc->cw, prc->ch);
                                                prc->imsbtree = tgt_create(prc->cw, prc->ch);
                                                
@@ -548,7 +548,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                                prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
 
                                                opj_free(prc->cblks);
-                                               prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
+                                               prc->cblks = (opj_tcd_cblk_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_t));
 
                                                if (prc->incltree != NULL) {
                                                        tgt_destroy(prc->incltree);
@@ -603,7 +603,7 @@ void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
                tileno = cp->tileno[j];         
                tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
                tile->numcomps = image->numcomps;
-               tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
+               tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
        }
 
        for (i = 0; i < image->numcomps; i++) {
@@ -799,6 +799,7 @@ void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
                                                cblk->y0 = int_max(cblkystart, prc->y0);
                                                cblk->x1 = int_min(cblkxend, prc->x1);
                                                cblk->y1 = int_min(cblkyend, prc->y1);
+                                               cblk->numsegs = 0;
                                        }
                                } /* precno */
                        } /* bandno */
@@ -1178,7 +1179,7 @@ int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, op
                                cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
                                cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
                        }
-                       cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numcomps * cstr_info->numlayers * numpacks * sizeof(opj_packet_info_t));
+                       cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
                }
                /* << INDEX */
                
index 30b0aee4d3e96adcc91f8aa71a1a08005c593984..a5dbcd3cef4fdc4f44da14ebe236c2d552aa1d11 100644 (file)
@@ -71,7 +71,7 @@ opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) {
                return NULL;
        }
 
-       tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));
+       tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t));
        if(!tree->nodes) {
                opj_free(tree);
                return NULL;
index eff86063b0abdeed200b5533947fdb991d1fcfbb..6ecaf8341659fd1474b9d9db29290c9cd25e9184 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "opj_includes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "openjpeg.h"
+#include "j2k.h"
+#include "jp2.h"
 #include "mj2.h"
 
 /* -------------------------------------------------------------------------- */
index c780fd116eea747f9efd123a9385ea6d8ae7cf87..299b8e6625e06f81d85ce6544c13ff44e7e1a6fe 100644 (file)
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include "opj_includes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "openjpeg.h"
+#include "j2k_lib.h"
+#include "j2k.h"
+#include "jp2.h"
+#include "cio.h"
 #include "mj2.h"
 #include "mj2_convert.h"
 #include "compat/getopt.h"
@@ -697,8 +705,8 @@ int main(int argc, char **argv)
   fwrite(buf,cio_tell(cio),1,mj2file);
   offset = cio_tell(cio);
   opj_cio_close(cio);
-  opj_free(buf);
-  
+  free(buf);
+
   for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
     if (movie->tk[i].track_type != 0) {
       fprintf(stderr, "Unable to write sound or hint tracks\n");
@@ -755,7 +763,7 @@ int main(int argc, char **argv)
 
       }
                        /* free buffer data */
-                       opj_free(buf);
+                       free(buf);
                        /* free image data */
                        opj_image_destroy(img);
     }
@@ -770,14 +778,14 @@ int main(int argc, char **argv)
   cio_write(cio, offset - mdat_initpos, 4);
   fwrite(buf, 4, 1, mj2file);
   fseek(mj2file,0,SEEK_END);
-  opj_free(buf);
-  
+  free(buf);
+
   // Writing MOOV box 
        buf = (char*) malloc ((TEMP_BUF+numframes*20) * sizeof(char));
        cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
        mj2_write_moov(movie, cio);
   fwrite(buf,cio_tell(cio),1,mj2file);
-  opj_free(buf);
+  free(buf);
 
        fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time);
   
index adf9bdf70f7b1dc4051b8f65caf4b895ecb669a4..92e8eac7b0ca764ac2742dbcdabe6fc37ea1b825 100644 (file)
@@ -75,6 +75,7 @@
                                OutputFile=".\Release/frames_to_mj2.exe"\r
                                LinkIncremental="1"\r
                                SuppressStartupBanner="true"\r
+                               IgnoreDefaultLibraryNames="LIBC"\r
                                ProgramDatabaseFile=".\Release/frames_to_mj2.pdb"\r
                                SubSystem="1"\r
                                TargetMachine="1"\r
                                OutputFile=".\frames_to_mj2___Win32_Debug0/frames_to_mj2.exe"\r
                                LinkIncremental="2"\r
                                SuppressStartupBanner="true"\r
-                               IgnoreDefaultLibraryNames="libcmt"\r
+                               IgnoreDefaultLibraryNames="LIBC,LIBCMT"\r
                                GenerateDebugInformation="true"\r
                                ProgramDatabaseFile=".\frames_to_mj2___Win32_Debug0/frames_to_mj2.pdb"\r
                                SubSystem="1"\r
index 022aa23254a81b0db4cd309a3652ef417759628b..1a15550ded408f28969838e5338a7a7a84aa331e 100644 (file)
--- a/mj2/mj2.c
+++ b/mj2/mj2.c
@@ -96,9 +96,8 @@ int mj2_init_stdmovie(opj_mj2_t * movie)
   movie->brand = MJ2_MJ2;
   movie->minversion = 0;
   movie->num_cl = 2;
-  movie->cl =
-    (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
-       
+  movie->cl = (unsigned int*) opj_malloc(movie->num_cl * sizeof(unsigned int));
+
   movie->cl[0] = MJ2_MJ2;
   movie->cl[1] = MJ2_MJ2S;
   time(&ltime);                        /* Time since 1/1/70 */
@@ -139,9 +138,7 @@ int mj2_init_stdmovie(opj_mj2_t * movie)
       tk->same_sample_size = 0;
                        
       tk->num_samplestochunk = 1;      /* One sample per chunk                                      */
-      tk->sampletochunk =
-                               (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
-                               sizeof(mj2_sampletochunk_t));
+               tk->sampletochunk = (mj2_sampletochunk_t*) opj_malloc(tk->num_samplestochunk * sizeof(mj2_sampletochunk_t));
       tk->sampletochunk[0].first_chunk = 1;
       tk->sampletochunk[0].samples_per_chunk = 1;
       tk->sampletochunk[0].sample_descr_idx = 1;
@@ -158,7 +155,7 @@ int mj2_init_stdmovie(opj_mj2_t * movie)
       }
                        
       tk->num_tts = 1;
-      tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
+               tk->tts = (mj2_tts_t*) opj_malloc(tk->num_tts * sizeof(mj2_tts_t));
       tk->tts[0].sample_count = tk->num_samples;
       tk->tts[0].sample_delta = tk->timescale / tk->sample_rate;
                        
@@ -196,7 +193,7 @@ int mj2_init_stdmovie(opj_mj2_t * movie)
       tk->or_fieldcount = 1;
       tk->or_fieldorder = 0;
       tk->num_br = 2;
-      tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
+               tk->br = (unsigned int*) opj_malloc(tk->num_br * sizeof(unsigned int));
       tk->br[0] = MJ2_JP2;
       tk->br[1] = MJ2_J2P0;
       tk->num_jp2x = 0;
@@ -226,10 +223,9 @@ void mj2_tts_decompact(mj2_tk_t * tk)
   for (i = 0; i < tk->num_tts; i++) {
     tk->num_samples += tk->tts[i].sample_count;
   }
-       
-  tk->sample =
-    (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
-       
+
+  tk->sample = (mj2_sample_t*) opj_malloc(tk->num_samples * sizeof(mj2_sample_t));
+
   for (i = 0; i < tk->num_tts; i++) {
     for (j = 0; j < tk->tts[i].sample_count; j++) {
       tk->sample[j].sample_delta = tk->tts[i].sample_delta;
@@ -251,15 +247,13 @@ void mj2_stsc_decompact(mj2_tk_t * tk)
     tk->num_chunks =
       (unsigned int) ceil((double) tk->num_samples /
       (double) tk->sampletochunk[0].samples_per_chunk);
-    tk->chunk =
-      (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
+        tk->chunk = (mj2_chunk_t*) opj_malloc(tk->num_chunks * sizeof(mj2_chunk_t));
     for (k = 0; k < tk->num_chunks; k++) {
       tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
     }
     
   } else {
-    tk->chunk =
-      (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
+    tk->chunk = (mj2_chunk_t*) opj_malloc(tk->num_samples * sizeof(mj2_chunk_t));
     tk->num_chunks = 0;
     for (i = 0; i < tk->num_samplestochunk -1 ; i++) {
       for (j = tk->sampletochunk[i].first_chunk - 1;
@@ -275,7 +269,7 @@ void mj2_stsc_decompact(mj2_tk_t * tk)
       tk->chunk[k].num_samples =
                                tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
     }
-    tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
+    tk->chunk = opj_realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
   }
   
 }
@@ -396,9 +390,8 @@ int mj2_read_ftyp(opj_mj2_t * movie, opj_cio_t *cio)
   movie->brand = cio_read(cio, 4);     /* BR              */
   movie->minversion = cio_read(cio, 4);        /* MinV            */
   movie->num_cl = (box.length - 16) / 4;
-  movie->cl =
-    (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
-       
+  movie->cl = (unsigned int*) opj_malloc(movie->num_cl * sizeof(unsigned int));
+
   for (i = movie->num_cl - 1; i > -1; i--)
     movie->cl[i] = cio_read(cio, 4);   /* CLi */
        
@@ -643,12 +636,9 @@ int mj2_read_stsc(mj2_tk_t * tk, opj_cio_t *cio)
   }
        
   tk->num_samplestochunk = cio_read(cio, 4);
-       
-  tk->sampletochunk =
-    (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
-               sizeof(mj2_sampletochunk_t));
-       
-       
+
+  tk->sampletochunk = (mj2_sampletochunk_t*) opj_malloc(tk->num_samplestochunk * sizeof(mj2_sampletochunk_t));
+
   for (i = 0; i < tk->num_samplestochunk; i++) {
     tk->sampletochunk[i].first_chunk = cio_read(cio, 4);
     tk->sampletochunk[i].samples_per_chunk = cio_read(cio, 4);
@@ -725,9 +715,9 @@ int mj2_read_stts(mj2_tk_t * tk, opj_cio_t *cio)
   }
        
   tk->num_tts = cio_read(cio, 4);
-       
-  tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
-       
+
+  tk->tts = (mj2_tts_t*) opj_malloc(tk->num_tts * sizeof(mj2_tts_t));
+
   for (i = 0; i < tk->num_tts; i++) {
     tk->tts[i].sample_count = cio_read(cio, 4);
     tk->tts[i].sample_delta = cio_read(cio, 4);
@@ -905,8 +895,8 @@ int mj2_read_jp2p(mj2_tk_t * tk, opj_cio_t *cio)
        
        
   tk->num_br = (box.length - 12) / 4;
-  tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
-       
+  tk->br = (unsigned int*) opj_malloc(tk->num_br * sizeof(unsigned int));
+
   for (i = 0; i < tk->num_br; i++) {
     tk->br[i] = cio_read(cio, 4);
   }
@@ -964,9 +954,8 @@ int mj2_read_jp2x(mj2_tk_t * tk, opj_cio_t *cio)
        
        
   tk->num_jp2x = (box.length - 8);
-  tk->jp2xdata =
-    (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
-       
+  tk->jp2xdata = (unsigned char*) opj_malloc(tk->num_jp2x * sizeof(unsigned char));
+
   for (i = 0; i < tk->num_jp2x; i++) {
     tk->jp2xdata[i] = cio_read(cio, 1);
   }
@@ -1172,10 +1161,10 @@ int mj2_read_smj2(opj_image_t * img, mj2_tk_t * tk, opj_cio_t *cio)
                opj_event_msg(tk->cinfo, EVT_ERROR, "Error reading JP2H Box\n");
     return 1;
   }
-  
-  tk->jp2_struct.comps = (opj_jp2_comps_t *) malloc(tk->jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
-  tk->jp2_struct.cl = (int *) malloc(sizeof(int));
-       
+
+  tk->jp2_struct.comps = (opj_jp2_comps_t*) opj_malloc(tk->jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
+  tk->jp2_struct.cl = (int*) opj_malloc(sizeof(int));
+
   tk->num_br = 0;
   tk->num_jp2x = 0;
        
@@ -2046,8 +2035,8 @@ int mj2_read_hdlr(mj2_tk_t * tk, opj_cio_t *cio)
   cio_skip(cio,12);                    /* Reserved */
        
   tk->name_size = box.length - 32;
-       
-  tk->name = (char *) malloc(tk->name_size * sizeof(char));
+
+  tk->name = (char*) opj_malloc(tk->name_size * sizeof(char));
   for (i = 0; i < tk->name_size; i++) {
     tk->name[i] = cio_read(cio, 1);    /* Name */
   }
@@ -2581,10 +2570,9 @@ int mj2_read_moov(opj_mj2_t * movie, opj_image_t * img, opj_cio_t *cio)
        
   if (mj2_read_mvhd(movie, cio))
     return 1;
-       
-  movie->tk =
-    (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
-       
+
+  movie->tk = (mj2_tk_t*) opj_malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
+
   for (i = 0; cio_tell(cio) - box.init_pos < box.length; i++) {
                mj2_tk_t *tk = &movie->tk[i];
                tk->cinfo = movie->cinfo;
@@ -2622,8 +2610,8 @@ int mj2_read_struct(FILE *file, opj_mj2_t *movie) {
        opj_cio_t *cio;
        
        /* open a byte stream for reading */    
-       src = (char*) malloc (300 * sizeof(char));      
-         
+       src = (char*) opj_malloc(300 * sizeof(char));   
+
        /* Assuming that jp and ftyp markers size do
      not exceed 300 bytes */
   fread(src,300,1, file);  
@@ -2706,7 +2694,7 @@ int mj2_read_struct(FILE *file, opj_mj2_t *movie) {
   }    
 
   fseek(file,foffset,SEEK_SET);
-  src = realloc(src,box.length);
+  src = opj_realloc(src,box.length);
   fsresult = fread(src,box.length,1,file);
   if (fsresult != 1) {
     opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read MOOV box\n"); 
@@ -2717,8 +2705,8 @@ int mj2_read_struct(FILE *file, opj_mj2_t *movie) {
   
   if (mj2_read_moov(movie, &img, cio))
     return 1;
-  
-  free(src);
+
+  opj_free(src);
   return 0;
 }
 
@@ -2728,12 +2716,12 @@ int mj2_read_struct(FILE *file, opj_mj2_t *movie) {
 
 opj_dinfo_t* mj2_create_decompress() {
        opj_mj2_t* mj2;
-       opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));
+       opj_dinfo_t *dinfo = (opj_dinfo_t*) opj_calloc(1, sizeof(opj_dinfo_t));
        if(!dinfo) return NULL;
 
        dinfo->is_decompressor = true;  
 
-  mj2 = (opj_mj2_t*)opj_malloc(sizeof(opj_mj2_t));
+       mj2 = (opj_mj2_t*) opj_calloc(1, sizeof(opj_mj2_t));
        dinfo->mj2_handle = mj2;
        if(mj2) {
                mj2->cinfo = (opj_common_ptr)dinfo;
@@ -2806,10 +2794,10 @@ void mj2_destroy_decompress(opj_mj2_t *movie) {
 
 opj_cinfo_t* mj2_create_compress() {
        opj_mj2_t* mj2;
-       opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
+       opj_cinfo_t *cinfo = (opj_cinfo_t*) opj_calloc(1, sizeof(opj_cinfo_t));
        if(!cinfo) return NULL;
 
-  mj2 = (opj_mj2_t*)opj_malloc(sizeof(opj_mj2_t));
+       mj2 = (opj_mj2_t*) opj_calloc(1, sizeof(opj_mj2_t));
        cinfo->mj2_handle = mj2;
        if(mj2) {
                mj2->cinfo = (opj_common_ptr)cinfo;
@@ -2831,16 +2819,14 @@ void mj2_setup_encoder(opj_mj2_t *movie, mj2_cparameters_t *parameters) {
 
                movie->brand = MJ2_MJ2;  // One brand: MJ2
                movie->num_cl = 2;        // Two compatible brands: MJ2 and MJ2S
-               movie->cl = (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
+               movie->cl = (unsigned int*) opj_malloc(movie->num_cl * sizeof(unsigned int));
                movie->cl[0] = MJ2_MJ2;
                movie->cl[1] = MJ2_MJ2S;
                movie->minversion = 0;    // Minimum version: 0         
 
-               movie->tk = (mj2_tk_t*) malloc (sizeof(mj2_tk_t)); //Memory allocation for the video track
-               movie->tk[0].sample = (mj2_sample_t*) malloc (sizeof(mj2_sample_t));
-               movie->tk[0].chunk = (mj2_chunk_t *) malloc(sizeof(mj2_chunk_t));  
-               movie->tk[0].track_type = 0;      // Video track
+               movie->tk = (mj2_tk_t*) opj_malloc(sizeof(mj2_tk_t)); //Memory allocation for the video track
                movie->tk[0].track_ID = 1;        // Track ID = 1 
+               movie->tk[0].track_type = 0;      // Video track
                movie->tk[0].Dim[0] = parameters->Dim[0];
                movie->tk[0].Dim[1] = parameters->Dim[1];
                movie->tk[0].w = parameters->w;
@@ -2848,17 +2834,19 @@ void mj2_setup_encoder(opj_mj2_t *movie, mj2_cparameters_t *parameters) {
                movie->tk[0].CbCr_subsampling_dx = parameters->CbCr_subsampling_dx;
                movie->tk[0].CbCr_subsampling_dy = parameters->CbCr_subsampling_dy;
                movie->tk[0].sample_rate = parameters->frame_rate;
-               
+               movie->tk[0].name_size = 0;
+               movie->tk[0].chunk = (mj2_chunk_t*) opj_malloc(sizeof(mj2_chunk_t));  
+               movie->tk[0].sample = (mj2_sample_t*) opj_malloc(sizeof(mj2_sample_t));
+
                jp2_struct = &movie->tk[0].jp2_struct;
                jp2_struct->numcomps = 3;       // NC           
-               jp2_struct->comps =
-                       (opj_jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(opj_jp2_comps_t));
+               jp2_struct->comps = (opj_jp2_comps_t*) opj_malloc(jp2_struct->numcomps * sizeof(opj_jp2_comps_t));
                jp2_struct->precedence = 0;   /* PRECEDENCE*/
                jp2_struct->approx = 0;   /* APPROX*/           
                jp2_struct->brand = JP2_JP2;    /* BR         */
                jp2_struct->minversion = 0;     /* MinV       */
                jp2_struct->numcl = 1;
-               jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));
+               jp2_struct->cl = (unsigned int*) opj_malloc(jp2_struct->numcl * sizeof(int));
                jp2_struct->cl[0] = JP2_JP2;    /* CL0 : JP2  */                
                jp2_struct->C = 7;      /* C : Always 7*/
                jp2_struct->UnkC = 0;      /* UnkC, colorspace specified in colr box*/
index bf4dfe6b10cf2f5b97bb3fef23a0b6be59a06cb5..a94f6612d27cd81dcd6892744cc46c3637a06cf1 100644 (file)
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include "opj_includes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "openjpeg.h"
+#include "j2k_lib.h"
+#include "j2k.h"
+#include "jp2.h"
 #include "mj2.h"
 #include "mj2_convert.h"
 
index 71dab0dee23b5f90d9fbdb0fe29063b4dcca41fc..dd2af757bd57c65b4e936afdbd360d3a8fe4af71 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "opj_includes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "openjpeg.h"
+#include "j2k.h"
+#include "jp2.h"
+#include "cio.h"
 #include "mj2.h"
 
+static int int_ceildiv(int a, int b) {
+       return (a + b - 1) / b;
+}
+
 /**
 Size of memory first allocated for MOOV box
 */