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 <vl_winsys.h>
31 #include <pipe/p_video_context.h>
32 #include <pipe/p_video_state.h>
33 #include <pipe/p_state.h>
34 #include <util/u_inlines.h>
35 #include <util/u_memory.h>
36 #include "xvmc_private.h"
38 static enum pipe_mpeg12_macroblock_type
TypeToPipe(int xvmc_mb_type
)
40 if (xvmc_mb_type
& XVMC_MB_TYPE_INTRA
)
41 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
;
42 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_FORWARD
)
43 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD
;
44 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_BACKWARD
)
45 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
;
46 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
))
47 return PIPE_MPEG12_MACROBLOCK_TYPE_BI
;
51 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized mb type 0x%08X.\n", xvmc_mb_type
);
56 static enum pipe_mpeg12_picture_type
PictureToPipe(int xvmc_pic
)
60 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP
;
61 case XVMC_BOTTOM_FIELD
:
62 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM
;
63 case XVMC_FRAME_PICTURE
:
64 return PIPE_MPEG12_PICTURE_TYPE_FRAME
;
69 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic
);
74 static enum pipe_mpeg12_motion_type
MotionToPipe(int xvmc_motion_type
, int xvmc_dct_type
)
76 switch (xvmc_motion_type
) {
77 case XVMC_PREDICTION_FRAME
:
78 if (xvmc_dct_type
== XVMC_DCT_TYPE_FIELD
)
79 return PIPE_MPEG12_MOTION_TYPE_16x8
;
80 else if (xvmc_dct_type
== XVMC_DCT_TYPE_FRAME
)
81 return PIPE_MPEG12_MOTION_TYPE_FRAME
;
83 case XVMC_PREDICTION_FIELD
:
84 return PIPE_MPEG12_MOTION_TYPE_FIELD
;
85 case XVMC_PREDICTION_DUAL_PRIME
:
86 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME
;
91 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized motion type 0x%08X (with DCT type 0x%08X).\n", xvmc_motion_type
, xvmc_dct_type
);
97 CreateOrResizeBackBuffer(struct vl_context
*vctx
, unsigned int width
, unsigned int height
,
98 struct pipe_surface
**backbuffer
)
100 struct pipe_video_context
*vpipe
;
101 struct pipe_texture
template;
102 struct pipe_texture
*tex
;
109 if ((*backbuffer
)->width
!= width
|| (*backbuffer
)->height
!= height
)
110 pipe_surface_reference(backbuffer
, NULL
);
115 memset(&template, 0, sizeof(struct pipe_texture
));
116 template.target
= PIPE_TEXTURE_2D
;
117 template.format
= vctx
->vscreen
->format
;
118 template.last_level
= 0;
119 template.width0
= width
;
120 template.height0
= height
;
122 template.tex_usage
= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
124 tex
= vpipe
->screen
->texture_create(vpipe
->screen
, &template);
128 *backbuffer
= vpipe
->screen
->get_tex_surface(vpipe
->screen
, tex
, 0, 0, 0,
129 PIPE_BUFFER_USAGE_GPU_READ
|
130 PIPE_BUFFER_USAGE_GPU_WRITE
);
131 pipe_texture_reference(&tex
, NULL
);
136 /* Clear the backbuffer in case the video doesn't cover the whole window */
137 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
138 vpipe
->surface_fill(vpipe
, *backbuffer
, 0, 0, width
, height
, 0);
144 MacroBlocksToPipe(struct pipe_screen
*screen
,
145 const XvMCMacroBlockArray
*xvmc_macroblocks
,
146 const XvMCBlockArray
*xvmc_blocks
,
147 unsigned int first_macroblock
,
148 unsigned int num_macroblocks
,
149 struct pipe_mpeg12_macroblock
*pipe_macroblocks
)
151 unsigned int i
, j
, k
, l
;
152 XvMCMacroBlock
*xvmc_mb
;
154 assert(xvmc_macroblocks
);
156 assert(pipe_macroblocks
);
157 assert(num_macroblocks
);
159 xvmc_mb
= xvmc_macroblocks
->macro_blocks
+ first_macroblock
;
161 for (i
= 0; i
< num_macroblocks
; ++i
) {
162 pipe_macroblocks
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
163 pipe_macroblocks
->mbx
= xvmc_mb
->x
;
164 pipe_macroblocks
->mby
= xvmc_mb
->y
;
165 pipe_macroblocks
->mb_type
= TypeToPipe(xvmc_mb
->macroblock_type
);
166 if (pipe_macroblocks
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
)
167 pipe_macroblocks
->mo_type
= MotionToPipe(xvmc_mb
->motion_type
, xvmc_mb
->dct_type
);
168 /* Get rid of Valgrind 'undefined' warnings */
170 pipe_macroblocks
->mo_type
= -1;
171 pipe_macroblocks
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
172 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
174 for (j
= 0; j
< 2; ++j
)
175 for (k
= 0; k
< 2; ++k
)
176 for (l
= 0; l
< 2; ++l
)
177 pipe_macroblocks
->pmv
[j
][k
][l
] = xvmc_mb
->PMV
[j
][k
][l
];
179 pipe_macroblocks
->cbp
= xvmc_mb
->coded_block_pattern
;
180 pipe_macroblocks
->blocks
= pipe_user_buffer_create(screen
, xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
,
188 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
190 XvMCContextPrivate
*context_priv
;
191 struct pipe_video_context
*vpipe
;
192 XvMCSurfacePrivate
*surface_priv
;
193 struct pipe_video_surface
*vsfc
;
195 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating surface %p.\n", surface
);
200 return XvMCBadContext
;
202 return XvMCBadSurface
;
204 context_priv
= context
->privData
;
205 vpipe
= context_priv
->vctx
->vpipe
;
207 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
211 assert(vpipe
->screen
->video_surface_create
);
212 vsfc
= vpipe
->screen
->video_surface_create(vpipe
->screen
, vpipe
->chroma_format
,
213 vpipe
->width
, vpipe
->height
);
219 surface_priv
->pipe_vsfc
= vsfc
;
220 surface_priv
->context
= context
;
222 surface
->surface_id
= XAllocID(dpy
);
223 surface
->context_id
= context
->context_id
;
224 surface
->surface_type_id
= context
->surface_type_id
;
225 surface
->width
= context
->width
;
226 surface
->height
= context
->height
;
227 surface
->privData
= surface_priv
;
231 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p created.\n", surface
);
236 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
237 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
238 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
239 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
242 struct pipe_video_context
*vpipe
;
243 struct pipe_surface
*t_vsfc
;
244 struct pipe_surface
*p_vsfc
;
245 struct pipe_surface
*f_vsfc
;
246 XvMCContextPrivate
*context_priv
;
247 XvMCSurfacePrivate
*target_surface_priv
;
248 XvMCSurfacePrivate
*past_surface_priv
;
249 XvMCSurfacePrivate
*future_surface_priv
;
250 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
253 XVMC_MSG(XVMC_TRACE
, "[XvMC] Rendering to surface %p.\n", target_surface
);
257 if (!context
|| !context
->privData
)
258 return XvMCBadContext
;
259 if (!target_surface
|| !target_surface
->privData
)
260 return XvMCBadSurface
;
262 if (picture_structure
!= XVMC_TOP_FIELD
&&
263 picture_structure
!= XVMC_BOTTOM_FIELD
&&
264 picture_structure
!= XVMC_FRAME_PICTURE
)
266 /* Bkwd pred equivalent to fwd (past && !future) */
267 if (future_surface
&& !past_surface
)
270 assert(context
->context_id
== target_surface
->context_id
);
271 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
272 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
277 assert(macroblocks
->context_id
== context
->context_id
);
278 assert(blocks
->context_id
== context
->context_id
);
280 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
282 target_surface_priv
= target_surface
->privData
;
283 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
284 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
286 assert(target_surface_priv
->context
== context
);
287 assert(!past_surface
|| past_surface_priv
->context
== context
);
288 assert(!future_surface
|| future_surface_priv
->context
== context
);
290 context_priv
= context
->privData
;
291 vpipe
= context_priv
->vctx
->vpipe
;
293 t_vsfc
= target_surface_priv
->pipe_vsfc
;
294 p_vsfc
= past_surface
? past_surface_priv
->pipe_vsfc
: NULL
;
295 f_vsfc
= future_surface
? future_surface_priv
->pipe_vsfc
: NULL
;
297 MacroBlocksToPipe(vpipe
->screen
, macroblocks
, blocks
, first_macroblock
,
298 num_macroblocks
, pipe_macroblocks
);
300 vpipe
->set_decode_target(vpipe
, t_vsfc
);
301 vpipe
->decode_macroblocks(vpipe
, p_vsfc
, f_vsfc
, num_macroblocks
,
302 &pipe_macroblocks
->base
, target_surface_priv
->render_fence
);
304 for (i
= 0; i
< num_macroblocks
; ++i
)
305 vpipe
->screen
->buffer_destroy(pipe_macroblocks
[i
].blocks
);
307 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for rendering.\n", target_surface
);
312 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
317 return XvMCBadSurface
;
322 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
327 return XvMCBadSurface
;
332 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
333 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
334 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
339 unsigned int width
, height
;
340 unsigned int border_width
;
342 struct pipe_video_context
*vpipe
;
343 XvMCSurfacePrivate
*surface_priv
;
344 XvMCContextPrivate
*context_priv
;
345 XvMCSubpicturePrivate
*subpicture_priv
;
346 XvMCContext
*context
;
347 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
348 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
350 XVMC_MSG(XVMC_TRACE
, "[XvMC] Displaying surface %p.\n", surface
);
354 if (!surface
|| !surface
->privData
)
355 return XvMCBadSurface
;
357 if (XGetGeometry(dpy
, drawable
, &root
, &x
, &y
, &width
, &height
, &border_width
, &depth
) == BadDrawable
)
360 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
361 assert(srcx
+ srcw
- 1 < surface
->width
);
362 assert(srcy
+ srch
- 1 < surface
->height
);
364 * Some apps (mplayer) hit these asserts because they call
365 * this function after the window has been resized by the WM
366 * but before they've handled the corresponding XEvent and
367 * know about the new dimensions. The output should be clipped
368 * until the app updates destw and desth.
371 assert(destx + destw - 1 < width);
372 assert(desty + desth - 1 < height);
375 surface_priv
= surface
->privData
;
376 context
= surface_priv
->context
;
377 context_priv
= context
->privData
;
378 subpicture_priv
= surface_priv
->subpicture
? surface_priv
->subpicture
->privData
: NULL
;
379 vpipe
= context_priv
->vctx
->vpipe
;
381 if (!CreateOrResizeBackBuffer(context_priv
->vctx
, width
, height
, &context_priv
->backbuffer
))
384 if (subpicture_priv
) {
385 struct pipe_video_rect src_rect
= {surface_priv
->subx
, surface_priv
->suby
, surface_priv
->subw
, surface_priv
->subh
};
386 struct pipe_video_rect dst_rect
= {surface_priv
->surfx
, surface_priv
->surfy
, surface_priv
->surfw
, surface_priv
->surfh
};
387 struct pipe_video_rect
*src_rects
[1] = {&src_rect
};
388 struct pipe_video_rect
*dst_rects
[1] = {&dst_rect
};
390 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p has subpicture %p.\n", surface
, surface_priv
->subpicture
);
392 assert(subpicture_priv
->surface
== surface
);
393 vpipe
->set_picture_layers(vpipe
, &subpicture_priv
->sfc
->texture
, &src_rects
, &dst_rects
, 1);
395 surface_priv
->subpicture
= NULL
;
396 subpicture_priv
->surface
= NULL
;
399 vpipe
->set_picture_layers(vpipe
, NULL
, NULL
, NULL
, 0);
401 vpipe
->render_picture(vpipe
, surface_priv
->pipe_vsfc
, PictureToPipe(flags
), &src_rect
,
402 context_priv
->backbuffer
, &dst_rect
, surface_priv
->disp_fence
);
404 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface
);
406 vl_video_bind_drawable(context_priv
->vctx
, drawable
);
408 vpipe
->screen
->flush_frontbuffer
411 context_priv
->backbuffer
,
415 XVMC_MSG(XVMC_TRACE
, "[XvMC] Pushed surface %p to front buffer.\n", surface
);
420 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
425 return XvMCBadSurface
;
434 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
436 XvMCSurfacePrivate
*surface_priv
;
438 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying surface %p.\n", surface
);
442 if (!surface
|| !surface
->privData
)
443 return XvMCBadSurface
;
445 surface_priv
= surface
->privData
;
446 pipe_video_surface_reference(&surface_priv
->pipe_vsfc
, NULL
);
448 surface
->privData
= NULL
;
450 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p destroyed.\n", surface
);
455 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
459 if (!surface
|| !surface
->privData
)
460 return XvMCBadSurface
;
462 /* No op, only for overlaid rendering */