c2p.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Fast C2P (Chunky-to-Planar) Conversion
  3. *
  4. * Copyright (C) 2003-2008 Geert Uytterhoeven
  5. *
  6. * NOTES:
  7. * - This code was inspired by Scout's C2P tutorial
  8. * - It assumes to run on a big endian system
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file COPYING in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <asm/unaligned.h>
  17. #include "c2p.h"
  18. /*
  19. * Basic transpose step
  20. */
  21. static inline void _transp(u32 d[], unsigned int i1, unsigned int i2,
  22. unsigned int shift, u32 mask)
  23. {
  24. u32 t = (d[i1] ^ (d[i2] >> shift)) & mask;
  25. d[i1] ^= t;
  26. d[i2] ^= t << shift;
  27. }
  28. extern void c2p_unsupported(void);
  29. static inline u32 get_mask(unsigned int n)
  30. {
  31. switch (n) {
  32. case 1:
  33. return 0x55555555;
  34. case 2:
  35. return 0x33333333;
  36. case 4:
  37. return 0x0f0f0f0f;
  38. case 8:
  39. return 0x00ff00ff;
  40. case 16:
  41. return 0x0000ffff;
  42. }
  43. c2p_unsupported();
  44. return 0;
  45. }
  46. static inline void transp8(u32 d[], unsigned int n, unsigned int m)
  47. {
  48. u32 mask = get_mask(n);
  49. switch (m) {
  50. case 1:
  51. /* First n x 1 block */
  52. _transp(d, 0, 1, n, mask);
  53. /* Second n x 1 block */
  54. _transp(d, 2, 3, n, mask);
  55. /* Third n x 1 block */
  56. _transp(d, 4, 5, n, mask);
  57. /* Fourth n x 1 block */
  58. _transp(d, 6, 7, n, mask);
  59. return;
  60. case 2:
  61. /* First n x 2 block */
  62. _transp(d, 0, 2, n, mask);
  63. _transp(d, 1, 3, n, mask);
  64. /* Second n x 2 block */
  65. _transp(d, 4, 6, n, mask);
  66. _transp(d, 5, 7, n, mask);
  67. return;
  68. case 4:
  69. /* Single n x 4 block */
  70. _transp(d, 0, 4, n, mask);
  71. _transp(d, 1, 5, n, mask);
  72. _transp(d, 2, 6, n, mask);
  73. _transp(d, 3, 7, n, mask);
  74. return;
  75. }
  76. c2p_unsupported();
  77. }
  78. /*
  79. * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
  80. * containing
  81. * - 32 8-bit chunky pixels on input
  82. * - permutated planar data (1 plane per 32-bit word) on output
  83. */
  84. static void c2p_32x8(u32 d[8])
  85. {
  86. transp8(d, 16, 4);
  87. transp8(d, 8, 2);
  88. transp8(d, 4, 1);
  89. transp8(d, 2, 4);
  90. transp8(d, 1, 2);
  91. }
  92. /*
  93. * Array containing the permutation indices of the planar data after c2p
  94. */
  95. static const int perm_c2p_32x8[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
  96. /*
  97. * Compose two values, using a bitmask as decision value
  98. * This is equivalent to (a & mask) | (b & ~mask)
  99. */
  100. static inline u32 comp(u32 a, u32 b, u32 mask)
  101. {
  102. return ((a ^ b) & mask) ^ b;
  103. }
  104. /*
  105. * Store a full block of planar data after c2p conversion
  106. */
  107. static inline void store_planar(void *dst, u32 dst_inc, u32 bpp, u32 d[8])
  108. {
  109. int i;
  110. for (i = 0; i < bpp; i++, dst += dst_inc)
  111. put_unaligned_be32(d[perm_c2p_32x8[i]], dst);
  112. }
  113. /*
  114. * Store a partial block of planar data after c2p conversion
  115. */
  116. static inline void store_planar_masked(void *dst, u32 dst_inc, u32 bpp,
  117. u32 d[8], u32 mask)
  118. {
  119. int i;
  120. for (i = 0; i < bpp; i++, dst += dst_inc)
  121. put_unaligned_be32(comp(d[perm_c2p_32x8[i]],
  122. get_unaligned_be32(dst), mask),
  123. dst);
  124. }
  125. /*
  126. * c2p - Copy 8-bit chunky image data to a planar frame buffer
  127. * @dst: Starting address of the planar frame buffer
  128. * @dx: Horizontal destination offset (in pixels)
  129. * @dy: Vertical destination offset (in pixels)
  130. * @width: Image width (in pixels)
  131. * @height: Image height (in pixels)
  132. * @dst_nextline: Frame buffer offset to the next line (in bytes)
  133. * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
  134. * @src_nextline: Image offset to the next line (in bytes)
  135. * @bpp: Bits per pixel of the planar frame buffer (1-8)
  136. */
  137. void c2p(void *dst, const void *src, u32 dx, u32 dy, u32 width, u32 height,
  138. u32 dst_nextline, u32 dst_nextplane, u32 src_nextline, u32 bpp)
  139. {
  140. union {
  141. u8 pixels[32];
  142. u32 words[8];
  143. } d;
  144. u32 dst_idx, first, last, w;
  145. const u8 *c;
  146. void *p;
  147. dst += dy*dst_nextline+(dx & ~31);
  148. dst_idx = dx % 32;
  149. first = 0xffffffffU >> dst_idx;
  150. last = ~(0xffffffffU >> ((dst_idx+width) % 32));
  151. while (height--) {
  152. c = src;
  153. p = dst;
  154. w = width;
  155. if (dst_idx+width <= 32) {
  156. /* Single destination word */
  157. first &= last;
  158. memset(d.pixels, 0, sizeof(d));
  159. memcpy(d.pixels+dst_idx, c, width);
  160. c += width;
  161. c2p_32x8(d.words);
  162. store_planar_masked(p, dst_nextplane, bpp, d.words,
  163. first);
  164. p += 4;
  165. } else {
  166. /* Multiple destination words */
  167. w = width;
  168. /* Leading bits */
  169. if (dst_idx) {
  170. w = 32 - dst_idx;
  171. memset(d.pixels, 0, dst_idx);
  172. memcpy(d.pixels+dst_idx, c, w);
  173. c += w;
  174. c2p_32x8(d.words);
  175. store_planar_masked(p, dst_nextplane, bpp,
  176. d.words, first);
  177. p += 4;
  178. w = width-w;
  179. }
  180. /* Main chunk */
  181. while (w >= 32) {
  182. memcpy(d.pixels, c, 32);
  183. c += 32;
  184. c2p_32x8(d.words);
  185. store_planar(p, dst_nextplane, bpp, d.words);
  186. p += 4;
  187. w -= 32;
  188. }
  189. /* Trailing bits */
  190. w %= 32;
  191. if (w > 0) {
  192. memcpy(d.pixels, c, w);
  193. memset(d.pixels+w, 0, 32-w);
  194. c2p_32x8(d.words);
  195. store_planar_masked(p, dst_nextplane, bpp,
  196. d.words, last);
  197. }
  198. }
  199. src += src_nextline;
  200. dst += dst_nextline;
  201. }
  202. }
  203. EXPORT_SYMBOL_GPL(c2p);
  204. MODULE_LICENSE("GPL");