aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-graphics/wayland/weston/0003-gst-recorder-Use-USERPTR-instead-of-DMABUF-for-VSP-o.patch
blob: 095507c1812677e61c6885405c70cde893bf50a6 (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
From ab3cf8044667bff4a9e2e5108c55dd198fa353be Mon Sep 17 00:00:00 2001
From: Damian Hobson-Garcia <dhobsong@igel.co.jp>
Date: Thu, 11 May 2017 12:05:56 +0900
Subject: [PATCH 3/4] gst-recorder: Use USERPTR instead of DMABUF for VSP
 output

The RCar-Gen3 encoder requires buffers to be allocated and managed
externally when using dmabuf buffers.  Since different sized buffers
are required for each output, the VSP cannot allocate these buffers
externally.  The encoder provides its own buffers in USERPTR mode, so
switch to that.
---
 src/gst-recorder.c | 100 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 63 insertions(+), 37 deletions(-)

diff --git a/src/gst-recorder.c b/src/gst-recorder.c
index 2e3b359..271fb69 100644
--- a/src/gst-recorder.c
+++ b/src/gst-recorder.c
@@ -314,7 +314,7 @@ vsp_init(const char *devname)
 		weston_log("failed to open subdev '%s'\n", buf);
 		goto error_media;
 	}
