[FFmpeg-soc] [soc]: r1195 - in jpeg2000: j2k.c j2k.h j2kdec.c j2kenc.c

k.nowosad subversion at mplayerhq.hu
Mon Aug 27 11:26:21 CEST 2007


Author: k.nowosad
Date: Mon Aug 27 11:26:21 2007
New Revision: 1195

Log:
simplified the code


Modified:
   jpeg2000/j2k.c
   jpeg2000/j2k.h
   jpeg2000/j2kdec.c
   jpeg2000/j2kenc.c

Modified: jpeg2000/j2k.c
==============================================================================
--- jpeg2000/j2k.c	(original)
+++ jpeg2000/j2k.c	Mon Aug 27 11:26:21 2007
@@ -183,14 +183,14 @@ void ff_j2k_set_significant(J2kT1Context
 
 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps)
 {
-    int reslevelno, bandno, gbandno = 0, ret;
+    int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
 
-    if (ret=ff_dwt_init(&comp->dwt,
-                (uint16_t[2][2]){{comp->x0, comp->x1}, {comp->y0, comp->y1}}, // will be changed soon
-                    codsty->nreslevels-1, codsty->transform))
+    if (ret=ff_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
         return ret;
+    for (i = 0; i < 2; i++)
+        csize *= comp->coord[i][1] - comp->coord[i][0];
 
-    comp->data = av_malloc((comp->y1 - comp->y0) * (comp->x1 -comp->x0) * sizeof(int));
+    comp->data = av_malloc(csize * sizeof(int));
     if (!comp->data)
         return AVERROR(ENOMEM);
     comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
@@ -198,28 +198,30 @@ int ff_j2k_init_component(J2kComponent *
     if (!comp->reslevel)
         return AVERROR(ENOMEM);
     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
-        int n = codsty->nreslevels - reslevelno;
+        int declvl = codsty->nreslevels - reslevelno;
         J2kResLevel *reslevel = comp->reslevel + reslevelno;
 
-        reslevel->x0 = ff_j2k_ceildivpow2(comp->x0, codsty->nreslevels - reslevelno - 1);
-        reslevel->x1 = ff_j2k_ceildivpow2(comp->x1, codsty->nreslevels - reslevelno - 1);
-        reslevel->y0 = ff_j2k_ceildivpow2(comp->y0, codsty->nreslevels - reslevelno - 1);
-        reslevel->y1 = ff_j2k_ceildivpow2(comp->y1, codsty->nreslevels - reslevelno - 1);
+        for (i = 0; i < 2; i++)
+            for (j = 0; j < 2; j++)
+                reslevel->coord[i][j] =
+                    ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
 
         if (reslevelno == 0)
             reslevel->nbands = 1;
         else
             reslevel->nbands = 3;
 
-        if (reslevel->x1 == reslevel->x0)
+        if (reslevel->coord[0][1] == reslevel->coord[0][0])
             reslevel->num_precincts_x = 0;
         else
-            reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->x1, codsty->log2_prec_width) - reslevel->x0 / (1<<codsty->log2_prec_width);
+            reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
+                                        - (reslevel->coord[0][0] >> codsty->log2_prec_width);
 
-        if (reslevel->y1 == reslevel->y0)
+        if (reslevel->coord[1][1] == reslevel->coord[1][0])
             reslevel->num_precincts_y = 0;
         else
-            reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->y1, codsty->log2_prec_height) - reslevel->y0 / (1<<codsty->log2_prec_height);
+            reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
+                                        - (reslevel->coord[1][0] >> codsty->log2_prec_height);
 
         reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
         if (!reslevel->band)
@@ -244,23 +246,20 @@ int ff_j2k_init_component(J2kComponent *
             if (reslevelno == 0){  // the same everywhere
                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
-
-                band->x0 = ff_j2k_ceildivpow2(comp->x0, n-1);
-                band->x1 = ff_j2k_ceildivpow2(comp->x1, n-1);
-                band->y0 = ff_j2k_ceildivpow2(comp->y0, n-1);
-                band->y1 = ff_j2k_ceildivpow2(comp->y1, n-1);
+                for (i = 0; i < 2; i++)
+                    for (j = 0; j < 2; j++)
+                        band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
             }
             else{
                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
 
-                band->x0 = ff_j2k_ceildivpow2(comp->x0 - (1 << (n-1)) * ((bandno+1)&1), n);
-                band->x1 = ff_j2k_ceildivpow2(comp->x1 - (1 << (n-1)) * ((bandno+1)&1), n);
-                band->y0 = ff_j2k_ceildivpow2(comp->y0 - (1 << (n-1)) * (((bandno+1)&2)>>1), n);
-                band->y1 = ff_j2k_ceildivpow2(comp->y1 - (1 << (n-1)) * (((bandno+1)&2)>>1), n);
+                for (i = 0; i < 2; i++)
+                    for (j = 0; j < 2; j++)
+                        band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
             }
-            band->cblknx = ff_j2k_ceildiv(band->x1, band->codeblock_width) - band->x0 / band->codeblock_width;
-            band->cblkny = ff_j2k_ceildiv(band->y1, band->codeblock_height) - band->y0 / band->codeblock_height;
+            band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width)  - band->coord[0][0] / band->codeblock_width;
+            band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
 
             band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
             if (!band->cblk)
@@ -278,10 +277,10 @@ int ff_j2k_init_component(J2kComponent *
                 cblk->npasses = 0;
             }
 
-            y0 = band->y0;
-            y1 = (band->y0 + (1<<codsty->log2_prec_height))/(1<<codsty->log2_prec_height)*(1<<codsty->log2_prec_height) - band->y0;
+            y0 = band->coord[1][0];
+            y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
             yi0 = 0;
-            yi1 = ff_j2k_ceildiv(y1 - y0, 1<<codsty->log2_cblk_height) * (1<<codsty->log2_cblk_height);
+            yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
             yi1 = FFMIN(yi1, band->cblkny);
             cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
             for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
@@ -293,10 +292,10 @@ int ff_j2k_init_component(J2kComponent *
                 yi0 = yi1 - cblkperprech;
                 yi1 = FFMIN(yi1, band->cblkny);
             }
-            x0 = band->x0;
-            x1 = (band->x0 + (1<<codsty->log2_prec_width))/(1<<codsty->log2_prec_width)*(1<<codsty->log2_prec_width) - band->x0;
+            x0 = band->coord[0][0];
+            x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
             xi0 = 0;
-            xi1 = ff_j2k_ceildiv(x1 - x0, 1<<codsty->log2_cblk_width) * (1<<codsty->log2_cblk_width);
+            xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
             xi1 = FFMIN(xi1, band->cblknx);
 
             cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);

