summaryrefslogtreecommitdiff
path: root/src/xvmc/surface.c
blob: 7c5f45bd346087e4f91cf53a6751c23f70de8edd (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
#include <assert.h>
#include <X11/Xlib.h>
#include <X11/extensions/XvMC.h>
#include <vl_display.h>
#include <vl_screen.h>
#include <vl_context.h>
#include <vl_surface.h>
#include <vl_types.h>

static enum vlMacroBlockType TypeToVL(int xvmc_mb_type)
{
	if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
		return vlMacroBlockTypeIntra;
	if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
		return vlMacroBlockTypeFwdPredicted;
	if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
		return vlMacroBlockTypeBkwdPredicted;
	if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
		return vlMacroBlockTypeBiPredicted;

	assert(0);

	return -1;
}

static enum vlPictureType PictureToVL(int xvmc_pic)
{
	switch (xvmc_pic)
	{
		case XVMC_TOP_FIELD:
			return vlPictureTypeTopField;
		case XVMC_BOTTOM_FIELD:
			return vlPictureTypeBottomField;
		case XVMC_FRAME_PICTURE:
			return vlPictureTypeFrame;
		default:
			assert(0);
	}

	return -1;
}

static enum vlMotionType MotionToVL(int xvmc_motion_type, int xvmc_dct_type)
{
	switch (xvmc_motion_type)
	{
		case XVMC_PREDICTION_FRAME:
			return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ? vlMotionType16x8 : vlMotionTypeFrame;
		case XVMC_PREDICTION_FIELD:
			return vlMotionTypeField;
		case XVMC_PREDICTION_DUAL_PRIME:
			return vlMotionTypeDualPrime;
		default:
			assert(0);
	}

	return -1;
}

Status XvMCCreateSurface(Display *display, XvMCContext *context, XvMCSurface *surface)
{
	struct vlContext *vl_ctx;
	struct vlSurface *vl_sfc;

	assert(display);

	if (!context)
		return XvMCBadContext;
	if (!surface)
		return XvMCBadSurface;

	vl_ctx = context->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlContextGetScreen(vl_ctx))));

	if (vlCreateSurface(vlContextGetScreen(vl_ctx),
	                    context->width, context->height,
	                    vlGetPictureFormat(vl_ctx),
	                    &vl_sfc))
	{
		return BadAlloc;
	}

	vlBindToContext(vl_sfc, vl_ctx);

	surface->surface_id = XAllocID(display);
	surface->context_id = context->context_id;
	surface->surface_type_id = context->surface_type_id;
	surface->width = context->width;
	surface->height = context->height;
	surface->privData = vl_sfc;

	return Success;
}

Status XvMCRenderSurface
(
	Display *display,
	XvMCContext *context,
	unsigned int picture_structure,
	XvMCSurface *target_surface,
	XvMCSurface *past_surface,
	XvMCSurface *future_surface,
	unsigned int flags,
	unsigned int num_macroblocks,
	unsigned int first_macroblock,
	XvMCMacroBlockArray *macroblocks,
	XvMCBlockArray *blocks
)
{
	struct vlContext		*vl_ctx;
	struct vlSurface		*target_vl_surface;
	struct vlSurface		*past_vl_surface;
	struct vlSurface		*future_vl_surface;
	struct vlMpeg2MacroBlockBatch	batch;
	struct vlMpeg2MacroBlock	vl_macroblocks[num_macroblocks];
	unsigned int			i;

	assert(display);

	if (!context)
		return XvMCBadContext;
	if (!target_surface)
		return XvMCBadSurface;

	if
	(
		picture_structure != XVMC_TOP_FIELD &&
		picture_structure != XVMC_BOTTOM_FIELD &&
		picture_structure != XVMC_FRAME_PICTURE
	)
		return BadValue;
	if (future_surface && !past_surface)
		return BadMatch;

	vl_ctx = context->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlContextGetScreen(vl_ctx))));

	target_vl_surface = target_surface->privData;
	past_vl_surface = past_surface ? past_surface->privData : NULL;
	future_vl_surface = future_surface ? future_surface->privData : NULL;

	assert(context->context_id == target_surface->context_id);
	assert(!past_surface || context->context_id == past_surface->context_id);
	assert(!future_surface || context->context_id == future_surface->context_id);

	assert(macroblocks);
	assert(blocks);

	assert(macroblocks->context_id == context->context_id);
	assert(blocks->context_id == context->context_id);

	assert(flags == 0 || flags == XVMC_SECOND_FIELD);

	batch.past_surface = past_vl_surface;
	batch.future_surface = future_vl_surface;
	batch.picture_type = PictureToVL(picture_structure);
	batch.field_order = flags & XVMC_SECOND_FIELD ? vlFieldOrderSecond : vlFieldOrderFirst;
	batch.num_macroblocks = num_macroblocks;
	batch.macroblocks = vl_macroblocks;

	for (i = 0; i < num_macroblocks; ++i)
	{
		unsigned int j = first_macroblock + i;

		unsigned int k, l, m;

		batch.macroblocks[i].mbx = macroblocks->macro_blocks[j].x;
		batch.macroblocks[i].mby = macroblocks->macro_blocks[j].y;
		batch.macroblocks[i].mb_type = TypeToVL(macroblocks->macro_blocks[j].macroblock_type);
		if (batch.macroblocks[i].mb_type != vlMacroBlockTypeIntra)
			batch.macroblocks[i].mo_type = MotionToVL(macroblocks->macro_blocks[j].motion_type, macroblocks->macro_blocks[j].dct_type);
		batch.macroblocks[i].dct_type = macroblocks->macro_blocks[j].dct_type == XVMC_DCT_TYPE_FIELD ? vlDCTTypeFieldCoded : vlDCTTypeFrameCoded;

		for (k = 0; k < 2; ++k)
			for (l = 0; l < 2; ++l)
				for (m = 0; m < 2; ++m)
					batch.macroblocks[i].PMV[k][l][m] = macroblocks->macro_blocks[j].PMV[k][l][m];

		batch.macroblocks[i].cbp = macroblocks->macro_blocks[j].coded_block_pattern;
		batch.macroblocks[i].blocks = blocks->blocks + (macroblocks->macro_blocks[j].index * 64);
	}

	vlRenderMacroBlocksMpeg2(&batch, target_vl_surface);

	return Success;
}

