Makes debugging a little more simple.
---
 libavcodec/jpeg2000.c    | 208 +++++++++++++++++++++++++----------------------
 libavcodec/jpeg2000dec.c |  20 ++---
 2 files changed, 122 insertions(+), 106 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 77cadd8..f5a844f 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -244,6 +244,111 @@ static void init_band_stepsize(AVCodecContext *avctx,
     band->i_stepsize = band->f_stepsize * (1 << 16);
 }
 
+static int init_prec(Jpeg2000Band *band,
+                     Jpeg2000ResLevel *reslevel,
+                     Jpeg2000Component *comp,
+                     int precno, int bandno, int reslevelno,
+                     int log2_band_prec_width,
+                     int log2_band_prec_height)
+{
+    Jpeg2000Prec *prec = band->prec + precno;
+    int nb_codeblocks, cblkno;
+
+    /* TODO: Explain formula for JPEG200 DCINEMA. */
+    /* TODO: Verify with previous count of codeblocks per band */
+
+    /* Compute P_x0 */
+    prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
+                        (1 << log2_band_prec_width);
+    prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
+
+    /* Compute P_y0 */
+    prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
+                        (1 << log2_band_prec_height);
+    prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
+
+    /* Compute P_x1 */
+    prec->coord[0][1] = prec->coord[0][0] +
+                        (1 << log2_band_prec_width);
+    prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
+
+    /* Compute P_y1 */
+    prec->coord[1][1] = prec->coord[1][0] +
+                        (1 << log2_band_prec_height);
+    prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
+
+    prec->nb_codeblocks_width =
+        ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
+                                prec->coord[0][0],
+                                band->log2_cblk_width);
+    prec->nb_codeblocks_height =
+        ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
+                                prec->coord[1][0],
+                                band->log2_cblk_height);
+
+    /* Tag trees initialization */
+    prec->cblkincl =
+        ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
+                                  prec->nb_codeblocks_height);
+    if (!prec->cblkincl)
+        return AVERROR(ENOMEM);
+
+    prec->zerobits =
+        ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
+                                  prec->nb_codeblocks_height);
+    if (!prec->zerobits)
+        return AVERROR(ENOMEM);
+
+    nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
+    prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
+    if (!prec->cblk)
+        return AVERROR(ENOMEM);
+    for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
+        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+        uint16_t Cx0, Cy0;
+
+        /* Compute coordinates of codeblocks */
+        /* Compute Cx0*/
+        Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << 
band->log2_cblk_width;
+        Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << 
band->log2_cblk_width);
+        cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
+
+        /* Compute Cy0*/
+        Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << 
band->log2_cblk_height;
+        Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << 
band->log2_cblk_height);
+        cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
+
+        /* Compute Cx1 */
+        cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
+                                  prec->coord[0][1]);
+
+        /* Compute Cy1 */
+        cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
+                                  prec->coord[1][1]);
+        /* Update code-blocks coordinates according sub-band position */
+        if ((bandno + !!reslevelno) & 1) {
+            cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
+                                 comp->reslevel[reslevelno-1].coord[0][0];
+            cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
+                                 comp->reslevel[reslevelno-1].coord[0][0];
+        }
+        if ((bandno + !!reslevelno) & 2) {
+            cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
+                                 comp->reslevel[reslevelno-1].coord[1][0];
+            cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
+                                 comp->reslevel[reslevelno-1].coord[1][0];
+        }
+
+        cblk->zero      = 0;
+        cblk->lblock    = 3;
+        cblk->length    = 0;
+        cblk->lengthinc = 0;
+        cblk->npasses   = 0;
+    }
+
+    return 0;
+}
+
 static int init_band(AVCodecContext *avctx,
                      Jpeg2000ResLevel *reslevel,
                      Jpeg2000Component *comp,
@@ -255,9 +360,9 @@ static int init_band(AVCodecContext *avctx,
     Jpeg2000Band *band = reslevel->band + bandno;
     uint8_t log2_band_prec_width, log2_band_prec_height;
     int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 
15444-1:2002 B.5
-    int cblkno, precno;
+    int precno;
     int nb_precincts;
-    int i, j;
+    int i, j, ret;
 
     init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, 
reslevelno, cbps);
 
@@ -314,100 +419,11 @@ static int init_band(AVCodecContext *avctx,
         return AVERROR(ENOMEM);
 
     for (precno = 0; precno < nb_precincts; precno++) {
-        Jpeg2000Prec *prec = band->prec + precno;
-        int nb_codeblocks;
-
-        /* TODO: Explain formula for JPEG200 DCINEMA. */
-        /* TODO: Verify with previous count of codeblocks per band */
-
-        /* Compute P_x0 */
-        prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
-                            (1 << log2_band_prec_width);
-        prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
-
-        /* Compute P_y0 */
-        prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
-                            (1 << log2_band_prec_height);
-        prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
-
-        /* Compute P_x1 */
-        prec->coord[0][1] = prec->coord[0][0] +
-                            (1 << log2_band_prec_width);
-        prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
-
-        /* Compute P_y1 */
-        prec->coord[1][1] = prec->coord[1][0] +
-                            (1 << log2_band_prec_height);
-        prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
-
-        prec->nb_codeblocks_width =
-            ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
-                                    prec->coord[0][0],
-                                    band->log2_cblk_width);
-        prec->nb_codeblocks_height =
-            ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
-                                    prec->coord[1][0],
-                                    band->log2_cblk_height);
-
-        /* Tag trees initialization */
-        prec->cblkincl =
-            ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
-                                      prec->nb_codeblocks_height);
-        if (!prec->cblkincl)
-            return AVERROR(ENOMEM);
-
-        prec->zerobits =
-            ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
-                                      prec->nb_codeblocks_height);
-        if (!prec->zerobits)
-            return AVERROR(ENOMEM);
-
-        nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
-        prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
-        if (!prec->cblk)
-            return AVERROR(ENOMEM);
-        for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
-            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
-            uint16_t Cx0, Cy0;
-
-            /* Compute coordinates of codeblocks */
-            /* Compute Cx0*/
-            Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << 
band->log2_cblk_width;
-            Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << 
band->log2_cblk_width);
-            cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
-
-            /* Compute Cy0*/
-            Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << 
band->log2_cblk_height;
-            Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << 
band->log2_cblk_height);
-            cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
-
-            /* Compute Cx1 */
-            cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
-                                      prec->coord[0][1]);
-
-            /* Compute Cy1 */
-            cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
-                                      prec->coord[1][1]);
-            /* Update code-blocks coordinates according sub-band position */
-            if ((bandno + !!reslevelno) & 1) {
-                cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
-                                     comp->reslevel[reslevelno-1].coord[0][0];
-                cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
-                                     comp->reslevel[reslevelno-1].coord[0][0];
-            }
-            if ((bandno + !!reslevelno) & 2) {
-                cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
-                                     comp->reslevel[reslevelno-1].coord[1][0];
-                cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
-                                     comp->reslevel[reslevelno-1].coord[1][0];
-            }
-
-            cblk->zero      = 0;
-            cblk->lblock    = 3;
-            cblk->length    = 0;
-            cblk->lengthinc = 0;
-            cblk->npasses   = 0;
-        }
+        ret = init_prec(band, reslevel, comp,
+                        precno, bandno, reslevelno,
+                        log2_band_prec_width, log2_band_prec_height);
+        if (ret < 0)
+            return ret;
     }
 
     return 0;
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 6f74eef..1bcaaef 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -209,15 +209,8 @@ static int get_siz(Jpeg2000DecoderContext *s)
         s->cdx[i]    = bytestream2_get_byteu(&s->g);
         s->cdy[i]    = bytestream2_get_byteu(&s->g);
 