Modified: jpeg2000/j2k.h
==============================================================================
--- jpeg2000/j2k.h	(original)
+++ jpeg2000/j2k.h	Mon Aug 27 11:26:21 2007
@@ -140,7 +140,7 @@ typedef struct {
 } J2kPrec; ///< precinct
 
 typedef struct {
-    uint16_t x0, x1, y0, y1;
+    uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
     uint16_t codeblock_width, codeblock_height;
     uint16_t cblknx, cblkny;
     uint32_t stepsize; ///< quantization stepsize (* 2^13)
@@ -149,8 +149,8 @@ typedef struct {
 } J2kBand; ///< subband
 
 typedef struct {
-    uint16_t x0, x1, y0, y1;
     uint8_t nbands;
+    uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
     uint16_t num_precincts_x, num_precincts_y; ///< number of precincts in x/y direction
     uint8_t log2_prec_width, log2_prec_height; ///< exponent of precinct size
     J2kBand *band;
@@ -160,7 +160,7 @@ typedef struct {
    J2kResLevel *reslevel;
    DWTContext dwt;
    int *data;
-   uint16_t x0, x1, y0, y1;
+   uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
 } J2kComponent;
 
 /* debug routines */

Modified: jpeg2000/j2kdec.c
==============================================================================
--- jpeg2000/j2kdec.c	(original)
+++ jpeg2000/j2kdec.c	Mon Aug 27 11:26:21 2007
@@ -438,10 +438,10 @@ static int init_tile(J2kDecoderContext *
         J2kQuantStyle  *qntsty = tile->qntsty + compno;
         int gbandno = 0, ret; // global bandno
 
-        comp->x0 = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
-        comp->x1 = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
-        comp->y0 = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
-        comp->y1 = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
+        comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
+        comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
+        comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
+        comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
 
         if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno]))
             return ret;
