OpenJPH: /build/openjph/src/OpenJPH/src/core/coding/ojph_block_common.cpp Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
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//***************************************************************************/
47
48namespace ojph {
49 namespace local {
50
51 //************************************************************************/
67
69 ui16 vlc_tbl0[1024] = { 0 };
72 ui16 vlc_tbl1[1024] = { 0 };
74
75 //************************************************************************/
102
104 ui16 uvlc_tbl0[256+64] = { 0 };
107 ui16 uvlc_tbl1[256] = { 0 };
109 ui8 uvlc_bias[256+64] = { 0 };
111
112 //************************************************************************/
117 static bool vlc_init_tables()
118 {
119 const bool debug = false; //useful for checking
120
121 //Data in the table is arranged in this format (taken from the standard)
122 // c_q is the context for a quad
123 // rho is the significance pattern for a quad
124 // u_off indicate if u value is 0 (u_off is 0), or communicated
125 // e_k, e_1 EMB patterns
126 // cwd VLC codeword
127 // cwd VLC codeword length
128 struct vlc_src_table { int c_q, rho, u_off, e_k, e_1, cwd, cwd_len; };
129 // initial quad rows
130 vlc_src_table tbl0[] = {
131 #include "table0.h"
132 };
133 // number of entries in the table
134 size_t tbl0_size = sizeof(tbl0) / sizeof(vlc_src_table);
135
136 // nono-initial quad rows
137 vlc_src_table tbl1[] = {
138 #include "table1.h"
139 };
140 // number of entries in the table
141 size_t tbl1_size = sizeof(tbl1) / sizeof(vlc_src_table);
142
143 if (debug) memset(vlc_tbl0, 0, sizeof(vlc_tbl0)); //unnecessary
144
145 // this is to convert table entries into values for decoder look up
146 // There can be at most 1024 possibilities, not all of them are valid.
147 //
148 for (int i = 0; i < 1024; ++i)
149 {
150 int cwd = i & 0x7F; // from i extract codeword
151 int c_q = i >> 7; // from i extract context
152 // See if this case exist in the table, if so then set the entry in
153 // vlc_tbl0
154 for (size_t j = 0; j < tbl0_size; ++j)
155 if (tbl0[j].c_q == c_q) // this is an and operation
156 if (tbl0[j].cwd == (cwd & ((1 << tbl0[j].cwd_len) - 1)))
157 {
158 if (debug) assert(vlc_tbl0[i] == 0);
159 // Put this entry into the table
160 vlc_tbl0[i] = (ui16)((tbl0[j].rho << 4) | (tbl0[j].u_off << 3)
161 | (tbl0[j].e_k << 12) | (tbl0[j].e_1 << 8) | tbl0[j].cwd_len);
162 }
163 }
164
165 if (debug) memset(vlc_tbl1, 0, sizeof(vlc_tbl1)); //unnecessary
166
167 // this the same as above but for non-initial rows
168 for (int i = 0; i < 1024; ++i)
169 {
170 int cwd = i & 0x7F; //7 bits
171 int c_q = i >> 7;
172 for (size_t j = 0; j < tbl1_size; ++j)
173 if (tbl1[j].c_q == c_q) // this is an and operation
174 if (tbl1[j].cwd == (cwd & ((1 << tbl1[j].cwd_len) - 1)))
175 {
176 if (debug) assert(vlc_tbl1[i] == 0);
177 vlc_tbl1[i] = (ui16)((tbl1[j].rho << 4) | (tbl1[j].u_off << 3)
178 | (tbl1[j].e_k << 12) | (tbl1[j].e_1 << 8) | tbl1[j].cwd_len);
179 }
180 }
181
182 return true;
183 }
184
185 //************************************************************************/
189 static bool uvlc_init_tables()
190 {
191 // table stores possible decoding three bits from vlc
192 // there are 8 entries for xx1, x10, 100, 000, where x means do not
193 // care table value is made up of
194 // 2 bits in the LSB for prefix length
195 // 3 bits for suffix length
196 // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
197 static const ui8 dec[8] = { // the index is the prefix codeword
198 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
199 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
200 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
201 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
202 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
203 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
204 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
205 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
206 };
207
208 for (ui32 i = 0; i < 256 + 64; ++i)
209 {
210 ui32 mode = i >> 6;
211 ui32 vlc = i & 0x3F;
212
213 if (mode == 0) { // both u_off are 0
214 uvlc_tbl0[i] = 0;
215 uvlc_bias[i] = 0;
216 }
217 else if (mode <= 2) // u_off are either 01 or 10
218 {
219 ui32 d = dec[vlc & 0x7]; //look at the least significant 3 bits
220
221 ui32 total_prefix = d & 0x3;
222 ui32 total_suffix = (d >> 2) & 0x7;
223 ui32 u0_suffix_len = (mode == 1) ? total_suffix : 0;
224 ui32 u0 = (mode == 1) ? (d >> 5) : 0;
225 ui32 u1 = (mode == 1) ? 0 : (d >> 5);
226
227 uvlc_tbl0[i] = (ui16)(total_prefix |
228 (total_suffix << 3) |
229 (u0_suffix_len << 7) |
230 (u0 << 10) |
231 (u1 << 13));
232
233 }
234 else if (mode == 3) // both u_off are 1, and MEL event is 0
235 {
236 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
237 vlc >>= d0 & 0x3; // Consume bits
238 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
239
240 ui32 total_prefix, u0_suffix_len, total_suffix, u0, u1;
241 if ((d0 & 0x3) == 3)
242 {
243 total_prefix = (d0 & 0x3) + 1;
244 u0_suffix_len = (d0 >> 2) & 0x7;
245 total_suffix = u0_suffix_len;
246 u0 = d0 >> 5;
247 u1 = (vlc & 1) + 1;
248 uvlc_bias[i] = 4; // 0b00 for u0 and 0b01 for u1
249 }
250 else
251 {
252 total_prefix = (d0 & 0x3) + (d1 & 0x3);
253 u0_suffix_len = (d0 >> 2) & 0x7;
254 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
255 u0 = d0 >> 5;
256 u1 = d1 >> 5;
257 uvlc_bias[i] = 0;
258 }
259
260 uvlc_tbl0[i] = (ui16)(total_prefix |
261 (total_suffix << 3) |
262 (u0_suffix_len << 7) |
263 (u0 << 10) |
264 (u1 << 13));
265 }
266 else if (mode == 4) // both u_off are 1, and MEL event is 1
267 {
268 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
269 vlc >>= d0 & 0x3; // Consume bits
270 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
271
272 ui32 total_prefix = (d0 & 0x3) + (d1 & 0x3);
273 ui32 u0_suffix_len = (d0 >> 2) & 0x7;
274 ui32 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
275 ui32 u0 = (d0 >> 5) + 2;
276 ui32 u1 = (d1 >> 5) + 2;
277
278 uvlc_tbl0[i] = (ui16)(total_prefix |
279 (total_suffix << 3) |
280 (u0_suffix_len << 7) |
281 (u0 << 10) |
282 (u1 << 13));
283 uvlc_bias[i] = 10; // 0b10 for u0 and 0b10 for u1
284 }
285 }
286
287 for (ui32 i = 0; i < 256; ++i)
288 {
289 ui32 mode = i >> 6;
290 ui32 vlc = i & 0x3F;
291
292 if (mode == 0) // both u_off are 0
293 uvlc_tbl1[i] = 0;
294 else if (mode <= 2) // u_off are either 01 or 10
295 {
296 ui32 d = dec[vlc & 0x7]; // look at the 3 LSB bits
297
298 ui32 total_prefix = d & 0x3;
299 ui32 total_suffix = (d >> 2) & 0x7;
300 ui32 u0_suffix_len = (mode == 1) ? total_suffix : 0;
301 ui32 u0 = (mode == 1) ? (d >> 5) : 0;
302 ui32 u1 = (mode == 1) ? 0 : (d >> 5);
303
304 uvlc_tbl1[i] = (ui16)(total_prefix |
305 (total_suffix << 3) |
306 (u0_suffix_len << 7) |
307 (u0 << 10) |
308 (u1 << 13));
309 }
310 else if (mode == 3) // both u_off are 1
311 {
312 ui32 d0 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
313 vlc >>= d0 & 0x3; // Consume bits
314 ui32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
315
316 ui32 total_prefix = (d0 & 0x3) + (d1 & 0x3);
317 ui32 u0_suffix_len = (d0 >> 2) & 0x7;
318 ui32 total_suffix = u0_suffix_len + ((d1 >> 2) & 0x7);
319 ui32 u0 = d0 >> 5;
320 ui32 u1 = d1 >> 5;
321
322 uvlc_tbl1[i] = (ui16)(total_prefix |
323 (total_suffix << 3) |
324 (u0_suffix_len << 7) |
325 (u0 << 10) |
326 (u1 << 13));
327 }
328 }
329 return true;
330 }
331
332 //************************************************************************/
336 static bool vlc_tables_initialized = vlc_init_tables();
337
338 //************************************************************************/
342 static bool uvlc_tables_initialized = uvlc_init_tables();
343
344 } // !namespace local
345} // !namespace ojph
ojph::local::uvlc_bias
ui8 uvlc_bias[256+64]
uvlc_bias contains decoding info. for initial row of quads
Definition ojph_block_common.cpp:109
ojph::local::uvlc_tables_initialized
static bool uvlc_tables_initialized
Initializes UVLC tables uvlc_tbl0 and uvlc_tbl1.
Definition ojph_block_common.cpp:342
ojph::local::uvlc_init_tables
static bool uvlc_init_tables()
Initializes uvlc_tbl0 and uvlc_tbl1 tables.
Definition ojph_block_common.cpp:189
ojph::local::uvlc_tbl0
ui16 uvlc_tbl0[256+64]
uvlc_tbl0 contains decoding information for initial row of quads
Definition ojph_block_common.cpp:104
ojph::local::uvlc_tbl1
ui16 uvlc_tbl1[256]
uvlc_tbl1 contains decoding information for non-initial row of quads
Definition ojph_block_common.cpp:107
ojph::local::vlc_tables_initialized
static bool vlc_tables_initialized
Initializes VLC tables vlc_tbl0 and vlc_tbl1.
Definition ojph_block_common.cpp:336
ojph::local::vlc_init_tables
static bool vlc_init_tables()
Initializes vlc_tbl0 and vlc_tbl1 tables, from table0.h and table1.h.
Definition ojph_block_common.cpp:117
ojph::local::vlc_tbl1
ui16 vlc_tbl1[1024]
vlc_tbl1 contains decoding information for non-initial row of quads
Definition ojph_block_common.cpp:72
ojph::local::vlc_tbl0
ui16 vlc_tbl0[1024]
vlc_tbl0 contains decoding information for initial row of quads
Definition ojph_block_common.cpp:69
ojph::local
Definition ojph_bitbuffer_read.h:52
ojph
Definition ojph_img_io.h:52
ojph::ui16
uint16_t ui16
Definition ojph_defs.h:52
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition ojph_defs.h:50
ojph_block_common.h
table0.h
table1.h