Debian Codegraph @

src/lib/core/t1/OJPH/coding/ojph_block_common.cpp

raw
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 //***************************************************************************/
44 /** @file ojph_block_common.cpp
45 * @brief defines tables used for decoding HTJ2K blocks
46 */
47
48 namespace ojph {
49 namespace local {
50
51 //************************************************************************/
52 /** @defgroup vlc_decoding_tables_grp VLC decoding tables
53 * @{
54 * VLC decoding tables used in decoding VLC codewords to these fields: \n
55 * \li \c cwd_len : 3bits -> the codeword length of the VLC codeword;
56 * the VLC cwd is in the LSB of bitstream \n
57 * \li \c u_off : 1bit -> u_offset, which is 1 if u value is not 0 \n
58 * \li \c rho : 4bits -> signficant samples within a quad \n
59 * \li \c e_1 : 4bits -> EMB e_1 \n
60 * \li \c e_k : 4bits -> EMB e_k \n
61 * \n
62 * The table index is 10 bits and composed of two parts: \n
63 * The 7 LSBs contain a codeword which might be shorter than 7 bits;
64 * this word is the next decoable bits in the bitstream. \n
65 * The 3 MSB is the context of for the codeword. \n
66 */
67
68 /// @brief vlc_tbl0 contains decoding information for initial row of quads
69 ui16 vlc_tbl0[1024] = { 0 };
70 /// @brief vlc_tbl1 contains decoding information for non-initial row of
71 /// quads
72 ui16 vlc_tbl1[1024] = { 0 };
73 /// @}
74
75 //************************************************************************/
76 /** @defgroup uvlc_decoding_tables_grp VLC decoding tables
77 * @{
78 * UVLC decoding tables used to partiallu decode u values from UVLC
79 * codewords. \n
80 * The table index is 8 (or 9) bits and composed of two parts: \n
81 * The 6 LSBs carries the head of the VLC to be decoded. Up to 6 bits to
82 * be used; these are uvlc prefix code for quad 0 and 1 \n
83 * The 2 (or 3) MSBs contain u_off of quad 0 + 2 * o_off quad 1
84 * + 4 * mel event for initial row of quads when needed \n
85 * \n
86 * Each entry contains, starting from the LSB \n
87 * \li \c total prefix length for quads 0 and 1 (3 bits) \n
88 * \li \c total suffix length for quads 0 and 1 (4 bits) \n
89 * \li \c suffix length for quad 0 (3 bits) \n
90 * \li \c prefix for quad 0 (3 bits) \n
91 * \li \c prefix for quad 1 (3 bits) \n
92 */
93
94 /// @brief uvlc_tbl0 contains decoding information for initial row of quads
95 ui16 uvlc_tbl0[256+64] = { 0 };
96 /// @brief uvlc_tbl1 contains decoding information for non-initial row of
97 /// quads
98 ui16 uvlc_tbl1[256] = { 0 };
99 /// @}
100
101 //************************************************************************/
102 /** @ingroup vlc_decoding_tables_grp
103 * @brief Initializes vlc_tbl0 and vlc_tbl1 tables, from table0.h and
104 * table1.h
105 */
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 //************************************************************************/
175 /** @ingroup uvlc_decoding_tables_grp
176 * @brief Initializes uvlc_tbl0 and uvlc_tbl1 tables
177 */
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 //************************************************************************/
317 /** @ingroup vlc_decoding_tables_grp
318 * @brief Initializes VLC tables vlc_tbl0 and vlc_tbl1
319 */
320 static bool vlc_tables_initialized = vlc_init_tables();
321
322 //************************************************************************/
323 /** @ingroup uvlc_decoding_tables_grp
324 * @brief Initializes UVLC tables uvlc_tbl0 and uvlc_tbl1
325 */
326 static bool uvlc_tables_initialized = uvlc_init_tables();
327
328 } // !namespace local
329 } // !namespace ojph