pwc-dec23.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. mutex_init(&pdec->lock);
  289. if (DEVICE_USE_CODEC3(type)) {
  290. flags = cmd[2] & 0x18;
  291. if (flags == 8)
  292. pdec->nbits = 7; /* More bits, mean more bits to encode the stream, but better quality */
  293. else if (flags == 0x10)
  294. pdec->nbits = 8;
  295. else
  296. pdec->nbits = 6;
  297. version = cmd[2] >> 5;
  298. build_table_color(KiaraRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
  299. build_table_color(KiaraRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
  300. } else {
  301. flags = cmd[2] & 6;
  302. if (flags == 2)
  303. pdec->nbits = 7;
  304. else if (flags == 4)
  305. pdec->nbits = 8;
  306. else
  307. pdec->nbits = 6;
  308. version = cmd[2] >> 3;
  309. build_table_color(TimonRomTable[version][0], pdec->table_0004_pass1, pdec->table_8004_pass1);
  310. build_table_color(TimonRomTable[version][1], pdec->table_0004_pass2, pdec->table_8004_pass2);
  311. }
  312. /* Informations can be coded on a variable number of bits but never less than 8 */
  313. shift = 8 - pdec->nbits;
  314. pdec->scalebits = SCALEBITS - shift;
  315. pdec->nbitsmask = 0xFF >> shift;
  316. fill_table_dc00_d800(pdec);
  317. build_subblock_pattern(pdec);
  318. build_bit_powermask_table(pdec);
  319. #if USE_LOOKUP_TABLE_TO_CLAMP
  320. /* Build the static table to clamp value [0-255] */
  321. for (i=0;i<MAX_OUTER_CROP_VALUE;i++)
  322. pwc_crop_table[i] = 0;
  323. for (i=0; i<256; i++)
  324. pwc_crop_table[MAX_OUTER_CROP_VALUE+i] = i;
  325. for (i=0; i<MAX_OUTER_CROP_VALUE; i++)
  326. pwc_crop_table[MAX_OUTER_CROP_VALUE+256+i] = 255;
  327. #endif
  328. return 0;
  329. }
  330. /*
  331. * Copy the 4x4 image block to Y plane buffer
  332. */
  333. static void copy_image_block_Y(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  334. {
  335. #if UNROLL_LOOP_FOR_COPY
  336. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  337. const int *c = src;
  338. unsigned char *d = dst;
  339. *d++ = cm[c[0] >> scalebits];
  340. *d++ = cm[c[1] >> scalebits];
  341. *d++ = cm[c[2] >> scalebits];
  342. *d++ = cm[c[3] >> scalebits];
  343. d = dst + bytes_per_line;
  344. *d++ = cm[c[4] >> scalebits];
  345. *d++ = cm[c[5] >> scalebits];
  346. *d++ = cm[c[6] >> scalebits];
  347. *d++ = cm[c[7] >> scalebits];
  348. d = dst + bytes_per_line*2;
  349. *d++ = cm[c[8] >> scalebits];
  350. *d++ = cm[c[9] >> scalebits];
  351. *d++ = cm[c[10] >> scalebits];
  352. *d++ = cm[c[11] >> scalebits];
  353. d = dst + bytes_per_line*3;
  354. *d++ = cm[c[12] >> scalebits];
  355. *d++ = cm[c[13] >> scalebits];
  356. *d++ = cm[c[14] >> scalebits];
  357. *d++ = cm[c[15] >> scalebits];
  358. #else
  359. int i;
  360. const int *c = src;
  361. unsigned char *d = dst;
  362. for (i = 0; i < 4; i++, c++)
  363. *d++ = CLAMP((*c) >> scalebits);
  364. d = dst + bytes_per_line;
  365. for (i = 0; i < 4; i++, c++)
  366. *d++ = CLAMP((*c) >> scalebits);
  367. d = dst + bytes_per_line*2;
  368. for (i = 0; i < 4; i++, c++)
  369. *d++ = CLAMP((*c) >> scalebits);
  370. d = dst + bytes_per_line*3;
  371. for (i = 0; i < 4; i++, c++)
  372. *d++ = CLAMP((*c) >> scalebits);
  373. #endif
  374. }
  375. /*
  376. * Copy the 4x4 image block to a CrCb plane buffer
  377. *
  378. */
  379. static void copy_image_block_CrCb(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  380. {
  381. #if UNROLL_LOOP_FOR_COPY
  382. /* Unroll all loops */
  383. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  384. const int *c = src;
  385. unsigned char *d = dst;
  386. *d++ = cm[c[0] >> scalebits];
  387. *d++ = cm[c[4] >> scalebits];
  388. *d++ = cm[c[1] >> scalebits];
  389. *d++ = cm[c[5] >> scalebits];
  390. *d++ = cm[c[2] >> scalebits];
  391. *d++ = cm[c[6] >> scalebits];
  392. *d++ = cm[c[3] >> scalebits];
  393. *d++ = cm[c[7] >> scalebits];
  394. d = dst + bytes_per_line;
  395. *d++ = cm[c[12] >> scalebits];
  396. *d++ = cm[c[8] >> scalebits];
  397. *d++ = cm[c[13] >> scalebits];
  398. *d++ = cm[c[9] >> scalebits];
  399. *d++ = cm[c[14] >> scalebits];
  400. *d++ = cm[c[10] >> scalebits];
  401. *d++ = cm[c[15] >> scalebits];
  402. *d++ = cm[c[11] >> scalebits];
  403. #else
  404. int i;
  405. const int *c1 = src;
  406. const int *c2 = src + 4;
  407. unsigned char *d = dst;
  408. for (i = 0; i < 4; i++, c1++, c2++) {
  409. *d++ = CLAMP((*c1) >> scalebits);
  410. *d++ = CLAMP((*c2) >> scalebits);
  411. }
  412. c1 = src + 12;
  413. d = dst + bytes_per_line;
  414. for (i = 0; i < 4; i++, c1++, c2++) {
  415. *d++ = CLAMP((*c1) >> scalebits);
  416. *d++ = CLAMP((*c2) >> scalebits);
  417. }
  418. #endif
  419. }
  420. #if ENABLE_BAYER_DECODER
  421. /*
  422. * Format: 8x2 pixels
  423. * . G . G . G . G . G . G . G
  424. * . . . . . . . . . . . . . .
  425. * . G . G . G . G . G . G . G
  426. * . . . . . . . . . . . . . .
  427. * or
  428. * . . . . . . . . . . . . . .
  429. * G . G . G . G . G . G . G .
  430. * . . . . . . . . . . . . . .
  431. * G . G . G . G . G . G . G .
  432. */
  433. static void copy_image_block_Green(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  434. {
  435. #if UNROLL_LOOP_FOR_COPY
  436. /* Unroll all loops */
  437. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  438. unsigned char *d = dst;
  439. const int *c = src;
  440. d[0] = cm[c[0] >> scalebits];
  441. d[2] = cm[c[1] >> scalebits];
  442. d[4] = cm[c[2] >> scalebits];
  443. d[6] = cm[c[3] >> scalebits];
  444. d[8] = cm[c[4] >> scalebits];
  445. d[10] = cm[c[5] >> scalebits];
  446. d[12] = cm[c[6] >> scalebits];
  447. d[14] = cm[c[7] >> scalebits];
  448. d = dst + bytes_per_line;
  449. d[0] = cm[c[8] >> scalebits];
  450. d[2] = cm[c[9] >> scalebits];
  451. d[4] = cm[c[10] >> scalebits];
  452. d[6] = cm[c[11] >> scalebits];
  453. d[8] = cm[c[12] >> scalebits];
  454. d[10] = cm[c[13] >> scalebits];
  455. d[12] = cm[c[14] >> scalebits];
  456. d[14] = cm[c[15] >> scalebits];
  457. #else
  458. int i;
  459. unsigned char *d;
  460. const int *c = src;
  461. d = dst;
  462. for (i = 0; i < 8; i++, c++)
  463. d[i*2] = CLAMP((*c) >> scalebits);
  464. d = dst + bytes_per_line;
  465. for (i = 0; i < 8; i++, c++)
  466. d[i*2] = CLAMP((*c) >> scalebits);
  467. #endif
  468. }
  469. #endif
  470. #if ENABLE_BAYER_DECODER
  471. /*
  472. * Format: 4x4 pixels
  473. * R . R . R . R
  474. * . B . B . B .
  475. * R . R . R . R
  476. * . B . B . B .
  477. */
  478. static void copy_image_block_RedBlue(const int *src, unsigned char *dst, unsigned int bytes_per_line, unsigned int scalebits)
  479. {
  480. #if UNROLL_LOOP_FOR_COPY
  481. /* Unroll all loops */
  482. const unsigned char *cm = pwc_crop_table+MAX_OUTER_CROP_VALUE;
  483. unsigned char *d = dst;
  484. const int *c = src;
  485. d[0] = cm[c[0] >> scalebits];
  486. d[2] = cm[c[1] >> scalebits];
  487. d[4] = cm[c[2] >> scalebits];
  488. d[6] = cm[c[3] >> scalebits];
  489. d = dst + bytes_per_line;
  490. d[1] = cm[c[4] >> scalebits];
  491. d[3] = cm[c[5] >> scalebits];
  492. d[5] = cm[c[6] >> scalebits];
  493. d[7] = cm[c[7] >> scalebits];
  494. d = dst + bytes_per_line*2;
  495. d[0] = cm[c[8] >> scalebits];
  496. d[2] = cm[c[9] >> scalebits];
  497. d[4] = cm[c[10] >> scalebits];
  498. d[6] = cm[c[11] >> scalebits];
  499. d = dst + bytes_per_line*3;
  500. d[1] = cm[c[12] >> scalebits];
  501. d[3] = cm[c[13] >> scalebits];
  502. d[5] = cm[c[14] >> scalebits];
  503. d[7] = cm[c[15] >> scalebits];
  504. #else
  505. int i;
  506. unsigned char *d;
  507. const int *c = src;
  508. d = dst;
  509. for (i = 0; i < 4; i++, c++)
  510. d[i*2] = CLAMP((*c) >> scalebits);
  511. d = dst + bytes_per_line;
  512. for (i = 0; i < 4; i++, c++)
  513. d[i*2+1] = CLAMP((*c) >> scalebits);
  514. d = dst + bytes_per_line*2;
  515. for (i = 0; i < 4; i++, c++)
  516. d[i*2] = CLAMP((*c) >> scalebits);
  517. d = dst + bytes_per_line*3;
  518. for (i = 0; i < 4; i++, c++)
  519. d[i*2+1] = CLAMP((*c) >> scalebits);
  520. #endif
  521. }
  522. #endif
  523. /*
  524. * To manage the stream, we keep bits in a 32 bits register.
  525. * fill_nbits(n): fill the reservoir with at least n bits
  526. * skip_bits(n): discard n bits from the reservoir
  527. * get_bits(n): fill the reservoir, returns the first n bits and discard the
  528. * bits from the reservoir.
  529. * __get_nbits(n): faster version of get_bits(n), but asumes that the reservoir
  530. * contains at least n bits. bits returned is discarded.
  531. */
  532. #define fill_nbits(pdec, nbits_wanted) do { \
  533. while (pdec->nbits_in_reservoir<(nbits_wanted)) \
  534. { \
  535. pdec->reservoir |= (*(pdec->stream)++) << (pdec->nbits_in_reservoir); \
  536. pdec->nbits_in_reservoir += 8; \
  537. } \
  538. } while(0);
  539. #define skip_nbits(pdec, nbits_to_skip) do { \
  540. pdec->reservoir >>= (nbits_to_skip); \
  541. pdec->nbits_in_reservoir -= (nbits_to_skip); \
  542. } while(0);
  543. #define get_nbits(pdec, nbits_wanted, result) do { \
  544. fill_nbits(pdec, nbits_wanted); \
  545. result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
  546. skip_nbits(pdec, nbits_wanted); \
  547. } while(0);
  548. #define __get_nbits(pdec, nbits_wanted, result) do { \
  549. result = (pdec->reservoir) & ((1U<<(nbits_wanted))-1); \
  550. skip_nbits(pdec, nbits_wanted); \
  551. } while(0);
  552. #define look_nbits(pdec, nbits_wanted) \
  553. ((pdec->reservoir) & ((1U<<(nbits_wanted))-1))
  554. /*
  555. * Decode a 4x4 pixel block
  556. */
  557. static void decode_block(struct pwc_dec23_private *pdec,
  558. const unsigned char *ptable0004,
  559. const unsigned char *ptable8004)
  560. {
  561. unsigned int primary_color;
  562. unsigned int channel_v, offset1, op;
  563. int i;
  564. fill_nbits(pdec, 16);
  565. __get_nbits(pdec, pdec->nbits, primary_color);
  566. if (look_nbits(pdec,2) == 0) {
  567. skip_nbits(pdec, 2);
  568. /* Very simple, the color is the same for all pixels of the square */
  569. for (i = 0; i < 16; i++)
  570. pdec->temp_colors[i] = pdec->table_dc00[primary_color];
  571. return;
  572. }
  573. /* This block is encoded with small pattern */
  574. for (i = 0; i < 16; i++)
  575. pdec->temp_colors[i] = pdec->table_d800[primary_color];
  576. __get_nbits(pdec, 3, channel_v);
  577. channel_v = ((channel_v & 1) << 2) | (channel_v & 2) | ((channel_v & 4) >> 2);
  578. ptable0004 += (channel_v * 128);
  579. ptable8004 += (channel_v * 32);
  580. offset1 = 0;
  581. do
  582. {
  583. unsigned int htable_idx, rows = 0;
  584. const unsigned int *block;
  585. /* [ zzzz y x x ]
  586. * xx == 00 :=> end of the block def, remove the two bits from the stream
  587. * yxx == 111
  588. * yxx == any other value
  589. *
  590. */
  591. fill_nbits(pdec, 16);
  592. htable_idx = look_nbits(pdec, 6);
  593. op = hash_table_ops[htable_idx * 4];
  594. if (op == 2) {
  595. skip_nbits(pdec, 2);
  596. } else if (op == 1) {
  597. /* 15bits [ xxxx xxxx yyyy 111 ]
  598. * yyy => offset in the table8004
  599. * xxx => offset in the tabled004 (tree)
  600. */
  601. unsigned int mask, shift;
  602. unsigned int nbits, col1;
  603. unsigned int yyyy;
  604. skip_nbits(pdec, 3);
  605. /* offset1 += yyyy */
  606. __get_nbits(pdec, 4, yyyy);
  607. offset1 += 1 + yyyy;
  608. offset1 &= 0x0F;
  609. nbits = ptable8004[offset1 * 2];
  610. /* col1 = xxxx xxxx */
  611. __get_nbits(pdec, nbits+1, col1);
  612. /* Bit mask table */
  613. mask = pdec->table_bitpowermask[nbits][col1];
  614. shift = ptable8004[offset1 * 2 + 1];
  615. rows = ((mask << shift) + 0x80) & 0xFF;
  616. block = pdec->table_subblock[rows];
  617. for (i = 0; i < 16; i++)
  618. pdec->temp_colors[i] += block[MulIdx[offset1][i]];
  619. } else {
  620. /* op == 0
  621. * offset1 is coded on 3 bits
  622. */
  623. unsigned int shift;
  624. offset1 += hash_table_ops [htable_idx * 4 + 2];
  625. offset1 &= 0x0F;
  626. rows = ptable0004[offset1 + hash_table_ops [htable_idx * 4 + 3]];
  627. block = pdec->table_subblock[rows];
  628. for (i = 0; i < 16; i++)
  629. pdec->temp_colors[i] += block[MulIdx[offset1][i]];
  630. shift = hash_table_ops[htable_idx * 4 + 1];
  631. skip_nbits(pdec, shift);
  632. }
  633. } while (op != 2);
  634. }
  635. static void DecompressBand23(struct pwc_dec23_private *pdec,
  636. const unsigned char *rawyuv,
  637. unsigned char *planar_y,
  638. unsigned char *planar_u,
  639. unsigned char *planar_v,
  640. unsigned int compressed_image_width,
  641. unsigned int real_image_width)
  642. {
  643. int compression_index, nblocks;
  644. const unsigned char *ptable0004;
  645. const unsigned char *ptable8004;
  646. pdec->reservoir = 0;
  647. pdec->nbits_in_reservoir = 0;
  648. pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */
  649. get_nbits(pdec, 4, compression_index);
  650. /* pass 1: uncompress Y component */
  651. nblocks = compressed_image_width / 4;
  652. ptable0004 = pdec->table_0004_pass1[compression_index];
  653. ptable8004 = pdec->table_8004_pass1[compression_index];
  654. /* Each block decode a square of 4x4 */
  655. while (nblocks) {
  656. decode_block(pdec, ptable0004, ptable8004);
  657. copy_image_block_Y(pdec->temp_colors, planar_y, real_image_width, pdec->scalebits);
  658. planar_y += 4;
  659. nblocks--;
  660. }
  661. /* pass 2: uncompress UV component */
  662. nblocks = compressed_image_width / 8;
  663. ptable0004 = pdec->table_0004_pass2[compression_index];
  664. ptable8004 = pdec->table_8004_pass2[compression_index];
  665. /* Each block decode a square of 4x4 */
  666. while (nblocks) {
  667. decode_block(pdec, ptable0004, ptable8004);
  668. copy_image_block_CrCb(pdec->temp_colors, planar_u, real_image_width/2, pdec->scalebits);
  669. decode_block(pdec, ptable0004, ptable8004);
  670. copy_image_block_CrCb(pdec->temp_colors, planar_v, real_image_width/2, pdec->scalebits);
  671. planar_v += 8;
  672. planar_u += 8;
  673. nblocks -= 2;
  674. }
  675. }
  676. #if ENABLE_BAYER_DECODER
  677. /*
  678. * Size need to be a multiple of 8 in width
  679. *
  680. * Return a block of four line encoded like this:
  681. *
  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. * G R G R G R G R G R G R G R G R
  685. * B G B G B G B G B G B G B G B G
  686. *
  687. */
  688. static void DecompressBandBayer(struct pwc_dec23_private *pdec,
  689. const unsigned char *rawyuv,
  690. unsigned char *rgbbayer,
  691. unsigned int compressed_image_width,
  692. unsigned int real_image_width)
  693. {
  694. int compression_index, nblocks;
  695. const unsigned char *ptable0004;
  696. const unsigned char *ptable8004;
  697. unsigned char *dest;
  698. pdec->reservoir = 0;
  699. pdec->nbits_in_reservoir = 0;
  700. pdec->stream = rawyuv + 1; /* The first byte of the stream is skipped */
  701. get_nbits(pdec, 4, compression_index);
  702. /* pass 1: uncompress RB component */
  703. nblocks = compressed_image_width / 4;
  704. ptable0004 = pdec->table_0004_pass1[compression_index];
  705. ptable8004 = pdec->table_8004_pass1[compression_index];
  706. dest = rgbbayer;
  707. /* Each block decode a square of 4x4 */
  708. while (nblocks) {
  709. decode_block(pdec, ptable0004, ptable8004);
  710. copy_image_block_RedBlue(pdec->temp_colors, rgbbayer, real_image_width, pdec->scalebits);
  711. dest += 8;
  712. nblocks--;
  713. }
  714. /* pass 2: uncompress G component */
  715. nblocks = compressed_image_width / 8;
  716. ptable0004 = pdec->table_0004_pass2[compression_index];
  717. ptable8004 = pdec->table_8004_pass2[compression_index];
  718. /* Each block decode a square of 4x4 */
  719. while (nblocks) {
  720. decode_block(pdec, ptable0004, ptable8004);
  721. copy_image_block_Green(pdec->temp_colors, rgbbayer+1, real_image_width, pdec->scalebits);
  722. decode_block(pdec, ptable0004, ptable8004);
  723. copy_image_block_Green(pdec->temp_colors, rgbbayer+real_image_width, real_image_width, pdec->scalebits);
  724. rgbbayer += 16;
  725. nblocks -= 2;
  726. }
  727. }
  728. #endif
  729. /**
  730. *
  731. * Uncompress a pwc23 buffer.
  732. *
  733. * pwc.view: size of the image wanted
  734. * pwc.image: size of the image returned by the camera
  735. * pwc.offset: (x,y) to displayer image in the view
  736. *
  737. * src: raw data
  738. * dst: image output
  739. * flags: PWCX_FLAG_PLANAR or PWCX_FLAG_BAYER
  740. */
  741. void pwc_dec23_decompress(const struct pwc_device *pwc,
  742. const void *src,
  743. void *dst,
  744. int flags)
  745. {
  746. int bandlines_left, stride, bytes_per_block;
  747. struct pwc_dec23_private *pdec = pwc->decompress_data;
  748. mutex_lock(&pdec->lock);
  749. bandlines_left = pwc->image.y / 4;
  750. bytes_per_block = pwc->view.x * 4;
  751. if (flags & PWCX_FLAG_BAYER) {
  752. #if ENABLE_BAYER_DECODER
  753. /* RGB Bayer format */
  754. unsigned char *rgbout;
  755. stride = pwc->view.x * pwc->offset.y;
  756. rgbout = dst + stride + pwc->offset.x;
  757. while (bandlines_left--) {
  758. DecompressBandBayer(pwc->decompress_data,
  759. src,
  760. rgbout,
  761. pwc->image.x, pwc->view.x);
  762. src += pwc->vbandlength;
  763. rgbout += bytes_per_block;
  764. }
  765. #else
  766. memset(dst, 0, pwc->view.x * pwc->view.y);
  767. #endif
  768. } else {
  769. /* YUV420P image format */
  770. unsigned char *pout_planar_y;
  771. unsigned char *pout_planar_u;
  772. unsigned char *pout_planar_v;
  773. unsigned int plane_size;
  774. plane_size = pwc->view.x * pwc->view.y;
  775. /* offset in Y plane */
  776. stride = pwc->view.x * pwc->offset.y;
  777. pout_planar_y = dst + stride + pwc->offset.x;
  778. /* offsets in U/V planes */
  779. stride = (pwc->view.x * pwc->offset.y) / 4 + pwc->offset.x / 2;
  780. pout_planar_u = dst + plane_size + stride;
  781. pout_planar_v = dst + plane_size + plane_size / 4 + stride;
  782. while (bandlines_left--) {
  783. DecompressBand23(pwc->decompress_data,
  784. src,
  785. pout_planar_y, pout_planar_u, pout_planar_v,
  786. pwc->image.x, pwc->view.x);
  787. src += pwc->vbandlength;
  788. pout_planar_y += bytes_per_block;
  789. pout_planar_u += pwc->view.x;
  790. pout_planar_v += pwc->view.x;
  791. }
  792. }
  793. mutex_unlock(&pdec->lock);
  794. }