summaryrefslogtreecommitdiff
path: root/drivers/media/video/mxc/opl/rotate90_u16.c
blob: f52ca87da1dcfdd51bee7b0dc6297167e4a3b416 (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
/*
 * 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 int opl_rotate90_u16_by16(const u8 *src, int src_line_stride, int width,
				 int height, u8 *dst, int dst_line_stride,
				 int vmirror);
static int opl_rotate90_u16_by4(const u8 *src, int src_line_stride, int width,
				int height, u8 *dst, int dst_line_stride,
				int vmirror);
static int opl_rotate90_vmirror_u16_both(const u8 *src, int src_line_stride,
					 int width, int height, u8 *dst,
					 int dst_line_stride, int vmirror);
int opl_rotate90_u16_qcif(const u8 *src, u8 *dst);

int opl_rotate90_u16(const u8 *src, int src_line_stride, int width, int height,
		     u8 *dst, int dst_line_stride)
{
	return opl_rotate90_vmirror_u16_both(src, src_line_stride, width,
					    height, dst, dst_line_stride, 0);
}

int opl_rotate90_vmirror_u16(const u8 *src, int src_line_stride, int width,
			    int height, u8 *dst, int dst_line_stride)
{
	return opl_rotate90_vmirror_u16_both(src, src_line_stride, width,
					    height, dst, dst_line_stride, 1);
}

static int opl_rotate90_vmirror_u16_both(const u8 *src, int src_line_stride,
					 int width, int height, u8 *dst,
					 int dst_line_stride, int vmirror)
{
	const int BLOCK_SIZE_PIXELS = CACHE_LINE_WORDS * BYTES_PER_WORD
	    / BYTES_PER_PIXEL;
	const int BLOCK_SIZE_PIXELS_BY4 = CACHE_LINE_WORDS * BYTES_PER_WORD
	    / BYTES_PER_PIXEL / 4;

	if (!src || !dst)
		return OPLERR_NULL_PTR;

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

	/* The QCIF algorithm doesn't support vertical mirroring */
	if (vmirror == 0 && width == QCIF_Y_WIDTH && height == QCIF_Y_HEIGHT
	    && src_line_stride == QCIF_Y_WIDTH * 2
	    && src_line_stride == QCIF_Y_HEIGHT * 2)
		return opl_rotate90_u16_qcif(src, dst);
	else if (width % BLOCK_SIZE_PIXELS == 0
		 && height % BLOCK_SIZE_PIXELS == 0)
		return opl_rotate90_u16_by16(src, src_line_stride, width,
					     height, dst, dst_line_stride,
					     vmirror);
	else if (width % BLOCK_SIZE_PIXELS_BY4 == 0
		 && height % BLOCK_SIZE_PIXELS_BY4 == 0)
		return opl_rotate90_u16_by4(src, src_line_stride, width, height,
					    dst, dst_line_stride, vmirror);
	else
		return OPLERR_BAD_ARG;
}

/*
 * Performs clockwise rotation (and possibly vertical mirroring depending
 * on the vmirror flag) using block sizes of 16x16
 * The algorithm is similar to 270 degree clockwise rotation algorithm
 */
