c2p_planar.c 3.7 KB

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