#define F_LFTG_BETA 0.052980118572961f
#define F_LFTG_GAMMA 0.882911075530934f
#define F_LFTG_DELTA 0.443506852043971f
-#define F_LFTG_K 1.230174104914001f
-#define F_LFTG_X 1.625732422f
-/* FIXME: Why use 1.625732422 instead of 1/F_LFTG_K?
- * Incorrect value in JPEG2000 norm.
- * see (ISO/IEC 15444:1 (version 2002) F.3.8.2 */
/* Lifting parameters in integer format.
* Computed as param = (float param) * (1 << 16) */
-#define I_LFTG_ALPHA 103949
-#define I_LFTG_BETA 3472
-#define I_LFTG_GAMMA 57862
-#define I_LFTG_DELTA 29066
-#define I_LFTG_K 80621
-#define I_LFTG_X 106544
+#define I_LFTG_ALPHA 103949ll
+#define I_LFTG_BETA 3472ll
+#define I_LFTG_GAMMA 57862ll
+#define I_LFTG_DELTA 29066ll
+#define I_LFTG_K 80621ll
+#define I_LFTG_X 53274ll
+#define I_PRESHIFT 8
static inline void extend53(int *p, int i0, int i1)
{
{
int i;
- if (i1 == i0 + 1)
+ if (i1 <= i0 + 1) {
+ if (i0 == 1)
+ p[1] <<= 1;
return;
+ }
extend53(p, i0, i1);
- for (i = (i0+1)/2 - 1; i < (i1+1)/2; i++)
+ for (i = ((i0+1)>>1) - 1; i < (i1+1)>>1; i++)
p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1;
- for (i = (i0+1)/2; i < (i1+1)/2; i++)
+ for (i = ((i0+1)>>1); i < (i1+1)>>1; i++)
p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2;
}
lp;
int *l;
- av_assert1(!mh && !mv);
- // 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++) {
for (i = 1-mv; i < lv; i+=2, j++)
t[w*j + lp] = l[i];
}
+
+ // 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];
+ }
}
}
static void sd_1d97_float(float *p, int i0, int i1)
if (i1 <= i0 + 1) {
if (i0 == 1)
- p[1] *= F_LFTG_X;
+ p[1] *= F_LFTG_X * 2;
else
p[0] *= F_LFTG_K;
return;
extend97_float(p, i0, i1);
i0++; i1++;
- for (i = i0/2 - 2; i < i1/2 + 1; i++)
+ for (i = (i0>>1) - 2; i < (i1>>1) + 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++)
+ for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++)
p[2*i] -= 0.052980 * (p[2*i-1] + p[2*i+1]);
- for (i = i0/2 - 1; i < i1/2; i++)
+ for (i = (i0>>1) - 1; i < (i1>>1); i++)
p[2*i+1] += 0.882911 * (p[2*i] + p[2*i+2]);
- for (i = i0/2; i < i1/2; i++)
+ for (i = (i0>>1); i < (i1>>1); i++)
p[2*i] += 0.443506 * (p[2*i-1] + p[2*i+1]);
}
lp;
float *l;
- av_assert1(!mh && !mv);
// HOR_SD
l = line + mh;
for (lp = 0; lp < lv; lp++){
// copy back and deinterleave
for (i = mh; i < lh; i+=2, j++)
- t[w*lp + j] = F_LFTG_X * l[i] / 2;
+ t[w*lp + j] = l[i];
for (i = 1-mh; i < lh; i+=2, j++)
- t[w*lp + j] = F_LFTG_K * l[i] / 2;
+ t[w*lp + j] = l[i];
}
// VER_SD
// copy back and deinterleave
for (i = mv; i < lv; i+=2, j++)
- t[w*j + lp] = F_LFTG_X * l[i] / 2;
+ t[w*j + lp] = l[i];
for (i = 1-mv; i < lv; i+=2, j++)
- t[w*j + lp] = F_LFTG_K * l[i] / 2;
+ t[w*j + lp] = l[i];
}
}
}
if (i1 <= i0 + 1) {
if (i0 == 1)
- p[1] = (p[1] * I_LFTG_X + (1<<15)) >> 16;
+ p[1] = (p[1] * I_LFTG_X + (1<<14)) >> 15;
else
p[0] = (p[0] * I_LFTG_K + (1<<15)) >> 16;
return;
extend97_int(p, i0, i1);
i0++; i1++;
- for (i = i0/2 - 2; i < i1/2 + 1; i++)
+ for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++)
p[2 * i + 1] -= (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
- for (i = i0/2 - 1; i < i1/2 + 1; i++)
+ for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++)
p[2 * i] -= (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
- for (i = i0/2 - 1; i < i1/2; i++)
+ for (i = (i0>>1) - 1; i < (i1>>1); i++)
p[2 * i + 1] += (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
- for (i = i0/2; i < i1/2; i++)
+ for (i = (i0>>1); i < (i1>>1); i++)
p[2 * i] += (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
}
static void dwt_encode97_int(DWTContext *s, int *t)
{
- int lev,
- w = s->linelen[s->ndeclevels-1][0];
+ int lev;
+ int w = s->linelen[s->ndeclevels-1][0];
+ int h = s->linelen[s->ndeclevels-1][1];
+ int i;
int *line = s->i_linebuf;
line += 5;
+ for (i = 0; i < w * h; i++)
+ t[i] <<= I_PRESHIFT;
+
for (lev = s->ndeclevels-1; lev >= 0; lev--){
int lh = s->linelen[lev][0],
lv = s->linelen[lev][1],
lp;
int *l;
- av_assert1(!mh && !mv);
+ // 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_int(line, mv, mv + lv);
+
+ // copy back and deinterleave
+ for (i = mv; i < lv; i+=2, j++)
+ t[w*j + lp] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16;
+ for (i = 1-mv; i < lv; i+=2, j++)
+ t[w*j + lp] = l[i];
+ }
// HOR_SD
l = line + mh;
// copy back and deinterleave
for (i = mh; i < lh; i+=2, j++)
- t[w*lp + j] = ((l[i] * I_LFTG_X) + (1 << 16)) >> 17;
+ t[w*lp + j] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16;
for (i = 1-mh; i < lh; i+=2, j++)
- t[w*lp + j] = ((l[i] * I_LFTG_K) + (1 << 16)) >> 17;
+ 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_1d97_int(line, mv, mv + lv);
-
- // copy back and deinterleave
- for (i = mv; i < lv; i+=2, j++)
- t[w*j + lp] = ((l[i] * I_LFTG_X) + (1 << 16)) >> 17;
- for (i = 1-mv; i < lv; i+=2, j++)
- t[w*j + lp] = ((l[i] * I_LFTG_K) + (1 << 16)) >> 17;
- }
}
+
+ for (i = 0; i < w * h; i++)
+ t[i] = (t[i] + ((1<<I_PRESHIFT)>>1)) >> I_PRESHIFT;
}
static void sr_1d53(int *p, int i0, int i1)
extend53(p, i0, i1);
- for (i = i0 / 2; i < i1 / 2 + 1; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
p[2 * i] -= (p[2 * i - 1] + p[2 * i + 1] + 2) >> 2;
- for (i = i0 / 2; i < i1 / 2; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1); i++)
p[2 * i + 1] += (p[2 * i] + p[2 * i + 2]) >> 1;
}
if (i0 == 1)
p[1] *= F_LFTG_K/2;
else
- p[0] *= F_LFTG_X/2;
+ p[0] *= F_LFTG_X;
return;
}
extend97_float(p, i0, i1);
- for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+ for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++)
p[2 * i] -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]);
/* step 4 */
- for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
+ for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++)
p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]);
/*step 5*/
- for (i = i0 / 2; i < i1 / 2 + 1; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
p[2 * i] += F_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]);
/* step 6 */
- for (i = i0 / 2; i < i1 / 2; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1); i++)
p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]);
}
int i, j = 0;
// copy with interleaving
for (i = mh; i < lh; i += 2, j++)
- l[i] = data[w * lp + j] * F_LFTG_K;
+ l[i] = data[w * lp + j];
for (i = 1 - mh; i < lh; i += 2, j++)
- l[i] = data[w * lp + j] * F_LFTG_X;
+ l[i] = data[w * lp + j];
sr_1d97_float(line, mh, mh + lh);
int i, j = 0;
// copy with interleaving
for (i = mv; i < lv; i += 2, j++)
- l[i] = data[w * j + lp] * F_LFTG_K;
+ l[i] = data[w * j + lp];
for (i = 1 - mv; i < lv; i += 2, j++)
- l[i] = data[w * j + lp] * F_LFTG_X;
+ l[i] = data[w * j + lp];
sr_1d97_float(line, mv, mv + lv);
if (i0 == 1)
p[1] = (p[1] * I_LFTG_K + (1<<16)) >> 17;
else
- p[0] = (p[0] * I_LFTG_X + (1<<16)) >> 17;
+ p[0] = (p[0] * I_LFTG_X + (1<<15)) >> 16;
return;
}
extend97_int(p, i0, i1);
- for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+ for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++)
p[2 * i] -= (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
/* step 4 */
- for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
+ for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++)
p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
/*step 5*/
- for (i = i0 / 2; i < i1 / 2 + 1; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
/* step 6 */
- for (i = i0 / 2; i < i1 / 2; i++)
+ for (i = (i0 >> 1); i < (i1 >> 1); i++)
p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
}
{
int lev;
int w = s->linelen[s->ndeclevels - 1][0];
+ int h = s->linelen[s->ndeclevels - 1][1];
+ int i;
int32_t *line = s->i_linebuf;
int32_t *data = t;
/* position at index O of line range [0-5,w+5] cf. extend function */
line += 5;
+ for (i = 0; i < w * h; i++)
+ data[i] <<= I_PRESHIFT;
+
for (lev = 0; lev < s->ndeclevels; lev++) {
int lh = s->linelen[lev][0],
lv = s->linelen[lev][1],
for (i = mh; i < lh; i += 2, j++)
l[i] = ((data[w * lp + j] * I_LFTG_K) + (1 << 15)) >> 16;
for (i = 1 - mh; i < lh; i += 2, j++)
- l[i] = ((data[w * lp + j] * I_LFTG_X) + (1 << 15)) >> 16;
+ l[i] = data[w * lp + j];
sr_1d97_int(line, mh, mh + lh);
for (i = mv; i < lv; i += 2, j++)
l[i] = ((data[w * j + lp] * I_LFTG_K) + (1 << 15)) >> 16;
for (i = 1 - mv; i < lv; i += 2, j++)
- l[i] = ((data[w * j + lp] * I_LFTG_X) + (1 << 15)) >> 16;
+ l[i] = data[w * j + lp];
sr_1d97_int(line, mv, mv + lv);
data[w * i + lp] = l[i];
}
}
+
+ for (i = 0; i < w * h; i++)
+ data[i] = (data[i] + ((1<<I_PRESHIFT)>>1)) >> I_PRESHIFT;
}
int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2],
av_freep(&s->f_linebuf);
av_freep(&s->i_linebuf);
}
+
+#ifdef TEST
+
+#include "libavutil/lfg.h"
+
+#define MAX_W 256
+
+static int test_dwt(int *array, int *ref, uint16_t border[2][2], int decomp_levels, int type, int max_diff) {
+ int ret, j;
+ DWTContext s1={{{0}}}, *s= &s1;
+ int64_t err2 = 0;
+
+ ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, type);
+ if (ret < 0) {
+ fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
+ return 1;
+ }
+ ret = ff_dwt_encode(s, array);
+ if (ret < 0) {
+ fprintf(stderr, "ff_dwt_encode failed\n");
+ return 1;
+ }
+ ret = ff_dwt_decode(s, array);
+ if (ret < 0) {
+ fprintf(stderr, "ff_dwt_encode failed\n");
+ return 1;
+ }
+ for (j = 0; j<MAX_W * MAX_W; j++) {
+ if (FFABS(array[j] - ref[j]) > max_diff) {
+ fprintf(stderr, "missmatch at %d (%d != %d) decomp:%d border %d %d %d %d\n",
+ j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
+ return 2;
+ }
+ err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
+ array[j] = ref[j];
+ }
+ ff_dwt_destroy(s);
+
+ printf("%s, decomp:%2d border %3d %3d %3d %3d milli-err2:%9"PRId64"\n",
+ type == FF_DWT53 ? "5/3i" : "9/7i",
+ decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
+ 1000*err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
+
+ return 0;
+}
+
+static int test_dwtf(float *array, float *ref, uint16_t border[2][2], int decomp_levels, float max_diff) {
+ int ret, j;
+ DWTContext s1={{{0}}}, *s= &s1;
+ double err2 = 0;
+
+ ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, FF_DWT97);
+ if (ret < 0) {
+ fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
+ return 1;
+ }
+ ret = ff_dwt_encode(s, array);
+ if (ret < 0) {
+ fprintf(stderr, "ff_dwt_encode failed\n");
+ return 1;
+ }
+ ret = ff_dwt_decode(s, array);
+ if (ret < 0) {
+ fprintf(stderr, "ff_dwt_encode failed\n");
+ return 1;
+ }
+ for (j = 0; j<MAX_W * MAX_W; j++) {
+ if (FFABS(array[j] - ref[j]) > max_diff) {
+ fprintf(stderr, "missmatch at %d (%f != %f) decomp:%d border %d %d %d %d\n",
+ j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
+ return 2;
+ }
+ err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
+ array[j] = ref[j];
+ }
+ ff_dwt_destroy(s);
+
+ printf("9/7f, decomp:%2d border %3d %3d %3d %3d err2:%20.3f\n",
+ decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
+ err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
+
+ return 0;
+}
+
+static int array[MAX_W * MAX_W];
+static int ref [MAX_W * MAX_W];
+static float arrayf[MAX_W * MAX_W];
+static float reff [MAX_W * MAX_W];
+
+int main(void) {
+ AVLFG prng;
+ int i,j;
+ uint16_t border[2][2];
+ int ret, decomp_levels;
+
+ av_lfg_init(&prng, 1);
+
+ for (i = 0; i<MAX_W * MAX_W; i++)
+ arrayf[i] = reff[i] = array[i] = ref[i] = av_lfg_get(&prng) % 2048;
+
+ for (i = 0; i < 100; i++) {
+ for (j=0; j<4; j++)
+ border[j>>1][j&1] = av_lfg_get(&prng) % MAX_W;
+ if (border[0][0] >= border[0][1] || border[1][0] >= border[1][1])
+ continue;
+ decomp_levels = av_lfg_get(&prng) % FF_DWT_MAX_DECLVLS;
+
+ ret = test_dwt(array, ref, border, decomp_levels, FF_DWT53, 0);
+ if (ret)
+ return ret;
+ ret = test_dwt(array, ref, border, decomp_levels, FF_DWT97_INT, FFMIN(7+5*decomp_levels, 15+3*decomp_levels));
+ if (ret)
+ return ret;
+ ret = test_dwtf(arrayf, reff, border, decomp_levels, 0.05);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+#endif