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_X8R8G8B8_UNORM
;
105 template.last_level
= 0;
106 template.width
[0] = width
;
107 template.height
[0] = height
;
108 template.depth
[0] = 1;
109 pf_get_block(template.format
, &template.block
);
110 template.tex_usage
= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
112 tex
= vpipe
->screen
->texture_create(vpipe
->screen
, &template);
116 *backbuffer
= vpipe
->screen
->get_tex_surface(vpipe
->screen
, tex
, 0, 0, 0,
117 PIPE_BUFFER_USAGE_GPU_READ
|
118 PIPE_BUFFER_USAGE_GPU_WRITE
);
119 pipe_texture_reference(&tex
, NULL
);
124 /* Clear the backbuffer in case the video doesn't cover the whole window */
125 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
126 vpipe
->clear_surface(vpipe
, 0, 0, width
, height
, 0, *backbuffer
);
132 MacroBlocksToPipe(const XvMCMacroBlockArray
*xvmc_macroblocks
,
133 const XvMCBlockArray
*xvmc_blocks
,
134 unsigned int first_macroblock
,
135 unsigned int num_macroblocks
,
136 struct pipe_mpeg12_macroblock
*pipe_macroblocks
)
138 unsigned int i
, j
, k
, l
;
139 XvMCMacroBlock
*xvmc_mb
;
141 assert(xvmc_macroblocks
);
143 assert(pipe_macroblocks
);
144 assert(num_macroblocks
);
146 xvmc_mb
= xvmc_macroblocks
->macro_blocks
+ first_macroblock
;
148 for (i
= 0; i
< num_macroblocks
; ++i
) {
149 pipe_macroblocks
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
150 pipe_macroblocks
->mbx
= xvmc_mb
->x
;
151 pipe_macroblocks
->mby
= xvmc_mb
->y
;
152 pipe_macroblocks
->mb_type
= TypeToPipe(xvmc_mb
->macroblock_type
);
153 if (pipe_macroblocks
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
)
154 pipe_macroblocks
->mo_type
= MotionToPipe(xvmc_mb
->motion_type
, xvmc_mb
->dct_type
);
155 /* Get rid of Valgrind 'undefined' warnings */
157 pipe_macroblocks
->mo_type
= -1;
158 pipe_macroblocks
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
159 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
161 for (j
= 0; j
< 2; ++j
)
162 for (k
= 0; k
< 2; ++k
)
163 for (l
= 0; l
< 2; ++l
)
164 pipe_macroblocks
->pmv
[j
][k
][l
] = xvmc_mb
->PMV
[j
][k
][l
];
166 pipe_macroblocks
->cbp
= xvmc_mb
->coded_block_pattern
;
167 pipe_macroblocks
->blocks
= xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
;
174 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
176 XvMCContextPrivate
*context_priv
;
177 struct pipe_video_context
*vpipe
;
178 XvMCSurfacePrivate
*surface_priv
;
179 struct pipe_video_surface
*vsfc
;
184 return XvMCBadContext
;
186 return XvMCBadSurface
;
188 context_priv
= context
->privData
;
189 vpipe
= context_priv
->vpipe
;
191 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
195 vsfc
= vpipe
->screen
->video_surface_create(vpipe
->screen
, vpipe
->chroma_format
,
196 vpipe
->width
, vpipe
->height
);
202 surface_priv
->pipe_vsfc
= vsfc
;
203 surface_priv
->context
= context
;
205 surface
->surface_id
= XAllocID(dpy
);
206 surface
->context_id
= context
->context_id
;
207 surface
->surface_type_id
= context
->surface_type_id
;
208 surface
->width
= context
->width
;
209 surface
->height
= context
->height
;
210 surface
->privData
= surface_priv
;
217 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
218 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
219 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
220 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
223 struct pipe_video_context
*vpipe
;
224 struct pipe_surface
*t_vsfc
;
225 struct pipe_surface
*p_vsfc
;
226 struct pipe_surface
*f_vsfc
;
227 XvMCContextPrivate
*context_priv
;
228 XvMCSurfacePrivate
*target_surface_priv
;
229 XvMCSurfacePrivate
*past_surface_priv
;
230 XvMCSurfacePrivate
*future_surface_priv
;
231 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
235 if (!context
|| !context
->privData
)
236 return XvMCBadContext
;
237 if (!target_surface
|| !target_surface
->privData
)
238 return XvMCBadSurface
;
240 if (picture_structure
!= XVMC_TOP_FIELD
&&
241 picture_structure
!= XVMC_BOTTOM_FIELD
&&
242 picture_structure
!= XVMC_FRAME_PICTURE
)
244 /* Bkwd pred equivalent to fwd (past && !future) */
245 if (future_surface
&& !past_surface
)
248 assert(context
->context_id
== target_surface
->context_id
);
249 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
250 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
255 assert(macroblocks
->context_id
== context
->context_id
);
256 assert(blocks
->context_id
== context
->context_id
);
258 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
260 target_surface_priv
= target_surface
->privData
;
261 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
262 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
264 assert(target_surface_priv
->context
== context
);
265 assert(!past_surface
|| past_surface_priv
->context
== context
);
266 assert(!future_surface
|| future_surface_priv
->context
== context
);
268 context_priv
= context
->privData
;
269 vpipe
= context_priv
->vpipe
;
271 t_vsfc
= target_surface_priv
->pipe_vsfc
;
272 p_vsfc
= past_surface
? past_surface_priv
->pipe_vsfc
: NULL
;
273 f_vsfc
= future_surface
? future_surface_priv
->pipe_vsfc
: NULL
;
275 MacroBlocksToPipe(macroblocks
, blocks
, first_macroblock
,
276 num_macroblocks
, pipe_macroblocks
);
278 vpipe
->set_decode_target(vpipe
, t_vsfc
);
279 vpipe
->decode_macroblocks(vpipe
, p_vsfc
, f_vsfc
, num_macroblocks
,
280 &pipe_macroblocks
->base
, target_surface_priv
->render_fence
);
285 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
290 return XvMCBadSurface
;
295 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
300 return XvMCBadSurface
;
305 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
306 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
307 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
312 unsigned int width
, height
;
313 unsigned int border_width
;
315 struct pipe_video_context
*vpipe
;
316 XvMCSurfacePrivate
*surface_priv
;
317 XvMCContextPrivate
*context_priv
;
318 XvMCContext
*context
;
319 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
320 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
324 if (!surface
|| !surface
->privData
)
325 return XvMCBadSurface
;
327 if (XGetGeometry(dpy
, drawable
, &root
, &x
, &y
, &width
, &height
, &border_width
, &depth
) == BadDrawable
)
330 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
331 assert(srcx
+ srcw
- 1 < surface
->width
);
332 assert(srcy
+ srch
- 1 < surface
->height
);
334 * Some apps (mplayer) hit these asserts because they call
335 * this function after the window has been resized by the WM
336 * but before they've handled the corresponding XEvent and
337 * know about the new dimensions. The output should be clipped
338 * until the app updates destw and desth.
341 assert(destx + destw - 1 < width);
342 assert(desty + desth - 1 < height);
345 surface_priv
= surface
->privData
;
346 context
= surface_priv
->context
;
347 context_priv
= context
->privData
;
348 vpipe
= context_priv
->vpipe
;
350 if (!CreateOrResizeBackBuffer(vpipe
, width
, height
, &context_priv
->backbuffer
))
353 vpipe
->render_picture(vpipe
, surface_priv
->pipe_vsfc
, PictureToPipe(flags
), &src_rect
,
354 context_priv
->backbuffer
, &dst_rect
, surface_priv
->disp_fence
);
356 vl_video_bind_drawable(vpipe
, drawable
);
358 vpipe
->screen
->flush_frontbuffer
361 context_priv
->backbuffer
,
368 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
373 return XvMCBadSurface
;
382 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
384 XvMCSurfacePrivate
*surface_priv
;
388 if (!surface
|| !surface
->privData
)
389 return XvMCBadSurface
;
391 surface_priv
= surface
->privData
;
392 pipe_video_surface_reference(&surface_priv
->pipe_vsfc
, NULL
);
394 surface
->privData
= NULL
;
399 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
403 if (!surface
|| !surface
->privData
)
404 return XvMCBadSurface
;
406 /* No op, only for overlaid rendering */