2 #include <X11/Xlibint.h>
3 #include <pipe/p_video_context.h>
4 #include <pipe/p_video_state.h>
5 #include <pipe/p_state.h>
6 #include <util/u_memory.h>
7 #include "xvmc_private.h"
9 static enum pipe_mpeg12_macroblock_type
TypeToPipe(int xvmc_mb_type
)
11 if (xvmc_mb_type
& XVMC_MB_TYPE_INTRA
)
12 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
;
13 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_FORWARD
)
14 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD
;
15 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == XVMC_MB_TYPE_MOTION_BACKWARD
)
16 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
;
17 if ((xvmc_mb_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) == (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
))
18 return PIPE_MPEG12_MACROBLOCK_TYPE_BI
;
25 static enum pipe_mpeg12_picture_type
PictureToPipe(int xvmc_pic
)
29 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP
;
30 case XVMC_BOTTOM_FIELD
:
31 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM
;
32 case XVMC_FRAME_PICTURE
:
33 return PIPE_MPEG12_PICTURE_TYPE_FRAME
;
41 static enum pipe_mpeg12_motion_type
MotionToPipe(int xvmc_motion_type
, int xvmc_dct_type
)
43 switch (xvmc_motion_type
) {
44 case XVMC_PREDICTION_FRAME
:
45 return xvmc_dct_type
== XVMC_DCT_TYPE_FIELD
?
46 PIPE_MPEG12_MOTION_TYPE_16x8
: PIPE_MPEG12_MOTION_TYPE_FRAME
;
47 case XVMC_PREDICTION_FIELD
:
48 return PIPE_MPEG12_MOTION_TYPE_FIELD
;
49 case XVMC_PREDICTION_DUAL_PRIME
:
50 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME
;
59 CreateOrResizeBackBuffer(struct pipe_video_context
*vpipe
, unsigned int width
, unsigned int height
,
60 struct pipe_surface
**backbuffer
)
62 struct pipe_texture
template;
63 struct pipe_texture
*tex
;
68 if ((*backbuffer
)->width
!= width
|| (*backbuffer
)->height
!= height
)
69 pipe_surface_reference(backbuffer
, NULL
);
74 memset(&template, 0, sizeof(struct pipe_texture
));
75 template.target
= PIPE_TEXTURE_2D
;
76 /* XXX: Needs to match the drawable's format? */
77 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
78 template.last_level
= 0;
79 template.width
[0] = width
;
80 template.height
[0] = height
;
81 template.depth
[0] = 1;
82 pf_get_block(template.format
, &template.block
);
83 template.tex_usage
= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
85 tex
= vpipe
->screen
->texture_create(vpipe
->screen
, &template);
89 *backbuffer
= vpipe
->screen
->get_tex_surface(vpipe
->screen
, tex
, 0, 0, 0,
90 PIPE_BUFFER_USAGE_GPU_READ
|
91 PIPE_BUFFER_USAGE_GPU_WRITE
);
92 pipe_texture_reference(&tex
, NULL
);
97 /* Clear the backbuffer in case the video doesn't cover the whole window */
98 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
99 vpipe
->clear_surface(vpipe
, 0, 0, width
, height
, 0, *backbuffer
);
105 MacroBlocksToPipe(const XvMCMacroBlockArray
*xvmc_macroblocks
,
106 const XvMCBlockArray
*xvmc_blocks
,
107 unsigned int first_macroblock
,
108 unsigned int num_macroblocks
,
109 struct pipe_mpeg12_macroblock
*pipe_macroblocks
)
111 unsigned int i
, j
, k
, l
;
112 XvMCMacroBlock
*xvmc_mb
;
114 assert(xvmc_macroblocks
);
116 assert(pipe_macroblocks
);
117 assert(num_macroblocks
);
119 xvmc_mb
= xvmc_macroblocks
->macro_blocks
+ first_macroblock
;
121 for (i
= 0; i
< num_macroblocks
; ++i
) {
122 pipe_macroblocks
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
123 pipe_macroblocks
->mbx
= xvmc_mb
->x
;
124 pipe_macroblocks
->mby
= xvmc_mb
->y
;
125 pipe_macroblocks
->mb_type
= TypeToPipe(xvmc_mb
->macroblock_type
);
126 if (pipe_macroblocks
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
)
127 pipe_macroblocks
->mo_type
= MotionToPipe(xvmc_mb
->motion_type
, xvmc_mb
->dct_type
);
128 /* Get rid of Valgrind 'undefined' warnings */
130 pipe_macroblocks
->mo_type
= -1;
131 pipe_macroblocks
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
132 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
134 for (j
= 0; j
< 2; ++j
)
135 for (k
= 0; k
< 2; ++k
)
136 for (l
= 0; l
< 2; ++l
)
137 pipe_macroblocks
->pmv
[j
][k
][l
] = xvmc_mb
->PMV
[j
][k
][l
];
139 pipe_macroblocks
->cbp
= xvmc_mb
->coded_block_pattern
;
140 pipe_macroblocks
->blocks
= xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
;
147 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
149 XvMCContextPrivate
*context_priv
;
150 struct pipe_video_context
*vpipe
;
151 XvMCSurfacePrivate
*surface_priv
;
152 struct pipe_video_surface
*vsfc
;
157 return XvMCBadContext
;
159 return XvMCBadSurface
;
161 context_priv
= context
->privData
;
162 vpipe
= context_priv
->vpipe
;
164 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
168 vsfc
= vpipe
->screen
->video_surface_create(vpipe
->screen
, vpipe
->chroma_format
,
169 vpipe
->width
, vpipe
->height
);
175 surface_priv
->pipe_vsfc
= vsfc
;
176 surface_priv
->context
= context
;
178 surface
->surface_id
= XAllocID(dpy
);
179 surface
->context_id
= context
->context_id
;
180 surface
->surface_type_id
= context
->surface_type_id
;
181 surface
->width
= context
->width
;
182 surface
->height
= context
->height
;
183 surface
->privData
= surface_priv
;
190 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
191 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
192 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
193 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
196 struct pipe_video_context
*vpipe
;
197 struct pipe_surface
*t_vsfc
;
198 struct pipe_surface
*p_vsfc
;
199 struct pipe_surface
*f_vsfc
;
200 XvMCContextPrivate
*context_priv
;
201 XvMCSurfacePrivate
*target_surface_priv
;
202 XvMCSurfacePrivate
*past_surface_priv
;
203 XvMCSurfacePrivate
*future_surface_priv
;
204 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
208 if (!context
|| !context
->privData
)
209 return XvMCBadContext
;
210 if (!target_surface
|| !target_surface
->privData
)
211 return XvMCBadSurface
;
213 if (picture_structure
!= XVMC_TOP_FIELD
&&
214 picture_structure
!= XVMC_BOTTOM_FIELD
&&
215 picture_structure
!= XVMC_FRAME_PICTURE
)
217 /* Bkwd pred equivalent to fwd (past && !future) */
218 if (future_surface
&& !past_surface
)
221 assert(context
->context_id
== target_surface
->context_id
);
222 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
223 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
228 assert(macroblocks
->context_id
== context
->context_id
);
229 assert(blocks
->context_id
== context
->context_id
);
231 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
233 target_surface_priv
= target_surface
->privData
;
234 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
235 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
237 assert(target_surface_priv
->context
== context
);
238 assert(!past_surface
|| past_surface_priv
->context
== context
);
239 assert(!future_surface
|| future_surface_priv
->context
== context
);
241 context_priv
= context
->privData
;
242 vpipe
= context_priv
->vpipe
;
244 t_vsfc
= target_surface_priv
->pipe_vsfc
;
245 p_vsfc
= past_surface
? past_surface_priv
->pipe_vsfc
: NULL
;
246 f_vsfc
= future_surface
? future_surface_priv
->pipe_vsfc
: NULL
;
248 MacroBlocksToPipe(macroblocks
, blocks
, first_macroblock
,
249 num_macroblocks
, pipe_macroblocks
);
251 vpipe
->set_decode_target(vpipe
, t_vsfc
);
252 vpipe
->decode_macroblocks(vpipe
, p_vsfc
, f_vsfc
, num_macroblocks
,
253 &pipe_macroblocks
->base
, target_surface_priv
->render_fence
);
258 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
263 return XvMCBadSurface
;
268 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
273 return XvMCBadSurface
;
278 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
279 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
280 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
285 unsigned int width
, height
;
286 unsigned int border_width
;
288 struct pipe_video_context
*vpipe
;
289 XvMCSurfacePrivate
*surface_priv
;
290 XvMCContextPrivate
*context_priv
;
291 XvMCContext
*context
;
292 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
293 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
297 if (!surface
|| !surface
->privData
)
298 return XvMCBadSurface
;
300 if (XGetGeometry(dpy
, drawable
, &root
, &x
, &y
, &width
, &height
, &border_width
, &depth
) == BadDrawable
)
303 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
304 assert(srcx
+ srcw
- 1 < surface
->width
);
305 assert(srcy
+ srch
- 1 < surface
->height
);
307 * Some apps (mplayer) hit these asserts because they call
308 * this function after the window has been resized by the WM
309 * but before they've handled the corresponding XEvent and
310 * know about the new dimensions. The output should be clipped
311 * until the app updates destw and desth.
314 assert(destx + destw - 1 < width);
315 assert(desty + desth - 1 < height);
318 surface_priv
= surface
->privData
;
319 context
= surface_priv
->context
;
320 context_priv
= context
->privData
;
321 vpipe
= context_priv
->vpipe
;
323 if (!CreateOrResizeBackBuffer(vpipe
, width
, height
, &context_priv
->backbuffer
))
326 vpipe
->render_picture(vpipe
, surface_priv
->pipe_vsfc
, PictureToPipe(flags
), &src_rect
,
327 context_priv
->backbuffer
, &dst_rect
, surface_priv
->disp_fence
);
329 vl_video_bind_drawable(vpipe
, drawable
);
331 vpipe
->screen
->flush_frontbuffer
334 context_priv
->backbuffer
,
341 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
346 return XvMCBadSurface
;
355 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
357 XvMCSurfacePrivate
*surface_priv
;
361 if (!surface
|| !surface
->privData
)
362 return XvMCBadSurface
;
364 surface_priv
= surface
->privData
;
365 pipe_video_surface_reference(&surface_priv
->pipe_vsfc
, NULL
);
367 surface
->privData
= NULL
;
372 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
376 if (!surface
|| !surface
->privData
)
377 return XvMCBadSurface
;
379 /* No op, only for overlaid rendering */