blob: 97de7869e1b781913e83fb70b210f41f731aa6bc [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001/*
Rickard Bolinbc6ee582022-11-04 08:24:29 +00002 * SPDX-FileCopyrightText: Copyright 2020 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Hall79d07d22020-04-27 18:20:16 +01003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <stdint.h>
22#include <stdbool.h>
23#include <string.h>
24#include <assert.h>
25#include <math.h>
26#include <stdarg.h>
27#include <math.h>
28#include "mlw_common.h"
29#include "mlw_decode.h"
30
31
32/////////////////////////////// Read from bitstream
33
34typedef struct bitbuf {
35 uint8_t *buf;
36 int buf_size; // in bytes
37 int pos; // bit pos of next bit
38 int log_symbols;
39} bitbuf_t;
40
41
42// size in byte
43static void bitbuf_init( bitbuf_t *bb, uint8_t *buf, int size, int log_symbols) {
44 bb->buf = buf;
45 bb->pos = 0;
46 bb->buf_size = size;
47 bb->log_symbols = log_symbols;
48}
49
50static int bitbuf_getbit( bitbuf_t *bb) {
51 int byte_pos = bb->pos>>3;
52 int bit_pos = bb->pos&7;
53 if ( byte_pos < 0 || byte_pos >= bb->buf_size ) {
54 printf("bitbuf_getbit: underrun, bit_pos %3d byte_pos %3d buf_size %3d\n", bit_pos, byte_pos, bb->buf_size);
55 exit(1);
56 }
57 int bit = bb->buf[ byte_pos ] & (1<<bit_pos) ? 1 : 0;
58 bb->pos++;
59 return bit;
60}
61
62static int bitbuf_get( bitbuf_t *bb, const char *name, int len) {
63 int i, data=0, save_pos=bb->pos;
64 if (len>0) {
65 for(i=0; i<len; i++) {
66 data |= bitbuf_getbit(bb)<<i;
67 }
68 if (bb->log_symbols)
69 printf("bitbuf: pos %3d %7s len %d data %x\n", save_pos, name, len, data);
70 }
71 return data;
72}
73
74// Decode the given weight stream
75// inbuf compressed bitstream
76// inbuf_size size of compressed bitstream in bytes
77// outbuf uncompressed 9bit signed weights, buffer malloced
78// verbose if non-zero, printf log
79// Return value is the number of uncompressed weights
80int mlw_decode( uint8_t *inbuf, int inbuf_size, int16_t **outbuf, int verbose) {
81 int nvalues;
82 int w_grc_div;
83 int w_grc_trunc;
84 int w_uncompressed;
85 int z_grc_div, z_prev_grc_div=0;
86 int new_palette;
87 int palsize=0, palbits=0;
88 int direct_offset=0;
89 int16_t palette[512];
90 int first=1;
91 int use_zero_run, i, j;
92 int outbuf_size=0;
93 int nchunks=0;
94
95 *outbuf=0;
96
97 bitbuf_t bitbuf_s, *bb=&bitbuf_s;
98 bitbuf_init( bb, inbuf, inbuf_size, (verbose&2)?1:0 );
99
100 // Loop over all slices
101 while(1) {
102 // Decode slice header
103 z_grc_div = bitbuf_get( bb, "ZDIV", 3 );
104 while(z_grc_div==ZDIV_EOS) { // TODO: change to ZDIV_PAD
105 // End of stream
106 // Byte align
107 bitbuf_get( bb, "BYTEALIGN", (8-(bb->pos&7))&7 );
108 first=1;
109 if ( (bb->pos/8) == inbuf_size) {
110 // Quit if we actually reached end of input stream
111 break;
112 }
113 z_grc_div = bitbuf_get( bb, "ZDIV", 3 );
114 }
115 if ( (bb->pos/8) == inbuf_size) {
116 break; // reached end of input stream
117 }
118 assert(z_grc_div<4 || z_grc_div==ZDIV_DISABLE);
119 use_zero_run = z_grc_div!=ZDIV_DISABLE; // alternating grc
120 nvalues = bitbuf_get( bb, "SLICELEN", 15 )+1;
121 w_grc_div = bitbuf_get( bb, "WDIV", 3 );
122 w_grc_trunc = bitbuf_get( bb, "WTRUNC", 1 );
123 new_palette = bitbuf_get( bb, "NEWPAL", 1 );
124 if (first) {
125 // the first slice must have a palette/direct mode setup
126 assert(new_palette);
127 first=0;
128 }
129 if (!new_palette) {
130 // At the moment it is not supported to change between alternating
131 // and non-alternating without redefining the palette (this is because
132 // the zero is not included in the palette in case of alternating)
133 int prev_use_zero_run = z_prev_grc_div!=ZDIV_DISABLE;
134 (void)(prev_use_zero_run);
135 assert( use_zero_run == prev_use_zero_run);
136 }
137 z_prev_grc_div = z_grc_div;
138 if (new_palette) {
139 direct_offset = bitbuf_get( bb, "DIROFS", 5 );
140 palsize = bitbuf_get( bb, "PALSIZE", 5 );
141 if (palsize>0)
142 palsize++;
143 palbits = bitbuf_get( bb, "PALBITS", 3 )+2;
144 for(i=0; i<palsize; i++) {
145 palette[i] = bitbuf_get( bb, "PALETTE", palbits );
146 }
147 }
148
149 if (w_grc_div==WDIV_UNCOMPRESSED) {
150 // Uncompressed mode
151 w_uncompressed = 1;
152 int uncompressed_bits;
153 if (palsize>0) {
154 // Uncompressed bits is given by palette size.
155 uncompressed_bits=0;
156 while( (1<<uncompressed_bits) < palsize )
157 uncompressed_bits++;
158 } else {
159 // No palette. PALBITS is used to specify uncompressed bits.
160 uncompressed_bits=palbits;
161 }
162 // In uncompressed mode there's only a remainder part (no unary)
163 // This is achieved by setting w_grc_div to index bit width
164 w_grc_div = uncompressed_bits;
165 } else {
166 w_uncompressed = 0;
167 assert(w_grc_div<6);
168 }
169
170 // Decode the slice
171 int z_nvalues = nvalues + (new_palette?1:0);
172 int *w_value = malloc( nvalues*sizeof(int) );
173 int *z_value = malloc( z_nvalues*sizeof(int) );
174 int w_pos=0, z_pos=0;
175 int w_prev_pos=0, z_prev_pos=0;
176 int w_unary0=0, w_unary1=0, w_unary1_len=0, w_q[12]={0}, w_carry=0;
177 int z_unary=0, z_q[12]={0}, z_carry=0;
178 int w_nsymbols=0;
179 int w_prev_enable=0, w_prev_nsymbols=0, w_prev_q[12]={0};
180 int z_nsymbols=0;
181 int z_prev_enable=0, z_prev_nsymbols=0, z_prev_q[12]={0};
182 int total_zcnt=0;
183 int z_unary_len = z_grc_div<3 ? 12 : 8;
184
185 // Loop over all chunks in the slice
186 do {
187 // Flow control to possibly throttle either the weights or zero-runs
188 int balance = use_zero_run ? w_pos - z_pos : 0;
189 int w_enable = (balance<8 || !use_zero_run) && w_pos<nvalues;
190 int z_enable = balance>=0 && use_zero_run && z_pos<z_nvalues;
191 if (w_enable) {
192 if (!w_uncompressed)
193 w_unary0 = bitbuf_get( bb, "WUNARY0", 12 );
194 else
195 w_unary0 = 0;
196 }
197 if (z_enable) {
198 z_unary = bitbuf_get( bb, "ZUNARY", z_unary_len );
199 z_nsymbols=0;
200 int cnt = z_carry;
201 for(i=0; i<z_unary_len; i++) {
202 if (z_unary & (1<<i)) {
203 cnt++;
204 } else {
205 z_q[z_nsymbols++] = cnt;
206 cnt=0;
207 }
208 }
209 z_carry = cnt;
210 z_pos += z_nsymbols;
211 }
212 if (w_enable) {
213 w_unary1_len=0;
214 int max_symbols = w_uncompressed && w_grc_div>5 ? 8 : 12;
215 for(i=0; i<max_symbols; i++) {
216 if (w_unary0&(1<<i))
217 w_unary1_len++;
218 }
219 w_unary1 = bitbuf_get( bb, "WUNARY1", w_unary1_len );
220 w_nsymbols=0;
221 int cnt = w_carry;
222 for(i=0; i<max_symbols; i++) {
223 int code=0;
224 if (w_unary0 & (1<<i)) {
225 code++;
226 if (w_unary1&1) {
227 code++;
228 }
229 w_unary1 = w_unary1>>1;
230 }
231 cnt += code;
232 if (code<2 || w_grc_trunc) {
233 w_q[w_nsymbols++] = cnt;
234 cnt=0;
235 }
236 }
237 w_carry = cnt;
238 w_pos += w_nsymbols;
239 }
240 if (w_prev_enable) {
241 for(i=0; i<w_prev_nsymbols && w_prev_pos<nvalues; i++, w_prev_pos++) {
242 int remain = bitbuf_get( bb, "WREMAIN", w_grc_div );
243 w_value[w_prev_pos] = (w_prev_q[i]<<w_grc_div) + remain;
244 }
245 }
246 if (z_prev_enable) {
247 for(i=0; i<z_prev_nsymbols && z_prev_pos<z_nvalues; i++, z_prev_pos++) {
248 int remain = bitbuf_get( bb, "ZREMAIN", z_grc_div );
249 z_value[z_prev_pos] = (z_prev_q[i]<<z_grc_div) + remain;
250 total_zcnt += z_value[z_prev_pos];
251 }
252 }
253 w_prev_enable = w_enable;
254 w_prev_nsymbols = w_nsymbols;
255 memcpy( w_prev_q, w_q, sizeof(w_prev_q));
256 z_prev_enable = z_enable;
257 z_prev_nsymbols = z_nsymbols;
258 memcpy( z_prev_q, z_q, sizeof(z_prev_q));
259 nchunks++;
260 } while( w_prev_enable || z_prev_enable );
261
262 // Interleave non-zero and zeros into the outbut buffer
263 // Increase the outbuffer to fit the new slice
264 *outbuf = realloc( *outbuf, (outbuf_size + nvalues + total_zcnt)*sizeof(int16_t));
265
266 int k=outbuf_size;
267
268 // Insert initial zeros
269 // (slices redefining the palette may start with zeros)
270 if (new_palette && use_zero_run) {
271 for(j=0; j<z_value[0]; j++) {
272 (*outbuf)[k++] = 0;
273 }
274 }
275
276 // Loop over all weights and insert zeros in-between
277 for(i=0; i<nvalues; i++) {
278 int val;
279 assert(w_value[i]<512); // HW supports 9bit
280 if (w_value[i]<palsize) {
281 val = palette[w_value[i]];
282 } else {
283 val = w_value[i]-palsize+direct_offset;
284 }
285 int sign = val&1;
286 int mag = val>>1;
287 (*outbuf)[k++] = sign ? -mag : mag;
288 if (use_zero_run) {
289 for(j=0; j<z_value[i+(new_palette?1:0)]; j++) {
290 (*outbuf)[k++] = 0;
291 }
292 }
293 }
294
295 outbuf_size = k;
296 free(w_value);
297 free(z_value);
298 }
299 return outbuf_size;
300}