OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_block_common.cpp
Go to the documentation of this file.
1/***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2022, Aous Naman
6// Copyright (c) 2022, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2022, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_block_common.cpp
34// Author: Aous Naman
35// Date: 13 May 2022
36//***************************************************************************/
37
38#include <cassert>
39#include <cstddef>
40#include <cstring>
41#include "ojph_block_common.h"
42
43//***************************************************************************/
48namespace ojph {
49 namespace local {
50
51 //************************************************************************/
68
69 ui16 vlc_tbl0[1024] = { 0 };
72 ui16 vlc_tbl1[1024] = { 0 };
74
75 //************************************************************************/
94
95 ui16 uvlc_tbl0[256+64] = { 0 };
98 ui16 uvlc_tbl1[256] = { 0 };
100
101 //************************************************************************/
106 static bool vlc_init_tables()
107 {
108 const bool debug = false; //useful for checking
109
110 //Data in the table is arranged in this format (taken from the standard)
111 // c_q is the context for a quad
112 // rho is the signficance pattern for a quad
113 // u_off indicate if u value is 0 (u_off is 0), or communicated
114 // e_k, e_1 EMB patterns
115 // cwd VLC codeword
116 // cwd VLC codeword length
117 struct vlc_src_table { int c_q, rho, u_off, e_k, e_1, cwd, cwd_len; };
118 // initial quad rows
119 vlc_src_table tbl0[] = {
120 #include "table0.h"
121 };
122 // number of entries in the table
123 size_t tbl0_size = sizeof(tbl0) / sizeof(vlc_src_table);
124
125 // nono-initial quad rows
126 vlc_src_table tbl1[] = {
127 #include "table1.h"
128 };
129 // number of entries in the table
130 size_t tbl1_size = sizeof(tbl1) / sizeof(vlc_src_table);
131
132 if (debug) memset(vlc_tbl0, 0, sizeof(vlc_tbl0)); //unnecessary
133
134 // this is to convert table entries into values for decoder look up
135 // There can be at most 1024 possibilites, not all of them are valid.
136 //
137 for (int i = 0; i < 1024; ++i)
138 {
139 int cwd = i & 0x7F; // from i extract codeword
140 int c_q = i >> 7; // from i extract context
141 // See if this case exist in the table, if so then set the entry in
142 // vlc_tbl0
143 for (size_t j = 0; j < tbl0_size; ++j)
144 if (tbl0[j].c_q == c_q) // this is an and operation
145 if (tbl0[j].cwd == (cwd & ((1 << tbl0[j].cwd_len) - 1)))
146 {
147 if (debug) assert(vlc_tbl0[i] == 0);
148 // Put this entry into the table
149 vlc_tbl0[i] = (ui16)((tbl0[j].rho << 4) | (tbl0[j].u_off << 3)
150 | (tbl0[j].e_k << 12) | (tbl0[j].e_1 << 8) | tbl0[j].cwd_len);
151 }
152 }
153
154 if (debug) memset(vlc_tbl1, 0, sizeof(vlc_tbl1)); //unnecessary
155
156 // this the same as above but for non-initial rows
157 for (int i = 0; i < 1024; ++i)
158 {
159 int cwd = i & 0x7F; //7 bits
160 int c_q = i >> 7;
161 for (size_t j = 0; j < tbl1_size; ++j)
162 if (tbl1[j].c_q == c_q) // this is an and operation
163 if (tbl1[j].cwd == (cwd & ((1 << tbl1[j].cwd_len) - 1)))
164 {
165 if (debug) assert(vlc_tbl1[i] == 0);
166 vlc_tbl1[i] = (ui16)((tbl1[j].rho << 4) | (tbl1[j].u_off << 3)
167 | (tbl1[j].e_k << 12) | (tbl1[j].e_1 << 8) | tbl1[j].cwd_len);
168 }
169 }
170
171 return true;
172 }
173
174 //************************************************************************/
178 static bool uvlc_init_tables()
179 {
180 // table stores possible decoding three bits from vlc
181 // there are 8 entries for xx1, x10, 100, 000, where x means do not
182 // care table value is made up of
183 // 2 bits in the LSB for prefix length
184 // 3 bits for suffix length
185 // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
186 static const ui8 dec[8] = { // the index is the prefix codeword
187 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
188 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
189 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
190 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
191 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
192 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
193 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
194 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
195 };
196
197 for (ui32 i = 0; i < 256 + 64; ++i)
198 {
199 ui32 mode = i >> 6;
200 ui32 vlc = i & 0x3F;
201
202 if (mode == 0) // both u_off are 0
203 uvlc_tbl0[i] = 0;
204 else if (mode <= 2) // u_off are either 01 or 10
205 {
206 ui32 d = dec[vlc & 0x7]; //look at the least significant 3 bits
207
208 ui32 total_prefix = d & 0x3;
209 ui32 total_suffix = (d >> 2) & 0x7;
210 ui32 u0_suffix_len = (mode == 1) ? total_suffix : 0;
211 ui32 u0 = (mode == 1) ? (d >> 5) : 0;
212 ui32 u1 = (mode == 1) ? 0 : (d >> 5);
213
214 uvlc_tbl0[i] = (ui16)(total_prefix |
215 (total_suffix << 3) |
216 (u0_suffix_len << 7) |
217 (u0 << 10) |
218 (u1 << 13));
219
220 }
221 else if (mode == 3) // both u_off are 1, and MEL event is 0
222 {
223 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
224 vlc >>= d0 & 0x3; // Consume bits
225 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
226
227 ui32 total_prefix, u0_suffix_len, total_suffix, u0, u1;
228 if ((d0 & 0x3) == 3)
229 {
230 total_prefix = (d0 & 0x3) + 1;
231 u0_suffix_len = (d0 >> 2) & 0x7;
232 total_suffix = u0_suffix_len;
233 u0 = d0 >> 5;
234 u1 = (vlc & 1) + 1;
235 }
236 else
237 {
238 total_prefix = (d0 & 0x3) + (d1 & 0x3);
239 u0_suffix_len = (d0 >> 2) & 0x7;
240 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
241 u0 = d0 >> 5;
242 u1 = d1 >> 5;
243 }
244
245 uvlc_tbl0[i] = (ui16)(total_prefix |
246 (total_suffix << 3) |
247 (u0_suffix_len << 7) |
248 (u0 << 10) |
249 (u1 << 13));
250 }
251 else if (mode == 4) // both u_off are 1, and MEL event is 1
252 {
253 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
254 vlc >>= d0 & 0x3; // Consume bits
255 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
256
257 ui32 total_prefix = (d0 & 0x3) + (d1 & 0x3);
258 ui32 u0_suffix_len = (d0 >> 2) & 0x7;
259 ui32 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
260 ui32 u0 = (d0 >> 5) + 2;
261 ui32 u1 = (d1 >> 5) + 2;
262
263 uvlc_tbl0[i] = (ui16)(total_prefix |
264 (total_suffix << 3) |
265 (u0_suffix_len << 7) |
266 (u0 << 10) |
267 (u1 << 13));
268 }
269 }
270
271 for (ui32 i = 0; i < 256; ++i)
272 {
273 ui32 mode = i >> 6;
274 ui32 vlc = i & 0x3F;
275
276 if (mode == 0) // both u_off are 0
277 uvlc_tbl1[i] = 0;
278 else if (mode <= 2) // u_off are either 01 or 10
279 {
280 ui32 d = dec[vlc & 0x7]; // look at the 3 LSB bits
281
282 ui32 total_prefix = d & 0x3;
283 ui32 total_suffix = (d >> 2) & 0x7;
284 ui32 u0_suffix_len = (mode == 1) ? total_suffix : 0;
285 ui32 u0 = (mode == 1) ? (d >> 5) : 0;
286 ui32 u1 = (mode == 1) ? 0 : (d >> 5);
287
288 uvlc_tbl1[i] = (ui16)(total_prefix |
289 (total_suffix << 3) |
290 (u0_suffix_len << 7) |
291 (u0 << 10) |
292 (u1 << 13));
293 }
294 else if (mode == 3) // both u_off are 1
295 {
296 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
297 vlc >>= d0 & 0x3; // Consume bits
298 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
299
300 ui32 total_prefix = (d0 & 0x3) + (d1 & 0x3);
301 ui32 u0_suffix_len = (d0 >> 2) & 0x7;
302 ui32 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
303 ui32 u0 = d0 >> 5;
304 ui32 u1 = d1 >> 5;
305
306 uvlc_tbl1[i] = (ui16)(total_prefix |
307 (total_suffix << 3) |
308 (u0_suffix_len << 7) |
309 (u0 << 10) |
310 (u1 << 13));
311 }
312 }
313 return true;
314 }
315
316 //************************************************************************/
321
322 //************************************************************************/
327
328 } // !namespace local
329} // !namespace ojph
static bool uvlc_tables_initialized
Initializes UVLC tables uvlc_tbl0 and uvlc_tbl1.
static bool uvlc_init_tables()
Initializes uvlc_tbl0 and uvlc_tbl1 tables.
ui16 uvlc_tbl0[256+64]
uvlc_tbl0 contains decoding information for initial row of quads
ui16 uvlc_tbl1[256]
uvlc_tbl1 contains decoding information for non-initial row of quads
static bool vlc_tables_initialized
Initializes VLC tables vlc_tbl0 and vlc_tbl1.
static bool vlc_init_tables()
Initializes vlc_tbl0 and vlc_tbl1 tables, from table0.h and table1.h.
ui16 vlc_tbl0[1024]
vlc_tbl0 contains decoding information for initial row of quads
ui16 vlc_tbl1[1024]
vlc_tbl1 contains decoding information for non-initial row of quads
uint16_t ui16
Definition: ojph_defs.h:52
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50