removed obsolete VS files
[openjpeg.git] / libopenjpeg / tcd.c
index 533fd2465e48def13d24eca4438c6dc628835068..7f154f4b03e7bddfdaf43415331b4a16415b1662 100644 (file)
@@ -33,7 +33,7 @@
 #include "opj_includes.h"
 
 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
-       int tileno, compno, resno, bandno, precno, cblkno;
+       int tileno, compno, resno, bandno, precno;//, cblkno;
 
        fprintf(fd, "image {\n");
        fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
@@ -68,6 +68,7 @@ void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
                                                fprintf(fd,
                                                        "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
                                                        prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
+                                               /*
                                                for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
                                                        opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
                                                        fprintf(fd, "            cblk {\n");
@@ -76,6 +77,7 @@ void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
                                                                cblk->x0, cblk->y0, cblk->x1, cblk->y1);
                                                        fprintf(fd, "            }\n");
                                                }
+                                               */
                                                fprintf(fd, "          }\n");
                                        }
                                        fprintf(fd, "        }\n");
@@ -313,7 +315,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_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_t));
+                                               prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
                                                prc->incltree = tgt_create(prc->cw, prc->ch);
                                                prc->imsbtree = tgt_create(prc->cw, prc->ch);
                                                
@@ -323,13 +325,18 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                                                        int cblkxend = cblkxstart + (1 << cblkwidthexpn);
                                                        int cblkyend = cblkystart + (1 << cblkheightexpn);
                                                        
-                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                                       opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
 
                                                        /* code-block size (global) */
                                                        cblk->x0 = int_max(cblkxstart, prc->x0);
                                                        cblk->y0 = int_max(cblkystart, prc->y0);
                                                        cblk->x1 = int_min(cblkxend, prc->x1);
                                                        cblk->y1 = int_min(cblkyend, prc->y1);
+                                                       cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
+                                                       /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
+                                                       cblk->data += 2;
+                                                       cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
+                                                       cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
                                                }
                                        }
                                }
@@ -341,7 +348,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
 }
 
 void tcd_free_encode(opj_tcd_t *tcd) {
-       int tileno, compno, resno, bandno, precno;
+       int tileno, compno, resno, bandno, precno, cblkno;
 
        for (tileno = 0; tileno < 1; tileno++) {
                opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
@@ -366,8 +373,12 @@ void tcd_free_encode(opj_tcd_t *tcd) {
                                                        tgt_destroy(prc->imsbtree);     
                                                        prc->imsbtree = NULL;
                                                }
-                                               opj_free(prc->cblks);
-                                               prc->cblks = NULL;
+                                               for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+                                                       opj_free(prc->cblks.enc[cblkno].data - 2);
+                                                       opj_free(prc->cblks.enc[cblkno].layers);
+                                                       opj_free(prc->cblks.enc[cblkno].passes);
+                                               }
+                                               opj_free(prc->cblks.enc);
                                        } /* for (precno */
                                        opj_free(band->precincts);
                                        band->precincts = NULL;
