summaryrefslogtreecommitdiff
path: root/drivers/media/video/mxc/opl/hmirror_rotate180_u16.c
blob: 6737f3a54d469f7dd2b81c3e1c6f92755f06c052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include "opl.h"

static inline u32 rot_left_u16(u16 x, unsigned int n)
{
	return (x << n) | (x >> (16 - n));
}

static inline u32 rot_left_u32(u32 x, unsigned int n)
{
	return (x << n) | (x >> (32 - n));
}

static inline u32 byte_swap_u32(u32 x)
{
	u32 t1, t2, t3;

	t1 = x ^ ((x << 16) | x >> 16);
	t2 = t1 & 0xff00ffff;
	t3 = (x >> 8) | (x << 24);
	return t3 ^ (t2 >> 8);
}

static int opl_hmirror_u16_by1(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror);
static int opl_hmirror_u16_by2(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror);
static int opl_hmirror_u16_by4(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror);
static int opl_hmirror_u16_by8(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror);

int opl_hmirror_u16(const u8 *src, int src_line_stride, int width, int height,
		    u8 *dst, int dst_line_stride)
{
	if (!src || !dst)
		return OPLERR_NULL_PTR;

	if (width == 0 || height == 0 || src_line_stride == 0
	    || dst_line_stride == 0)
		return OPLERR_BAD_ARG;

	if (width % 8 == 0)
		return opl_hmirror_u16_by8(src, src_line_stride, width, height,
					   dst, dst_line_stride, 0);
	else if (width % 4 == 0)
		return opl_hmirror_u16_by4(src, src_line_stride, width, height,
					   dst, dst_line_stride, 0);
	else if (width % 2 == 0)
		return opl_hmirror_u16_by2(src, src_line_stride, width, height,
					   dst, dst_line_stride, 0);
	else			/* (width % 1) */
		return opl_hmirror_u16_by1(src, src_line_stride, width, height,
					   dst, dst_line_stride, 0);
}

int opl_rotate180_u16(const u8 *src, int src_line_stride, int width,
		      int height, u8 *dst, int dst_line_stride)
{
	if (!src || !dst)
		return OPLERR_NULL_PTR;

	if (width == 0 || height == 0 || src_line_stride == 0
	    || dst_line_stride == 0)
		return OPLERR_BAD_ARG;

	if (width % 8 == 0)
		return opl_hmirror_u16_by8(src, src_line_stride, width, height,
					   dst, dst_line_stride, 1);
	else if (width % 4 == 0)
		return opl_hmirror_u16_by4(src, src_line_stride, width, height,
					   dst, dst_line_stride, 1);
	else if (width % 2 == 0)
		return opl_hmirror_u16_by2(src, src_line_stride, width, height,
					   dst, dst_line_stride, 1);
	else			/* (width % 1) */
		return opl_hmirror_u16_by1(src, src_line_stride, width, height,
					   dst, dst_line_stride, 1);
}

static int opl_hmirror_u16_by1(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror)
{
	const u8 *src_row_addr;
	const u8 *psrc;
	u8 *dst_row_addr, *pdst;
	int i, j;
	u16 pixel;

	src_row_addr = src;
	if (vmirror) {
		dst_row_addr = dst + dst_line_stride * (height - 1);
		dst_line_stride = -dst_line_stride;
	} else
		dst_row_addr = dst;

	/* Loop over all rows */
	for (i = 0; i < height; i++) {
		/* Loop over each pixel */
		psrc = src_row_addr;
		pdst = dst_row_addr + (width - 1) * BYTES_PER_PIXEL
		    - (BYTES_PER_PIXEL - BYTES_PER_PIXEL);
		for (j = 0; j < width; j++) {
			pixel = *(u16 *) psrc;
			*(u16 *) pdst = pixel;
			psrc += BYTES_PER_PIXEL;
			pdst -= BYTES_PER_PIXEL;
		}
		src_row_addr += src_line_stride;
		dst_row_addr += dst_line_stride;
	}

	return OPLERR_SUCCESS;
}

