GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / pwc / pwc-dec23.c
blob79333603d8bb36709b74882a2c21c1aadac8c433
1 /* Linux driver for Philips webcam
2 Decompression for chipset version 2 et 3
3 (C) 2004-2006 Luc Saillard (luc@saillard.org)
5 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
6 driver and thus may have bugs that are not present in the original version.
7 Please send bug reports and support requests to <luc@saillard.org>.
8 The decompression routines have been implemented by reverse-engineering the
9 Nemosoft binary pwcx module. Caveat emptor.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "pwc-timon.h"
28 #include "pwc-kiara.h"
29 #include "pwc-dec23.h"
30 #include <media/pwc-ioctl.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
36 * USE_LOOKUP_TABLE_TO_CLAMP
37 * 0: use a C version of this tests: { a<0?0:(a>255?255:a) }
38 * 1: use a faster lookup table for cpu with a big cache (intel)
40 #define USE_LOOKUP_TABLE_TO_CLAMP 1
42 * UNROLL_LOOP_FOR_COPYING_BLOCK
43 * 0: use a loop for a smaller code (but little slower)
44 * 1: when unrolling the loop, gcc produces some faster code (perhaps only
45 * valid for intel processor class). Activating this option, automaticaly
46 * activate USE_LOOKUP_TABLE_TO_CLAMP
48 #define UNROLL_LOOP_FOR_COPY 1
49 #if UNROLL_LOOP_FOR_COPY
50 # undef USE_LOOKUP_TABLE_TO_CLAMP
51 # define USE_LOOKUP_TABLE_TO_CLAMP 1
52 #endif
55 * ENABLE_BAYER_DECODER
56 * 0: bayer decoder is not build (save some space)
57 * 1: bayer decoder is build and can be used
59 #define ENABLE_BAYER_DECODER 0
61 static void build_subblock_pattern(struct pwc_dec23_private *pdec)
63 static const unsigned int initial_values[12] = {
64 -0x526500, -0x221200, 0x221200, 0x526500,
65 -0x3de200, 0x3de200,
66 -0x6db480, -0x2d5d00, 0x2d5d00, 0x6db480,
67 -0x12c200, 0x12c200
70 static const unsigned int values_derivated[12] = {
71 0xa4ca, 0x4424, -0x4424, -0xa4ca,
72 0x7bc4, -0x7bc4,
73 0xdb69, 0x5aba, -0x5aba, -0xdb69,
74 0x2584, -0x2584
76 unsigned int temp_values[12];
77 int i, j;
79 memcpy(temp_values, initial_values, sizeof(initial_values));
80 for (i = 0; i < 256; i++) {
81 for (j = 0; j < 12; j++) {
82 pdec->table_subblock[i][j] = temp_values[j];
83 temp_values[j] += values_derivated[j];
88 static void build_bit_powermask_table(struct pwc_dec23_private *pdec)
90 unsigned char *p;
91 unsigned int bit, byte, mask, val;
92 unsigned int bitpower = 1;
94 for (bit = 0; bit < 8; bit++) {
95 mask = bitpower - 1;
96 p = pdec->table_bitpowermask[bit];
97 for (byte = 0; byte < 256; byte++) {
98 val = (byte & mask);
99 if (byte & bitpower)
100 val = -val;
101 *p++ = val;
103 bitpower<<=1;
108 static void build_table_color(const unsigned int romtable[16][8],
109 unsigned char p0004[16][1024],
110 unsigned char p8004[16][256])
112 int compression_mode, j, k, bit, pw;
113 unsigned char *p0, *p8;
114 const unsigned int *r;
116 /* We have 16 compressions tables */
117 for (compression_mode = 0; compression_mode < 16; compression_mode++) {
118 p0 = p0004[compression_mode];
119 p8 = p8004[compression_mode];
120 r = romtable[compression_mode];
122 for (j = 0; j < 8; j++, r++, p0 += 128) {
124 for (k = 0; k < 16; k++) {
125 if (k == 0)
126 bit = 1;
127 else if (k >= 1 && k < 3)
128 bit = (r[0] >> 15) & 7;
129 else if (k >= 3 && k < 6)
130 bit = (r[0] >> 12) & 7;
131 else if (k >= 6 && k < 10)
132 bit = (r[0] >> 9) & 7;
133 else if (k >= 10 && k < 13)
134 bit = (r[0] >> 6) & 7;
135 else if (k >= 13 && k < 15)
136 bit = (r[0] >> 3) & 7;
137 else
138 bit = (r[0]) & 7;
139 if (k == 0)
140 *p8++ = 8;
141 else
142 *p8++ = j - bit;
143 *p8++ = bit;
145 pw = 1 << bit;
146 p0[k + 0x00] = (1 * pw) + 0x80;
147 p0[k + 0x10] = (2 * pw) + 0x80;
148 p0[k + 0x20] = (3 * pw) + 0x80;
149 p0[k + 0x30] = (4 * pw) + 0x80;
150 p0[k + 0x40] = (-1 * pw) + 0x80;
151 p0[k + 0x50] = (-2 * pw) + 0x80;
152 p0[k + 0x60] = (-3 * pw) + 0x80;
153 p0[k + 0x70] = (-4 * pw) + 0x80;
154 } /* end of for (k=0; k<16; k++, p8++) */
155 } /* end of for (j=0; j<8; j++ , table++) */
156 } /* end of foreach compression_mode */
162 static void fill_table_dc00_d800(struct pwc_dec23_private *pdec)
164 #define SCALEBITS 15
165 #define ONE_HALF (1UL << (SCALEBITS - 1))
166 int i;
167 unsigned int offset1 = ONE_HALF;
168 unsigned int offset2 = 0x0000;
170 for (i=0; i<256; i++) {
171 pdec->table_dc00[i] = offset1 & ~(ONE_HALF);
172 pdec->table_d800[i] = offset2;
174 offset1 += 0x7bc4;
175 offset2 += 0x7bc4;
179 static const unsigned char hash_table_ops[64*4] = {
180 0x02, 0x00, 0x00, 0x00,
181 0x00, 0x03, 0x01, 0x00,
182 0x00, 0x04, 0x01, 0x10,
183 0x00, 0x06, 0x01, 0x30,
184 0x02, 0x00, 0x00, 0x00,
185 0x00, 0x03, 0x01, 0x40,
186 0x00, 0x05, 0x01, 0x20,
187 0x01, 0x00, 0x00, 0x00,
188 0x02, 0x00, 0x00, 0x00,
189 0x00, 0x03, 0x01, 0x00,
190 0x00, 0x04, 0x01, 0x50,
191 0x00, 0x05, 0x02, 0x00,
192 0x02, 0x00, 0x00, 0x00,
193 0x00, 0x03, 0x01, 0x40,
194 0x00, 0x05, 0x03, 0x00,
195 0x01, 0x00, 0x00, 0x00,
196 0x02, 0x00, 0x00, 0x00,
197 0x00, 0x03, 0x01, 0x00,
198 0x00, 0x04, 0x01, 0x10,
199 0x00, 0x06, 0x02, 0x10,
200 0x02, 0x00, 0x00, 0x00,
201 0x00, 0x03, 0x01, 0x40,
202 0x00, 0x05, 0x01, 0x60,
203 0x01, 0x00, 0x00, 0x00,
204 0x02, 0x00, 0x00, 0x00,
205 0x00, 0x03, 0x01, 0x00,
206 0x00, 0x04, 0x01, 0x50,
207 0x00, 0x05, 0x02, 0x40,
208 0x02, 0x00, 0x00, 0x00,
209 0x00, 0x03, 0x01, 0x40,
210 0x00, 0x05, 0x03, 0x40,
211 0x01, 0x00, 0x00, 0x00,
212 0x02, 0x00, 0x00, 0x00,
213 0x00, 0x03, 0x01, 0x00,
214 0x00, 0x04, 0x01, 0x10,
215 0x00, 0x06, 0x01, 0x70,
216 0x02, 0x00, 0x00, 0x00,
217 0x00, 0x03, 0x01, 0x40,
218 0x00, 0x05, 0x01, 0x20,
219 0x01, 0x00, 0x00, 0x00,
220 0x02, 0x00, 0x00, 0x00,
221 0x00, 0x03, 0x01, 0x00,
222 0x00, 0x04, 0x01, 0x50,
223 0x00, 0x05, 0x02, 0x00,
224 0x02, 0x00, 0x00, 0x00,
225 0x00, 0x03, 0x01, 0x40,
226 0x00, 0x05, 0x03, 0x00,
227 0x01, 0x00, 0x00, 0x00,
228 0x02, 0x00, 0x00, 0x00,
229 0x00, 0x03, 0x01, 0x00,
230 0x00, 0x04, 0x01, 0x10,
231 0x00, 0x06, 0x02, 0x50,
232 0x02, 0x00, 0x00, 0x00,
233 0x00, 0x03, 0x01, 0x40,
234 0x00, 0x05, 0x01, 0x60,
235 0x01, 0x00, 0x00, 0x00,
236 0x02, 0x00, 0x00, 0x00,
237 0x00, 0x03, 0x01, 0x00,
238 0x00, 0x04, 0x01, 0x50,
239 0x00, 0x05, 0x02, 0x40,
240 0x02, 0x00, 0x00, 0x00,
241 0x00, 0x03, 0x01, 0x40,
242 0x00, 0x05, 0x03, 0x40,
243 0x01, 0x00, 0x00, 0x00
249 static const unsigned int MulIdx[16][16] = {
250 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
251 {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3,},
252 {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,},
253 {4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,},
254 {6, 7, 8, 9, 7, 10, 11, 8, 8, 11, 10, 7, 9, 8, 7, 6,},
255 {4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4,},
256 {1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2,},
257 {0, 3, 3, 0, 1, 2, 2, 1, 2, 1, 1, 2, 3, 0, 0, 3,},
258 {0, 1, 2, 3, 3, 2, 1, 0, 3, 2, 1, 0, 0, 1, 2, 3,},
259 {1, 1, 1, 1, 3, 3, 3, 3, 0, 0, 0, 0, 2, 2, 2, 2,},
260 {7, 10, 11, 8, 9, 8, 7, 6, 6, 7, 8, 9, 8, 11, 10, 7,},
261 {4, 5, 5, 4, 5, 4, 4, 5, 5, 4, 4, 5, 4, 5, 5, 4,},
262 {7, 9, 6, 8, 10, 8, 7, 11, 11, 7, 8, 10, 8, 6, 9, 7,},
263 {1, 3, 0, 2, 2, 0, 3, 1, 2, 0, 3, 1, 1, 3, 0, 2,},
264 {1, 2, 2, 1, 3, 0, 0, 3, 0, 3, 3, 0, 2, 1, 1, 2,},
265 {10, 8, 7, 11, 8, 6, 9, 7, 7, 9, 6, 8, 11, 7, 8, 10}
268 #if USE_LOOKUP_TABLE_TO_CLAMP
269 #define MAX_OUTER_CROP_VALUE (512)
270 static unsigned char pwc_crop_table[256 + 2*MAX_OUTER_CROP_VALUE];
271 #define CLAMP(x) (pwc_crop_table[MAX_OUTER_CROP_VALUE+(x)])
272 #else
273 #define CLAMP(x) ((x)>255?255:((x)<0?0:x))
274 #endif
277 /* If the type or the command change, we rebuild the lookup table */
278 int pwc_dec23_init(struct pwc_device *pwc, int type, unsigned char *cmd)
280 int flags, version, shift, i;
281 struct pwc_dec23_private *pdec;
283 if (pwc->decompress_data == NULL) {
284 pdec = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
285 if (pdec == NULL)
286 return -ENOMEM;
287 pwc->decompress_data = pdec;
289 pdec = pwc->decompress_data;
291 if (DEVICE_USE_CODEC3(type)) {
292 flags = cmd[2] & 0x18;
293 if (flags == 8)
294 pdec->nbits = 7; /* More bits, mean more bits to encode the stream, but better quality */
295 else if (flags == 0x10)
296 pdec->nbits = 8;
297 else
298 pdec->nbits = 6;
300 version = cmd[2] >> 5;
301 build_table_color(KiaraRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
302 build_table_color(KiaraRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
304 } else {
306 flags = cmd[2] & 6;
307 if (flags == 2)
308 pdec->nbits = 7;
309 else if (flags == 4)
310 pdec->nbits = 8;
311 else
312 pdec->nbits = 6;
314 version = cmd[2] >> 3;
315 build_table_color(TimonRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
316 build_table_color(TimonRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
319 /* Informations can be coded on a variable number of bits but never less than 8 */
320 shift = 8 - pdec->nbits;
321 pdec->scalebits = SCALEBITS - shift;
322 pdec->nbitsmask = 0xFF >> shift;
324 fill_table_dc00_d800(pdec);
325 build_subblock_pattern(pdec);
326 build_bit_powermask_table(pdec);
328 #if USE_LOOKUP_TABLE_TO_CLAMP
329 /* Build the static table to clamp value [0-255] */
330 for (i=0;i<MAX_OUTER_CROP_VALUE;i++)
331 pwc_crop_table[i] = 0;
332 for (i=0; i<256; i++)
333 pwc_crop_table[MAX_OUTER_CROP_VALUE+i] = i;
334 for (i=0; i<MAX_OUTER_CROP_VALUE; i++)
335 pwc_crop_table[MAX_OUTER_CROP_VALUE+256+i] = 255;
336 #endif
338 return 0;
342 * Copy the 4x4 image block to Y plane buffer
344 static void copy_image_block_Y(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
346 #if UNROLL_LOOP_FOR_COPY
347 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
348 const int *c = src;
349 unsigned char *d = dst;
351 *d++ = cm[c[0] >> scalebits];
352 *d++ = cm[c[1] >> scalebits];
353 *d++ = cm[c[2] >> scalebits];
354 *d++ = cm[c[3] >> scalebits];
356 d = dst + bytes_per_line;
357 *d++ = cm[c[4] >> scalebits];
358 *d++ = cm[c[5] >> scalebits];
359 *d++ = cm[c[6] >> scalebits];
360 *d++ = cm[c[7] >> scalebits];
362 d = dst + bytes_per_line*2;
363 *d++ = cm[c[8] >> scalebits];
364 *d++ = cm[c[9] >> scalebits];
365 *d++ = cm[c[10] >> scalebits];
366 *d++ = cm[c[11] >> scalebits];
368 d = dst + bytes_per_line*3;
369 *d++ = cm[c[12] >> scalebits];
370 *d++ = cm[c[13] >> scalebits];
371 *d++ = cm[c[14] >> scalebits];
372 *d++ = cm[c[15] >> scalebits];
373 #else
374 int i;
375 const int *c = src;
376 unsigned char *d = dst;
377 for (i = 0; i < 4; i++, c++)
378 *d++ = CLAMP((*c) >> scalebits);
380 d = dst + bytes_per_line;
381 for (i = 0; i < 4; i++, c++)
382 *d++ = CLAMP((*c) >> scalebits);
384 d = dst + bytes_per_line*2;
385 for (i = 0; i < 4; i++, c++)
386 *d++ = CLAMP((*c) >> scalebits);
388 d = dst + bytes_per_line*3;
389 for (i = 0; i < 4; i++, c++)
390 *d++ = CLAMP((*c) >> scalebits);
391 #endif
395 * Copy the 4x4 image block to a CrCb plane buffer
398 static void copy_image_block_CrCb(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
400 #if UNROLL_LOOP_FOR_COPY
401 /* Unroll all loops */
402 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
403 const int *c = src;
404 unsigned char *d = dst;
406 *d++ = cm[c[0] >> scalebits];
407 *d++ = cm[c[4] >> scalebits];
408 *d++ = cm[c[1] >> scalebits];
409 *d++ = cm[c[5] >> scalebits];
410 *d++ = cm[c[2] >> scalebits];
411 *d++ = cm[c[6] >> scalebits];
412 *d++ = cm[c[3] >> scalebits];
413 *d++ = cm[c[7] >> scalebits];
415 d = dst + bytes_per_line;
416 *d++ = cm[c[12] >> scalebits];
417 *d++ = cm[c[8] >> scalebits];
418 *d++ = cm[c[13] >> scalebits];
419 *d++ = cm[c[9] >> scalebits];
420 *d++ = cm[c[14] >> scalebits];
421 *d++ = cm[c[10] >> scalebits];
422 *d++ = cm[c[15] >> scalebits];
423 *d++ = cm[c[11] >> scalebits];
424 #else
425 int i;
426 const int *c1 = src;
427 const int *c2 = src + 4;
428 unsigned char *d = dst;
430 for (i = 0; i < 4; i++, c1++, c2++) {
431 *d++ = CLAMP((*c1) >> scalebits);
432 *d++ = CLAMP((*c2) >> scalebits);
434 c1 = src + 12;
435 d = dst + bytes_per_line;
436 for (i = 0; i < 4; i++, c1++, c2++) {
437 *d++ = CLAMP((*c1) >> scalebits);
438 *d++ = CLAMP((*c2) >> scalebits);
440 #endif
443 #if ENABLE_BAYER_DECODER
445 * Format: 8x2 pixels
446 * . G . G . G . G . G . G . G
447 * . . . . . . . . . . . . . .
448 * . G . G . G . G . G . G . G
449 * . . . . . . . . . . . . . .
450 * or
451 * . . . . . . . . . . . . . .
452 * G . G . G . G . G . G . G .
453 * . . . . . . . . . . . . . .
454 * G . G . G . G . G . G . G .
456 static void copy_image_block_Green(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
458 #if UNROLL_LOOP_FOR_COPY
459 /* Unroll all loops */
460 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
461 unsigned char *d = dst;
462 const int *c = src;
464 d[0] = cm[c[0] >> scalebits];
465 d[2] = cm[c[1] >> scalebits];
466 d[4] = cm[c[2] >> scalebits];
467 d[6] = cm[c[3] >> scalebits];
468 d[8] = cm[c[4] >> scalebits];
469 d[10] = cm[c[5] >> scalebits];
470 d[12] = cm[c[6] >> scalebits];
471 d[14] = cm[c[7] >> scalebits];
473 d = dst + bytes_per_line;
474 d[0] = cm[c[8] >> scalebits];
475 d[2] = cm[c[9] >> scalebits];
476 d[4] = cm[c[10] >> scalebits];
477 d[6] = cm[c[11] >> scalebits];
478 d[8] = cm[c[12] >> scalebits];
479 d[10] = cm[c[13] >> scalebits];
480 d[12] = cm[c[14] >> scalebits];
481 d[14] = cm[c[15] >> scalebits];
482 #else
483 int i;
484 unsigned char *d;
485 const int *c = src;
487 d = dst;
488 for (i = 0; i < 8; i++, c++)
489 d[i*2] = CLAMP((*c) >> scalebits);
491 d = dst + bytes_per_line;
492 for (i = 0; i < 8; i++, c++)
493 d[i*2] = CLAMP((*c) >> scalebits);
494 #endif
496 #endif
498 #if ENABLE_BAYER_DECODER
500 * Format: 4x4 pixels
501 * R . R . R . R
502 * . B . B . B .
503 * R . R . R . R
504 * . B . B . B .
506 static void copy_image_block_RedBlue(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
508 #if UNROLL_LOOP_FOR_COPY
509 /* Unroll all loops */
510 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
511 unsigned char *d = dst;
512 const int *c = src;
514 d[0] = cm[c[0] >> scalebits];
515 d[2] = cm[c[1] >> scalebits];
516 d[4] = cm[c[2] >> scalebits];
517 d[6] = cm[c[3] >> scalebits];
519 d = dst + bytes_per_line;
520 d[1] = cm[c[4] >> scalebits];
521 d[3] = cm[c[5] >> scalebits];
522 d[5] = cm[c[6] >> scalebits];
523 d[7] = cm[c[7] >> scalebits];
525 d = dst + bytes_per_line*2;
526 d[0] = cm[c[8] >> scalebits];
527 d[2] = cm[c[9] >> scalebits];
528 d[4] = cm[c[10] >> scalebits];
529 d[6] = cm[c[11] >> scalebits];
531 d = dst + bytes_per_line*3;
532 d[1] = cm[c[12] >> scalebits];
533 d[3] = cm[c[13] >> scalebits];
534 d[5] = cm[c[14] >> scalebits];
535 d[7] = cm[c[15] >> scalebits];
536 #else
537 int i;
538 unsigned char *d;
539 const int *c = src;
541 d = dst;
542 for (i = 0; i < 4; i++, c++)
543 d[i*2] = CLAMP((*c) >> scalebits);
545 d = dst + bytes_per_line;
546 for (i = 0; i < 4; i++, c++)
547 d[i*2+1] = CLAMP((*c) >> scalebits);
549 d = dst + bytes_per_line*2;
550 for (i = 0; i < 4; i++, c++)
551 d[i*2] = CLAMP((*c) >> scalebits);
553 d = dst + bytes_per_line*3;
554 for (i = 0; i < 4; i++, c++)
555 d[i*2+1] = CLAMP((*c) >> scalebits);
556 #endif
558 #endif
561 * To manage the stream, we keep bits in a 32 bits register.
562 * fill_nbits(n): fill the reservoir with at least n bits
563 * skip_bits(n): discard n bits from the reservoir
564 * get_bits(n): fill the reservoir, returns the first n bits and discard the
565 * bits from the reservoir.
566 * __get_nbits(n): faster version of get_bits(n), but asumes that the reservoir
567 * contains at least n bits. bits returned is discarded.
569 #define fill_nbits(pdec, nbits_wanted) do { \
570 while (pdec->nbits_in_reservoir<(nbits_wanted)) \
572 pdec->reservoir |= (*(pdec->stream)++) << (pdec->nbits_in_reservoir); \
573 pdec->nbits_in_reservoir += 8; \
575 } while(0);
577 #define skip_nbits(pdec, nbits_to_skip) do { \
578 pdec->reservoir >>= (nbits_to_skip); \
579 pdec->nbits_in_reservoir -= (nbits_to_skip); \
580 } while(0);
582 #define get_nbits(pdec, nbits_wanted, result) do { \
583 fill_nbits(pdec, nbits_wanted); \
584 result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
585 skip_nbits(pdec, nbits_wanted); \
586 } while(0);
588 #define __get_nbits(pdec, nbits_wanted, result) do { \
589 result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
590 skip_nbits(pdec, nbits_wanted); \
591 } while(0);
593 #define look_nbits(pdec, nbits_wanted) \
594 ((pdec->reservoir) & ((1U<<(nbits_wanted))-1))
597 * Decode a 4x4 pixel block
599 static void decode_block(struct pwc_dec23_private *pdec,
600 const unsigned char *ptable0004,
601 const unsigned char *ptable8004)
603 unsigned int primary_color;
604 unsigned int channel_v, offset1, op;
605 int i;
607 fill_nbits(pdec, 16);
608 __get_nbits(pdec, pdec->nbits, primary_color);
610 if (look_nbits(pdec,2) == 0) {
611 skip_nbits(pdec, 2);
612 /* Very simple, the color is the same for all pixels of the square */
613 for (i = 0; i < 16; i++)
614 pdec->temp_colors[i] = pdec->table_dc00[primary_color];
616 return;
619 /* This block is encoded with small pattern */
620 for (i = 0; i < 16; i++)
621 pdec->temp_colors[i] = pdec->table_d800[primary_color];
623 __get_nbits(pdec, 3, channel_v);
624 channel_v = ((channel_v & 1) << 2) | (channel_v & 2) | ((channel_v & 4) >> 2);
626 ptable0004 += (channel_v * 128);
627 ptable8004 += (channel_v * 32);
629 offset1 = 0;
632 unsigned int htable_idx, rows = 0;
633 const unsigned int *block;
635 /* [ zzzz y x x ]
636 * xx == 00 :=> end of the block def, remove the two bits from the stream
637 * yxx == 111
638 * yxx == any other value
641 fill_nbits(pdec, 16);
642 htable_idx = look_nbits(pdec, 6);
643 op = hash_table_ops[htable_idx * 4];
645 if (op == 2) {
646 skip_nbits(pdec, 2);
648 } else if (op == 1) {
649 unsigned int mask, shift;
650 unsigned int nbits, col1;
651 unsigned int yyyy;
653 skip_nbits(pdec, 3);
654 /* offset1 += yyyy */
655 __get_nbits(pdec, 4, yyyy);
656 offset1 += 1 + yyyy;
657 offset1 &= 0x0F;
658 nbits = ptable8004[offset1 * 2];
660 __get_nbits(pdec, nbits+1, col1);
662 /* Bit mask table */
663 mask = pdec->table_bitpowermask[nbits][col1];
664 shift = ptable8004[offset1 * 2 + 1];
665 rows = ((mask << shift) + 0x80) & 0xFF;
667 block = pdec->table_subblock[rows];
668 for (i = 0; i < 16; i++)
669 pdec->temp_colors[i] += block[MulIdx[offset1][i]];
671 } else {
672 /* op == 0
673 * offset1 is coded on 3 bits
675 unsigned int shift;
677 offset1 += hash_table_ops [htable_idx * 4 + 2];
678 offset1 &= 0x0F;
680 rows = ptable0004[offset1 + hash_table_ops [htable_idx * 4 + 3]];
681 block = pdec->table_subblock[rows];
682 for (i = 0; i < 16; i++)
683 pdec->temp_colors[i] += block[MulIdx[offset1][i]];
685 shift = hash_table_ops[htable_idx * 4 + 1];
686 skip_nbits(pdec, shift);
689 } while (op != 2);
693 static void DecompressBand23(struct pwc_dec23_private *pdec,
694 const unsigned char *rawyuv,
695 unsigned char *planar_y,
696 unsigned char *planar_u,
697 unsigned char *planar_v,
698 unsigned int compressed_image_width,
699 unsigned int real_image_width)
701 int compression_index, nblocks;
702 const unsigned char *ptable0004;
703 const unsigned char *ptable8004;
705 pdec->reservoir = 0;
706 pdec->nbits_in_reservoir = 0;
707 pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */
709 get_nbits(pdec, 4, compression_index);
711 /* pass 1: uncompress Y component */
712 nblocks = compressed_image_width / 4;
714 ptable0004 = pdec->table_0004_pass1[compression_index];
715 ptable8004 = pdec->table_8004_pass1[compression_index];
717 /* Each block decode a square of 4x4 */
718 while (nblocks) {
719 decode_block(pdec, ptable0004, ptable8004);
720 copy_image_block_Y(pdec->temp_colors, planar_y, real_image_width, pdec->scalebits);
721 planar_y += 4;
722 nblocks--;
725 /* pass 2: uncompress UV component */
726 nblocks = compressed_image_width / 8;
728 ptable0004 = pdec->table_0004_pass2[compression_index];
729 ptable8004 = pdec->table_8004_pass2[compression_index];
731 /* Each block decode a square of 4x4 */
732 while (nblocks) {
733 decode_block(pdec, ptable0004, ptable8004);
734 copy_image_block_CrCb(pdec->temp_colors, planar_u, real_image_width/2, pdec->scalebits);
736 decode_block(pdec, ptable0004, ptable8004);
737 copy_image_block_CrCb(pdec->temp_colors, planar_v, real_image_width/2, pdec->scalebits);
739 planar_v += 8;
740 planar_u += 8;
741 nblocks -= 2;
746 #if ENABLE_BAYER_DECODER
748 * Size need to be a multiple of 8 in width
750 * Return a block of four line encoded like this:
752 * G R G R G R G R G R G R G R G R
753 * B G B G B G B G B G B G B G B G
754 * G R G R G R G R G R G R G R G R
755 * B G B G B G B G B G B G B G B G
758 static void DecompressBandBayer(struct pwc_dec23_private *pdec,
759 const unsigned char *rawyuv,
760 unsigned char *rgbbayer,
761 unsigned int compressed_image_width,
762 unsigned int real_image_width)
764 int compression_index, nblocks;
765 const unsigned char *ptable0004;
766 const unsigned char *ptable8004;
767 unsigned char *dest;
769 pdec->reservoir = 0;
770 pdec->nbits_in_reservoir = 0;
771 pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */
773 get_nbits(pdec, 4, compression_index);
775 /* pass 1: uncompress RB component */
776 nblocks = compressed_image_width / 4;
778 ptable0004 = pdec->table_0004_pass1[compression_index];
779 ptable8004 = pdec->table_8004_pass1[compression_index];
780 dest = rgbbayer;
782 /* Each block decode a square of 4x4 */
783 while (nblocks) {
784 decode_block(pdec, ptable0004, ptable8004);
785 copy_image_block_RedBlue(pdec->temp_colors, rgbbayer, real_image_width, pdec->scalebits);
786 dest += 8;
787 nblocks--;
790 /* pass 2: uncompress G component */
791 nblocks = compressed_image_width / 8;
793 ptable0004 = pdec->table_0004_pass2[compression_index];
794 ptable8004 = pdec->table_8004_pass2[compression_index];
796 /* Each block decode a square of 4x4 */
797 while (nblocks) {
798 decode_block(pdec, ptable0004, ptable8004);
799 copy_image_block_Green(pdec->temp_colors, rgbbayer+1, real_image_width, pdec->scalebits);
801 decode_block(pdec, ptable0004, ptable8004);
802 copy_image_block_Green(pdec->temp_colors, rgbbayer+real_image_width, real_image_width, pdec->scalebits);
804 rgbbayer += 16;
805 nblocks -= 2;
808 #endif
813 * Uncompress a pwc23 buffer.
815 * pwc.view: size of the image wanted
816 * pwc.image: size of the image returned by the camera
817 * pwc.offset: (x,y) to displayer image in the view
819 * src: raw data
820 * dst: image output
821 * flags: PWCX_FLAG_PLANAR or PWCX_FLAG_BAYER
823 void pwc_dec23_decompress(const struct pwc_device *pwc,
824 const void *src,
825 void *dst,
826 int flags)
828 int bandlines_left, stride, bytes_per_block;
830 bandlines_left = pwc->image.y / 4;
831 bytes_per_block = pwc->view.x * 4;
833 if (flags & PWCX_FLAG_BAYER) {
834 #if ENABLE_BAYER_DECODER
835 /* RGB Bayer format */
836 unsigned char *rgbout;
838 stride = pwc->view.x * pwc->offset.y;
839 rgbout = dst + stride + pwc->offset.x;
842 while (bandlines_left--) {
844 DecompressBandBayer(pwc->decompress_data,
845 src,
846 rgbout,
847 pwc->image.x, pwc->view.x);
849 src += pwc->vbandlength;
850 rgbout += bytes_per_block;
853 #else
854 memset(dst, 0, pwc->view.x * pwc->view.y);
855 #endif
857 } else {
858 /* YUV420P image format */
859 unsigned char *pout_planar_y;
860 unsigned char *pout_planar_u;
861 unsigned char *pout_planar_v;
862 unsigned int plane_size;
864 plane_size = pwc->view.x * pwc->view.y;
866 /* offset in Y plane */
867 stride = pwc->view.x * pwc->offset.y;
868 pout_planar_y = dst + stride + pwc->offset.x;
870 /* offsets in U/V planes */
871 stride = (pwc->view.x * pwc->offset.y) / 4 + pwc->offset.x / 2;
872 pout_planar_u = dst + plane_size + stride;
873 pout_planar_v = dst + plane_size + plane_size / 4 + stride;
875 while (bandlines_left--) {
877 DecompressBand23(pwc->decompress_data,
878 src,
879 pout_planar_y, pout_planar_u, pout_planar_v,
880 pwc->image.x, pwc->view.x);
881 src += pwc->vbandlength;
882 pout_planar_y += bytes_per_block;
883 pout_planar_u += pwc->view.x;
884 pout_planar_v += pwc->view.x;
892 void pwc_dec23_exit(void)
894 /* Do nothing */
899 * Allocate a private structure used by lookup table.
900 * You must call kfree() to free the memory allocated.
902 int pwc_dec23_alloc(struct pwc_device *pwc)
904 pwc->decompress_data = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
905 if (pwc->decompress_data == NULL)
906 return -ENOMEM;
907 return 0;
910 /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */