summaryrefslogtreecommitdiffstats
path: root/src/picture.c
blob: 2b30d992b918e4aa2e7ec257a9a2407cd702e27a (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
/*
 * 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 "picture.h"
#include "buffer.h"
#include "context.h"
#include "surface.h"
#include "va_config.h"

#include "mpeg2.h"
#include "mpeg4.h"

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

#include <errno.h>

#include <sys/ioctl.h>

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

/*
 * A Picture is an encoded input frame made of several buffers. A single input
 * can contain slice data, headers and IQ matrix. Each Picture is assigned a
 * request ID when created and each corresponding buffer might be turned into a
 * v4l buffers or extended control when rendered. Finally they are submitted to
 * kernel space when reaching EndPicture.
 */

VAStatus sunxi_cedrus_BeginPicture(VADriverContextP ctx, VAContextID context,
		VASurfaceID render_target)
{
	INIT_DRIVER_DATA
	VAStatus vaStatus = VA_STATUS_SUCCESS;
	object_context_p obj_context;
	object_surface_p obj_surface;
	int i, b;

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

	obj_context = CONTEXT(context);
	assert(obj_context);

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

/*
	if (obj_surface->status == VASurfaceRendering) {
printf("%s: Go to rendering\n", __func__);
		vaStatus = sunxi_cedrus_SyncSurface(ctx, render_target);
		if (vaStatus != VA_STATUS_SUCCESS)
			return vaStatus;
	}
*/

	obj_surface->status = VASurfaceRendering;

	printf("%s: surface %d\n", __func__, obj_surface->index);

//	driver_data->slice_offset[obj_surface->input_buf_index] = 0;

	obj_context->current_render_target = obj_surface->base.id;

	return vaStatus;
}

VAStatus sunxi_cedrus_RenderPicture(VADriverContextP ctx, VAContextID context,
		VABufferID *buffers, int num_buffers)
{
	INIT_DRIVER_DATA
	VAStatus vaStatus = VA_STATUS_SUCCESS;
	object_context_p obj_context;
	object_surface_p obj_surface;
	object_config_p obj_config;
	int i;

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

	obj_context = CONTEXT(context);
	assert(obj_context);

	obj_config = CONFIG(obj_context->config_id);
	if (NULL == obj_config)
	{
		vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
		return vaStatus;
	}

	obj_surface = SURFACE(obj_context->current_render_target);
	assert(obj_surface);

	printf("%s: current render surface: %d\n", __func__, obj_context->current_render_target);
//	printf("%s: surface index %d\n", __func__, obj_surface->input_buf_index);
	printf("%s: rendering %d buffers\n", __func__, num_buffers);

	/* verify that we got valid buffer references */
	for(i = 0; i < num_buffers; i++)
	{
		printf("%s: buffer %d\n", __func__, buffers[i]);
		object_buffer_p obj_buffer = BUFFER(buffers[i]);
		assert(obj_buffer);
		if (NULL == obj_buffer)
		{
			vaStatus = VA_STATUS_ERROR_INVALID_BUFFER;
			break;
		}

		switch(obj_config->profile) {
			case VAProfileMPEG2Simple:
			case VAProfileMPEG2Main:
				if(obj_buffer->type == VASliceDataBufferType)
					vaStatus = sunxi_cedrus_render_mpeg2_slice_data(ctx, obj_context, obj_surface, obj_buffer);
				else if(obj_buffer->type == VAPictureParameterBufferType)
					vaStatus = sunxi_cedrus_render_mpeg2_picture_parameter(ctx, obj_context, obj_surface, obj_buffer);
				else if(obj_buffer->type == VASliceParameterBufferType)
					vaStatus = sunxi_cedrus_render_mpeg2_slice_parameter(ctx, obj_context, obj_surface, obj_buffer);
				else
printf("%s: MPEG2 profile else\n", __func__);
				break;
			case VAProfileMPEG4Simple:
			case VAProfileMPEG4AdvancedSimple:
			case VAProfileMPEG4Main:
printf("%s: MPEG4 profile type %d among %d/%d/%d\n", __func__, obj_buffer->type, VASliceDataBufferType, VAPictureParameterBufferType, VASliceParameterBufferType);
				if(obj_buffer->type == VASliceDataBufferType)
					vaStatus = sunxi_cedrus_render_mpeg4_slice_data(ctx, obj_context, obj_surface, obj_buffer);
				else if(obj_buffer->type == VAPictureParameterBufferType)
					vaStatus = sunxi_cedrus_render_mpeg4_picture_parameter(ctx, obj_context, obj_surface, obj_buffer);
				else if(obj_buffer->type == VASliceParameterBufferType)
					vaStatus = sunxi_cedrus_render_mpeg4_slice_parameter(ctx, obj_context, obj_surface, obj_buffer);
				else
printf("%s: MPEG4 profile else\n", __func__);
				break;
			default:
				break;
		}
	}

	return vaStatus;
}

VAStatus sunxi_cedrus_EndPicture(VADriverContextP ctx, VAContextID context)
{
	INIT_DRIVER_DATA
	VAStatus vaStatus = VA_STATUS_SUCCESS;
	object_context_p obj_context;
	object_surface_p obj_surface;
	struct v4l2_buffer out_buf, cap_buf;
	struct v4l2_plane plane[1];
	struct v4l2_plane planes[2];
	struct v4l2_ext_control ctrl;
	struct v4l2_ext_controls ctrls;
	struct media_request_alloc request_alloc;
	struct v4l2_ctrl_mpeg2_frame_hdr *frame_header;
	object_config_p obj_config;
int rc;

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

	obj_context = CONTEXT(context);
	assert(obj_context);

	obj_surface = SURFACE(obj_context->current_render_target);
	assert(obj_surface);

	obj_config = CONFIG(obj_context->config_id);
	if (NULL == obj_config)
	{
		vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
		return vaStatus;
	}

	/*
	 * The real rendering is done in EndPicture instead of RenderPicture
	 * because the v4l2 driver expects to have the full corresponding
	 * extended control when a buffer is queued and we don't know in which
	 * order the different RenderPicture will be called.
	 */


	if (obj_surface->request_fd < 0) {
		assert(ioctl(driver_data->media_fd, MEDIA_IOC_REQUEST_ALLOC, &request_alloc)==0);
		obj_surface->request_fd = request_alloc.fd;
	} else {
		assert(ioctl(obj_surface->request_fd, MEDIA_REQUEST_IOC_REINIT, NULL)==0);
	}

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

	frame_header = obj_surface->header_data;

	memset(plane, 0, sizeof(struct v4l2_plane));
	memset(planes, 0, 2 * sizeof(struct v4l2_plane));
	memset(&ctrl, 0, sizeof(struct v4l2_ext_control));
	memset(&ctrls, 0, sizeof(struct v4l2_ext_controls));

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

	switch(obj_config->profile) {
		case VAProfileMPEG2Simple:
		case VAProfileMPEG2Main:
printf("%s: MPEG2 profile\n", __func__);
//	obj_context->mpeg2_frame_hdr.slice_pos = 0;
	frame_header.slice_len = obj_surface->slice_offset * 8;

			out_buf.m.planes[0].bytesused = obj_surface->slice_offset;
printf("%s: ending on %d bytes used\n", __func__, out_buf.m.planes[0].bytesused);
			ctrl.id = V4L2_CID_MPEG_VIDEO_MPEG2_FRAME_HDR;
			ctrl.ptr = &obj_context->mpeg2_frame_hdr;
			ctrl.size = sizeof(obj_context->mpeg2_frame_hdr);
			break;
		case VAProfileMPEG4Simple:
		case VAProfileMPEG4AdvancedSimple:
		case VAProfileMPEG4Main:
printf("%s: MPEG4 profile\n", __func__);
			out_buf.m.planes[0].bytesused = obj_context->mpeg4_frame_hdr.slice_len/8;
			ctrl.id = V4L2_CID_MPEG_VIDEO_MPEG4_FRAME_HDR;
			ctrl.ptr = &obj_context->mpeg4_frame_hdr;
			ctrl.size = sizeof(obj_context->mpeg4_frame_hdr);
			break;
		default:
printf("%s: MPEG2 dummy/fallback non-profile\n", __func__);
			out_buf.m.planes[0].bytesused = 0;
			ctrl.id = V4L2_CID_MPEG_VIDEO_MPEG2_FRAME_HDR;
			ctrl.ptr = NULL;
			ctrl.size = 0;
			break;
	}

	driver_data->slice_offset[obj_surface->index] = 0;

	memset(&(cap_buf), 0, sizeof(cap_buf));
	cap_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	cap_buf.memory = V4L2_MEMORY_MMAP;
	cap_buf.index = obj_surface->index;
	cap_buf.length = 2;
	cap_buf.m.planes = planes;
	// FIXME: actual frame size here

	ctrls.controls = &ctrl;
	ctrls.count = 1;
	ctrls.which = V4L2_CTRL_WHICH_REQUEST;
	ctrls.request_fd = obj_surface->request_fd;

	rc = ioctl(driver_data->mem2mem_fd, VIDIOC_S_EXT_CTRLS, &ctrls);
	if (rc) {
		printf("ioctl VIDIOC_S_EXT_CTRLS failed with %d/%d/%s\n", rc, errno, strerror(errno));
		assert(0);
	}

	out_buf.flags = V4L2_BUF_FLAG_REQUEST_FD;
	out_buf.request_fd = obj_surface->request_fd;

	cap_buf.flags = V4L2_BUF_FLAG_REQUEST_FD;
	cap_buf.request_fd = obj_surface->request_fd;

//	assert(ioctl(driver_data->mem2mem_fd, VIDIOC_PREPARE_BUF, &out_buf) == 0);
//	assert(ioctl(driver_data->mem2mem_fd, VIDIOC_PREPARE_BUF, &cap_buf) == 0);

	if(ioctl(driver_data->mem2mem_fd, VIDIOC_QBUF, &cap_buf)) {
		obj_surface->status = VASurfaceSkipped;
		sunxi_cedrus_msg("Error when queuing output: %s\n", strerror(errno));

		return VA_STATUS_ERROR_UNKNOWN;
	}

	if(ioctl(driver_data->mem2mem_fd, VIDIOC_QBUF, &out_buf)) {
		obj_surface->status = VASurfaceSkipped;
		sunxi_cedrus_msg("Error when queuing input: %s\n", strerror(errno));
//		ioctl(driver_data->mem2mem_fd, VIDIOC_DQBUF, &cap_buf);
		return VA_STATUS_ERROR_UNKNOWN;
	}



//	sunxi_cedrus_SyncSurface(ctx, obj_context->current_render_target);

	/* For now, assume that we are done with rendering right away */
	obj_context->current_render_target = -1;

	return vaStatus;
}