@@ -679,13 +679,16 @@ static int decode_cblk(J2kDecoderContext
 
 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
 {
-    int i, *src[3], i0, i1, i2;
+    int i, j, *src[3], i0, i1, i2, csize = 1;
 
     for (i = 0; i < 3; i++)
         src[i] = tile->comp[i].data;
 
+    for (i = 0; i < 2; i++)
+        csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
+
     if (tile->codsty[0].transform == FF_DWT97){
-        for (i = 0; i < (tile->comp[0].y1 - tile->comp[0].y0) * (tile->comp[0].x1 - tile->comp[0].x0); i++){
+        for (i = 0; i < csize; i++){
             i0 = *src[0] + (*src[2] * 46802 >> 16);
             i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
             i2 = *src[0] + (116130 * *src[1] >> 16);
@@ -694,7 +697,7 @@ static void mct_decode(J2kDecoderContext
             *src[2]++ = i2;
         }
     } else{
-        for (i = 0; i < (tile->comp[0].y1 - tile->comp[0].y0) * (tile->comp[0].x1 - tile->comp[0].x0); i++){
+        for (i = 0; i < csize; i++){
             i1 = *src[0] - (*src[2] + *src[1] >> 2);
             i0 = i1 + *src[2];
             i2 = i1 + *src[1];
@@ -724,20 +727,22 @@ static int decode_tile(J2kDecoderContext
 
                 bandpos = bandno + (reslevelno > 0);
 
-                yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].y1 - comp->reslevel[reslevelno-1].y0;
+                yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
                 y0 = yy0;
-                yy1 = FFMIN(ff_j2k_ceildiv(band->y0 + 1, band->codeblock_height) * band->codeblock_height, band->y1) - band->y0 + yy0;
+                yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
+                            band->coord[1][1]) - band->coord[1][0] + yy0;
 
-                if (band->x0 == band->x1 || band->y0 == band->y1)
+                if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
                     continue;
 
                 for (cblky = 0; cblky < band->cblkny; cblky++){
                     if (reslevelno == 0 || bandno == 1)
                         xx0 = 0;
                     else
-                        xx0 = comp->reslevel[reslevelno-1].x1 - comp->reslevel[reslevelno-1].x0;
+                        xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
                     x0 = xx0;
-                    xx1 = FFMIN(ff_j2k_ceildiv(band->x0 + 1, band->codeblock_width) * band->codeblock_width, band->x1) - band->x0 + xx0;
+                    xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
+                                band->coord[0][1]) - band->coord[0][0] + xx0;
 
                     for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
                         int y, x;
@@ -746,7 +751,7 @@ static int decode_tile(J2kDecoderContext
                             for (y = yy0; y < yy1; y++){
                                 int *ptr = t1.data[y-yy0];
                                 for (x = xx0; x < xx1; x++){
-                                    comp->data[(comp->x1 - comp->x0) * y + x] = *ptr++ >> 1;
+                                    comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
                                 }
                             }
                         } else{
@@ -755,15 +760,15 @@ static int decode_tile(J2kDecoderContext
                                 for (x = xx0; x < xx1; x++){
                                     int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
                                     tmp2 = FFABS(tmp>>1) + FFABS(tmp&1);
-                                    comp->data[(comp->x1 - comp->x0) * y + x] = tmp < 0 ? -tmp2 : tmp2;
+                                    comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
                                 }
                             }
                         }
                         xx0 = xx1;
-                        xx1 = FFMIN(xx1 + band->codeblock_width, band->x1 - band->x0 + x0);
+                        xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
                     }
                     yy0 = yy1;
-                    yy1 = FFMIN(yy1 + band->codeblock_height, band->y1 - band->y0 + y0);
+                    yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
                 }
             }
         }
@@ -773,19 +778,19 @@ static int decode_tile(J2kDecoderContext
     if (tile->codsty[0].mct)
         mct_decode(s, tile);
 
-    y = tile->comp[0].y0 - s->image_offset_y;
+    y = tile->comp[0].coord[1][0] - s->image_offset_y;
 
     line = s->picture.data[0] + y * s->picture.linesize[0];
     if (s->avctx->pix_fmt == PIX_FMT_BGRA) // RGBA -> BGRA
         FFSWAP(int *, src[0], src[2]);
 
-    for (; y < tile->comp[0].y1 - s->image_offset_y; y++){
+    for (; y < tile->comp[0].coord[1][1] - s->image_offset_y; y++){
         uint8_t *dst;
 
-        x = tile->comp[0].x0 - s->image_offset_x;
+        x = tile->comp[0].coord[0][0] - s->image_offset_x;
         dst = line + x * s->ncomponents;
 
-        for (; x < tile->comp[0].x1 - s->image_offset_x; x++)
+        for (; x < tile->comp[0].coord[0][1] - s->image_offset_x; x++)
             for (compno = 0; compno < s->ncomponents; compno++){
                 *src[compno] += 1 << (s->cbps[compno]-1);
                 if (*src[compno] < 0)

Modified: jpeg2000/j2kenc.c
==============================================================================
--- jpeg2000/j2kenc.c	(original)
+++ jpeg2000/j2kenc.c	Mon Aug 27 11:26:21 2007
@@ -345,10 +345,10 @@ static int init_tiles(J2kEncoderContext 
                 J2kComponent *comp = tile->comp + compno;
                 int ret;
 
-                comp->x0 = tilex * s->tile_width;
-                comp->x1 = FFMIN((tilex+1)*s->tile_width, s->width);
-                comp->y0 = tiley * s->tile_height;
-                comp->y1 = FFMIN((tiley+1)*s->tile_height, s->height);
+                comp->coord[0][0] = tilex * s->tile_width;
+                comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
+                comp->coord[1][0] = tiley * s->tile_height;
+                comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
 
                 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno]))
                     return ret;
@@ -356,12 +356,13 @@ static int init_tiles(J2kEncoderContext 
         }
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
         J2kTile *tile = s->tile + tileno;
-        uint8_t *line = s->picture->data[0] + tile->comp[0].y0 * s->picture->linesize[0] + tile->comp[0].x0 * s->ncomponents;
+        uint8_t *line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
+                        + tile->comp[0].coord[0][0] * s->ncomponents;
 
         i = 0;
-        for (y = tile->comp[0].y0; y < tile->comp[0].y1; y++){
+        for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
             uint8_t *ptr = line;
-            for (x = tile->comp[0].x0; x < tile->comp[0].x1; x++, i++){
+            for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
                 for (compno = 0; compno < s->ncomponents; compno++){
                     tile->comp[compno].data[i] = *ptr++  - (1 << 7);
                 }
@@ -596,8 +597,8 @@ static int encode_packet(J2kEncoderConte
 
     // is the packet empty?
     for (bandno = 0; bandno < rlevel->nbands; bandno++){
-        if (rlevel->band[bandno].x0 < rlevel->band[bandno].x1
-        &&  rlevel->band[bandno].y0 < rlevel->band[bandno].y1){
+        if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
+        &&  rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
             empty = 0;
             break;
         }
@@ -765,11 +766,12 @@ static int encode_tile(J2kEncoderContext
             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
                 J2kBand *band = reslevel->band + bandno;
                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
-                yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].y1 - comp->reslevel[reslevelno-1].y0;
+                yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
                 y0 = yy0;
-                yy1 = FFMIN(ff_j2k_ceildiv(band->y0 + 1, band->codeblock_height) * band->codeblock_height, band->y1) - band->y0 + yy0;
+                yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
+                            band->coord[1][1]) - band->coord[1][0] + yy0;
 
-                if (band->x0 == band->x1 || band->y0 == band->y1)
+                if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
                     continue;
 
                 bandpos = bandno + (reslevelno > 0);
@@ -778,24 +780,25 @@ static int encode_tile(J2kEncoderContext
                     if (reslevelno == 0 || bandno == 1)
                         xx0 = 0;
                     else
-                        xx0 = comp->reslevel[reslevelno-1].x1 - comp->reslevel[reslevelno-1].x0;
+                        xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
                     x0 = xx0;
-                    xx1 = FFMIN(ff_j2k_ceildiv(band->x0 + 1, band->codeblock_width) * band->codeblock_width, band->x1) - band->x0 + xx0;
+                    xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
+                                band->coord[0][1]) - band->coord[0][0] + xx0;
 
                     for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
                         int y, x;
                         for (y = yy0; y < yy1; y++){
                             int *ptr = t1.data[y-yy0];
                             for (x = xx0; x < xx1; x++)
-                                *ptr++ = comp->data[(comp->x1 - comp->x0) * y + x] << NMSEDEC_FRACBITS;
+                                *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
                         }
                         encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
                                     bandpos, codsty->nreslevels - reslevelno - 1);
                         xx0 = xx1;
-                        xx1 = FFMIN(xx1 + band->codeblock_width, band->x1 - band->x0 + x0);
+                        xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
                     }
                     yy0 = yy1;
-                    yy1 = FFMIN(yy1 + band->codeblock_height, band->y1 - band->y0 + y0);
+                    yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
                 }
             }
         }



More information about the FFmpeg-soc mailing list