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 <util/u_math.h>
37 #include "xvmc_private.h"
39 static enum pipe_mpeg12_macroblock_type
TypeToPipe(int xvmc_mb_type
)
41 if (xvmc_mb_type
& XVMC_MB_TYPE_INTRA
)
42 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
;
43 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_FORWARD
)
44 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD
;
45 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_BACKWARD
)
46 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
;
47 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
))
48 return PIPE_MPEG12_MACROBLOCK_TYPE_BI
;
52 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized mb type 0x%08X.\n", xvmc_mb_type
);
57 static enum pipe_mpeg12_picture_type
PictureToPipe(int xvmc_pic
)
61 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP
;
62 case XVMC_BOTTOM_FIELD
:
63 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM
;
64 case XVMC_FRAME_PICTURE
:
65 return PIPE_MPEG12_PICTURE_TYPE_FRAME
;
70 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic
);
75 static enum pipe_mpeg12_motion_type
MotionToPipe(int xvmc_motion_type
, int xvmc_dct_type
)
77 switch (xvmc_motion_type
) {
78 case XVMC_PREDICTION_FRAME
:
79 if (xvmc_dct_type
== XVMC_DCT_TYPE_FIELD
)
80 return PIPE_MPEG12_MOTION_TYPE_16x8
;
81 else if (xvmc_dct_type
== XVMC_DCT_TYPE_FRAME
)
82 return PIPE_MPEG12_MOTION_TYPE_FRAME
;
84 case XVMC_PREDICTION_FIELD
:
85 return PIPE_MPEG12_MOTION_TYPE_FIELD
;
86 case XVMC_PREDICTION_DUAL_PRIME
:
87 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME
;
92 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized motion type 0x%08X (with DCT type 0x%08X).\n", xvmc_motion_type
, xvmc_dct_type
);
99 CreateOrResizeBackBuffer(struct vl_context
*vctx
, unsigned int width
, unsigned int height
,
100 struct pipe_surface
**backbuffer
)
102 struct pipe_video_context
*vpipe
;
103 struct pipe_resource
template;
104 struct pipe_resource
*tex
;
111 if ((*backbuffer
)->width
!= width
|| (*backbuffer
)->height
!= height
)
112 pipe_surface_reference(backbuffer
, NULL
);
117 memset(&template, 0, sizeof(struct pipe_resource
));
118 template.target
= PIPE_TEXTURE_2D
;
119 template.format
= vctx
->vscreen
->format
;
120 template.last_level
= 0;
121 template.width0
= width
;
122 template.height0
= height
;
124 template.usage
= PIPE_USAGE_DEFAULT
;
125 template.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_BLIT_SOURCE
;
128 tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &template);
132 *backbuffer
= vpipe
->screen
->get_tex_surface(vpipe
->screen
, tex
, 0, 0, 0,
134 pipe_resource_reference(&tex
, NULL
);
139 /* Clear the backbuffer in case the video doesn't cover the whole window */
140 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
141 vpipe
->surface_fill(vpipe
, *backbuffer
, 0, 0, width
, height
, 0);
148 MacroBlocksToPipe(struct pipe_screen
*screen
,
149 const XvMCMacroBlockArray
*xvmc_macroblocks
,
150 const XvMCBlockArray
*xvmc_blocks
,
151 unsigned int first_macroblock
,
152 unsigned int num_macroblocks
,
153 struct pipe_mpeg12_macroblock
*pipe_macroblocks
)
155 unsigned int i
, j
, k
, l
;
156 XvMCMacroBlock
*xvmc_mb
;
158 assert(xvmc_macroblocks
);
160 assert(pipe_macroblocks
);
161 assert(num_macroblocks
);
163 xvmc_mb
= xvmc_macroblocks
->macro_blocks
+ first_macroblock
;
165 for (i
= 0; i
< num_macroblocks
; ++i
) {
166 pipe_macroblocks
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
167 pipe_macroblocks
->mbx
= xvmc_mb
->x
;
168 pipe_macroblocks
->mby
= xvmc_mb
->y
;
169 pipe_macroblocks
->mb_type
= TypeToPipe(xvmc_mb
->macroblock_type
);
170 if (pipe_macroblocks
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
)
171 pipe_macroblocks
->mo_type
= MotionToPipe(xvmc_mb
->motion_type
, xvmc_mb
->dct_type
);
172 /* Get rid of Valgrind 'undefined' warnings */
174 pipe_macroblocks
->mo_type
= -1;
175 pipe_macroblocks
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
176 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
178 for (j
= 0; j
< 2; ++j
)
179 for (k
= 0; k
< 2; ++k
)
180 for (l
= 0; l
< 2; ++l
)
181 pipe_macroblocks
->pmv
[j
][k
][l
] = xvmc_mb
->PMV
[j
][k
][l
];
183 pipe_macroblocks
->cbp
= xvmc_mb
->coded_block_pattern
;
184 pipe_macroblocks
->blocks
= xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
;
192 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
194 XvMCContextPrivate
*context_priv
;
195 struct pipe_video_context
*vpipe
;
196 XvMCSurfacePrivate
*surface_priv
;
197 struct pipe_resource
template;
198 struct pipe_resource
*vsfc_tex
;
199 struct pipe_surface
*vsfc
;
201 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating surface %p.\n", surface
);
206 return XvMCBadContext
;
208 return XvMCBadSurface
;
210 context_priv
= context
->privData
;
211 vpipe
= context_priv
->vctx
->vpipe
;
213 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
217 memset(&template, 0, sizeof(struct pipe_resource
));
218 template.target
= PIPE_TEXTURE_2D
;
219 template.format
= (enum pipe_format
)vpipe
->get_param(vpipe
, PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT
);
220 template.last_level
= 0;
221 if (vpipe
->is_format_supported(vpipe
, template.format
,
222 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
,
223 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
)) {
224 template.width0
= context
->width
;
225 template.height0
= context
->height
;
228 assert(vpipe
->is_format_supported(vpipe
, template.format
,
229 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
,
230 PIPE_TEXTURE_GEOM_NON_SQUARE
));
231 template.width0
= util_next_power_of_two(context
->width
);
232 template.height0
= util_next_power_of_two(context
->height
);
235 template.usage
= PIPE_USAGE_DEFAULT
;
236 template.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
238 vsfc_tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &template);
244 vsfc
= vpipe
->screen
->get_tex_surface(vpipe
->screen
, vsfc_tex
, 0, 0, 0,
245 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
);
246 pipe_resource_reference(&vsfc_tex
, NULL
);
252 surface_priv
->pipe_vsfc
= vsfc
;
253 surface_priv
->context
= context
;
255 surface
->surface_id
= XAllocID(dpy
);
256 surface
->context_id
= context
->context_id
;
257 surface
->surface_type_id
= context
->surface_type_id
;
258 surface
->width
= context
->width
;
259 surface
->height
= context
->height
;
260 surface
->privData
= surface_priv
;
264 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p created.\n", surface
);
270 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
271 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
272 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
273 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
276 struct pipe_video_context
*vpipe
;
277 struct pipe_surface
*t_vsfc
;
278 struct pipe_surface
*p_vsfc
;
279 struct pipe_surface
*f_vsfc
;
280 XvMCContextPrivate
*context_priv
;
281 XvMCSurfacePrivate
*target_surface_priv
;
282 XvMCSurfacePrivate
*past_surface_priv
;
283 XvMCSurfacePrivate
*future_surface_priv
;
284 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
286 XVMC_MSG(XVMC_TRACE
, "[XvMC] Rendering to surface %p.\n", target_surface
);
290 if (!context
|| !context
->privData
)
291 return XvMCBadContext
;
292 if (!target_surface
|| !target_surface
->privData
)
293 return XvMCBadSurface
;
295 if (picture_structure
!= XVMC_TOP_FIELD
&&
296 picture_structure
!= XVMC_BOTTOM_FIELD
&&
297 picture_structure
!= XVMC_FRAME_PICTURE
)
299 /* Bkwd pred equivalent to fwd (past && !future) */
300 if (future_surface
&& !past_surface
)
303 assert(context
->context_id
== target_surface
->context_id
);
304 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
305 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
310 assert(macroblocks
->context_id
== context
->context_id
);
311 assert(blocks
->context_id
== context
->context_id
);
313 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
315 target_surface_priv
= target_surface
->privData
;
316 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
317 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
319 assert(target_surface_priv
->context
== context
);
320 assert(!past_surface
|| past_surface_priv
->context
== context
);
321 assert(!future_surface
|| future_surface_priv
->context
== context
);
323 context_priv
= context
->privData
;
324 vpipe
= context_priv
->vctx
->vpipe
;
326 t_vsfc
= target_surface_priv
->pipe_vsfc
;
327 p_vsfc
= past_surface
? past_surface_priv
->pipe_vsfc
: NULL
;
328 f_vsfc
= future_surface
? future_surface_priv
->pipe_vsfc
: NULL
;
330 MacroBlocksToPipe(vpipe
->screen
, macroblocks
, blocks
, first_macroblock
,
331 num_macroblocks
, pipe_macroblocks
);
333 vpipe
->set_decode_target(vpipe
, t_vsfc
);
334 vpipe
->decode_macroblocks(vpipe
, p_vsfc
, f_vsfc
, num_macroblocks
,
335 &pipe_macroblocks
->base
, &target_surface_priv
->render_fence
);
337 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for rendering.\n", target_surface
);
343 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
348 return XvMCBadSurface
;
354 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
359 return XvMCBadSurface
;
365 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
366 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
367 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
370 struct pipe_video_context
*vpipe
;
371 XvMCSurfacePrivate
*surface_priv
;
372 XvMCContextPrivate
*context_priv
;
373 XvMCSubpicturePrivate
*subpicture_priv
;
374 XvMCContext
*context
;
375 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
376 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
377 struct pipe_surface
*drawable_surface
;
379 XVMC_MSG(XVMC_TRACE
, "[XvMC] Displaying surface %p.\n", surface
);
383 if (!surface
|| !surface
->privData
)
384 return XvMCBadSurface
;
386 surface_priv
= surface
->privData
;
387 context
= surface_priv
->context
;
388 context_priv
= context
->privData
;
390 drawable_surface
= vl_drawable_surface_get(context_priv
->vctx
->vscreen
, drawable
);
391 if (!drawable_surface
)
394 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
395 assert(srcx
+ srcw
- 1 < surface
->width
);
396 assert(srcy
+ srch
- 1 < surface
->height
);
398 * Some apps (mplayer) hit these asserts because they call
399 * this function after the window has been resized by the WM
400 * but before they've handled the corresponding XEvent and
401 * know about the new dimensions. The output should be clipped
402 * until the app updates destw and desth.
405 assert(destx + destw - 1 < drawable_surface->width);
406 assert(desty + desth - 1 < drawable_surface->height);
409 subpicture_priv
= surface_priv
->subpicture
? surface_priv
->subpicture
->privData
: NULL
;
410 vpipe
= context_priv
->vctx
->vpipe
;
413 if (!CreateOrResizeBackBuffer(context_priv
->vctx
, width
, height
, &context_priv
->backbuffer
))
417 if (subpicture_priv
) {
418 struct pipe_video_rect src_rect
= {surface_priv
->subx
, surface_priv
->suby
, surface_priv
->subw
, surface_priv
->subh
};
419 struct pipe_video_rect dst_rect
= {surface_priv
->surfx
, surface_priv
->surfy
, surface_priv
->surfw
, surface_priv
->surfh
};
420 struct pipe_video_rect
*src_rects
[1] = {&src_rect
};
421 struct pipe_video_rect
*dst_rects
[1] = {&dst_rect
};
423 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p has subpicture %p.\n", surface
, surface_priv
->subpicture
);
425 assert(subpicture_priv
->surface
== surface
);
426 vpipe
->set_picture_layers(vpipe
, &subpicture_priv
->sfc
, src_rects
, dst_rects
, 1);
428 surface_priv
->subpicture
= NULL
;
429 subpicture_priv
->surface
= NULL
;
432 vpipe
->set_picture_layers(vpipe
, NULL
, NULL
, NULL
, 0);
434 vpipe
->render_picture(vpipe
, surface_priv
->pipe_vsfc
, PictureToPipe(flags
), &src_rect
,
435 drawable_surface
, &dst_rect
, &surface_priv
->disp_fence
);
437 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface
);
439 vpipe
->screen
->flush_frontbuffer
443 vl_contextprivate_get(context_priv
->vctx
, drawable_surface
)
446 pipe_surface_reference(&drawable_surface
, NULL
);
448 XVMC_MSG(XVMC_TRACE
, "[XvMC] Pushed surface %p to front buffer.\n", surface
);
454 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
459 return XvMCBadSurface
;
469 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
471 XvMCSurfacePrivate
*surface_priv
;
473 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying surface %p.\n", surface
);
477 if (!surface
|| !surface
->privData
)
478 return XvMCBadSurface
;
480 surface_priv
= surface
->privData
;
481 pipe_surface_reference(&surface_priv
->pipe_vsfc
, NULL
);
483 surface
->privData
= NULL
;
485 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p destroyed.\n", surface
);
491 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
495 if (!surface
|| !surface
->privData
)
496 return XvMCBadSurface
;
498 /* No op, only for overlaid rendering */