summaryrefslogtreecommitdiff
path: root/drivers/media/video/mxc/opl/rotate270_u16.c
blob: 8b995212aeff2081ab33fc9ca6ad7850fdf200c4 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * 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_rotate270_u16_by16(const u8 *src, int src_line_stride,
				  int width, int height, u8 *dst,
				  int dst_line_stride, int vmirror);
static int opl_rotate270_u16_by4(const u8 *src, int src_line_stride, int width,
				 int height, u8 *dst, int dst_line_stride,
				 int vmirror);
static int opl_rotate270_vmirror_u16_both(const u8 *src, int src_line_stride,
					  int width, int height, u8 *dst,
					  int dst_line_stride, int vmirror);
int opl_rotate270_u16_qcif(const u8 *src, u8 *dst);

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

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

static int opl_rotate270_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_rotate270_u16_qcif(src, dst);
	else if (width % BLOCK_SIZE_PIXELS == 0
		 && height % BLOCK_SIZE_PIXELS == 0)
		return opl_rotate270_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_rotate270_u16_by4(src, src_line_stride, width,
					     height, dst, dst_line_stride,
					     vmirror);
	else
		return OPLERR_BAD_ARG;
}

/*
 * Rotate Counter Clockwise, divide RGB component into 16 row strips, read
 * non sequentially and write sequentially. This is done in 16 line strips
 * so that the cache is used better. Cachelines are 8 words = 32 bytes. Pixels
 * are 2 bytes. The 16 reads will be cache misses, but the next 240 should
 * be from cache. The writes to the output buffer will be sequential for 16
 * writes.
 *
 * Example:
 * Input data matrix:	    output matrix
 *
 * 0 | 1 | 2 | 3 | 4 |      4 | 0 | 0 | 3 |
 * 4 | 3 | 2 | 1 | 0 |      3 | 1 | 9 | 6 |
 * 6 | 7 | 8 | 9 | 0 |      2 | 2 | 8 | 2 |
 * 5 | 3 | 2 | 6 | 3 |      1 | 3 | 7 | 3 |
 * ^			    0 | 4 | 6 | 5 | < Write the input data sequentially
 * Read first column
 * Start at the bottom
 * Move to next column and repeat
 *
 * Loop over k decreasing (blocks)
 * in_block_ptr = src + (((RGB_HEIGHT_PIXELS / BLOCK_SIZE_PIXELS) - k)
 *                * BLOCK_SIZE_PIXELS) * (RGB_WIDTH_BYTES)
 * out_block_ptr = dst + (((RGB_HEIGHT_PIXELS / BLOCK_SIZE_PIXELS) - k)
 *                * BLOCK_SIZE_BYTES) + (RGB_WIDTH_PIXELS - 1)
 *                * RGB_HEIGHT_PIXELS * BYTES_PER_PIXEL
 *
 * Loop over i decreasing (width)
 * Each pix:
 * in_block_ptr += RGB_WIDTH_BYTES
 * out_block_ptr += 4
 *
 * Each row of block:
 * in_block_ptr -= RGB_WIDTH_BYTES * BLOCK_SIZE_PIXELS - 2
 * out_block_ptr -= RGB_HEIGHT_PIXELS * BYTES_PER_PIXEL + 2 * BLOCK_SIZE_PIXELS;
 *
 * It may perform vertical mirroring too depending on the vmirror flag.
 */
static int opl_rotate270_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 IN_INDEX = src_line_stride * BLOCK_SIZE_PIXELS
	    - BYTES_PER_PIXEL;
	const int OUT_INDEX = vmirror ?
	    -dst_line_stride + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS
	    : dst_line_stride + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS;
	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 + (((height / BLOCK_SIZE_PIXELS) - k)
				      * BLOCK_SIZE_PIXELS) * src_line_stride;
		out_block_ptr = dst + (((height / BLOCK_SIZE_PIXELS) - k)
				       * BLOCK_SIZE_PIXELS * BYTES_PER_PIXEL) +
		    (width - 1) * dst_line_stride;

		/*
		* For vertical mirroring the writing starts from the
		* first 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;
}

/*
 * Rotate Counter Clockwise, divide RGB component into 4 row strips, read
 * non sequentially and write sequentially. This is done in 4 line strips
 * so that the cache is used better. Cachelines are 8 words = 32 bytes. Pixels
 * are 2 bytes. The 4 reads will be cache misses, but the next 60 should
 * be from cache. The writes to the output buffer will be sequential for 4
 * writes.
 *
 * Example:
 * Input data matrix:	    output matrix
 *
 * 0 | 1 | 2 | 3 | 4 |      4 | 0 | 0 | 3 |
 * 4 | 3 | 2 | 1 | 0 |      3 | 1 | 9 | 6 |
 * 6 | 7 | 8 | 9 | 0 |      2 | 2 | 8 | 2 |
 * 5 | 3 | 2 | 6 | 3 |      1 | 3 | 7 | 3 |
 * ^			    0 | 4 | 6 | 5 | < Write the input data sequentially
 * Read first column
 * Start at the bottom
 * Move to next column and repeat
 *
 * Loop over k decreasing (blocks)
 * in_block_ptr = src + (((RGB_HEIGHT_PIXELS / BLOCK_SIZE_PIXELS) - k)
 *                * BLOCK_SIZE_PIXELS) * (RGB_WIDTH_BYTES)
 * out_block_ptr = dst + (((RGB_HEIGHT_PIXELS / BLOCK_SIZE_PIXELS) - k)
 *                * BLOCK_SIZE_BYTES) + (RGB_WIDTH_PIXELS - 1)
 *                * RGB_HEIGHT_PIXELS * BYTES_PER_PIXEL
 *
 * Loop over i decreasing (width)
 * Each pix:
 * in_block_ptr += RGB_WIDTH_BYTES
 * out_block_ptr += 4
 *
 * Each row of block:
 * in_block_ptr -= RGB_WIDTH_BYTES * BLOCK_SIZE_PIXELS - 2
 * out_block_ptr -= RGB_HEIGHT_PIXELS * BYTES_PER_PIXEL + 2 * BLOCK_SIZE_PIXELS;
 *
 * It may perform vertical mirroring too depending on the vmirror flag.
 */
static int opl_rotate270_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 IN_INDEX = src_line_stride * BLOCK_SIZE_PIXELS
	    - BYTES_PER_PIXEL;
	const int OUT_INDEX = vmirror ?
	    -dst_line_stride + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS
	    : dst_line_stride + BYTES_PER_PIXEL * BLOCK_SIZE_PIXELS;
	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 + (((height / BLOCK_SIZE_PIXELS) - k)
				      * BLOCK_SIZE_PIXELS) * src_line_stride;
		out_block_ptr = dst + (((height / BLOCK_SIZE_PIXELS) - k)
				       * BLOCK_SIZE_PIXELS * BYTES_PER_PIXEL)
		    + (width - 1) * dst_line_stride;

		/*
		* For vertical mirroring the writing starts from the
		* first 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_rotate270_u16);
EXPORT_SYMBOL(opl_rotate270_vmirror_u16);