summaryrefslogtreecommitdiff
path: root/isp/controls.c
blob: 1c460395fb3b41d23851d98c9ad7d242524e3d56 (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
/*
 * OMAP3 ISP library - OMAP3 ISP controls
 *
 * Copyright (C) 2010-2011 Ideas on board SPRL
 *
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/omap3isp.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "controls.h"
#include "omap3isp.h"
#include "omap3isp-priv.h"
#include "subdev.h"

/* -----------------------------------------------------------------------------
 * Matrix algebra helper functions
 */

struct matrix {
	float coeff[3][3];
};

static void matrix_zero(struct matrix *m)
{
	unsigned int i, j;

	for (i = 0; i < 3; ++i) {
		for (j = 0; j < 3; ++j)
			m->coeff[i][j] = 0.0;
	}
}

static void matrix_invert(struct matrix *m)
{
	/* Invert the matrix using the transpose of the matrix of cofactors. The
	 * Gauss-Jordan elimination would be faster in the general case, but we
	 * know that the matrix is 3x3.
	 */
	const float eps = 1e-6;
	struct matrix out;
	unsigned int i, j;
	float det;

	out.coeff[0][0] = m->coeff[1][1] * m->coeff[2][2]
			- m->coeff[1][2] * m->coeff[2][1];
	out.coeff[0][1] = m->coeff[0][2] * m->coeff[2][1]
			- m->coeff[0][1] * m->coeff[2][2];
	out.coeff[0][2] = m->coeff[0][1] * m->coeff[1][2]
			- m->coeff[0][2] * m->coeff[1][1];
	out.coeff[1][0] = m->coeff[1][2] * m->coeff[2][0]
			- m->coeff[1][0] * m->coeff[2][2];
	out.coeff[1][1] = m->coeff[0][0] * m->coeff[2][2]
			- m->coeff[0][2] * m->coeff[2][0];
	out.coeff[1][2] = m->coeff[0][2] * m->coeff[1][0]
			- m->coeff[0][0] * m->coeff[1][2];
	out.coeff[2][0] = m->coeff[1][0] * m->coeff[2][1]
			- m->coeff[1][1] * m->coeff[2][0];
	out.coeff[2][1] = m->coeff[0][1] * m->coeff[2][0]
			- m->coeff[0][0] * m->coeff[2][1];
	out.coeff[2][2] = m->coeff[0][0] * m->coeff[1][1]
			- m->coeff[1][0] * m->coeff[0][1];

	det = m->coeff[0][0] * out.coeff[0][0] +
	      m->coeff[0][1] * out.coeff[1][0] +
	      m->coeff[0][2] * out.coeff[2][0];

	if (det < eps)
		return;

	det = 1/det;

	for (i = 0; i < 3; ++i)
		for (j = 0; j < 3; ++j)
			m->coeff[i][j] = out.coeff[i][j] * det;
}

static void matrix_multiply(struct matrix *a, const struct matrix *b)
{
	struct matrix out;

	/* Compute a * b and return the result in a. */
	out.coeff[0][0] = a->coeff[0][0] * b->coeff[0][0]
			+ a->coeff[0][1] * b->coeff[1][0]
			+ a->coeff[0][2] * b->coeff[2][0];
	out.coeff[0][1] = a->coeff[0][0] * b->coeff[0][1]
			+ a->coeff[0][1] * b->coeff[1][1]
			+ a->coeff[0][2] * b->coeff[2][1];
	out.coeff[0][2] = a->coeff[0][0] * b->coeff[0][2]
			+ a->coeff[0][1] * b->coeff[1][2]
			+ a->coeff[0][2] * b->coeff[2][2];
	out.coeff[1][0] = a->coeff[1][0] * b->coeff[0][0]
			+ a->coeff[1][1] * b->coeff[1][0]
			+ a->coeff[1][2] * b->coeff[2][0];
	out.coeff[1][1] = a->coeff[1][0] * b->coeff[0][1]
			+ a->coeff[1][1] * b->coeff[1][1]
			+ a->coeff[1][2] * b->coeff[2][1];
	out.coeff[1][2] = a->coeff[1][0] * b->coeff[0][2]
			+ a->coeff[1][1] * b->coeff[1][2]
			+ a->coeff[1][2] * b->coeff[2][2];
	out.coeff[2][0] = a->coeff[2][0] * b->coeff[0][0]
			+ a->coeff[2][1] * b->coeff[1][0]
			+ a->coeff[2][2] * b->coeff[2][0];
	out.coeff[2][1] = a->coeff[2][0] * b->coeff[0][1]
			+ a->coeff[2][1] * b->coeff[1][1]
			+ a->coeff[2][2] * b->coeff[2][1];
	out.coeff[2][2] = a->coeff[2][0] * b->coeff[0][2]
			+ a->coeff[2][1] * b->coeff[1][2]
			+ a->coeff[2][2] * b->coeff[2][2];

	*a = out;
}

static void matrix_float_to_s10q8(__u16 out[3][3], const struct matrix *in)
{
	unsigned int i, j;

	for (i = 0; i < 3; ++i) {
		for (j = 0; j < 3; ++j)
			out[i][j] = (__u16)((__s16)(in->coeff[i][j] * 256) & 0x3ff);
	}
}

static void matrix_float_to_s12q8(__u16 out[3][3], const struct matrix *in)
{
	unsigned int i, j;

	for (i = 0; i < 3; ++i) {
		for (j = 0; j < 3; ++j)
			out[i][j] = (__u16)((__s16)(in->coeff[i][j] * 256) & 0xfff);
	}
}

/* -----------------------------------------------------------------------------
 * Preview engine parameters configuration
 */

static const struct matrix omap3isp_preview_csc = {
	.coeff = {
		/* Default values. */
		{  0.2968750,  0.5937500,  0.1093750 },
		{ -0.1718750, -0.3281250,  0.5000000 },
		{  0.5000000, -0.3828125, -0.0781250 },
#if 0
		/* Default values for fluorescent light. */
		{  0.25781250,  0.50390625,  0.09765625 },
		{ -0.14843750, -0.29296875,  0.43750000 },
		{  0.43750000, -0.36718750, -0.07031250 },
#endif
	},
};

static const struct matrix omap3isp_preview_rgb2rgb = {
	.coeff = {
		/* Default values. */
		{ 1.0, 0.0, 0.0 },
		{ 0.0, 1.0, 0.0 },
		{ 0.0, 0.0, 1.0 },
#if 0
		/* Default values for fluorescent light. */
		{  1.88281250, -0.812500, -0.07031250 },
		{ -0.39453125,  1.671875, -0.27734375 },
		{ -0.12500000, -1.250000,  2.37500000 },
#endif
	},
};

int omap3_isp_preview_setup(struct omap3_isp_device *isp)
{
	struct omap3isp_prev_update_config config;
	struct omap3isp_prev_rgbtorgb rgb2rgb;
	struct omap3isp_prev_csc csc;
	int ret;

	memset(&config, 0, sizeof config);
	config.update = OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV;
	config.flag = OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV;
	config.rgb2rgb = &rgb2rgb;
	config.csc = &csc;

	memset(&rgb2rgb, 0, sizeof rgb2rgb);
	matrix_float_to_s12q8(rgb2rgb.matrix, &omap3isp_preview_rgb2rgb);

	memset(&csc, 0, sizeof csc);
	matrix_float_to_s10q8(csc.matrix, &omap3isp_preview_csc);

	v4l2_subdev_open(isp->preview);

	ret = ioctl(isp->preview->fd, VIDIOC_OMAP3ISP_PRV_CFG, &config);
	if (ret < 0)
		return -errno;

	return ret;
}

int omap3_isp_preview_set_contrast(struct omap3_isp_device *isp, unsigned int value)
{
	int contrast = value;
	int ret;

	ret = v4l2_subdev_set_control(isp->preview, V4L2_CID_CONTRAST, &contrast);
	if (ret < 0)
		return -errno;

	return ret;
}
int omap3_isp_preview_set_saturation(struct omap3_isp_device *isp, float value)
{
	struct omap3isp_prev_update_config config;
	struct omap3isp_prev_rgbtorgb rgb2rgb;
	struct matrix saturation;
	struct matrix gain;
	int ret;

	matrix_zero(&gain);
	gain.coeff[0][0] = 1.0;
	gain.coeff[1][1] = value;
	gain.coeff[2][2] = value;

	saturation = omap3isp_preview_csc;
	matrix_invert(&saturation);
	matrix_multiply(&saturation, &gain);
	matrix_multiply(&saturation, &omap3isp_preview_csc);

	memset(&config, 0, sizeof config);
	config.update = OMAP3ISP_PREV_RGB2RGB;
	config.flag = OMAP3ISP_PREV_RGB2RGB;
	config.rgb2rgb = &rgb2rgb;

	memset(&rgb2rgb, 0, sizeof rgb2rgb);
	matrix_float_to_s12q8(rgb2rgb.matrix, &saturation);

	ret = ioctl(isp->preview->fd, VIDIOC_OMAP3ISP_PRV_CFG, &config);
	if (ret < 0)
		return -errno;

	return ret;
}