]> git.sesse.net Git - kdenlive/blob - src/v4l/dec_s561.c
ae1bc4fcd94a9799f7b295b3e9efac0560124cdf
[kdenlive] / src / v4l / dec_s561.c
1 /* Spca561decoder (C) 2005 Andrzej Szombierski [qq@kuku.eu.org] */
2 /*==============================================================*/
3 /* This program is distributed under the terms of the GNU       */
4 /* General Public License, version 2. You may use, modify,      */
5 /* and redistribute it under the terms of this license. A       */
6 /* copy should be included with this source.                    */
7
8 /*
9  *      Decoder for compressed spca561 images
10  *      It was developed for "Labtec WebCam Elch 2(SPCA561A)" (046d:0929)
11  *      but it might work with other spca561 cameras
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include <string.h>
19 #include "src.h"
20 #include "dec.h"
21
22 /*fixme: not reentrant */
23 static unsigned int bit_bucket;
24 static const unsigned char *input_ptr;
25
26 static void refill(int *bitfill)
27 {
28         if (*bitfill < 8) {
29                 bit_bucket = (bit_bucket << 8) | *(input_ptr++);
30                 *bitfill += 8;
31         }
32 }
33
34 static int nbits(int *bitfill, int n)
35 {
36         bit_bucket = (bit_bucket << 8) | *(input_ptr++);
37         *bitfill -= n;
38         return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1);
39 }
40
41 static int _nbits(int *bitfill, int n)
42 {
43         *bitfill -= n;
44         return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1);
45 }
46
47 static int fun_A(int *bitfill)
48 {
49         int ret;
50         static int tab[] = {
51                 12, 13, 14, 15, 16, 17, 18, 19, -12, -13, -14, -15,
52                 -16, -17, -18, -19, -19
53         };
54
55         ret = tab[nbits(bitfill, 4)];
56
57         refill(bitfill);
58         return ret;
59 }
60 static int fun_B(int *bitfill)
61 {
62         static int tab1[] =
63             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31, 31,
64                 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
65                     16, 17,
66                 18,
67                 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
68         };
69         static int tab[] =
70             { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -5,
71                 -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17,
72                     -18, -19
73         };
74         unsigned int tmp;
75
76         tmp = nbits(bitfill, 7) - 68;
77         refill(bitfill);
78         if (tmp > 47)
79                 return 0xff;
80         return tab[tab1[tmp]];
81 }
82 static int fun_C(int *bitfill, int gkw)
83 {
84         static int tab1[] =
85             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23, 23, 23, 23, 23, 23,
86                 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
87                     12, 13,
88                 14,
89                 15, 16, 17, 18, 19, 20, 21, 22
90         };
91         static int tab[] =
92             { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -9, -10, -11,
93                 -12, -13, -14, -15, -16, -17, -18, -19
94         };
95         unsigned int tmp;
96
97         if (gkw == 0xfe) {
98                 if (nbits(bitfill, 1) == 0)
99                         return 7;
100                 else
101                         return -8;
102         }
103
104         if (gkw != 0xff)
105                 return 0xff;
106
107         tmp = nbits(bitfill, 7) - 72;
108         if (tmp > 43)
109                 return 0xff;
110
111         refill(bitfill);
112         return tab[tab1[tmp]];
113 }
114 static int fun_D(int *bitfill, int gkw)
115 {
116         if (gkw == 0xfd) {
117                 if (nbits(bitfill, 1) == 0)
118                         return 12;
119                 return -13;
120         }
121
122         if (gkw == 0xfc) {
123                 if (nbits(bitfill, 1) == 0)
124                         return 13;
125                 return -14;
126         }
127
128         if (gkw == 0xfe) {
129                 switch (nbits(bitfill, 2)) {
130                 case 0:
131                         return 14;
132                 case 1:
133                         return -15;
134                 case 2:
135                         return 15;
136                 case 3:
137                         return -16;
138                 }
139         }
140
141         if (gkw == 0xff) {
142                 switch (nbits(bitfill, 3)) {
143                 case 4:
144                         return 16;
145                 case 5:
146                         return -17;
147                 case 6:
148                         return 17;
149                 case 7:
150                         return -18;
151                 case 2:
152                         return _nbits(bitfill, 1) ? 0xed : 0x12;
153                 case 3:
154                         (*bitfill)--;
155                         return 18;
156                 }
157                 return 0xff;
158         }
159         return gkw;
160 }
161
162 static int fun_E(int cur_byte, int *bitfill)
163 {
164         static int tab0[] = { 0, -1, 1, -2, 2, -3, 3, -4 };
165         static int tab1[] = { 4, -5, 5, -6, 6, -7, 7, -8 };
166         static int tab2[] = { 8, -9, 9, -10, 10, -11, 11, -12 };
167         static int tab3[] = { 12, -13, 13, -14, 14, -15, 15, -16 };
168         static int tab4[] = { 16, -17, 17, -18, 18, -19, 19, -19 };
169
170         if ((cur_byte & 0xf0) >= 0x80) {
171                 *bitfill -= 4;
172                 return tab0[(cur_byte >> 4) & 7];
173         }
174         if ((cur_byte & 0xc0) == 0x40) {
175                 *bitfill -= 5;
176                 return tab1[(cur_byte >> 3) & 7];
177
178         }
179         if ((cur_byte & 0xe0) == 0x20) {
180                 *bitfill -= 6;
181                 return tab2[(cur_byte >> 2) & 7];
182
183         }
184         if ((cur_byte & 0xf0) == 0x10) {
185                 *bitfill -= 7;
186                 return tab3[(cur_byte >> 1) & 7];
187
188         }
189         if ((cur_byte & 0xf8) == 8) {
190                 *bitfill -= 8;
191                 return tab4[cur_byte & 7];
192         }
193         return 0xff;
194 }
195
196 static int fun_F(int cur_byte, int *bitfill)
197 {
198         *bitfill -= 5;
199         switch (cur_byte & 0xf8) {
200         case 0x80:
201                 return 0;
202         case 0x88:
203                 return -1;
204         case 0x90:
205                 return 1;
206         case 0x98:
207                 return -2;
208         case 0xa0:
209                 return 2;
210         case 0xa8:
211                 return -3;
212         case 0xb0:
213                 return 3;
214         case 0xb8:
215                 return -4;
216         case 0xc0:
217                 return 4;
218         case 0xc8:
219                 return -5;
220         case 0xd0:
221                 return 5;
222         case 0xd8:
223                 return -6;
224         case 0xe0:
225                 return 6;
226         case 0xe8:
227                 return -7;
228         case 0xf0:
229                 return 7;
230         case 0xf8:
231                 return -8;
232         }
233
234         *bitfill -= 1;
235         switch (cur_byte & 0xfc) {
236         case 0x40:
237                 return 8;
238         case 0x44:
239                 return -9;
240         case 0x48:
241                 return 9;
242         case 0x4c:
243                 return -10;
244         case 0x50:
245                 return 10;
246         case 0x54:
247                 return -11;
248         case 0x58:
249                 return 11;
250         case 0x5c:
251                 return -12;
252         case 0x60:
253                 return 12;
254         case 0x64:
255                 return -13;
256         case 0x68:
257                 return 13;
258         case 0x6c:
259                 return -14;
260         case 0x70:
261                 return 14;
262         case 0x74:
263                 return -15;
264         case 0x78:
265                 return 15;
266         case 0x7c:
267                 return -16;
268         }
269
270         *bitfill -= 1;
271         switch (cur_byte & 0xfe) {
272         case 0x20:
273                 return 16;
274         case 0x22:
275                 return -17;
276         case 0x24:
277                 return 17;
278         case 0x26:
279                 return -18;
280         case 0x28:
281                 return 18;
282         case 0x2a:
283                 return -19;
284         case 0x2c:
285                 return 19;
286         }
287
288         *bitfill += 7;
289         return 0xff;
290 }
291
292 static int spca561_decode(int width, int height,
293                                    const unsigned char *inbuf,
294                                    unsigned char *outbuf)
295 {
296         /* buffers */
297         static int accum[8 * 8 * 8];
298         static int i_hits[8 * 8 * 8];
299
300         const int nbits_A[] =
301             { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
302                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
303                     1, 1,
304                 1, 1, 1, 1, 1,
305                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
306                     1, 1,
307                 1, 1, 1, 1, 1,
308                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
309                     1, 1,
310                 1, 1, 1, 1, 1,
311                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
312                     1, 1,
313                 1, 1, 1, 1, 1,
314                 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 7, 7,
315                     7, 7,
316                 7, 7, 7, 7, 7,
317                 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
318                     3, 3,
319                 3, 3, 3, 3, 3,
320                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
321                     5, 5,
322                 5, 5, 5, 5, 5,
323                 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3,
324                     3, 3,
325                 3, 3, 3, 3, 3,
326                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
327         };
328         const int tab_A[] =
329             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
330                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
331                     0, 0,
332                 0, 0, 0, 0,
333                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
334                     0, 0,
335                 0, 0, 0, 0,
336                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
337                     0, 0,
338                 0, 0, 0, 0,
339                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
340                     0, 0,
341                 0, 0, 0, 0,
342                 0, 0, 0, 0, 11, -11, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
343                     10, 10,
344                 255, 254, -4,
345                 -4, -5, -5, -6, -6, -7, -7, -8, -8, -9, -9, -10, -10, -1,
346                     -1, -1,
347                 -1, -1, -1,
348                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
349                     -1, -1,
350                 -1, -1,
351                 -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
352                     3, 3, 3,
353                 3, 3, 3,
354                 -2, -2, -2, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -3, -3,
355                     -3, 1,
356                 1, 1, 1, 1,
357                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
358                     1, 1,
359                 1, 1, 1, 1,
360                 1
361         };
362
363         const int nbits_B[] =
364             { 0, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
365                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
366                     3, 3,
367                 3, 3, 3, 3, 3,
368                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2,
369                     2, 2,
370                 2, 2, 2, 2, 2,
371                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
372                     2, 2,
373                 2, 2, 2, 2, 2,
374                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
375                     2, 2,
376                 2, 2, 2, 2, 2,
377                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
378                     1, 1,
379                 1, 1, 1, 1, 1,
380                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
381                     1, 1,
382                 1, 1, 1, 1, 1,
383                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
384                     1, 1,
385                 1, 1, 1, 1, 1,
386                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
387                     1, 1,
388                 1, 1, 1, 1, 1,
389                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
390         };
391         const int tab_B[] =
392             { 0xff, -4, 3, 3, -3, -3, -3, -3, 2, 2, 2, 2, 2, 2, 2, 2, -2,
393                 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
394                     1, 1,
395                 1, 1, 1, 1,
396                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
397                     1, 1,
398                 1, 1, 1, 1,
399                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
400                     -1, -1,
401                 -1, -1,
402                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
403                     -1, -1,
404                 -1, -1,
405                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
406                     -1, -1,
407                 -1, -1,
408                 -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409                     0, 0, 0,
410                 0, 0, 0, 0,
411                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
412                     0, 0,
413                 0, 0, 0, 0,
414                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
415                     0, 0,
416                 0, 0, 0, 0,
417                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
418                     0, 0,
419                 0, 0, 0, 0,
420                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
421                     0, 0,
422                 0, 0, 0, 0,
423                 0, 0, 0, 0, 0, 0, 0,
424         };
425
426         const int nbits_C[] =
427             { 0, 0, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5,
428                 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
429                     4, 4,
430                 4, 4, 4, 4, 4,
431                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3,
432                     3, 3,
433                 3, 3, 3, 3, 3,
434                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
435                     3, 3,
436                 3, 3, 3, 3, 3,
437                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
438                     3, 3,
439                 3, 3, 3, 3, 3,
440                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
441                     2, 2,
442                 2, 2, 2, 2, 2,
443                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
444                     2, 2,
445                 2, 2, 2, 2, 2,
446                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
447                     2, 2,
448                 2, 2, 2, 2, 2,
449                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
450                     2, 2,
451                 2, 2, 2, 2, 2,
452                 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
453         };
454         const int tab_C[] =
455             { 0xff, 0xfe, 6, -7, 5, 5, -6, -6, 4, 4, 4, 4, -5, -5, -5, -5,
456                 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4, -4, 2,
457                     2, 2, 2,
458                 2, 2, 2,
459                 2, 2, 2, 2, 2, 2, 2, 2, 2, -3, -3, -3, -3, -3, -3, -3, -3,
460                     -3, -3,
461                 -3, -3, -3,
462                 -3, -3, -3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
463                     1, 1,
464                 1, 1, 1, 1,
465                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2,
466                     -2, -2,
467                 -2, -2, -2,
468                 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
469                     -2, -2,
470                 -2, -2,
471                 -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
472                     0, 0, 0,
473                 0, 0, 0, 0,
474                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
475                     0, 0,
476                 0, 0, 0, 0,
477                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1,
478                     -1, -1,
479                 -1, -1, -1,
480                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
481                     -1, -1,
482                 -1, -1,
483                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
484                     -1, -1,
485                 -1, -1,
486                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
487                     -1,
488         };
489
490         const int nbits_D[] =
491             { 0, 0, 0, 0, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6,
492                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5,
493                     5, 5,
494                 5, 5, 5, 5, 5,
495                 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4,
496                     4, 4,
497                 4, 4, 4, 4, 4,
498                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
499                     4, 4,
500                 4, 4, 4, 4, 4,
501                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
502                     4, 4,
503                 4, 4, 4, 4, 4,
504                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
505                     3, 3,
506                 3, 3, 3, 3, 3,
507                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
508                     3, 3,
509                 3, 3, 3, 3, 3,
510                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
511                     3, 3,
512                 3, 3, 3, 3, 3,
513                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
514                     3, 3,
515                 3, 3, 3, 3, 3,
516                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
517         };
518         const int tab_D[] =
519             { 0xff, 0xfe, 0xfd, 0xfc, 10, -11, 11, -12, 8, 8, -9, -9, 9, 9,
520                 -10, -10, 6, 6, 6, 6, -7, -7, -7, -7, 7, 7, 7, 7, -8, -8,
521                     -8, -8,
522                 4, 4, 4, 4,
523                 4, 4, 4, 4, -5, -5, -5, -5, -5, -5, -5, -5, 5, 5, 5, 5, 5,
524                     5, 5, 5,
525                 -6, -6,
526                 -6, -6, -6, -6, -6, -6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
527                     2, 2,
528                 2, 2, -3,
529                 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
530                     3, 3,
531                 3, 3, 3, 3,
532                 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4,
533                     -4, -4,
534                 -4, -4, -4,
535                 -4, -4, -4, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
536                     0, 0, 0,
537                 0, 0, 0, 0,
538                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
539                     -1, -1,
540                 -1, -1, -1,
541                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
542                     -1, -1,
543                 -1, -1,
544                 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
545                     1, 1, 1,
546                 1, 1, 1,
547                 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2, -2,
548                     -2, -2,
549                 -2, -2, -2,
550                 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
551                     -2, -2,
552                 -2, -2
553         };
554
555         /* a_curve[19 + i] = ... [-19..19] => [-160..160] */
556         const int a_curve[] =
557             { -160, -144, -128, -112, -98, -88, -80, -72, -64, -56, -48,
558                 -40, -32, -24, -18, -12, -8, -5, -2, 0, 2, 5, 8, 12, 18,
559                     24, 32,
560                 40, 48, 56, 64,
561                 72, 80, 88, 98, 112, 128, 144, 160
562         };
563         /* clamp0_255[256 + i] = min(max(i,255),0) */
564         const unsigned char clamp0_255[] =
565             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
566                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
567                     0, 0,
568                 0, 0, 0, 0, 0, 0, 0, 0, 0,
569                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
570                     0, 0,
571                 0, 0, 0, 0, 0, 0, 0, 0, 0,
572                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
573                     0, 0,
574                 0, 0, 0, 0, 0, 0, 0, 0, 0,
575                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
576                     0, 0,
577                 0, 0, 0, 0, 0, 0, 0, 0, 0,
578                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
579                     0, 0,
580                 0, 0, 0, 0, 0, 0, 0, 0, 0,
581                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
582                     0, 0,
583                 0, 0, 0, 0, 0, 0, 0, 0, 0,
584                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
585                     0, 0,
586                 0, 0, 0, 0, 0, 0, 0, 0, 0,
587                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
588                     1, 2,
589                 3, 4, 5, 6, 7, 8, 9, 10,
590                 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
591                     26, 27,
592                 28, 29, 30, 31, 32, 33,
593                 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
594                     49, 50,
595                 51, 52, 53, 54, 55, 56,
596                 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
597                     72, 73,
598                 74, 75, 76, 77, 78, 79,
599                 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
600                     95, 96,
601                 97, 98, 99, 100, 101,
602                 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
603                     114,
604                 115, 116, 117, 118, 119,
605                 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
606                     132,
607                 133, 134, 135, 136, 137,
608                 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
609                     150,
610                 151, 152, 153, 154, 155,
611                 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
612                     168,
613                 169, 170, 171, 172, 173,
614                 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
615                     186,
616                 187, 188, 189, 190, 191,
617                 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
618                     204,
619                 205, 206, 207, 208, 209,
620                 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
621                     222,
622                 223, 224, 225, 226, 227,
623                 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
624                     240,
625                 241, 242, 243, 244, 245,
626                 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 255, 255,
627                     255,
628                 255, 255, 255, 255, 255,
629                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
630                     255,
631                 255, 255, 255, 255, 255,
632                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
633                     255,
634                 255, 255, 255, 255, 255,
635                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
636                     255,
637                 255, 255, 255, 255, 255,
638                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
639                     255,
640                 255, 255, 255, 255, 255,
641                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
642                     255,
643                 255, 255, 255, 255, 255,
644                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
645                     255,
646                 255, 255, 255, 255, 255,
647                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
648                     255,
649                 255, 255, 255, 255, 255,
650                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
651                     255,
652                 255, 255, 255, 255, 255,
653                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
654                     255,
655                 255, 255, 255, 255, 255,
656                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
657                     255,
658                 255, 255, 255, 255, 255,
659                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
660                     255,
661                 255, 255, 255, 255, 255,
662                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
663                     255,
664                 255, 255, 255, 255, 255,
665                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
666                     255,
667                 255, 255, 255, 255, 255,
668                 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
669                     255,
670                 255
671         };
672         /* abs_clamp15[19 + i] = min(abs(i), 15) */
673         const int abs_clamp15[] =
674             { 15, 15, 15, 15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3,
675                 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
676                     15, 15,
677                 15
678         };
679         /* diff_encoding[256 + i] = ... */
680         const int diff_encoding[] =
681             { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
682                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
683                     7, 7,
684                 7, 7, 7, 7, 7, 7, 7,
685                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
686                     7, 7,
687                 7, 7, 7, 7, 7, 7, 7,
688                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
689                     7, 7,
690                 7, 7, 7, 7, 7, 7, 7,
691                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
692                     7, 7,
693                 7, 7, 7, 7, 7, 7, 7,
694                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
695                     7, 7,
696                 7, 7, 7, 7, 7, 7, 7,
697                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
698                     7, 7,
699                 7, 7, 7, 7, 7, 7, 7,
700                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
701                     7, 7,
702                 7, 7, 7, 7, 7, 7, 7,
703                 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5,
704                     5, 5,
705                 5, 5, 5, 5, 5, 3, 3,
706                 3, 3, 1, 1, 0, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6,
707                     6, 6,
708                 6, 6, 6, 6, 6, 6, 6,
709                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
710                     6, 6,
711                 6, 6, 6, 6, 6, 6, 6,
712                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
713                     6, 6,
714                 6, 6, 6, 6, 6, 6, 6,
715                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
716                     6, 6,
717                 6, 6, 6, 6, 6, 6, 6,
718                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
719                     6, 6,
720                 6, 6, 6, 6, 6, 6, 6,
721                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
722                     6, 6,
723                 6, 6, 6, 6, 6, 6, 6,
724                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
725                     6, 6,
726                 6, 6, 6, 6, 6, 6, 6,
727                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
728                     6, 6,
729                 6, 6, 6, 6, 6, 6, 6,
730                 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
731                     6, 6,
732                 6, 6, 6, 6, 6, 6
733         };
734
735         int block;
736         int bitfill = 0;
737         int xwidth = width + 6;
738         int off_up_right = 2 - 2 * xwidth;
739         int off_up_left = -2 - 2 * xwidth;
740         int pixel_U = 0, saved_pixel_UR = 0;
741         int pixel_x = 0, pixel_y = 2;
742         unsigned char *output_ptr = outbuf;
743
744         memset(i_hits, 0, sizeof(i_hits));
745         memset(accum, 0, sizeof(accum));
746
747         memcpy(outbuf + xwidth * 2 + 3, inbuf + 0x14, width);
748         memcpy(outbuf + xwidth * 3 + 3, inbuf + 0x14 + width, width);
749
750         input_ptr = inbuf + 0x14 + width * 2;
751         output_ptr = outbuf + (xwidth) * 4 + 3;
752
753         bit_bucket = 0;
754
755         for (block = 0; block < ((height - 2) * width) / 32; ++block) {
756                 int b_it, var_7 = 0;
757                 int cur_byte;
758
759                 refill(&bitfill);
760
761                 cur_byte = (bit_bucket >> (bitfill & 7)) & 0xff;
762
763                 if ((cur_byte & 0x80) == 0) {
764                         var_7 = 0;
765                         bitfill--;
766                 } else if ((cur_byte & 0xC0) == 0x80) {
767                         var_7 = 1;
768                         bitfill -= 2;
769                 } else if ((cur_byte & 0xc0) == 0xc0) {
770                         var_7 = 2;
771                         bitfill -= 2;
772                 }
773
774                 for (b_it = 0; b_it < 32; b_it++) {
775                         int index;
776                         int pixel_L, pixel_UR, pixel_UL;
777                         int multiplier;
778                         int dL, dC, dR;
779                         int gkw;        /* God knows what */
780
781                         refill(&bitfill);
782                         cur_byte = bit_bucket >> (bitfill & 7) & 0xff;
783
784                         pixel_L = output_ptr[-2];
785                         pixel_UR = output_ptr[off_up_right];
786                         pixel_UL = output_ptr[off_up_left];
787
788                         dL = diff_encoding[0x100 + pixel_UL - pixel_L];
789                         dC = diff_encoding[0x100 + pixel_U - pixel_UL];
790                         dR = diff_encoding[0x100 + pixel_UR - pixel_U];
791
792                         if (pixel_x < 2) {
793                                 pixel_L = pixel_UL = pixel_U =
794                                     output_ptr[-xwidth * 2];
795                                 pixel_UR = output_ptr[off_up_right];
796                                 dL = dC = 0;
797                                 dR = diff_encoding[0x100 + pixel_UR -
798                                                    pixel_U];
799                         } else if (pixel_x > width - 3)
800                                 dR = 0;
801
802                         multiplier = 4;
803                         index = dR + dC * 8 + dL * 64;
804
805                         if (pixel_L + pixel_U * 2 <= 144
806                             && (pixel_y & 1) == 0
807                             && (b_it & 3) == 0 && (dR < 5) && (dC < 5)
808                             && (dL < 5)) {
809                                 multiplier = 1;
810                         } else if (pixel_L <= 48
811                                    && dL <= 4 && dC <= 4 && dL >= 1
812                                    && dC >= 1) {
813                                 multiplier = 2;
814                         } else if (var_7 == 1) {
815                                 multiplier = 2;
816                         } else if (dC + dL >= 11 || var_7 == 2) {
817                                 multiplier = 8;
818                         }
819
820                         if (i_hits[index] < 7) {
821                                 bitfill -= nbits_A[cur_byte];
822                                 gkw = tab_A[cur_byte];
823                                 if (gkw == 0xfe)
824                                         gkw = fun_A(&bitfill);
825                         } else if (i_hits[index] >= accum[index]) {
826                                 bitfill -= nbits_B[cur_byte];
827                                 gkw = tab_B[cur_byte];
828                                 if (cur_byte == 0)
829                                         gkw = fun_B(&bitfill);
830                         } else if (i_hits[index] * 2 >= accum[index]) {
831                                 bitfill -= nbits_C[cur_byte];
832                                 gkw = tab_C[cur_byte];
833                                 if (cur_byte < 2)
834                                         gkw = fun_C(&bitfill, gkw);
835                         } else if (i_hits[index] * 4 >= accum[index]) {
836                                 bitfill -= nbits_D[cur_byte];
837                                 gkw = tab_D[cur_byte];
838                                 if (cur_byte < 4)
839                                         gkw = fun_D(&bitfill, gkw);
840                         } else if (i_hits[index] * 8 >= accum[index]) {
841                                 gkw = fun_E(cur_byte, &bitfill);
842                         } else {
843                                 gkw = fun_F(cur_byte, &bitfill);
844                         }
845
846                         if (gkw == 0xff)
847                                 return -3;
848
849                         {
850                                 int tmp1, tmp2;
851
852                                 tmp1 =
853                                     (pixel_U + pixel_L) * 3 - pixel_UL * 2;
854                                 tmp1 += (tmp1 < 0) ? 3 : 0;
855                                 tmp2 = a_curve[19 + gkw] * multiplier;
856                                 tmp2 += (tmp2 < 0) ? 1 : 0;
857
858                                 *(output_ptr++) =
859                                     clamp0_255[0x100 + (tmp1 >> 2) -
860                                                (tmp2 >> 1)];
861                         }
862                         pixel_U = saved_pixel_UR;
863                         saved_pixel_UR = pixel_UR;
864
865                         if (++pixel_x == width) {
866                                 output_ptr += 6;
867                                 pixel_x = 0;
868                                 pixel_y++;
869                         }
870
871                         accum[index] += abs_clamp15[19 + gkw];
872
873                         if (i_hits[index]++ == 15) {
874                                 i_hits[index] = 8;
875                                 accum[index] /= 2;
876                         }
877                 }
878         }
879         return 0;
880 }
881
882 /* FIXME, change spca561_decode not to need the extra border
883    around its dest buffer */
884 int fswc_add_image_s561(avgbmp_t *dst, uint8_t *img, uint32_t length, uint32_t width, uint32_t height, int palette)
885 {
886         uint x, y;
887         uint8_t *s, *d;
888         unsigned char tmpimg[650 * 490];
889         
890         if(spca561_decode(width, height, img, tmpimg) != 0)
891         {
892                 return(-1);
893         }
894         
895         /* Remove buffer border */
896         d = tmpimg;
897         s = tmpimg + 2 * (width + 6) + 3;
898         for(y = 0; y < height; y++)
899         {
900                 for(x = 0; x < width; x++) *(d++) = *(s++);
901                 s += 6;
902         }
903         
904         fswc_add_image_bayer(dst, tmpimg, width * height, width, height, SRC_PAL_SGBRG8);
905         
906         return(0);
907 }
908