1 #include <limits.h>
2 
3 static void _t2(inv_xform, Int, DIMS)(Int* p);
4 
5 /* private functions ------------------------------------------------------- */
6 
7 /* inverse lifting transform of 4-vector */
8 static void
_t1(inv_lift,Int)9 _t1(inv_lift, Int)(Int* p, uint s)
10 {
11   Int x, y, z, w;
12   x = *p; p += s;
13   y = *p; p += s;
14   z = *p; p += s;
15   w = *p; p += s;
16 
17   /*
18   ** non-orthogonal transform
19   **       ( 4  6 -4 -1) (x)
20   ** 1/4 * ( 4  2  4  5) (y)
21   **       ( 4 -2  4 -5) (z)
22   **       ( 4 -6 -4  1) (w)
23   */
24   y += w >> 1; w -= y >> 1;
25   y += w; w <<= 1; w -= y;
26   z += x; x <<= 1; x -= z;
27   y += z; z <<= 1; z -= y;
28   w += x; x <<= 1; x -= w;
29 
30   p -= s; *p = w;
31   p -= s; *p = z;
32   p -= s; *p = y;
33   p -= s; *p = x;
34 }
35 
36 /* map two's complement signed integer to negabinary unsigned integer */
37 static Int
_t1(uint2int,UInt)38 _t1(uint2int, UInt)(UInt x)
39 {
40   return (Int)((x ^ NBMASK) - NBMASK);
41 }
42 
43 /* reorder unsigned coefficients and convert to signed integer */
44 static void
_t1(inv_order,Int)45 _t1(inv_order, Int)(const UInt* ublock, Int* iblock, const uchar* perm, uint n)
46 {
47   do
48     iblock[*perm++] = _t1(uint2int, UInt)(*ublock++);
49   while (--n);
50 }
51 
52 /* decompress sequence of size unsigned integers */
53 static uint
_t1(decode_ints,UInt)54 _t1(decode_ints, UInt)(bitstream* restrict_ stream, uint maxbits, uint maxprec, UInt* restrict_ data, uint size)
55 {
56   /* make a copy of bit stream to avoid aliasing */
57   bitstream s = *stream;
58   uint intprec = CHAR_BIT * (uint)sizeof(UInt);
59   uint kmin = intprec > maxprec ? intprec - maxprec : 0;
60   uint bits = maxbits;
61   uint i, k, m, n;
62   uint64 x;
63 
64   /* initialize data array to all zeros */
65   for (i = 0; i < size; i++)
66     data[i] = 0;
67 
68   /* decode one bit plane at a time from MSB to LSB */
69   for (k = intprec, n = 0; bits && k-- > kmin;) {
70     /* decode first n bits of bit plane #k */
71     m = MIN(n, bits);
72     bits -= m;
73     x = stream_read_bits(&s, m);
74     /* unary run-length decode remainder of bit plane */
75     for (; n < size && bits && (bits--, stream_read_bit(&s)); x += (uint64)1 << n++)
76       for (; n < size - 1 && bits && (bits--, !stream_read_bit(&s)); n++)
77         ;
78     /* deposit bit plane from x */
79     for (i = 0; x; i++, x >>= 1)
80       data[i] += (UInt)(x & 1u) << k;
81   }
82 
83   *stream = s;
84   return maxbits - bits;
85 }
86 
87 /* decompress sequence of size > 64 unsigned integers */
88 static uint
_t1(decode_many_ints,UInt)89 _t1(decode_many_ints, UInt)(bitstream* restrict_ stream, uint maxbits, uint maxprec, UInt* restrict_ data, uint size)
90 {
91   /* make a copy of bit stream to avoid aliasing */
92   bitstream s = *stream;
93   uint intprec = CHAR_BIT * (uint)sizeof(UInt);
94   uint kmin = intprec > maxprec ? intprec - maxprec : 0;
95   uint bits = maxbits;
96   uint i, k, m, n;
97 
98   /* initialize data array to all zeros */
99   for (i = 0; i < size; i++)
100     data[i] = 0;
101 
102   /* decode one bit plane at a time from MSB to LSB */
103   for (k = intprec, n = 0; bits && k-- > kmin;) {
104     /* decode first n bits of bit plane #k */
105     m = MIN(n, bits);
106     bits -= m;
107     for (i = 0; i < m; i++)
108       if (stream_read_bit(&s))
109         data[i] += (UInt)1 << k;
110     /* unary run-length decode remainder of bit plane */
111     for (; n < size && bits && (--bits, stream_read_bit(&s)); data[n] += (UInt)1 << k, n++)
112       for (; n < size - 1 && bits && (--bits, !stream_read_bit(&s)); n++)
113         ;
114   }
115 
116   *stream = s;
117   return maxbits - bits;
118 }
119 
120 /* decode block of integers */
121 static uint
_t2(decode_block,Int,DIMS)122 _t2(decode_block, Int, DIMS)(bitstream* stream, int minbits, int maxbits, int maxprec, Int* iblock)
123 {
124   int bits;
125   cache_align_(UInt ublock[BLOCK_SIZE]);
126   /* decode integer coefficients */
127   if (BLOCK_SIZE <= 64)
128     bits = _t1(decode_ints, UInt)(stream, maxbits, maxprec, ublock, BLOCK_SIZE);
129   else
130     bits = _t1(decode_many_ints, UInt)(stream, maxbits, maxprec, ublock, BLOCK_SIZE);
131   /* read at least minbits bits */
132   if (bits < minbits) {
133     stream_skip(stream, minbits - bits);
134     bits = minbits;
135   }
136   /* reorder unsigned coefficients and convert to signed integer */
137   _t1(inv_order, Int)(ublock, iblock, PERM, BLOCK_SIZE);
138   /* perform decorrelating transform */
139   _t2(inv_xform, Int, DIMS)(iblock);
140   return bits;
141 }
142