blob: ec7ac8526f060b8c4cc6cad632b5dd673b2fd3e8 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Adrian Bunk8b54b612008-07-17 21:16:20 +020011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010013
14#include <asm/unaligned.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "c2p.h"
Geert Uytterhoeven2cd1de02009-01-04 11:58:20 +010017#include "c2p_core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19
20 /*
21 * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
22 * containing
23 * - 32 8-bit chunky pixels on input
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010024 * - permutated planar data (1 plane per 32-bit word) on output
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010027static void c2p_32x8(u32 d[8])
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010029 transp8(d, 16, 4);
30 transp8(d, 8, 2);
31 transp8(d, 4, 1);
32 transp8(d, 2, 4);
33 transp8(d, 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36
37 /*
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010038 * Array containing the permutation indices of the planar data after c2p
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010041static const int perm_c2p_32x8[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
44 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Store a full block of planar data after c2p conversion
46 */
47
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010048static inline void store_planar(void *dst, u32 dst_inc, u32 bpp, u32 d[8])
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010050 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010052 for (i = 0; i < bpp; i++, dst += dst_inc)
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010053 put_unaligned_be32(d[perm_c2p_32x8[i]], dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56
57 /*
58 * Store a partial block of planar data after c2p conversion
59 */
60
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010061static inline void store_planar_masked(void *dst, u32 dst_inc, u32 bpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 u32 d[8], u32 mask)
63{
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010064 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010066 for (i = 0; i < bpp; i++, dst += dst_inc)
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010067 put_unaligned_be32(comp(d[perm_c2p_32x8[i]],
68 get_unaligned_be32(dst), mask),
69 dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72
73 /*
Geert Uytterhoeven2eab7ff2008-12-21 15:48:13 +010074 * c2p_planar - Copy 8-bit chunky image data to a planar frame buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * @dst: Starting address of the planar frame buffer
76 * @dx: Horizontal destination offset (in pixels)
77 * @dy: Vertical destination offset (in pixels)
78 * @width: Image width (in pixels)
79 * @height: Image height (in pixels)
80 * @dst_nextline: Frame buffer offset to the next line (in bytes)
81 * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
82 * @src_nextline: Image offset to the next line (in bytes)
83 * @bpp: Bits per pixel of the planar frame buffer (1-8)
84 */
85
Geert Uytterhoeven2eab7ff2008-12-21 15:48:13 +010086void c2p_planar(void *dst, const void *src, u32 dx, u32 dy, u32 width,
87 u32 height, u32 dst_nextline, u32 dst_nextplane,
88 u32 src_nextline, u32 bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010090 union {
91 u8 pixels[32];
92 u32 words[8];
93 } d;
94 u32 dst_idx, first, last, w;
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010095 const u8 *c;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +010096 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +010098 dst += dy*dst_nextline+(dx & ~31);
99 dst_idx = dx % 32;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100100 first = 0xffffffffU >> dst_idx;
101 last = ~(0xffffffffU >> ((dst_idx+width) % 32));
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100102 while (height--) {
103 c = src;
104 p = dst;
105 w = width;
106 if (dst_idx+width <= 32) {
107 /* Single destination word */
108 first &= last;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100109 memset(d.pixels, 0, sizeof(d));
110 memcpy(d.pixels+dst_idx, c, width);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100111 c += width;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100112 c2p_32x8(d.words);
113 store_planar_masked(p, dst_nextplane, bpp, d.words,
114 first);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100115 p += 4;
116 } else {
117 /* Multiple destination words */
118 w = width;
119 /* Leading bits */
120 if (dst_idx) {
121 w = 32 - dst_idx;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100122 memset(d.pixels, 0, dst_idx);
123 memcpy(d.pixels+dst_idx, c, w);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100124 c += w;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100125 c2p_32x8(d.words);
126 store_planar_masked(p, dst_nextplane, bpp,
127 d.words, first);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100128 p += 4;
129 w = width-w;
130 }
131 /* Main chunk */
132 while (w >= 32) {
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100133 memcpy(d.pixels, c, 32);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100134 c += 32;
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100135 c2p_32x8(d.words);
136 store_planar(p, dst_nextplane, bpp, d.words);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100137 p += 4;
138 w -= 32;
139 }
140 /* Trailing bits */
141 w %= 32;
142 if (w > 0) {
Geert Uytterhoeven1f034452009-01-04 11:43:00 +0100143 memcpy(d.pixels, c, w);
144 memset(d.pixels+w, 0, 32-w);
145 c2p_32x8(d.words);
146 store_planar_masked(p, dst_nextplane, bpp,
147 d.words, last);
Geert Uytterhoeven8280eb8a2009-01-04 11:42:16 +0100148 }
149 }
150 src += src_nextline;
151 dst += dst_nextline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Geert Uytterhoeven2eab7ff2008-12-21 15:48:13 +0100154EXPORT_SYMBOL_GPL(c2p_planar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Adrian Bunk8b54b612008-07-17 21:16:20 +0200156MODULE_LICENSE("GPL");