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
23 #include "libavcodec/jpeg2000dwt.c"
25 #include "libavutil/lfg.h"
29 static int test_dwt(int *array, int *ref, int border[2][2], int decomp_levels, int type, int max_diff) {
31 DWTContext s1={{{0}}}, *s= &s1;
34 ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, type);
36 fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
39 ret = ff_dwt_encode(s, array);
41 fprintf(stderr, "ff_dwt_encode failed\n");
44 ret = ff_dwt_decode(s, array);
46 fprintf(stderr, "ff_dwt_encode failed\n");
49 for (j = 0; j<MAX_W * MAX_W; j++) {
50 if (FFABS(array[j] - ref[j]) > max_diff) {
51 fprintf(stderr, "missmatch at %d (%d != %d) decomp:%d border %d %d %d %d\n",
52 j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
55 err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
60 printf("%s, decomp:%2d border %3d %3d %3d %3d milli-err2:%9"PRId64"\n",
61 type == FF_DWT53 ? "5/3i" : "9/7i",
62 decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
63 1000*err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
68 static int test_dwtf(float *array, float *ref, int border[2][2], int decomp_levels, float max_diff) {
70 DWTContext s1={{{0}}}, *s= &s1;
73 ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, FF_DWT97);
75 fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
78 ret = ff_dwt_encode(s, array);
80 fprintf(stderr, "ff_dwt_encode failed\n");
83 ret = ff_dwt_decode(s, array);
85 fprintf(stderr, "ff_dwt_encode failed\n");
88 for (j = 0; j<MAX_W * MAX_W; j++) {
89 if (FFABS(array[j] - ref[j]) > max_diff) {
90 fprintf(stderr, "missmatch at %d (%f != %f) decomp:%d border %d %d %d %d\n",
91 j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
94 err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
99 printf("9/7f, decomp:%2d border %3d %3d %3d %3d err2:%20.3f\n",
100 decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
101 err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
106 static int array[MAX_W * MAX_W];
107 static int ref [MAX_W * MAX_W];
108 static float arrayf[MAX_W * MAX_W];
109 static float reff [MAX_W * MAX_W];
115 int ret, decomp_levels;
117 av_lfg_init(&prng, 1);
119 for (i = 0; i<MAX_W * MAX_W; i++)
120 arrayf[i] = reff[i] = array[i] = ref[i] = av_lfg_get(&prng) % 2048;
122 for (i = 0; i < 100; i++) {
124 border[j>>1][j&1] = av_lfg_get(&prng) % MAX_W;
125 if (border[0][0] >= border[0][1] || border[1][0] >= border[1][1])
127 decomp_levels = av_lfg_get(&prng) % FF_DWT_MAX_DECLVLS;
129 ret = test_dwt(array, ref, border, decomp_levels, FF_DWT53, 0);
132 ret = test_dwt(array, ref, border, decomp_levels, FF_DWT97_INT, FFMIN(7+5*decomp_levels, 15+3*decomp_levels));
135 ret = test_dwtf(arrayf, reff, border, decomp_levels, 0.05);