• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff --git a/third_party/libopenjpeg20/dwt.c b/third_party/libopenjpeg20/dwt.c
2index 83c148002..1455ee84a 100644
3--- a/third_party/libopenjpeg20/dwt.c
4+++ b/third_party/libopenjpeg20/dwt.c
5@@ -62,8 +62,6 @@
6
7 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
8 /*@{*/
9-#define OPJ_WS(i) v->mem[(i)*2]
10-#define OPJ_WD(i) v->mem[(1+(i)*2)]
11
12 #ifdef __AVX2__
13 /** Number of int32 values in a AVX2 register */
14@@ -81,6 +79,7 @@
15
16 typedef struct dwt_local {
17     OPJ_INT32* mem;
18+    OPJ_SIZE_T mem_count;
19     OPJ_INT32 dn;   /* number of elements in high pass band */
20     OPJ_INT32 sn;   /* number of elements in low pass band */
21     OPJ_INT32 cas;  /* 0 = start on even coord, 1 = start on odd coord */
22@@ -132,13 +131,14 @@ static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
23 /**
24 Forward 5-3 wavelet transform in 1-D
25 */
26-static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
27-                             OPJ_INT32 cas);
28+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_SIZE_T a_count, OPJ_INT32 dn,
29+    OPJ_INT32 sn, OPJ_INT32 cas);
30 /**
31 Forward 9-7 wavelet transform in 1-D
32 */
33-static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
34-                                  OPJ_INT32 cas);
35+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_SIZE_T a_count,
36+    OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
37+
38
39 /**
40 Explicit calculation of the Quantization Stepsizes
41@@ -149,14 +149,14 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
42 Inverse wavelet transform in 2-D.
43 */
44 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
45-                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
46+                                    const opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
47
48 static OPJ_BOOL opj_dwt_decode_partial_tile(
49     opj_tcd_tilecomp_t* tilec,
50     OPJ_UINT32 numres);
51
52-static OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
53-        void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32));
54+static OPJ_BOOL opj_dwt_encode_procedure(const opj_tcd_tilecomp_t * tilec,
55+        void(*p_function)(OPJ_INT32 *, OPJ_SIZE_T, OPJ_INT32, OPJ_INT32, OPJ_INT32));
56
57 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
58         OPJ_UINT32 i);
59@@ -205,13 +205,20 @@ static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
60
61 /*@}*/
62
63-#define OPJ_S(i) a[(i)*2]
64-#define OPJ_D(i) a[(1+(i)*2)]
65-#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
66-#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
67+#define IDX_S(i) (i)*2
68+#define IDX_D(i) 1 + (i)* 2
69+#define UNDERFLOW_SN(i) ((i) >= sn&&sn>0)
70+#define UNDERFLOW_DN(i) ((i) >= dn&&dn>0)
71+#define OVERFLOW_S(i) (IDX_S(i) >= a_count)
72+#define OVERFLOW_D(i) (IDX_D(i) >= a_count)
73+
74+#define OPJ_S(i) a[IDX_S(i)]
75+#define OPJ_D(i) a[IDX_D(i)]
76+#define OPJ_S_(i) ((i)<0 ? OPJ_S(0) : (UNDERFLOW_SN(i) ? OPJ_S(sn - 1) : OVERFLOW_S(i) ? OPJ_S(i - 1) : OPJ_S(i)))
77+#define OPJ_D_(i) ((i)<0 ? OPJ_D(0) : (UNDERFLOW_DN(i) ? OPJ_D(dn - 1) : OVERFLOW_D(i) ? OPJ_D(i - 1) : OPJ_D(i)))
78 /* new */
79-#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
80-#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
81+#define OPJ_SS_(i) ((i)<0 ? OPJ_S(0) : (UNDERFLOW_DN(i) ? OPJ_S(dn - 1) : OVERFLOW_S(i) ? OPJ_S(i - 1) : OPJ_S(i)))
82+#define OPJ_DD_(i) ((i)<0 ? OPJ_D(0) : (UNDERFLOW_SN(i) ? OPJ_D(sn - 1) : OVERFLOW_D(i) ? OPJ_D(i - 1) : OPJ_D(i)))
83
84 /* <summary>                                                              */
85 /* This table contains the norms of the 5-3 wavelets for different bands. */
86@@ -344,8 +351,8 @@ static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
87 /* <summary>                            */
88 /* Forward 5-3 wavelet transform in 1-D. */
89 /* </summary>                           */
90-static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
91-                             OPJ_INT32 cas)
92+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_SIZE_T a_count, OPJ_INT32 dn,
93+                             OPJ_INT32 sn, OPJ_INT32 cas)
94 {
95     OPJ_INT32 i;
96
97@@ -376,8 +383,8 @@ static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
98 /* <summary>                            */
99 /* Inverse 5-3 wavelet transform in 1-D. */
100 /* </summary>                           */
101-static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
102-                              OPJ_INT32 cas)
103+static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_SIZE_T a_count, OPJ_INT32 dn,
104+                              OPJ_INT32 sn, OPJ_INT32 cas)
105 {
106     OPJ_INT32 i;
107
108@@ -406,7 +413,7 @@ static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
109
110 static void opj_dwt_decode_1(const opj_dwt_t *v)
111 {
112-    opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
113+    opj_dwt_decode_1_(v->mem, v->mem_count, v->dn, v->sn, v->cas);
114 }
115
116 #endif /* STANDARD_SLOW_VERSION */
117@@ -1037,8 +1044,8 @@ static void opj_idwt53_v(const opj_dwt_t *dwt,
118 /* <summary>                             */
119 /* Forward 9-7 wavelet transform in 1-D. */
120 /* </summary>                            */
121-static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
122-                                  OPJ_INT32 cas)
123+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_SIZE_T a_count,
124+                                  OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas)
125 {
126     OPJ_INT32 i;
127     if (!cas) {
128@@ -1106,8 +1113,8 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
129 /* <summary>                            */
130 /* Forward 5-3 wavelet transform in 2-D. */
131 /* </summary>                           */
132-static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
133-        void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32))
134+static INLINE OPJ_BOOL opj_dwt_encode_procedure(const opj_tcd_tilecomp_t * tilec,
135+        void(*p_function)(OPJ_INT32 *, OPJ_SIZE_T, OPJ_INT32, OPJ_INT32, OPJ_INT32))
136 {
137     OPJ_INT32 i, j, k;
138     OPJ_INT32 *a = 00;
139@@ -1117,6 +1124,7 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
140
141     OPJ_INT32 rw;           /* width of the resolution level computed   */
142     OPJ_INT32 rh;           /* height of the resolution level computed  */
143+    OPJ_SIZE_T l_data_count;
144     OPJ_SIZE_T l_data_size;
145
146     opj_tcd_resolution_t * l_cur_res = 0;
147@@ -1129,13 +1137,13 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
148     l_cur_res = tilec->resolutions + l;
149     l_last_res = l_cur_res - 1;
150
151-    l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
152+    l_data_count = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
153     /* overflow check */
154-    if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
155+    if (l_data_count > (SIZE_MAX / sizeof(OPJ_INT32))) {
156         /* FIXME event manager error callback */
157         return OPJ_FALSE;
158     }
159-    l_data_size *= sizeof(OPJ_INT32);
160+    l_data_size = l_data_count * sizeof(OPJ_INT32);
161     bj = (OPJ_INT32*)opj_malloc(l_data_size);
162     /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
163     /* in that case, so do not error out */
164@@ -1167,7 +1175,7 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
165                 bj[k] = aj[k * w];
166             }
167
168-            (*p_function)(bj, dn, sn, cas_col);
169+            (*p_function) (bj, l_data_count, dn, sn, cas_col);
170
171             opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
172         }
173@@ -1180,7 +1188,7 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
174             for (k = 0; k < rw; k++) {
175                 bj[k] = aj[k];
176             }
177-            (*p_function)(bj, dn, sn, cas_row);
178+            (*p_function) (bj, l_data_count, dn, sn, cas_row);
179             opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
180         }
181
182@@ -1379,7 +1387,7 @@ static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
183 /* Inverse wavelet transform in 2-D.    */
184 /* </summary>                           */
185 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
186-                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
187+        const opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
188 {
189     opj_dwt_t h;
190     opj_dwt_t v;
191@@ -1401,22 +1409,23 @@ static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
192         return OPJ_TRUE;
193     }
194     num_threads = opj_thread_pool_get_thread_count(tp);
195-    h_mem_size = opj_dwt_max_resolution(tr, numres);
196+    h.mem_count = opj_dwt_max_resolution(tr, numres);
197     /* overflow check */
198-    if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
199+    if (h.mem_count > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
200         /* FIXME event manager error callback */
201         return OPJ_FALSE;
202     }
203     /* We need PARALLEL_COLS_53 times the height of the array, */
204     /* since for the vertical pass */
205     /* we process PARALLEL_COLS_53 columns at a time */
206-    h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
207+    h_mem_size = h.mem_count * PARALLEL_COLS_53 * sizeof(OPJ_INT32);
208     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
209     if (! h.mem) {
210         /* FIXME event manager error callback */
211         return OPJ_FALSE;
212     }
213
214+    v.mem_count = h.mem_count;
215     v.mem = h.mem;
216
217     while (--numres) {
218@@ -1594,7 +1603,8 @@ static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
219     OPJ_UNUSED(ret);
220 }
221
222-static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
223+static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_SIZE_T a_count,
224+                                     OPJ_INT32 dn, OPJ_INT32 sn,
225                                      OPJ_INT32 cas,
226                                      OPJ_INT32 win_l_x0,
227                                      OPJ_INT32 win_l_x1,
228@@ -1974,16 +1984,16 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
229         opj_sparse_array_int32_free(sa);
230         return OPJ_TRUE;
231     }
232-    h_mem_size = opj_dwt_max_resolution(tr, numres);
233+    h.mem_count = opj_dwt_max_resolution(tr, numres);
234     /* overflow check */
235     /* in vertical pass, we process 4 columns at a time */
236-    if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
237+    if (h.mem_count > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
238         /* FIXME event manager error callback */
239         opj_sparse_array_int32_free(sa);
240         return OPJ_FALSE;
241     }
242
243-    h_mem_size *= 4 * sizeof(OPJ_INT32);
244+    h_mem_size = h.mem_count * 4 * sizeof(OPJ_INT32);
245     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
246     if (! h.mem) {
247         /* FIXME event manager error callback */
248@@ -1991,6 +2001,7 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
249         return OPJ_FALSE;
250     }
251
252+    v.mem_count = h.mem_count;
253     v.mem = h.mem;
254
255     for (resno = 1; resno < numres; resno ++) {
256@@ -2101,7 +2112,7 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
257                                              win_ll_x1,
258                                              win_hl_x0,
259                                              win_hl_x1);
260-                opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
261+                opj_dwt_decode_partial_1(h.mem, h.mem_count, h.dn, h.sn, h.cas,
262                                          (OPJ_INT32)win_ll_x0,
263                                          (OPJ_INT32)win_ll_x1,
264                                          (OPJ_INT32)win_hl_x0,
265