summaryrefslogtreecommitdiffstats
path: root/src/surface.c
blob: 30631f5e84653cfa285bbf3d6bed5f20be243b4e (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
/*
 * Copyright (c) 2016 Florent Revest, <florent.revest@free-electrons.com>
 *               2007 Intel Corporation. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "sunxi_cedrus_drv_video.h"
#include "surface.h"

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

#include <sys/mman.h>
#include <sys/ioctl.h>

#include <linux/videodev2.h>
#include <linux/media.h>

#include <X11/Xlib.h>

/*
 * A Surface is an internal data structure never handled by the VA's user
 * containing the output of a rendering. Usualy, a bunch of surfaces are created
 * at the begining of decoding and they are then used alternatively. When
 * created, a surface is assigned a corresponding v4l capture buffer and it is
 * kept until the end of decoding. Syncing a surface waits for the v4l buffer to
 * be available and then dequeue it.
 *
 * Note: since a Surface is kept private from the VA's user, it can ask to
 * directly render a Surface on screen in an X Drawable. Some kind of
 * implementation is available in PutSurface but this is only for development
 * purpose.
 */

VAStatus sunxi_cedrus_CreateSurfaces(VADriverContextP ctx, int width,
		int height, int format, int num_surfaces, VASurfaceID *surfaces)
{
	INIT_DRIVER_DATA
	VAStatus vaStatus = VA_STATUS_SUCCESS;
	int i, j;
	struct v4l2_buffer buf;
	struct v4l2_plane planes[2];
	struct v4l2_create_buffers create_bufs;
	struct v4l2_format fmt;
	void *map;

	memset(planes, 0, 2 * sizeof(struct v4l2_plane));

	printf("%s()\n", __func__);

	/* We only support one format */
	if (VA_RT_FORMAT_YUV420 != format)
		return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;

	printf("%s() format is good\n", __func__);

	/* Set format for capture */
	memset(&(fmt), 0, sizeof(fmt));
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	fmt.fmt.pix_mp.width = width;
	fmt.fmt.pix_mp.height = height;
	fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_SUNXI;
	fmt.fmt.pix_mp.field = V4L2_FIELD_ANY;
	fmt.fmt.pix_mp.num_planes = 2;
	assert(ioctl(driver_data->mem2mem_fd, VIDIOC_S_FMT, &fmt)==0);

	memset (&create_bufs, 0, sizeof (struct v4l2_create_buffers));
	create_bufs.count = num_surfaces;
	create_bufs.memory = V4L2_MEMORY_MMAP;
	create_bufs.format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	assert(ioctl(driver_data->mem2mem_fd, VIDIOC_G_FMT, &create_bufs.format)==0);
	assert(ioctl(driver_data->mem2mem_fd, VIDIOC_CREATE_BUFS, &create_bufs)==0);
//	driver_data->num_dst_bufs = create_bufs.count;

	driver_data->destination_index_offset = create_bufs.index;

	for (i = 0; i < create_bufs.count; i++)
	{
		VASurfaceID surfaceID = object_heap_allocate(&driver_data->surface_heap);

		object_surface_p obj_surface = SURFACE(surfaceID);
		if (NULL == obj_surface)
		{
			vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
			break;
		}
		// FIXME memset 0
		obj_surface->surface_id = surfaceID;
		surfaces[i] = surfaceID;

		printf("%s: registering surface with ID %d\n", __func__, surfaceID);

		obj_surface->header_data = NULL;
		obj_surface->slice_offset = 0;

		memset(&(buf), 0, sizeof(buf));
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
		buf.memory = V4L2_MEMORY_MMAP;
		buf.index = create_bufs.index + i;
		buf.length = 2;
		buf.m.planes = planes;

		assert(ioctl(driver_data->mem2mem_fd, VIDIOC_QUERYBUF, &buf)==0);

		for (j = 0; j < 2; j++) {
			map = mmap(NULL, buf.m.planes[j].length, PROT_READ | PROT_WRITE, MAP_SHARED, driver_data->mem2mem_fd, buf.m.planes[j].m.mem_offset);
			assert(map != MAP_FAILED);

			obj_surface->destination_buffers[j] = map;
		}

		obj_surface->index = i;

		obj_surface->width = width;
		obj_surface->height = height;
		obj_surface->status = VASurfaceReady;
	}

	/* Error recovery */
	if (VA_STATUS_SUCCESS != vaStatus)
	{
		/* surfaces[i-1] was the last successful allocation */
		for(; i--;)
		{
			object_surface_p obj_surface = SURFACE(surfaces[i]);
			surfaces[i] = VA_INVALID_SURFACE;
			assert(obj_surface);
			object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
		}
	}
	return vaStatus;
}

VAStatus sunxi_cedrus_DestroySurfaces(VADriverContextP ctx,
		VASurfaceID *surface_list, int num_surfaces)
{
	INIT_DRIVER_DATA
	int i;
	for(i = num_surfaces; i--;)
	{
		object_surface_p obj_surface = SURFACE(surface_list[i]);
		assert(obj_surface);

		if (obj_surface->header_data != NULL)
			free(obj_surface->header_data);

		object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
	}
	return VA_STATUS_SUCCESS;
}

VAStatus sunxi_cedrus_SyncSurface(VADriverContextP ctx,
		VASurfaceID render_target)
{
	INIT_DRIVER_DATA
	object_surface_p obj_surface;
	struct v4l2_buffer buf;
	struct v4l2_plane plane[1];
	struct v4l2_plane planes[2];
        fd_set expect_fds;
	struct timeval tv = {0, 300000};
	int request_fd;
	int rc;

	memset(plane, 0, sizeof(struct v4l2_plane));
	memset(planes, 0, 2 * sizeof(struct v4l2_plane));

	obj_surface = SURFACE(render_target);
	assert(obj_surface);

	if (obj_surface->status == VASurfaceReady)
		return VA_STATUS_SUCCESS;

	if(obj_surface->status == VASurfaceSkipped)
		return VA_STATUS_ERROR_UNKNOWN;

	printf("> %s(%d)\n", __func__, render_target);


	request_fd = obj_surface->request_fd;
	if(request_fd < 0)
		return  VA_STATUS_ERROR_UNKNOWN;

	printf(">> %s: input buffer #%d, request fd %d\n", __func__, obj_surface->index, request_fd);

	assert(ioctl(request_fd, MEDIA_REQUEST_IOC_QUEUE, NULL)==0);

	FD_ZERO(&expect_fds);
	FD_SET(request_fd, &expect_fds);

	rc = select(request_fd + 1, NULL, NULL, &expect_fds, &tv);

	printf(">> %s: select returned %d\n", __func__, rc);

	if(rc <= 0)
		// FIXME: Properly dispose of the buffers here, also reinit request when it fails, also set surface status
		return VA_STATUS_ERROR_UNKNOWN;

//	assert(ioctl(request_fd, MEDIA_REQUEST_IOC_REINIT, NULL)==0); // moved to picture

	memset(&(buf), 0, sizeof(buf));
	buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
	buf.memory = V4L2_MEMORY_MMAP;
	buf.index = driver_data->source_index_offset + obj_surface->index;
	buf.length = 1;
	buf.m.planes = plane;

	if(ioctl(driver_data->mem2mem_fd, VIDIOC_DQBUF, &buf)) {
		sunxi_cedrus_msg("Error when dequeuing input: %s\n", strerror(errno));
		return VA_STATUS_ERROR_UNKNOWN;
	}

	memset(&(buf), 0, sizeof(buf));
	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	buf.memory = V4L2_MEMORY_MMAP;
	buf.index = driver_data->destination_index_offset + obj_surface->index;
	buf.length = 2;
	buf.m.planes = planes;


	if(ioctl(driver_data->mem2mem_fd, VIDIOC_DQBUF, &buf)) {
		sunxi_cedrus_msg("Error when dequeuing output: %s\n", strerror(errno));
		return VA_STATUS_ERROR_UNKNOWN;
	}

	obj_surface->status = VASurfaceReady;

	printf("< %s()\n", __func__);

	return VA_STATUS_SUCCESS;
}

VAStatus sunxi_cedrus_QuerySurfaceStatus(VADriverContextP ctx,
		VASurfaceID render_target, VASurfaceStatus *status)
{
	INIT_DRIVER_DATA
	VAStatus vaStatus = VA_STATUS_SUCCESS;
	object_surface_p obj_surface;

	obj_surface = SURFACE(render_target);
	assert(obj_surface);

	*status = obj_surface->status;

	return vaStatus;
}

/* WARNING: This is for development purpose only!!! */
VAStatus sunxi_cedrus_PutSurface(VADriverContextP ctx, VASurfaceID surface,
		void *draw, short srcx, short srcy, unsigned short srcw,
		unsigned short srch, short destx, short desty,
		unsigned short destw, unsigned short desth,
		VARectangle *cliprects, unsigned int number_cliprects,
		unsigned int flags)
{
/*
	INIT_DRIVER_DATA
	GC gc;
	Display *display;
	const XID xid = (XID)(uintptr_t)draw;
	XColor xcolor;
	int screen;
	Colormap cm;
	int colorratio = 65535 / 255;
	int x, y;
	object_surface_p obj_surface;

	obj_surface = SURFACE(surface);
	assert(obj_surface);

	display = XOpenDisplay(getenv("DISPLAY"));
	if (display == NULL) {
		sunxi_cedrus_msg("Cannot connect to X server\n");
		exit(1);
	}

	sunxi_cedrus_msg("warning: using vaPutSurface with sunxi-cedrus is not recommended\n");
	screen = DefaultScreen(display);
	gc =  XCreateGC(display, RootWindow(display, screen), 0, NULL);
	XSync(display, False);

	cm = DefaultColormap(display, screen);
	xcolor.flags = DoRed | DoGreen | DoBlue;

	for(x=destx; x < destx+destw; x++) {
		for(y=desty; y < desty+desth; y++) {
			char lum = driver_data->luma_bufs[obj_surface->output_buf_index][x+srcw*y];
			xcolor.red = xcolor.green = xcolor.blue = lum*colorratio;
			XAllocColor(display, cm, &xcolor);
			XSetForeground(display, gc, xcolor.pixel);
			XDrawPoint(display, xid, gc, x, y);
		}
	}

	XFlush(display);
	XCloseDisplay(display);
*/
	return VA_STATUS_SUCCESS;
}

VAStatus sunxi_cedrus_LockSurface(VADriverContextP ctx, VASurfaceID surface,
		unsigned int *fourcc, unsigned int *luma_stride,
		unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
		unsigned int *luma_offset, unsigned int *chroma_u_offset,
		unsigned int *chroma_v_offset, unsigned int *buffer_name,
		void **buffer)
{ return VA_STATUS_ERROR_UNIMPLEMENTED; }

VAStatus sunxi_cedrus_UnlockSurface(VADriverContextP ctx, VASurfaceID surface)
{ return VA_STATUS_ERROR_UNIMPLEMENTED; }