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

k.nowosad subversion at mplayerhq.hu
Mon Aug 27 13:37:01 CEST 2007


Author: k.nowosad
Date: Mon Aug 27 13:37:01 2007
New Revision: 1198

Log:
moved some initialization from encode_frame to j2kenc_init


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

Modified: jpeg2000/j2k.c
==============================================================================
--- jpeg2000/j2k.c	(original)
+++ jpeg2000/j2k.c	Mon Aug 27 13:37:01 2007
@@ -51,17 +51,24 @@ void ff_j2k_printu(uint8_t *tab, int l)
 /* tag tree routines */
 
 /** allocate the memory for tag tree */
+
+static int tag_tree_size(int w, int h)
+{
+    int res = 0;
+    while (w > 1 || h > 1){
+        res += w * h;
+        w = (w+1) >> 1;
+        h = (h+1) >> 1;
+    }
+    return res + 1;
+}
+
 J2kTgtNode *ff_j2k_tag_tree_init(int w, int h)
 {
-    int size = 1, pw = w, ph = h;
+    int pw = w, ph = h;
     J2kTgtNode *res, *t, *t2;
 
-    while (pw > 1 || ph > 1){
-        size += pw*ph;
-        pw = (pw+1) >> 1;
-        ph = (ph+1) >> 1;
-    }
-    t = res = av_mallocz(size*sizeof(J2kTgtNode));
+    t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
 
     if (res == NULL)
         return NULL;
@@ -85,6 +92,16 @@ J2kTgtNode *ff_j2k_tag_tree_init(int w, 
     return res;
 }
 
+static void tag_tree_zero(J2kTgtNode *t, int w, int h)
+{
+    int i, siz = tag_tree_size(w, h);
+
+    for (i = 0; i < siz; i++){
+        t[i].val = 0;
+        t[i].vis = 0;
+    }
+}
+
 uint8_t ff_j2k_nbctxno_lut[256][4];
 
 static int getnbctxno(int flag, int bandno)
@@ -320,3 +337,24 @@ int ff_j2k_init_component(J2kComponent *
     }
     return 0;
 }
+
+void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
+{
+    int reslevelno, bandno, cblkno, precno;
+    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
+        J2kResLevel *rlevel = comp->reslevel + reslevelno;
+        for (bandno = 0; bandno < rlevel->nbands; bandno++){
+            J2kBand *band = rlevel->band + bandno;
+            for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
+                J2kPrec *prec = band->prec + precno;
+                tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
+                tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
+            }
+            for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
+                J2kCblk *cblk = band->cblk + cblkno;
+                cblk->length = 0;
+                cblk->lblock = 3;
+            }
+        }
+    }
+}

Modified: jpeg2000/j2k.h
==============================================================================
--- jpeg2000/j2k.h	(original)
+++ jpeg2000/j2k.h	Mon Aug 27 13:37:01 2007
@@ -212,5 +212,6 @@ static inline int ff_j2k_getsgnctxno(int
 }
 
 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps);
+void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty);
 
 #endif /* J2K_H */

Modified: jpeg2000/j2kenc.c
==============================================================================
--- jpeg2000/j2kenc.c	(original)
+++ jpeg2000/j2kenc.c	Mon Aug 27 13:37:01 2007
@@ -324,7 +324,7 @@ static uint8_t *put_sot(J2kEncoderContex
  */
 static int init_tiles(J2kEncoderContext *s)
 {
-    int y, x, tileno, tilex, tiley, compno, reslevelno, bandno, i;
+    int tileno, tilex, tiley, compno;
     J2kCodingStyle *codsty = &s->codsty;
     J2kQuantStyle  *qntsty = &s->qntsty;
 
@@ -354,6 +354,12 @@ static int init_tiles(J2kEncoderContext 
                     return ret;
             }
         }
+    return 0;
+}
+
+static void copy_frame(J2kEncoderContext *s)
+{
+    int tileno, compno, i, y, x;
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
         J2kTile *tile = s->tile + tileno;
         uint8_t *line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
@@ -370,7 +376,14 @@ static int init_tiles(J2kEncoderContext 
             line += s->picture->linesize[0];
         }
     }
-    // calculate band bps and exponents
+}
+
+static void init_quantization(J2kEncoderContext *s)
+{
+    int compno, reslevelno, bandno;
+    J2kQuantStyle  *qntsty = &s->qntsty;
+    J2kCodingStyle *codsty = &s->codsty;
+
     for (compno = 0; compno < s->ncomponents; compno++){
         int gbandno = 0;
         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
@@ -384,7 +397,6 @@ static int init_tiles(J2kEncoderContext 
             }
         }
     }
-    return 0;
 }
 
 static void init_luts()
@@ -801,7 +813,6 @@ static int encode_tile(J2kEncoderContext
                 }
             }
         }
-        av_free(comp->data);
         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
     }
 
@@ -839,24 +850,80 @@ void cleanup(J2kEncoderContext *s)
             }
             ff_dwt_destroy(&comp->dwt);
             av_free(comp->reslevel);
