summaryrefslogtreecommitdiff
path: root/v4l2-mfc-example/fimc.c
blob: ed9dc870a729ebb3fc7952d45e27ea596905a1e1 (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
/*
 * V4L2 Codec decoding example application
 * Kamil Debski <k.debski@samsung.com>
 *
 * FIMC operations
 *
 * Copyright 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <linux/videodev2.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

#include "common.h"
#include "fimc.h"

#include <errno.h>
#include <drm/drm_fourcc.h>


static char *dbg_type[2] = {"OUTPUT", "CAPTURE"};
static char *dbg_status[2] = {"ON", "OFF"};

int fimc_open(struct instance *i)
{
	struct v4l2_capability cap;
	int ret;

	i->fimc.fd = open(i->fimc.name, O_RDWR, 0);
	if (i->fimc.fd < 0) {
		err("Failed to open FIMC: %s", i->fimc.name);
		free(i->fimc.name);
		return -1;
	}

	memzero(cap);
	ret = ioctl(i->fimc.fd, VIDIOC_QUERYCAP, &cap);
	if (ret != 0) {
		err("Failed to verify capabilities");
		return -1;
	}

	dbg("FIMC Info (%s): driver=\"%s\" bus_info=\"%s\" card=\"%s\" fd=0x%x",
			i->fimc.name, cap.driver, cap.bus_info, cap.card, i->fimc.fd);

	if (	!(((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) &&
		  (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)) ||
		  (cap.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)) ||
		!(cap.capabilities & V4L2_CAP_STREAMING)) {
		err("Insufficient capabilities of FIMC device (is %s correct?)",
									i->fimc.name);
		free(i->fimc.name);

		return -1;
	}
	free(i->fimc.name);

        return 0;
}

void fimc_close(struct instance *i)
{
	close(i->fimc.fd);
}

int fimc_sfmt(struct instance *i, int width, int height,
	 enum v4l2_buf_type type, unsigned long pix_fmt, int num_planes,
	 struct v4l2_plane_pix_format planes[])
{
	struct v4l2_format fmt;
	int ret;
	int n;

	memzero(fmt);
	fmt.fmt.pix_mp.pixelformat = pix_fmt;
	fmt.type = type;
	fmt.fmt.pix_mp.width = width;
	fmt.fmt.pix_mp.height = height;
	fmt.fmt.pix_mp.num_planes = num_planes;

	for (n = 0; n < num_planes; n++)
		memcpy(&fmt.fmt.pix_mp.plane_fmt[n], &planes[n],
			sizeof(*planes));

	fmt.fmt.pix_mp.field = V4L2_FIELD_ANY;

	ret = ioctl(i->fimc.fd, VIDIOC_S_FMT, &fmt);

	if (ret != 0) {
		err("Failed to SFMT on %s of FIMC",
			dbg_type[type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE]);
		return -1;
	}

	if (fmt.fmt.pix_mp.width != width ||
		fmt.fmt.pix_mp.height != height ||
		fmt.fmt.pix_mp.num_planes != num_planes ||
		fmt.fmt.pix_mp.pixelformat != pix_fmt) {
		err("Format was changed by FIMC so we abort operations\n"
		    "This may happen around kernel 3.16 as the format supported by FIMC\n"
		    "was changed.\n\n"
		    "To fix this please change function fimc_setup_capture in fimc.c (around line 162)\n"
		    "It should use V4L2_PIX_FMT_BGR32 for 3.16+ instead of V4L2_PIX_FMT_RGB32 used by 3.15-\n\n"
		    "Please read patch: \"s5p-fimc: Changed RGB32 to BGR32\"\n"
		    "\thttp://www.spinics.net/lists/linux-media/msg74953.html\n"
		    "\thttps://patchwork.linuxtv.org/patch/23236/");
		if (i->fimc.ignore_format_change) {
			err("!!! IGNORING FORMAT CHANGE !!!");
		} else {
			return -1;
		}
	}

	dbg("Successful SFMT on %s of FIMC (%dx%d)",
			dbg_type[type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE],
			width, height);

	return 0;
}

int fimc_setup_output_from_mfc(struct instance *i)
{
	struct v4l2_plane_pix_format planes[MFC_CAP_PLANES];
	struct v4l2_requestbuffers reqbuf;
	int ret;
	int n;

	memzero(planes);
	for (n = 0; n < MFC_CAP_PLANES; n++) {
		planes[n].sizeimage = i->mfc.cap_buf_size[n];
		planes[n].bytesperline = i->mfc.cap_w;
	}

	ret = fimc_sfmt(i, i->mfc.cap_w, i->mfc.cap_h,
		V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, i->mfc.cap_pixfmt,
		MFC_CAP_PLANES, planes);

	if (ret)
		return ret;

	memzero(reqbuf);
	reqbuf.count = i->mfc.cap_buf_cnt;
	reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
	if (i->fimc.dmabuf) {
		reqbuf.memory = V4L2_MEMORY_DMABUF;
	} else {
		reqbuf.memory = V4L2_MEMORY_USERPTR;
	}

	ret = ioctl(i->fimc.fd, VIDIOC_REQBUFS, &reqbuf);
	if (ret) {
		err("REQBUFS failed on OUTPUT of FIMC");
		return -1;
	}

	dbg("Succesfully setup OUTPUT of FIMC");

	return 0;
}

int fimc_setup_capture(struct instance *i)
{
	struct v4l2_plane_pix_format planes[MFC_OUT_PLANES];
	struct v4l2_requestbuffers reqbuf;
	unsigned long fmt;
	int ret;

	memzero(planes);
	planes[0].sizeimage = i->fimc.stride * i->fimc.height;
	planes[0].bytesperline = i->fimc.stride;

	switch (i->fimc.bpp) {
	case 16:
		fmt = V4L2_PIX_FMT_RGB565;
		break;
	case 32:
		fmt = V4L2_PIX_FMT_BGR32;
		break;
	default:
		err("Framebuffer format in not recognized. Bpp=%d", i->fimc.bpp);
		return -1;
	}

	ret = fimc_sfmt(i, i->fimc.width, i->fimc.height,
		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, fmt, 1, planes);

	if (ret)
		return -1;

	memzero(reqbuf);
	reqbuf.count = i->fimc.buffers;
	dbg("reqbuf.count = %d", reqbuf.count);
	reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	if (i->fimc.dmabuf && i->drm.enabled)
		reqbuf.memory = V4L2_MEMORY_DMABUF;
	else
		reqbuf.memory = V4L2_MEMORY_USERPTR;

	ret = ioctl(i->fimc.fd, VIDIOC_REQBUFS, &reqbuf);
	if (ret) {
		err("REQBUFS failed on CAPTURE of FIMC");
		return -1;
	}

	dbg("Succesfully setup CAPTURE of FIMC");

	return 0;
}

int fimc_stream(struct instance *i, enum v4l2_buf_type type, int status)
{
	int ret;

	ret = ioctl(i->fimc.fd, status, &type);
	if (ret) {
		err("Failed to change streaming on FIMC (type=%s, status=%s)",
			dbg_type[type==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE],
			dbg_status[status==VIDIOC_STREAMOFF]);
		return -1;
	}

	dbg("Stream %s on %s queue\n", dbg_status[status==VIDIOC_STREAMOFF],
		dbg_type[type==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE]);

	return 0;
}

int fimc_dec_queue_buf_out_from_mfc(struct instance *inst, int n)
{
	struct v4l2_buffer buf;
	struct v4l2_plane planes[MFC_CAP_PLANES];
	int ret,p;

	memzero(buf);
	memzero(planes);
	buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
	buf.index = n;
	buf.m.planes = planes;
	buf.length = MFC_CAP_PLANES;

	if (inst->fimc.dmabuf) {
		buf.memory = V4L2_MEMORY_DMABUF;
	} else {
		buf.memory = V4L2_MEMORY_USERPTR;
	}

	for(p = 0; p < 2; p++) {
		buf.m.planes[p].bytesused = inst->mfc.cap_buf_size[p];
		buf.m.planes[p].length = inst->mfc.cap_buf_size[p];

		if (inst->fimc.dmabuf) {
			buf.m.planes[p].m.fd = inst->mfc.dbuf[n][p];
		} else {
			buf.m.planes[p].m.userptr = (unsigned long)inst->mfc.cap_buf_addr[n][p];
		}
	}
	ret = ioctl(inst->fimc.fd, VIDIOC_QBUF, &buf);

	if (ret) {
		err("Failed to queue buffer (index=%d) on CAPTURE", n);
		return -1;
	}

	dbg("Queued buffer on CAPTURE queue with index %d", n);

	return 0;
}

int fimc_dec_queue_buf_cap(struct instance *i, int n)
{
	struct v4l2_buffer buf;
	struct v4l2_plane planes[FIMC_CAP_PLANES];
	int ret;

	memzero(buf);
	memzero(planes);
	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	buf.index = n;
	buf.m.planes = planes;
	buf.length = FIMC_CAP_PLANES;

	buf.m.planes[0].bytesused = i->fimc.size;
	buf.m.planes[0].length = i->fimc.size;
	if (i->fimc.dmabuf && i->drm.enabled) {
		buf.memory = V4L2_MEMORY_DMABUF;
		buf.m.planes[0].m.fd = i->fimc.dbuf[n];
	} else {
		buf.memory = V4L2_MEMORY_USERPTR;
		buf.m.planes[0].m.userptr = (unsigned long)i->fimc.p[n];
	}

	ret = ioctl(i->fimc.fd, VIDIOC_QBUF, &buf);

	if (ret) {
		err("Failed to queue buffer (index=%d) on CAPTURE", n);
		return -1;
	}

	dbg("Queued buffer on OUTPUT queue with index %d", n);

	return 0;
}

int fimc_dec_dequeue_buf(struct instance *i, int *n, int nplanes, int type)
{
	struct v4l2_buffer buf;
	struct v4l2_plane planes[MFC_MAX_PLANES];
	int ret;

	memzero(buf);
	memzero(planes);
	buf.type = type;
	buf.memory = V4L2_MEMORY_USERPTR;
	buf.m.planes = planes;
	buf.length = nplanes;

	ret = ioctl(i->fimc.fd, VIDIOC_DQBUF, &buf);

	if (ret) {
		err("Failed to dequeue buffer");
		return -1;
	}

	*n = buf.index;

	dbg("Dequeued buffer with index %d on %s queue", buf.index,
		dbg_type[type==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE]);

	return 0;
}

int fimc_dec_dequeue_buf_cap(struct instance *i, int *n)
{
	return fimc_dec_dequeue_buf(i, n, 1, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
}

int fimc_dec_dequeue_buf_out(struct instance *i, int *n)
{
	return fimc_dec_dequeue_buf(i, n, 2, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
}

int fimc_set_crop(struct instance *i, int type, int width, int height, int left,
									int top)
{
	struct v4l2_crop crop;

	memzero(crop);
	crop.type = type;
	crop.c.width = width;
	crop.c.height = height;
	crop.c.left = left;
	crop.c.top = top;

	if (ioctl(i->fimc.fd, VIDIOC_S_CROP, &crop)) {
		err("Failed to set CROP on %s",
			dbg_type[type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE]);
		return -1;
	}

	return 0;
}