summaryrefslogtreecommitdiff
path: root/isp/stats.c
blob: 9a2d5f343c97bd7bb8d6fd9adce744b4e8f64341 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * OMAP3 ISP library - OMAP3 ISP statistics
 *
 * Copyright (C) 2010-2012 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 <linux/videodev2.h>

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

#include "omap3isp.h"
#include "omap3isp-priv.h"
#include "stats.h"
#include "subdev.h"
#include "tools.h"

/* -----------------------------------------------------------------------------
 * Automatic Exposure and White Balance
 */

static void omap3_isp_aewb_process(struct omap3_isp_device *isp, void *buffer,
				   size_t size __attribute__((__unused__)))
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct omap3_isp_aewb_stats stats;
	uint16_t *data = buffer;
	unsigned int windows;
	unsigned int i, j;

	memset(&stats, 0, sizeof stats);

	/* Number of windows, exclusing black row. */
	windows = aewb->win_n_x * aewb->win_n_y;

	for (i = 0; i < windows; ++i) {
		for (j = 0; j < 8; ++j)
			stats.accum[j] += *data++;

		if (i % 8 == 7) {
			for (j = 0; j < 8; ++j)
				stats.unsat += *data++;
		}
	}

	if (windows % 8) {
		/* Skip black row windows up to the next boundary of 8
		 * windows.
		 */
		data += min(8 - windows % 8, aewb->win_n_x) * 8;
		for (j = 0; j < windows % 8; ++j)
			stats.unsat += *data++;
	}

	stats.npix = div_round_up(aewb->win_w, aewb->win_inc_x) * aewb->win_n_x
		   * div_round_up(aewb->win_h, aewb->win_inc_y) * aewb->win_n_y;

	isp->ops->aewb_ready(isp, &stats);
}

static void omap3_isp_aewb_event(void *priv)
{
	struct omap3_isp_device *isp = priv;
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct omap3isp_stat_data data;
	struct v4l2_event event;
	struct omap3isp_stat_event_status *status =
		(struct omap3isp_stat_event_status *)event.u.data;
	int ret;

	memset(&event, 0, sizeof event);
	ret = ioctl(aewb->entity->fd, VIDIOC_DQEVENT, &event);
	if (ret < 0) {
		printf("unable to retrieve AEWB event: %s (%d).\n",
			strerror(errno), errno);
		return;
	}

	if (status->buf_err) {
		printf("AEWB: stats error, skipping buffer.\n");
		return;
	}

	memset(&data, 0, sizeof data);
	data.buf = aewb->buffer;
	data.buf_size = aewb->size;

	ret = ioctl(aewb->entity->fd, VIDIOC_OMAP3ISP_STAT_REQ, &data);
	if (ret < 0) {
		printf("unable to retrieve AEWB data: %s (%d).\n",
			strerror(errno), errno);
		return;
	}

	omap3_isp_aewb_process(isp, data.buf, data.buf_size);
}

static const struct format_info {
	__u32 code;
	unsigned int bpp;
} formats[] = {
	{ V4L2_MBUS_FMT_SBGGR8_1X8, 8, },
	{ V4L2_MBUS_FMT_SGBRG8_1X8, 8, },
	{ V4L2_MBUS_FMT_SGRBG8_1X8, 8, },
	{ V4L2_MBUS_FMT_SRGGB8_1X8, 8, },
	{ V4L2_MBUS_FMT_SBGGR10_1X10, 10, },
	{ V4L2_MBUS_FMT_SGBRG10_1X10, 10, },
	{ V4L2_MBUS_FMT_SGRBG10_1X10, 10, },
	{ V4L2_MBUS_FMT_SRGGB10_1X10, 10, },
};

