-
Notifications
You must be signed in to change notification settings - Fork 453
/
ht_dec.c
2640 lines (2337 loc) · 105 KB
/
ht_dec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2021, Aous Naman
// Copyright (c) 2021, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2021, The University of New South Wales, Australia
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***************************************************************************/
// This file is part of the OpenJpeg software implementation.
// File: ht_dec.c
// Author: Aous Naman
// Date: 01 September 2021
//***************************************************************************/
//***************************************************************************/
/** @file ht_dec.c
* @brief implements HTJ2K block decoder
*/
#include <assert.h>
#include <string.h>
#include "opj_includes.h"
#include "t1_ht_luts.h"
/////////////////////////////////////////////////////////////////////////////
// compiler detection
/////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#define OPJ_COMPILER_MSVC
#elif (defined __GNUC__)
#define OPJ_COMPILER_GNUC
#endif
//************************************************************************/
/** @brief Displays the error message for disabling the decoding of SPP and
* MRP passes
*/
static OPJ_BOOL only_cleanup_pass_is_decoded = OPJ_FALSE;
//************************************************************************/
/** @brief Generates population count (i.e., the number of set bits)
*
* @param [in] val is the value for which population count is sought
*/
static INLINE
OPJ_UINT32 population_count(OPJ_UINT32 val)
{
#if defined(OPJ_COMPILER_MSVC) && (defined(_M_IX86) || defined(_M_AMD64))
return (OPJ_UINT32)__popcnt(val);
#elif (defined OPJ_COMPILER_GNUC)
return (OPJ_UINT32)__builtin_popcount(val);
#else
val -= ((val >> 1) & 0x55555555);
val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
val = (((val >> 4) + val) & 0x0f0f0f0f);
val += (val >> 8);
val += (val >> 16);
return (OPJ_UINT32)(val & 0x0000003f);
#endif
}
//************************************************************************/
/** @brief Counts the number of leading zeros
*
* @param [in] val is the value for which leading zero count is sought
*/
#ifdef OPJ_COMPILER_MSVC
#pragma intrinsic(_BitScanReverse)
#endif
static INLINE
OPJ_UINT32 count_leading_zeros(OPJ_UINT32 val)
{
#ifdef OPJ_COMPILER_MSVC
unsigned long result = 0;
_BitScanReverse(&result, val);
return 31U ^ (OPJ_UINT32)result;
#elif (defined OPJ_COMPILER_GNUC)
return (OPJ_UINT32)__builtin_clz(val);
#else
val |= (val >> 1);
val |= (val >> 2);
val |= (val >> 4);
val |= (val >> 8);
val |= (val >> 16);
return 32U - population_count(val);
#endif
}
//************************************************************************/
/** @brief Read a little-endian serialized UINT32.
*
* @param [in] dataIn pointer to byte stream to read from
*/
static INLINE OPJ_UINT32 read_le_uint32(const void* dataIn)
{
#if defined(OPJ_BIG_ENDIAN)
const OPJ_UINT8* data = (const OPJ_UINT8*)dataIn;
return ((OPJ_UINT32)data[0]) | (OPJ_UINT32)(data[1] << 8) | (OPJ_UINT32)(
data[2] << 16) | (((
OPJ_UINT32)data[3]) <<
24U);
#else
return *(OPJ_UINT32*)dataIn;
#endif
}
//************************************************************************/
/** @brief MEL state structure for reading and decoding the MEL bitstream
*
* A number of events is decoded from the MEL bitstream ahead of time
* and stored in run/num_runs.
* Each run represents the number of zero events before a one event.
*/
typedef struct dec_mel {
// data decoding machinery
OPJ_UINT8* data; //!<the address of data (or bitstream)
OPJ_UINT64 tmp; //!<temporary buffer for read data
int bits; //!<number of bits stored in tmp
int size; //!<number of bytes in MEL code
OPJ_BOOL unstuff; //!<true if the next bit needs to be unstuffed
int k; //!<state of MEL decoder
// queue of decoded runs
int num_runs; //!<number of decoded runs left in runs (maximum 8)
OPJ_UINT64 runs; //!<runs of decoded MEL codewords (7 bits/run)
} dec_mel_t;
//************************************************************************/
/** @brief Reads and unstuffs the MEL bitstream
*
* This design needs more bytes in the codeblock buffer than the length
* of the cleanup pass by up to 2 bytes.
*
* Unstuffing removes the MSB of the byte following a byte whose
* value is 0xFF; this prevents sequences larger than 0xFF7F in value
* from appearing the bitstream.
*
* @param [in] melp is a pointer to dec_mel_t structure
*/
static INLINE
void mel_read(dec_mel_t *melp)
{
OPJ_UINT32 val;
int bits;
OPJ_UINT32 t;
OPJ_BOOL unstuff;
if (melp->bits > 32) { //there are enough bits in the tmp variable
return; // return without reading new data
}
val = 0xFFFFFFFF; // feed in 0xFF if buffer is exhausted
if (melp->size > 4) { // if there is more than 4 bytes the MEL segment
val = read_le_uint32(melp->data); // read 32 bits from MEL data
melp->data += 4; // advance pointer
melp->size -= 4; // reduce counter
} else if (melp->size > 0) { // 4 or less
OPJ_UINT32 m, v;
int i = 0;
while (melp->size > 1) {
OPJ_UINT32 v = *melp->data++; // read one byte at a time
OPJ_UINT32 m = ~(0xFFu << i); // mask of location
val = (val & m) | (v << i); // put byte in its correct location
--melp->size;
i += 8;
}
// size equal to 1
v = *melp->data++; // the one before the last is different
v |= 0xF; // MEL and VLC segments can overlap
m = ~(0xFFu << i);
val = (val & m) | (v << i);
--melp->size;
}
// next we unstuff them before adding them to the buffer
bits = 32 - melp->unstuff; // number of bits in val, subtract 1 if
// the previously read byte requires
// unstuffing
// data is unstuffed and accumulated in t
// bits has the number of bits in t
t = val & 0xFF;
unstuff = ((val & 0xFF) == 0xFF); // true if the byte needs unstuffing
bits -= unstuff; // there is one less bit in t if unstuffing is needed
t = t << (8 - unstuff); // move up to make room for the next byte
//this is a repeat of the above
t |= (val >> 8) & 0xFF;
unstuff = (((val >> 8) & 0xFF) == 0xFF);
bits -= unstuff;
t = t << (8 - unstuff);
t |= (val >> 16) & 0xFF;
unstuff = (((val >> 16) & 0xFF) == 0xFF);
bits -= unstuff;
t = t << (8 - unstuff);
t |= (val >> 24) & 0xFF;
melp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
// move t to tmp, and push the result all the way up, so we read from
// the MSB
melp->tmp |= ((OPJ_UINT64)t) << (64 - bits - melp->bits);
melp->bits += bits; //increment the number of bits in tmp
}
//************************************************************************/
/** @brief Decodes unstuffed MEL segment bits stored in tmp to runs
*
* Runs are stored in "runs" and the number of runs in "num_runs".
* Each run represents a number of zero events that may or may not
* terminate in a 1 event.
* Each run is stored in 7 bits. The LSB is 1 if the run terminates in
* a 1 event, 0 otherwise. The next 6 bits, for the case terminating
* with 1, contain the number of consecutive 0 zero events * 2; for the
* case terminating with 0, they store (number of consecutive 0 zero
* events - 1) * 2.
* A total of 6 bits (made up of 1 + 5) should have been enough.
*
* @param [in] melp is a pointer to dec_mel_t structure
*/
static INLINE
void mel_decode(dec_mel_t *melp)
{
static const int mel_exp[13] = { //MEL exponents
0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5
};
if (melp->bits < 6) { // if there are less than 6 bits in tmp
mel_read(melp); // then read from the MEL bitstream
}
// 6 bits is the largest decodable MEL cwd
//repeat so long that there is enough decodable bits in tmp,
// and the runs store is not full (num_runs < 8)
while (melp->bits >= 6 && melp->num_runs < 8) {
int eval = mel_exp[melp->k]; // number of bits associated with state
int run = 0;
if (melp->tmp & (1ull << 63)) { //The next bit to decode (stored in MSB)
//one is found
run = 1 << eval;
run--; // consecutive runs of 0 events - 1
melp->k = melp->k + 1 < 12 ? melp->k + 1 : 12;//increment, max is 12
melp->tmp <<= 1; // consume one bit from tmp
melp->bits -= 1;
run = run << 1; // a stretch of zeros not terminating in one
} else {
//0 is found
run = (int)(melp->tmp >> (63 - eval)) & ((1 << eval) - 1);
melp->k = melp->k - 1 > 0 ? melp->k - 1 : 0; //decrement, min is 0
melp->tmp <<= eval + 1; //consume eval + 1 bits (max is 6)
melp->bits -= eval + 1;
run = (run << 1) + 1; // a stretch of zeros terminating with one
}
eval = melp->num_runs * 7; // 7 bits per run
melp->runs &= ~((OPJ_UINT64)0x3F << eval); // 6 bits are sufficient
melp->runs |= ((OPJ_UINT64)run) << eval; // store the value in runs
melp->num_runs++; // increment count
}
}
//************************************************************************/
/** @brief Initiates a dec_mel_t structure for MEL decoding and reads
* some bytes in order to get the read address to a multiple
* of 4
*
* @param [in] melp is a pointer to dec_mel_t structure
* @param [in] bbuf is a pointer to byte buffer
* @param [in] lcup is the length of MagSgn+MEL+VLC segments
* @param [in] scup is the length of MEL+VLC segments
*/
static INLINE
void mel_init(dec_mel_t *melp, OPJ_UINT8* bbuf, int lcup, int scup)
{
int num;
int i;
melp->data = bbuf + lcup - scup; // move the pointer to the start of MEL
melp->bits = 0; // 0 bits in tmp
melp->tmp = 0; //
melp->unstuff = OPJ_FALSE; // no unstuffing
melp->size = scup - 1; // size is the length of MEL+VLC-1
melp->k = 0; // 0 for state
melp->num_runs = 0; // num_runs is 0
melp->runs = 0; //
//This code is borrowed; original is for a different architecture
//These few lines take care of the case where data is not at a multiple
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the MEL segment
num = 4 - (int)((intptr_t)(melp->data) & 0x3);
for (i = 0; i < num; ++i) { // this code is similar to mel_read
OPJ_UINT64 d;
int d_bits;
assert(melp->unstuff == OPJ_FALSE || melp->data[0] <= 0x8F);
d = (melp->size > 0) ? *melp->data : 0xFF; // if buffer is consumed
// set data to 0xFF
if (melp->size == 1) {
d |= 0xF; //if this is MEL+VLC-1, set LSBs to 0xF
}
// see the standard
melp->data += melp->size-- > 0; //increment if the end is not reached
d_bits = 8 - melp->unstuff; //if unstuffing is needed, reduce by 1
melp->tmp = (melp->tmp << d_bits) | d; //store bits in tmp
melp->bits += d_bits; //increment tmp by number of bits
melp->unstuff = ((d & 0xFF) == 0xFF); //true of next byte needs
//unstuffing
}
melp->tmp <<= (64 - melp->bits); //push all the way up so the first bit
// is the MSB
}
//************************************************************************/
/** @brief Retrieves one run from dec_mel_t; if there are no runs stored
* MEL segment is decoded
*
* @param [in] melp is a pointer to dec_mel_t structure
*/
static INLINE
int mel_get_run(dec_mel_t *melp)
{
int t;
if (melp->num_runs == 0) { //if no runs, decode more bit from MEL segment
mel_decode(melp);
}
t = melp->runs & 0x7F; //retrieve one run
melp->runs >>= 7; // remove the retrieved run
melp->num_runs--;
return t; // return run
}
//************************************************************************/
/** @brief A structure for reading and unstuffing a segment that grows
* backward, such as VLC and MRP
*/
typedef struct rev_struct {
//storage
OPJ_UINT8* data; //!<pointer to where to read data
OPJ_UINT64 tmp; //!<temporary buffer of read data
OPJ_UINT32 bits; //!<number of bits stored in tmp
int size; //!<number of bytes left
OPJ_BOOL unstuff; //!<true if the last byte is more than 0x8F
//!<then the current byte is unstuffed if it is 0x7F
} rev_struct_t;
//************************************************************************/
/** @brief Read and unstuff data from a backwardly-growing segment
*
* This reader can read up to 8 bytes from before the VLC segment.
* Care must be taken not read from unreadable memory, causing a
* segmentation fault.
*
* Note that there is another subroutine rev_read_mrp that is slightly
* different. The other one fills zeros when the buffer is exhausted.
* This one basically does not care if the bytes are consumed, because
* any extra data should not be used in the actual decoding.
*
* Unstuffing is needed to prevent sequences more than 0xFF8F from
* appearing in the bits stream; since we are reading backward, we keep
* watch when a value larger than 0x8F appears in the bitstream.
* If the byte following this is 0x7F, we unstuff this byte (ignore the
* MSB of that byte, which should be 0).
*
* @param [in] vlcp is a pointer to rev_struct_t structure
*/
static INLINE
void rev_read(rev_struct_t *vlcp)
{
OPJ_UINT32 val;
OPJ_UINT32 tmp;
OPJ_UINT32 bits;
OPJ_BOOL unstuff;
//process 4 bytes at a time
if (vlcp->bits > 32) { // if there are more than 32 bits in tmp, then
return; // reading 32 bits can overflow vlcp->tmp
}
val = 0;
//the next line (the if statement) needs to be tested first
if (vlcp->size > 3) { // if there are more than 3 bytes left in VLC
// (vlcp->data - 3) move pointer back to read 32 bits at once
val = read_le_uint32(vlcp->data - 3); // then read 32 bits
vlcp->data -= 4; // move data pointer back by 4
vlcp->size -= 4; // reduce available byte by 4
} else if (vlcp->size > 0) { // 4 or less
int i = 24;
while (vlcp->size > 0) {
OPJ_UINT32 v = *vlcp->data--; // read one byte at a time
val |= (v << i); // put byte in its correct location
--vlcp->size;
i -= 8;
}
}
//accumulate in tmp, number of bits in tmp are stored in bits
tmp = val >> 24; //start with the MSB byte
// test unstuff (previous byte is >0x8F), and this byte is 0x7F
bits = 8u - ((vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = (val >> 24) > 0x8F; //this is for the next byte
tmp |= ((val >> 16) & 0xFF) << bits; //process the next byte
bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = ((val >> 16) & 0xFF) > 0x8F;
tmp |= ((val >> 8) & 0xFF) << bits;
bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = ((val >> 8) & 0xFF) > 0x8F;
tmp |= (val & 0xFF) << bits;
bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = (val & 0xFF) > 0x8F;
// now move the read and unstuffed bits into vlcp->tmp
vlcp->tmp |= (OPJ_UINT64)tmp << vlcp->bits;
vlcp->bits += bits;
vlcp->unstuff = unstuff; // this for the next read
}
//************************************************************************/
/** @brief Initiates the rev_struct_t structure and reads a few bytes to
* move the read address to multiple of 4
*
* There is another similar rev_init_mrp subroutine. The difference is
* that this one, rev_init, discards the first 12 bits (they have the
* sum of the lengths of VLC and MEL segments), and first unstuff depends
* on first 4 bits.
*
* @param [in] vlcp is a pointer to rev_struct_t structure
* @param [in] data is a pointer to byte at the start of the cleanup pass
* @param [in] lcup is the length of MagSgn+MEL+VLC segments
* @param [in] scup is the length of MEL+VLC segments
*/
static INLINE
void rev_init(rev_struct_t *vlcp, OPJ_UINT8* data, int lcup, int scup)
{
OPJ_UINT32 d;
int num, tnum, i;
//first byte has only the upper 4 bits
vlcp->data = data + lcup - 2;
//size can not be larger than this, in fact it should be smaller
vlcp->size = scup - 2;
d = *vlcp->data--; // read one byte (this is a half byte)
vlcp->tmp = d >> 4; // both initialize and set
vlcp->bits = 4 - ((vlcp->tmp & 7) == 7); //check standard
vlcp->unstuff = (d | 0xF) > 0x8F; //this is useful for the next byte
//This code is designed for an architecture that read address should
// align to the read size (address multiple of 4 if read size is 4)
//These few lines take care of the case where data is not at a multiple
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the VLC bitstream.
// To read 32 bits, read from (vlcp->data - 3)
num = 1 + (int)((intptr_t)(vlcp->data) & 0x3);
tnum = num < vlcp->size ? num : vlcp->size;
for (i = 0; i < tnum; ++i) {
OPJ_UINT64 d;
OPJ_UINT32 d_bits;
d = *vlcp->data--; // read one byte and move read pointer
//check if the last byte was >0x8F (unstuff == true) and this is 0x7F
d_bits = 8u - ((vlcp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
vlcp->tmp |= d << vlcp->bits; // move data to vlcp->tmp
vlcp->bits += d_bits;
vlcp->unstuff = d > 0x8F; // for next byte
}
vlcp->size -= tnum;
rev_read(vlcp); // read another 32 buts
}
//************************************************************************/
/** @brief Retrieves 32 bits from the head of a rev_struct structure
*
* By the end of this call, vlcp->tmp must have no less than 33 bits
*
* @param [in] vlcp is a pointer to rev_struct structure
*/
static INLINE
OPJ_UINT32 rev_fetch(rev_struct_t *vlcp)
{
if (vlcp->bits < 32) { // if there are less then 32 bits, read more
rev_read(vlcp); // read 32 bits, but unstuffing might reduce this
if (vlcp->bits < 32) { // if there is still space in vlcp->tmp for 32 bits
rev_read(vlcp); // read another 32
}
}
return (OPJ_UINT32)vlcp->tmp; // return the head (bottom-most) of vlcp->tmp
}
//************************************************************************/
/** @brief Consumes num_bits from a rev_struct structure
*
* @param [in] vlcp is a pointer to rev_struct structure
* @param [in] num_bits is the number of bits to be removed
*/
static INLINE
OPJ_UINT32 rev_advance(rev_struct_t *vlcp, OPJ_UINT32 num_bits)
{
assert(num_bits <= vlcp->bits); // vlcp->tmp must have more than num_bits
vlcp->tmp >>= num_bits; // remove bits
vlcp->bits -= num_bits; // decrement the number of bits
return (OPJ_UINT32)vlcp->tmp;
}
//************************************************************************/
/** @brief Reads and unstuffs from rev_struct
*
* This is different than rev_read in that this fills in zeros when the
* the available data is consumed. The other does not care about the
* values when all data is consumed.
*
* See rev_read for more information about unstuffing
*
* @param [in] mrp is a pointer to rev_struct structure
*/
static INLINE
void rev_read_mrp(rev_struct_t *mrp)
{
OPJ_UINT32 val;
OPJ_UINT32 tmp;
OPJ_UINT32 bits;
OPJ_BOOL unstuff;
//process 4 bytes at a time
if (mrp->bits > 32) {
return;
}
val = 0;
if (mrp->size > 3) { // If there are 3 byte or more
// (mrp->data - 3) move pointer back to read 32 bits at once
val = read_le_uint32(mrp->data - 3); // read 32 bits
mrp->data -= 4; // move back pointer
mrp->size -= 4; // reduce count
} else if (mrp->size > 0) {
int i = 24;
while (mrp->size > 0) {
OPJ_UINT32 v = *mrp->data--; // read one byte at a time
val |= (v << i); // put byte in its correct location
--mrp->size;
i -= 8;
}
}
//accumulate in tmp, and keep count in bits
tmp = val >> 24;
//test if the last byte > 0x8F (unstuff must be true) and this is 0x7F
bits = 8u - ((mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = (val >> 24) > 0x8F;
//process the next byte
tmp |= ((val >> 16) & 0xFF) << bits;
bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = ((val >> 16) & 0xFF) > 0x8F;
tmp |= ((val >> 8) & 0xFF) << bits;
bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = ((val >> 8) & 0xFF) > 0x8F;
tmp |= (val & 0xFF) << bits;
bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
unstuff = (val & 0xFF) > 0x8F;
mrp->tmp |= (OPJ_UINT64)tmp << mrp->bits; // move data to mrp pointer
mrp->bits += bits;
mrp->unstuff = unstuff; // next byte
}
//************************************************************************/
/** @brief Initialized rev_struct structure for MRP segment, and reads
* a number of bytes such that the next 32 bits read are from
* an address that is a multiple of 4. Note this is designed for
* an architecture that read size must be compatible with the
* alignment of the read address
*
* There is another similar subroutine rev_init. This subroutine does
* NOT skip the first 12 bits, and starts with unstuff set to true.
*
* @param [in] mrp is a pointer to rev_struct structure
* @param [in] data is a pointer to byte at the start of the cleanup pass
* @param [in] lcup is the length of MagSgn+MEL+VLC segments
* @param [in] len2 is the length of SPP+MRP segments
*/
static INLINE
void rev_init_mrp(rev_struct_t *mrp, OPJ_UINT8* data, int lcup, int len2)
{
int num, i;
mrp->data = data + lcup + len2 - 1;
mrp->size = len2;
mrp->unstuff = OPJ_TRUE;
mrp->bits = 0;
mrp->tmp = 0;
//This code is designed for an architecture that read address should
// align to the read size (address multiple of 4 if read size is 4)
//These few lines take care of the case where data is not at a multiple
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the MRP stream
num = 1 + (int)((intptr_t)(mrp->data) & 0x3);
for (i = 0; i < num; ++i) {
OPJ_UINT64 d;
OPJ_UINT32 d_bits;
//read a byte, 0 if no more data
d = (mrp->size-- > 0) ? *mrp->data-- : 0;
//check if unstuffing is needed
d_bits = 8u - ((mrp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
mrp->tmp |= d << mrp->bits; // move data to vlcp->tmp
mrp->bits += d_bits;
mrp->unstuff = d > 0x8F; // for next byte
}
rev_read_mrp(mrp);
}
//************************************************************************/
/** @brief Retrieves 32 bits from the head of a rev_struct structure
*
* By the end of this call, mrp->tmp must have no less than 33 bits
*
* @param [in] mrp is a pointer to rev_struct structure
*/
static INLINE
OPJ_UINT32 rev_fetch_mrp(rev_struct_t *mrp)
{
if (mrp->bits < 32) { // if there are less than 32 bits in mrp->tmp
rev_read_mrp(mrp); // read 30-32 bits from mrp
if (mrp->bits < 32) { // if there is a space of 32 bits
rev_read_mrp(mrp); // read more
}
}
return (OPJ_UINT32)mrp->tmp; // return the head of mrp->tmp
}
//************************************************************************/
/** @brief Consumes num_bits from a rev_struct structure
*
* @param [in] mrp is a pointer to rev_struct structure
* @param [in] num_bits is the number of bits to be removed
*/
static INLINE
OPJ_UINT32 rev_advance_mrp(rev_struct_t *mrp, OPJ_UINT32 num_bits)
{
assert(num_bits <= mrp->bits); // we must not consume more than mrp->bits
mrp->tmp >>= num_bits; // discard the lowest num_bits bits
mrp->bits -= num_bits;
return (OPJ_UINT32)mrp->tmp; // return data after consumption
}
//************************************************************************/
/** @brief Decode initial UVLC to get the u value (or u_q)
*
* @param [in] vlc is the head of the VLC bitstream
* @param [in] mode is 0, 1, 2, 3, or 4. Values in 0 to 3 are composed of
* u_off of 1st quad and 2nd quad of a quad pair. The value
* 4 occurs when both bits are 1, and the event decoded
* from MEL bitstream is also 1.
* @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
* this value is a partial calculation of u + kappa.
*/
static INLINE
OPJ_UINT32 decode_init_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
{
//table stores possible decoding three bits from vlc
// there are 8 entries for xx1, x10, 100, 000, where x means do not care
// table value is made up of
// 2 bits in the LSB for prefix length
// 3 bits for suffix length
// 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
static const OPJ_UINT8 dec[8] = { // the index is the prefix codeword
3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
};
OPJ_UINT32 consumed_bits = 0;
if (mode == 0) { // both u_off are 0
u[0] = u[1] = 1; //Kappa is 1 for initial line
} else if (mode <= 2) { // u_off are either 01 or 10
OPJ_UINT32 d;
OPJ_UINT32 suffix_len;
d = dec[vlc & 0x7]; //look at the least significant 3 bits
vlc >>= d & 0x3; //prefix length
consumed_bits += d & 0x3;
suffix_len = ((d >> 2) & 0x7);
consumed_bits += suffix_len;
d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = (mode == 1) ? d + 1 : 1; // kappa is 1 for initial line
u[1] = (mode == 1) ? 1 : d + 1; // kappa is 1 for initial line
} else if (mode == 3) { // both u_off are 1, and MEL event is 0
OPJ_UINT32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d1 & 0x3; // Consume bits
consumed_bits += d1 & 0x3;
if ((d1 & 0x3) > 2) {
OPJ_UINT32 suffix_len;
//u_{q_2} prefix
u[1] = (vlc & 1) + 1 + 1; //Kappa is 1 for initial line
++consumed_bits;
vlc >>= 1;
suffix_len = ((d1 >> 2) & 0x7);
consumed_bits += suffix_len;
d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = d1 + 1; //Kappa is 1 for initial line
} else {
OPJ_UINT32 d2;
OPJ_UINT32 suffix_len;
d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d2 & 0x3; // Consume bits
consumed_bits += d2 & 0x3;
suffix_len = ((d1 >> 2) & 0x7);
consumed_bits += suffix_len;
d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = d1 + 1; //Kappa is 1 for initial line
vlc >>= suffix_len;
suffix_len = ((d2 >> 2) & 0x7);
consumed_bits += suffix_len;
d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[1] = d2 + 1; //Kappa is 1 for initial line
}
} else if (mode == 4) { // both u_off are 1, and MEL event is 1
OPJ_UINT32 d1;
OPJ_UINT32 d2;
OPJ_UINT32 suffix_len;
d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d1 & 0x3; // Consume bits
consumed_bits += d1 & 0x3;
d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d2 & 0x3; // Consume bits
consumed_bits += d2 & 0x3;
suffix_len = ((d1 >> 2) & 0x7);
consumed_bits += suffix_len;
d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = d1 + 3; // add 2+kappa
vlc >>= suffix_len;
suffix_len = ((d2 >> 2) & 0x7);
consumed_bits += suffix_len;
d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[1] = d2 + 3; // add 2+kappa
}
return consumed_bits;
}
//************************************************************************/
/** @brief Decode non-initial UVLC to get the u value (or u_q)
*
* @param [in] vlc is the head of the VLC bitstream
* @param [in] mode is 0, 1, 2, or 3. The 1st bit is u_off of 1st quad
* and 2nd for 2nd quad of a quad pair
* @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
* this value is a partial calculation of u + kappa.
*/
static INLINE
OPJ_UINT32 decode_noninit_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
{
//table stores possible decoding three bits from vlc
// there are 8 entries for xx1, x10, 100, 000, where x means do not care
// table value is made up of
// 2 bits in the LSB for prefix length
// 3 bits for suffix length
// 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
static const OPJ_UINT8 dec[8] = {
3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
};
OPJ_UINT32 consumed_bits = 0;
if (mode == 0) {
u[0] = u[1] = 1; //for kappa
} else if (mode <= 2) { //u_off are either 01 or 10
OPJ_UINT32 d;
OPJ_UINT32 suffix_len;
d = dec[vlc & 0x7]; //look at the least significant 3 bits
vlc >>= d & 0x3; //prefix length
consumed_bits += d & 0x3;
suffix_len = ((d >> 2) & 0x7);
consumed_bits += suffix_len;
d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = (mode == 1) ? d + 1 : 1; //for kappa
u[1] = (mode == 1) ? 1 : d + 1; //for kappa
} else if (mode == 3) { // both u_off are 1
OPJ_UINT32 d1;
OPJ_UINT32 d2;
OPJ_UINT32 suffix_len;
d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d1 & 0x3; // Consume bits
consumed_bits += d1 & 0x3;
d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
vlc >>= d2 & 0x3; // Consume bits
consumed_bits += d2 & 0x3;
suffix_len = ((d1 >> 2) & 0x7);
consumed_bits += suffix_len;
d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[0] = d1 + 1; //1 for kappa
vlc >>= suffix_len;
suffix_len = ((d2 >> 2) & 0x7);
consumed_bits += suffix_len;
d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
u[1] = d2 + 1; //1 for kappa
}
return consumed_bits;
}
//************************************************************************/
/** @brief State structure for reading and unstuffing of forward-growing
* bitstreams; these are: MagSgn and SPP bitstreams
*/
typedef struct frwd_struct {
const OPJ_UINT8* data; //!<pointer to bitstream
OPJ_UINT64 tmp; //!<temporary buffer of read data
OPJ_UINT32 bits; //!<number of bits stored in tmp
OPJ_BOOL unstuff; //!<true if a bit needs to be unstuffed from next byte
int size; //!<size of data
OPJ_UINT32 X; //!<0 or 0xFF, X's are inserted at end of bitstream
} frwd_struct_t;
//************************************************************************/
/** @brief Read and unstuffs 32 bits from forward-growing bitstream
*
* A subroutine to read from both the MagSgn or SPP bitstreams;
* in particular, when MagSgn bitstream is consumed, 0xFF's are fed,
* while when SPP is exhausted 0's are fed in.
* X controls this value.
*
* Unstuffing prevent sequences that are more than 0xFF7F from appearing
* in the conpressed sequence. So whenever a value of 0xFF is coded, the
* MSB of the next byte is set 0 and must be ignored during decoding.
*
* Reading can go beyond the end of buffer by up to 3 bytes.
*
* @param [in] msp is a pointer to frwd_struct_t structure
*
*/
static INLINE
void frwd_read(frwd_struct_t *msp)
{
OPJ_UINT32 val;
OPJ_UINT32 bits;
OPJ_UINT32 t;
OPJ_BOOL unstuff;
assert(msp->bits <= 32); // assert that there is a space for 32 bits
val = 0u;
if (msp->size > 3) {
val = read_le_uint32(msp->data); // read 32 bits
msp->data += 4; // increment pointer
msp->size -= 4; // reduce size
} else if (msp->size > 0) {
int i = 0;
val = msp->X != 0 ? 0xFFFFFFFFu : 0;
while (msp->size > 0) {
OPJ_UINT32 v = *msp->data++; // read one byte at a time
OPJ_UINT32 m = ~(0xFFu << i); // mask of location
val = (val & m) | (v << i); // put one byte in its correct location
--msp->size;
i += 8;
}
} else {
val = msp->X != 0 ? 0xFFFFFFFFu : 0;
}
// we accumulate in t and keep a count of the number of bits in bits
bits = 8u - (msp->unstuff ? 1u : 0u);
t = val & 0xFF;
unstuff = ((val & 0xFF) == 0xFF); // Do we need unstuffing next?
t |= ((val >> 8) & 0xFF) << bits;
bits += 8u - (unstuff ? 1u : 0u);
unstuff = (((val >> 8) & 0xFF) == 0xFF);
t |= ((val >> 16) & 0xFF) << bits;
bits += 8u - (unstuff ? 1u : 0u);
unstuff = (((val >> 16) & 0xFF) == 0xFF);
t |= ((val >> 24) & 0xFF) << bits;
bits += 8u - (unstuff ? 1u : 0u);
msp->unstuff = (((val >> 24) & 0xFF) == 0xFF); // for next byte
msp->tmp |= ((OPJ_UINT64)t) << msp->bits; // move data to msp->tmp
msp->bits += bits;
}
//************************************************************************/
/** @brief Initialize frwd_struct_t struct and reads some bytes
*
* @param [in] msp is a pointer to frwd_struct_t
* @param [in] data is a pointer to the start of data
* @param [in] size is the number of byte in the bitstream
* @param [in] X is the value fed in when the bitstream is exhausted.
* See frwd_read.
*/
static INLINE
void frwd_init(frwd_struct_t *msp, const OPJ_UINT8* data, int size,
OPJ_UINT32 X)
{
int num, i;
msp->data = data;
msp->tmp = 0;
msp->bits = 0;
msp->unstuff = OPJ_FALSE;
msp->size = size;
msp->X = X;
assert(msp->X == 0 || msp->X == 0xFF);
//This code is designed for an architecture that read address should
// align to the read size (address multiple of 4 if read size is 4)
//These few lines take care of the case where data is not at a multiple
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the bitstream
num = 4 - (int)((intptr_t)(msp->data) & 0x3);
for (i = 0; i < num; ++i) {
OPJ_UINT64 d;
//read a byte if the buffer is not exhausted, otherwise set it to X
d = msp->size-- > 0 ? *msp->data++ : msp->X;
msp->tmp |= (d << msp->bits); // store data in msp->tmp
msp->bits += 8u - (msp->unstuff ? 1u : 0u); // number of bits added to msp->tmp
msp->unstuff = ((d & 0xFF) == 0xFF); // unstuffing for next byte
}
frwd_read(msp); // read 32 bits more
}
//************************************************************************/
/** @brief Consume num_bits bits from the bitstream of frwd_struct_t
*
* @param [in] msp is a pointer to frwd_struct_t
* @param [in] num_bits is the number of bit to consume
*/
static INLINE
void frwd_advance(frwd_struct_t *msp, OPJ_UINT32 num_bits)
{
assert(num_bits <= msp->bits);
msp->tmp >>= num_bits; // consume num_bits
msp->bits -= num_bits;
}
//************************************************************************/
/** @brief Fetches 32 bits from the frwd_struct_t bitstream