-        if (s->cdx[i] != 1 || s->cdy[i] != 1) {
-            avpriv_request_sample(s->avctx,
-                                  "CDxy values %d %d for component %d",
-                                  s->cdx[i], s->cdy[i], i);
             if (!s->cdx[i] || !s->cdy[i])
                 return AVERROR_INVALIDDATA;
-            else
-                return AVERROR_PATCHWELCOME;
-        }
     }
 
     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, 
s->tile_width);
@@ -590,6 +583,13 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
         comp->coord_o[1][0] = FFMAX(tiley       * s->tile_height + 
s->tile_offset_y, s->image_offset_y);
         comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + 
s->tile_offset_y, s->height);
 
+        if (compno) {
+            comp->coord_o[0][0] /= s->cdx[compno];
+            comp->coord_o[0][1] /= s->cdx[compno];
+            comp->coord_o[1][0] /= s->cdy[compno];
+            comp->coord_o[1][1] /= s->cdy[compno];
+        }
+
         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
s->reduction_factor);
         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], 
s->reduction_factor);
         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
s->reduction_factor);
@@ -1155,14 +1155,14 @@ static inline void 
tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                                                                                
                   \
             y    = tile->comp[compno].coord[1][0] - s->image_offset_y;         
                   \
             line = (PIXEL *)picture->data[0] + y * linesize;                   
                   \
-            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y 
+= s->cdy[compno]) { \
+            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; 
y++) {                 \
                 PIXEL *dst;                                                    
                   \
                                                                                
                   \
                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;      
                   \
                 dst = line + x * s->ncomponents + compno;                      
                   \
                                                                                
                   \
                 if (codsty->transform == FF_DWT97) {                           
                   \
-                    for (; x < w; x += s->cdx[compno]) {                       
                   \
+                    for (; x < w; x++) {                                       
                   \
                         int val = lrintf(*datap) + (1 << (cbps - 1));          
                   \
                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 
*/                  \
                         val  = av_clip(val, 0, (1 << cbps) - 1);               
                   \
@@ -1171,7 +1171,7 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile
                         dst += s->ncomponents;                                 
                   \
                     }                                                          
                   \
                 } else {                                                       
                   \
-                    for (; x < w; x += s->cdx[compno]) {                       
                   \
+                    for (; x < w; x++) {                                       
                   \
                         int val = *i_datap + (1 << (cbps - 1));                
                   \
                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 
*/                  \
                         val  = av_clip(val, 0, (1 << cbps) - 1);               
                   \
-- 
2.5.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to