[FFmpeg-soc] [soc]: r1171 - in jpeg2000: checkout.sh dwt.c dwt.h ffmpeg.patch j2k.h j2kdec.c j2kenc.c

k.nowosad subversion at mplayerhq.hu
Fri Aug 24 01:50:42 CEST 2007


Author: k.nowosad
Date: Fri Aug 24 01:50:42 2007
New Revision: 1171

Log:
put dwt with minor changes into a separate file


Added:
   jpeg2000/dwt.c
   jpeg2000/dwt.h
Modified:
   jpeg2000/checkout.sh
   jpeg2000/ffmpeg.patch
   jpeg2000/j2k.h
   jpeg2000/j2kdec.c
   jpeg2000/j2kenc.c

Modified: jpeg2000/checkout.sh
==============================================================================
--- jpeg2000/checkout.sh	(original)
+++ jpeg2000/checkout.sh	Fri Aug 24 01:50:42 2007
@@ -14,4 +14,6 @@ ln -s ../../mqcenc.c mqcenc.c
 ln -s ../../mqc.h mqc.h
 ln -s ../../mqc.c mqc.c
 ln -s ../../mqcdec.c mqcdec.c
+ln -s ../../dwt.h dwt.h
+ln -s ../../dwt.c dwt.c
 echo "Done, now just do a regular configure and make to build."

