pwc-dec23.c 19 KB

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