summaryrefslogtreecommitdiffstats
path: root/camera/exynos_jpeg.c
blob: 24e74fc2d763ea2e10aaa4b065f4ef9492a81694 (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (C) 2013 Paul Kocialkowski
 *
 * 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 "exynos_jpeg"
#include <utils/Log.h>

#include "exynos_camera.h"

#ifdef EXYNOS_JPEG_HW
int exynos_jpeg_start(struct exynos_camera *exynos_camera,
	struct exynos_jpeg *jpeg)
{
	struct jpeg_config config;
	struct jpeg_buf *buffer_in;
	struct jpeg_buf *buffer_out;
	camera_memory_t *memory = NULL;
#ifdef EXYNOS_ION
	int memory_ion_fd = -1;
#endif
	int address;
	int fd = -1;
	int rc;

	if (exynos_camera == NULL || jpeg == NULL)
		return -EINVAL;

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

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

	buffer_in = &jpeg->buffer_in;
	buffer_out = &jpeg->buffer_out;

#ifdef EXYNOS_ION
	jpeg->memory_in_ion_fd = -1;
	jpeg->memory_out_ion_fd = -1;
#endif

	fd = jpeghal_enc_init();
	if (fd < 0) {
		ALOGE("%s: Unable to init jpeg encoder", __func__);
		goto error;
	}

	jpeg->fd = fd;

	memset(&config, 0, sizeof(config));
	config.mode = JPEG_ENCODE;
	config.width = jpeg->width;
	config.height = jpeg->height;
	config.num_planes = 1;
	config.pix.enc_fmt.in_fmt = jpeg->format;
	config.pix.enc_fmt.out_fmt = V4L2_PIX_FMT_JPEG_420;

	if (jpeg->quality >= 90)
		config.enc_qual = QUALITY_LEVEL_1;
	else if (jpeg->quality >= 80)
		config.enc_qual = QUALITY_LEVEL_2;
	else if (jpeg->quality >= 70)
		config.enc_qual = QUALITY_LEVEL_3;
	else
		config.enc_qual = QUALITY_LEVEL_4;

	rc = jpeghal_enc_setconfig(fd, &config);
	if (rc < 0) {
		ALOGE("%s: Unable to set jpeg config", __func__);
		goto error;
	}

	rc = jpeghal_s_ctrl(fd, V4L2_CID_CACHEABLE, 1);
	if (rc < 0) {
		ALOGE("%s: Unable to set cacheable control", __func__);
		goto error;
	}

	memset(buffer_in, 0, sizeof(struct jpeg_buf));
	buffer_in->memory = V4L2_MEMORY_MMAP;
	buffer_in->num_planes = 1;

	memset(buffer_out, 0, sizeof(struct jpeg_buf));
	buffer_out->memory = V4L2_MEMORY_MMAP;
	buffer_out->num_planes = 1;

	rc = jpeghal_set_inbuf(fd, buffer_in);
	if (rc < 0) {
#ifdef EXYNOS_ION
		// Input

		buffer_in->memory = V4L2_MEMORY_USERPTR;
		buffer_in->length[0] = exynos_camera_buffer_length(jpeg->width, jpeg->height, jpeg->format);

		memory_ion_fd = exynos_ion_alloc(exynos_camera, buffer_in->length[0]);
		if (memory_ion_fd < 0) {
			ALOGE("%s: Unable to alloc input ION memory", __func__);
			goto error;
		}

		address = exynos_ion_phys(exynos_camera, memory_ion_fd);

		if (EXYNOS_CAMERA_CALLBACK_DEFINED(request_memory)) {
			memory = exynos_camera->callbacks.request_memory(memory_ion_fd, buffer_in->length[0], 1, exynos_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;
		}

		jpeg->memory_in = memory;
		jpeg->memory_in_pointer = memory->data;
		jpeg->memory_in_ion_fd = memory_ion_fd;
		buffer_in->start[0] = (void *) address;

		rc = jpeghal_set_inbuf(fd, buffer_in);
		if (rc < 0) {
			ALOGE("%s: Unable to set input buffer", __func__);
			goto error;
		}

		// Output

		buffer_out->memory = V4L2_MEMORY_USERPTR;
		buffer_out->length[0] = jpeg->width * jpeg->height * 4;

		memory_ion_fd = exynos_ion_alloc(exynos_camera, buffer_out->length[0]);
		if (memory_ion_fd < 0) {
			ALOGE("%s: Unable to alloc output ION memory", __func__);
			goto error;
		}

		address = exynos_ion_phys(exynos_camera, memory_ion_fd);

		if (EXYNOS_CAMERA_CALLBACK_DEFINED(request_memory)) {
			memory = exynos_camera->callbacks.request_memory(memory_ion_fd, buffer_out->length[0], 1, exynos_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;
		}

		jpeg->memory_out = memory;
		jpeg->memory_out_pointer = memory->data;
		jpeg->memory_out_ion_fd = memory_ion_fd;
		buffer_out->start[0] = (void *) address;
#else
		ALOGE("%s: Unable to set input buffer", __func__);
		goto error;
#endif
	} else {
		jpeg->memory_in_pointer = buffer_in->start[0];
		jpeg->memory_out_pointer = buffer_out->start[0];
	}

	rc = jpeghal_set_outbuf(fd, buffer_out);
	if (rc < 0) {
		ALOGE("%s: Unable to set output buffer", __func__);
		goto error;
	}

	jpeg->enabled = 1;

	rc = 0;
	goto complete;

error:
	if (fd >= 0) {
		// Avoid releasing unrequested mmap buffers

		if (buffer_in->memory == 0)
			buffer_in->memory = V4L2_MEMORY_USERPTR;

		if (buffer_out->memory == 0)
			buffer_out->memory = V4L2_MEMORY_USERPTR;

		jpeghal_deinit(fd, buffer_in, buffer_out);
		jpeg->fd = -1;
	}

	if (jpeg->memory_in != NULL && jpeg->memory_in->release != NULL) {
		jpeg->memory_in->release(jpeg->memory_in);
		jpeg->memory_in = NULL;
#ifdef EXYNOS_ION
		if (jpeg->memory_in_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_in_ion_fd);
			jpeg->memory_in_ion_fd = -1;
		}
#endif
	}

	if (jpeg->memory_out != NULL && jpeg->memory_out->release != NULL) {
		jpeg->memory_out->release(jpeg->memory_out);
		jpeg->memory_out = NULL;

#ifdef EXYNOS_ION
		if (jpeg->memory_out_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_out_ion_fd);
			jpeg->memory_out_ion_fd = -1;
		}
#endif
	}

	rc = -1;

complete:
	return rc;
}

void exynos_jpeg_stop(struct exynos_camera *exynos_camera,
	struct exynos_jpeg *jpeg)
{
	struct jpeg_buf *buffer_in;
	struct jpeg_buf *buffer_out;
	int fd = -1;
	int rc;

	if (exynos_camera == NULL || jpeg == NULL)
		return;

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

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

	buffer_in = &jpeg->buffer_in;
	buffer_out = &jpeg->buffer_out;

	fd = jpeg->fd;

	if (fd >= 0) {
		jpeghal_deinit(fd, buffer_in, buffer_out);
		jpeg->fd = -1;
	}

	if (jpeg->memory_in != NULL && jpeg->memory_in->release != NULL) {
		jpeg->memory_in->release(jpeg->memory_in);
		jpeg->memory_in = NULL;
#ifdef EXYNOS_ION
		if (jpeg->memory_in_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_in_ion_fd);
			jpeg->memory_in_ion_fd = -1;
		}
#endif
	}

	if (jpeg->memory_out != NULL && jpeg->memory_out->release != NULL) {
		jpeg->memory_out->release(jpeg->memory_out);
		jpeg->memory_out = NULL;

#ifdef EXYNOS_ION
		if (jpeg->memory_out_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_out_ion_fd);
			jpeg->memory_out_ion_fd = -1;
		}
#endif
	}

	jpeg->enabled = 0;
}

