pwc-dec23.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* Linux driver for Philips webcam
  2. Decompression for chipset version 2 et 3
  3. (C) 2004-2006 Luc Saillard (luc@saillard.org)
  4. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  5. driver and thus may have bugs that are not present in the original version.
  6. Please send bug reports and support requests to <luc@saillard.org>.
  7. The decompression routines have been implemented by reverse-engineering the
  8. Nemosoft binary pwcx module. Caveat emptor.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "pwc-timon.h"
  22. #include "pwc-kiara.h"
  23. #include "pwc-dec23.h"
  24. #include <media/pwc-ioctl.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. /*
  28. * USE_LOOKUP_TABLE_TO_CLAMP
  29. * 0: use a C version of this tests: { a<0?0:(a>255?255:a) }
  30. * 1: use a faster lookup table for cpu with a big cache (intel)
  31. */
  32. #define USE_LOOKUP_TABLE_TO_CLAMP 1
  33. /*
  34. * UNROLL_LOOP_FOR_COPYING_BLOCK
  35. * 0: use a loop for a smaller code (but little slower)
  36. * 1: when unrolling the loop, gcc produces some faster code (perhaps only
  37. * valid for intel processor class). Activating this option, automaticaly
  38. * activate USE_LOOKUP_TABLE_TO_CLAMP
  39. */
  40. #define UNROLL_LOOP_FOR_COPY 1
  41. #if UNROLL_LOOP_FOR_COPY
  42. # undef USE_LOOKUP_TABLE_TO_CLAMP
  43. # define USE_LOOKUP_TABLE_TO_CLAMP 1
  44. #endif
  45. static void build_subblock_pattern(struct pwc_dec23_private *pdec)
  46. {
  47. static const unsigned int initial_values[12] = {
  48. -0x526500, -0x221200, 0x221200, 0x526500,
  49. -0x3de200, 0x3de200,
  50. -0x6db480, -0x2d5d00, 0x2d5d00, 0x6db480,
  51. -0x12c200, 0x12c200
  52. };
  53. static const unsigned int values_derivated[12] = {
  54. 0xa4ca, 0x4424, -0x4424, -0xa4ca,
  55. 0x7bc4, -0x7bc4,
  56. 0xdb69, 0x5aba, -0x5aba, -0xdb69,
  57. 0x2584, -0x2584
  58. };
  59. unsigned int temp_values[12];
  60. int i, j;
  61. memcpy(temp_values, initial_values, sizeof(initial_values));
  62. for (i = 0; i < 256; i++) {
  63. for (j = 0; j < 12; j++) {
  64. pdec->table_subblock[i][j] = temp_values[j];
  65. temp_values[j] += values_derivated[j];
  66. }
  67. }
  68. }
  69. static void build_bit_powermask_table(struct pwc_dec23_private *pdec)
  70. {
  71. unsigned char *p;
  72. unsigned int bit, byte, mask, val;
  73. unsigned int bitpower = 1;
  74. for (bit = 0; bit < 8; bit++) {
  75. mask = bitpower - 1;
  76. p = pdec->table_bitpowermask[bit];
  77. for (byte = 0; byte < 256; byte++) {
  78. val = (byte & mask);
  79. if (byte & bitpower)
  80. val = -val;
  81. *p++ = val;
  82. }
  83. bitpower<<=1;
  84. }
  85. }
  86. static void build_table_color(const unsigned int romtable[16][8],
  87. unsigned char p0004[16][1024],
  88. unsigned char p8004[16][256])
  89. {
  90. int compression_mode, j, k, bit, pw;
  91. unsigned char *p0, *p8;
  92. const unsigned int *r;
  93. /* We have 16 compressions tables */
  94. for (compression_mode = 0; compression_mode < 16; compression_mode++) {
  95. p0 = p0004[compression_mode];
  96. p8 = p8004[compression_mode];
  97. r = romtable[compression_mode];
  98. for (j = 0; j < 8; j++, r++, p0 += 128) {
  99. for (k = 0; k < 16; k++) {
  100. if (k == 0)
  101. bit = 1;
  102. else if (k >= 1 && k < 3)
  103. bit = (r[0] >> 15) & 7;
  104. else if (k >= 3 && k < 6)
  105. bit = (r[0] >> 12) & 7;
  106. else if (k >= 6 && k < 10)
  107. bit = (r[0] >> 9) & 7;
  108. else if (k >= 10 && k < 13)
  109. bit = (r[0] >> 6) & 7;
  110. else if (k >= 13 && k < 15)
  111. bit = (r[0] >> 3) & 7;
  112. else
  113. bit = (r[0]) & 7;
  114. if (k == 0)
  115. *p8++ = 8;
  116. else
  117. *p8++ = j - bit;
  118. *p8++ = bit;
  119. pw = 1 << bit;
  120. p0[k + 0x00] = (1 * pw) + 0x80;
  121. p0[k + 0x10] = (2 * pw) + 0x80;
  122. p0[k + 0x20] = (3 * pw) + 0x80;
  123. p0[k + 0x30] = (4 * pw) + 0x80;
  124. p0[k + 0x40] = (-1 * pw) + 0x80;
  125. p0[k + 0x50] = (-2 * pw) + 0x80;
  126. p0[k + 0x60] = (-3 * pw) + 0x80;
  127. p0[k + 0x70] = (-4 * pw) + 0x80;
  128. } /* end of for (k=0; k<16; k++, p8++) */
  129. } /* end of for (j=0; j<8; j++ , table++) */
  130. } /* end of foreach compression_mode */
  131. }
  132. /*
  133. *
  134. */
  135. static void fill_table_dc00_d800(struct pwc_dec23_private *pdec)
  136. {
  137. #define SCALEBITS 15
  138. #define ONE_HALF (1UL << (SCALEBITS - 1))
  139. int i;
  140. unsigned int offset1 = ONE_HALF;
  141. unsigned int offset2 = 0x0000;
  142. for (i=0; i<256; i++) {
  143. pdec->table_dc00[i] = offset1 & ~(ONE_HALF);
  144. pdec->table_d800[i] = offset2;
  145. offset1 += 0x7bc4;
  146. offset2 += 0x7bc4;
  147. }
  148. }
  149. /*
  150. * To decode the stream:
  151. * if look_bits(2) == 0: # op == 2 in the lookup table
  152. * skip_bits(2)
  153. * end of the stream
  154. * elif look_bits(3) == 7: # op == 1 in the lookup table
  155. * skip_bits(3)
  156. * yyyy = get_bits(4)
  157. * xxxx = get_bits(8)
  158. * else: # op == 0 in the lookup table
  159. * skip_bits(x)
  160. *
  161. * For speedup processing, we build a lookup table and we takes the first 6 bits.
  162. *
  163. * struct {
  164. * unsigned char op; // operation to execute
  165. * unsigned char bits; // bits use to perform operation
  166. * unsigned char offset1; // offset to add to access in the table_0004 % 16
  167. * unsigned char offset2; // offset to add to access in the table_0004
  168. * }
  169. *
  170. * How to build this table ?
  171. * op == 2 when (i%4)==0
  172. * op == 1 when (i%8)==7
  173. * op == 0 otherwise
  174. *
  175. */
  176. static const unsigned char hash_table_ops[64*4] = {
  177. 0x02, 0x00, 0x00, 0x00,
  178. 0x00, 0x03, 0x01, 0x00,
  179. 0x00, 0x04, 0x01, 0x10,
  180. 0x00, 0x06, 0x01, 0x30,
  181. 0x02, 0x00, 0x00, 0x00,
  182. 0x00, 0x03, 0x01, 0x40,
  183. 0x00, 0x05, 0x01, 0x20,
  184. 0x01, 0x00, 0x00, 0x00,
  185. 0x02, 0x00, 0x00, 0x00,
  186. 0x00, 0x03, 0x01, 0x00,
  187. 0x00, 0x04, 0x01, 0x50,
  188. 0x00, 0x05, 0x02, 0x00,
  189. 0x02, 0x00, 0x00, 0x00,
  190. 0x00, 0x03, 0x01, 0x40,
  191. 0x00, 0x05, 0x03, 0x00,
  192. 0x01, 0x00, 0x00, 0x00,
  193. 0x02, 0x00, 0x00, 0x00,
  194. 0x00, 0x03, 0x01, 0x00,
  195. 0x00, 0x04, 0x01, 0x10,
  196. 0x00, 0x06, 0x02, 0x10,
  197. 0x02, 0x00, 0x00, 0x00,
  198. 0x00, 0x03, 0x01, 0x40,
  199. 0x00, 0x05, 0x01, 0x60,
  200. 0x01, 0x00, 0x00, 0x00,
  201. 0x02, 0x00, 0x00, 0x00,
  202. 0x00, 0x03, 0x01, 0x00,
  203. 0x00, 0x04, 0x01, 0x50,
  204. 0x00, 0x05, 0x02, 0x40,
  205. 0x02, 0x00, 0x00, 0x00,
  206. 0x00, 0x03, 0x01, 0x40,
  207. 0x00, 0x05, 0x03, 0x40,
  208. 0x01, 0x00, 0x00, 0x00,
  209. 0x02, 0x00, 0x00, 0x00,
  210. 0x00, 0x03, 0x01, 0x00,
  211. 0x00, 0x04, 0x01, 0x10,
  212. 0x00, 0x06, 0x01, 0x70,
  213. 0x02, 0x00, 0x00, 0x00,
  214. 0x00, 0x03, 0x01, 0x40,
  215. 0x00, 0x05, 0x01, 0x20,
  216. 0x01, 0x00, 0x00, 0x00,
  217. 0x02, 0x00, 0x00, 0x00,
  218. 0x00, 0x03, 0x01, 0x00,
  219. 0x00, 0x04, 0x01, 0x50,
  220. 0x00, 0x05, 0x02, 0x00,
  221. 0x02, 0x00, 0x00, 0x00,
  222. 0x00, 0x03, 0x01, 0x40,
  223. 0x00, 0x05, 0x03, 0x00,
  224. 0x01, 0x00, 0x00, 0x00,
  225. 0x02, 0x00, 0x00, 0x00,
  226. 0x00, 0x03, 0x01, 0x00,
  227. 0x00, 0x04, 0x01, 0x10,
  228. 0x00, 0x06, 0x02, 0x50,
  229. 0x02, 0x00, 0x00, 0x00,
  230. 0x00, 0x03, 0x01, 0x40,
  231. 0x00, 0x05, 0x01, 0x60,
  232. 0x01, 0x00, 0x00, 0x00,
  233. 0x02, 0x00, 0x00, 0x00,
  234. 0x00, 0x03, 0x01, 0x00,
  235. 0x00, 0x04, 0x01, 0x50,
  236. 0x00, 0x05, 0x02, 0x40,
  237. 0x02, 0x00, 0x00, 0x00,
  238. 0x00, 0x03, 0x01, 0x40,
  239. 0x00, 0x05, 0x03, 0x40,
  240. 0x01, 0x00, 0x00, 0x00
  241. };
  242. /*
  243. *
  244. */
  245. static const unsigned int MulIdx[16][16] = {
  246. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  247. {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3,},
  248. {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,},
  249. {4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,},
  250. {6, 7, 8, 9, 7, 10, 11, 8, 8, 11, 10, 7, 9, 8, 7, 6,},
  251. {4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4,},
  252. {1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2, 1, 3, 0, 2,},
  253. {0, 3, 3, 0, 1, 2, 2, 1, 2, 1, 1, 2, 3, 0, 0, 3,},
  254. {0, 1, 2, 3, 3, 2, 1, 0, 3, 2, 1, 0, 0, 1, 2, 3,},
  255. {1, 1, 1, 1, 3, 3, 3, 3, 0, 0, 0, 0, 2, 2, 2, 2,},
  256. {7, 10, 11, 8, 9, 8, 7, 6, 6, 7, 8, 9, 8, 11, 10, 7,},
  257. {4, 5, 5, 4, 5, 4, 4, 5, 5, 4, 4, 5, 4, 5, 5, 4,},
  258. {7, 9, 6, 8, 10, 8, 7, 11, 11, 7, 8, 10, 8, 6, 9, 7,},
  259. {1, 3, 0, 2, 2, 0, 3, 1, 2, 0, 3, 1, 1, 3, 0, 2,},
  260. {1, 2, 2, 1, 3, 0, 0, 3, 0, 3, 3, 0, 2, 1, 1, 2,},
  261. {10, 8, 7, 11, 8, 6, 9, 7, 7, 9, 6, 8, 11, 7, 8, 10}
  262. };
  263. #if USE_LOOKUP_TABLE_TO_CLAMP
  264. #define MAX_OUTER_CROP_VALUE (512)
  265. static unsigned char pwc_crop_table[256 + 2*MAX_OUTER_CROP_VALUE];
  266. #define CLAMP(x) (pwc_crop_table[MAX_OUTER_CROP_VALUE+(x)])
  267. #else
  268. #define CLAMP(x) ((x)>255?255:((x)<0?0:x))
  269. #endif
  270. /* If the type or the command change, we rebuild the lookup table */
  271. int pwc_dec23_init(struct pwc_device *pwc, int type, unsigned char *cmd)
  272. {
  273. int flags, version, shift, i;
  274. struct pwc_dec23_private *pdec;
  275. if (pwc->decompress_data == NULL) {
  276. pdec = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
  277. if (pdec == NULL)
  278. return -ENOMEM;
  279. pwc->decompress_data = pdec;
  280. }
  281. pdec = pwc->decompress_data;
  282. mutex_init(&pdec->lock);
  283. if (DEVICE_USE_CODEC3(type)) {
  284. flags = cmd[2] & 0x18;
  285. if (flags == 8)
  286. pdec->nbits = 7; /* More bits, mean more bits to encode the stream, but better quality */
  287. else if (flags == 0x10)
  288. pdec->nbits = 8;
  289. else
  290. pdec->nbits = 6;
  291. version = cmd[2] >> 5;
  292. build_table_color(KiaraRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
  293. build_table_color(KiaraRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
  294. } else {
  295. flags = cmd[2] & 6;
  296. if (flags == 2)
  297. pdec->nbits = 7;
  298. else if (flags == 4)
  299. pdec->nbits = 8;
  300. else
  301. pdec->nbits = 6;
  302. version = cmd[2] >> 3;
  303. build_table_color(TimonRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
  304. build_table_color(TimonRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
  305. }
  306. /* Informations can be coded on a variable number of bits but never less than 8 */
  307. shift = 8 - pdec->nbits;
  308. pdec->scalebits = SCALEBITS - shift;
  309. pdec->nbitsmask = 0xFF >> shift;
  310. fill_table_dc00_d800(pdec);
  311. build_subblock_pattern(pdec);
  312. build_bit_powermask_table(pdec);
  313. #if USE_LOOKUP_TABLE_TO_CLAMP
  314. /* Build the static table to clamp value [0-255] */
  315. for (i=0;i<MAX_OUTER_CROP_VALUE;i++)
  316. pwc_crop_table[i] = 0;
  317. for (i=0; i<256; i++)
  318. pwc_crop_table[MAX_OUTER_CROP_VALUE+i] = i;
  319. for (i=0; i<MAX_OUTER_CROP_VALUE; i++)
  320. pwc_crop_table[MAX_OUTER_CROP_VALUE+256+i] = 255;
  321. #endif
  322. return 0;
  323. }
  324. /*
  325. * Copy the 4x4 image block to Y plane buffer
  326. */
  327. static void copy_image_block_Y(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  328. {
  329. #if UNROLL_LOOP_FOR_COPY
  330. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  331. const int *c = src;
  332. unsigned char *d = dst;
  333. *d++ = cm[c[0] >> scalebits];
  334. *d++ = cm[c[1] >> scalebits];
  335. *d++ = cm[c[2] >> scalebits];
  336. *d++ = cm[c[3] >> scalebits];
  337. d = dst + bytes_per_line;
  338. *d++ = cm[c[4] >> scalebits];
  339. *d++ = cm[c[5] >> scalebits];
  340. *d++ = cm[c[6] >> scalebits];
  341. *d++ = cm[c[7] >> scalebits];
  342. d = dst + bytes_per_line*2;
  343. *d++ = cm[c[8] >> scalebits];
  344. *d++ = cm[c[9] >> scalebits];
  345. *d++ = cm[c[10] >> scalebits];
  346. *d++ = cm[c[11] >> scalebits];
  347. d = dst + bytes_per_line*3;
  348. *d++ = cm[c[12] >> scalebits];
  349. *d++ = cm[c[13] >> scalebits];
  350. *d++ = cm[c[14] >> scalebits];
  351. *d++ = cm[c[15] >> scalebits];
  352. #else
  353. int i;
  354. const int *c = src;
  355. unsigned char *d = dst;
  356. for (i = 0; i < 4; i++, c++)
  357. *d++ = CLAMP((*c) >> scalebits);
  358. d = dst + bytes_per_line;
  359. for (i = 0; i < 4; i++, c++)
  360. *d++ = CLAMP((*c) >> scalebits);
  361. d = dst + bytes_per_line*2;
  362. for (i = 0; i < 4; i++, c++)
  363. *d++ = CLAMP((*c) >> scalebits);
  364. d = dst + bytes_per_line*3;
  365. for (i = 0; i < 4; i++, c++)
  366. *d++ = CLAMP((*c) >> scalebits);
  367. #endif
  368. }
  369. /*
  370. * Copy the 4x4 image block to a CrCb plane buffer
  371. *
  372. */
  373. static void copy_image_block_CrCb(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  374. {
  375. #if UNROLL_LOOP_FOR_COPY
  376. /* Unroll all loops */
  377. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  378. const int *c = src;
  379. unsigned char *d = dst;
  380. *d++ = cm[c[0] >> scalebits];
  381. *d++ = cm[c[4] >> scalebits];
  382. *d++ = cm[c[1] >> scalebits];
  383. *d++ = cm[c[5] >> scalebits];
  384. *d++ = cm[c[2] >> scalebits];
  385. *d++ = cm[c[6] >> scalebits];
  386. *d++ = cm[c[3] >> scalebits];
  387. *d++ = cm[c[7] >> scalebits];
  388. d = dst + bytes_per_line;
  389. *d++ = cm[c[12] >> scalebits];
  390. *d++ = cm[c[8] >> scalebits];
  391. *d++ = cm[c[13] >> scalebits];
  392. *d++ = cm[c[9] >> scalebits];
  393. *d++ = cm[c[14] >> scalebits];
  394. *d++ = cm[c[10] >> scalebits];
  395. *d++ = cm[c[15] >> scalebits];
  396. *d++ = cm[c[11] >> scalebits];
  397. #else
  398. int i;
  399. const int *c1 = src;
  400. const int *c2 = src + 4;
  401. unsigned char *d = dst;
  402. for (i = 0; i < 4; i++, c1++, c2++) {
  403. *d++ = CLAMP((*c1) >> scalebits);
  404. *d++ = CLAMP((*c2) >> scalebits);
  405. }
  406. c1 = src + 12;
  407. d = dst + bytes_per_line;
  408. for (i = 0; i < 4; i++, c1++, c2++) {
  409. *d++ = CLAMP((*c1) >> scalebits);
  410. *d++ = CLAMP((*c2) >> scalebits);
  411. }
  412. #endif
  413. }
  414. /*
  415. * To manage the stream, we keep bits in a 32 bits register.
  416. * fill_nbits(n): fill the reservoir with at least n bits
  417. * skip_bits(n): discard n bits from the reservoir
  418. * get_bits(n): fill the reservoir, returns the first n bits and discard the
  419. * bits from the reservoir.
  420. * __get_nbits(n): faster version of get_bits(n), but asumes that the reservoir
  421. * contains at least n bits. bits returned is discarded.
  422. */
  423. #define fill_nbits(pdec, nbits_wanted) do { \
  424. while (pdec->nbits_in_reservoir<(nbits_wanted)) \
  425. { \
  426. pdec->reservoir |= (*(pdec->stream)++) << (pdec->nbits_in_reservoir); \
  427. pdec->nbits_in_reservoir += 8; \
  428. } \
  429. } while(0);
  430. #define skip_nbits(pdec, nbits_to_skip) do { \
  431. pdec->reservoir >>= (nbits_to_skip); \
  432. pdec->nbits_in_reservoir -= (nbits_to_skip); \
  433. } while(0);
  434. #define get_nbits(pdec, nbits_wanted, result) do { \
  435. fill_nbits(pdec, nbits_wanted); \
  436. result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
  437. skip_nbits(pdec, nbits_wanted); \
  438. } while(0);
  439. #define __get_nbits(pdec, nbits_wanted, result) do { \
  440. result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
  441. skip_nbits(pdec, nbits_wanted); \
  442. } while(0);
  443. #define look_nbits(pdec, nbits_wanted) \
  444. ((pdec->reservoir) & ((1U<<(nbits_wanted))-1))
  445. /*
  446. * Decode a 4x4 pixel block
  447. */
  448. static void decode_block(struct pwc_dec23_private *pdec,
  449. const unsigned char *ptable0004,
  450. const unsigned char *ptable8004)
  451. {
  452. unsigned int primary_color;
  453. unsigned int channel_v, offset1, op;
  454. int i;
  455. fill_nbits(pdec, 16);
  456. __get_nbits(pdec, pdec->nbits, primary_color);
  457. if (look_nbits(pdec,2) == 0) {
  458. skip_nbits(pdec, 2);
  459. /* Very simple, the color is the same for all pixels of the square */
  460. for (i = 0; i < 16; i++)
  461. pdec->temp_colors[i] = pdec->table_dc00[primary_color];
  462. return;
  463. }
  464. /* This block is encoded with small pattern */
  465. for (i = 0; i < 16; i++)
  466. pdec->temp_colors[i] = pdec->table_d800[primary_color];
  467. __get_nbits(pdec, 3, channel_v);
  468. channel_v = ((channel_v & 1) << 2) | (channel_v & 2) | ((channel_v & 4) >> 2);
  469. ptable0004 += (channel_v * 128);
  470. ptable8004 += (channel_v * 32);
  471. offset1 = 0;
  472. do
  473. {
  474. unsigned int htable_idx, rows = 0;
  475. const unsigned int *block;
  476. /* [ zzzz y x x ]
  477. * xx == 00 :=> end of the block def, remove the two bits from the stream
  478. * yxx == 111
  479. * yxx == any other value
  480. *
  481. */
  482. fill_nbits(pdec, 16);
  483. htable_idx = look_nbits(pdec, 6);
  484. op = hash_table_ops[htable_idx * 4];
  485. if (op == 2) {
  486. skip_nbits(pdec, 2);
  487. } else if (op == 1) {
  488. /* 15bits [ xxxx xxxx yyyy 111 ]
  489. * yyy => offset in the table8004
  490. * xxx => offset in the tabled004 (tree)
  491. */
  492. unsigned int mask, shift;
  493. unsigned int nbits, col1;
  494. unsigned int yyyy;
  495. skip_nbits(pdec, 3);
  496. /* offset1 += yyyy */
  497. __get_nbits(pdec, 4, yyyy);
  498. offset1 += 1 + yyyy;
  499. offset1 &= 0x0F;
  500. nbits = ptable8004[offset1 * 2];
  501. /* col1 = xxxx xxxx */
  502. __get_nbits(pdec, nbits+1, col1);
  503. /* Bit mask table */
  504. mask = pdec->table_bitpowermask[nbits][col1];
  505. shift = ptable8004[offset1 * 2 + 1];
  506. rows = ((mask << shift) + 0x80) & 0xFF;
  507. block = pdec->table_subblock[rows];
  508. for (i = 0; i < 16; i++)
  509. pdec->temp_colors[i] += block[MulIdx[offset1][i]];
  510. } else {
  511. /* op == 0
  512. * offset1 is coded on 3 bits
  513. */
  514. unsigned int shift;
  515. offset1 += hash_table_ops [htable_idx * 4 + 2];
  516. offset1 &= 0x0F;
  517. rows = ptable0004[offset1 + hash_table_ops [htable_idx * 4 + 3]];
  518. block = pdec->table_subblock[rows];
  519. for (i = 0; i < 16; i++)
  520. pdec->temp_colors[i] += block[MulIdx[offset1][i]];
  521. shift = hash_table_ops[htable_idx * 4 + 1];
  522. skip_nbits(pdec, shift);
  523. }
  524. } while (op != 2);
  525. }
  526. static void DecompressBand23(struct pwc_dec23_private *pdec,
  527. const unsigned char *rawyuv,
  528. unsigned char *planar_y,
  529. unsigned char *planar_u,
  530. unsigned char *planar_v,
  531. unsigned int compressed_image_width,
  532. unsigned int real_image_width)
  533. {
  534. int compression_index, nblocks;
  535. const unsigned char *ptable0004;
  536. const unsigned char *ptable8004;
  537. pdec->reservoir = 0;
  538. pdec->nbits_in_reservoir = 0;
  539. pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */
  540. get_nbits(pdec, 4, compression_index);
  541. /* pass 1: uncompress Y component */
  542. nblocks = compressed_image_width / 4;
  543. ptable0004 = pdec->table_0004_pass1[compression_index];
  544. ptable8004 = pdec->table_8004_pass1[compression_index];
  545. /* Each block decode a square of 4x4 */
  546. while (nblocks) {
  547. decode_block(pdec, ptable0004, ptable8004);
  548. copy_image_block_Y(pdec->temp_colors, planar_y, real_image_width, pdec->scalebits);
  549. planar_y += 4;
  550. nblocks--;
  551. }
  552. /* pass 2: uncompress UV component */
  553. nblocks = compressed_image_width / 8;
  554. ptable0004 = pdec->table_0004_pass2[compression_index];
  555. ptable8004 = pdec->table_8004_pass2[compression_index];
  556. /* Each block decode a square of 4x4 */
  557. while (nblocks) {
  558. decode_block(pdec, ptable0004, ptable8004);
  559. copy_image_block_CrCb(pdec->temp_colors, planar_u, real_image_width/2, pdec->scalebits);
  560. decode_block(pdec, ptable0004, ptable8004);
  561. copy_image_block_CrCb(pdec->temp_colors, planar_v, real_image_width/2, pdec->scalebits);
  562. planar_v += 8;
  563. planar_u += 8;
  564. nblocks -= 2;
  565. }
  566. }
  567. /**
  568. *
  569. * Uncompress a pwc23 buffer.
  570. *
  571. * pwc.view: size of the image wanted
  572. * pwc.image: size of the image returned by the camera
  573. * pwc.offset: (x,y) to displayer image in the view
  574. *
  575. * src: raw data
  576. * dst: image output
  577. */
  578. void pwc_dec23_decompress(const struct pwc_device *pwc,
  579. const void *src,
  580. void *dst)
  581. {
  582. int bandlines_left, stride, bytes_per_block;
  583. struct pwc_dec23_private *pdec = pwc->decompress_data;
  584. /* YUV420P image format */
  585. unsigned char *pout_planar_y;
  586. unsigned char *pout_planar_u;
  587. unsigned char *pout_planar_v;
  588. unsigned int plane_size;
  589. mutex_lock(&pdec->lock);
  590. bandlines_left = pwc->image.y / 4;
  591. bytes_per_block = pwc->view.x * 4;
  592. plane_size = pwc->view.x * pwc->view.y;
  593. /* offset in Y plane */
  594. stride = pwc->view.x * pwc->offset.y;
  595. pout_planar_y = dst + stride + pwc->offset.x;
  596. /* offsets in U/V planes */
  597. stride = (pwc->view.x * pwc->offset.y) / 4 + pwc->offset.x / 2;
  598. pout_planar_u = dst + plane_size + stride;
  599. pout_planar_v = dst + plane_size + plane_size / 4 + stride;
  600. while (bandlines_left--) {
  601. DecompressBand23(pwc->decompress_data,
  602. src,
  603. pout_planar_y, pout_planar_u, pout_planar_v,
  604. pwc->image.x, pwc->view.x);
  605. src += pwc->vbandlength;
  606. pout_planar_y += bytes_per_block;
  607. pout_planar_u += pwc->view.x;
  608. pout_planar_v += pwc->view.x;
  609. }
  610. mutex_unlock(&pdec->lock);
  611. }