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 **************************************************************************/
30 #include <X11/Xlibint.h>
31 #include <vl_winsys.h>
32 #include <pipe/p_video_context.h>
33 #include <pipe/p_video_state.h>
34 #include <pipe/p_state.h>
35 #include <util/u_inlines.h>
36 #include <util/u_memory.h>
37 #include <util/u_math.h>
38 #include "xvmc_private.h"
40 static enum pipe_mpeg12_picture_type
PictureToPipe(int xvmc_pic
)
44 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP
;
45 case XVMC_BOTTOM_FIELD
:
46 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM
;
47 case XVMC_FRAME_PICTURE
:
48 return PIPE_MPEG12_PICTURE_TYPE_FRAME
;
53 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic
);
58 static enum pipe_mpeg12_motion_type
MotionToPipe(int xvmc_motion_type
, unsigned xvmc_picture_structure
)
60 switch (xvmc_motion_type
) {
61 case XVMC_PREDICTION_FRAME
:
62 if (xvmc_picture_structure
== XVMC_FRAME_PICTURE
)
63 return PIPE_MPEG12_MOTION_TYPE_FRAME
;
65 return PIPE_MPEG12_MOTION_TYPE_16x8
;
68 case XVMC_PREDICTION_FIELD
:
69 return PIPE_MPEG12_MOTION_TYPE_FIELD
;
71 case XVMC_PREDICTION_DUAL_PRIME
:
72 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME
;
75 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized motion type 0x%08X (with picture structure 0x%08X).\n", xvmc_motion_type
, xvmc_picture_structure
);
81 MacroBlocksToPipe(struct pipe_screen
*screen
,
82 unsigned int xvmc_picture_structure
,
83 const XvMCMacroBlockArray
*xvmc_macroblocks
,
84 const XvMCBlockArray
*xvmc_blocks
,
85 unsigned int first_macroblock
,
86 unsigned int num_macroblocks
,
87 struct pipe_mpeg12_macroblock
*mb
)
90 XvMCMacroBlock
*xvmc_mb
;
92 assert(xvmc_macroblocks
);
95 assert(num_macroblocks
);
97 xvmc_mb
= xvmc_macroblocks
->macro_blocks
+ first_macroblock
;
99 for (i
= 0; i
< num_macroblocks
; ++i
) {
100 mb
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
101 mb
->mbx
= xvmc_mb
->x
;
102 mb
->mby
= xvmc_mb
->y
;
104 if (!(xvmc_mb
->macroblock_type
& XVMC_MB_TYPE_INTRA
))
105 mb
->mo_type
= MotionToPipe(xvmc_mb
->motion_type
, xvmc_picture_structure
);
106 /* Get rid of Valgrind 'undefined' warnings */
110 mb
->dct_intra
= xvmc_mb
->macroblock_type
& XVMC_MB_TYPE_INTRA
;
111 mb
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
112 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
114 switch (xvmc_mb
->macroblock_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) {
115 case XVMC_MB_TYPE_MOTION_FORWARD
:
116 mb
->mv
[0].wheight
= 255;
117 mb
->mv
[1].wheight
= 0;
120 case (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
):
121 mb
->mv
[0].wheight
= 127;
122 mb
->mv
[1].wheight
= 127;
125 case XVMC_MB_TYPE_MOTION_BACKWARD
:
126 mb
->mv
[0].wheight
= 0;
127 mb
->mv
[1].wheight
= 255;
131 /* workaround for xines xxmc video out plugin */
132 if (!(xvmc_mb
->macroblock_type
& ~XVMC_MB_TYPE_PATTERN
)) {
133 mb
->mv
[0].wheight
= 255;
134 mb
->mv
[1].wheight
= 0;
136 mb
->mv
[0].wheight
= 0;
137 mb
->mv
[1].wheight
= 0;
142 for (j
= 0; j
< 2; ++j
) {
143 mb
->mv
[j
].top
.x
= xvmc_mb
->PMV
[0][j
][0];
144 mb
->mv
[j
].top
.y
= xvmc_mb
->PMV
[0][j
][1];
145 mb
->mv
[j
].bottom
.x
= xvmc_mb
->PMV
[1][j
][0];
146 mb
->mv
[j
].bottom
.y
= xvmc_mb
->PMV
[1][j
][1];
149 mb
->mv
[0].top
.field_select
= xvmc_mb
->motion_vertical_field_select
& XVMC_SELECT_FIRST_FORWARD
;
150 mb
->mv
[1].top
.field_select
= xvmc_mb
->motion_vertical_field_select
& XVMC_SELECT_FIRST_BACKWARD
;
151 mb
->mv
[0].bottom
.field_select
= xvmc_mb
->motion_vertical_field_select
& XVMC_SELECT_SECOND_FORWARD
;
152 mb
->mv
[1].bottom
.field_select
= xvmc_mb
->motion_vertical_field_select
& XVMC_SELECT_SECOND_BACKWARD
;
154 mb
->cbp
= xvmc_mb
->coded_block_pattern
;
155 mb
->blocks
= xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
;
163 unmap_and_flush_surface(XvMCSurfacePrivate
*surface
)
165 struct pipe_video_buffer
*ref_frames
[2];
166 XvMCContextPrivate
*context_priv
;
171 context_priv
= surface
->context
->privData
;
173 for ( i
= 0; i
< 2; ++i
) {
174 if (surface
->ref_surfaces
[i
]) {
175 XvMCSurfacePrivate
*ref
= surface
->ref_surfaces
[i
]->privData
;
179 unmap_and_flush_surface(ref
);
180 surface
->ref_surfaces
[i
] = NULL
;
181 ref_frames
[i
] = ref
->video_buffer
;
183 ref_frames
[i
] = NULL
;
187 if (surface
->mapped
) {
188 surface
->decode_buffer
->unmap(surface
->decode_buffer
);
189 context_priv
->decoder
->flush_buffer(surface
->decode_buffer
,
191 surface
->video_buffer
,
192 &surface
->flush_fence
);
198 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
200 const enum pipe_format resource_formats
[3] = {
201 PIPE_FORMAT_R8_SNORM
,
202 PIPE_FORMAT_R8_SNORM
,
206 XvMCContextPrivate
*context_priv
;
207 struct pipe_video_context
*vpipe
;
208 XvMCSurfacePrivate
*surface_priv
;
210 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating surface %p.\n", surface
);
215 return XvMCBadContext
;
217 return XvMCBadSurface
;
219 context_priv
= context
->privData
;
220 vpipe
= context_priv
->vctx
->vpipe
;
222 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
226 surface_priv
->decode_buffer
= context_priv
->decoder
->create_buffer(context_priv
->decoder
);
227 surface_priv
->video_buffer
= vpipe
->create_buffer(vpipe
, PIPE_FORMAT_YV12
, //TODO
229 context_priv
->decoder
->chroma_format
,
230 context_priv
->decoder
->width
,
231 context_priv
->decoder
->height
);
232 surface_priv
->context
= context
;
234 surface
->surface_id
= XAllocID(dpy
);
235 surface
->context_id
= context
->context_id
;
236 surface
->surface_type_id
= context
->surface_type_id
;
237 surface
->width
= context
->width
;
238 surface
->height
= context
->height
;
239 surface
->privData
= surface_priv
;
243 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p created.\n", surface
);
249 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
250 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
251 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
252 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
255 struct pipe_video_context
*vpipe
;
256 struct pipe_video_decode_buffer
*t_buffer
;
257 XvMCContextPrivate
*context_priv
;
258 XvMCSurfacePrivate
*target_surface_priv
;
259 XvMCSurfacePrivate
*past_surface_priv
;
260 XvMCSurfacePrivate
*future_surface_priv
;
261 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
263 XVMC_MSG(XVMC_TRACE
, "[XvMC] Rendering to surface %p, with past %p and future %p\n",
264 target_surface
, past_surface
, future_surface
);
268 if (!context
|| !context
->privData
)
269 return XvMCBadContext
;
270 if (!target_surface
|| !target_surface
->privData
)
271 return XvMCBadSurface
;
273 if (picture_structure
!= XVMC_TOP_FIELD
&&
274 picture_structure
!= XVMC_BOTTOM_FIELD
&&
275 picture_structure
!= XVMC_FRAME_PICTURE
)
277 /* Bkwd pred equivalent to fwd (past && !future) */
278 if (future_surface
&& !past_surface
)
281 assert(context
->context_id
== target_surface
->context_id
);
282 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
283 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
288 assert(macroblocks
->context_id
== context
->context_id
);
289 assert(blocks
->context_id
== context
->context_id
);
291 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
293 target_surface_priv
= target_surface
->privData
;
294 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
295 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
297 assert(target_surface_priv
->context
== context
);
298 assert(!past_surface
|| past_surface_priv
->context
== context
);
299 assert(!future_surface
|| future_surface_priv
->context
== context
);
301 context_priv
= context
->privData
;
302 vpipe
= context_priv
->vctx
->vpipe
;
304 t_buffer
= target_surface_priv
->decode_buffer
;
306 // enshure that all reference frames are flushed
307 // not really nessasary, but speeds ups rendering
309 unmap_and_flush_surface(past_surface
->privData
);
312 unmap_and_flush_surface(future_surface
->privData
);
314 /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
315 if (target_surface_priv
->mapped
&& (
316 target_surface_priv
->ref_surfaces
[0] != past_surface
||
317 target_surface_priv
->ref_surfaces
[1] != future_surface
)) {
319 // If they change anyway we need to flush our surface
320 unmap_and_flush_surface(target_surface_priv
);
323 MacroBlocksToPipe(vpipe
->screen
, picture_structure
, macroblocks
, blocks
, first_macroblock
,
324 num_macroblocks
, pipe_macroblocks
);
326 if (!target_surface_priv
->mapped
) {
327 t_buffer
->map(t_buffer
);
328 target_surface_priv
->ref_surfaces
[0] = past_surface
;
329 target_surface_priv
->ref_surfaces
[1] = future_surface
;
330 target_surface_priv
->mapped
= 1;
333 t_buffer
->add_macroblocks(t_buffer
, num_macroblocks
, &pipe_macroblocks
->base
);
335 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for rendering.\n", target_surface
);
341 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
346 return XvMCBadSurface
;
348 // don't call flush here, because this is usually
349 // called once for every slice instead of every frame
351 XVMC_MSG(XVMC_TRACE
, "[XvMC] Flushing surface %p\n", surface
);
357 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
362 return XvMCBadSurface
;
364 XVMC_MSG(XVMC_TRACE
, "[XvMC] Syncing surface %p\n", surface
);
370 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
371 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
372 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
375 static int dump_window
= -1;
377 struct pipe_video_context
*vpipe
;
378 struct pipe_video_compositor
*compositor
;
380 XvMCSurfacePrivate
*surface_priv
;
381 XvMCContextPrivate
*context_priv
;
382 XvMCSubpicturePrivate
*subpicture_priv
;
383 XvMCContext
*context
;
384 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
385 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
386 struct pipe_surface
*drawable_surface
;
388 XVMC_MSG(XVMC_TRACE
, "[XvMC] Displaying surface %p.\n", surface
);
392 if (!surface
|| !surface
->privData
)
393 return XvMCBadSurface
;
395 surface_priv
= surface
->privData
;
396 context
= surface_priv
->context
;
397 context_priv
= context
->privData
;
399 drawable_surface
= vl_drawable_surface_get(context_priv
->vctx
, drawable
);
400 if (!drawable_surface
)
403 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
404 assert(srcx
+ srcw
- 1 < surface
->width
);
405 assert(srcy
+ srch
- 1 < surface
->height
);
407 * Some apps (mplayer) hit these asserts because they call
408 * this function after the window has been resized by the WM
409 * but before they've handled the corresponding XEvent and
410 * know about the new dimensions. The output should be clipped
411 * until the app updates destw and desth.
414 assert(destx + destw - 1 < drawable_surface->width);
415 assert(desty + desth - 1 < drawable_surface->height);
418 subpicture_priv
= surface_priv
->subpicture
? surface_priv
->subpicture
->privData
: NULL
;
419 vpipe
= context_priv
->vctx
->vpipe
;
420 compositor
= context_priv
->compositor
;
422 unmap_and_flush_surface(surface_priv
);
424 compositor
->clear_layers(compositor
);
425 compositor
->set_buffer_layer(compositor
, 0, surface_priv
->video_buffer
, &src_rect
, NULL
);
427 if (subpicture_priv
) {
428 struct pipe_video_rect src_rect
= {surface_priv
->subx
, surface_priv
->suby
, surface_priv
->subw
, surface_priv
->subh
};
429 struct pipe_video_rect dst_rect
= {surface_priv
->surfx
, surface_priv
->surfy
, surface_priv
->surfw
, surface_priv
->surfh
};
431 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p has subpicture %p.\n", surface
, surface_priv
->subpicture
);
433 assert(subpicture_priv
->surface
== surface
);
434 if (subpicture_priv
->palette
)
435 compositor
->set_palette_layer(compositor
, 1, subpicture_priv
->sampler
, subpicture_priv
->palette
, &src_rect
, &dst_rect
);
437 compositor
->set_rgba_layer(compositor
, 1, subpicture_priv
->sampler
, &src_rect
, &dst_rect
);
439 surface_priv
->subpicture
= NULL
;
440 subpicture_priv
->surface
= NULL
;
443 compositor
->render_picture(compositor
, PictureToPipe(flags
), drawable_surface
, &dst_rect
, &surface_priv
->disp_fence
);
445 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface
);
447 vpipe
->screen
->flush_frontbuffer
450 drawable_surface
->texture
,
452 vl_contextprivate_get(context_priv
->vctx
, drawable_surface
)
455 pipe_surface_reference(&drawable_surface
, NULL
);
457 if(dump_window
== -1) {
458 dump_window
= debug_get_num_option("XVMC_DUMP", 0);
462 static unsigned int framenum
= 0;
464 sprintf(cmd
, "xwd -id %d -out xvmc_frame_%08d.xwd", (int)drawable
, ++framenum
);
468 XVMC_MSG(XVMC_TRACE
, "[XvMC] Pushed surface %p to front buffer.\n", surface
);
474 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
479 return XvMCBadSurface
;
489 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
491 XvMCSurfacePrivate
*surface_priv
;
493 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying surface %p.\n", surface
);
497 if (!surface
|| !surface
->privData
)
498 return XvMCBadSurface
;
500 surface_priv
= surface
->privData
;
501 surface_priv
->decode_buffer
->destroy(surface_priv
->decode_buffer
);
502 surface_priv
->video_buffer
->destroy(surface_priv
->video_buffer
);
504 surface
->privData
= NULL
;
506 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p destroyed.\n", surface
);
512 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
516 if (!surface
|| !surface
->privData
)
517 return XvMCBadSurface
;
519 /* No op, only for overlaid rendering */