@@ -407,12 +418,19 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                /* Modification of the RATE >> */
                for (j = 0; j < tcp->numlayers; j++) {
                        tcp->rates[j] = tcp->rates[j] ? 
-                                               ((float) (tile->numcomps 
-                                                               * (tile->x1 - tile->x0) 
-                                                               * (tile->y1 - tile->y0) 
-                                                               * image->comps[0].prec))/ 
-                                               (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) 
-                                               : 0;
+                               cp->tp_on ? 
+                                       (((float) (tile->numcomps 
+                                       * (tile->x1 - tile->x0) 
+                                       * (tile->y1 - tile->y0)
+                                       * image->comps[0].prec))
+                                       /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
+                                       :
+                               ((float) (tile->numcomps 
+                                       * (tile->x1 - tile->x0) 
+                                       * (tile->y1 - tile->y0) 
+                                       * image->comps[0].prec))/ 
+                                       (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
+                                       : 0;
 
                        if (tcp->rates[j]) {
                                if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
@@ -547,8 +565,8 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                                prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
                                                prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
 
-                                               opj_free(prc->cblks);
-                                               prc->cblks = (opj_tcd_cblk_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_t));
+                                               opj_free(prc->cblks.enc);
+                                               prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
 
                                                if (prc->incltree != NULL) {
                                                        tgt_destroy(prc->incltree);
@@ -566,13 +584,18 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                                        int cblkxend = cblkxstart + (1 << cblkwidthexpn);
                                                        int cblkyend = cblkystart + (1 << cblkheightexpn);
 
-                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
-                                                       
+                                                       opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
+
                                                        /* code-block size (global) */
                                                        cblk->x0 = int_max(cblkxstart, prc->x0);
                                                        cblk->y0 = int_max(cblkystart, prc->y0);
                                                        cblk->x1 = int_min(cblkxend, prc->x1);
                                                        cblk->y1 = int_min(cblkyend, prc->y1);
+                                                       cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
+                                                       /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
+                                                       cblk->data += 2;
+                                                       cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
+                                                       cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
                                                }
                                        } /* precno */
                                } /* bandno */
@@ -641,7 +664,6 @@ void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
                w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
                h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
 
-               image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
                image->comps[i].w = w;
                image->comps[i].h = h;
                image->comps[i].x0 = x0;
@@ -671,8 +693,6 @@ void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
                tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
                tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
 
-               /* The +3 is headroom required by the vectorized DWT */
-               tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
                tilec->numresolutions = tccp->numresolutions;
                tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
                
@@ -782,9 +802,9 @@ void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
                                        brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
                                        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.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
+
                                        prc->incltree = tgt_create(prc->cw, prc->ch);
                                        prc->imsbtree = tgt_create(prc->cw, prc->ch);
                                        
@@ -793,9 +813,11 @@ void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
                                                int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
                                                int cblkxend = cblkxstart + (1 << cblkwidthexpn);
                                                int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
-                                               
+
+                                               opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
+                                               cblk->data = NULL;
+                                               cblk->segs = NULL;
                                                /* code-block size (global) */
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
                                                cblk->x0 = int_max(cblkxstart, prc->x0);
                                                cblk->y0 = int_max(cblkystart, prc->y0);
                                                cblk->x1 = int_min(cblkxend, prc->x1);
@@ -840,7 +862,7 @@ void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
                                for (precno = 0; precno < res->pw * res->ph; precno++) {
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
                                                opj_tcd_layer_t *layer = &cblk->layers[layno];
                                                int n;
                                                int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
@@ -921,7 +943,7 @@ void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
                                for (precno = 0; precno < res->pw * res->ph; precno++) {
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
                                                opj_tcd_layer_t *layer = &cblk->layers[layno];
                                                
                                                int n;
@@ -1005,7 +1027,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
 
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
 
                                                for (passno = 0; passno < cblk->totalpasses; passno++) {
                                                        opj_tcd_pass_t *pass = &cblk->passes[passno];
@@ -1072,7 +1094,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                        opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
                        double thresh = 0;
 
-                       for (i = 0; i < 32; i++) {
+                       for (i = 0; i < 128; i++) {
                                int l = 0;
                                double distoachieved = 0;       /* fixed_quality */
                                thresh = (lo + hi) / 2;
@@ -1081,7 +1103,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                                
                                if (cp->fixed_quality) {        /* fixed_quality */
                                        if(cp->cinema){
-                                               l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+                                               l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
                                                if (l == -999) {
                                                        lo = thresh;
                                                        continue;
@@ -1107,7 +1129,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                                                lo = thresh;
                                        }
                                } else {
-                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
                                        /* TODO: what to do with l ??? seek / tell ??? */
                                        /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
                                        if (l == -999) {
@@ -1271,7 +1293,7 @@ int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, op
        }
 
        t2 = t2_create(tcd->cinfo, image, cp);
-       l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
+       l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
        t2_destroy(t2);
        
        /*---------------CLEAN-------------------*/
@@ -1352,7 +1374,10 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
        t1_time = opj_clock();  /* time needed to decode a tile */
        t1 = t1_create(tcd->cinfo);
        for (compno = 0; compno < tile->numcomps; ++compno) {
-               t1_decode_cblks(t1, &tile->comps[compno], &tcd->tcp->tccps[compno]);
+               opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
+               /* The +3 is headroom required by the vectorized DWT */
+               tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
+               t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
        }
        t1_destroy(t1);
        t1_time = opj_clock() - t1_time;
@@ -1368,9 +1393,9 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
                if (tcd->cp->reduce != 0) {
                        tcd->image->comps[compno].resno_decoded =
                                tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
-                       if (tcd->image->comps[compno].resno_decoded < 1) {
-                               opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove is higher than the number "
-                                       " of resolutions in the original codestream\nModify the cp_reduce parameter.\n");
+                       if (tcd->image->comps[compno].resno_decoded < 0) {                              
+                               opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
+                                       " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
                                return false;
                        }
                }
@@ -1423,6 +1448,9 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
                int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
 
                int i, j;
+               if(!imagec->data){
+                       imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
+               }
                if(tcd->tcp->tccps[compno].qmfbid == 1) {
                        for(j = res->y0; j < res->y1; ++j) {
                                for(i = res->x0; i < res->x1; ++i) {
@@ -1441,16 +1469,12 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
                                }
                        }
                }
+               opj_aligned_free(tilec->data);
        }
 
        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_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
-               tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
-       }
-
        if (eof) {
                return false;
        }
@@ -1460,7 +1484,7 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
 
 void tcd_free_decode(opj_tcd_t *tcd) {
        opj_tcd_image_t *tcd_image = tcd->tcd_image;    
-       if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);
+       opj_free(tcd_image->tiles);
 }
 
 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
@@ -1477,15 +1501,16 @@ void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
                                opj_tcd_band_t *band = &res->bands[bandno];
                                for (precno = 0; precno < res->ph * res->pw; precno++) {
                                        opj_tcd_precinct_t *prec = &band->precincts[precno];
-                                       if (prec->cblks != NULL) opj_free(prec->cblks);
                                        if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
                                        if (prec->incltree != NULL) tgt_destroy(prec->incltree);
                                }
-                               if (band->precincts != NULL) opj_free(band->precincts);
+                               opj_free(band->precincts);
                        }
                }
-               if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
+               opj_free(tilec->resolutions);
        }
-       if (tile->comps != NULL) opj_free(tile->comps);
+       opj_free(tile->comps);
 }
 
+
+