int exynos_jpeg(struct exynos_camera *exynos_camera, struct exynos_jpeg *jpeg)
{
	struct jpeg_buf *buffer_in;
	struct jpeg_buf *buffer_out;
	int memory_size;
	int fd = -1;
	int rc;

	if (exynos_camera == NULL || jpeg == NULL)
		return -EINVAL;

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

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

	buffer_in = &jpeg->buffer_in;
	buffer_out = &jpeg->buffer_out;

	fd = jpeg->fd;
	if (fd < 0) {
		ALOGE("%s: Invalid jpeg fd", __func__);
		goto error;
	}

#ifdef EXYNOS_ION
	if (jpeg->memory_in != NULL && jpeg->memory_in_ion_fd >= 0) {
		rc = exynos_ion_msync(exynos_camera, jpeg->memory_in_ion_fd, 0, buffer_in->length[0]);
		if (rc < 0) {
			ALOGE("%s: Unable to sync ION memory", __func__);
			goto error;
		}
	}
#endif

	rc = jpeghal_enc_exe(fd, buffer_in, buffer_out);
	if (rc < 0) {
		ALOGE("%s: Unable to encode jpeg", __func__);
		goto error;
	}

	memory_size = jpeghal_g_ctrl(fd, V4L2_CID_CAM_JPEG_ENCODEDSIZE);
	if (memory_size <= 0) {
		ALOGE("%s: Unable to get jpeg size", __func__);
		goto error;
	}

	jpeg->memory_out_size = memory_size;

#ifdef EXYNOS_ION
	if (jpeg->memory_out != NULL && jpeg->memory_out_ion_fd >= 0) {
		rc = exynos_ion_msync(exynos_camera, jpeg->memory_out_ion_fd, 0, memory_size);
		if (rc < 0) {
			ALOGE("%s: Unable to sync ION memory", __func__);
			goto error;
		}
	}
#endif

	rc = 0;
	goto complete;

error:
	if (fd >= 0) {
		// Avoid releasing unrequested mmap buffers

		if (buffer_in->memory == 0)
			buffer_in->memory = V4L2_MEMORY_USERPTR;

		if (buffer_out->memory == 0)
			buffer_out->memory = V4L2_MEMORY_USERPTR;

		jpeghal_deinit(fd, buffer_in, buffer_out);
		jpeg->fd = -1;
	}

	if (jpeg->memory_in != NULL && jpeg->memory_in->release != NULL) {
		jpeg->memory_in->release(jpeg->memory_in);
		jpeg->memory_in = NULL;

#ifdef EXYNOS_ION
		if (jpeg->memory_in_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_in_ion_fd);
			jpeg->memory_in_ion_fd = -1;
		}
#endif
	}

	if (jpeg->memory_out != NULL && jpeg->memory_out->release != NULL) {
		jpeg->memory_out->release(jpeg->memory_out);
		jpeg->memory_out = NULL;

#ifdef EXYNOS_ION
		if (jpeg->memory_out_ion_fd >= 0) {
			exynos_ion_free(exynos_camera, jpeg->memory_out_ion_fd);
			jpeg->memory_out_ion_fd = -1;
		}
#endif
	}

	rc = -1;

complete:
	return rc;
}
#endif