OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
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 const int num_uvlc_entries = 75;
73
75 static bool vlc_init_tables()
76 {
77 struct vlc_src_table { int c_q, rho, u_off, e_k, e_1, cwd, cwd_len; };
78 vlc_src_table tbl0[] = {
79 #include "table0.h"
80 };
81 size_t tbl0_size = sizeof(tbl0) / sizeof(vlc_src_table);
82
83 si32 pattern_popcnt[16];
84 for (ui32 i = 0; i < 16; ++i)
85 pattern_popcnt[i] = (si32)population_count(i);
86
87 vlc_src_table* src_tbl = tbl0;
88 ui16 *tgt_tbl = vlc_tbl0;
89 size_t tbl_size = tbl0_size;
90 for (int i = 0; i < 2048; ++i)
91 {
92 int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF;
93 if (((emb & rho) != emb) || (rho == 0 && c_q == 0))
94 tgt_tbl[i] = 0;
95 else
96 {
97 vlc_src_table *best_entry = NULL;
98 if (emb) // u_off = 1
99 {
100 int best_e_k = -1;
101 for (size_t j = 0; j < tbl_size; ++j)
102 {
103 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
104 if (src_tbl[j].u_off == 1)
105 if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1)
106 {
107 //now we need to find the smallest cwd with the highest
108 // number of bits set in e_k
109 int ones_count = pattern_popcnt[src_tbl[j].e_k];
110 if (ones_count >= best_e_k)
111 {
112 best_entry = src_tbl + j;
113 best_e_k = ones_count;
114 }
115 }
116 }
117 }
118 else // u_off = 0
119 {
120 for (size_t j = 0; j < tbl_size; ++j)
121 {
122 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
123 if (src_tbl[j].u_off == 0)
124 {
125 best_entry = src_tbl + j;
126 break;
127 }
128 }
129 }
130 assert(best_entry);
131 tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4)
132 + best_entry->e_k);
133 }
134 }
135
136 vlc_src_table tbl1[] = {
137 #include "table1.h"
138 };
139 size_t tbl1_size = sizeof(tbl1) / sizeof(vlc_src_table);
140
141 src_tbl = tbl1;
142 tgt_tbl = vlc_tbl1;
143 tbl_size = tbl1_size;
144 for (int i = 0; i < 2048; ++i)
145 {
146 int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF;
147 if (((emb & rho) != emb) || (rho == 0 && c_q == 0))
148 tgt_tbl[i] = 0;
149 else
150 {
151 vlc_src_table *best_entry = NULL;
152 if (emb) // u_off = 1
153 {
154 int best_e_k = -1;
155 for (size_t j = 0; j < tbl_size; ++j)
156 {
157 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
158 if (src_tbl[j].u_off == 1)
159 if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1)
160 {
161 //now we need to find the smallest cwd with the highest
162 // number of bits set in e_k
163 int ones_count = pattern_popcnt[src_tbl[j].e_k];
164 if (ones_count >= best_e_k)
165 {
166 best_entry = src_tbl + j;
167 best_e_k = ones_count;
168 }
169 }
170 }
171 }
172 else // u_off = 0
173 {
174 for (size_t j = 0; j < tbl_size; ++j)
175 {
176 if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho)
177 if (src_tbl[j].u_off == 0)
178 {
179 best_entry = src_tbl + j;
180 break;
181 }
182 }
183 }
184 assert(best_entry);
185 tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4)
186 + best_entry->e_k);
187 }
188 }
189
190
191 return true;
192 }
193
195 static bool uvlc_init_tables()
196 {
197 //code goes from 0 to 31, extension and 32 are not supported here
198 uvlc_tbl[0].pre = 0;
199 uvlc_tbl[0].pre_len = 0;
200 uvlc_tbl[0].suf = 0;
201 uvlc_tbl[0].suf_len = 0;
202 uvlc_tbl[0].ext = 0;
203 uvlc_tbl[0].ext_len = 0;
204
205 uvlc_tbl[1].pre = 1;
206 uvlc_tbl[1].pre_len = 1;
207 uvlc_tbl[1].suf = 0;
208 uvlc_tbl[1].suf_len = 0;
209 uvlc_tbl[1].ext = 0;
210 uvlc_tbl[1].ext_len = 0;
211
212 uvlc_tbl[2].pre = 2;
213 uvlc_tbl[2].pre_len = 2;
214 uvlc_tbl[2].suf = 0;
215 uvlc_tbl[2].suf_len = 0;
216 uvlc_tbl[2].ext = 0;
217 uvlc_tbl[2].ext_len = 0;
218
219 uvlc_tbl[3].pre = 4;
220 uvlc_tbl[3].pre_len = 3;
221 uvlc_tbl[3].suf = 0;
222 uvlc_tbl[3].suf_len = 1;
223 uvlc_tbl[3].ext = 0;
224 uvlc_tbl[3].ext_len = 0;
225
226 uvlc_tbl[4].pre = 4;
227 uvlc_tbl[4].pre_len = 3;
228 uvlc_tbl[4].suf = 1;
229 uvlc_tbl[4].suf_len = 1;
230 uvlc_tbl[4].ext = 0;
231 uvlc_tbl[4].ext_len = 0;
232
233 for (int i = 5; i < 33; ++i)
234 {
235 uvlc_tbl[i].pre = 0;
236 uvlc_tbl[i].pre_len = 3;
237 uvlc_tbl[i].suf = (ui8)(i - 5);
238 uvlc_tbl[i].suf_len = 5;
239 uvlc_tbl[i].ext = 0;
240 uvlc_tbl[i].ext_len = 0;
241 }
242
243 for (int i = 33; i < num_uvlc_entries; ++i)
244 {
245 uvlc_tbl[i].pre = 0;
246 uvlc_tbl[i].pre_len = 3;
247 uvlc_tbl[i].suf = (ui8)(28 + (i - 33) % 4);
248 uvlc_tbl[i].suf_len = 5;
249 uvlc_tbl[i].ext = (ui8)((i - 33) / 4);
250 uvlc_tbl[i].ext_len = 4;
251 }
252
253 return true;
254 }
255
257 static bool tables_initialized = false;
258
261 if (!tables_initialized) {
262 memset(vlc_tbl0, 0, 2048 * sizeof(ui16));
263 memset(vlc_tbl1, 0, 2048 * sizeof(ui16));
266 }
267 return tables_initialized;
268 }
269
271 //
273 struct mel_struct {
274 //storage
275 ui8* buf; //pointer to data buffer
276 ui32 pos; //position of next writing within buf
277 ui32 buf_size; //size of buffer, which we must not exceed
278
279 // all these can be replaced by bytes
280 int remaining_bits; //number of empty bits in tmp
281 int tmp; //temporary storage of coded bits
282 int run; //number of 0 run
283 int k; //state
284 int threshold; //threshold where one bit must be coded
285 };
286
288 static inline void
289 mel_init(mel_struct* melp, ui32 buffer_size, ui8* data)
290 {
291 melp->buf = data;
292 melp->pos = 0;
293 melp->buf_size = buffer_size;
294 melp->remaining_bits = 8;
295 melp->tmp = 0;
296 melp->run = 0;
297 melp->k = 0;
298 melp->threshold = 1; // this is 1 << mel_exp[melp->k];
299 }
300
302 static inline void
304 {
305 assert(v == 0 || v == 1);
306 melp->tmp = (melp->tmp << 1) + v;
307 melp->remaining_bits--;
308 if (melp->remaining_bits == 0)
309 {
310 if (melp->pos >= melp->buf_size)
311 OJPH_ERROR(0x00020001, "mel encoder's buffer is full");
312
313 melp->buf[melp->pos++] = (ui8)melp->tmp;
314 melp->remaining_bits = (melp->tmp == 0xFF ? 7 : 8);
315 melp->tmp = 0;
316 }
317 }
318
320 static inline void
321 mel_encode(mel_struct* melp, bool bit)
322 {
323 //MEL exponent
324 static const int mel_exp[13] = {0,0,0,1,1,1,2,2,2,3,3,4,5};
325
326 if (bit == false)
327 {
328 ++melp->run;
329 if (melp->run >= melp->threshold)
330 {
331 mel_emit_bit(melp, 1);
332 melp->run = 0;
333 melp->k = ojph_min(12, melp->k + 1);
334 melp->threshold = 1 << mel_exp[melp->k];
335 }
336 }
337 else
338 {
339 mel_emit_bit(melp, 0);
340 int t = mel_exp[melp->k];
341 while (t > 0)
342 mel_emit_bit(melp, (melp->run >> --t) & 1);
343 melp->run = 0;
344 melp->k = ojph_max(0, melp->k - 1);
345 melp->threshold = 1 << mel_exp[melp->k];
346 }
347 }
348
350 //
352 struct vlc_struct {
353 //storage
354 ui8* buf; //pointer to data buffer
355 ui32 pos; //position of next writing within buf
356 ui32 buf_size; //size of buffer, which we must not exceed
357
358 int used_bits; //number of occupied bits in tmp
359 int tmp; //temporary storage of coded bits
360 bool last_greater_than_8F; //true if last byte us greater than 0x8F
361 };
362
364 static inline void
365 vlc_init(vlc_struct* vlcp, ui32 buffer_size, ui8* data)
366 {
367 vlcp->buf = data + buffer_size - 1; //points to last byte
368 vlcp->pos = 1; //locations will be all -pos
369 vlcp->buf_size = buffer_size;
370
371 vlcp->buf[0] = 0xFF;
372 vlcp->used_bits = 4;
373 vlcp->tmp = 0xF;
374 vlcp->last_greater_than_8F = true;
375 }
376
378 static inline void
379 vlc_encode(vlc_struct* vlcp, int cwd, int cwd_len)
380 {
381 while (cwd_len > 0)
382 {
383 if (vlcp->pos >= vlcp->buf_size)
384 OJPH_ERROR(0x00020002, "vlc encoder's buffer is full");
385
386 int avail_bits = 8 - vlcp->last_greater_than_8F - vlcp->used_bits;
387 int t = ojph_min(avail_bits, cwd_len);
388 vlcp->tmp |= (cwd & ((1 << t) - 1)) << vlcp->used_bits;
389 vlcp->used_bits += t;
390 avail_bits -= t;
391 cwd_len -= t;
392 cwd >>= t;
393 if (avail_bits == 0)
394 {
395 if (vlcp->last_greater_than_8F && vlcp->tmp != 0x7F)
396 {
397 vlcp->last_greater_than_8F = false;
398 continue; //one empty bit remaining
399 }
400 *(vlcp->buf - vlcp->pos) = (ui8)(vlcp->tmp);
401 vlcp->pos++;
402 vlcp->last_greater_than_8F = vlcp->tmp > 0x8F;
403 vlcp->tmp = 0;
404 vlcp->used_bits = 0;
405 }
406 }
407 }
408
410 //
412 static inline void
414 {
415 if (melp->run > 0)
416 mel_emit_bit(melp, 1);
417
418 melp->tmp = melp->tmp << melp->remaining_bits;
419 int mel_mask = (0xFF << melp->remaining_bits) & 0xFF;
420 int vlc_mask = 0xFF >> (8 - vlcp->used_bits);
421 if ((mel_mask | vlc_mask) == 0)
422 return; //last mel byte cannot be 0xFF, since then
423 //melp->remaining_bits would be < 8
424 if (melp->pos >= melp->buf_size)
425 OJPH_ERROR(0x00020003, "mel encoder's buffer is full");
426 int fuse = melp->tmp | vlcp->tmp;
427 if ( ( ((fuse ^ melp->tmp) & mel_mask)
428 | ((fuse ^ vlcp->tmp) & vlc_mask) ) == 0
429 && (fuse != 0xFF) && vlcp->pos > 1)
430 {
431 melp->buf[melp->pos++] = (ui8)fuse;
432 }
433 else
434 {
435 if (vlcp->pos >= vlcp->buf_size)
436 OJPH_ERROR(0x00020004, "vlc encoder's buffer is full");
437 melp->buf[melp->pos++] = (ui8)melp->tmp; //melp->tmp cannot be 0xFF
438 *(vlcp->buf - vlcp->pos) = (ui8)vlcp->tmp;
439 vlcp->pos++;
440 }
441 }
442
444 //
446 struct ms_struct {
447 //storage
448 ui8* buf; //pointer to data buffer
449 ui32 pos; //position of next writing within buf
450 ui32 buf_size; //size of buffer, which we must not exceed
451
452 int max_bits; //maximum number of bits that can be store in tmp
453 int used_bits; //number of occupied bits in tmp
454 ui32 tmp; //temporary storage of coded bits
455 };
456
458 static inline void
459 ms_init(ms_struct* msp, ui32 buffer_size, ui8* data)
460 {
461 msp->buf = data;
462 msp->pos = 0;
463 msp->buf_size = buffer_size;
464 msp->max_bits = 8;
465 msp->used_bits = 0;
466 msp->tmp = 0;
467 }
468
470 static inline void
471 ms_encode(ms_struct* msp, ui32 cwd, int cwd_len)
472 {
473 while (cwd_len > 0)
474 {
475 if (msp->pos >= msp->buf_size)
476 OJPH_ERROR(0x00020005, "magnitude sign encoder's buffer is full");
477 int t = ojph_min(msp->max_bits - msp->used_bits, cwd_len);
478 msp->tmp |= (cwd & ((1U << t) - 1)) << msp->used_bits;
479 msp->used_bits += t;
480 cwd >>= t;
481 cwd_len -= t;
482 if (msp->used_bits >= msp->max_bits)
483 {
484 msp->buf[msp->pos++] = (ui8)msp->tmp;
485 msp->max_bits = (msp->tmp == 0xFF) ? 7 : 8;
486 msp->tmp = 0;
487 msp->used_bits = 0;
488 }
489 }
490 }
491
493 static inline void
494 ms_encode64(ms_struct* msp, ui64 cwd, int cwd_len)
495 {
496 while (cwd_len > 0)
497 {
498 if (msp->pos >= msp->buf_size)
499 OJPH_ERROR(0x00020005, "magnitude sign encoder's buffer is full");
500 int t = ojph_min(msp->max_bits - msp->used_bits, cwd_len);
501 msp->tmp |= (ui32)((cwd & ((1ULL << t) - 1)) << msp->used_bits);
502 msp->used_bits += t;
503 cwd >>= t;
504 cwd_len -= t;
505 if (msp->used_bits >= msp->max_bits)
506 {
507 msp->buf[msp->pos++] = (ui8)msp->tmp;
508 msp->max_bits = (msp->tmp == 0xFF) ? 7 : 8;
509 msp->tmp = 0;
510 msp->used_bits = 0;
511 }
512 }
513 }
514
516 static inline void
518 {
519 if (msp->used_bits)
520 {
521 int t = msp->max_bits - msp->used_bits; //unused bits
522 msp->tmp |= (0xFF & ((1U << t) - 1)) << msp->used_bits;
523 msp->used_bits += t;
524 if (msp->tmp != 0xFF)
525 {
526 if (msp->pos >= msp->buf_size)
527 OJPH_ERROR(0x00020006, "magnitude sign encoder's buffer is full");
528 msp->buf[msp->pos++] = (ui8)msp->tmp;
529 }
530 }
531 else if (msp->max_bits == 7)
532 msp->pos--;
533 }
534
536 //
537 //
538 //
539 //
540 //
542 void ojph_encode_codeblock32(ui32* buf, ui32 missing_msbs, ui32 num_passes,
543 ui32 width, ui32 height, ui32 stride,
544 ui32* lengths,
546 ojph::coded_lists *& coded)
547 {
548 assert(num_passes == 1);
549 (void)num_passes; //currently not used
550 const int ms_size = (16384*16+14)/15; //more than enough
551 ui8 ms_buf[ms_size];
552 const int mel_vlc_size = 3072; //more than enough
553 ui8 mel_vlc_buf[mel_vlc_size];
554 const int mel_size = 192;
555 ui8 *mel_buf = mel_vlc_buf;
556 const int vlc_size = mel_vlc_size - mel_size;
557 ui8 *vlc_buf = mel_vlc_buf + mel_size;
558
559 mel_struct mel;
560 mel_init(&mel, mel_size, mel_buf);
561 vlc_struct vlc;
562 vlc_init(&vlc, vlc_size, vlc_buf);
563 ms_struct ms;
564 ms_init(&ms, ms_size, ms_buf);
565
566 ui32 p = 30 - missing_msbs;
567
568 //e_val: E values for a line (these are the highest set bit)
569 //cx_val: is the context values
570 //Each byte stores the info for the 2 sample. For E, it is maximum
571 // of the two samples, while for cx, it is the OR of these two samples.
572 //The maximum is between the pixel at the bottom left of one quad
573 // and the bottom right of the earlier quad. The same is true for cx.
574 //For a 1024 pixels, we need 512 bytes, the 2 extra,
575 // one for the non-existing earlier quad, and one for beyond the
576 // the end
577 ui8 e_val[513];
578 ui8 cx_val[513];
579 ui8* lep = e_val; lep[0] = 0;
580 ui8* lcxp = cx_val; lcxp[0] = 0;
581
582 //initial row of quads
583 int e_qmax[2] = {0,0}, e_q[8] = {0,0,0,0,0,0,0,0};
584 int rho[2] = {0,0};
585 int c_q0 = 0;
586 ui32 s[8] = {0,0,0,0,0,0,0,0}, val, t;
587 ui32 y = 0;
588 ui32 *sp = buf;
589 for (ui32 x = 0; x < width; x += 4)
590 {
591 //prepare two quads
592 t = sp[0];
593 val = t + t; //multiply by 2 and get rid of sign
594 val >>= p; // 2 \mu_p + x
595 val &= ~1u; // 2 \mu_p
596 if (val)
597 {
598 rho[0] = 1;
599 e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
600 e_qmax[0] = e_q[0];
601 s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
602 }
603
604 t = height > 1 ? sp[stride] : 0;
605 ++sp;
606 val = t + t; //multiply by 2 and get rid of sign
607 val >>= p; // 2 \mu_p + x
608 val &= ~1u;// 2 \mu_p
609 if (val)
610 {
611 rho[0] += 2;
612 e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
613 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
614 s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
615 }
616
617 if (x+1 < width)
618 {
619 t = sp[0];
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[0] += 4;
626 e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
627 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
628 s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
629 }
630
631 t = height > 1 ? sp[stride] : 0;
632 ++sp;
633 val = t + t; //multiply by 2 and get rid of sign
634 val >>= p; // 2 \mu_p + x
635 val &= ~1u;// 2 \mu_p
636 if (val)
637 {
638 rho[0] += 8;
639 e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
640 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
641 s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
642 }
643 }
644
645 int Uq0 = ojph_max(e_qmax[0], 1); //kappa_q = 1
646 int u_q0 = Uq0 - 1, u_q1 = 0; //kappa_q = 1
647
648 int eps0 = 0;
649 if (u_q0 > 0)
650 {
651 eps0 |= (e_q[0] == e_qmax[0]);
652 eps0 |= (e_q[1] == e_qmax[0]) << 1;
653 eps0 |= (e_q[2] == e_qmax[0]) << 2;
654 eps0 |= (e_q[3] == e_qmax[0]) << 3;
655 }
656 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
657 lep[0] = (ui8)e_q[3];
658 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
659 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
660
661 ui16 tuple0 = vlc_tbl0[(c_q0 << 8) + (rho[0] << 4) + eps0];
662 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
663
664 if (c_q0 == 0)
665 mel_encode(&mel, rho[0] != 0);
666
667 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
668 ms_encode(&ms, s[0] & ((1U<<m)-1), m);
669 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
670 ms_encode(&ms, s[1] & ((1U<<m)-1), m);
671 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
672 ms_encode(&ms, s[2] & ((1U<<m)-1), m);
673 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
674 ms_encode(&ms, s[3] & ((1U<<m)-1), m);
675
676 if (x+2 < width)
677 {
678 t = sp[0];
679 val = t + t; //multiply by 2 and get rid of sign
680 val >>= p; // 2 \mu_p + x
681 val &= ~1u;// 2 \mu_p
682 if (val)
683 {
684 rho[1] = 1;
685 e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
686 e_qmax[1] = e_q[4];
687 s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
688 }
689
690 t = height > 1 ? sp[stride] : 0;
691 ++sp;
692 val = t + t; //multiply by 2 and get rid of sign
693 val >>= p; // 2 \mu_p + x
694 val &= ~1u;// 2 \mu_p
695 if (val)
696 {
697 rho[1] += 2;
698 e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
699 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
700 s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
701 }
702
703 if (x+3 < width)
704 {
705 t = sp[0];
706 val = t + t; //multiply by 2 and get rid of sign
707 val >>= p; // 2 \mu_p + x
708 val &= ~1u;// 2 \mu_p
709 if (val)
710 {
711 rho[1] += 4;
712 e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
713 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
714 s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
715 }
716
717 t = height > 1 ? sp[stride] : 0;
718 ++sp;
719 val = t + t; //multiply by 2 and get rid of sign
720 val >>= p; // 2 \mu_p + x
721 val &= ~1u;// 2 \mu_p
722 if (val)
723 {
724 rho[1] += 8;
725 e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
726 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
727 s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
728 }
729 }
730
731 int c_q1 = (rho[0] >> 1) | (rho[0] & 1);
732 int Uq1 = ojph_max(e_qmax[1], 1); //kappa_q = 1
733 u_q1 = Uq1 - 1; //kappa_q = 1
734
735 int eps1 = 0;
736 if (u_q1 > 0)
737 {
738 eps1 |= (e_q[4] == e_qmax[1]);
739 eps1 |= (e_q[5] == e_qmax[1]) << 1;
740 eps1 |= (e_q[6] == e_qmax[1]) << 2;
741 eps1 |= (e_q[7] == e_qmax[1]) << 3;
742 }
743 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
744 lep[0] = (ui8)e_q[7];
745 lcxp[0] |= (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
746 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
747 ui16 tuple1 = vlc_tbl0[(c_q1 << 8) + (rho[1] << 4) + eps1];
748 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
749
750 if (c_q1 == 0)
751 mel_encode(&mel, rho[1] != 0);
752
753 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
754 ms_encode(&ms, s[4] & ((1U<<m)-1), m);
755 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
756 ms_encode(&ms, s[5] & ((1U<<m)-1), m);
757 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
758 ms_encode(&ms, s[6] & ((1U<<m)-1), m);
759 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
760 ms_encode(&ms, s[7] & ((1U<<m)-1), m);
761 }
762
763 if (u_q0 > 0 && u_q1 > 0)
764 mel_encode(&mel, ojph_min(u_q0, u_q1) > 2);
765
766 if (u_q0 > 2 && u_q1 > 2)
767 {
768 vlc_encode(&vlc, uvlc_tbl[u_q0-2].pre, uvlc_tbl[u_q0-2].pre_len);
769 vlc_encode(&vlc, uvlc_tbl[u_q1-2].pre, uvlc_tbl[u_q1-2].pre_len);
770 vlc_encode(&vlc, uvlc_tbl[u_q0-2].suf, uvlc_tbl[u_q0-2].suf_len);
771 vlc_encode(&vlc, uvlc_tbl[u_q1-2].suf, uvlc_tbl[u_q1-2].suf_len);
772 }
773 else if (u_q0 > 2 && u_q1 > 0)
774 {
775 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
776 vlc_encode(&vlc, u_q1 - 1, 1);
777 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
778 }
779 else
780 {
781 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
782 vlc_encode(&vlc, uvlc_tbl[u_q1].pre, uvlc_tbl[u_q1].pre_len);
783 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
784 vlc_encode(&vlc, uvlc_tbl[u_q1].suf, uvlc_tbl[u_q1].suf_len);
785 }
786
787 //prepare for next iteration
788 c_q0 = (rho[1] >> 1) | (rho[1] & 1);
789 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
790 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;
791 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
792 }
793
794 lep[1] = 0;
795
796 for (y = 2; y < height; y += 2)
797 {
798 lep = e_val;
799 int max_e = ojph_max(lep[0], lep[1]) - 1;
800 lep[0] = 0;
801 lcxp = cx_val;
802 c_q0 = lcxp[0] + (lcxp[1] << 2);
803 lcxp[0] = 0;
804
805 sp = buf + y * stride;
806 for (ui32 x = 0; x < width; x += 4)
807 {
808 //prepare two quads
809 t = sp[0];
810 val = t + t; //multiply by 2 and get rid of sign
811 val >>= p; // 2 \mu_p + x
812 val &= ~1u;// 2 \mu_p
813 if (val)
814 {
815 rho[0] = 1;
816 e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
817 e_qmax[0] = e_q[0];
818 s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
819 }
820
821 t = y + 1 < height ? sp[stride] : 0;
822 ++sp;
823 val = t + t; //multiply by 2 and get rid of sign
824 val >>= p; // 2 \mu_p + x
825 val &= ~1u;// 2 \mu_p
826 if (val)
827 {
828 rho[0] += 2;
829 e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
830 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
831 s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
832 }
833
834 if (x+1 < width)
835 {
836 t = sp[0];
837 val = t + t; //multiply by 2 and get rid of sign
838 val >>= p; // 2 \mu_p + x
839 val &= ~1u;// 2 \mu_p
840 if (val)
841 {
842 rho[0] += 4;
843 e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
844 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
845 s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
846 }
847
848 t = y + 1 < height ? sp[stride] : 0;
849 ++sp;
850 val = t + t; //multiply by 2 and get rid of sign
851 val >>= p; // 2 \mu_p + x
852 val &= ~1u;// 2 \mu_p
853 if (val)
854 {
855 rho[0] += 8;
856 e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
857 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
858 s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
859 }
860 }
861
862 int kappa = (rho[0] & (rho[0]-1)) ? ojph_max(1,max_e) : 1;
863 int Uq0 = ojph_max(e_qmax[0], kappa);
864 int u_q0 = Uq0 - kappa, u_q1 = 0;
865
866 int eps0 = 0;
867 if (u_q0 > 0)
868 {
869 eps0 |= (e_q[0] == e_qmax[0]);
870 eps0 |= (e_q[1] == e_qmax[0]) << 1;
871 eps0 |= (e_q[2] == e_qmax[0]) << 2;
872 eps0 |= (e_q[3] == e_qmax[0]) << 3;
873 }
874 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
875 max_e = ojph_max(lep[0], lep[1]) - 1;
876 lep[0] = (ui8)e_q[3];
877 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
878 int c_q1 = lcxp[0] + (lcxp[1] << 2);
879 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
880 ui16 tuple0 = vlc_tbl1[(c_q0 << 8) + (rho[0] << 4) + eps0];
881 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
882
883 if (c_q0 == 0)
884 mel_encode(&mel, rho[0] != 0);
885
886 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
887 ms_encode(&ms, s[0] & ((1U<<m)-1), m);
888 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
889 ms_encode(&ms, s[1] & ((1U<<m)-1), m);
890 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
891 ms_encode(&ms, s[2] & ((1U<<m)-1), m);
892 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
893 ms_encode(&ms, s[3] & ((1U<<m)-1), m);
894
895 if (x+2 < width)
896 {
897 t = sp[0];
898 val = t + t; //multiply by 2 and get rid of sign
899 val >>= p; // 2 \mu_p + x
900 val &= ~1u;// 2 \mu_p
901 if (val)
902 {
903 rho[1] = 1;
904 e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
905 e_qmax[1] = e_q[4];
906 s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
907 }
908
909 t = y + 1 < height ? sp[stride] : 0;
910 ++sp;
911 val = t + t; //multiply by 2 and get rid of sign
912 val >>= p; // 2 \mu_p + x
913 val &= ~1u;// 2 \mu_p
914 if (val)
915 {
916 rho[1] += 2;
917 e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
918 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
919 s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
920 }
921
922 if (x+3 < width)
923 {
924 t = sp[0];
925 val = t + t; //multiply by 2 and get rid of sign
926 val >>= p; // 2 \mu_p + x
927 val &= ~1u;// 2 \mu_p
928 if (val)
929 {
930 rho[1] += 4;
931 e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
932 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
933 s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
934 }
935
936 t = y + 1 < height ? sp[stride] : 0;
937 ++sp;
938 val = t + t; //multiply by 2 and get rid of sign
939 val >>= p; // 2 \mu_p + x
940 val &= ~1u;// 2 \mu_p
941 if (val)
942 {
943 rho[1] += 8;
944 e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1
945 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
946 s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n
947 }
948 }
949
950 kappa = (rho[1] & (rho[1]-1)) ? ojph_max(1,max_e) : 1;
951 c_q1 |= ((rho[0] & 4) >> 1) | ((rho[0] & 8) >> 2);
952 int Uq1 = ojph_max(e_qmax[1], kappa);
953 u_q1 = Uq1 - kappa;
954
955 int eps1 = 0;
956 if (u_q1 > 0)
957 {
958 eps1 |= (e_q[4] == e_qmax[1]);
959 eps1 |= (e_q[5] == e_qmax[1]) << 1;
960 eps1 |= (e_q[6] == e_qmax[1]) << 2;
961 eps1 |= (e_q[7] == e_qmax[1]) << 3;
962 }
963 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
964 max_e = ojph_max(lep[0], lep[1]) - 1;
965 lep[0] = (ui8)e_q[7];
966 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
967 c_q0 = lcxp[0] + (lcxp[1] << 2);
968 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
969 ui16 tuple1 = vlc_tbl1[(c_q1 << 8) + (rho[1] << 4) + eps1];
970 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
971
972 if (c_q1 == 0)
973 mel_encode(&mel, rho[1] != 0);
974
975 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
976 ms_encode(&ms, s[4] & ((1U<<m)-1), m);
977 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
978 ms_encode(&ms, s[5] & ((1U<<m)-1), m);
979 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
980 ms_encode(&ms, s[6] & ((1U<<m)-1), m);
981 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
982 ms_encode(&ms, s[7] & ((1U<<m)-1), m);
983 }
984
985 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
986 vlc_encode(&vlc, uvlc_tbl[u_q1].pre, uvlc_tbl[u_q1].pre_len);
987 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
988 vlc_encode(&vlc, uvlc_tbl[u_q1].suf, uvlc_tbl[u_q1].suf_len);
989
990 //prepare for next iteration
991 c_q0 |= ((rho[1] & 4) >> 1) | ((rho[1] & 8) >> 2);
992 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
993 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;
994 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
995 }
996 }
997
998
999 terminate_mel_vlc(&mel, &vlc);
1000 ms_terminate(&ms);
1001
1002 //copy to elastic
1003 lengths[0] = mel.pos + vlc.pos + ms.pos;
1004 elastic->get_buffer(mel.pos + vlc.pos + ms.pos, coded);
1005 memcpy(coded->buf, ms.buf, ms.pos);
1006 memcpy(coded->buf + ms.pos, mel.buf, mel.pos);
1007 memcpy(coded->buf + ms.pos + mel.pos, vlc.buf - vlc.pos + 1, vlc.pos);
1008
1009 // put in the interface locator word
1010 ui32 num_bytes = mel.pos + vlc.pos;
1011 coded->buf[lengths[0]-1] = (ui8)(num_bytes >> 4);
1012 coded->buf[lengths[0]-2] = coded->buf[lengths[0]-2] & 0xF0;
1013 coded->buf[lengths[0]-2] =
1014 (ui8)(coded->buf[lengths[0]-2] | (num_bytes & 0xF));
1015
1016 coded->avail_size -= lengths[0];
1017 }
1018
1020 //
1021 //
1022 //
1023 //
1024 //
1026 void ojph_encode_codeblock64(ui64* buf, ui32 missing_msbs, ui32 num_passes,
1027 ui32 width, ui32 height, ui32 stride,
1028 ui32* lengths,
1030 ojph::coded_lists *& coded)
1031 {
1032 assert(num_passes == 1);
1033 (void)num_passes; //currently not used
1034 // 38 bits/sample + 1 color + 4 wavelet = 43 bits per sample.
1035 // * 4096 samples / 8 bits per byte = 22016; then rounded up to the
1036 // nearest 1 kB, givin 22528. This expanded further to take into
1037 // consideration stuffing at a max rate of 16 bits per 15 bits
1038 // (1 bit for every 15 bits of data); in reality, it is much smaller
1039 // than this.
1040 const int ms_size = (22528 * 16 + 14) / 15; //more than enough
1041 ui8 ms_buf[ms_size];
1042 // For each quad, we need at most, 7 bits for VLC and 12 bits for UVLC.
1043 // So we have 1024 quads * 19 / 8, which is 2432. This must be
1044 // multiplied by 16 / 15 to accommodate stuffing.
1045 // The mel is at most around 1 bit/quad, giving around 128 byte -- in
1046 // practice there was on case where it got to 132 bytes. Even
1047 // accounting for stuffing, it is smaller than 192. Therefore,
1048 // 3072 is more than enough
1049 const int mel_vlc_size = 3072; //more than enough
1050 ui8 mel_vlc_buf[mel_vlc_size];
1051 const int mel_size = 192;
1052 ui8 *mel_buf = mel_vlc_buf;
1053 const int vlc_size = mel_vlc_size - mel_size;
1054 ui8 *vlc_buf = mel_vlc_buf + mel_size;
1055
1056 mel_struct mel;
1057 mel_init(&mel, mel_size, mel_buf);
1058 vlc_struct vlc;
1059 vlc_init(&vlc, vlc_size, vlc_buf);
1060 ms_struct ms;
1061 ms_init(&ms, ms_size, ms_buf);
1062
1063 ui32 p = 62 - missing_msbs;
1064
1065 //e_val: E values for a line (these are the highest set bit)
1066 //cx_val: is the context values
1067 //Each byte stores the info for the 2 sample. For E, it is maximum
1068 // of the two samples, while for cx, it is the OR of these two samples.
1069 //The maximum is between the pixel at the bottom left of one quad
1070 // and the bottom right of the earlier quad. The same is true for cx.
1071 //For a 1024 pixels, we need 512 bytes, the 2 extra,
1072 // one for the non-existing earlier quad, and one for beyond the
1073 // the end
1074 ui8 e_val[513];
1075 ui8 cx_val[513];
1076 ui8* lep = e_val; lep[0] = 0;
1077 ui8* lcxp = cx_val; lcxp[0] = 0;
1078
1079 //initial row of quads
1080 int e_qmax[2] = {0,0}, e_q[8] = {0,0,0,0,0,0,0,0};
1081 int rho[2] = {0,0};
1082 int c_q0 = 0;
1083 ui64 s[8] = {0,0,0,0,0,0,0,0}, val, t;
1084 ui32 y = 0;
1085 ui64 *sp = buf;
1086 for (ui32 x = 0; x < width; x += 4)
1087 {
1088 //prepare two quads
1089 t = sp[0];
1090 val = t + t; //multiply by 2 and get rid of sign
1091 val >>= p; // 2 \mu_p + x
1092 val &= ~1ULL; // 2 \mu_p
1093 if (val)
1094 {
1095 rho[0] = 1;
1096 e_q[0] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1097 e_qmax[0] = e_q[0];
1098 s[0] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1099 }
1100
1101 t = height > 1 ? sp[stride] : 0;
1102 ++sp;
1103 val = t + t; //multiply by 2 and get rid of sign
1104 val >>= p; // 2 \mu_p + x
1105 val &= ~1ULL;// 2 \mu_p
1106 if (val)
1107 {
1108 rho[0] += 2;
1109 e_q[1] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1110 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
1111 s[1] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1112 }
1113
1114 if (x + 1 < width)
1115 {
1116 t = sp[0];
1117 val = t + t; //multiply by 2 and get rid of sign
1118 val >>= p; // 2 \mu_p + x
1119 val &= ~1ULL;// 2 \mu_p
1120 if (val)
1121 {
1122 rho[0] += 4;
1123 e_q[2] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1124 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
1125 s[2] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1126 }
1127
1128 t = height > 1 ? sp[stride] : 0;
1129 ++sp;
1130 val = t + t; //multiply by 2 and get rid of sign
1131 val >>= p; // 2 \mu_p + x
1132 val &= ~1ULL;// 2 \mu_p
1133 if (val)
1134 {
1135 rho[0] += 8;
1136 e_q[3] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1137 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
1138 s[3] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1139 }
1140 }
1141
1142 int Uq0 = ojph_max(e_qmax[0], 1); //kappa_q = 1
1143 int u_q0 = Uq0 - 1, u_q1 = 0; //kappa_q = 1
1144
1145 int eps0 = 0;
1146 if (u_q0 > 0)
1147 {
1148 eps0 |= (e_q[0] == e_qmax[0]);
1149 eps0 |= (e_q[1] == e_qmax[0]) << 1;
1150 eps0 |= (e_q[2] == e_qmax[0]) << 2;
1151 eps0 |= (e_q[3] == e_qmax[0]) << 3;
1152 }
1153 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
1154 lep[0] = (ui8)e_q[3];
1155 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
1156 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
1157
1158 ui16 tuple0 = vlc_tbl0[(c_q0 << 8) + (rho[0] << 4) + eps0];
1159 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
1160
1161 if (c_q0 == 0)
1162 mel_encode(&mel, rho[0] != 0);
1163
1164 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
1165 ms_encode64(&ms, s[0] & ((1ULL << m) - 1), m);
1166 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
1167 ms_encode64(&ms, s[1] & ((1ULL << m) - 1), m);
1168 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
1169 ms_encode64(&ms, s[2] & ((1ULL << m) - 1), m);
1170 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
1171 ms_encode64(&ms, s[3] & ((1ULL << m) - 1), m);
1172
1173 if (x + 2 < width)
1174 {
1175 t = sp[0];
1176 val = t + t; //multiply by 2 and get rid of sign
1177 val >>= p; // 2 \mu_p + x
1178 val &= ~1ULL;// 2 \mu_p
1179 if (val)
1180 {
1181 rho[1] = 1;
1182 e_q[4] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1183 e_qmax[1] = e_q[4];
1184 s[4] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1185 }
1186
1187 t = height > 1 ? sp[stride] : 0;
1188 ++sp;
1189 val = t + t; //multiply by 2 and get rid of sign
1190 val >>= p; // 2 \mu_p + x
1191 val &= ~1ULL;// 2 \mu_p
1192 if (val)
1193 {
1194 rho[1] += 2;
1195 e_q[5] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1196 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
1197 s[5] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1198 }
1199
1200 if (x + 3 < width)
1201 {
1202 t = sp[0];
1203 val = t + t; //multiply by 2 and get rid of sign
1204 val >>= p; // 2 \mu_p + x
1205 val &= ~1ULL;// 2 \mu_p
1206 if (val)
1207 {
1208 rho[1] += 4;
1209 e_q[6] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1210 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
1211 s[6] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1212 }
1213
1214 t = height > 1 ? sp[stride] : 0;
1215 ++sp;
1216 val = t + t; //multiply by 2 and get rid of sign
1217 val >>= p; // 2 \mu_p + x
1218 val &= ~1ULL;// 2 \mu_p
1219 if (val)
1220 {
1221 rho[1] += 8;
1222 e_q[7] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1223 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
1224 s[7] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1225 }
1226 }
1227
1228 int c_q1 = (rho[0] >> 1) | (rho[0] & 1);
1229 int Uq1 = ojph_max(e_qmax[1], 1); //kappa_q = 1
1230 u_q1 = Uq1 - 1; //kappa_q = 1
1231
1232 int eps1 = 0;
1233 if (u_q1 > 0)
1234 {
1235 eps1 |= (e_q[4] == e_qmax[1]);
1236 eps1 |= (e_q[5] == e_qmax[1]) << 1;
1237 eps1 |= (e_q[6] == e_qmax[1]) << 2;
1238 eps1 |= (e_q[7] == e_qmax[1]) << 3;
1239 }
1240 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
1241 lep[0] = (ui8)e_q[7];
1242 lcxp[0] |= (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
1243 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
1244 ui16 tuple1 = vlc_tbl0[(c_q1 << 8) + (rho[1] << 4) + eps1];
1245 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
1246
1247 if (c_q1 == 0)
1248 mel_encode(&mel, rho[1] != 0);
1249
1250 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
1251 ms_encode64(&ms, s[4] & ((1ULL << m) - 1), m);
1252 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
1253 ms_encode64(&ms, s[5] & ((1ULL << m) - 1), m);
1254 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
1255 ms_encode64(&ms, s[6] & ((1ULL << m) - 1), m);
1256 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
1257 ms_encode64(&ms, s[7] & ((1ULL << m) - 1), m);
1258 }
1259
1260 if (u_q0 > 0 && u_q1 > 0)
1261 mel_encode(&mel, ojph_min(u_q0, u_q1) > 2);
1262
1263 if (u_q0 > 2 && u_q1 > 2)
1264 {
1265 vlc_encode(&vlc, uvlc_tbl[u_q0-2].pre, uvlc_tbl[u_q0-2].pre_len);
1266 vlc_encode(&vlc, uvlc_tbl[u_q1-2].pre, uvlc_tbl[u_q1-2].pre_len);
1267 vlc_encode(&vlc, uvlc_tbl[u_q0-2].suf, uvlc_tbl[u_q0-2].suf_len);
1268 vlc_encode(&vlc, uvlc_tbl[u_q1-2].suf, uvlc_tbl[u_q1-2].suf_len);
1269 vlc_encode(&vlc, uvlc_tbl[u_q0-2].ext, uvlc_tbl[u_q0-2].ext_len);
1270 vlc_encode(&vlc, uvlc_tbl[u_q1-2].ext, uvlc_tbl[u_q1-2].ext_len);
1271 }
1272 else if (u_q0 > 2 && u_q1 > 0)
1273 {
1274 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
1275 vlc_encode(&vlc, u_q1 - 1, 1);
1276 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
1277 vlc_encode(&vlc, uvlc_tbl[u_q0].ext, uvlc_tbl[u_q0].ext_len);
1278 }
1279 else
1280 {
1281 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
1282 vlc_encode(&vlc, uvlc_tbl[u_q1].pre, uvlc_tbl[u_q1].pre_len);
1283 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
1284 vlc_encode(&vlc, uvlc_tbl[u_q1].suf, uvlc_tbl[u_q1].suf_len);
1285 vlc_encode(&vlc, uvlc_tbl[u_q0].ext, uvlc_tbl[u_q0].ext_len);
1286 vlc_encode(&vlc, uvlc_tbl[u_q1].ext, uvlc_tbl[u_q1].ext_len);
1287 }
1288
1289 //prepare for next iteration
1290 c_q0 = (rho[1] >> 1) | (rho[1] & 1);
1291 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
1292 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;
1293 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
1294 }
1295
1296 lep[1] = 0;
1297
1298 for (y = 2; y < height; y += 2)
1299 {
1300 lep = e_val;
1301 int max_e = ojph_max(lep[0], lep[1]) - 1;
1302 lep[0] = 0;
1303 lcxp = cx_val;
1304 c_q0 = lcxp[0] + (lcxp[1] << 2);
1305 lcxp[0] = 0;
1306
1307 sp = buf + y * stride;
1308 for (ui32 x = 0; x < width; x += 4)
1309 {
1310 //prepare two quads
1311 t = sp[0];
1312 val = t + t; //multiply by 2 and get rid of sign
1313 val >>= p; // 2 \mu_p + x
1314 val &= ~1ULL;// 2 \mu_p
1315 if (val)
1316 {
1317 rho[0] = 1;
1318 e_q[0] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1319 e_qmax[0] = e_q[0];
1320 s[0] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1321 }
1322
1323 t = y + 1 < height ? sp[stride] : 0;
1324 ++sp;
1325 val = t + t; //multiply by 2 and get rid of sign
1326 val >>= p; // 2 \mu_p + x
1327 val &= ~1ULL;// 2 \mu_p
1328 if (val)
1329 {
1330 rho[0] += 2;
1331 e_q[1] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1332 e_qmax[0] = ojph_max(e_qmax[0], e_q[1]);
1333 s[1] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1334 }
1335
1336 if (x + 1 < width)
1337 {
1338 t = sp[0];
1339 val = t + t; //multiply by 2 and get rid of sign
1340 val >>= p; // 2 \mu_p + x
1341 val &= ~1ULL;// 2 \mu_p
1342 if (val)
1343 {
1344 rho[0] += 4;
1345 e_q[2] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1346 e_qmax[0] = ojph_max(e_qmax[0], e_q[2]);
1347 s[2] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1348 }
1349
1350 t = y + 1 < height ? sp[stride] : 0;
1351 ++sp;
1352 val = t + t; //multiply by 2 and get rid of sign
1353 val >>= p; // 2 \mu_p + x
1354 val &= ~1ULL;// 2 \mu_p
1355 if (val)
1356 {
1357 rho[0] += 8;
1358 e_q[3] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1359 e_qmax[0] = ojph_max(e_qmax[0], e_q[3]);
1360 s[3] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1361 }
1362 }
1363
1364 int kappa = (rho[0] & (rho[0]-1)) ? ojph_max(1,max_e) : 1;
1365 int Uq0 = ojph_max(e_qmax[0], kappa);
1366 int u_q0 = Uq0 - kappa, u_q1 = 0;
1367
1368 int eps0 = 0;
1369 if (u_q0 > 0)
1370 {
1371 eps0 |= (e_q[0] == e_qmax[0]);
1372 eps0 |= (e_q[1] == e_qmax[0]) << 1;
1373 eps0 |= (e_q[2] == e_qmax[0]) << 2;
1374 eps0 |= (e_q[3] == e_qmax[0]) << 3;
1375 }
1376 lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++;
1377 max_e = ojph_max(lep[0], lep[1]) - 1;
1378 lep[0] = (ui8)e_q[3];
1379 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++;
1380 int c_q1 = lcxp[0] + (lcxp[1] << 2);
1381 lcxp[0] = (ui8)((rho[0] & 8) >> 3);
1382 ui16 tuple0 = vlc_tbl1[(c_q0 << 8) + (rho[0] << 4) + eps0];
1383 vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7);
1384
1385 if (c_q0 == 0)
1386 mel_encode(&mel, rho[0] != 0);
1387
1388 int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0;
1389 ms_encode64(&ms, s[0] & ((1ULL << m) - 1), m);
1390 m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0;
1391 ms_encode64(&ms, s[1] & ((1ULL << m) - 1), m);
1392 m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0;
1393 ms_encode64(&ms, s[2] & ((1ULL << m) - 1), m);
1394 m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0;
1395 ms_encode64(&ms, s[3] & ((1ULL << m) - 1), m);
1396
1397 if (x + 2 < width)
1398 {
1399 t = sp[0];
1400 val = t + t; //multiply by 2 and get rid of sign
1401 val >>= p; // 2 \mu_p + x
1402 val &= ~1ULL;// 2 \mu_p
1403 if (val)
1404 {
1405 rho[1] = 1;
1406 e_q[4] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1407 e_qmax[1] = e_q[4];
1408 s[4] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1409 }
1410
1411 t = y + 1 < height ? sp[stride] : 0;
1412 ++sp;
1413 val = t + t; //multiply by 2 and get rid of sign
1414 val >>= p; // 2 \mu_p + x
1415 val &= ~1ULL;// 2 \mu_p
1416 if (val)
1417 {
1418 rho[1] += 2;
1419 e_q[5] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1420 e_qmax[1] = ojph_max(e_qmax[1], e_q[5]);
1421 s[5] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1422 }
1423
1424 if (x + 3 < width)
1425 {
1426 t = sp[0];
1427 val = t + t; //multiply by 2 and get rid of sign
1428 val >>= p; // 2 \mu_p + x
1429 val &= ~1ULL;// 2 \mu_p
1430 if (val)
1431 {
1432 rho[1] += 4;
1433 e_q[6] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1434 e_qmax[1] = ojph_max(e_qmax[1], e_q[6]);
1435 s[6] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1436 }
1437
1438 t = y + 1 < height ? sp[stride] : 0;
1439 ++sp;
1440 val = t + t; //multiply by 2 and get rid of sign
1441 val >>= p; // 2 \mu_p + x
1442 val &= ~1ULL;// 2 \mu_p
1443 if (val)
1444 {
1445 rho[1] += 8;
1446 e_q[7] = 64 - (int)count_leading_zeros(--val); //2\mu_p - 1
1447 e_qmax[1] = ojph_max(e_qmax[1], e_q[7]);
1448 s[7] = --val + (t >> 63); //v_n = 2(\mu_p-1) + s_n
1449 }
1450 }
1451
1452 kappa = (rho[1] & (rho[1]-1)) ? ojph_max(1,max_e) : 1;
1453 c_q1 |= ((rho[0] & 4) >> 1) | ((rho[0] & 8) >> 2);
1454 int Uq1 = ojph_max(e_qmax[1], kappa);
1455 u_q1 = Uq1 - kappa;
1456
1457 int eps1 = 0;
1458 if (u_q1 > 0)
1459 {
1460 eps1 |= (e_q[4] == e_qmax[1]);
1461 eps1 |= (e_q[5] == e_qmax[1]) << 1;
1462 eps1 |= (e_q[6] == e_qmax[1]) << 2;
1463 eps1 |= (e_q[7] == e_qmax[1]) << 3;
1464 }
1465 lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++;
1466 max_e = ojph_max(lep[0], lep[1]) - 1;
1467 lep[0] = (ui8)e_q[7];
1468 lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++;
1469 c_q0 = lcxp[0] + (lcxp[1] << 2);
1470 lcxp[0] = (ui8)((rho[1] & 8) >> 3);
1471 ui16 tuple1 = vlc_tbl1[(c_q1 << 8) + (rho[1] << 4) + eps1];
1472 vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7);
1473
1474 if (c_q1 == 0)
1475 mel_encode(&mel, rho[1] != 0);
1476
1477 int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0;
1478 ms_encode64(&ms, s[4] & ((1ULL << m) - 1), m);
1479 m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0;
1480 ms_encode64(&ms, s[5] & ((1ULL << m) - 1), m);
1481 m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0;
1482 ms_encode64(&ms, s[6] & ((1ULL << m) - 1), m);
1483 m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0;
1484 ms_encode64(&ms, s[7] & ((1ULL << m) - 1), m);
1485 }
1486
1487 vlc_encode(&vlc, uvlc_tbl[u_q0].pre, uvlc_tbl[u_q0].pre_len);
1488 vlc_encode(&vlc, uvlc_tbl[u_q1].pre, uvlc_tbl[u_q1].pre_len);
1489 vlc_encode(&vlc, uvlc_tbl[u_q0].suf, uvlc_tbl[u_q0].suf_len);
1490 vlc_encode(&vlc, uvlc_tbl[u_q1].suf, uvlc_tbl[u_q1].suf_len);
1491 vlc_encode(&vlc, uvlc_tbl[u_q0].ext, uvlc_tbl[u_q0].ext_len);
1492 vlc_encode(&vlc, uvlc_tbl[u_q1].ext, uvlc_tbl[u_q1].ext_len);
1493
1494 //prepare for next iteration
1495 c_q0 |= ((rho[1] & 4) >> 1) | ((rho[1] & 8) >> 2);
1496 s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0;
1497 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;
1498 rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0;
1499 }
1500 }
1501
1502
1503 terminate_mel_vlc(&mel, &vlc);
1504 ms_terminate(&ms);
1505
1506 //copy to elastic
1507 lengths[0] = mel.pos + vlc.pos + ms.pos;
1508 elastic->get_buffer(mel.pos + vlc.pos + ms.pos, coded);
1509 memcpy(coded->buf, ms.buf, ms.pos);
1510 memcpy(coded->buf + ms.pos, mel.buf, mel.pos);
1511 memcpy(coded->buf + ms.pos + mel.pos, vlc.buf - vlc.pos + 1, vlc.pos);
1512
1513 // put in the interface locator word
1514 ui32 num_bytes = mel.pos + vlc.pos;
1515 coded->buf[lengths[0]-1] = (ui8)(num_bytes >> 4);
1516 coded->buf[lengths[0]-2] = coded->buf[lengths[0]-2] & 0xF0;
1517 coded->buf[lengths[0]-2] =
1518 (ui8)(coded->buf[lengths[0]-2] | (num_bytes & 0xF));
1519
1520 coded->avail_size -= lengths[0];
1521 }
1522 }
1523}
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:91
static bool uvlc_init_tables()
Initializes uvlc_tbl0 and uvlc_tbl1 tables.
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 void ms_encode64(ms_struct *msp, ui64 cwd, int cwd_len)
static void vlc_encode(vlc_struct *vlcp, int cwd, int cwd_len)
void ojph_encode_codeblock32(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)
void ojph_encode_codeblock64(ui64 *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 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 void mel_encode(mel_struct *melp, bool bit)
static void mel_emit_bit(mel_struct *melp, int v)
bool initialize_block_encoder_tables()
static bool tables_initialized
static void vlc_init(vlc_struct *vlcp, ui32 buffer_size, ui8 *data)
static uvlc_tbl_struct uvlc_tbl[num_uvlc_entries]
const int num_uvlc_entries
uint64_t ui64
Definition ojph_defs.h:56
uint16_t ui16
Definition ojph_defs.h:52
static ui32 population_count(ui32 val)
Definition ojph_arch.h:152
static ui32 count_leading_zeros(ui32 val)
Definition ojph_arch.h:173
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,...)