static int omap3_isp_aewb_setup(struct omap3_isp_device *isp)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct omap3isp_h3a_aewb_config config;
	unsigned int buf_size;
	int ret;

	memset(&config, 0, sizeof config);
	config.saturation_limit = aewb->saturation;
	config.win_width = aewb->win_w;
	config.win_height = aewb->win_h;
	config.hor_win_count = aewb->win_n_x;
	config.ver_win_count = aewb->win_n_y;
	config.hor_win_start = aewb->win_x;
	config.ver_win_start = aewb->win_y;
	config.blk_win_height = 2;
	config.blk_ver_win_start = aewb->win_y + aewb->win_h * (aewb->win_n_y - 1) + 2;
	config.subsample_hor_inc = aewb->win_inc_x;
	config.subsample_ver_inc = aewb->win_inc_y;
	config.alaw_enable = 0;

	buf_size = (aewb->win_n_x * aewb->win_n_y +
		    (aewb->win_n_x * aewb->win_n_y + 7) / 8 +
		    aewb->win_n_x + (aewb->win_n_x + 7) / 8)
		 * 16;
	config.buf_size = buf_size;

	ret = ioctl(aewb->entity->fd, VIDIOC_OMAP3ISP_AEWB_CFG, &config);
	if (ret < 0)
		return -errno;

	if (config.buf_size != buf_size)
		printf("AEWB: buf size was %u, is %u\n", buf_size,
			config.buf_size);

	aewb->size = config.buf_size;
	aewb->buffer = malloc(config.buf_size);
	if (aewb->buffer == NULL)
		return -ENOMEM;

	return 0;
}

int omap3_isp_aewb_configure(struct omap3_isp_device *isp, struct v4l2_rect *rect,
			     unsigned int saturation)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct v4l2_mbus_framefmt format;
	struct media_entity_pad *source;
	unsigned int win_n_x = 10;
	unsigned int win_n_y = 7;
	unsigned int win_inc_x;
	unsigned int win_inc_y;
	unsigned int win_h;
	unsigned int win_w;
	unsigned int win_x;
	unsigned int win_y;
	unsigned int bpp = 0;
	unsigned int i;
	int ret;

	source = media_entity_remote_source(&aewb->entity->pads[0]);
	if (source == NULL)
		return -ENOENT;

	ret = v4l2_subdev_get_format(source->entity, &format, source->index,
				     V4L2_SUBDEV_FORMAT_TRY);
	if (ret < 0)
		return ret;

	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
		if (formats[i].code == format.code) {
			bpp = formats[i].bpp;
			break;
		}
	}

	if (bpp == 0)
		return -EINVAL;

	/* Validate the requested rectangle. */
	if (rect->left < 0 || rect->left + rect->width > format.width ||
	    rect->top < 0 || rect->top + rect->height > format.height)
		return -ERANGE;

	/* Window width and height are computed by dividing the frame width and
	 * height by the number of windows horizontally and vertically. Make
	 * sure they fall in the admissible ranges by modifying the number of
	 * windows is necessary. Finally, clamp the number of windows to the
	 * admissible range.
	 */
	win_w = (rect->width / win_n_x) & ~1;
	win_w = clamp_t(unsigned int, win_w, OMAP3ISP_AEWB_MIN_WIN_W,
			OMAP3ISP_AEWB_MAX_WIN_W);
	win_n_x = rect->width / win_w;
	win_n_x = clamp_t(unsigned int, win_n_x, OMAP3ISP_AEWB_MIN_WINHC,
			  OMAP3ISP_AEWB_MAX_WINHC);

	win_h = (rect->height / win_n_y) & ~1;
	win_h = clamp_t(unsigned int, win_h, OMAP3ISP_AEWB_MIN_WIN_H,
			OMAP3ISP_AEWB_MAX_WIN_H);
	win_n_y = rect->height / win_h;
	win_n_y = clamp_t(unsigned int, win_n_y, OMAP3ISP_AEWB_MIN_WINVC,
			  OMAP3ISP_AEWB_MAX_WINVC);

	/* Accumulators are 16-bit registers. To avoid overflows limit the
	 * number of pixels to 64 (for 10-bit formats) or 256 (for 8-bit
	 * formats) by increasing the horizontal and vertical increments.
	 */
	win_inc_x = 2;
	win_inc_y = 2;

	while ((win_w / win_inc_x) * (win_h / win_inc_y) > 1U << (16 - bpp)) {
		if (win_inc_x <= win_inc_y)
			win_inc_x += 2;
		else
			win_inc_y += 2;
	}

	/* Center the windows in the image area. The black row will be
	 * positionned at the end of the frame. Make sure the position is a
	 * multiple of 2 pixels to keep the Bayer pattern.
	 */
	win_x = ((format.width - win_w * win_n_x) / 2) & ~1;
	win_y = ((format.height - win_h * win_n_y) / 2) & ~1;

	printf("AEWB: #win %ux%u start %ux%u size %ux%u inc %ux%u\n",
		win_n_x, win_n_y, win_x, win_y, win_w, win_h,
		win_inc_x, win_inc_y);

	aewb->win_x = win_x;
	aewb->win_y = win_y;
	aewb->win_n_x = win_n_x;
	aewb->win_n_y = win_n_y;
	aewb->win_w = win_w;
	aewb->win_h = win_h;
	aewb->win_inc_x = win_inc_x;
	aewb->win_inc_y = win_inc_y;

	aewb->saturation = saturation;

	rect->width = win_n_x * win_w;
	rect->height = win_n_y * win_h;

	return 0;
}

