Fix 2.2.0 regression when reading codestream with reperated calls to opj_get_decoded_...
[openjpeg.git] / src / lib / openjp2 / bench_dwt.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2017, IntoPix SA <contact@intopix.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 #ifdef _WIN32
35 #include <windows.h>
36 #else
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/times.h>
40 #endif /* _WIN32 */
41
42 OPJ_INT32 getValue(OPJ_UINT32 i)
43 {
44     return ((OPJ_INT32)i % 511) - 256;
45 }
46
47 void init_tilec(opj_tcd_tilecomp_t * l_tilec,
48                 OPJ_INT32 x0,
49                 OPJ_INT32 y0,
50                 OPJ_INT32 x1,
51                 OPJ_INT32 y1,
52                 OPJ_UINT32 numresolutions)
53 {
54     opj_tcd_resolution_t* l_res;
55     OPJ_UINT32 resno, l_level_no;
56     size_t i, nValues;
57
58     memset(l_tilec, 0, sizeof(*l_tilec));
59     l_tilec->x0 = x0;
60     l_tilec->y0 = y0;
61     l_tilec->x1 = x1;
62     l_tilec->y1 = y1;
63     nValues = (size_t)(l_tilec->x1 - l_tilec->x0) *
64               (size_t)(l_tilec->y1 - l_tilec->y0);
65     l_tilec->data = opj_malloc(sizeof(OPJ_INT32) * nValues);
66     for (i = 0; i < nValues; i++) {
67         l_tilec->data[i] = getValue((OPJ_UINT32)i);
68     }
69     l_tilec->numresolutions = numresolutions;
70     l_tilec->resolutions = opj_calloc(l_tilec->numresolutions,
71                                       sizeof(opj_tcd_resolution_t));
72
73     l_level_no = l_tilec->numresolutions;
74     l_res = l_tilec->resolutions;
75
76     /* Adapted from opj_tcd_init_tile() */
77     for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
78
79         --l_level_no;
80
81         /* border for each resolution level (global) */
82         l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
83         l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
84         l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
85         l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
86
87         ++l_res;
88     }
89 }
90
91 void free_tilec(opj_tcd_tilecomp_t * l_tilec)
92 {
93     opj_free(l_tilec->data);
94     opj_free(l_tilec->resolutions);
95 }
96
97 void usage(void)
98 {
99     printf(
100         "bench_dwt [-size value] [-check] [-display] [-num_resolutions val]\n");
101     printf(
102         "          [-offset x y] [-num_threads val]\n");
103     exit(1);
104 }
105
106
107 OPJ_FLOAT64 opj_clock(void)
108 {
109 #ifdef _WIN32
110     /* _WIN32: use QueryPerformance (very accurate) */
111     LARGE_INTEGER freq, t ;
112     /* freq is the clock speed of the CPU */
113     QueryPerformanceFrequency(&freq) ;
114     /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
115     /* t is the high resolution performance counter (see MSDN) */
116     QueryPerformanceCounter(& t) ;
117     return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64) freq.QuadPart) : 0 ;
118 #else
119     /* Unix or Linux: use resource usage */
120     struct rusage t;
121     OPJ_FLOAT64 procTime;
122     /* (1) Get the rusage data structure at this moment (man getrusage) */
123     getrusage(0, &t);
124     /* (2) What is the elapsed time ? - CPU time = User time + System time */
125     /* (2a) Get the seconds */
126     procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
127     /* (2b) More precisely! Get the microseconds part ! */
128     return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
129             1e-6) ;
130 #endif
131 }
132
133 int main(int argc, char** argv)
134 {
135     int num_threads = 0;
136     opj_tcd_t tcd;
137     opj_tcd_image_t tcd_image;
138     opj_tcd_tile_t tcd_tile;
139     opj_tcd_tilecomp_t tilec;
140     opj_image_t image;
141     opj_image_comp_t image_comp;
142     opj_thread_pool_t* tp;
143     OPJ_INT32 i, j, k;
144     OPJ_BOOL display = OPJ_FALSE;
145     OPJ_BOOL check = OPJ_FALSE;
146     OPJ_INT32 size = 16384 - 1;
147     OPJ_FLOAT64 start, stop;
148     OPJ_UINT32 offset_x = ((OPJ_UINT32)size + 1) / 2 - 1;
149     OPJ_UINT32 offset_y = ((OPJ_UINT32)size + 1) / 2 - 1;
150     OPJ_UINT32 num_resolutions = 6;
151
152     for (i = 1; i < argc; i++) {
153         if (strcmp(argv[i], "-display") == 0) {
154             display = OPJ_TRUE;
155             check = OPJ_TRUE;
156         } else if (strcmp(argv[i], "-check") == 0) {
157             check = OPJ_TRUE;
158         } else if (strcmp(argv[i], "-size") == 0 && i + 1 < argc) {
159             size = atoi(argv[i + 1]);
160             i ++;
161         } else if (strcmp(argv[i], "-num_threads") == 0 && i + 1 < argc) {
162             num_threads = atoi(argv[i + 1]);
163             i ++;
164         } else if (strcmp(argv[i], "-num_resolutions") == 0 && i + 1 < argc) {
165             num_resolutions = (OPJ_UINT32)atoi(argv[i + 1]);
166             if (num_resolutions == 0 || num_resolutions > 32) {
167                 fprintf(stderr,
168                         "Invalid value for num_resolutions. Should be >= 1 and <= 32\n");
169                 exit(1);
170             }
171             i ++;
172         } else if (strcmp(argv[i], "-offset") == 0 && i + 2 < argc) {
173             offset_x = (OPJ_UINT32)atoi(argv[i + 1]);
174             offset_y = (OPJ_UINT32)atoi(argv[i + 2]);
175             i += 2;
176         } else {
177             usage();
178         }
179     }
180
181     tp = opj_thread_pool_create(num_threads);
182
183     init_tilec(&tilec, (OPJ_INT32)offset_x, (OPJ_INT32)offset_y,
184                (OPJ_INT32)offset_x + size, (OPJ_INT32)offset_y + size,
185                num_resolutions);
186
187     if (display) {
188         printf("Before\n");
189         k = 0;
190         for (j = 0; j < tilec.y1 - tilec.y0; j++) {
191             for (i = 0; i < tilec.x1 - tilec.x0; i++) {
192                 printf("%d ", tilec.data[k]);
193                 k ++;
194             }
195             printf("\n");
196         }
197     }
198
199     memset(&tcd, 0, sizeof(tcd));
200     tcd.thread_pool = tp;
201     tcd.whole_tile_decoding = OPJ_TRUE;
202     tcd.win_x0 = (OPJ_UINT32)tilec.x0;
203     tcd.win_y0 = (OPJ_UINT32)tilec.y0;
204     tcd.win_x1 = (OPJ_UINT32)tilec.x1;
205     tcd.win_y1 = (OPJ_UINT32)tilec.y1;
206     tcd.tcd_image = &tcd_image;
207     memset(&tcd_image, 0, sizeof(tcd_image));
208     tcd_image.tiles = &tcd_tile;
209     memset(&tcd_tile, 0, sizeof(tcd_tile));
210     tcd_tile.x0 = tilec.x0;
211     tcd_tile.y0 = tilec.y0;
212     tcd_tile.x1 = tilec.x1;
213     tcd_tile.y1 = tilec.y1;
214     tcd_tile.numcomps = 1;
215     tcd_tile.comps = &tilec;
216     tcd.image = &image;
217     memset(&image, 0, sizeof(image));
218     image.numcomps = 1;
219     image.comps = &image_comp;
220     memset(&image_comp, 0, sizeof(image_comp));
221     image_comp.dx = 1;
222     image_comp.dy = 1;
223
224     start = opj_clock();
225     opj_dwt_decode(&tcd, &tilec, tilec.numresolutions);
226     stop = opj_clock();
227     printf("time for dwt_decode: %.03f s\n", stop - start);
228
229     if (display || check) {
230         if (display) {
231             printf("After IDWT\n");
232             k = 0;
233             for (j = 0; j < tilec.y1 - tilec.y0; j++) {
234                 for (i = 0; i < tilec.x1 - tilec.x0; i++) {
235                     printf("%d ", tilec.data[k]);
236                     k ++;
237                 }
238                 printf("\n");
239             }
240         }
241
242         opj_dwt_encode(&tilec);
243         if (display) {
244             printf("After FDWT\n");
245             k = 0;
246             for (j = 0; j < tilec.y1 - tilec.y0; j++) {
247                 for (i = 0; i < tilec.x1 - tilec.x0; i++) {
248                     printf("%d ", tilec.data[k]);
249                     k ++;
250                 }
251                 printf("\n");
252             }
253         }
254
255         if (check) {
256             size_t idx;
257             size_t nValues = (size_t)(tilec.x1 - tilec.x0) *
258                              (size_t)(tilec.y1 - tilec.y0);
259             for (idx = 0; i < (OPJ_INT32)nValues; i++) {
260                 if (tilec.data[idx] != getValue((OPJ_UINT32)idx)) {
261                     printf("Difference found at idx = %u\n", (OPJ_UINT32)idx);
262                     exit(1);
263                 }
264             }
265         }
266     }
267
268     free_tilec(&tilec);
269
270     opj_thread_pool_destroy(tp);
271     return 0;
272 }