2 * Discrete wavelet transform
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Discrete wavelet transform
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mem.h"
31 #include "jpeg2000dwt.h"
34 /* Defines for 9/7 DWT lifting parameters.
35 * Parameters are in float. */
36 #define F_LFTG_ALPHA 1.586134342059924f
37 #define F_LFTG_BETA 0.052980118572961f
38 #define F_LFTG_GAMMA 0.882911075530934f
39 #define F_LFTG_DELTA 0.443506852043971f
41 /* Lifting parameters in integer format.
42 * Computed as param = (float param) * (1 << 16) */
43 #define I_LFTG_ALPHA 103949ll
44 #define I_LFTG_BETA 3472ll
45 #define I_LFTG_GAMMA 57862ll
46 #define I_LFTG_DELTA 29066ll
47 #define I_LFTG_K 80621ll
48 #define I_LFTG_X 53274ll
51 static inline void extend53(int *p, int i0, int i1)
53 p[i0 - 1] = p[i0 + 1];
55 p[i0 - 2] = p[i0 + 2];
56 p[i1 + 1] = p[i1 - 3];
59 static inline void extend97_float(float *p, int i0, int i1)
63 for (i = 1; i <= 4; i++) {
64 p[i0 - i] = p[i0 + i];
65 p[i1 + i - 1] = p[i1 - i - 1];
69 static inline void extend97_int(int32_t *p, int i0, int i1)
73 for (i = 1; i <= 4; i++) {
74 p[i0 - i] = p[i0 + i];
75 p[i1 + i - 1] = p[i1 - i - 1];
79 static void sd_1d53(int *p, int i0, int i1)
91 for (i = ((i0+1)>>1) - 1; i < (i1+1)>>1; i++)
92 p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1;
93 for (i = ((i0+1)>>1); i < (i1+1)>>1; i++)
94 p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2;
97 static void dwt_encode53(DWTContext *s, int *t)
100 w = s->linelen[s->ndeclevels-1][0];
101 int *line = s->i_linebuf;
104 for (lev = s->ndeclevels-1; lev >= 0; lev--){
105 int lh = s->linelen[lev][0],
106 lv = s->linelen[lev][1],
114 for (lp = 0; lp < lh; lp++) {
117 for (i = 0; i < lv; i++)
120 sd_1d53(line, mv, mv + lv);
122 // copy back and deinterleave
123 for (i = mv; i < lv; i+=2, j++)
125 for (i = 1-mv; i < lv; i+=2, j++)
131 for (lp = 0; lp < lv; lp++){
134 for (i = 0; i < lh; i++)
137 sd_1d53(line, mh, mh + lh);
139 // copy back and deinterleave
140 for (i = mh; i < lh; i+=2, j++)
142 for (i = 1-mh; i < lh; i+=2, j++)
147 static void sd_1d97_float(float *p, int i0, int i1)
153 p[1] *= F_LFTG_X * 2;
159 extend97_float(p, i0, i1);
162 for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++)
163 p[2*i+1] -= 1.586134 * (p[2*i] + p[2*i+2]);
164 for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++)
165 p[2*i] -= 0.052980 * (p[2*i-1] + p[2*i+1]);
166 for (i = (i0>>1) - 1; i < (i1>>1); i++)
167 p[2*i+1] += 0.882911 * (p[2*i] + p[2*i+2]);
168 for (i = (i0>>1); i < (i1>>1); i++)
169 p[2*i] += 0.443506 * (p[2*i-1] + p[2*i+1]);
172 static void dwt_encode97_float(DWTContext *s, float *t)
175 w = s->linelen[s->ndeclevels-1][0];
176 float *line = s->f_linebuf;
179 for (lev = s->ndeclevels-1; lev >= 0; lev--){
180 int lh = s->linelen[lev][0],
181 lv = s->linelen[lev][1],
189 for (lp = 0; lp < lv; lp++){
192 for (i = 0; i < lh; i++)
195 sd_1d97_float(line, mh, mh + lh);
197 // copy back and deinterleave
198 for (i = mh; i < lh; i+=2, j++)
200 for (i = 1-mh; i < lh; i+=2, j++)
206 for (lp = 0; lp < lh; lp++) {
209 for (i = 0; i < lv; i++)
212 sd_1d97_float(line, mv, mv + lv);
214 // copy back and deinterleave
215 for (i = mv; i < lv; i+=2, j++)
217 for (i = 1-mv; i < lv; i+=2, j++)
223 static void sd_1d97_int(int *p, int i0, int i1)
229 p[1] = (p[1] * I_LFTG_X + (1<<14)) >> 15;
231 p[0] = (p[0] * I_LFTG_K + (1<<15)) >> 16;
235 extend97_int(p, i0, i1);
238 for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++)
239 p[2 * i + 1] -= (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
240 for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++)
241 p[2 * i] -= (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
242 for (i = (i0>>1) - 1; i < (i1>>1); i++)
243 p[2 * i + 1] += (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
244 for (i = (i0>>1); i < (i1>>1); i++)
245 p[2 * i] += (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
248 static void dwt_encode97_int(DWTContext *s, int *t)
251 int w = s->linelen[s->ndeclevels-1][0];
252 int h = s->linelen[s->ndeclevels-1][1];
254 int *line = s->i_linebuf;
257 for (i = 0; i < w * h; i++)
260 for (lev = s->ndeclevels-1; lev >= 0; lev--){
261 int lh = s->linelen[lev][0],
262 lv = s->linelen[lev][1],
270 for (lp = 0; lp < lh; lp++) {
273 for (i = 0; i < lv; i++)
276 sd_1d97_int(line, mv, mv + lv);
278 // copy back and deinterleave
279 for (i = mv; i < lv; i+=2, j++)
280 t[w*j + lp] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16;
281 for (i = 1-mv; i < lv; i+=2, j++)
287 for (lp = 0; lp < lv; lp++){
290 for (i = 0; i < lh; i++)
293 sd_1d97_int(line, mh, mh + lh);
295 // copy back and deinterleave
296 for (i = mh; i < lh; i+=2, j++)
297 t[w*lp + j] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16;
298 for (i = 1-mh; i < lh; i+=2, j++)
304 for (i = 0; i < w * h; i++)
305 t[i] = (t[i] + ((1<<I_PRESHIFT)>>1)) >> I_PRESHIFT;
308 static void sr_1d53(int *p, int i0, int i1)
320 for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
321 p[2 * i] -= (p[2 * i - 1] + p[2 * i + 1] + 2) >> 2;
322 for (i = (i0 >> 1); i < (i1 >> 1); i++)
323 p[2 * i + 1] += (p[2 * i] + p[2 * i + 2]) >> 1;
326 static void dwt_decode53(DWTContext *s, int *t)
329 int w = s->linelen[s->ndeclevels - 1][0];
330 int32_t *line = s->i_linebuf;
333 for (lev = 0; lev < s->ndeclevels; lev++) {
334 int lh = s->linelen[lev][0],
335 lv = s->linelen[lev][1],
343 for (lp = 0; lp < lv; lp++) {
345 // copy with interleaving
346 for (i = mh; i < lh; i += 2, j++)
347 l[i] = t[w * lp + j];
348 for (i = 1 - mh; i < lh; i += 2, j++)
349 l[i] = t[w * lp + j];
351 sr_1d53(line, mh, mh + lh);
353 for (i = 0; i < lh; i++)
354 t[w * lp + i] = l[i];
359 for (lp = 0; lp < lh; lp++) {
361 // copy with interleaving
362 for (i = mv; i < lv; i += 2, j++)
363 l[i] = t[w * j + lp];
364 for (i = 1 - mv; i < lv; i += 2, j++)
365 l[i] = t[w * j + lp];
367 sr_1d53(line, mv, mv + lv);
369 for (i = 0; i < lv; i++)
370 t[w * i + lp] = l[i];
375 static void sr_1d97_float(float *p, int i0, int i1)
387 extend97_float(p, i0, i1);
389 for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++)
390 p[2 * i] -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]);
392 for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++)
393 p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]);
395 for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
396 p[2 * i] += F_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]);
398 for (i = (i0 >> 1); i < (i1 >> 1); i++)
399 p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]);
402 static void dwt_decode97_float(DWTContext *s, float *t)
405 int w = s->linelen[s->ndeclevels - 1][0];
406 float *line = s->f_linebuf;
408 /* position at index O of line range [0-5,w+5] cf. extend function */
411 for (lev = 0; lev < s->ndeclevels; lev++) {
412 int lh = s->linelen[lev][0],
413 lv = s->linelen[lev][1],
420 for (lp = 0; lp < lv; lp++) {
422 // copy with interleaving
423 for (i = mh; i < lh; i += 2, j++)
424 l[i] = data[w * lp + j];
425 for (i = 1 - mh; i < lh; i += 2, j++)
426 l[i] = data[w * lp + j];
428 sr_1d97_float(line, mh, mh + lh);
430 for (i = 0; i < lh; i++)
431 data[w * lp + i] = l[i];
436 for (lp = 0; lp < lh; lp++) {
438 // copy with interleaving
439 for (i = mv; i < lv; i += 2, j++)
440 l[i] = data[w * j + lp];
441 for (i = 1 - mv; i < lv; i += 2, j++)
442 l[i] = data[w * j + lp];
444 sr_1d97_float(line, mv, mv + lv);
446 for (i = 0; i < lv; i++)
447 data[w * i + lp] = l[i];
452 static void sr_1d97_int(int32_t *p, int i0, int i1)
458 p[1] = (p[1] * I_LFTG_K + (1<<16)) >> 17;
460 p[0] = (p[0] * I_LFTG_X + (1<<15)) >> 16;
464 extend97_int(p, i0, i1);
466 for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++)
467 p[2 * i] -= (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
469 for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++)
470 p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
472 for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++)
473 p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
475 for (i = (i0 >> 1); i < (i1 >> 1); i++)
476 p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
479 static void dwt_decode97_int(DWTContext *s, int32_t *t)
482 int w = s->linelen[s->ndeclevels - 1][0];
483 int h = s->linelen[s->ndeclevels - 1][1];
485 int32_t *line = s->i_linebuf;
487 /* position at index O of line range [0-5,w+5] cf. extend function */
490 for (i = 0; i < w * h; i++)
491 data[i] <<= I_PRESHIFT;
493 for (lev = 0; lev < s->ndeclevels; lev++) {
494 int lh = s->linelen[lev][0],
495 lv = s->linelen[lev][1],
502 for (lp = 0; lp < lv; lp++) {
504 // rescale with interleaving
505 for (i = mh; i < lh; i += 2, j++)
506 l[i] = ((data[w * lp + j] * I_LFTG_K) + (1 << 15)) >> 16;
507 for (i = 1 - mh; i < lh; i += 2, j++)
508 l[i] = data[w * lp + j];
510 sr_1d97_int(line, mh, mh + lh);
512 for (i = 0; i < lh; i++)
513 data[w * lp + i] = l[i];
518 for (lp = 0; lp < lh; lp++) {
520 // rescale with interleaving
521 for (i = mv; i < lv; i += 2, j++)
522 l[i] = ((data[w * j + lp] * I_LFTG_K) + (1 << 15)) >> 16;
523 for (i = 1 - mv; i < lv; i += 2, j++)
524 l[i] = data[w * j + lp];
526 sr_1d97_int(line, mv, mv + lv);
528 for (i = 0; i < lv; i++)
529 data[w * i + lp] = l[i];
533 for (i = 0; i < w * h; i++)
534 data[i] = (data[i] + ((1<<I_PRESHIFT)>>1)) >> I_PRESHIFT;
537 int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2],
538 int decomp_levels, int type)
540 int i, j, lev = decomp_levels, maxlen,
543 s->ndeclevels = decomp_levels;
546 for (i = 0; i < 2; i++)
547 for (j = 0; j < 2; j++)
548 b[i][j] = border[i][j];
550 maxlen = FFMAX(b[0][1] - b[0][0],
553 for (i = 0; i < 2; i++) {
554 s->linelen[lev][i] = b[i][1] - b[i][0];
555 s->mod[lev][i] = b[i][0] & 1;
556 for (j = 0; j < 2; j++)
557 b[i][j] = (b[i][j] + 1) >> 1;
561 s->f_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->f_linebuf));
563 return AVERROR(ENOMEM);
566 s->i_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->i_linebuf));
568 return AVERROR(ENOMEM);
571 s->i_linebuf = av_malloc_array((maxlen + 6), sizeof(*s->i_linebuf));
573 return AVERROR(ENOMEM);
581 int ff_dwt_encode(DWTContext *s, void *t)
585 dwt_encode97_float(s, t); break;
587 dwt_encode97_int(s, t); break;
589 dwt_encode53(s, t); break;
596 int ff_dwt_decode(DWTContext *s, void *t)
600 dwt_decode97_float(s, t);
603 dwt_decode97_int(s, t);
614 void ff_dwt_destroy(DWTContext *s)
616 av_freep(&s->f_linebuf);
617 av_freep(&s->i_linebuf);
622 #include "libavutil/lfg.h"
626 static int test_dwt(int *array, int *ref, uint16_t border[2][2], int decomp_levels, int type, int max_diff) {
628 DWTContext s1={{{0}}}, *s= &s1;
631 ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, type);
633 fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
636 ret = ff_dwt_encode(s, array);
638 fprintf(stderr, "ff_dwt_encode failed\n");
641 ret = ff_dwt_decode(s, array);
643 fprintf(stderr, "ff_dwt_encode failed\n");
646 for (j = 0; j<MAX_W * MAX_W; j++) {
647 if (FFABS(array[j] - ref[j]) > max_diff) {
648 fprintf(stderr, "missmatch at %d (%d != %d) decomp:%d border %d %d %d %d\n",
649 j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
652 err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
657 printf("%s, decomp:%2d border %3d %3d %3d %3d milli-err2:%9"PRId64"\n",
658 type == FF_DWT53 ? "5/3i" : "9/7i",
659 decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
660 1000*err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
665 static int test_dwtf(float *array, float *ref, uint16_t border[2][2], int decomp_levels, float max_diff) {
667 DWTContext s1={{{0}}}, *s= &s1;
670 ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, FF_DWT97);
672 fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
675 ret = ff_dwt_encode(s, array);
677 fprintf(stderr, "ff_dwt_encode failed\n");
680 ret = ff_dwt_decode(s, array);
682 fprintf(stderr, "ff_dwt_encode failed\n");
685 for (j = 0; j<MAX_W * MAX_W; j++) {
686 if (FFABS(array[j] - ref[j]) > max_diff) {
687 fprintf(stderr, "missmatch at %d (%f != %f) decomp:%d border %d %d %d %d\n",
688 j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
691 err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
696 printf("9/7f, decomp:%2d border %3d %3d %3d %3d err2:%20.3f\n",
697 decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
698 err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
703 static int array[MAX_W * MAX_W];
704 static int ref [MAX_W * MAX_W];
705 static float arrayf[MAX_W * MAX_W];
706 static float reff [MAX_W * MAX_W];
711 uint16_t border[2][2];
712 int ret, decomp_levels;
714 av_lfg_init(&prng, 1);
716 for (i = 0; i<MAX_W * MAX_W; i++)
717 arrayf[i] = reff[i] = array[i] = ref[i] = av_lfg_get(&prng) % 2048;
719 for (i = 0; i < 100; i++) {
721 border[j>>1][j&1] = av_lfg_get(&prng) % MAX_W;
722 if (border[0][0] >= border[0][1] || border[1][0] >= border[1][1])
724 decomp_levels = av_lfg_get(&prng) % FF_DWT_MAX_DECLVLS;
726 ret = test_dwt(array, ref, border, decomp_levels, FF_DWT53, 0);
729 ret = test_dwt(array, ref, border, decomp_levels, FF_DWT97_INT, FFMIN(7+5*decomp_levels, 15+3*decomp_levels));
732 ret = test_dwtf(arrayf, reff, border, decomp_levels, 0.05);