* [FOD] Robustified MJ2 codecs
* [Parvatha] Solved problems with codec reading from image file directory when filename had more than one "." in name
* [Callum Lerwick] Minor cleanup patch, that gets rid of a bunch of "old style declaration" warnings from Intel's compiler
+* [Callum Lerwick] Aligned malloc using Intel's _mm_malloc(). Cleanup on the t1 memory allocation, getting rid of some leftover debug code
August 20, 2007
+ [FOD] Added support for the TGA file format in the codec
The functions in J2K_LIB.C are internal utilities mainly used for memory management.
*/
+#ifndef __GCC__
+#define __attribute__(x) /* */
+#endif
+
/** @defgroup MISC MISC - Miscellaneous internal functions */
/*@{*/
@param size Bytes to allocate
@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
*/
-void* opj_malloc( size_t size );
+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
+*/
+#include <xmmintrin.h>
+#define opj_aligned_malloc(size) _mm_malloc(size, 16)
+#define opj_aligned_free(m) _mm_free(m)
/**
Reallocate memory blocks.
@param size New size in bytes
@return Returns a void pointer to the reallocated (and possibly moved) memory block
*/
-void* opj_realloc( void *memblock, size_t size );
+void* __attribute__ ((malloc)) opj_realloc( void *memblock, size_t size );
/**
Deallocates or frees a memory block.
return wmsedec;
}
-static void allocate_buffers(
+static bool allocate_buffers(
opj_t1_t *t1,
int w,
int h)
{
- int datasize;
+ int datasize=w * h;
int flagssize;
- datasize=w * h;
- //fprintf(stderr,"w=%i h=%i datasize=%i flagssize=%i\n",w,h,datasize,flagssize);
-
if(datasize > t1->datasize){
- //fprintf(stderr,"Allocating t1->data: datasize=%i\n",datasize);
- free(t1->data);
- t1->data=malloc(datasize * sizeof(int));
+ opj_aligned_free(t1->data);
+ t1->data=opj_aligned_malloc(datasize * sizeof(int));
if(!t1->data){
- return;
+ return false;
}
t1->datasize=datasize;
}
- //memset(t1->data,0xff,t1->datasize);
memset(t1->data,0,datasize * sizeof(int));
t1->flags_stride=w+2;
- flagssize=t1->flags_stride * (h+2);
+ flagssize = t1->flags_stride * (h+2);
if(flagssize > t1->flagssize){
- //fprintf(stderr,"Allocating t1->flags: flagssize=%i\n",flagssize);
- free(t1->flags);
- t1->flags=malloc(flagssize * sizeof(flag_t));
+ opj_aligned_free(t1->flags);
+ t1->flags=opj_aligned_malloc(flagssize * sizeof(flag_t));
if(!t1->flags){
- fprintf(stderr,"Allocating t1->flags FAILED!\n");
- return;
+ return false;
}
t1->flagssize=flagssize;
}
- //memset(t1->flags,0xff,t1->flagssize);
memset(t1->flags,0,flagssize * sizeof(flag_t));
t1->w=w;
t1->h=h;
+
+ return true;
}
/** mod fixed_quality */
opj_raw_t *raw = t1->raw; /* RAW component */
opj_mqc_t *mqc = t1->mqc; /* MQC component */
-
- allocate_buffers(
- t1,
- cblk->x1 - cblk->x0,
- cblk->y1 - cblk->y0);
-
+
+ if(!allocate_buffers(
+ t1,
+ cblk->x1 - cblk->x0,
+ cblk->y1 - cblk->y0))
+ {
+ return;
+ }
+
bpno = roishift + cblk->numbps - 1;
passtype = 2;
/* destroy MQC and RAW handles */
mqc_destroy(t1->mqc);
raw_destroy(t1->raw);
- free(t1->data);
- free(t1->flags);
+ opj_aligned_free(t1->data);
+ opj_aligned_free(t1->flags);
free(t1);
}
}
y += pres->y1 - pres->y0;
}
- allocate_buffers(
- t1,
- cblk->x1 - cblk->x0,
- cblk->y1 - cblk->y0);
+ if(!allocate_buffers(
+ t1,
+ cblk->x1 - cblk->x0,
+ cblk->y1 - cblk->y0))
+ {
+ return;
+ }
w = tilec->x1 - tilec->x0;
if (tcp->tccps[compno].qmfbid == 1) {
tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
- tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
+ tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
- tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
+ tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
/* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
for (resno = 0; resno < tilec->numresolutions; resno++) {
tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
- tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
+ tilec->data = (int*) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
tcd->encoding_time = opj_clock() - tcd->encoding_time;
opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
-
+
/* cleaning memory */
for (compno = 0; compno < tile->numcomps; compno++) {
opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
- opj_free(tilec->data);
+ opj_aligned_free(tilec->data);
}
}
-
+
return l;
}
tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
-
+
for (compno = 0; compno < tile->numcomps; compno++) {
- opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
+ opj_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
}
-
+
if (eof) {
return false;
}