Status XvMCFlushSurface(Display *display, XvMCSurface *surface)
{
	struct vlSurface *vl_sfc;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	vl_sfc = surface->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlSurfaceGetScreen(vl_sfc))));

	vlSurfaceFlush(vl_sfc);

	return Success;
}

Status XvMCSyncSurface(Display *display, XvMCSurface *surface)
{
	struct vlSurface *vl_sfc;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	vl_sfc = surface->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlSurfaceGetScreen(vl_sfc))));

	vlSurfaceSync(vl_sfc);

	return Success;
}

Status XvMCPutSurface
(
	Display *display,
	XvMCSurface *surface,
	Drawable drawable,
	short srcx,
	short srcy,
	unsigned short srcw,
	unsigned short srch,
	short destx,
	short desty,
	unsigned short destw,
	unsigned short desth,
	int flags
)
{
	Window			root;
	int			x, y;
	unsigned int		width, height;
	unsigned int		border_width;
	unsigned int		depth;
	struct vlSurface	*vl_sfc;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	if (XGetGeometry(display, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
		return BadDrawable;

	assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);

	/* TODO: Correct for negative srcx,srcy & destx,desty by clipping */

	assert(srcx + srcw - 1 < surface->width);
	assert(srcy + srch - 1 < surface->height);
	/* XXX: Some apps (mplayer) hit these asserts because they call
	 * this function after the window has been resized by the WM
	 * but before they've handled the corresponding XEvent and
	 * know about the new dimensions. The output will be clipped
	 * for a few frames until the app updates destw and desth.
	 */
	/*assert(destx + destw - 1 < width);
	assert(desty + desth - 1 < height);*/

	vl_sfc = surface->privData;

	vlPutPicture(vl_sfc, drawable, srcx, srcy, srcw, srch, destx, desty, destw, desth, width, height, PictureToVL(flags));

	return Success;
}

Status XvMCGetSurfaceStatus(Display *display, XvMCSurface *surface, int *status)
{
	struct vlSurface	*vl_sfc;
	enum vlResourceStatus	res_status;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	assert(status);

	vl_sfc = surface->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlSurfaceGetScreen(vl_sfc))));

	vlSurfaceGetStatus(vl_sfc, &res_status);

	switch (res_status)
	{
		case vlResourceStatusFree:
		{
			*status = 0;
			break;
		}
		case vlResourceStatusRendering:
		{
			*status = XVMC_RENDERING;
			break;
		}
		case vlResourceStatusDisplaying:
		{
			*status = XVMC_DISPLAYING;
			break;
		}
		default:
			assert(0);
	}

	return Success;
}

Status XvMCDestroySurface(Display *display, XvMCSurface *surface)
{
	struct vlSurface *vl_sfc;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	vl_sfc = surface->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlSurfaceGetScreen(vl_sfc))));

	vlDestroySurface(vl_sfc);

	return Success;
}

Status XvMCHideSurface(Display *display, XvMCSurface *surface)
{
	struct vlSurface *vl_sfc;

	assert(display);

	if (!surface)
		return XvMCBadSurface;

	vl_sfc = surface->privData;

	assert(display == vlGetNativeDisplay(vlGetDisplay(vlSurfaceGetScreen(vl_sfc))));

	/* No op, only for overlaid rendering */

	return Success;
}