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 **************************************************************************/
31 #include <X11/Xlibint.h>
33 #include <pipe/p_video_context.h>
34 #include <pipe/p_video_state.h>
35 #include <pipe/p_state.h>
37 #include <util/u_inlines.h>
38 #include <util/u_memory.h>
39 #include <util/u_math.h>
41 #include <vl_winsys.h>
43 #include "xvmc_private.h"
45 static enum pipe_mpeg12_picture_type
PictureToPipe(int xvmc_pic
)
49 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP
;
50 case XVMC_BOTTOM_FIELD
:
51 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM
;
52 case XVMC_FRAME_PICTURE
:
53 return PIPE_MPEG12_PICTURE_TYPE_FRAME
;
58 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic
);
64 MacroBlockTypeToPipeWeights(const XvMCMacroBlock
*xvmc_mb
, unsigned weights
[2])
68 switch (xvmc_mb
->macroblock_type
& (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
)) {
69 case XVMC_MB_TYPE_MOTION_FORWARD
:
70 weights
[0] = PIPE_VIDEO_MV_WEIGHT_MAX
;
71 weights
[1] = PIPE_VIDEO_MV_WEIGHT_MIN
;
74 case (XVMC_MB_TYPE_MOTION_FORWARD
| XVMC_MB_TYPE_MOTION_BACKWARD
):
75 weights
[0] = PIPE_VIDEO_MV_WEIGHT_HALF
;
76 weights
[1] = PIPE_VIDEO_MV_WEIGHT_HALF
;
79 case XVMC_MB_TYPE_MOTION_BACKWARD
:
80 weights
[0] = PIPE_VIDEO_MV_WEIGHT_MIN
;
81 weights
[1] = PIPE_VIDEO_MV_WEIGHT_MAX
;
85 /* workaround for xines xxmc video out plugin */
86 if (!(xvmc_mb
->macroblock_type
& ~XVMC_MB_TYPE_PATTERN
)) {
87 weights
[0] = PIPE_VIDEO_MV_WEIGHT_MAX
;
88 weights
[1] = PIPE_VIDEO_MV_WEIGHT_MIN
;
90 weights
[0] = PIPE_VIDEO_MV_WEIGHT_MIN
;
91 weights
[1] = PIPE_VIDEO_MV_WEIGHT_MIN
;
97 static inline struct pipe_motionvector
98 MotionVectorToPipe(const XvMCMacroBlock
*xvmc_mb
, unsigned vector
,
99 unsigned field_select_mask
, unsigned weight
)
101 struct pipe_motionvector mv
;
105 switch (xvmc_mb
->motion_type
) {
106 case XVMC_PREDICTION_FRAME
:
107 mv
.top
.x
= xvmc_mb
->PMV
[0][vector
][0];
108 mv
.top
.y
= xvmc_mb
->PMV
[0][vector
][1];
109 mv
.top
.field_select
= PIPE_VIDEO_FRAME
;
110 mv
.top
.weight
= weight
;
112 mv
.bottom
.x
= xvmc_mb
->PMV
[0][vector
][0];
113 mv
.bottom
.y
= xvmc_mb
->PMV
[0][vector
][1];
114 mv
.bottom
.weight
= weight
;
115 mv
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
118 case XVMC_PREDICTION_FIELD
:
119 mv
.top
.x
= xvmc_mb
->PMV
[0][vector
][0];
120 mv
.top
.y
= xvmc_mb
->PMV
[0][vector
][1];
121 mv
.top
.field_select
= (xvmc_mb
->motion_vertical_field_select
& field_select_mask
) ?
122 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
123 mv
.top
.weight
= weight
;
125 mv
.bottom
.x
= xvmc_mb
->PMV
[1][vector
][0];
126 mv
.bottom
.y
= xvmc_mb
->PMV
[1][vector
][1];
127 mv
.bottom
.field_select
= (xvmc_mb
->motion_vertical_field_select
& (field_select_mask
<< 2)) ?
128 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
129 mv
.bottom
.weight
= weight
;
132 default: // TODO: Support DUALPRIME and 16x8
140 MacroBlocksToPipe(XvMCSurfacePrivate
*surface
,
141 unsigned int xvmc_picture_structure
,
142 const XvMCMacroBlock
*xvmc_mb
,
143 const XvMCBlockArray
*xvmc_blocks
,
144 unsigned int num_macroblocks
,
145 struct pipe_mpeg12_macroblock
*mb
)
152 assert(num_macroblocks
);
154 for (i
= 0; i
< num_macroblocks
; ++i
) {
155 unsigned mv_pos
= xvmc_mb
->x
+ surface
->mv_stride
* xvmc_mb
->y
;
156 unsigned mv_weights
[2];
158 mb
->base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
159 mb
->mbx
= xvmc_mb
->x
;
160 mb
->mby
= xvmc_mb
->y
;
162 mb
->dct_intra
= xvmc_mb
->macroblock_type
& XVMC_MB_TYPE_INTRA
;
163 mb
->dct_type
= xvmc_mb
->dct_type
== XVMC_DCT_TYPE_FIELD
?
164 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
165 mb
->cbp
= xvmc_mb
->coded_block_pattern
;
166 mb
->blocks
= xvmc_blocks
->blocks
+ xvmc_mb
->index
* BLOCK_SIZE_SAMPLES
;
168 MacroBlockTypeToPipeWeights(xvmc_mb
, mv_weights
);
170 for (j
= 0; j
< 2; ++j
) {
171 if (!surface
->ref
[j
].mv
) continue;
173 surface
->ref
[j
].mv
[mv_pos
] = MotionVectorToPipe
176 j
? XVMC_SELECT_FIRST_BACKWARD
: XVMC_SELECT_FIRST_FORWARD
,
188 unmap_and_flush_surface(XvMCSurfacePrivate
*surface
)
190 struct pipe_video_buffer
*ref_frames
[2];
191 XvMCContextPrivate
*context_priv
;
196 context_priv
= surface
->context
->privData
;
198 for ( i
= 0; i
< 2; ++i
) {
199 if (surface
->ref
[i
].surface
) {
200 XvMCSurfacePrivate
*ref
= surface
->ref
[i
].surface
->privData
;
204 unmap_and_flush_surface(ref
);
205 surface
->ref
[i
].surface
= NULL
;
206 ref_frames
[i
] = ref
->video_buffer
;
208 ref_frames
[i
] = NULL
;
212 if (surface
->mapped
) {
213 surface
->decode_buffer
->unmap(surface
->decode_buffer
);
214 context_priv
->decoder
->flush_buffer(surface
->decode_buffer
,
216 surface
->video_buffer
,
217 &surface
->flush_fence
);
223 Status
XvMCCreateSurface(Display
*dpy
, XvMCContext
*context
, XvMCSurface
*surface
)
225 const enum pipe_format resource_formats
[3] = {
226 PIPE_FORMAT_R8_SNORM
,
227 PIPE_FORMAT_R8_SNORM
,
231 XvMCContextPrivate
*context_priv
;
232 struct pipe_video_context
*vpipe
;
233 XvMCSurfacePrivate
*surface_priv
;
235 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating surface %p.\n", surface
);
240 return XvMCBadContext
;
242 return XvMCBadSurface
;
244 context_priv
= context
->privData
;
245 vpipe
= context_priv
->vctx
->vpipe
;
247 surface_priv
= CALLOC(1, sizeof(XvMCSurfacePrivate
));
251 surface_priv
->decode_buffer
= context_priv
->decoder
->create_buffer(context_priv
->decoder
);
252 surface_priv
->mv_stride
= surface_priv
->decode_buffer
->get_mv_stream_stride(surface_priv
->decode_buffer
);
253 surface_priv
->video_buffer
= vpipe
->create_buffer(vpipe
, PIPE_FORMAT_YV12
, //TODO
255 context_priv
->decoder
->chroma_format
,
256 context_priv
->decoder
->width
,
257 context_priv
->decoder
->height
);
258 surface_priv
->context
= context
;
260 surface
->surface_id
= XAllocID(dpy
);
261 surface
->context_id
= context
->context_id
;
262 surface
->surface_type_id
= context
->surface_type_id
;
263 surface
->width
= context
->width
;
264 surface
->height
= context
->height
;
265 surface
->privData
= surface_priv
;
269 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p created.\n", surface
);
275 Status
XvMCRenderSurface(Display
*dpy
, XvMCContext
*context
, unsigned int picture_structure
,
276 XvMCSurface
*target_surface
, XvMCSurface
*past_surface
, XvMCSurface
*future_surface
,
277 unsigned int flags
, unsigned int num_macroblocks
, unsigned int first_macroblock
,
278 XvMCMacroBlockArray
*macroblocks
, XvMCBlockArray
*blocks
281 struct pipe_video_context
*vpipe
;
282 struct pipe_video_decode_buffer
*t_buffer
;
284 XvMCContextPrivate
*context_priv
;
285 XvMCSurfacePrivate
*target_surface_priv
;
286 XvMCSurfacePrivate
*past_surface_priv
;
287 XvMCSurfacePrivate
*future_surface_priv
;
288 XvMCMacroBlock
*xvmc_mb
;
292 struct pipe_mpeg12_macroblock pipe_macroblocks
[num_macroblocks
];
294 XVMC_MSG(XVMC_TRACE
, "[XvMC] Rendering to surface %p, with past %p and future %p\n",
295 target_surface
, past_surface
, future_surface
);
299 if (!context
|| !context
->privData
)
300 return XvMCBadContext
;
301 if (!target_surface
|| !target_surface
->privData
)
302 return XvMCBadSurface
;
304 if (picture_structure
!= XVMC_TOP_FIELD
&&
305 picture_structure
!= XVMC_BOTTOM_FIELD
&&
306 picture_structure
!= XVMC_FRAME_PICTURE
)
308 /* Bkwd pred equivalent to fwd (past && !future) */
309 if (future_surface
&& !past_surface
)
312 assert(context
->context_id
== target_surface
->context_id
);
313 assert(!past_surface
|| context
->context_id
== past_surface
->context_id
);
314 assert(!future_surface
|| context
->context_id
== future_surface
->context_id
);
319 assert(macroblocks
->context_id
== context
->context_id
);
320 assert(blocks
->context_id
== context
->context_id
);
322 assert(flags
== 0 || flags
== XVMC_SECOND_FIELD
);
324 target_surface_priv
= target_surface
->privData
;
325 past_surface_priv
= past_surface
? past_surface
->privData
: NULL
;
326 future_surface_priv
= future_surface
? future_surface
->privData
: NULL
;
328 assert(target_surface_priv
->context
== context
);
329 assert(!past_surface
|| past_surface_priv
->context
== context
);
330 assert(!future_surface
|| future_surface_priv
->context
== context
);
332 context_priv
= context
->privData
;
333 vpipe
= context_priv
->vctx
->vpipe
;
335 t_buffer
= target_surface_priv
->decode_buffer
;
337 // enshure that all reference frames are flushed
338 // not really nessasary, but speeds ups rendering
340 unmap_and_flush_surface(past_surface
->privData
);
343 unmap_and_flush_surface(future_surface
->privData
);
345 xvmc_mb
= macroblocks
->macro_blocks
+ first_macroblock
;
347 /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
348 if (target_surface_priv
->mapped
&& (
349 target_surface_priv
->ref
[0].surface
!= past_surface
||
350 target_surface_priv
->ref
[1].surface
!= future_surface
||
351 (xvmc_mb
->x
== 0 && xvmc_mb
->y
== 0))) {
353 // If they change anyway we need to clear our surface
354 unmap_and_flush_surface(target_surface_priv
);
357 if (!target_surface_priv
->mapped
) {
358 t_buffer
->map(t_buffer
);
360 for (i
= 0; i
< 2; ++i
) {
361 target_surface_priv
->ref
[i
].surface
= i
== 0 ? past_surface
: future_surface
;
363 if (target_surface_priv
->ref
[i
].surface
)
364 target_surface_priv
->ref
[i
].mv
= t_buffer
->get_mv_stream(t_buffer
, i
);
366 target_surface_priv
->ref
[i
].mv
= NULL
;
368 target_surface_priv
->mapped
= 1;
371 MacroBlocksToPipe(target_surface_priv
, picture_structure
, xvmc_mb
, blocks
, num_macroblocks
, pipe_macroblocks
);
373 t_buffer
->add_macroblocks(t_buffer
, num_macroblocks
, &pipe_macroblocks
->base
);
375 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for rendering.\n", target_surface
);
381 Status
XvMCFlushSurface(Display
*dpy
, XvMCSurface
*surface
)
386 return XvMCBadSurface
;
388 // don't call flush here, because this is usually
389 // called once for every slice instead of every frame
391 XVMC_MSG(XVMC_TRACE
, "[XvMC] Flushing surface %p\n", surface
);
397 Status
XvMCSyncSurface(Display
*dpy
, XvMCSurface
*surface
)
402 return XvMCBadSurface
;
404 XVMC_MSG(XVMC_TRACE
, "[XvMC] Syncing surface %p\n", surface
);
410 Status
XvMCPutSurface(Display
*dpy
, XvMCSurface
*surface
, Drawable drawable
,
411 short srcx
, short srcy
, unsigned short srcw
, unsigned short srch
,
412 short destx
, short desty
, unsigned short destw
, unsigned short desth
,
415 static int dump_window
= -1;
417 struct pipe_video_context
*vpipe
;
418 struct pipe_video_compositor
*compositor
;
420 XvMCSurfacePrivate
*surface_priv
;
421 XvMCContextPrivate
*context_priv
;
422 XvMCSubpicturePrivate
*subpicture_priv
;
423 XvMCContext
*context
;
424 struct pipe_video_rect src_rect
= {srcx
, srcy
, srcw
, srch
};
425 struct pipe_video_rect dst_rect
= {destx
, desty
, destw
, desth
};
426 struct pipe_surface
*drawable_surface
;
428 XVMC_MSG(XVMC_TRACE
, "[XvMC] Displaying surface %p.\n", surface
);
432 if (!surface
|| !surface
->privData
)
433 return XvMCBadSurface
;
435 surface_priv
= surface
->privData
;
436 context
= surface_priv
->context
;
437 context_priv
= context
->privData
;
439 drawable_surface
= vl_drawable_surface_get(context_priv
->vctx
, drawable
);
440 if (!drawable_surface
)
443 assert(flags
== XVMC_TOP_FIELD
|| flags
== XVMC_BOTTOM_FIELD
|| flags
== XVMC_FRAME_PICTURE
);
444 assert(srcx
+ srcw
- 1 < surface
->width
);
445 assert(srcy
+ srch
- 1 < surface
->height
);
447 * Some apps (mplayer) hit these asserts because they call
448 * this function after the window has been resized by the WM
449 * but before they've handled the corresponding XEvent and
450 * know about the new dimensions. The output should be clipped
451 * until the app updates destw and desth.
454 assert(destx + destw - 1 < drawable_surface->width);
455 assert(desty + desth - 1 < drawable_surface->height);
458 subpicture_priv
= surface_priv
->subpicture
? surface_priv
->subpicture
->privData
: NULL
;
459 vpipe
= context_priv
->vctx
->vpipe
;
460 compositor
= context_priv
->compositor
;
462 unmap_and_flush_surface(surface_priv
);
464 compositor
->clear_layers(compositor
);
465 compositor
->set_buffer_layer(compositor
, 0, surface_priv
->video_buffer
, &src_rect
, NULL
);
467 if (subpicture_priv
) {
468 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p has subpicture %p.\n", surface
, surface_priv
->subpicture
);
470 assert(subpicture_priv
->surface
== surface
);
472 if (subpicture_priv
->palette
)
473 compositor
->set_palette_layer(compositor
, 1, subpicture_priv
->sampler
, subpicture_priv
->palette
,
474 &subpicture_priv
->src_rect
, &subpicture_priv
->dst_rect
);
476 compositor
->set_rgba_layer(compositor
, 1, subpicture_priv
->sampler
, &src_rect
, &dst_rect
);
478 surface_priv
->subpicture
= NULL
;
479 subpicture_priv
->surface
= NULL
;
482 compositor
->render_picture(compositor
, PictureToPipe(flags
), drawable_surface
, &dst_rect
, &surface_priv
->disp_fence
);
484 XVMC_MSG(XVMC_TRACE
, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface
);
486 vpipe
->screen
->flush_frontbuffer
489 drawable_surface
->texture
,
491 vl_contextprivate_get(context_priv
->vctx
, drawable_surface
)
494 pipe_surface_reference(&drawable_surface
, NULL
);
496 if(dump_window
== -1) {
497 dump_window
= debug_get_num_option("XVMC_DUMP", 0);
501 static unsigned int framenum
= 0;
504 sprintf(cmd
, "xwd -id %d -out xvmc_frame_%08d.xwd", (int)drawable
, ++framenum
);
505 if (system(cmd
) != 0)
506 XVMC_MSG(XVMC_ERR
, "[XvMC] Dumping surface %p failed.\n", surface
);
509 XVMC_MSG(XVMC_TRACE
, "[XvMC] Pushed surface %p to front buffer.\n", surface
);
515 Status
XvMCGetSurfaceStatus(Display
*dpy
, XvMCSurface
*surface
, int *status
)
520 return XvMCBadSurface
;
530 Status
XvMCDestroySurface(Display
*dpy
, XvMCSurface
*surface
)
532 XvMCSurfacePrivate
*surface_priv
;
534 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying surface %p.\n", surface
);
538 if (!surface
|| !surface
->privData
)
539 return XvMCBadSurface
;
541 surface_priv
= surface
->privData
;
542 surface_priv
->decode_buffer
->destroy(surface_priv
->decode_buffer
);
543 surface_priv
->video_buffer
->destroy(surface_priv
->video_buffer
);
545 surface
->privData
= NULL
;
547 XVMC_MSG(XVMC_TRACE
, "[XvMC] Surface %p destroyed.\n", surface
);
553 Status
XvMCHideSurface(Display
*dpy
, XvMCSurface
*surface
)
557 if (!surface
|| !surface
->privData
)
558 return XvMCBadSurface
;
560 /* No op, only for overlaid rendering */