Added option for Digital cinema profile compliant codestream. This can be chosen...
[openjpeg.git] / libopenjpeg / tcd.c
index 8454d57688089932da6ad03cafa58af2ebcd23a5..89658ce70b99650a4143d0428c94913a2ec852c0 100644 (file)
@@ -1,9 +1,10 @@
 /*
+ * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2007, Professor Benoit Macq
  * Copyright (c) 2001-2003, David Janssens
  * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
- * Copyright (c) 2005, Herv� Drolon, FreeImage Team
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -121,13 +122,6 @@ void tcd_destroy(opj_tcd_t *tcd) {
 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
        int tileno, compno, resno, bandno, precno, cblkno;
 
-       opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile */
-       opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec */
-       opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res */
-       opj_tcd_band_t *band = NULL;            /* pointer to tcd->band */
-       opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc */
-       opj_tcd_cblk_t *cblk = NULL;            /* pointer to tcd->cblk */
-
        tcd->image = image;
        tcd->cp = cp;
        tcd->tcd_image->tw = cp->tw;
@@ -143,8 +137,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
 
                /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
-               tcd->tile = tcd->tcd_image->tiles;
-               tile = tcd->tile;
+               opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
 
                /* 4 borders of the tile rescale on the image if necessary */
                tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
@@ -157,11 +150,11 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                /* Modification of the RATE >> */
                for (j = 0; j < tcp->numlayers; j++) {
                        tcp->rates[j] = tcp->rates[j] ? 
-                               int_ceildiv(tile->numcomps 
+                               ((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)
+                                       * image->comps[0].prec))/ 
+                                       (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
                                        : 0;
 
                        if (tcp->rates[j]) {
@@ -179,9 +172,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                for (compno = 0; compno < tile->numcomps; compno++) {
                        opj_tccp_t *tccp = &tcp->tccps[compno];
 
-                       /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
-                       tcd->tilec = &tile->comps[compno];
-                       tilec = tcd->tilec;
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
 
                        /* border of each tile component (global) */
                        tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
@@ -202,9 +193,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                                int cbgwidthexpn, cbgheightexpn;
                                int cblkwidthexpn, cblkheightexpn;
 
-                               /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */
-                               tcd->res = &tilec->resolutions[resno];
-                               res = tcd->res;
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
                                
                                /* border for each resolution level (global) */
                                res->x0 = int_ceildivpow2(tilec->x0, levelno);
@@ -255,8 +244,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                                        int gain, numbps;
                                        opj_stepsize_t *ss = NULL;
 
-                                       tcd->band = &res->bands[bandno];
-                                       band = tcd->band;
+                                       opj_tcd_band_t *band = &res->bands[bandno];
 
                                        band->bandno = resno == 0 ? 0 : bandno + 1;
                                        x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
@@ -298,9 +286,7 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c
                                                int cbgxend = cbgxstart + (1 << cbgwidthexpn);
                                                int cbgyend = cbgystart + (1 << cbgheightexpn);
 
-                                               /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */
-                                               tcd->prc = &band->precincts[precno];
-                                               prc = tcd->prc;
+                                               opj_tcd_precinct_t *prc = &band->precincts[precno];
 
                                                /* precinct size (global) */
                                                prc->x0 = int_max(cbgxstart, band->x0);
@@ -325,8 +311,7 @@ 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);
                                                        
-                                                       tcd->cblk = &prc->cblks[cblkno];
-                                                       cblk = tcd->cblk;
+                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
 
                                                        /* code-block size (global) */
                                                        cblk->x0 = int_max(cblkxstart, prc->x0);
@@ -346,31 +331,20 @@ 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;
 
-       opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile         */
-       opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec        */
-       opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res          */
-       opj_tcd_band_t *band = NULL;            /* pointer to tcd->band         */
-       opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc          */
-
        for (tileno = 0; tileno < 1; tileno++) {
-               tcd->tile = tcd->tcd_image->tiles;
-               tile = tcd->tile;
+               opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
 
                for (compno = 0; compno < tile->numcomps; compno++) {
-                       tcd->tilec = &tile->comps[compno];
-                       tilec = tcd->tilec;
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
 
                        for (resno = 0; resno < tilec->numresolutions; resno++) {
-                               tcd->res = &tilec->resolutions[resno];
-                               res = tcd->res;
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
 
                                for (bandno = 0; bandno < res->numbands; bandno++) {
-                                       tcd->band = &res->bands[bandno];
-                                       band = tcd->band;
+                                       opj_tcd_band_t *band = &res->bands[bandno];
 
                                        for (precno = 0; precno < res->pw * res->ph; precno++) {
-                                               tcd->prc = &band->precincts[precno];
-                                               prc = tcd->prc;
+                                               opj_tcd_precinct_t *prc = &band->precincts[precno];
 
                                                if (prc->incltree != NULL) {
                                                        tgt_destroy(prc->incltree);
@@ -400,13 +374,6 @@ void tcd_free_encode(opj_tcd_t *tcd) {
 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
        int tileno, compno, resno, bandno, precno, cblkno;
 
-       opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile */
-       opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec */
-       opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res */
-       opj_tcd_band_t *band = NULL;            /* pointer to tcd->band */
-       opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc */
-       opj_tcd_cblk_t *cblk = NULL;            /* pointer to tcd->cblk */
-
        for (tileno = 0; tileno < 1; tileno++) {
                opj_tcp_t *tcp = &cp->tcps[curtileno];
                int j;
@@ -414,8 +381,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                int p = curtileno % cp->tw;
                int q = curtileno / cp->tw;
 
-               tcd->tile = tcd->tcd_image->tiles;
-               tile = tcd->tile;
+               opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
                
                /* 4 borders of the tile rescale on the image if necessary */
                tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
@@ -429,12 +395,12 @@ 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] ? 
-                               int_ceildiv(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;
+                                               ((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) {
@@ -451,8 +417,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                for (compno = 0; compno < tile->numcomps; compno++) {
                        opj_tccp_t *tccp = &tcp->tccps[compno];
                        
-                       tcd->tilec = &tile->comps[compno];
-                       tilec = tcd->tilec;
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
 
                        /* border of each tile component (global) */
                        tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
@@ -472,8 +437,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                int cbgwidthexpn, cbgheightexpn;
                                int cblkwidthexpn, cblkheightexpn;
                                
-                               tcd->res = &tilec->resolutions[resno];
-                               res = tcd->res;
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
 
                                /* border for each resolution level (global) */
                                res->x0 = int_ceildivpow2(tilec->x0, levelno);
@@ -523,8 +487,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                        int gain, numbps;
                                        opj_stepsize_t *ss = NULL;
 
-                                       tcd->band = &res->bands[bandno];
-                                       band = tcd->band;
+                                       opj_tcd_band_t *band = &res->bands[bandno];
 
                                        band->bandno = resno == 0 ? 0 : bandno + 1;
                                        x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
@@ -557,8 +520,7 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                                int cbgxend = cbgxstart + (1 << cbgwidthexpn);
                                                int cbgyend = cbgystart + (1 << cbgheightexpn);
                                                
-                                               tcd->prc = &band->precincts[precno];
-                                               prc = tcd->prc;
+                                               opj_tcd_precinct_t *prc = &band->precincts[precno];
 
                                                /* precinct size (global) */
                                                prc->x0 = int_max(cbgxstart, band->x0);
@@ -592,8 +554,7 @@ 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);
 
-                                                       tcd->cblk = &prc->cblks[cblkno];
-                                                       cblk = tcd->cblk;
+                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
                                                        
                                                        /* code-block size (global) */
                                                        cblk->x0 = int_max(cblkxstart, prc->x0);
@@ -945,7 +906,7 @@ void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
                                                                dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
                                                        }
                                                        if (!dr) {
-                                                               if (dd)
+                                                               if (dd != 0)
                                                                        n = passno + 1;
                                                                continue;
                                                        }
@@ -983,7 +944,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
        int compno, resno, bandno, precno, cblkno, passno, layno;
        double min, max;
        double cumdisto[100];   /* fixed_quality */
-       const double K = 1;             /* 1.1; // fixed_quality */
+       const double K = 1;             /* 1.1; fixed_quality */
        double maxSE = 0;
 
        opj_cp_t *cp = tcd->cp;
@@ -998,14 +959,19 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
        for (compno = 0; compno < tcd_tile->numcomps; compno++) {
                opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
                tilec->nbpix = 0;
+
                for (resno = 0; resno < tilec->numresolutions; resno++) {
                        opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
                        for (bandno = 0; bandno < res->numbands; bandno++) {
                                opj_tcd_band_t *band = &res->bands[bandno];
+
                                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];
+
                                                for (passno = 0; passno < cblk->totalpasses; passno++) {
                                                        opj_tcd_pass_t *pass = &cblk->passes[passno];
                                                        int dr;
@@ -1042,28 +1008,28 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
                        * ((double)(tilec->nbpix));
        } /* compno */
        
-       /* add antonin index */
+       /* index file */
        if(image_info && image_info->index_on) {
-               opj_tile_info_t *info_TL = &image_info->tile[tcd->tcd_tileno];
-               info_TL->nbpix = tcd_tile->nbpix;
-               info_TL->distotile = tcd_tile->distotile;
-               info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
+               opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
+               tile_info->nbpix = tcd_tile->nbpix;
+               tile_info->distotile = tcd_tile->distotile;
+               tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
        }
-       /* dda */
        
        for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
                double lo = min;
                double hi = max;
                int success = 0;
-               int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len;
-               double goodthresh;
+               /* TODO: remove maxlen */
+               int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
+               double goodthresh = 0;
                int i;
                double distotarget;             /* fixed_quality */
                
                /* fixed_quality */
                distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
         
-               if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {
+               if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
                        opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
 
                        for (i = 0; i < 32; i++) {
@@ -1074,15 +1040,32 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
                                tcd_makelayer(tcd, layno, thresh, 0);
                                
                                if (cp->fixed_quality) {        /* fixed_quality */
-                                       distoachieved = layno == 0 ? 
-                                               tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
-                                       if (distoachieved < distotarget) {
-                                               hi = thresh;
-                                               continue;
+                                       if(cp->cinema){
+                                               l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info);
+                                               if (l == -999) {
+                                                       lo = thresh;
+                                                       continue;
+                                               }else{
+                       distoachieved = layno == 0 ? 
+                                                       tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
+                                                       if (distoachieved < distotarget) {
+                                                               hi=thresh; continue;
+                                                       }else{
+                                                               lo=thresh;
+                                                       }
+                                               }
+                                       }else{
+                                               distoachieved = (layno == 0) ? 
+                                                       tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
+                                               if (distoachieved < distotarget) {
+                                                       hi = thresh;
+                                                       continue;
+                                               }
+                                               lo = thresh;
                                        }
-                                       lo = thresh;
                                } else {
                                        l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info);
+                                       /* 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) {
                                                lo = thresh;
@@ -1110,7 +1093,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
                tcd_makelayer(tcd, layno, goodthresh, 1);
         
                /* fixed_quality */
-               cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];     
+               cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
        }
 
        return true;
@@ -1254,8 +1237,8 @@ int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, op
        
        /* cleaning memory */
        for (compno = 0; compno < tile->numcomps; compno++) {
-               tcd->tilec = &tile->comps[compno];
-               opj_free(tcd->tilec->data);
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+               opj_free(tilec->data);
        }
        
        return l;