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