Added option for Digital cinema profile compliant codestream. This can be chosen...
[openjpeg.git] / libopenjpeg / tcd.c
index e178f6eac8c2f2630154f258aaf966284578e22e..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
@@ -149,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]) {
@@ -394,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) {
@@ -943,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;
@@ -1019,8 +1020,8 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in
                double lo = min;
                double hi = max;
                int success = 0;
-               // TODO: remove maxlen
-               int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len;
+               /* 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 */
@@ -1039,16 +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 ???
+                                       /* 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;