summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--LibOpenJPEG.vcproj6
-rw-r--r--Makefile2
-rw-r--r--libopenjpeg/image.c4
-rw-r--r--libopenjpeg/j2k.c68
-rw-r--r--libopenjpeg/j2k_lib.c17
-rw-r--r--libopenjpeg/j2k_lib.h85
-rw-r--r--libopenjpeg/jp2.c2
-rw-r--r--libopenjpeg/opj_includes.h6
-rw-r--r--libopenjpeg/opj_malloc.h140
-rw-r--r--libopenjpeg/pi.c34
-rw-r--r--libopenjpeg/t1.c18
-rw-r--r--libopenjpeg/tcd.c9
-rw-r--r--libopenjpeg/tgt.c2
-rw-r--r--mj2/extract_j2k_from_mj2.c8
-rw-r--r--mj2/frames_to_mj2.c22
-rw-r--r--mj2/frames_to_mj2.vcproj3
-rw-r--r--mj2/mj2.c112
-rw-r--r--mj2/mj2_to_frames.c9
-rw-r--r--mj2/wrap_j2k_in_mj2.c13
20 files changed, 324 insertions, 248 deletions
diff --git a/ChangeLog b/ChangeLog
index 9df723ea..a5c2a87b 100644
--- 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.
diff --git a/LibOpenJPEG.vcproj b/LibOpenJPEG.vcproj
index 505f89b2..8d217d12 100644
--- a/LibOpenJPEG.vcproj
+++ b/LibOpenJPEG.vcproj
@@ -3,7 +3,7 @@
ProjectType="Visual C++"
Version="8,00"
Name="LibOpenJPEG"
- ProjectGUID="{BDB8C37B-824E-4617-827C-B13E2F015EFE}"
+ ProjectGUID="{4F27AA53-4181-4A1A-8238-3931B0A41048}"
>
<Platforms>
<Platform
@@ -604,6 +604,10 @@
>
</File>
<File
+ RelativePath=".\libopenjpeg\opj_malloc.h"
+ >
+ </File>
+ <File
RelativePath="libopenjpeg\pi.h"
>
</File>
diff --git a/Makefile b/Makefile
index eb97b940..f7564984 100644
--- 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:
diff --git a/libopenjpeg/image.c b/libopenjpeg/image.c
index 6698f459..ea8e59ea 100644
--- a/libopenjpeg/image.c
+++ b/libopenjpeg/image.c
@@ -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);
diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c
index 042b8643..114ee9ec 100644
--- a/libopenjpeg/j2k.c
+++ b/libopenjpeg/j2k.c
@@ -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);
diff --git a/libopenjpeg/j2k_lib.c b/libopenjpeg/j2k_lib.c
index 62774320..91aee007 100644
--- a/libopenjpeg/j2k_lib.c
+++ b/libopenjpeg/j2k_lib.c
@@ -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);
-}
-
-
diff --git a/libopenjpeg/j2k_lib.h b/libopenjpeg/j2k_lib.h
index 99f06196..7df4d367 100644
--- a/libopenjpeg/j2k_lib.h
+++ b/libopenjpeg/j2k_lib.h
@@ -29,13 +29,9 @@
@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 );
-
/* ----------------------------------------------------------------------- */
/*@}*/
diff --git a/libopenjpeg/jp2.c b/libopenjpeg/jp2.c
index 92abf2e1..14f9493c 100644
--- a/libopenjpeg/jp2.c
+++ b/libopenjpeg/jp2.c
@@ -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 */
diff --git a/libopenjpeg/opj_includes.h b/libopenjpeg/opj_includes.h
index 72152a5e..7599a71f 100644
--- a/libopenjpeg/opj_includes.h
+++ b/libopenjpeg/opj_includes.h
@@ -54,6 +54,11 @@
==========================================================
*/
+/* 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
index 00000000..9b7c8956
--- /dev/null
+++ b/libopenjpeg/opj_malloc.h
@@ -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 */
+
diff --git a/libopenjpeg/pi.c b/libopenjpeg/pi.c
index e43be817..b5e8656d 100644
--- a/libopenjpeg/pi.c
+++ b/libopenjpeg/pi.c
@@ -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;
diff --git a/libopenjpeg/t1.c b/libopenjpeg/t1.c
index c05d05c0..d359b093 100644
--- a/libopenjpeg/t1.c
+++ b/libopenjpeg/t1.c
@@ -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);
}
}
diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c
index 8d91b851..2a8a6223 100644
--- a/libopenjpeg/tcd.c
+++ b/libopenjpeg/tcd.c
@@ -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 */
diff --git a/libopenjpeg/tgt.c b/libopenjpeg/tgt.c
index 30b0aee4..a5dbcd3c 100644
--- a/libopenjpeg/tgt.c
+++ b/libopenjpeg/tgt.c
@@ -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;
diff --git a/mj2/extract_j2k_from_mj2.c b/mj2/extract_j2k_from_mj2.c
index eff86063..6ecaf834 100644
--- a/mj2/extract_j2k_from_mj2.c
+++ b/mj2/extract_j2k_from_mj2.c
@@ -26,7 +26,13 @@
* 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"
/* -------------------------------------------------------------------------- */
diff --git a/mj2/frames_to_mj2.c b/mj2/frames_to_mj2.c
index c780fd11..299b8e66 100644
--- a/mj2/frames_to_mj2.c
+++ b/mj2/frames_to_mj2.c
@@ -25,7 +25,15 @@
* 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);
diff --git a/mj2/frames_to_mj2.vcproj b/mj2/frames_to_mj2.vcproj
index adf9bdf7..92e8eac7 100644
--- a/mj2/frames_to_mj2.vcproj
+++ b/mj2/frames_to_mj2.vcproj
@@ -75,6 +75,7 @@
OutputFile=".\Release/frames_to_mj2.exe"
LinkIncremental="1"
SuppressStartupBanner="true"
+ IgnoreDefaultLibraryNames="LIBC"
ProgramDatabaseFile=".\Release/frames_to_mj2.pdb"
SubSystem="1"
TargetMachine="1"
@@ -166,7 +167,7 @@
OutputFile=".\frames_to_mj2___Win32_Debug0/frames_to_mj2.exe"
LinkIncremental="2"
SuppressStartupBanner="true"
- IgnoreDefaultLibraryNames="libcmt"
+ IgnoreDefaultLibraryNames="LIBC,LIBCMT"
GenerateDebugInformation="true"
ProgramDatabaseFile=".\frames_to_mj2___Win32_Debug0/frames_to_mj2.pdb"
SubSystem="1"
diff --git a/mj2/mj2.c b/mj2/mj2.c
index 022aa232..1a15550d 100644
--- 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*/
diff --git a/mj2/mj2_to_frames.c b/mj2/mj2_to_frames.c
index bf4dfe6b..a94f6612 100644
--- a/mj2/mj2_to_frames.c
+++ b/mj2/mj2_to_frames.c
@@ -25,7 +25,14 @@
* 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"
diff --git a/mj2/wrap_j2k_in_mj2.c b/mj2/wrap_j2k_in_mj2.c
index 71dab0de..dd2af757 100644
--- a/mj2/wrap_j2k_in_mj2.c
+++ b/mj2/wrap_j2k_in_mj2.c
@@ -26,9 +26,20 @@
* 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
*/