blob: c170fff0d35e3e9f872b1eea2cd2fa25718cd821 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast C2P (Chunky-to-Planar) Conversion
3 *
Geert Uytterhoeven1f034452009-01-04 11:43:00 +01004 * Copyright (C) 2003-2008 Geert Uytterhoeven
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Adrian Bunk8b54b612008-07-17 21:16:20 +020015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010017
18#include <asm/unaligned.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "c2p.h"
21
22
23 /*
24 * Basic transpose step
25 */
26
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010027static inline void _transp(u32 d[], unsigned int i1, unsigned int i2,
28 unsigned int shift, u32 mask)
29{
30 u32 t = (d[i1] ^ (d[i2] >> shift)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010032 d[i1] ^= t;
33 d[i2] ^= t << shift;
34}
35
36extern void c2p_unsupported(void);
37
38static inline u32 get_mask(unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010040 switch (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 case 1:
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010042 return 0x55555555;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 case 2:
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010045 return 0x33333333;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 case 4:
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010048 return 0x0f0f0f0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 case 8:
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010051 return 0x00ff00ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 case 16:
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010054 return 0x0000ffff;
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010055 }
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010056
57 c2p_unsupported();
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010061static inline void transp8(u32 d[], unsigned int n, unsigned int m)
62{
63 u32 mask = get_mask(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010065 switch (m) {
66 case 1:
67 /* First n x 1 block */
68 _transp(d, 0, 1, n, mask);
69 /* Second n x 1 block */
70 _transp(d, 2, 3, n, mask);
71 /* Third n x 1 block */
72 _transp(d, 4, 5, n, mask);
73 /* Fourth n x 1 block */
74 _transp(d, 6, 7, n, mask);
75 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010077 case 2:
78 /* First n x 2 block */
79 _transp(d, 0, 2, n, mask);
80 _transp(d, 1, 3, n, mask);
81 /* Second n x 2 block */
82 _transp(d, 4, 6, n, mask);
83 _transp(d, 5, 7, n, mask);
84 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010086 case 4:
87 /* Single n x 4 block */
88 _transp(d, 0, 4, n, mask);
89 _transp(d, 1, 5, n, mask);
90 _transp(d, 2, 6, n, mask);
91 _transp(d, 3, 7, n, mask);
92 return;
93 }
94
95 c2p_unsupported();
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98
99 /*
100 * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
101 * containing
102 * - 32 8-bit chunky pixels on input
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100103 * - permutated planar data (1 plane per 32-bit word) on output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100106static void c2p_32x8(u32 d[8])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100108 transp8(d, 16, 4);
109 transp8(d, 8, 2);
110 transp8(d, 4, 1);
111 transp8(d, 2, 4);
112 transp8(d, 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115
116 /*
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100117 * Array containing the permutation indices of the planar data after c2p
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100120static const int perm_c2p_32x8[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122
123 /*
124 * Compose two values, using a bitmask as decision value
125 * This is equivalent to (a & mask) | (b & ~mask)
126 */
127
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100128static inline u32 comp(u32 a, u32 b, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 return ((a ^ b) & mask) ^ b;
131}
132
133
134 /*
135 * Store a full block of planar data after c2p conversion
136 */
137
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100138static inline void store_planar(void *dst, u32 dst_inc, u32 bpp, u32 d[8])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100140 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100142 for (i = 0; i < bpp; i++, dst += dst_inc)
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100143 put_unaligned_be32(d[perm_c2p_32x8[i]], dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146
147 /*
148 * Store a partial block of planar data after c2p conversion
149 */
150
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100151static inline void store_planar_masked(void *dst, u32 dst_inc, u32 bpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 u32 d[8], u32 mask)
153{
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100154 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100156 for (i = 0; i < bpp; i++, dst += dst_inc)
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100157 put_unaligned_be32(comp(d[perm_c2p_32x8[i]],
158 get_unaligned_be32(dst), mask),
159 dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162
163 /*
164 * c2p - Copy 8-bit chunky image data to a planar frame buffer
165 * @dst: Starting address of the planar frame buffer
166 * @dx: Horizontal destination offset (in pixels)
167 * @dy: Vertical destination offset (in pixels)
168 * @width: Image width (in pixels)
169 * @height: Image height (in pixels)
170 * @dst_nextline: Frame buffer offset to the next line (in bytes)
171 * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
172 * @src_nextline: Image offset to the next line (in bytes)
173 * @bpp: Bits per pixel of the planar frame buffer (1-8)
174 */
175
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100176void c2p(void *dst, const void *src, u32 dx, u32 dy, u32 width, u32 height,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 u32 dst_nextline, u32 dst_nextplane, u32 src_nextline, u32 bpp)
178{
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100179 union {
180 u8 pixels[32];
181 u32 words[8];
182 } d;
183 u32 dst_idx, first, last, w;
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100184 const u8 *c;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100185 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100187 dst += dy*dst_nextline+(dx & ~31);
188 dst_idx = dx % 32;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100189 first = 0xffffffffU >> dst_idx;
190 last = ~(0xffffffffU >> ((dst_idx+width) % 32));
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100191 while (height--) {
192 c = src;
193 p = dst;
194 w = width;
195 if (dst_idx+width <= 32) {
196 /* Single destination word */
197 first &= last;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100198 memset(d.pixels, 0, sizeof(d));
199 memcpy(d.pixels+dst_idx, c, width);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100200 c += width;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100201 c2p_32x8(d.words);
202 store_planar_masked(p, dst_nextplane, bpp, d.words,
203 first);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100204 p += 4;
205 } else {
206 /* Multiple destination words */
207 w = width;
208 /* Leading bits */
209 if (dst_idx) {
210 w = 32 - dst_idx;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100211 memset(d.pixels, 0, dst_idx);
212 memcpy(d.pixels+dst_idx, c, w);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100213 c += w;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100214 c2p_32x8(d.words);
215 store_planar_masked(p, dst_nextplane, bpp,
216 d.words, first);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100217 p += 4;
218 w = width-w;
219 }
220 /* Main chunk */
221 while (w >= 32) {
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100222 memcpy(d.pixels, c, 32);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100223 c += 32;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100224 c2p_32x8(d.words);
225 store_planar(p, dst_nextplane, bpp, d.words);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100226 p += 4;
227 w -= 32;
228 }
229 /* Trailing bits */
230 w %= 32;
231 if (w > 0) {
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100232 memcpy(d.pixels, c, w);
233 memset(d.pixels+w, 0, 32-w);
234 c2p_32x8(d.words);
235 store_planar_masked(p, dst_nextplane, bpp,
236 d.words, last);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100237 }
238 }
239 src += src_nextline;
240 dst += dst_nextline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Adrian Bunk880e5e22008-07-17 21:16:22 +0200243EXPORT_SYMBOL_GPL(c2p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Adrian Bunk8b54b612008-07-17 21:16:20 +0200245MODULE_LICENSE("GPL");