Added: jpeg2000/dwt.c
==============================================================================
--- (empty file)
+++ jpeg2000/dwt.c	Fri Aug 24 01:50:42 2007
@@ -0,0 +1,379 @@
+/*
+ * Discrete wavelet transform
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * Discrete wavelet transform
+ * @file dwt.c
+ * @author Kamil Nowosad
+ */
+
+#include "dwt.h"
+
+const static float scale97[] = {1.625786, 1.230174};
+
+static inline void extend53(int *p, int i0, int i1)
+{
+    p[i0 - 1] = p[i0 + 1];
+    p[i1    ] = p[i1 - 2];
+    p[i0 - 2] = p[i0 + 2];
+    p[i1 + 1] = p[i1 - 3];
+}
+
+static inline void extend97(float *p, int i0, int i1)
+{
+    int i;
+
+    for (i = 1; i <= 4; i++){
+        p[i0 - i] = p[i0 + i];
+        p[i1 + i - 1] = p[i1 - i - 1];
+    }
+}
+
+static void sd_1d53(int *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend53(p, i0, i1);
+
+    for (i = (i0+1)/2 - 1; i < (i1+1)/2; i++)
+        p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1;
+    for (i = (i0+1)/2; i < (i1+1)/2; i++)
+        p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2;
+}
+
+static void dwt_encode53(DWTContext *s, int *t)
+{
+    int lev,
+        w = s->linelen[s->ndeclevels-1][0];
+    int *line = s->linebuf;
+    line += 3;
+
+    for (lev = s->ndeclevels-1; lev >= 0; lev--){
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        int *l;
+
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++){
+            int i, j = 0;
+
+            for (i = 0; i < lh; i++)
+                l[i] = t[w*lp + i];
+
+            sd_1d53(line, mh, mh + lh);
+
+            // copy back and deinterleave
+            for (i =   mh; i < lh; i+=2, j++)
+                t[w*lp + j] = l[i];
+            for (i = 1-mh; i < lh; i+=2, j++)
+                t[w*lp + j] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++) {
+            int i, j = 0;
+
+            for (i = 0; i < lv; i++)
+                l[i] = t[w*i + lp];
+
+            sd_1d53(line, mv, mv + lv);
+
+            // copy back and deinterleave
+            for (i =   mv; i < lv; i+=2, j++)
+                t[w*j + lp] = l[i];
+            for (i = 1-mv; i < lv; i+=2, j++)
+                t[w*j + lp] = l[i];
+        }
+    }
+}
+
+static void sd_1d97(float *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend97(p, i0, i1);
+    i0++; i1++;
+
+    for (i = i0/2 - 2; i < i1/2 + 1; i++)
+        p[2*i+1] -= 1.586134 * (p[2*i] + p[2*i+2]);
+    for (i = i0/2 - 1; i < i1/2 + 1; i++)
+        p[2*i] -= 0.052980 * (p[2*i-1] + p[2*i+1]);
+    for (i = i0/2 - 1; i < i1/2; i++)
+        p[2*i+1] += 0.882911 * (p[2*i] + p[2*i+2]);
+    for (i = i0/2; i < i1/2; i++)
+        p[2*i] += 0.443506 * (p[2*i-1] + p[2*i+1]);
+}
+
+static void dwt_encode97(DWTContext *s, int *t)
+{
+    int lev,
+        w = s->linelen[s->ndeclevels-1][0];
+    float *line = s->linebuf;
+    line += 5;
+
+    for (lev = s->ndeclevels-1; lev >= 0; lev--){
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        float *l;
+
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++){
+            int i, j = 0;
+
+            for (i = 0; i < lh; i++)
+                l[i] = t[w*lp + i];
+
+            sd_1d97(line, mh, mh + lh);
+
+            // copy back and deinterleave
+            for (i =   mh; i < lh; i+=2, j++)
+                t[w*lp + j] = scale97[mh] * l[i];
+            for (i = 1-mh; i < lh; i+=2, j++)
+                t[w*lp + j] = scale97[mh] * l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++) {
+            int i, j = 0;
+
+            for (i = 0; i < lv; i++)
+                l[i] = t[w*i + lp];
+
+            sd_1d97(line, mv, mv + lv);
+
+            // copy back and deinterleave
+            for (i =   mv; i < lv; i+=2, j++)
+                t[w*j + lp] = scale97[mv] * l[i];
+            for (i = 1-mv; i < lv; i+=2, j++)
+                t[w*j + lp] = scale97[mv] * l[i];
+        }
+    }
+}
+
+static void sr_1d53(int *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend53(p, i0, i1);
+
+    for (i = i0/2; i < i1/2 + 1; i++)
+        p[2*i] -= (p[2*i-1] + p[2*i+1] + 2) >> 2;
+    for (i = i0/2; i < i1/2; i++)
+        p[2*i+1] += (p[2*i] + p[2*i+2]) >> 1;
+}
+
+static void dwt_decode53(DWTContext *s, int *t)
+{
+    int lev,
+        w = s->linelen[s->ndeclevels-1][0];
+    int *line = s->linebuf;
+    line += 3;
+
+    for (lev = 0; lev < s->ndeclevels; lev++){
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        int *l;
+
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++){
+            int i, j = 0;
+            // copy with interleaving
+            for (i =   mh; i < lh; i+=2, j++)
+                l[i] = t[w*lp + j];
+            for (i = 1-mh; i < lh; i+=2, j++)
+                l[i] = t[w*lp + j];
+
+            sr_1d53(line, mh, mh + lh);
+
+            for (i = 0; i < lh; i++)
+                t[w*lp + i] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++){
+            int i, j = 0;
+            // copy with interleaving
+            for (i =   mv; i < lv; i+=2, j++)
+                l[i] = t[w*j + lp];
+            for (i = 1-mv; i < lv; i+=2, j++)
+                l[i] = t[w*j + lp];
+
+            sr_1d53(line, mv, mv + lv);
+
+            for (i = 0; i < lv; i++)
+                t[w*i + lp] = l[i];
+        }
+    }
+}
+
+static void sr_1d97(float *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend97(p, i0, i1);
+
+    for (i = i0/2 - 1; i < i1/2 + 2; i++)
+        p[2*i] -= 0.443506 * (p[2*i-1] + p[2*i+1]);
+    for (i = i0/2 - 1; i < i1/2 + 1; i++)
+        p[2*i+1] -= 0.882911 * (p[2*i] + p[2*i+2]);
+    for (i = i0/2; i < i1/2 + 1; i++)
+        p[2*i] += 0.052980 * (p[2*i-1] + p[2*i+1]);
+    for (i = i0/2; i < i1/2; i++)
+        p[2*i+1] += 1.586134 * (p[2*i] + p[2*i+2]);
+}
+
+static void dwt_decode97(DWTContext *s, int *t)
+{
+    int lev,
+        w = s->linelen[s->ndeclevels-1][0];
+    float *line = s->linebuf;
+    line += 5;
+
+    for (lev = 0; lev < s->ndeclevels; lev++){
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        float *l;
+
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++){
+            int i, j = 0;
+            // copy with interleaving
+            for (i =   mh; i < lh; i+=2, j++)
+                l[i] = scale97[1-mh] * t[w*lp + j];
+            for (i = 1-mh; i < lh; i+=2, j++)
+                l[i] = scale97[1-mh] * t[w*lp + j];
+
+            sr_1d97(line, mh, mh + lh);
+
+            for (i = 0; i < lh; i++)
+                t[w*lp + i] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++){
+            int i, j = 0;
+            // copy with interleaving
+            for (i =   mv; i < lv; i+=2, j++)
+                l[i] = scale97[1-mv] * t[w*j + lp];
+            for (i = 1-mv; i < lv; i+=2, j++)
+                l[i] = scale97[1-mv] * t[w*j + lp];
+
+            sr_1d97(line, mv, mv + lv);
+
+            for (i = 0; i < lv; i++)
+                t[w*i + lp] = l[i];
+        }
+    }
+}
+
+int ff_dwt_init(DWTContext *s, uint16_t border[2][2], int decomp_levels, int type)
+{
+    int i, j, lev = decomp_levels, maxlen;
+
+    s->ndeclevels = decomp_levels;
+    s->type = type;
+
+    maxlen = FFMAX(border[0][1] - border[0][0],
+                   border[1][1] - border[1][0]);
+
+    while(--lev >= 0){
+        for (i = 0; i < 2; i++){
+            s->linelen[lev][i] = border[i][1] - border[i][0];
+            s->mod[lev][i] = border[i][0] & 1;
+            for (j = 0; j < 2; j++)
+                border[i][j] = (border[i][j] + 1) >> 1;
+        }
+    }
+    if (type == FF_DWT97)
+        s->linebuf = av_malloc((maxlen + 12) * sizeof(float));
+    else if (type == FF_DWT53)
+        s->linebuf = av_malloc((maxlen + 6) * sizeof(int));
+    else
+        return -1;
+
+    if (!s->linebuf)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+int ff_dwt_encode(DWTContext *s, int *t)
+{
+    switch(s->type){
+        case FF_DWT97:
+            dwt_encode97(s, t); break;
+        case FF_DWT53:
+            dwt_encode53(s, t); break;
+        default:
+            return -1;
+    }
+    return 0;
+}
+
+int ff_dwt_decode(DWTContext *s, int *t)
+{
+    switch(s->type){
+        case FF_DWT97:
+            dwt_decode97(s, t); break;
+        case FF_DWT53:
+            dwt_decode53(s, t); break;
+        default:
+            return -1;
+    }
+    return 0;
+}
+
+void ff_dwt_destroy(DWTContext *s)
+{
+    av_free(s->linebuf);
+}

Added: jpeg2000/dwt.h
==============================================================================
--- (empty file)
+++ jpeg2000/dwt.h	Fri Aug 24 01:50:42 2007
@@ -0,0 +1,63 @@
+/*
+ * Discrete wavelet transform
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * Discrete wavelet transform
+ * @file dwt.h
+ * @author Kamil Nowosad
+ */
+
+#ifndef DWT_H
+#define DWT_H
+
+#include "avcodec.h"
+
+#define FF_DWT_MAX_DECLVLS 32 ///< max number of decomposition levels
+
+enum DWTType{
+    FF_DWT97,
+    FF_DWT53
+};
+
+typedef struct {
+    ///line lengths {horizontal, vertical} in consecutive decomposition levels
+    uint16_t linelen[FF_DWT_MAX_DECLVLS][2];
+    uint8_t  mod[FF_DWT_MAX_DECLVLS][2]; ///< coordinates (x0, y0) of decomp. levels mod 2
+    uint8_t  ndeclevels;                 ///< number of decomposition levels
+    uint8_t  type;                       ///< 0 for 9/7; 1 for 5/3
+    void     *linebuf;                   ///< buffer used by transform (int or float)
+} DWTContext;
+
+/**
+ * initialize DWT
+ * @param s DWT context
+ * @param border coordinates of transformed region {{x0, x1}, {y0, y1}}
+ * @param decomp_levels number of decomposition levels
+ * @param type 0 for DWT 9/7; 1 for DWT 5/3
+ */
+int ff_dwt_init(DWTContext *s, uint16_t border[2][2], int decomp_levels, int type);
+
+int ff_dwt_encode(DWTContext *s, int *t);
+int ff_dwt_decode(DWTContext *s, int *t);
+
+void ff_dwt_destroy(DWTContext *s);
+
+#endif

Modified: jpeg2000/ffmpeg.patch
==============================================================================
--- jpeg2000/ffmpeg.patch	(original)
+++ jpeg2000/ffmpeg.patch	Fri Aug 24 01:50:42 2007
@@ -6,8 +6,8 @@ Index: Makefile
  OBJS-$(CONFIG_INDEO3_DECODER)          += indeo3.o
  OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
  OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
-+OBJS-$(CONFIG_JPEG2000_DECODER)        += j2kdec.o mqcdec.o mqc.o j2k.o
-+OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o mqcenc.o mqc.o j2k.o
++OBJS-$(CONFIG_JPEG2000_DECODER)        += j2kdec.o mqcdec.o mqc.o j2k.o dwt.o
++OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o mqcenc.o mqc.o j2k.o dwt.o
  OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o mjpegdec.o mjpeg.o golomb.o
  OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o golomb.o
  OBJS-$(CONFIG_KMVC_DECODER)            += kmvc.o

Modified: jpeg2000/j2k.h
==============================================================================
--- jpeg2000/j2k.h	(original)
+++ jpeg2000/j2k.h	Fri Aug 24 01:50:42 2007
@@ -53,11 +53,6 @@ enum J2kMarkers{
     J2K_EOC = 0xffd9, ///< end of codestream
 };
 
-enum J2kTransform{ ///< type of wavelet transform
-    J2K_DWT97,
-    J2K_DWT53
-};
-
 enum J2kQuantsty{ ///< quantization style
     J2K_QSTY_NONE, ///< no quantization
     J2K_QSTY_SI,   ///< scalar derived

Modified: jpeg2000/j2kdec.c
==============================================================================
--- jpeg2000/j2kdec.c	(original)
+++ jpeg2000/j2kdec.c	Fri Aug 24 01:50:42 2007
@@ -30,6 +30,7 @@
 #include "bytestream.h"
 #include "j2k.h"
 #include "common.h"
+#include "dwt.h"
 
 #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
 
@@ -54,6 +55,7 @@ typedef struct {
 
 typedef struct {
    J2kResLevel *reslevel;
+   DWTContext dwt;
    int *data;
    uint16_t x0, x1, y0, y1;
 } J2kComponent;
@@ -425,13 +427,18 @@ static int init_tile(J2kDecoderContext *
         J2kComponent *comp = tile->comp + compno;
         J2kCodingStyle *codsty = tile->codsty + compno;
         J2kQuantStyle  *qntsty = tile->qntsty + compno;
-        int gbandno = 0; // global bandno
+        int gbandno = 0, ret; // global bandno
 
         comp->x0 = FFMAX(p * s->tile_width + s->tile_offset_x, s->image_offset_x);
         comp->x1 = FFMIN((p+1)*s->tile_width + s->tile_offset_x, s->width);
         comp->y0 = FFMAX(q * s->tile_height + s->tile_offset_y, s->image_offset_y);
         comp->y1 = FFMIN((q+1)*s->tile_height + s->tile_offset_y, s->height);
 
+        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))
+            return ret;
+
         comp->data = av_malloc((comp->y1 - comp->y0) * (comp->x1 -comp->x0) * sizeof(int));
         if (!comp->data)
             return AVERROR(ENOMEM);
@@ -779,169 +786,6 @@ static int decode_cblk(J2kDecoderContext
     return 0;
 }
 
-/* inverse discrete wavelet transform routines */
-static void sr_1d53(int *p, int i0, int i1)
-{
-    int i;
-
-    if (i1 == i0 + 1)
-        return;
-
-    p[i0 - 1] = p[i0 + 1];
-    p[i1    ] = p[i1 - 2];
-    p[i0 - 2] = p[i0 + 2];
-    p[i1 + 1] = p[i1 - 3];
-
-    for (i = i0/2; i < i1/2 + 1; i++)
-        p[2*i] -= (p[2*i-1] + p[2*i+1] + 2) >> 2;
-    for (i = i0/2; i < i1/2; i++)
-        p[2*i+1] += (p[2*i] + p[2*i+2]) >> 1;
-}
-
-static void sr_1d97(float *p, int i0, int i1)
-{
-    int i;
-
-    if (i1 == i0 + 1)
-        return;
-
-    for (i = 1; i <= 4; i++){
-        p[i0 - i] = p[i0 + i];
-        p[i1 + i - 1] = p[i1 - i - 1];
-    }
-
-    for (i = i0/2 - 1; i < i1/2 + 2; i++)
-        p[2*i] *= 1.230174;
-    for (i = i0/2 - 2; i < i1/2 + 2; i++)
-        p[2*i+1] *= 1.625786;
-    for (i = i0/2 - 1; i < i1/2 + 2; i++)
-        p[2*i] -= 0.443506 * (p[2*i-1] + p[2*i+1]);
-    for (i = i0/2 - 1; i < i1/2 + 1; i++)
-        p[2*i+1] -= 0.882911 * (p[2*i] + p[2*i+2]);
-    for (i = i0/2; i < i1/2 + 1; i++)
-        p[2*i] += 0.052980 * (p[2*i-1] + p[2*i+1]);
-    for (i = i0/2; i < i1/2; i++)
-        p[2*i+1] += 1.586134 * (p[2*i] + p[2*i+2]);
-}
-
-static int dwt_decode53(J2kDecoderContext *s, J2kComponent *comp, int nreslevels)
-{
-    int lev = nreslevels, i,
-        *t = comp->data, w = comp->x1 - comp->x0;
-    int *ppv = av_malloc((comp->reslevel[lev-1].y1 + 4)*sizeof(int)), *pv = ppv+2;
-    int *ppu = av_malloc((comp->reslevel[lev-1].x1 + 4)*sizeof(int)), *pu = ppu+2;
-
-    if (!ppv || !ppu)
-        return -1;
-
-    for (i = 1; i < lev; i++){
-        int u0 = comp->reslevel[i].x0,
-            u1 = comp->reslevel[i].x1,
-            v0 = comp->reslevel[i].y0,
-            v1 = comp->reslevel[i].y1,
-            u = u0, v = v0;
-
-        // HOR_SD
-        while (v < v1){
-            int i, j;
-            // copy with interleaving
-            for (i = u0 + (u0 & 1), j = 0; i < u1; i+=2, j++){
-                pu[i] = t[w*(v-v0) + j];
-            }
-            for (i = u0 + 1 - (u0 % 2); i < u1; i+=2, j++){
-                pu[i] = t[w*(v-v0) + j];
-            }
-
-            sr_1d53(pu, u0, u1);
-
-            for (i = u0; i < u1; i++)
-                t[w*(v-v0) + i-u0] = pu[i];
-
-            v++;
-        }
-        // VER_SD
-        while (u < u1){
-            int i, j;
-            // copy with interleaving
-            for (i = v0 + (v0 & 1), j = 0; i < v1; i+=2, j++){
-                pv[i] = t[w*j + u-u0];
-            }
-            for (i = v0 + 1 - (v0 % 2); i < v1; i+=2, j++){
-                pv[i] = t[w*j + u-u0];
-            }
-
-            sr_1d53(pv, v0, v1);
-
-            for (i = v0; i < v1; i++)
-                t[w*(i-v0) + u-u0] = pv[i];
-
-            u++;
-        }
-    }
-    av_free(ppv);
-    av_free(ppu);
-    return 0;
-}
-
-static int dwt_decode97(J2kDecoderContext *s, J2kComponent *comp, int nreslevels)
-{
-    int lev = nreslevels, i,
-        *t = comp->data, w = comp->x1 - comp->x0;
-    float *ppv = av_malloc((comp->reslevel[lev-1].y1 + 8)*sizeof(float)), *pv = ppv+4;
-    float *ppu = av_malloc((comp->reslevel[lev-1].x1 + 8)*sizeof(float)), *pu = ppu+4;
-
-    if (!ppv || !ppu)
-        return -1;
-
-    for (i = 1; i < lev; i++){
-        int u0 = comp->reslevel[i].x0,
-            u1 = comp->reslevel[i].x1,
-            v0 = comp->reslevel[i].y0,
-            v1 = comp->reslevel[i].y1,
-            u = u0, v = v0;
-
-        // HOR_SD
-        while (v < v1){
-            int i, j;
-            // copy with interleaving
-            for (i = u0 + (u0 & 1), j = 0; i < u1; i+=2, j++){
-                pu[i] = t[w*(v-v0) + j];
-            }
-            for (i = u0 + 1 - (u0 % 2); i < u1; i+=2, j++){
-                pu[i] = t[w*(v-v0) + j];
-            }
-
-            sr_1d97(pu, u0, u1);
-
-            for (i = u0; i < u1; i++)
-                t[w*(v-v0) + i-u0] = pu[i];
-
-            v++;
-        }
-        // VER_SD
-        while (u < u1){
-            int i, j;
-            // copy with interleaving
-            for (i = v0 + (v0 & 1), j = 0; i < v1; i+=2, j++){
-                pv[i] = t[w*j + u-u0];
-            }
-            for (i = v0 + 1 - (v0 % 2); i < v1; i+=2, j++){
-                pv[i] = t[w*j + u-u0];
-            }
-
-            sr_1d97(pv, v0, v1);
-
-            for (i = v0; i < v1; i++)
-                t[w*(i-v0) + u-u0] = pv[i];
-
-            u++;
-        }
-    }
-    av_free(ppv);
-    av_free(ppu);
-    return 0;
-}
-
 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
 {
     int i, *src[3], i0, i1, i2;
@@ -949,7 +793,7 @@ static void mct_decode(J2kDecoderContext
     for (i = 0; i < 3; i++)
         src[i] = tile->comp[i].data;
 
-    if (tile->codsty[0].transform == J2K_DWT97){
+    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++){
             i0 = *src[0] + (*src[2] * 46802 >> 16);
             i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
@@ -1007,7 +851,7 @@ static int decode_tile(J2kDecoderContext
                     for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
                         int y, x;
                         decode_cblk(s, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
-                        if (codsty->transform == J2K_DWT53){
+                        if (codsty->transform == FF_DWT53){
                             for (y = yy0; y < yy1; y++){
                                 int *ptr = t1.data[y-yy0];
                                 for (x = xx0; x < xx1; x++){
@@ -1032,13 +876,7 @@ static int decode_tile(J2kDecoderContext
                 }
             }
         }
-        if (codsty->transform == J2K_DWT53){
-            if (dwt_decode53(s, comp, codsty->nreslevels))
-                return -1;
-        } else{
-            if (dwt_decode97(s, comp, codsty->nreslevels))
-                return -1;
-        }
+        ff_dwt_decode(&comp->dwt, comp->data);
         src[compno] = comp->data;
     }
     if (tile->codsty[0].mct)
@@ -1096,6 +934,7 @@ static void cleanup(J2kDecoderContext *s
                 av_free(reslevel->band);
             }
             av_free(comp->reslevel);
+            ff_dwt_destroy(&comp->dwt);
         }
         av_free(s->tile[tileno].comp);
     }

Modified: jpeg2000/j2kenc.c
==============================================================================
--- jpeg2000/j2kenc.c	(original)
+++ jpeg2000/j2kenc.c	Fri Aug 24 01:50:42 2007
@@ -30,6 +30,7 @@
 #include "bytestream.h"
 #include "j2k.h"
 #include "common.h"
+#include "dwt.h"
 
 #define NMSEDEC_BITS 7
 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
@@ -43,6 +44,7 @@ static int lut_nmsedec_ref [1<<NMSEDEC_B
 
 typedef struct {
    J2kResLevel *reslevel;
+   DWTContext dwt;
    int *data;
    uint16_t x0, x1, y0, y1;
 } J2kComponent;
@@ -333,11 +335,18 @@ static int init_tiles(J2kEncoderContext 
                 return AVERROR(ENOMEM);
             for (compno = 0; compno < s->ncomponents; compno++){
                 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);
+
+                if (ret=ff_dwt_init(&comp->dwt,
+                                    (uint16_t[2][2]){{comp->x0, comp->x1}, {comp->y0, comp->y1}}, // will be changed soon
+                                    s->nreslevels-1, 1))
+                    return ret;
+
                 comp->data = av_malloc((comp->y1 - comp->y0) * (comp->x1 -comp->x0) * sizeof(int));
                 if (!comp->data)
                     return AVERROR(ENOMEM);
@@ -503,79 +512,6 @@ static void init_luts()
     }
 }
 
-/* discrete wavelet transform routines */
-static void sd_1d(int *p, int i0, int i1)
-{
-    int i;
-
-    if (i1 == i0 + 1)
-        return;
-
-    p[i0 - 1] = p[i0 + 1];
-    p[i1    ] = p[i1 - 2];
-    p[i0 - 2] = p[i0 + 2];
-    p[i1 + 1] = p[i1 - 3];
-
-    for (i = (i0+1)/2 - 1; i < (i1+1)/2; i++){
-        p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1;
-    }
-    for (i = (i0+1)/2; i < (i1+1)/2; i++){
-        p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2;
-    }
-}
-
-static void dwt_encode53(J2kEncoderContext *s, J2kComponent *comp)
-{
-    int lev = s->nreslevels,
-        *t = comp->data, w = comp->x1 - comp->x0;
-    int *ppv = av_malloc((comp->reslevel[lev-1].y1 + 4)*sizeof(int)), *pv = ppv+2;
-    int *ppu = av_malloc((comp->reslevel[lev-1].x1 + 4)*sizeof(int)), *pu = ppu+2;
-
-    while (--lev){
-        int u0 = comp->reslevel[lev].x0,
-            u1 = comp->reslevel[lev].x1,
-            v0 = comp->reslevel[lev].y0,
-            v1 = comp->reslevel[lev].y1,
-            u = u0, v = v0;
-
-        //VER_SD
-        while (u < u1){
-            int i, j;
-            for (i = v0; i < v1; i++)
-                pv[i] = t[w*(i-v0) + u-u0];
-            sd_1d(pv, v0, v1);
-
-            // copy back and deinterleave
-            for (i = v0+v0%2, j = 0; i < v1; i+=2, j++){
-                t[w*j + u-u0] = pv[i];
-            }
-            for (i = v0+1-v0%2; i < v1; i+=2, j++){
-                t[w*j + u-u0] = pv[i];
-            }
-            u++;
-        }
-
-        //HOR_SD
-        while (v < v1){
-            int i, j;
-            for (i = u0; i < u1; i++)
-                pu[i] = t[w*(v-v0) + i-u0];
-            sd_1d(pu, u0, u1);
-
-            // copy back and deinterleave
-            for (i = u0+u0%2, j = 0; i < u1; i+=2, j++){
-                t[w*(v-v0) + j] = pu[i];
-            }
-            for (i = u0+1-u0%2; i < u1; i+=2, j++){
-                t[w*(v-v0) + j] = pu[i];
-            }
-            v++;
-        }
-    }
-    av_free(ppv);
-    av_free(ppu);
-}
-
 /* tier-1 routines */
 static int getnmsedec_sig(int x, int bpno)
 {
@@ -909,7 +845,7 @@ static void encode_tile(J2kEncoderContex
         J2kComponent *comp = s->tile[tileno].comp + compno;
 
         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
-        dwt_encode53(s, &s->tile[tileno].comp[compno]);
+        ff_dwt_encode(&comp->dwt, comp->data);
         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
 
         for (reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
@@ -984,6 +920,7 @@ void cleanup(J2kEncoderContext *s)
                     }
                 av_free(reslevel->band);
             }
+            ff_dwt_destroy(&comp->dwt);
             av_free(comp->reslevel);
         }
         av_free(s->tile[tileno].comp);



More information about the FFmpeg-soc mailing list