c2p_planar.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Fast C2P (Chunky-to-Planar) Conversion
  3. *
  4. * Copyright (C) 2003-2008 Geert Uytterhoeven
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <asm/unaligned.h>
  13. #include "c2p.h"
  14. #include "c2p_core.h"
  15. /*
  16. * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
  17. * containing
  18. * - 32 8-bit chunky pixels on input
  19. * - permutated planar data (1 plane per 32-bit word) on output
  20. */
  21. static void c2p_32x8(u32 d[8])
  22. {
  23. transp8(d, 16, 4);
  24. transp8(d, 8, 2);
  25. transp8(d, 4, 1);
  26. transp8(d, 2, 4);
  27. transp8(d, 1, 2);
  28. }
  29. /*
  30. * Array containing the permutation indices of the planar data after c2p
  31. */
  32. static const int perm_c2p_32x8[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
  33. /*
  34. * Store a full block of planar data after c2p conversion
  35. */
  36. static inline void store_planar(void *dst, u32 dst_inc, u32 bpp, u32 d[8])
  37. {
  38. int i;
  39. for (i = 0; i < bpp; i++, dst += dst_inc)
  40. put_unaligned_be32(d[perm_c2p_32x8[i]], dst);
  41. }
  42. /*
  43. * Store a partial block of planar data after c2p conversion
  44. */
  45. static inline void store_planar_masked(void *dst, u32 dst_inc, u32 bpp,
  46. u32 d[8], u32 mask)
  47. {
  48. int i;
  49. for (i = 0; i < bpp; i++, dst += dst_inc)
  50. put_unaligned_be32(comp(d[perm_c2p_32x8[i]],
  51. get_unaligned_be32(dst), mask),
  52. dst);
  53. }
  54. /*
  55. * c2p_planar - Copy 8-bit chunky image data to a planar frame buffer
  56. * @dst: Starting address of the planar frame buffer
  57. * @dx: Horizontal destination offset (in pixels)
  58. * @dy: Vertical destination offset (in pixels)
  59. * @width: Image width (in pixels)
  60. * @height: Image height (in pixels)
  61. * @dst_nextline: Frame buffer offset to the next line (in bytes)
  62. * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
  63. * @src_nextline: Image offset to the next line (in bytes)
  64. * @bpp: Bits per pixel of the planar frame buffer (1-8)
  65. */
  66. void c2p_planar(void *dst, const void *src, u32 dx, u32 dy, u32 width,
  67. u32 height, u32 dst_nextline, u32 dst_nextplane,
  68. u32 src_nextline, u32 bpp)
  69. {
  70. union {
  71. u8 pixels[32];
  72. u32 words[8];
  73. } d;
  74. u32 dst_idx, first, last, w;
  75. const u8 *c;
  76. void *p;
  77. dst += dy*dst_nextline+(dx & ~31);
  78. dst_idx = dx % 32;
  79. first = 0xffffffffU >> dst_idx;
  80. last = ~(0xffffffffU >> ((dst_idx+width) % 32));
  81. while (height--) {
  82. c = src;
  83. p = dst;
  84. w = width;
  85. if (dst_idx+width <= 32) {
  86. /* Single destination word */
  87. first &= last;
  88. memset(d.pixels, 0, sizeof(d));
  89. memcpy(d.pixels+dst_idx, c, width);
  90. c += width;
  91. c2p_32x8(d.words);
  92. store_planar_masked(p, dst_nextplane, bpp, d.words,
  93. first);
  94. p += 4;
  95. } else {
  96. /* Multiple destination words */
  97. w = width;
  98. /* Leading bits */
  99. if (dst_idx) {
  100. w = 32 - dst_idx;
  101. memset(d.pixels, 0, dst_idx);
  102. memcpy(d.pixels+dst_idx, c, w);
  103. c += w;
  104. c2p_32x8(d.words);
  105. store_planar_masked(p, dst_nextplane, bpp,
  106. d.words, first);
  107. p += 4;
  108. w = width-w;
  109. }
  110. /* Main chunk */
  111. while (w >= 32) {
  112. memcpy(d.pixels, c, 32);
  113. c += 32;
  114. c2p_32x8(d.words);
  115. store_planar(p, dst_nextplane, bpp, d.words);
  116. p += 4;
  117. w -= 32;
  118. }
  119. /* Trailing bits */
  120. w %= 32;
  121. if (w > 0) {
  122. memcpy(d.pixels, c, w);
  123. memset(d.pixels+w, 0, 32-w);
  124. c2p_32x8(d.words);
  125. store_planar_masked(p, dst_nextplane, bpp,
  126. d.words, last);
  127. }
  128. }
  129. src += src_nextline;
  130. dst += dst_nextline;
  131. }
  132. }
  133. EXPORT_SYMBOL_GPL(c2p_planar);
  134. MODULE_LICENSE("GPL");