Repository navigation
Expand file tree
/
Copy pathslabelacdecode.m
More file actions
227 lines (221 loc) · 6.71 KB
/
Copy pathslabelacdecode.m
File metadata and controls
227 lines (221 loc) · 6.71 KB
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
function output = slabelacdecode(input, varargin)
%
% output = slabelacdecode(input)
%
% Decompress a slabelac stream (experimental; see slabelacencode): the elements' little-endian
% bytes in their row-major order. ZMat (with slabelac) does the work when it is installed; else
% this file's vectorized code (both passes a loop over wavefront planes and groups of rANS lanes).
%
% input:
% input: the slabelac stream (uint8 or char)
%
% output:
% output: the decoded bytes (uint8 row); typecast them to the array's class
%
% examples:
% z = slabelacencode(uint8([0 0 1 1 1 2]));
% slabelacdecode(z)
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://neurojson.org/jsonlab)
%
% ZMat's compiled decoder, when it has slabelac
persistent nozmat
if (isempty(nozmat) && exist('zmat', 'file'))
try
output = zmat(reshape(uint8(input), 1, []), 0, 'slabelac');
if (~isempty(output) || numel(input) < 8)
output = reshape(output, 1, []);
return
end
catch
end
nozmat = true;
end
b = reshape(uint8(input), 1, []);
if (isempty(b) || b(1) ~= 2)
error('slabelac: not a version-2 stream');
end
at = 1;
[es, at] = getvarint_(b, at);
[nx, at] = getvarint_(b, at);
[ny, at] = getvarint_(b, at);
[count, at] = getvarint_(b, at);
[K, at] = getvarint_(b, at);
if (~any(es == [1 2 4 8]) || nx == 0 || ny == 0 || (count && mod(count, nx * ny)) || K > 65536 || (count && K == 0))
error('slabelac: a malformed header');
end
if (numel(b) - at < K * es)
error('slabelac: the value table ends early');
end
cls = {'uint8', 'uint16', '', 'uint32', '', '', '', 'uint64'};
values = typecast(b(at + 1:at + K * es), cls{es});
at = at + K * es;
if (count == 0)
output = zeros(1, 0, 'uint8');
return
end
if (K == 1)
output = typecast(repmat(values, 1, count), 'uint8');
return
end
[bits, at] = getvarint_(b, at);
if (bits > 6)
error('slabelac: a malformed block size');
end
B = 2^bits;
nz = count / (nx * ny);
nb = [floor((nx - 1) / B) + 1, floor((ny - 1) / B) + 1, floor((nz - 1) / B) + 1];
nblocks = prod(nb);
[N, at] = getvarint_(b, at);
if (N == 0 || N > count + nblocks || numel(b) - at < 4 * N)
error('slabelac: a malformed lane count');
end
state = double(typecast(b(at + 1:at + 4 * N), 'uint32'))';
at = at + 4 * N;
nw = floor((numel(b) - at) / 2);
words = double(typecast(b(at + 1:at + 2 * nw), 'uint16'))';
wp = 0;
% the block pass: version 1 over the block grid, K + 1 symbols, a plane at a time
[block, state, wp] = planes_(state, words, wp, N, nb(1), nb(2), nblocks, K + 1, zeros(nblocks, 1), [], 0);
% the single-label blocks' elements, then the mixed blocks' elements in version 1's order
i = (0:count - 1)';
x = mod(i, nx);
y = mod(floor(i / nx), ny);
z = floor(i / (nx * ny));
bi = floor(x / B) + nb(1) * (floor(y / B) + nb(2) * floor(z / B));
clear i x y z;
sym = block(bi + 1);
clear bi;
[sym, state, wp] = planes_(state, words, wp, N, nx, ny, count, K, sym, sym == K, nblocks);
output = typecast(reshape(values(sym + 1), 1, []), 'uint8');
%% -------------------------------------------------------------------------
function [sym, state, wp] = planes_(state, words, wp, N, nx, ny, count, K, sym, keep, pos0)
% labelac version 1's decoding of the elements of an nx x ny x (count / nx ny) array into sym, a
% plane at a time; with keep, only the elements where keep is true (the others are known); the
% pass's first position is pos0 of the stream
M = 65536;
LOW = 65536;
INC = 128;
VOTE = 128;
LIMIT = 65535;
nw = numel(words);
i = (0:count - 1)';
x = mod(i, nx);
y = mod(floor(i / nx), ny);
z = floor(i / (nx * ny));
t = x + y + z;
if (isempty(keep))
sel = (1:count)';
else
sel = find(keep);
end
[~, o] = sort(t(sel)); % (stable)
order = sel(o);
bounds = [0; cumsum(accumarray(t(sel) + 1, 1, [max(t) + 1, 1]))];
clear o sel t;
hasl = x > 0;
hasu = y > 0;
hasf = z > 0;
mkeys = zeros(0, 1);
mrows = zeros(0, 1);
counts = zeros(1024, K);
nrow = 0;
for p = 1:numel(bounds) - 1
lo = bounds(p);
hi = bounds(p + 1);
if (hi <= lo)
continue
end
j = order(lo + 1:hi);
L = zeros(numel(j), 1);
U = L;
F = L;
k = hasl(j);
L(k) = sym(j(k) - 1);
k = hasu(j);
U(k) = sym(j(k) - nx);
k = hasf(j);
F(k) = sym(j(k) - nx * ny);
[uk, ~, inv] = unique((L * K + U) * K + F);
inv = inv(:);
[tf, loc] = ismember(uk, mkeys);
rows = zeros(numel(uk), 1);
rows(tf) = mrows(loc(tf));
new = uk(~tf);
if (~isempty(new))
newrows = nrow + (1:numel(new))';
if (newrows(end) > size(counts, 1))
counts(max(2 * size(counts, 1), newrows(end)), K) = 0;
end
counts(newrows, :) = 1;
for nb = [floor(new / (K * K)), mod(floor(new / K), K), mod(new, K)]
ix = newrows + nb * size(counts, 1);
counts(ix) = counts(ix) + VOTE;
end
rows(~tf) = newrows;
nrow = newrows(end);
[mkeys, o] = sort([mkeys; new]);
mrows = [mrows; newrows];
mrows = mrows(o);
end
nu = numel(rows);
cu = counts(rows, :);
f = 1 + floor(cu * (M - K) ./ repmat(sum(cu, 2), 1, K));
[~, im] = max(cu, [], 2);
ix = (1:nu)' + (im - 1) * nu;
f(ix) = f(ix) + M - sum(f, 2);
c = cumsum(f, 2) - f;
s = zeros(hi - lo, 1);
for g = lo:N:hi - 1
ge = min(g + N, hi);
lanes = mod(pos0 + (g:ge - 1), N)' + 1;
xs = state(lanes);
slot = mod(xs, 65536);
ci = inv(g - lo + 1:ge - lo);
sg = sum(bsxfun(@le, c(ci, :), slot), 2) - 1;
ix = ci + sg * nu;
fs = f(ix);
cs = c(ix);
xs = fs(:) .* floor(xs / 65536) + slot - cs(:);
need = find(xs < LOW);
if (~isempty(need))
if (wp + numel(need) > nw)
error('slabelac: the data ends early');
end
xs(need) = xs(need) * 65536 + words(wp + 1:wp + numel(need));
wp = wp + numel(need);
end
state(lanes) = xs;
s(g - lo + 1:ge - lo) = sg;
end
sym(j) = s;
counts(rows, :) = counts(rows, :) + accumarray([inv, s + 1], INC, [nu, K]);
touched = rows;
while (~isempty(touched))
big = touched(sum(counts(touched, :), 2) > LIMIT);
if (isempty(big))
break
end
counts(big, :) = floor((counts(big, :) + 1) / 2);
touched = big;
end
end
%% -------------------------------------------------------------------------
function [v, at] = getvarint_(b, at)
v = 0;
scale = 1;
while (true)
if (at >= numel(b))
error('slabelac: the header ends early');
end
at = at + 1;
c = double(b(at));
v = v + mod(c, 128) * scale;
if (c < 128)
return
end
scale = scale * 128;
end