1/* Linux driver for Philips webcam 2 Decompression for chipset version 2 et 3 3 (C) 2004-2006 Luc Saillard (luc@saillard.org) 4 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. 10 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. 15 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. 20 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 24 25*/ 26 27#include "pwc-timon.h" 28#include "pwc-kiara.h" 29#include "pwc-dec23.h" 30#include <media/pwc-ioctl.h> 31 32#include <linux/string.h> 33 34/* 35 * USE_LOOKUP_TABLE_TO_CLAMP 36 * 0: use a C version of this tests: { a<0?0:(a>255?255:a) } 37 * 1: use a faster lookup table for cpu with a big cache (intel) 38 */ 39#define USE_LOOKUP_TABLE_TO_CLAMP 1 40/* 41 * UNROLL_LOOP_FOR_COPYING_BLOCK 42 * 0: use a loop for a smaller code (but little slower) 43 * 1: when unrolling the loop, gcc produces some faster code (perhaps only 44 * valid for intel processor class). Activating this option, automaticaly 45 * activate USE_LOOKUP_TABLE_TO_CLAMP 46 */ 47#define UNROLL_LOOP_FOR_COPY 1 48#if UNROLL_LOOP_FOR_COPY 49# undef USE_LOOKUP_TABLE_TO_CLAMP 50# define USE_LOOKUP_TABLE_TO_CLAMP 1 51#endif 52 53/* 54 * ENABLE_BAYER_DECODER 55 * 0: bayer decoder is not build (save some space) 56 * 1: bayer decoder is build and can be used 57 */ 58#define ENABLE_BAYER_DECODER 0 59 60static void build_subblock_pattern(struct pwc_dec23_private *pdec) 61{ 62 static const unsigned int initial_values[12] = { 63 -0x526500, -0x221200, 0x221200, 0x526500, 64 -0x3de200, 0x3de200, 65 -0x6db480, -0x2d5d00, 0x2d5d00, 0x6db480, 66 -0x12c200, 0x12c200 67 68 }; 69 static const unsigned int values_derivated[12] = { 70 0xa4ca, 0x4424, -0x4424, -0xa4ca, 71 0x7bc4, -0x7bc4, 72 0xdb69, 0x5aba, -0x5aba, -0xdb69, 73 0x2584, -0x2584 74 }; 75 unsigned int temp_values[12]; 76 int i, j; 77 78 memcpy(temp_values, initial_values, sizeof(initial_values)); 79 for (i = 0; i < 256; i++) { 80 for (j = 0; j < 12; j++) { 81 pdec->table_subblock[i][j] = temp_values[j]; 82 temp_values[j] += values_derivated[j]; 83 } 84 } 85} 86 87static void build_bit_powermask_table(struct pwc_dec23_private *pdec) 88{ 89 unsigned char *p; 90 unsigned int bit, byte, mask, val; 91 unsigned int bitpower = 1; 92 93 for (bit = 0; bit < 8; bit++) { 94 mask = bitpower - 1; 95 p = pdec->table_bitpowermask[bit]; 96 for (byte = 0; byte < 256; byte++) { 97 val = (byte & mask); 98 if (byte & bitpower) 99 val = -val; 100 *p++ = val; 101 } 102 bitpower<<=1; 103 } 104} 105 106 107static void build_table_color(const unsigned int romtable[16][8], 108 unsigned char p0004[16][1024], 109 unsigned char p8004[16][256]) 110{ 111 int compression_mode, j, k, bit, pw; 112 unsigned char *p0, *p8; 113 const unsigned int *r; 114 115 /* We have 16 compressions tables */ 116 for (compression_mode = 0; compression_mode < 16; compression_mode++) { 117 p0 = p0004[compression_mode]; 118 p8 = p8004[compression_mode]; 119 r = romtable[compression_mode]; 120 121 for (j = 0; j < 8; j++, r++, p0 += 128) { 122 123 for (k = 0; k < 16; k++) { 124 if (k == 0) 125 bit = 1; 126 else if (k >= 1 && k < 3) 127 bit = (r[0] >> 15) & 7; 128 else if (k >= 3 && k < 6) 129 bit = (r[0] >> 12) & 7; 130 else if (k >= 6 && k < 10) 131 bit = (r[0] >> 9) & 7; 132 else if (k >= 10 && k < 13) 133 bit = (r[0] >> 6) & 7; 134 else if (k >= 13 && k < 15) 135 bit = (r[0] >> 3) & 7; 136 else 137 bit = (r[0]) & 7; 138 if (k == 0) 139 *p8++ = 8; 140 else 141 *p8++ = j - bit; 142 *p8++ = bit; 143 144 pw = 1 << bit; 145 p0[k + 0x00] = (1 * pw) + 0x80; 146 p0[k + 0x10] = (2 * pw) + 0x80; 147 p0[k + 0x20] = (3 * pw) + 0x80; 148 p0[k + 0x30] = (4 * pw) + 0x80; 149 p0[k + 0x40] = (-1 * pw) + 0x80; 150 p0[k + 0x50] = (-2 * pw) + 0x80; 151 p0[k + 0x60] = (-3 * pw) + 0x80; 152 p0[k + 0x70] = (-4 * pw) + 0x80; 153 } /* end of for (k=0; k<16; k++, p8++) */ 154 } /* end of for (j=0; j<8; j++ , table++) */ 155 } /* end of foreach compression_mode */ 156} 157 158/* 159 * 160 */ 161static void fill_table_dc00_d800(struct pwc_dec23_private *pdec) 162{ 163#define SCALEBITS 15 164#define ONE_HALF (1UL << (SCALEBITS - 1)) 165 int i; 166 unsigned int offset1 = ONE_HALF; 167 unsigned int offset2 = 0x0000; 168 169 for (i=0; i<256; i++) { 170 pdec->table_dc00[i] = offset1 & ~(ONE_HALF); 171 pdec->table_d800[i] = offset2; 172 173 offset1 += 0x7bc4; 174 offset2 += 0x7bc4; 175 } 176} 177 178static const unsigned char hash_table_ops[64*4] = { 179 0x02, 0x00, 0x00, 0x00, 180 0x00, 0x03, 0x01, 0x00, 181 0x00, 0x04, 0x01, 0x10, 182 0x00, 0x06, 0x01, 0x30, 183 0x02, 0x00, 0x00, 0x00, 184 0x00, 0x03, 0x01, 0x40, 185 0x00, 0x05, 0x01, 0x20, 186 0x01, 0x00, 0x00, 0x00, 187 0x02, 0x00, 0x00, 0x00, 188 0x00, 0x03, 0x01, 0x00, 189 0x00, 0x04, 0x01, 0x50, 190 0x00, 0x05, 0x02, 0x00, 191 0x02, 0x00, 0x00, 0x00, 192 0x00, 0x03, 0x01, 0x40, 193 0x00, 0x05, 0x03, 0x00, 194 0x01, 0x00, 0x00, 0x00, 195 0x02, 0x00, 0x00, 0x00, 196 0x00, 0x03, 0x01, 0x00, 197 0x00, 0x04, 0x01, 0x10, 198 0x00, 0x06, 0x02, 0x10, 199 0x02, 0x00, 0x00, 0x00, 200 0x00, 0x03, 0x01, 0x40, 201 0x00, 0x05, 0x01, 0x60, 202 0x01, 0x00, 0x00, 0x00, 203 0x02, 0x00, 0x00, 0x00, 204 0x00, 0x03, 0x01, 0x00, 205 0x00, 0x04, 0x01, 0x50, 206 0x00, 0x05, 0x02, 0x40, 207 0x02, 0x00, 0x00, 0x00, 208 0x00, 0x03, 0x01, 0x40, 209 0x00, 0x05, 0x03, 0x40, 210 0x01, 0x00, 0x00, 0x00, 211 0x02, 0x00, 0x00, 0x00, 212 0x00, 0x03, 0x01, 0x00, 213 0x00, 0x04, 0x01, 0x10, 214 0x00, 0x06, 0x01, 0x70, 215 0x02, 0x00, 0x00, 0x00, 216 0x00, 0x03, 0x01, 0x40, 217 0x00, 0x05, 0x01, 0x20, 218 0x01, 0x00, 0x00, 0x00, 219 0x02, 0x00, 0x00, 0x00, 220 0x00, 0x03, 0x01, 0x00, 221 0x00, 0x04, 0x01, 0x50, 222 0x00, 0x05, 0x02, 0x00, 223 0x02, 0x00, 0x00, 0x00, 224 0x00, 0x03, 0x01, 0x40, 225 0x00, 0x05, 0x03, 0x00, 226 0x01, 0x00, 0x00, 0x00, 227 0x02, 0x00, 0x00, 0x00, 228 0x00, 0x03, 0x01, 0x00, 229 0x00, 0x04, 0x01, 0x10, 230 0x00, 0x06, 0x02, 0x50, 231 0x02, 0x00, 0x00, 0x00, 232 0x00, 0x03, 0x01, 0x40, 233 0x00, 0x05, 0x01, 0x60, 234 0x01, 0x00, 0x00, 0x00, 235 0x02, 0x00, 0x00, 0x00, 236 0x00, 0x03, 0x01, 0x00, 237 0x00, 0x04, 0x01, 0x50, 238 0x00, 0x05, 0x02, 0x40, 239 0x02, 0x00, 0x00, 0x00, 240 0x00, 0x03, 0x01, 0x40, 241 0x00, 0x05, 0x03, 0x40, 242 0x01, 0x00, 0x00, 0x00 243}; 244 245/* 246 * 247 */ 248static const unsigned int MulIdx[16][16] = { 249 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 250 {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3,}, 251 {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,}, 252 {4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,}, 253 {6, 7, 8, 9, 7, 10, 11, 8, 8, 11, 10, 7, 9, 8, 7, 6,}, 254 {4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4,}, 255 {1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2,}, 256 {0, 3, 3, 0, 1, 2, 2, 1, 2, 1, 1, 2, 3, 0, 0, 3,}, 257 {0, 1, 2, 3, 3, 2, 1, 0, 3, 2, 1, 0, 0, 1, 2, 3,}, 258 {1, 1, 1, 1, 3, 3, 3, 3, 0, 0, 0, 0, 2, 2, 2, 2,}, 259 {7, 10, 11, 8, 9, 8, 7, 6, 6, 7, 8, 9, 8, 11, 10, 7,}, 260 {4, 5, 5, 4, 5, 4, 4, 5, 5, 4, 4, 5, 4, 5, 5, 4,}, 261 {7, 9, 6, 8, 10, 8, 7, 11, 11, 7, 8, 10, 8, 6, 9, 7,}, 262 {1, 3, 0, 2, 2, 0, 3, 1, 2, 0, 3, 1, 1, 3, 0, 2,}, 263 {1, 2, 2, 1, 3, 0, 0, 3, 0, 3, 3, 0, 2, 1, 1, 2,}, 264 {10, 8, 7, 11, 8, 6, 9, 7, 7, 9, 6, 8, 11, 7, 8, 10} 265}; 266 267#if USE_LOOKUP_TABLE_TO_CLAMP 268#define MAX_OUTER_CROP_VALUE (512) 269static unsigned char pwc_crop_table[256 + 2*MAX_OUTER_CROP_VALUE]; 270#define CLAMP(x) (pwc_crop_table[MAX_OUTER_CROP_VALUE+(x)]) 271#else 272#define CLAMP(x) ((x)>255?255:((x)<0?0:x)) 273#endif 274 275 276/* If the type or the command change, we rebuild the lookup table */ 277int pwc_dec23_init(struct pwc_device *pwc, int type, unsigned char *cmd) 278{ 279 int flags, version, shift, i; 280 struct pwc_dec23_private *pdec; 281 282 if (pwc->decompress_data == NULL) { 283 pdec = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL); 284 if (pdec == NULL) 285 return -ENOMEM; 286 pwc->decompress_data = pdec; 287 } 288 pdec = pwc->decompress_data; 289 290 if (DEVICE_USE_CODEC3(type)) { 291 flags = cmd[2] & 0x18; 292 if (flags == 8) 293 pdec->nbits = 7; /* More bits, mean more bits to encode the stream, but better quality */ 294 else if (flags == 0x10) 295 pdec->nbits = 8; 296 else 297 pdec->nbits = 6; 298 299 version = cmd[2] >> 5; 300 build_table_color(KiaraRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1); 301 build_table_color(KiaraRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2); 302 303 } else { 304 305 flags = cmd[2] & 6; 306 if (flags == 2) 307 pdec->nbits = 7; 308 else if (flags == 4) 309 pdec->nbits = 8; 310 else 311 pdec->nbits = 6; 312 313 version = cmd[2] >> 3; 314 build_table_color(TimonRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1); 315 build_table_color(TimonRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2); 316 } 317 318 /* Informations can be coded on a variable number of bits but never less than 8 */ 319 shift = 8 - pdec->nbits; 320 pdec->scalebits = SCALEBITS - shift; 321 pdec->nbitsmask = 0xFF >> shift; 322 323 fill_table_dc00_d800(pdec); 324 build_subblock_pattern(pdec); 325 build_bit_powermask_table(pdec); 326 327#if USE_LOOKUP_TABLE_TO_CLAMP 328 /* Build the static table to clamp value [0-255] */ 329 for (i=0;i<MAX_OUTER_CROP_VALUE;i++) 330 pwc_crop_table[i] = 0; 331 for (i=0; i<256; i++) 332 pwc_crop_table[MAX_OUTER_CROP_VALUE+i] = i; 333 for (i=0; i<MAX_OUTER_CROP_VALUE; i++) 334 pwc_crop_table[MAX_OUTER_CROP_VALUE+256+i] = 255; 335#endif 336 337 return 0; 338} 339 340/* 341 * Copy the 4x4 image block to Y plane buffer 342 */ 343static void copy_image_block_Y(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits) 344{ 345#if UNROLL_LOOP_FOR_COPY 346 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE; 347 const int *c = src; 348 unsigned char *d = dst; 349 350 *d++ = cm[c[0] >> scalebits]; 351 *d++ = cm[c[1] >> scalebits]; 352 *d++ = cm[c[2] >> scalebits]; 353 *d++ = cm[c[3] >> scalebits]; 354 355 d = dst + bytes_per_line; 356 *d++ = cm[c[4] >> scalebits]; 357 *d++ = cm[c[5] >> scalebits]; 358 *d++ = cm[c[6] >> scalebits]; 359 *d++ = cm[c[7] >> scalebits]; 360 361 d = dst + bytes_per_line*2; 362 *d++ = cm[c[8] >> scalebits]; 363 *d++ = cm[c[9] >> scalebits]; 364 *d++ = cm[c[10] >> scalebits]; 365 *d++ = cm[c[11] >> scalebits]; 366 367 d = dst + bytes_per_line*3; 368 *d++ = cm[c[12] >> scalebits]; 369 *d++ = cm[c[13] >> scalebits]; 370 *d++ = cm[c[14] >> scalebits]; 371 *d++ = cm[c[15] >> scalebits]; 372#else 373 int i; 374 const int *c = src; 375 unsigned char *d = dst; 376 for (i = 0; i < 4; i++, c++) 377 *d++ = CLAMP((*c) >> scalebits); 378 379 d = dst + bytes_per_line; 380 for (i = 0; i < 4; i++, c++) 381 *d++ = CLAMP((*c) >> scalebits); 382 383 d = dst + bytes_per_line*2; 384 for (i = 0; i < 4; i++, c++) 385 *d++ = CLAMP((*c) >> scalebits); 386 387 d = dst + bytes_per_line*3; 388 for (i = 0; i < 4; i++, c++) 389 *d++ = CLAMP((*c) >> scalebits); 390#endif 391} 392 393/* 394 * Copy the 4x4 image block to a CrCb plane buffer 395 * 396 */ 397static void copy_image_block_CrCb(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits) 398{ 399#if UNROLL_LOOP_FOR_COPY 400 /* Unroll all loops */ 401 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE; 402 const int *c = src; 403 unsigned char *d = dst; 404 405 *d++ = cm[c[0] >> scalebits]; 406 *d++ = cm[c[4] >> scalebits]; 407 *d++ = cm[c[1] >> scalebits]; 408 *d++ = cm[c[5] >> scalebits]; 409 *d++ = cm[c[2] >> scalebits]; 410 *d++ = cm[c[6] >> scalebits]; 411 *d++ = cm[c[3] >> scalebits]; 412 *d++ = cm[c[7] >> scalebits]; 413 414 d = dst + bytes_per_line; 415 *d++ = cm[c[12] >> scalebits]; 416 *d++ = cm[c[8] >> scalebits]; 417 *d++ = cm[c[13] >> scalebits]; 418 *d++ = cm[c[9] >> scalebits]; 419 *d++ = cm[c[14] >> scalebits]; 420 *d++ = cm[c[10] >> scalebits]; 421 *d++ = cm[c[15] >> scalebits]; 422 *d++ = cm[c[11] >> scalebits]; 423#else 424 int i; 425 const int *c1 = src; 426 const int *c2 = src + 4; 427 unsigned char *d = dst; 428 429 for (i = 0; i < 4; i++, c1++, c2++) { 430 *d++ = CLAMP((*c1) >> scalebits); 431 *d++ = CLAMP((*c2) >> scalebits); 432 } 433 c1 = src + 12; 434 d = dst + bytes_per_line; 435 for (i = 0; i < 4; i++, c1++, c2++) { 436 *d++ = CLAMP((*c1) >> scalebits); 437 *d++ = CLAMP((*c2) >> scalebits); 438 } 439#endif 440} 441 442#if ENABLE_BAYER_DECODER 443/* 444 * Format: 8x2 pixels 445 * . G . G . G . G . G . G . G 446 * . . . . . . . . . . . . . . 447 * . G . G . G . G . G . G . G 448 * . . . . . . . . . . . . . . 449 * or 450 * . . . . . . . . . . . . . . 451 * G . G . G . G . G . G . G . 452 * . . . . . . . . . . . . . . 453 * G . G . G . G . G . G . G . 454*/ 455static void copy_image_block_Green(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits) 456{ 457#if UNROLL_LOOP_FOR_COPY 458 /* Unroll all loops */ 459 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE; 460 unsigned char *d = dst; 461 const int *c = src; 462 463 d[0] = cm[c[0] >> scalebits]; 464 d[2] = cm[c[1] >> scalebits]; 465 d[4] = cm[c[2] >> scalebits]; 466 d[6] = cm[c[3] >> scalebits]; 467 d[8] = cm[c[4] >> scalebits]; 468 d[10] = cm[c[5] >> scalebits]; 469 d[12] = cm[c[6] >> scalebits]; 470 d[14] = cm[c[7] >> scalebits]; 471 472 d = dst + bytes_per_line; 473 d[0] = cm[c[8] >> scalebits]; 474 d[2] = cm[c[9] >> scalebits]; 475 d[4] = cm[c[10] >> scalebits]; 476 d[6] = cm[c[11] >> scalebits]; 477 d[8] = cm[c[12] >> scalebits]; 478 d[10] = cm[c[13] >> scalebits]; 479 d[12] = cm[c[14] >> scalebits]; 480 d[14] = cm[c[15] >> scalebits]; 481#else 482 int i; 483 unsigned char *d; 484 const int *c = src; 485 486 d = dst; 487 for (i = 0; i < 8; i++, c++) 488 d[i*2] = CLAMP((*c) >> scalebits); 489 490 d = dst + bytes_per_line; 491 for (i = 0; i < 8; i++, c++) 492 d[i*2] = CLAMP((*c) >> scalebits); 493#endif 494} 495#endif 496 497#if ENABLE_BAYER_DECODER 498/* 499 * Format: 4x4 pixels 500 * R . R . R . R 501 * . B . B . B . 502 * R . R . R . R 503 * . B . B . B . 504 */ 505static void copy_image_block_RedBlue(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits) 506{ 507#if UNROLL_LOOP_FOR_COPY 508 /* Unroll all loops */ 509 const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE; 510 unsigned char *d = dst; 511 const int *c = src; 512 513 d[0] = cm[c[0] >> scalebits]; 514 d[2] = cm[c[1] >> scalebits]; 515 d[4] = cm[c[2] >> scalebits]; 516 d[6] = cm[c[3] >> scalebits]; 517 518 d = dst + bytes_per_line; 519 d[1] = cm[c[4] >> scalebits]; 520 d[3] = cm[c[5] >> scalebits]; 521 d[5] = cm[c[6] >> scalebits]; 522 d[7] = cm[c[7] >> scalebits]; 523 524 d = dst + bytes_per_line*2; 525 d[0] = cm[c[8] >> scalebits]; 526 d[2] = cm[c[9] >> scalebits]; 527 d[4] = cm[c[10] >> scalebits]; 528 d[6] = cm[c[11] >> scalebits]; 529 530 d = dst + bytes_per_line*3; 531 d[1] = cm[c[12] >> scalebits]; 532 d[3] = cm[c[13] >> scalebits]; 533 d[5] = cm[c[14] >> scalebits]; 534 d[7] = cm[c[15] >> scalebits]; 535#else 536 int i; 537 unsigned char *d; 538 const int *c = src; 539 540 d = dst; 541 for (i = 0; i < 4; i++, c++) 542 d[i*2] = CLAMP((*c) >> scalebits); 543 544 d = dst + bytes_per_line; 545 for (i = 0; i < 4; i++, c++) 546 d[i*2+1] = CLAMP((*c) >> scalebits); 547 548 d = dst + bytes_per_line*2; 549 for (i = 0; i < 4; i++, c++) 550 d[i*2] = CLAMP((*c) >> scalebits); 551 552 d = dst + bytes_per_line*3; 553 for (i = 0; i < 4; i++, c++) 554 d[i*2+1] = CLAMP((*c) >> scalebits); 555#endif 556} 557#endif 558 559/* 560 * To manage the stream, we keep bits in a 32 bits register. 561 * fill_nbits(n): fill the reservoir with at least n bits 562 * skip_bits(n): discard n bits from the reservoir 563 * get_bits(n): fill the reservoir, returns the first n bits and discard the 564 * bits from the reservoir. 565 * __get_nbits(n): faster version of get_bits(n), but asumes that the reservoir 566 * contains at least n bits. bits returned is discarded. 567 */ 568#define fill_nbits(pdec, nbits_wanted) do { \ 569 while (pdec->nbits_in_reservoir<(nbits_wanted)) \ 570 { \ 571 pdec->reservoir |= (*(pdec->stream)++) << (pdec->nbits_in_reservoir); \ 572 pdec->nbits_in_reservoir += 8; \ 573 } \ 574} while(0); 575 576#define skip_nbits(pdec, nbits_to_skip) do { \ 577 pdec->reservoir >>= (nbits_to_skip); \ 578 pdec->nbits_in_reservoir -= (nbits_to_skip); \ 579} while(0); 580 581#define get_nbits(pdec, nbits_wanted, result) do { \ 582 fill_nbits(pdec, nbits_wanted); \ 583 result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \ 584 skip_nbits(pdec, nbits_wanted); \ 585} while(0); 586 587#define __get_nbits(pdec, nbits_wanted, result) do { \ 588 result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \ 589 skip_nbits(pdec, nbits_wanted); \ 590} while(0); 591 592#define look_nbits(pdec, nbits_wanted) \ 593 ((pdec->reservoir) & ((1U<<(nbits_wanted))-1)) 594 595/* 596 * Decode a 4x4 pixel block 597 */ 598static void decode_block(struct pwc_dec23_private *pdec, 599 const unsigned char *ptable0004, 600 const unsigned char *ptable8004) 601{ 602 unsigned int primary_color; 603 unsigned int channel_v, offset1, op; 604 int i; 605 606 fill_nbits(pdec, 16); 607 __get_nbits(pdec, pdec->nbits, primary_color); 608 609 if (look_nbits(pdec,2) == 0) { 610 skip_nbits(pdec, 2); 611 /* Very simple, the color is the same for all pixels of the square */ 612 for (i = 0; i < 16; i++) 613 pdec->temp_colors[i] = pdec->table_dc00[primary_color]; 614 615 return; 616 } 617 618 /* This block is encoded with small pattern */ 619 for (i = 0; i < 16; i++) 620 pdec->temp_colors[i] = pdec->table_d800[primary_color]; 621 622 __get_nbits(pdec, 3, channel_v); 623 channel_v = ((channel_v & 1) << 2) | (channel_v & 2) | ((channel_v & 4) >> 2); 624 625 ptable0004 += (channel_v * 128); 626 ptable8004 += (channel_v * 32); 627 628 offset1 = 0; 629 do 630 { 631 unsigned int htable_idx, rows = 0; 632 const unsigned int *block; 633 634 /* [ zzzz y x x ] 635 * xx == 00 :=> end of the block def, remove the two bits from the stream 636 * yxx == 111 637 * yxx == any other value 638 * 639 */ 640 fill_nbits(pdec, 16); 641 htable_idx = look_nbits(pdec, 6); 642 op = hash_table_ops[htable_idx * 4]; 643 644 if (op == 2) { 645 skip_nbits(pdec, 2); 646 647 } else if (op == 1) { 648 unsigned int mask, shift; 649 unsigned int nbits, col1; 650 unsigned int yyyy; 651 652 skip_nbits(pdec, 3); 653 /* offset1 += yyyy */ 654 __get_nbits(pdec, 4, yyyy); 655 offset1 += 1 + yyyy; 656 offset1 &= 0x0F; 657 nbits = ptable8004[offset1 * 2]; 658 659 __get_nbits(pdec, nbits+1, col1); 660 661 /* Bit mask table */ 662 mask = pdec->table_bitpowermask[nbits][col1]; 663 shift = ptable8004[offset1 * 2 + 1]; 664 rows = ((mask << shift) + 0x80) & 0xFF; 665 666 block = pdec->table_subblock[rows]; 667 for (i = 0; i < 16; i++) 668 pdec->temp_colors[i] += block[MulIdx[offset1][i]]; 669 670 } else { 671 /* op == 0 672 * offset1 is coded on 3 bits 673 */ 674 unsigned int shift; 675 676 offset1 += hash_table_ops [htable_idx * 4 + 2]; 677 offset1 &= 0x0F; 678 679 rows = ptable0004[offset1 + hash_table_ops [htable_idx * 4 + 3]]; 680 block = pdec->table_subblock[rows]; 681 for (i = 0; i < 16; i++) 682 pdec->temp_colors[i] += block[MulIdx[offset1][i]]; 683 684 shift = hash_table_ops[htable_idx * 4 + 1]; 685 skip_nbits(pdec, shift); 686 } 687 688 } while (op != 2); 689 690} 691 692static void DecompressBand23(struct pwc_dec23_private *pdec, 693 const unsigned char *rawyuv, 694 unsigned char *planar_y, 695 unsigned char *planar_u, 696 unsigned char *planar_v, 697 unsigned int compressed_image_width, 698 unsigned int real_image_width) 699{ 700 int compression_index, nblocks; 701 const unsigned char *ptable0004; 702 const unsigned char *ptable8004; 703 704 pdec->reservoir = 0; 705 pdec->nbits_in_reservoir = 0; 706 pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */ 707 708 get_nbits(pdec, 4, compression_index); 709 710 /* pass 1: uncompress Y component */ 711 nblocks = compressed_image_width / 4; 712 713 ptable0004 = pdec->table_0004_pass1[compression_index]; 714 ptable8004 = pdec->table_8004_pass1[compression_index]; 715 716 /* Each block decode a square of 4x4 */ 717 while (nblocks) { 718 decode_block(pdec, ptable0004, ptable8004); 719 copy_image_block_Y(pdec->temp_colors, planar_y, real_image_width, pdec->scalebits); 720 planar_y += 4; 721 nblocks--; 722 } 723 724 /* pass 2: uncompress UV component */ 725 nblocks = compressed_image_width / 8; 726 727 ptable0004 = pdec->table_0004_pass2[compression_index]; 728 ptable8004 = pdec->table_8004_pass2[compression_index]; 729 730 /* Each block decode a square of 4x4 */ 731 while (nblocks) { 732 decode_block(pdec, ptable0004, ptable8004); 733 copy_image_block_CrCb(pdec->temp_colors, planar_u, real_image_width/2, pdec->scalebits); 734 735 decode_block(pdec, ptable0004, ptable8004); 736 copy_image_block_CrCb(pdec->temp_colors, planar_v, real_image_width/2, pdec->scalebits); 737 738 planar_v += 8; 739 planar_u += 8; 740 nblocks -= 2; 741 } 742 743} 744 745#if ENABLE_BAYER_DECODER 746/* 747 * Size need to be a multiple of 8 in width 748 * 749 * Return a block of four line encoded like this: 750 * 751 * G R G R G R G R G R G R G R G R 752 * B G B G B G B G B G B G B G B G 753 * G R G R G R G R G R G R G R G R 754 * B G B G B G B G B G B G B G B G 755 * 756 */ 757static void DecompressBandBayer(struct pwc_dec23_private *pdec, 758 const unsigned char *rawyuv, 759 unsigned char *rgbbayer, 760 unsigned int compressed_image_width, 761 unsigned int real_image_width) 762{ 763 int compression_index, nblocks; 764 const unsigned char *ptable0004; 765 const unsigned char *ptable8004; 766 unsigned char *dest; 767 768 pdec->reservoir = 0; 769 pdec->nbits_in_reservoir = 0; 770 pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */ 771 772 get_nbits(pdec, 4, compression_index); 773 774 /* pass 1: uncompress RB component */ 775 nblocks = compressed_image_width / 4; 776 777 ptable0004 = pdec->table_0004_pass1[compression_index]; 778 ptable8004 = pdec->table_8004_pass1[compression_index]; 779 dest = rgbbayer; 780 781 /* Each block decode a square of 4x4 */ 782 while (nblocks) { 783 decode_block(pdec, ptable0004, ptable8004); 784 copy_image_block_RedBlue(pdec->temp_colors, rgbbayer, real_image_width, pdec->scalebits); 785 dest += 8; 786 nblocks--; 787 } 788 789 /* pass 2: uncompress G component */ 790 nblocks = compressed_image_width / 8; 791 792 ptable0004 = pdec->table_0004_pass2[compression_index]; 793 ptable8004 = pdec->table_8004_pass2[compression_index]; 794 795 /* Each block decode a square of 4x4 */ 796 while (nblocks) { 797 decode_block(pdec, ptable0004, ptable8004); 798 copy_image_block_Green(pdec->temp_colors, rgbbayer+1, real_image_width, pdec->scalebits); 799 800 decode_block(pdec, ptable0004, ptable8004); 801 copy_image_block_Green(pdec->temp_colors, rgbbayer+real_image_width, real_image_width, pdec->scalebits); 802 803 rgbbayer += 16; 804 nblocks -= 2; 805 } 806} 807#endif 808 809 810/** 811 * 812 * Uncompress a pwc23 buffer. 813 * 814 * pwc.view: size of the image wanted 815 * pwc.image: size of the image returned by the camera 816 * pwc.offset: (x,y) to displayer image in the view 817 * 818 * src: raw data 819 * dst: image output 820 * flags: PWCX_FLAG_PLANAR or PWCX_FLAG_BAYER 821 */ 822void pwc_dec23_decompress(const struct pwc_device *pwc, 823 const void *src, 824 void *dst, 825 int flags) 826{ 827 int bandlines_left, stride, bytes_per_block; 828 829 bandlines_left = pwc->image.y / 4; 830 bytes_per_block = pwc->view.x * 4; 831 832 if (flags & PWCX_FLAG_BAYER) { 833#if ENABLE_BAYER_DECODER 834 /* RGB Bayer format */ 835 unsigned char *rgbout; 836 837 stride = pwc->view.x * pwc->offset.y; 838 rgbout = dst + stride + pwc->offset.x; 839 840 841 while (bandlines_left--) { 842 843 DecompressBandBayer(pwc->decompress_data, 844 src, 845 rgbout, 846 pwc->image.x, pwc->view.x); 847 848 src += pwc->vbandlength; 849 rgbout += bytes_per_block; 850 851 } 852#else 853 memset(dst, 0, pwc->view.x * pwc->view.y); 854#endif 855 856 } else { 857 /* YUV420P image format */ 858 unsigned char *pout_planar_y; 859 unsigned char *pout_planar_u; 860 unsigned char *pout_planar_v; 861 unsigned int plane_size; 862 863 plane_size = pwc->view.x * pwc->view.y; 864 865 /* offset in Y plane */ 866 stride = pwc->view.x * pwc->offset.y; 867 pout_planar_y = dst + stride + pwc->offset.x; 868 869 /* offsets in U/V planes */ 870 stride = (pwc->view.x * pwc->offset.y) / 4 + pwc->offset.x / 2; 871 pout_planar_u = dst + plane_size + stride; 872 pout_planar_v = dst + plane_size + plane_size / 4 + stride; 873 874 while (bandlines_left--) { 875 876 DecompressBand23(pwc->decompress_data, 877 src, 878 pout_planar_y, pout_planar_u, pout_planar_v, 879 pwc->image.x, pwc->view.x); 880 src += pwc->vbandlength; 881 pout_planar_y += bytes_per_block; 882 pout_planar_u += pwc->view.x; 883 pout_planar_v += pwc->view.x; 884 885 } 886 887 } 888 889} 890 891void pwc_dec23_exit(void) 892{ 893 /* Do nothing */ 894 895} 896 897/** 898 * Allocate a private structure used by lookup table. 899 * You must call kfree() to free the memory allocated. 900 */ 901int pwc_dec23_alloc(struct pwc_device *pwc) 902{ 903 pwc->decompress_data = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL); 904 if (pwc->decompress_data == NULL) 905 return -ENOMEM; 906 return 0; 907} 908 909/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */ 910