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.
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
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:
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;
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);
}
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;
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);
}
}
#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 */
}
#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
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 */
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));
}
}
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;
}
/* ----------------------------------------------------------------------- */
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;
}
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;
/* ----------------------------------------------------------------------- */
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;
}
}
/* 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;
/* 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];
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 */
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 */
tcd_free_encode(tcd);
tcd_destroy(tcd);
- free(j2k->cur_totnum_tp);
+ opj_free(j2k->cur_totnum_tp);
j2k_write_eoc(j2k);
#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);
-}
-
-
@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 */
/*@{*/
*/
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 );
-
/* ----------------------------------------------------------------------- */
/*@}*/
/* ----------------------------------------------------------------------- */
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 */
==========================================================
*/
+/* 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 ...
#endif /* INLINE */
#include "j2k_lib.h"
+#include "opj_malloc.h"
#include "event.h"
#include "cio.h"
--- /dev/null
+/*
+ * 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 */
+
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;
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;
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);
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);
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;
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;
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;
}
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;
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;
}
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;
}
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;
}
raw_destroy(t1->raw);
opj_aligned_free(t1->data);
opj_aligned_free(t1->flags);
- free(t1);
+ opj_free(t1);
}
}
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);
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);
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++) {
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 */
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 */
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;
* 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"
/* -------------------------------------------------------------------------- */
* 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"
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");
}
/* free buffer data */
- opj_free(buf);
+ free(buf);
/* free image data */
opj_image_destroy(img);
}
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);
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
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(<ime); /* Time since 1/1/70 */
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;
}
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;
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;
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;
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;
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));
}
}
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 */
}
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);
}
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);
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);
}
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);
}
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;
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 */
}
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;
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);
}
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");
if (mj2_read_moov(movie, &img, cio))
return 1;
-
- free(src);
+
+ opj_free(src);
return 0;
}
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;
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;
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;
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*/
* 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"
* 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
*/