OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_block_encoder.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) 2019, Aous Naman
6// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2019, 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_encoder.cpp
34// Author: Aous Naman
35// Date: 17 September 2019
36//***************************************************************************/
37
38//***************************************************************************/
43#include <cassert>
44#include <cstring>
45#include <cstdint>
46#include <climits>
47
48#include "ojph_mem.h"
49#include "ojph_arch.h"
50#include "ojph_block_encoder.h"
51#include "ojph_message.h"
52
53namespace ojph {
54 namespace local {
55
57 // tables
59
60 //VLC encoding
61 // index is (c_q << 8) + (rho << 4) + eps
62 // data is (cwd << 8) + (cwd_len << 4) + eps
63 // table 0 is for the initial line of quads
64 static ui16 vlc_tbl0[2048] = { 0 };
65 static ui16 vlc_tbl1[2048] = { 0 };
66
67 //UVLC encoding
68 static int ulvc_cwd_pre[33];
69 static int ulvc_cwd_pre_len[33];
70 static int ulvc_cwd_suf[33];
71 static int ulvc_cwd_suf_len[33];
72
74 static bool vlc_init_tables()
75 {
76 struct vlc_src_table { int c_q, rho, u_off, e_k, e_1, cwd, cwd_len; };
77 vlc_src_table tbl0[] = {
78 #include "table0.h"
79 };
80 size_t tbl0_size = sizeof(tbl0) / sizeof(vlc_src_table);
81
82 si32 pattern_popcnt[16];
83 for (ui32 i = 0; i < 16; ++i)
84 pattern_popcnt[i] = (si32)population_count(i);
85
86 vlc_src_table* src_tbl = tbl0;
87 ui16 *tgt_tbl = vlc_tbl0;
88 size_t tbl_size = tbl0_size;
89 for (int i = 0; i < 2048; ++i)
90 {
91 int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF;
92 if (((emb & rho) != emb) || (rho == 0 && c_q == 0))
93 tgt_tbl[i] = 0;
94 else
95 {
96 vlc_src_table *best_entry = NULL;
97 if (emb) // u_off = 1
98 {
99 int best_e_k = -1;
100 for (size_t j = 0; j < tbl_size; ++j)
101 {
102 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
103 if (src_tbl[j].u_off == 1)
104 if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1)
105 {
106 //now we need to find the smallest cwd with the highest
107 // number of bits set in e_k
108 int ones_count = pattern_popcnt[src_tbl[j].e_k];
109 if (ones_count >= best_e_k)
110 {
111 best_entry = src_tbl + j;
112 best_e_k = ones_count;
113 }
114 }
115 }
116 }
117 else // u_off = 0
118 {
119 for (size_t j = 0; j < tbl_size; ++j)
120 {
121 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
122 if (src_tbl[j].u_off == 0)
123 {
124 best_entry = src_tbl + j;
125 break;
126 }
127 }
128 }
129 assert(best_entry);
130 tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4)
131 + best_entry->e_k);
132 }
133 }
134
135 vlc_src_table tbl1[] = {
136 #include "table1.h"
137 };
138 size_t tbl1_size = sizeof(tbl1) / sizeof(vlc_src_table);
139
140 src_tbl = tbl1;
141 tgt_tbl = vlc_tbl1;
142 tbl_size = tbl1_size;
143 for (int i = 0; i < 2048; ++i)
144 {
145 int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF;
146 if (((emb & rho) != emb) || (rho == 0 && c_q == 0))
147 tgt_tbl[i] = 0;
148 else
149 {
150 vlc_src_table *best_entry = NULL;
151 if (emb) // u_off = 1
152 {
153 int best_e_k = -1;
154 for (size_t j = 0; j < tbl_size; ++j)
155 {
156 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
157 if (src_tbl[j].u_off == 1)
158 if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1)
159 {
160 //now we need to find the smallest cwd with the highest
161 // number of bits set in e_k
162 int ones_count = pattern_popcnt[src_tbl[j].e_k];
163 if (ones_count >= best_e_k)
164 {
165 best_entry = src_tbl + j;
166 best_e_k = ones_count;
167 }
168 }
169 }
170 }
171 else // u_off = 0
172 {
173 for (size_t j = 0; j < tbl_size; ++j)
174 {
175 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
176 if (src_tbl[j].u_off == 0)
177 {
178 best_entry = src_tbl + j;
179 break;
180 }
181 }
182 }
183 assert(best_entry);
184 tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4)
185 + best_entry->e_k);
186 }
187 }
188
189
190 return true;
191 }
192
194 static bool uvlc_init_tables()
195 {
196 //code goes from 0 to 31, extension and 32 are not supported here
197 ulvc_cwd_pre[0] = 0; ulvc_cwd_pre[1] = 1; ulvc_cwd_pre[2] = 2;
198 ulvc_cwd_pre[3] = 4; ulvc_cwd_pre[4] = 4;
199 ulvc_cwd_pre_len[0] = 0; ulvc_cwd_pre_len[1] = 1;
200 ulvc_cwd_pre_len[2] = 2;
201 ulvc_cwd_pre_len[3] = 3; ulvc_cwd_pre_len[4] = 3;
202 ulvc_cwd_suf[0] = 0; ulvc_cwd_suf[1] = 0; ulvc_cwd_suf[2] = 0;
203 ulvc_cwd_suf[3] = 0; ulvc_cwd_suf[4] = 1;
204 ulvc_cwd_suf_len[0] = 0; ulvc_cwd_suf_len[1] = 0;
205 ulvc_cwd_suf_len[2] = 0;
206 ulvc_cwd_suf_len[3] = 1; ulvc_cwd_suf_len[4] = 1;
207 for (int i = 5; i < 33; ++i)
208 {
209 ulvc_cwd_pre[i] = 0;
210 ulvc_cwd_pre_len[i] = 3;
211 ulvc_cwd_suf[i] = i-5;
212 ulvc_cwd_suf_len[i] = 5;
213 }
214 return true;
215 }
216
220
222 //
224 struct mel_struct {
225 //storage
226 ui8* buf; //pointer to data buffer
227 ui32 pos; //position of next writing within buf
228 ui32 buf_size; //size of buffer, which we must not exceed
229
230 // all these can be replaced by bytes
231 int remaining_bits; //number of empty bits in tmp
232 int tmp; //temporary storage of coded bits
233 int run; //number of 0 run
234 int k; //state
235 int threshold; //threshold where one bit must be coded
236 };
237
239 static inline void
240 mel_init(mel_struct* melp, ui32 buffer_size, ui8* data)
241 {
242 melp->buf = data;
243 melp->pos = 0;
244 melp->buf_size = buffer_size;
245 melp->remaining_bits = 8;
246 melp->tmp = 0;
247 melp->run = 0;
248 melp->k = 0;
249 melp->threshold = 1; // this is 1 << mel_exp[melp->k];
250 }
251
253 static inline void
255 {
256 assert(v == 0 || v == 1);
257 melp->tmp = (melp->tmp << 1) + v;
258 melp->remaining_bits--;
259 if (melp->remaining_bits == 0)
260 {
261 if (melp->pos >= melp->buf_size)
262 OJPH_ERROR(0x00020001, "mel encoder's buffer is full");
263
264 melp->buf[melp->pos++] = (ui8)melp->tmp;
265 melp->remaining_bits = (melp->tmp == 0xFF ? 7 : 8);
266 melp->tmp = 0;
267 }
268 }
269
271 static inline void
272 mel_encode(mel_struct* melp, bool bit)
273 {
274 //MEL exponent
275 static const int mel_exp[13] = {0,0,0,1,1,1,2,2,2,3,3,4,5};
276
277 if (bit == false)
278 {
279 ++melp->run;
280 if (melp->run >= melp->threshold)
281 {
282 mel_emit_bit(melp, 1);
283 melp->run = 0;
284 melp->k = ojph_min(12, melp->k + 1);
285 melp->threshold = 1 << mel_exp[melp->k];
286 }
287 }
288 else
289 {
290 mel_emit_bit(melp, 0);
291 int t = mel_exp[melp->k];
292 while (t > 0)
293 mel_emit_bit(melp, (melp->run >> --t) & 1);
294 melp->run = 0;
295 melp->k = ojph_max(0, melp->k - 1);
296 melp->threshold = 1 << mel_exp[melp->k];
297 }
298 }
299
301 //
303 struct vlc_struct {
304 //storage
305 ui8* buf; //pointer to data buffer
306 ui32 pos; //position of next writing within buf
307 ui32 buf_size; //size of buffer, which we must not exceed
308
309 int used_bits; //number of occupied bits in tmp
310 int tmp; //temporary storage of coded bits
311 bool last_greater_than_8F; //true if last byte us greater than 0x8F
312 };
313
315 static inline void
316 vlc_init(vlc_struct* vlcp, ui32 buffer_size, ui8* data)
317 {
318 vlcp->buf = data + buffer_size - 1; //points to last byte
319 vlcp->pos = 1; //locations will be all -pos
320 vlcp->buf_size = buffer_size;
321
322 vlcp->buf[0] = 0xFF;
323 vlcp->used_bits = 4;
324 vlcp->tmp = 0xF;
325 vlcp->last_greater_than_8F = true;
326 }
327
329 static inline void
330 vlc_encode(vlc_struct* vlcp, int cwd, int cwd_len)
331 {
332 while (cwd_len > 0)
333 {
334 if (vlcp->pos >= vlcp->buf_size)
335 OJPH_ERROR(0x00020002, "vlc encoder's buffer is full");
336
337 int avail_bits = 8 - vlcp->last_greater_than_8F - vlcp->used_bits;
338 int t = ojph_min(avail_bits, cwd_len);
339 vlcp->tmp |= (cwd & ((1 << t) - 1)) << vlcp->used_bits;
340 vlcp->used_bits += t;
341 avail_bits -= t;
342 cwd_len -= t;
343 cwd >>= t;
344 if (avail_bits == 0)
345 {
346 if (vlcp->last_greater_than_8F && vlcp->tmp != 0x7F)
347 {
348 vlcp->last_greater_than_8F = false;
349 continue; //one empty bit remaining
350 }
351 *(vlcp->buf - vlcp->pos) = (ui8)(vlcp->tmp);
352 vlcp->pos++;
353 vlcp->last_greater_than_8F = vlcp->tmp > 0x8F;
354 vlcp->tmp = 0;
355 vlcp->used_bits = 0;
356 }
357 }
358 }
359
361 //
363 static inline void
365 {
366 if (melp->run > 0)
367 mel_emit_bit(melp, 1);
368
369 melp->tmp = melp->tmp << melp->remaining_bits;
370 int mel_mask = (0xFF << melp->remaining_bits) & 0xFF;
371 int vlc_mask = 0xFF >> (8 - vlcp->used_bits);
372 if ((mel_mask | vlc_mask) == 0)
373 return; //last mel byte cannot be 0xFF, since then
374 //melp->remaining_bits would be < 8
375 if (melp->pos >= melp->buf_size)
376 OJPH_ERROR(0x00020003, "mel encoder's buffer is full");
377 int fuse = melp->tmp | vlcp->tmp;
378 if ( ( ((fuse ^ melp->tmp) & mel_mask)
379 | ((fuse ^ vlcp->tmp) & vlc_mask) ) == 0
380 && (fuse != 0xFF) && vlcp->pos > 1)
381 {
382 melp->buf[melp->pos++] = (ui8)fuse;
383 }
384 else
385 {
386 if (vlcp->pos >= vlcp->buf_size)
387 OJPH_ERROR(0x00020004, "vlc encoder's buffer is full");
388 melp->buf[melp->pos++] = (ui8)melp->tmp; //melp->tmp cannot be 0xFF
389 *(vlcp->buf - vlcp->pos) = (ui8)vlcp->tmp;
390 vlcp->pos++;
391 }
392 }
393
395 //
397 struct ms_struct {
398 //storage
399 ui8* buf; //pointer to data buffer
400 ui32 pos; //position of next writing within buf
401 ui32 buf_size; //size of buffer, which we must not exceed
402
403 int max_bits; //maximum number of bits that can be store in tmp
404 int used_bits; //number of occupied bits in tmp
405 ui32 tmp; //temporary storage of coded bits
406 };
407
409 static inline void
410 ms_init(ms_struct* msp, ui32 buffer_size, ui8* data)
411 {
412 msp->buf = data;
413 msp->pos = 0;
414 msp->buf_size = buffer_size;
415 msp->max_bits = 8;
416 msp->used_bits = 0;
417 msp->tmp = 0;
418 }
419
421 static inline void
422 ms_encode(ms_struct* msp, ui32 cwd, int cwd_len)
423 {
424 while (cwd_len > 0)
425 {
426 if (msp->pos >= msp->buf_size)
427 OJPH_ERROR(0x00020005, "magnitude sign encoder's buffer is full");
428 int t = ojph_min(msp->max_bits - msp->used_bits, cwd_len);
429 msp->tmp |= (cwd & ((1U << t) - 1)) << msp->used_bits;
430 msp->used_bits += t;
431 cwd >>= t;
432 cwd_len -= t;
433 if (msp->used_bits >= msp->max_bits)
434 {
435 msp->buf[msp->pos++] = (ui8)msp->tmp;
436 msp->max_bits = (msp->tmp == 0xFF) ? 7 : 8;
437 msp->tmp = 0;
438 msp->used_bits = 0;
439 }
440 }
441 }
442
444 static inline void
446 {
447 if (msp->used_bits)
448 {
449 int t = msp->max_bits - msp->used_bits; //unused bits
450 msp->tmp |= (0xFF & ((1U << t) - 1)) << msp->used_bits;
451 msp->used_bits += t;
452 if (msp->tmp != 0xFF)
453 {
454 if (msp->pos >= msp->buf_size)
455 OJPH_ERROR(0x00020006, "magnitude sign encoder's buffer is full");
456 msp->buf[msp->pos++] = (ui8)msp->tmp;
457 }
458 }
459 else if (msp->max_bits == 7)
460 msp->pos--;
461 }
462
464 //
465 //
466 //
467 //
468 //
470 void ojph_encode_codeblock(ui32* buf, ui32 missing_msbs, ui32 num_passes,
471 ui32 width, ui32 height, ui32 stride,
472 ui32* lengths,
474 ojph::coded_lists *& coded)
475 {
476 assert(num_passes == 1);
477 (void)num_passes; //currently not used
478 const int ms_size = (16384*16+14)/15; //more than enough
479 ui8 ms_buf[ms_size];
480 const int mel_vlc_size = 3072; //more than enough
481 ui8 mel_vlc_buf[mel_vlc_size];
482 const int mel_size = 192;
483 ui8 *mel_buf = mel_vlc_buf;
484 const int vlc_size = mel_vlc_size - mel_size;
485 ui8 *vlc_buf = mel_vlc_buf + mel_size;
486
487 mel_struct mel;
488 mel_init(&mel, mel_size, mel_buf);
489 vlc_struct vlc;
490 vlc_init(&vlc, vlc_size, vlc_buf);
491 ms_struct ms;
492 ms_init(&ms, ms_size, ms_buf);
493
494 ui32 p = 30 - missing_msbs;
495
496 //e_val: E values for a line (these are the highest set bit)
497 //cx_val: is the context values
498 //Each byte stores the info for the 2 sample. For E, it is maximum
499 // of the two samples, while for cx, it is the OR of these two samples.
500 //The maximum is between the pixel at the bottom left of one quad
501 // and the bottom right of the earlier quad. The same is true for cx.
502 //For a 1024 pixels, we need 512 bytes, the 2 extra,
503 // one for the non-existing earlier quad, and one for beyond the
504 // the end
505 ui8 e_val[513];
506 ui8 cx_val[513];
507 ui8* lep = e_val; lep[0] = 0;
508 ui8* lcxp = cx_val; lcxp[0] = 0;
509
510 //initial row of quads
511 int e_qmax[2] = {0,0}, e_q[8] = {0,0,0,0,0,0,0,0};
512 int rho[2] = {0,0};
513 int c_q0 = 0;
514 ui32 s[8] = {0,0,0,0,0,0,0,0}, val, t;
515 ui32 y = 0;
516 ui32 *sp = buf;
517 for (ui32 x = 0; x < width; x += 4)
518 {
519 //prepare two quads
520 t = sp[0];
521 val = t + t; //multiply by 2 and get rid of sign
522 val >>= p; // 2 \mu_p + x
523 val &= ~1u; // 2 \mu_p
524 if (val)
525 {
526 rho[0] = 1;
527 e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
528 e_qmax[0] = e_q[0];
529 s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
530 }
531
532 t = height > 1 ? sp[stride] : 0;
533 ++sp;
534 val = t + t; //multiply by 2 and get rid of sign
535 val >>= p; // 2 \mu_p + x
536 val &= ~1u;// 2 \mu_p
537 if (val)
538 {
539 rho[0] += 2;
540 e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
541 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
542 s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
543 }
544
545 if (x+1 < width)
546 {
547 t = sp[0];
548 val = t + t; //multiply by 2 and get rid of sign
549 val >>= p; // 2 \mu_p + x
550 val &= ~1u;// 2 \mu_p
551 if (val)
552 {
553 rho[0] += 4;
554 e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
555 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
556 s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
557 }
558
559 t = height > 1 ? sp[stride] : 0;
560 ++sp;
561 val = t + t; //multiply by 2 and get rid of sign
562 val >>= p; // 2 \mu_p + x
563 val &= ~1u;// 2 \mu_p
564 if (val)
565 {
566 rho[0] += 8;
567 e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
568 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
569 s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
570 }
571 }
572
573 int Uq0 = ojph_max(e_qmax[0], 1); //kappa_q = 1
574 int u_q0 = Uq0 - 1, u_q1 = 0; //kappa_q = 1
575
576 int eps0 = 0;
577 if (u_q0 > 0)
578 {
579 eps0 |= (e_q[0] == e_qmax[0]);
580 eps0 |= (e_q[1] == e_qmax[0]) << 1;
581 eps0 |= (e_q[2] == e_qmax[0]) << 2;
582 eps0 |= (e_q[3] == e_qmax[0]) << 3;
583 }
584 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
585 lep[0] = (ui8)e_q[3];
586 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
587 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
588
589 ui16 tuple0 = vlc_tbl0[(c_q0 << 8) + (rho[0] << 4) + eps0];
590 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
591
592 if (c_q0 == 0)
593 mel_encode(&mel, rho[0] != 0);
594
595 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
596 ms_encode(&ms, s[0] & ((1U<<m)-1), m);
597 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
598 ms_encode(&ms, s[1] & ((1U<<m)-1), m);
599 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
600 ms_encode(&ms, s[2] & ((1U<<m)-1), m);
601 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
602 ms_encode(&ms, s[3] & ((1U<<m)-1), m);
603
604 if (x+2 < width)
605 {
606 t = sp[0];
607 val = t + t; //multiply by 2 and get rid of sign
608 val >>= p; // 2 \mu_p + x
609 val &= ~1u;// 2 \mu_p
610 if (val)
611 {
612 rho[1] = 1;
613 e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
614 e_qmax[1] = e_q[4];
615 s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
616 }
617
618 t = height > 1 ? sp[stride] : 0;
619 ++sp;
620 val = t + t; //multiply by 2 and get rid of sign
621 val >>= p; // 2 \mu_p + x
622 val &= ~1u;// 2 \mu_p
623 if (val)
624 {
625 rho[1] += 2;
626 e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
627 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
628 s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
629 }
630
631 if (x+3 < width)
632 {
633 t = sp[0];
634 val = t + t; //multiply by 2 and get rid of sign
635 val >>= p; // 2 \mu_p + x
636 val &= ~1u;// 2 \mu_p
637 if (val)
638 {
639 rho[1] += 4;
640 e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
641 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
642 s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
643 }
644
645 t = height > 1 ? sp[stride] : 0;
646 ++sp;
647 val = t + t; //multiply by 2 and get rid of sign
648 val >>= p; // 2 \mu_p + x
649 val &= ~1u;// 2 \mu_p
650 if (val)
651 {
652 rho[1] += 8;
653 e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
654 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
655 s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
656 }
657 }
658
659 int c_q1 = (rho[0] >> 1) | (rho[0] & 1);
660 int Uq1 = ojph_max(e_qmax[1], 1); //kappa_q = 1
661 u_q1 = Uq1 - 1; //kappa_q = 1
662
663 int eps1 = 0;
664 if (u_q1 > 0)
665 {
666 eps1 |= (e_q[4] == e_qmax[1]);
667 eps1 |= (e_q[5] == e_qmax[1]) << 1;
668 eps1 |= (e_q[6] == e_qmax[1]) << 2;
669 eps1 |= (e_q[7] == e_qmax[1]) << 3;
670 }
671 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
672 lep[0] = (ui8)e_q[7];
673 lcxp[0] |= (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
674 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
675 ui16 tuple1 = vlc_tbl0[(c_q1 << 8) + (rho[1] << 4) + eps1];
676 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
677
678 if (c_q1 == 0)
679 mel_encode(&mel, rho[1] != 0);
680
681 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
682 ms_encode(&ms, s[4] & ((1U<<m)-1), m);
683 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
684 ms_encode(&ms, s[5] & ((1U<<m)-1), m);
685 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
686 ms_encode(&ms, s[6] & ((1U<<m)-1), m);
687 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
688 ms_encode(&ms, s[7] & ((1U<<m)-1), m);
689 }
690
691 if (u_q0 > 0 && u_q1 > 0)
692 mel_encode(&mel, ojph_min(u_q0, u_q1) > 2);
693
694 if (u_q0 > 2 && u_q1 > 2)
695 {
696 vlc_encode(&vlc, ulvc_cwd_pre[u_q0-2], ulvc_cwd_pre_len[u_q0-2]);
697 vlc_encode(&vlc, ulvc_cwd_pre[u_q1-2], ulvc_cwd_pre_len[u_q1-2]);
698 vlc_encode(&vlc, ulvc_cwd_suf[u_q0-2], ulvc_cwd_suf_len[u_q0-2]);
699 vlc_encode(&vlc, ulvc_cwd_suf[u_q1-2], ulvc_cwd_suf_len[u_q1-2]);
700 }
701 else if (u_q0 > 2 && u_q1 > 0)
702 {
703 vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]);
704 vlc_encode(&vlc, u_q1 - 1, 1);
705 vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]);
706 }
707 else
708 {
709 vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]);
710 vlc_encode(&vlc, ulvc_cwd_pre[u_q1], ulvc_cwd_pre_len[u_q1]);
711 vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]);
712 vlc_encode(&vlc, ulvc_cwd_suf[u_q1], ulvc_cwd_suf_len[u_q1]);
713 }
714
715 //prepare for next iteration
716 c_q0 = (rho[1] >> 1) | (rho[1] & 1);
717 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
718 e_q[0]=e_q[1]=e_q[2]=e_q[3]=e_q[4]=e_q[5]=e_q[6]=e_q[7]=0;
719 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
720 }
721
722 lep[1] = 0;
723
724 for (y = 2; y < height; y += 2)
725 {
726 lep = e_val;
727 int max_e = ojph_max(lep[0], lep[1]) - 1;
728 lep[0] = 0;
729 lcxp = cx_val;
730 c_q0 = lcxp[0] + (lcxp[1] << 2);
731 lcxp[0] = 0;
732
733 sp = buf + y * stride;
734 for (ui32 x = 0; x < width; x += 4)
735 {
736 //prepare two quads
737 t = sp[0];
738 val = t + t; //multiply by 2 and get rid of sign
739 val >>= p; // 2 \mu_p + x
740 val &= ~1u;// 2 \mu_p
741 if (val)
742 {
743 rho[0] = 1;
744 e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
745 e_qmax[0] = e_q[0];
746 s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
747 }
748
749 t = y + 1 < height ? sp[stride] : 0;
750 ++sp;
751 val = t + t; //multiply by 2 and get rid of sign
752 val >>= p; // 2 \mu_p + x
753 val &= ~1u;// 2 \mu_p
754 if (val)
755 {
756 rho[0] += 2;
757 e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
758 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
759 s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
760 }
761
762 if (x+1 < width)
763 {
764 t = sp[0];
765 val = t + t; //multiply by 2 and get rid of sign
766 val >>= p; // 2 \mu_p + x
767 val &= ~1u;// 2 \mu_p
768 if (val)
769 {
770 rho[0] += 4;
771 e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
772 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
773 s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
774 }
775
776 t = y + 1 < height ? sp[stride] : 0;
777 ++sp;
778 val = t + t; //multiply by 2 and get rid of sign
779 val >>= p; // 2 \mu_p + x
780 val &= ~1u;// 2 \mu_p
781 if (val)
782 {
783 rho[0] += 8;
784 e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
785 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
786 s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
787 }
788 }
789
790 int kappa = (rho[0] & (rho[0]-1)) ? ojph_max(1,max_e) : 1;
791 int Uq0 = ojph_max(e_qmax[0], kappa);
792 int u_q0 = Uq0 - kappa, u_q1 = 0;
793
794 int eps0 = 0;
795 if (u_q0 > 0)
796 {
797 eps0 |= (e_q[0] == e_qmax[0]);
798 eps0 |= (e_q[1] == e_qmax[0]) << 1;
799 eps0 |= (e_q[2] == e_qmax[0]) << 2;
800 eps0 |= (e_q[3] == e_qmax[0]) << 3;
801 }
802 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
803 max_e = ojph_max(lep[0], lep[1]) - 1;
804 lep[0] = (ui8)e_q[3];
805 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
806 int c_q1 = lcxp[0] + (lcxp[1] << 2);
807 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
808 ui16 tuple0 = vlc_tbl1[(c_q0 << 8) + (rho[0] << 4) + eps0];
809 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
810
811 if (c_q0 == 0)
812 mel_encode(&mel, rho[0] != 0);
813
814 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
815 ms_encode(&ms, s[0] & ((1U<<m)-1), m);
816 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
817 ms_encode(&ms, s[1] & ((1U<<m)-1), m);
818 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
819 ms_encode(&ms, s[2] & ((1U<<m)-1), m);
820 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
821 ms_encode(&ms, s[3] & ((1U<<m)-1), m);
822
823 if (x+2 < width)
824 {
825 t = sp[0];
826 val = t + t; //multiply by 2 and get rid of sign
827 val >>= p; // 2 \mu_p + x
828 val &= ~1u;// 2 \mu_p
829 if (val)
830 {
831 rho[1] = 1;
832 e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
833 e_qmax[1] = e_q[4];
834 s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
835 }
836
837 t = y + 1 < height ? sp[stride] : 0;
838 ++sp;
839 val = t + t; //multiply by 2 and get rid of sign
840 val >>= p; // 2 \mu_p + x
841 val &= ~1u;// 2 \mu_p
842 if (val)
843 {
844 rho[1] += 2;
845 e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
846 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
847 s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
848 }
849
850 if (x+3 < width)
851 {
852 t = sp[0];
853 val = t + t; //multiply by 2 and get rid of sign
854 val >>= p; // 2 \mu_p + x
855 val &= ~1u;// 2 \mu_p
856 if (val)
857 {
858 rho[1] += 4;
859 e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
860 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
861 s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
862 }
863
864 t = y + 1 < height ? sp[stride] : 0;
865 ++sp;
866 val = t + t; //multiply by 2 and get rid of sign
867 val >>= p; // 2 \mu_p + x
868 val &= ~1u;// 2 \mu_p
869 if (val)
870 {
871 rho[1] += 8;
872 e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
873 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
874 s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
875 }
876 }
877
878 kappa = (rho[1] & (rho[1]-1)) ? ojph_max(1,max_e) : 1;
879 c_q1 |= ((rho[0] & 4) >> 1) | ((rho[0] & 8) >> 2);
880 int Uq1 = ojph_max(e_qmax[1], kappa);
881 u_q1 = Uq1 - kappa;
882
883 int eps1 = 0;
884 if (u_q1 > 0)
885 {
886 eps1 |= (e_q[4] == e_qmax[1]);
887 eps1 |= (e_q[5] == e_qmax[1]) << 1;
888 eps1 |= (e_q[6] == e_qmax[1]) << 2;
889 eps1 |= (e_q[7] == e_qmax[1]) << 3;
890 }
891 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
892 max_e = ojph_max(lep[0], lep[1]) - 1;
893 lep[0] = (ui8)e_q[7];
894 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
895 c_q0 = lcxp[0] + (lcxp[1] << 2);
896 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
897 ui16 tuple1 = vlc_tbl1[(c_q1 << 8) + (rho[1] << 4) + eps1];
898 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
899
900 if (c_q1 == 0)
901 mel_encode(&mel, rho[1] != 0);
902
903 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
904 ms_encode(&ms, s[4] & ((1U<<m)-1), m);
905 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
906 ms_encode(&ms, s[5] & ((1U<<m)-1), m);
907 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
908 ms_encode(&ms, s[6] & ((1U<<m)-1), m);
909 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
910 ms_encode(&ms, s[7] & ((1U<<m)-1), m);
911 }
912
913 vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]);
914 vlc_encode(&vlc, ulvc_cwd_pre[u_q1], ulvc_cwd_pre_len[u_q1]);
915 vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]);
916 vlc_encode(&vlc, ulvc_cwd_suf[u_q1], ulvc_cwd_suf_len[u_q1]);
917
918 //prepare for next iteration
919 c_q0 |= ((rho[1] & 4) >> 1) | ((rho[1] & 8) >> 2);
920 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
921 e_q[0]=e_q[1]=e_q[2]=e_q[3]=e_q[4]=e_q[5]=e_q[6]=e_q[7]=0;
922 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
923 }
924 }
925
926
927 terminate_mel_vlc(&mel, &vlc);
928 ms_terminate(&ms);
929
930 //copy to elastic
931 lengths[0] = mel.pos + vlc.pos + ms.pos;
932 elastic->get_buffer(mel.pos + vlc.pos + ms.pos, coded);
933 memcpy(coded->buf, ms.buf, ms.pos);
934 memcpy(coded->buf + ms.pos, mel.buf, mel.pos);
935 memcpy(coded->buf + ms.pos + mel.pos, vlc.buf - vlc.pos + 1, vlc.pos);
936
937 // put in the interface locator word
938 ui32 num_bytes = mel.pos + vlc.pos;
939 coded->buf[lengths[0]-1] = (ui8)(num_bytes >> 4);
940 coded->buf[lengths[0]-2] = coded->buf[lengths[0]-2] & 0xF0;
941 coded->buf[lengths[0]-2] =
942 (ui8)(coded->buf[lengths[0]-2] | (num_bytes & 0xF));
943
944 coded->avail_size -= lengths[0];
945 }
946 }
947}
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition: ojph_mem.cpp:95
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.
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
static void ms_terminate(ms_struct *msp)
static int ulvc_cwd_suf[33]
static int ulvc_cwd_suf_len[33]
static void vlc_encode(vlc_struct *vlcp, int cwd, int cwd_len)
static void terminate_mel_vlc(mel_struct *melp, vlc_struct *vlcp)
static void mel_init(dec_mel_st *melp, ui8 *bbuf, int lcup, int scup)
Initiates a dec_mel_st structure for MEL decoding and reads some bytes in order to get the read addre...
static void ms_init(ms_struct *msp, ui32 buffer_size, ui8 *data)
static void ms_encode(ms_struct *msp, ui32 cwd, int cwd_len)
static int ulvc_cwd_pre_len[33]
static int ulvc_cwd_pre[33]
static void mel_encode(mel_struct *melp, bool bit)
static void mel_emit_bit(mel_struct *melp, int v)
void ojph_encode_codeblock(ui32 *buf, ui32 missing_msbs, ui32 num_passes, ui32 width, ui32 height, ui32 stride, ui32 *lengths, ojph::mem_elastic_allocator *elastic, ojph::coded_lists *&coded)
static void vlc_init(vlc_struct *vlcp, ui32 buffer_size, ui8 *data)
uint16_t ui16
Definition: ojph_defs.h:52
static ui32 population_count(ui32 val)
Definition: ojph_arch.h:110
static ui32 count_leading_zeros(ui32 val)
Definition: ojph_arch.h:130
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50
#define ojph_max(a, b)
Definition: ojph_defs.h:73
#define ojph_min(a, b)
Definition: ojph_defs.h:76
#define OJPH_ERROR(t,...)
Definition: ojph_message.h:131