pwc-dec23.c 25 KB

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