1
/**************************************************************************
3
* Copyright 2009 Younes Manton.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
**************************************************************************/
29
#include <X11/Xlibint.h>
30
#include <pipe/p_video_context.h>
31
#include <pipe/p_video_state.h>
32
#include <pipe/p_state.h>
33
#include <util/u_memory.h>
34
#include "xvmc_private.h"
36
static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
38
if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
39
return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
40
if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
41
return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
42
if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
43
return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
44
if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
45
return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
52
static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
56
return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
57
case XVMC_BOTTOM_FIELD:
58
return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
59
case XVMC_FRAME_PICTURE:
60
return PIPE_MPEG12_PICTURE_TYPE_FRAME;
68
static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
70
switch (xvmc_motion_type) {
71
case XVMC_PREDICTION_FRAME:
72
return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ?
73
PIPE_MPEG12_MOTION_TYPE_16x8 : PIPE_MPEG12_MOTION_TYPE_FRAME;
74
case XVMC_PREDICTION_FIELD:
75
return PIPE_MPEG12_MOTION_TYPE_FIELD;
76
case XVMC_PREDICTION_DUAL_PRIME:
77
return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
86
CreateOrResizeBackBuffer(struct pipe_video_context *vpipe, unsigned int width, unsigned int height,
87
struct pipe_surface **backbuffer)
89
struct pipe_texture template;
90
struct pipe_texture *tex;
95
if ((*backbuffer)->width != width || (*backbuffer)->height != height)
96
pipe_surface_reference(backbuffer, NULL);
101
memset(&template, 0, sizeof(struct pipe_texture));
102
template.target = PIPE_TEXTURE_2D;
103
/* XXX: Needs to match the drawable's format? */
104
template.format = PIPE_FORMAT_B8G8R8X8_UNORM;
105
template.last_level = 0;
106
template.width0 = width;
107
template.height0 = height;
109
template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
111
tex = vpipe->screen->texture_create(vpipe->screen, &template);
115
*backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
116
PIPE_BUFFER_USAGE_GPU_READ |
117
PIPE_BUFFER_USAGE_GPU_WRITE);
118
pipe_texture_reference(&tex, NULL);
123
/* Clear the backbuffer in case the video doesn't cover the whole window */
124
/* FIXME: Need to clear every time a frame moves and leaves dirty rects */
125
vpipe->clear_surface(vpipe, 0, 0, width, height, 0, *backbuffer);
131
MacroBlocksToPipe(const XvMCMacroBlockArray *xvmc_macroblocks,
132
const XvMCBlockArray *xvmc_blocks,
133
unsigned int first_macroblock,
134
unsigned int num_macroblocks,
135
struct pipe_mpeg12_macroblock *pipe_macroblocks)
137
unsigned int i, j, k, l;
138
XvMCMacroBlock *xvmc_mb;
140
assert(xvmc_macroblocks);
142
assert(pipe_macroblocks);
143
assert(num_macroblocks);
145
xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
147
for (i = 0; i < num_macroblocks; ++i) {
148
pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
149
pipe_macroblocks->mbx = xvmc_mb->x;
150
pipe_macroblocks->mby = xvmc_mb->y;
151
pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
152
if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
153
pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
154
/* Get rid of Valgrind 'undefined' warnings */
156
pipe_macroblocks->mo_type = -1;
157
pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
158
PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
160
for (j = 0; j < 2; ++j)
161
for (k = 0; k < 2; ++k)
162
for (l = 0; l < 2; ++l)
163
pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
165
pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
166
pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
173
Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
175
XvMCContextPrivate *context_priv;
176
struct pipe_video_context *vpipe;
177
XvMCSurfacePrivate *surface_priv;
178
struct pipe_video_surface *vsfc;
183
return XvMCBadContext;
185
return XvMCBadSurface;
187
context_priv = context->privData;
188
vpipe = context_priv->vpipe;
190
surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
194
vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
195
vpipe->width, vpipe->height);
201
surface_priv->pipe_vsfc = vsfc;
202
surface_priv->context = context;
204
surface->surface_id = XAllocID(dpy);
205
surface->context_id = context->context_id;
206
surface->surface_type_id = context->surface_type_id;
207
surface->width = context->width;
208
surface->height = context->height;
209
surface->privData = surface_priv;
216
Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
217
XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
218
unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
219
XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
222
struct pipe_video_context *vpipe;
223
struct pipe_surface *t_vsfc;
224
struct pipe_surface *p_vsfc;
225
struct pipe_surface *f_vsfc;
226
XvMCContextPrivate *context_priv;
227
XvMCSurfacePrivate *target_surface_priv;
228
XvMCSurfacePrivate *past_surface_priv;
229
XvMCSurfacePrivate *future_surface_priv;
230
struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
234
if (!context || !context->privData)
235
return XvMCBadContext;
236
if (!target_surface || !target_surface->privData)
237
return XvMCBadSurface;
239
if (picture_structure != XVMC_TOP_FIELD &&
240
picture_structure != XVMC_BOTTOM_FIELD &&
241
picture_structure != XVMC_FRAME_PICTURE)
243
/* Bkwd pred equivalent to fwd (past && !future) */
244
if (future_surface && !past_surface)
247
assert(context->context_id == target_surface->context_id);
248
assert(!past_surface || context->context_id == past_surface->context_id);
249
assert(!future_surface || context->context_id == future_surface->context_id);
254
assert(macroblocks->context_id == context->context_id);
255
assert(blocks->context_id == context->context_id);
257
assert(flags == 0 || flags == XVMC_SECOND_FIELD);
259
target_surface_priv = target_surface->privData;
260
past_surface_priv = past_surface ? past_surface->privData : NULL;
261
future_surface_priv = future_surface ? future_surface->privData : NULL;
263
assert(target_surface_priv->context == context);
264
assert(!past_surface || past_surface_priv->context == context);
265
assert(!future_surface || future_surface_priv->context == context);
267
context_priv = context->privData;
268
vpipe = context_priv->vpipe;
270
t_vsfc = target_surface_priv->pipe_vsfc;
271
p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
272
f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
274
MacroBlocksToPipe(macroblocks, blocks, first_macroblock,
275
num_macroblocks, pipe_macroblocks);
277
vpipe->set_decode_target(vpipe, t_vsfc);
278
vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
279
&pipe_macroblocks->base, target_surface_priv->render_fence);
284
Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
289
return XvMCBadSurface;
294
Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
299
return XvMCBadSurface;
304
Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
305
short srcx, short srcy, unsigned short srcw, unsigned short srch,
306
short destx, short desty, unsigned short destw, unsigned short desth,
311
unsigned int width, height;
312
unsigned int border_width;
314
struct pipe_video_context *vpipe;
315
XvMCSurfacePrivate *surface_priv;
316
XvMCContextPrivate *context_priv;
317
XvMCContext *context;
318
struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
319
struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
323
if (!surface || !surface->privData)
324
return XvMCBadSurface;
326
if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
329
assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
330
assert(srcx + srcw - 1 < surface->width);
331
assert(srcy + srch - 1 < surface->height);
333
* Some apps (mplayer) hit these asserts because they call
334
* this function after the window has been resized by the WM
335
* but before they've handled the corresponding XEvent and
336
* know about the new dimensions. The output should be clipped
337
* until the app updates destw and desth.
340
assert(destx + destw - 1 < width);
341
assert(desty + desth - 1 < height);
344
surface_priv = surface->privData;
345
context = surface_priv->context;
346
context_priv = context->privData;
347
vpipe = context_priv->vpipe;
349
if (!CreateOrResizeBackBuffer(vpipe, width, height, &context_priv->backbuffer))
352
vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
353
context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
355
vl_video_bind_drawable(vpipe, drawable);
357
vpipe->screen->flush_frontbuffer
360
context_priv->backbuffer,
367
Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
372
return XvMCBadSurface;
381
Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
383
XvMCSurfacePrivate *surface_priv;
387
if (!surface || !surface->privData)
388
return XvMCBadSurface;
390
surface_priv = surface->privData;
391
pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
393
surface->privData = NULL;
398
Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
402
if (!surface || !surface->privData)
403
return XvMCBadSurface;
405
/* No op, only for overlaid rendering */