static int opl_hmirror_u16_by2(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror)
{
	const u8 *src_row_addr;
	const u8 *psrc;
	u8 *dst_row_addr, *pdst;
	int i, j;
	u32 pixelsin, pixelsout;

	src_row_addr = src;
	if (vmirror) {
		dst_row_addr = dst + dst_line_stride * (height - 1);
		dst_line_stride = -dst_line_stride;
	} else
		dst_row_addr = dst;

	/* Loop over all rows */
	for (i = 0; i < height; i++) {
		/* Loop over each pixel */
		psrc = src_row_addr;
		pdst = dst_row_addr + (width - 2) * BYTES_PER_PIXEL;
		for (j = 0; j < (width >> 1); j++) {
			pixelsin = *(u32 *) psrc;
			pixelsout = rot_left_u32(pixelsin, 16);
			*(u32 *) pdst = pixelsout;
			psrc += BYTES_PER_2PIXEL;
			pdst -= BYTES_PER_2PIXEL;
		}
		src_row_addr += src_line_stride;
		dst_row_addr += dst_line_stride;
	}

	return OPLERR_SUCCESS;
}

static int opl_hmirror_u16_by4(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror)
{
	const u8 *src_row_addr;
	const u8 *psrc;
	u8 *dst_row_addr, *pdst;
	int i, j;

	union doubleword {
		u64 dw;
		u32 w[2];
	};

	union doubleword inbuf;
	union doubleword outbuf;

	src_row_addr = src;
	if (vmirror) {
		dst_row_addr = dst + dst_line_stride * (height - 1);
		dst_line_stride = -dst_line_stride;
	} else
		dst_row_addr = dst;

	/* Loop over all rows */
	for (i = 0; i < height; i++) {
		/* Loop over each pixel */
		psrc = src_row_addr;
		pdst = dst_row_addr + (width - 4) * BYTES_PER_PIXEL;
		for (j = 0; j < (width >> 2); j++) {
			inbuf.dw = *(u64 *) psrc;
			outbuf.w[0] = rot_left_u32(inbuf.w[1], 16);
			outbuf.w[1] = rot_left_u32(inbuf.w[0], 16);
			*(u64 *) pdst = outbuf.dw;
			psrc += BYTES_PER_4PIXEL;
			pdst -= BYTES_PER_4PIXEL;
		}
		src_row_addr += src_line_stride;
		dst_row_addr += dst_line_stride;
	}
	return OPLERR_SUCCESS;
}

static int opl_hmirror_u16_by8(const u8 *src, int src_line_stride, int width,
			       int height, u8 *dst, int dst_line_stride,
			       int vmirror)
{
	const u8 *src_row_addr;
	const u8 *psrc;
	u8 *dst_row_addr, *pdst;
	int i, j;

	src_row_addr = src;
	if (vmirror) {
		dst_row_addr = dst + dst_line_stride * (height - 1);
		dst_line_stride = -dst_line_stride;
	} else
		dst_row_addr = dst;

	/* Loop over all rows */
	for (i = 0; i < height; i++) {
		/* Loop over each pixel */
		psrc = src_row_addr;
		pdst = dst_row_addr + (width - 1) * BYTES_PER_PIXEL - 2;
		for (j = (width >> 3); j > 0; j--) {
			__asm__ volatile (
				"ldmia	%0!,{r2-r5}\n\t"
				"mov	r6, r2\n\t"
				"mov	r7, r3\n\t"
				"mov	r2, r5, ROR #16\n\t"
				"mov	r3, r4, ROR #16\n\t"
				"mov	r4, r7, ROR #16\n\t"
				"mov	r5, r6, ROR #16\n\t"
				"stmda	%1!,{r2-r5}\n\t"

				: "+r"(psrc), "+r"(pdst)
				: "0"(psrc), "1"(pdst)
				: "r2", "r3", "r4", "r5", "r6", "r7",
				"memory"
			);
		}
		src_row_addr += src_line_stride;
		dst_row_addr += dst_line_stride;
	}

	return OPLERR_SUCCESS;
}

EXPORT_SYMBOL(opl_hmirror_u16);
EXPORT_SYMBOL(opl_rotate180_u16);