-	else if ((vsp->output.fd = open(media_entity_get_devname(entity), O_RDWR | O_NONBLOCK)) < 0)
+	else if ((vsp->output.fd = open(media_entity_get_devname(entity), O_RDWR )) < 0)
 	{
 		weston_log("failed to open device '%s'\n", media_entity_get_devname(entity));
 		goto error_media;
@@ -467,7 +467,8 @@ vsp_request_buffers(vsp_data_t *vsp, vsp_port_n port, unsigned int num)
 	memset(&reqbuf, 0, sizeof(reqbuf));
 	reqbuf.type = (port == VSP_PORT_INPUT) ?
 		V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-	reqbuf.memory = V4L2_MEMORY_DMABUF;
+	reqbuf.memory = (port == VSP_PORT_INPUT) ?
+		V4L2_MEMORY_DMABUF : V4L2_MEMORY_USERPTR;
 	reqbuf.count = num;
 	if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
 		weston_log("VSP: %s REQBUFS failed: %d\n",
@@ -539,7 +540,8 @@ vsp_input_buffer_dequeue_dmafd(vsp_data_t *vsp)
 
 /* ...enqueue output buffer */
 static int
-vsp_output_buffer_queue_dmafd(vsp_data_t *vsp, int i, int dmafd[])
+vsp_output_buffer_queue_userptr(vsp_data_t *vsp, int i, void * omx_mem,
+		int y_plane_size, int c_plane_size)
 {
 	vsp_media_pad_t    *pad = &vsp->output;
 	struct v4l2_plane   planes[2];
@@ -549,16 +551,23 @@ vsp_output_buffer_queue_dmafd(vsp_data_t *vsp, int i, int dmafd[])
 	memset(&buf, 0, sizeof(buf));
 	memset(planes, 0, sizeof(planes));
 	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.memory = V4L2_MEMORY_USERPTR;
 	buf.index = i;
 	buf.m.planes = planes;
 	buf.length = VSP_OUTPUT_BUFFERS_PLANE;
-	buf.m.planes[0].m.fd = dmafd[0];
-	buf.m.planes[1].m.fd = dmafd[1];
+
+	buf.m.planes[0].m.userptr = (unsigned long) omx_mem;
+	buf.m.planes[1].m.userptr = (unsigned long) omx_mem + y_plane_size;
+
+	buf.m.planes[0].bytesused = y_plane_size;
+	buf.m.planes[0].length = y_plane_size;
+
+	buf.m.planes[1].bytesused = c_plane_size;
+	buf.m.planes[1].length = c_plane_size;
 
 	/* ...submit buffer */
 	if (ioctl(pad->fd, VIDIOC_QBUF, &buf) < 0) {
-		weston_log("VSP: output dmafd queue failed: %d\n", errno);
+		weston_log("VSP: output buffer queue failed: %d\n", errno);
 		return -1;
 	}
 
@@ -567,7 +576,7 @@ vsp_output_buffer_queue_dmafd(vsp_data_t *vsp, int i, int dmafd[])
 
 /* ...dequeue output buffer */
 static int
-vsp_output_buffer_dequeue_dmafd(vsp_data_t *vsp)
+vsp_output_buffer_dequeue_userptr(vsp_data_t *vsp)
 {
 	vsp_media_pad_t    *pad = &vsp->output;
 	struct v4l2_buffer  buf;
@@ -577,12 +586,12 @@ vsp_output_buffer_dequeue_dmafd(vsp_data_t *vsp)
 	memset(&buf, 0, sizeof(buf));
 	memset(planes, 0, sizeof(planes));
 	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.memory = V4L2_MEMORY_USERPTR;
 	buf.m.planes = planes;
 	buf.length = VSP_OUTPUT_BUFFERS_PLANE;
 
 	if (ioctl(pad->fd, VIDIOC_DQBUF, &buf) < 0) {
-		weston_log("VSP: output dmafd de-queue failed: %d\n", errno);
+		weston_log("VSP: output buffer de-queue failed: %d\n", errno);
 		return -1;
 	}
 
@@ -879,33 +888,25 @@ err:
 }
 
 static int
-gst_recorder_omx_buffer_acquire(struct gst_recorder *r, GstBuffer **ret_buf, int fd[])
+gst_recorder_omx_buffer_acquire(struct gst_recorder *r, GstBuffer **ret_buf, GstMapInfo *info)
 {
-	unsigned int i;
 	GstFlowReturn ret;
 	GstBuffer *buf;
-	guint n_mem;
-	GstMemory *mem;
 
 	ret = gst_buffer_pool_acquire_buffer(r->omx_pool, &buf, NULL);
 	if (ret != GST_FLOW_OK) {
-		weston_log("OMX buffer acquire failed\n");
-		return -1;
+		weston_log("GStreamer buffer acquire failed\n");
+		goto err_release;
 	}
 
-	n_mem = gst_buffer_n_memory(buf);
-	if (n_mem < 1) {
-		weston_log("Buffer with no mem!\n");
+	if (!gst_buffer_is_writable(buf)) {
+		weston_log("GStreamer buffer not writable\n");
 		goto err_release;
 	}
 
-	for (i = 0; i < n_mem; i++) {
-		mem = gst_buffer_peek_memory (buf, i);
-		if (!gst_is_dmabuf_memory (mem)) {
-			weston_log("Mem not dmabuf\n");
-			goto err_release;
-		}
-		fd[i] = gst_dmabuf_memory_get_fd (mem);
+	if (!gst_buffer_map(buf, info, GST_MAP_WRITE)) {
+		weston_log("Cannot map GStreamer buffer\n");
+		goto err_release;
 	}
 
 	*ret_buf = buf;
@@ -959,7 +960,7 @@ gst_recorder_create(struct gst_recorder_settings *settings)
 
 	/* omx */
 	ptr += sprintf(ptr,
-		"omxh264enc target-bitrate=%d control-rate=2 name=my_encoder ! "
+		"omxh264enc target-bitrate=%d control-rate=2 no-copy=true name=my_encoder ! "
 		"video/x-h264,width=%d,height=%d ! ",
 		r->set->bitrate, r->set->crop.width, r->set->crop.height);
 
@@ -1012,6 +1013,12 @@ gst_recorder_create(struct gst_recorder_settings *settings)
 					"framerate", GST_TYPE_FRACTION, 0, DEFAULT_FPS,
 					NULL), NULL);
 
+	g_object_set(G_OBJECT(r->appsrc),
+			"stream-type", 0,
+			"format", GST_FORMAT_TIME,
+			"is-live", TRUE,
+			NULL);
+
 	r->appsrc_pad = gst_element_get_static_pad(GST_ELEMENT_CAST(r->appsrc), "src");
 	if (!r->appsrc_pad)
 		weston_log("Failed to get src0 pad of appsrc\n");
@@ -1088,14 +1095,23 @@ gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride)
 {
 	int ret;
 	GstBuffer *buf;
-	int omx_fd[2];
+	GstMapInfo info;
+	int ysize;
+	int csize;
 
         /* get GST buffer */
-	if (gst_recorder_omx_buffer_acquire(r, &buf, omx_fd) < 0) {
+	if (gst_recorder_omx_buffer_acquire(r, &buf, &info) < 0) {
 		weston_log("VSP: can not acquire GST buffer, dropping frame\n");
 		return 0;
 	}
 
+	ysize = r->set->crop.width * r->set->crop.height;
+#ifdef VSP_OUTPUT_NV16
+	csize = ysize;
+#else
+	csize = ysize / 2;
+#endif
+
 	pthread_mutex_lock(&r->vsp->mutex);
 	/* setup vsp */
 	if (vsp_set_formats(r->vsp, r->set->width, r->set->height, &r->set->crop) < 0) {
@@ -1116,7 +1132,7 @@ gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride)
 	}
 
 	/* queue output biffer */
-	if (vsp_output_buffer_queue_dmafd(r->vsp, 0, omx_fd) < 0) {
+	if (vsp_output_buffer_queue_userptr(r->vsp, 0, info.data, ysize, csize) < 0) {
 		weston_log("can not queue OMX buffer %d to VSP\n", 0);
 		gst_recorder_omx_buffer_release(r, buf);
 		goto err_vsp;
@@ -1147,11 +1163,15 @@ gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride)
 	}
 
 	/* dequeue output */
-	if (vsp_output_buffer_dequeue_dmafd(r->vsp) < 0) {
-		weston_log("VSP: failed to dequeu output buffer\n");
+	if (vsp_output_buffer_dequeue_userptr(r->vsp) < 0) {
+		weston_log("VSP: failed to dequeue output buffer\n");
+		gst_buffer_unmap(buf, &info);
 		gst_recorder_omx_buffer_release(r, buf);
-		/* fall through */
 	} else {
+
+		gst_buffer_unmap(buf, &info);
+		gst_buffer_set_size(buf, ysize + csize);
+
 		/* set timestamp */
 		gst_recorder_set_timestamp(r, buf);
 
@@ -1174,6 +1194,7 @@ gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride)
 	vsp_request_buffers(r->vsp, VSP_PORT_OUTPUT, 0);
 
 	pthread_mutex_unlock(&r->vsp->mutex);
+	close(fd);
 	return 0;
 
 err_vsp:
@@ -1181,6 +1202,7 @@ err_vsp:
 	/* finish vsp here */
 err:
 	pthread_mutex_unlock(&r->vsp->mutex);
+	close(fd);
 	return -1;
 }
 
@@ -1197,9 +1219,13 @@ gst_recorder_frame_dmafd(struct gst_recorder *r, int fd, int stride)
 		goto unlock;
 	}
 		
-	/* The mutex is never released while encoding, so this point should
-	 * never be reached if input.valid is true. */
-	assert(!r->input.valid);
+	/* It is possible that the frame callback can be called mutiple
+	 * times before the worker thread wakes up.  In this case
+	 * drop all buf the first frame */
+	if(r->input.valid) {
+		close(fd);
+		goto unlock;
+	}
 
 	r->input.prime_fd = fd;
 	r->input.stride = stride;
@@ -1209,5 +1235,5 @@ gst_recorder_frame_dmafd(struct gst_recorder *r, int fd, int stride)
 unlock:
 	pthread_mutex_unlock(&r->mutex);
 
-	return 0;
+	return ret;
 }
-- 
1.9.1