summaryrefslogtreecommitdiffstats
path: root/camera/smdk4x12_v4l2_output.c
blob: 0f8cad242c87814061420fcdbf962802df322569 (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
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>

#define LOG_TAG "smdk4x12_v4l2_output"
#include <utils/Log.h>

#include "smdk4x12_camera.h"

int smdk4x12_v4l2_output_start(struct smdk4x12_camera *smdk4x12_camera,
	struct smdk4x12_v4l2_output *output)
{
	int width, height, format;
	int buffer_width, buffer_height, buffer_format;
	camera_memory_t *memory = NULL;
	int memory_address, memory_size;
#ifdef EXYNOS_ION
	int memory_ion_fd = -1;
#endif
	int buffers_count, buffer_length;
	int v4l2_id;
	int value;
	int fd;
	int rc;
	int i;

	if (smdk4x12_camera == NULL || output == NULL)
		return -EINVAL;

//	ALOGD("%s()", __func__);

	if (output->enabled) {
		ALOGE("Output was already started");
		return -1;
	}

	width = output->width;
	height = output->height;
	format = output->format;

	buffer_width = output->buffer_width;
	buffer_height = output->buffer_height;
	buffer_format = output->buffer_format;

	v4l2_id = output->v4l2_id;

	buffers_count = output->buffers_count;
	if (buffers_count <= 0) {
		ALOGE("%s: Invalid buffers count: %d", __func__, buffers_count);
		goto error;
	}

	buffer_length = smdk4x12_camera_buffer_length(width, height, format);

	rc = smdk4x12_v4l2_open(smdk4x12_camera, v4l2_id);
	if (rc < 0) {
		ALOGE("%s: Unable to open v4l2 device", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_querycap_out(smdk4x12_camera, v4l2_id);
	if (rc < 0) {
		ALOGE("%s: Unable to query capabilities", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_g_fmt_out(smdk4x12_camera, v4l2_id, NULL, NULL, NULL);
	if (rc < 0) {
		ALOGE("%s: Unable to get format", __func__);
		goto error;
	}

	value = 0;
	rc = smdk4x12_v4l2_g_ctrl(smdk4x12_camera, v4l2_id, V4L2_CID_RESERVED_MEM_BASE_ADDR, &value);
	if (rc < 0) {
		ALOGE("%s: Unable to get address", __func__);
		goto error;
	}

	memory_address = value;

	value = 0;
	rc = smdk4x12_v4l2_g_ctrl(smdk4x12_camera, v4l2_id, V4L2_CID_RESERVED_MEM_SIZE, &value);
	if (rc < 0) {
		ALOGE("%s: Unable to get size", __func__);
		goto error;
	}

	memory_size = value * 1024;

	rc = smdk4x12_v4l2_s_ctrl(smdk4x12_camera, v4l2_id, V4L2_CID_OVLY_MODE, FIMC_OVLY_NONE_MULTI_BUF);
	if (rc < 0) {
		ALOGE("%s: Unable to set overlay mode", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_s_fmt_pix_out(smdk4x12_camera, v4l2_id, buffer_width, buffer_height, buffer_format, 0);
	if (rc < 0) {
		ALOGE("%s: Unable to set output pixel format!", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_s_crop_out(smdk4x12_camera, v4l2_id, 0, 0, buffer_width, buffer_height);
	if (rc < 0) {
		ALOGE("%s: Unable to crop", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_reqbufs_out(smdk4x12_camera, v4l2_id, 1);
	if (rc < 0) {
		ALOGE("%s: Unable to request buffers", __func__);
		goto error;
	}

	if (memory_address != 0 && memory_address != (int) 0xffffffff && memory_size >= buffer_length) {
		for (i = buffers_count; i > 0; i--) {
			if (buffer_length * i < memory_size)
				break;
		}

		// This should never happen
		if (i == 0)
			goto error;

		buffers_count = i;
		ALOGD("Found %d buffers available for output!", buffers_count);

		if (SMDK4x12_CAMERA_CALLBACK_DEFINED(request_memory)) {
			fd = smdk4x12_v4l2_fd(smdk4x12_camera, v4l2_id);
			if (fd < 0) {
				ALOGE("%s: Unable to get v4l2 fd for id %d", __func__, v4l2_id);
				goto error;
			}

			memory = smdk4x12_camera->callbacks.request_memory(fd, buffer_length, buffers_count, smdk4x12_camera->callbacks.user);
			if (memory == NULL || memory->data == NULL || memory->data == MAP_FAILED) {
				ALOGE("%s: Unable to request memory", __func__);
				goto error;
			}
		} else {
			ALOGE("%s: No memory request function!", __func__);
			goto error;
		}
	} else {
#ifdef EXYNOS_ION
		memory_ion_fd = smdk4x12_ion_alloc(smdk4x12_camera, buffers_count * buffer_length);
		if (memory_ion_fd < 0) {
			ALOGE("%s: Unable to alloc ION memory", __func__);
			goto error;
		}

		if (SMDK4x12_CAMERA_CALLBACK_DEFINED(request_memory)) {
			memory = smdk4x12_camera->callbacks.request_memory(memory_ion_fd, buffer_length, buffers_count, smdk4x12_camera->callbacks.user);
			if (memory == NULL || memory->data == NULL || memory->data == MAP_FAILED) {
				ALOGE("%s: Unable to request memory", __func__);
				goto error;
			}
		} else {
			ALOGE("%s: No memory request function!", __func__);
			goto error;
		}

		memory_address = smdk4x12_ion_phys(smdk4x12_camera, memory_ion_fd);
#else
		ALOGE("%s: Unable to find memory", __func__);
		goto error;
#endif
	}

	output->memory = memory;
	output->memory_address = memory_address;
#ifdef EXYNOS_ION
	output->memory_ion_fd = memory_ion_fd;
#endif
	output->memory_index = 0;
	output->buffers_count = buffers_count;
	output->buffer_length = buffer_length;

	output->enabled = 1;

	rc = 0;
	goto complete;

error:
	if (memory != NULL && memory->release != NULL) {
		memory->release(memory);
		output->memory = NULL;
	}

#ifdef EXYNOS_ION
	if (memory_ion_fd >= 0)
		smdk4x12_ion_free(smdk4x12_camera, memory_ion_fd);
#endif

	smdk4x12_v4l2_close(smdk4x12_camera, v4l2_id);

	rc = -1;

complete:
	return rc;
}

void smdk4x12_v4l2_output_stop(struct smdk4x12_camera *smdk4x12_camera,
	struct smdk4x12_v4l2_output *output)
{
	int v4l2_id;
	int rc;

	if (smdk4x12_camera == NULL || output == NULL)
		return;

//	ALOGD("%s()", __func__);

	if (!output->enabled) {
		ALOGE("Output was already stopped");
		return;
	}

	v4l2_id = output->v4l2_id;

	rc = smdk4x12_v4l2_reqbufs_out(smdk4x12_camera, v4l2_id, 0);
	if (rc < 0)
		ALOGE("%s: Unable to request buffers", __func__);

	if (output->memory != NULL && output->memory->release != NULL) {
		output->memory->release(output->memory);
		output->memory = NULL;
	}

#ifdef EXYNOS_ION
	if (output->memory_ion_fd >= 0) {
		smdk4x12_ion_free(smdk4x12_camera, output->memory_ion_fd);
		output->memory_ion_fd = -1;
	}
#endif

	smdk4x12_v4l2_close(smdk4x12_camera, v4l2_id);

	output->enabled = 0;
}

int smdk4x12_v4l2_output(struct smdk4x12_camera *smdk4x12_camera,
	struct smdk4x12_v4l2_output *output, int buffer_address)
{
	struct fimc_buf fimc_buffer;
	void *fb_base;
	int width, height, format;
	int buffer_width, buffer_height, buffer_format;
	int buffer_length;
	int address;
	int v4l2_id;
	int rc;

	if (smdk4x12_camera == NULL || output == NULL)
		return -EINVAL;

//	ALOGD("%s()", __func__);

	if (!output->enabled) {
		ALOGE("Output was not started");
		return -1;
	}

	width = output->width;
	height = output->height;
	format = output->format;

	buffer_width = output->buffer_width;
	buffer_height = output->buffer_height;
	buffer_format = output->buffer_format;

	buffer_length = output->buffer_length;
	v4l2_id = output->v4l2_id;

	rc = smdk4x12_v4l2_g_fbuf(smdk4x12_camera, v4l2_id, &fb_base, NULL, NULL, NULL);
	if (rc < 0) {
		ALOGE("%s: Unable to get fbuf", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_s_fbuf(smdk4x12_camera, v4l2_id, fb_base, width, height, format);
	if (rc < 0) {
		ALOGE("%s: Unable to set fbuf", __func__);
		goto error;
	}

	memset(&fimc_buffer, 0, sizeof(fimc_buffer));

	address = output->memory_address + buffer_length * output->memory_index;

	smdk4x12_camera_yuv_planes(width, height, format, address, (int *) &fimc_buffer.base[0], (int *) &fimc_buffer.base[1], (int *) &fimc_buffer.base[2]);

	rc = smdk4x12_v4l2_s_ctrl(smdk4x12_camera, v4l2_id, V4L2_CID_DST_INFO, (int) &fimc_buffer);
	if (rc < 0) {
		ALOGE("%s: Unable to set dst info", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_s_fmt_win(smdk4x12_camera, v4l2_id, 0, 0, width, height);
	if (rc < 0) {
		ALOGE("%s: Unable to set overlay win", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_streamon_out(smdk4x12_camera, v4l2_id);
	if (rc < 0) {
		ALOGE("%s: Unable to start stream", __func__);
		goto error;
	}

	memset(&fimc_buffer, 0, sizeof(fimc_buffer));

	smdk4x12_camera_yuv_planes(buffer_width, buffer_height, buffer_format, buffer_address, (int *) &fimc_buffer.base[0], (int *) &fimc_buffer.base[1], (int *) &fimc_buffer.base[2]);

	rc = smdk4x12_v4l2_qbuf_out(smdk4x12_camera, v4l2_id, 0, (unsigned long) &fimc_buffer);
	if (rc < 0) {
		ALOGE("%s: Unable to queue buffer", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_dqbuf_out(smdk4x12_camera, v4l2_id);
	if (rc < 0) {
		ALOGE("%s: Unable to dequeue buffer", __func__);
		goto error;
	}

	rc = smdk4x12_v4l2_streamoff_out(smdk4x12_camera, v4l2_id);
	if (rc < 0) {
		ALOGE("%s: Unable to stop stream", __func__);
		goto error;
	}

	rc = 0;
	goto complete;

error:
	rc = -1;

complete:
	return rc;
}

int smdk4x12_v4l2_output_release(struct smdk4x12_camera *smdk4x12_camera,
	struct smdk4x12_v4l2_output *output)
{
	int buffers_count;
	int memory_index;

	if (smdk4x12_camera == NULL || output == NULL)
		return -EINVAL;

//	ALOGD("%s()", __func__);

	buffers_count = output->buffers_count;
	memory_index = output->memory_index;

	memory_index++;
	output->memory_index = memory_index % buffers_count;

	return 0;
}