+            av_free(comp->data);
         }
         av_free(s->tile[tileno].comp);
     }
     av_free(s->tile);
 }
 
+static void reinit(J2kEncoderContext *s)
+{
+    int tileno, compno;
+    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
+        J2kTile *tile = s->tile + tileno;
+        for (compno = 0; compno < s->ncomponents; compno++)
+            ff_j2k_reinit(tile->comp + compno, &s->codsty);
+    }
+}
+
 static int encode_frame(AVCodecContext *avctx,
                         uint8_t *buf, int buf_size,
                         void *data)
 {
-    int tileno, i, ret;
+    int tileno, ret;
+    J2kEncoderContext *s = avctx->priv_data;
+
+    // init:
+    s->buf = s->buf_start = buf;
+    s->buf_end = buf + buf_size;
+
+    s->picture = data;
+
+    s->lambda = s->picture->quality * LAMBDA_SCALE;
+
+    init_quantization(s);
+    copy_frame(s);
+    reinit(s);
+
+    if (s->buf_end - s->buf < 2)
+        return -1;
+    bytestream_put_be16(&s->buf, J2K_SOC);
+    if (ret = put_siz(s))
+        return ret;
+    if (ret = put_cod(s))
+        return ret;
+    if (ret = put_qcd(s, 0))
+        return ret;
+
+    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
+        uint8_t *psotptr;
+        if ((psotptr = put_sot(s, tileno)) < 0)
+            return psotptr;
+        if (s->buf_end - s->buf < 2)
+            return -1;
+        bytestream_put_be16(&s->buf, J2K_SOD);
+        if (ret = encode_tile(s, s->tile + tileno, tileno))
+            return ret;
+        bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
+    }
+    if (s->buf_end - s->buf < 2)
+        return -1;
+    bytestream_put_be16(&s->buf, J2K_EOC);
+
+    av_log(s->avctx, AV_LOG_DEBUG, "end\n");
+    return s->buf - s->buf_start;
+}
+
+static int j2kenc_init(AVCodecContext *avctx)
+{
+    int i, ret;
     J2kEncoderContext *s = avctx->priv_data;
     J2kCodingStyle *codsty = &s->codsty;
     J2kQuantStyle  *qntsty = &s->qntsty;
 
     s->avctx = avctx;
-    av_log(s->avctx, AV_LOG_DEBUG, "start\n");
-    s->picture = data;
+    av_log(s->avctx, AV_LOG_DEBUG, "init\n");
 
     // defaults:
     // TODO: implement setting non-standard precinct size
@@ -873,16 +940,9 @@ static int encode_frame(AVCodecContext *
     s->tile_width            = 256;
     s->tile_height           = 256;
 
-    // init:
-    s->buf = s->buf_start = buf;
-    s->buf_end = buf + buf_size;
     s->width = avctx->width;
     s->height = avctx->height;
 
-    s->lambda = s->picture->quality * LAMBDA_SCALE;
-
-    ff_j2k_init_tier1_luts();
-
     // TODO: other pixel formats
     for (i = 0; i < 3; i++)
         s->cbps[i] = 8;
@@ -897,40 +957,24 @@ static int encode_frame(AVCodecContext *
         return -1;
     }
 
-    av_log(s->avctx, AV_LOG_DEBUG, "init\n");
+    ff_j2k_init_tier1_luts();
+
+    init_luts();
+
     if (ret=init_tiles(s))
         return ret;
-    init_luts();
+
     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
 
-    if (s->buf_end - s->buf < 2)
-        return -1;
-    bytestream_put_be16(&s->buf, J2K_SOC);
-    if (ret = put_siz(s))
-        return ret;
-    if (ret = put_cod(s))
-        return ret;
-    if (ret = put_qcd(s, 0))
-        return ret;
+    return 0;
+}
 
-    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
-        uint8_t *psotptr;
-        if ((psotptr = put_sot(s, tileno)) < 0)
-            return psotptr;
-        if (s->buf_end - s->buf < 2)
-            return -1;
-        bytestream_put_be16(&s->buf, J2K_SOD);
-        if (ret = encode_tile(s, s->tile + tileno, tileno))
-            return ret;
-        bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
-    }
-    if (s->buf_end - s->buf < 2)
-        return -1;
-    bytestream_put_be16(&s->buf, J2K_EOC);
+static int j2kenc_destroy(AVCodecContext *avctx)
+{
+    J2kEncoderContext *s = avctx->priv_data;
 
     cleanup(s);
-    av_log(s->avctx, AV_LOG_DEBUG, "end\n");
-    return s->buf - s->buf_start;
+    return 0;
 }
 
 AVCodec jpeg2000_encoder = {
@@ -938,9 +982,9 @@ AVCodec jpeg2000_encoder = {
     CODEC_TYPE_VIDEO,
     CODEC_ID_JPEG2000,
     sizeof(J2kEncoderContext),
-    NULL,
+    j2kenc_init,
     encode_frame,
-    NULL,
+    j2kenc_destroy,
     NULL,
     0,
     .pix_fmts =



More information about the FFmpeg-soc mailing list