1diff --git a/third_party/libopenjpeg20/dwt.c b/third_party/libopenjpeg20/dwt.c 2index 5930d1c71..6512b1e4c 100644 3--- a/third_party/libopenjpeg20/dwt.c 4+++ b/third_party/libopenjpeg20/dwt.c 5@@ -63,9 +63,6 @@ 6 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ 7 /*@{*/ 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 #define VREG_INT_COUNT 8 15@@ -82,6 +79,7 @@ 16 17 typedef struct dwt_local { 18 OPJ_INT32* mem; 19+ OPJ_SIZE_T mem_count; 20 OPJ_INT32 dn; /* number of elements in high pass band */ 21 OPJ_INT32 sn; /* number of elements in low pass band */ 22 OPJ_INT32 cas; /* 0 = start on even coord, 1 = start on odd coord */ 23@@ -133,13 +131,13 @@ static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, 24 /** 25 Forward 5-3 wavelet transform in 1-D 26 */ 27-static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, 28- OPJ_INT32 cas); 29+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_SIZE_T a_count, OPJ_INT32 dn, 30+ OPJ_INT32 sn, OPJ_INT32 cas); 31 /** 32 Forward 9-7 wavelet transform in 1-D 33 */ 34-static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, 35- OPJ_INT32 cas); 36+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_SIZE_T a_count, 37+ OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas); 38 /** 39 Explicit calculation of the Quantization Stepsizes 40 */ 41@@ -149,14 +147,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 +203,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 +349,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 +381,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 +411,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 +1042,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 +1111,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 +1122,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 +1135,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 +1173,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 +1186,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 +1385,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 +1407,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 +1601,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 +1982,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 +1999,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 +2110,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