/* -----------------------------------------------------------------------------
 * Start/stop, init/cleanup
 */

int omap3_isp_stats_get_format(struct omap3_isp_device *isp,
			       struct v4l2_mbus_framefmt *format)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct media_entity_pad *source;

	source = media_entity_remote_source(&aewb->entity->pads[0]);
	if (source == NULL)
		return -ENOENT;

	return v4l2_subdev_get_format(source->entity, format, source->index,
				      V4L2_SUBDEV_FORMAT_TRY);
}

void omap3_isp_stats_enable(struct omap3_isp_device *isp, bool enable)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;

	aewb->enabled = enable;
}

int omap3_isp_stats_start(struct omap3_isp_device *isp)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct v4l2_event_subscription esub;
	unsigned long enable = 1;
	int ret;

	if (!aewb->enabled)
		return 0;

	ret = omap3_isp_aewb_setup(isp);
	if (ret < 0) {
		printf("unable to configure AEWB engine: %s (%d).\n",
			strerror(errno), errno);
		return ret;
	}

	memset(&esub, 0, sizeof esub);
	esub.type = V4L2_EVENT_OMAP3ISP_AEWB;
	ret = ioctl(aewb->entity->fd, VIDIOC_SUBSCRIBE_EVENT, &esub);
	if (ret < 0) {
		printf("unable to subscribe to AEWB event: %s (%d).\n",
			strerror(errno), errno);
		return ret;
	}

	ret = ioctl(aewb->entity->fd, VIDIOC_OMAP3ISP_STAT_EN, &enable);
	if (ret < 0) {
		printf("unable to start AEWB engine: %s (%d).\n",
			strerror(errno), errno);
		return ret;
	}

	isp->ops->watch_fd(aewb->entity->fd, OMAP3_ISP_EVENT_EXCEPTION,
			   omap3_isp_aewb_event, isp);

	return 0;
}

void omap3_isp_stats_stop(struct omap3_isp_device *isp)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;
	struct v4l2_event_subscription esub;
	unsigned long enable = 0;

	if (!aewb->enabled)
		return;

	isp->ops->unwatch_fd(aewb->entity->fd);
	ioctl(aewb->entity->fd, VIDIOC_OMAP3ISP_STAT_EN, &enable);

	memset(&esub, 0, sizeof esub);
	esub.type = V4L2_EVENT_OMAP3ISP_AEWB;
	ioctl(aewb->entity->fd, VIDIOC_UNSUBSCRIBE_EVENT, &esub);
}

int omap3_isp_stats_init(struct omap3_isp_device *isp)
{
	struct omap3_isp_aewb *aewb = &isp->aewb;

	aewb->entity = media_get_entity_by_name(isp->mdev, "OMAP3 ISP AEWB");
	if (aewb->entity == NULL)
		return -ENOENT;

	return v4l2_subdev_open(aewb->entity);
}

void omap3_isp_stats_cleanup(struct omap3_isp_device *isp)
{
	v4l2_subdev_close(isp->aewb.entity);
}