static int opl_rotate90_u16_by16(const u8 *src, int src_line_stride, int width,
				 int height, u8 *dst, int dst_line_stride,
				 int vmirror)
{
	const int BLOCK_SIZE_PIXELS = CACHE_LINE_WORDS * BYTES_PER_WORD
	    / BYTES_PER_PIXEL;
	const int BLOCK_SIZE_BYTES = BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS;
	const int IN_INDEX = src_line_stride * BLOCK_SIZE_PIXELS
	    + BYTES_PER_PIXEL;
	const int OUT_INDEX = vmirror ?
	    -dst_line_stride - BLOCK_SIZE_BYTES
	    : dst_line_stride - BLOCK_SIZE_BYTES;
	const u8 *in_block_ptr;
	u8 *out_block_ptr;
	int i, k;

	for (k = height / BLOCK_SIZE_PIXELS; k > 0; k--) {
		in_block_ptr = src + src_line_stride * (height - 1)
		    - (src_line_stride * BLOCK_SIZE_PIXELS *
		       (height / BLOCK_SIZE_PIXELS - k));
		out_block_ptr = dst + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS *
		    ((height / BLOCK_SIZE_PIXELS) - k);

		/*
		 * For vertical mirroring the writing starts from the
		 * bottom line
		 */
		if (vmirror)
			out_block_ptr += dst_line_stride * (width - 1);

		for (i = width; i > 0; i--) {
			__asm__ volatile (
				"ldrh	r2, [%0], -%4\n\t"
				"ldrh	r3, [%0], -%4\n\t"
				"ldrh	r4, [%0], -%4\n\t"
				"ldrh	r5, [%0], -%4\n\t"
				"orr	r2, r2, r3, lsl #16\n\t"
				"orr	r4, r4, r5, lsl #16\n\t"
				"str	r2, [%1], #4\n\t"
				"str	r4, [%1], #4\n\t"

				"ldrh	r2, [%0], -%4\n\t"
				"ldrh	r3, [%0], -%4\n\t"
				"ldrh	r4, [%0], -%4\n\t"
				"ldrh	r5, [%0], -%4\n\t"
				"orr	r2, r2, r3, lsl #16\n\t"
				"orr	r4, r4, r5, lsl #16\n\t"
				"str	r2, [%1], #4\n\t"
				"str	r4, [%1], #4\n\t"

				"ldrh	r2, [%0], -%4\n\t"
				"ldrh	r3, [%0], -%4\n\t"
				"ldrh	r4, [%0], -%4\n\t"
				"ldrh	r5, [%0], -%4\n\t"
				"orr	r2, r2, r3, lsl #16\n\t"
				"orr	r4, r4, r5, lsl #16\n\t"
				"str	r2, [%1], #4\n\t"
				"str	r4, [%1], #4\n\t"

				"ldrh	r2, [%0], -%4\n\t"
				"ldrh	r3, [%0], -%4\n\t"
				"ldrh	r4, [%0], -%4\n\t"
				"ldrh	r5, [%0], -%4\n\t"
				"orr	r2, r2, r3, lsl #16\n\t"
				"orr	r4, r4, r5, lsl #16\n\t"
				"str	r2, [%1], #4\n\t"
				"str	r4, [%1], #4\n\t"

				: "+r" (in_block_ptr), "+r"(out_block_ptr)	/* output */
				: "0"(in_block_ptr), "1"(out_block_ptr), "r"(src_line_stride)	/* input  */
				: "r2", "r3", "r4", "r5", "memory"	/* modify */
			);
			in_block_ptr += IN_INDEX;
			out_block_ptr += OUT_INDEX;
		}
	}

	return OPLERR_SUCCESS;
}

/*
 * Performs clockwise rotation (and possibly vertical mirroring depending
 * on the vmirror flag) using block sizes of 4x4
 * The algorithm is similar to 270 degree clockwise rotation algorithm
 */
static int opl_rotate90_u16_by4(const u8 *src, int src_line_stride, int width,
				int height, u8 *dst, int dst_line_stride,
				int vmirror)
{
	const int BLOCK_SIZE_PIXELS = CACHE_LINE_WORDS * BYTES_PER_WORD
	    / BYTES_PER_PIXEL / 4;
	const int BLOCK_SIZE_BYTES = BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS;
	const int IN_INDEX = src_line_stride * BLOCK_SIZE_PIXELS
	    + BYTES_PER_PIXEL;
	const int OUT_INDEX = vmirror ?
	    -dst_line_stride - BLOCK_SIZE_BYTES
	    : dst_line_stride - BLOCK_SIZE_BYTES;
	const u8 *in_block_ptr;
	u8 *out_block_ptr;
	int i, k;

	for (k = height / BLOCK_SIZE_PIXELS; k > 0; k--) {
		in_block_ptr = src + src_line_stride * (height - 1)
		    - (src_line_stride * BLOCK_SIZE_PIXELS *
		       (height / BLOCK_SIZE_PIXELS - k));
		out_block_ptr = dst + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS
		    * ((height / BLOCK_SIZE_PIXELS) - k);

		/*
		 * For horizontal mirroring the writing starts from the
		 * bottom line
		 */
		if (vmirror)
			out_block_ptr += dst_line_stride * (width - 1);

		for (i = width; i > 0; i--) {
			__asm__ volatile (
				"ldrh	r2, [%0], -%4\n\t"
				"ldrh	r3, [%0], -%4\n\t"
				"ldrh	r4, [%0], -%4\n\t"
				"ldrh	r5, [%0], -%4\n\t"
				"orr	r2, r2, r3, lsl #16\n\t"
				"orr	r4, r4, r5, lsl #16\n\t"
				"str	r2, [%1], #4\n\t"
				"str	r4, [%1], #4\n\t"

				: "+r" (in_block_ptr), "+r"(out_block_ptr)	/* output */
				: "0"(in_block_ptr), "1"(out_block_ptr), "r"(src_line_stride)	/* input  */
				: "r2", "r3", "r4", "r5", "memory"	/* modify */
			);
			in_block_ptr += IN_INDEX;
			out_block_ptr += OUT_INDEX;
		}
	}

	return OPLERR_SUCCESS;
}

EXPORT_SYMBOL(opl_rotate90_u16);
EXPORT_SYMBOL(opl_rotate90_vmirror_u16);