pwc-dec23.c 25 KB

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