**************************************************************************/
#include <assert.h>
+#include <stdio.h>
+
#include <X11/Xlibint.h>
+
#include <pipe/p_video_context.h>
#include <pipe/p_video_state.h>
#include <pipe/p_state.h>
-#include <util/u_memory.h>
-#include "xvmc_private.h"
-static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
-{
- if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
- return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
- if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
- return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
- if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
- return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
- if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
- return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
+#include <util/u_inlines.h>
+#include <util/u_memory.h>
+#include <util/u_math.h>
- assert(0);
+#include <vl_winsys.h>
- return -1;
-}
+#include "xvmc_private.h"
static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
{
assert(0);
}
+ XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic);
+
return -1;
}
-static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
+static inline void
+MacroBlockTypeToPipeWeights(const XvMCMacroBlock *xvmc_mb, unsigned weights[2])
{
- switch (xvmc_motion_type) {
- case XVMC_PREDICTION_FRAME:
- return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ?
- PIPE_MPEG12_MOTION_TYPE_16x8 : PIPE_MPEG12_MOTION_TYPE_FRAME;
- case XVMC_PREDICTION_FIELD:
- return PIPE_MPEG12_MOTION_TYPE_FIELD;
- case XVMC_PREDICTION_DUAL_PRIME:
- return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
- default:
- assert(0);
+ assert(xvmc_mb);
+
+ switch (xvmc_mb->macroblock_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) {
+ case XVMC_MB_TYPE_MOTION_FORWARD:
+ weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
+ weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
+ break;
+
+ case (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD):
+ weights[0] = PIPE_VIDEO_MV_WEIGHT_HALF;
+ weights[1] = PIPE_VIDEO_MV_WEIGHT_HALF;
+ break;
+
+ case XVMC_MB_TYPE_MOTION_BACKWARD:
+ weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
+ weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;
+ break;
+
+ default:
+ /* workaround for xines xxmc video out plugin */
+ if (!(xvmc_mb->macroblock_type & ~XVMC_MB_TYPE_PATTERN)) {
+ weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
+ weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
+ } else {
+ weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
+ weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
+ }
+ break;
}
-
- return -1;
}
-static bool
-CreateOrResizeBackBuffer(struct pipe_video_context *vpipe, unsigned int width, unsigned int height,
- struct pipe_surface **backbuffer)
+static inline struct pipe_motionvector
+MotionVectorToPipe(const XvMCMacroBlock *xvmc_mb, unsigned vector,
+ unsigned field_select_mask, unsigned weight)
{
- struct pipe_texture template;
- struct pipe_texture *tex;
-
- assert(vpipe);
-
- if (*backbuffer) {
- if ((*backbuffer)->width != width || (*backbuffer)->height != height)
- pipe_surface_reference(backbuffer, NULL);
- else
- return true;
+ struct pipe_motionvector mv;
+
+ assert(xvmc_mb);
+
+ switch (xvmc_mb->motion_type) {
+ case XVMC_PREDICTION_FRAME:
+ mv.top.x = xvmc_mb->PMV[0][vector][0];
+ mv.top.y = xvmc_mb->PMV[0][vector][1];
+ mv.top.field_select = PIPE_VIDEO_FRAME;
+ mv.top.weight = weight;
+
+ mv.bottom.x = xvmc_mb->PMV[0][vector][0];
+ mv.bottom.y = xvmc_mb->PMV[0][vector][1];
+ mv.bottom.weight = weight;
+ mv.bottom.field_select = PIPE_VIDEO_FRAME;
+ break;
+
+ case XVMC_PREDICTION_FIELD:
+ mv.top.x = xvmc_mb->PMV[0][vector][0];
+ mv.top.y = xvmc_mb->PMV[0][vector][1];
+ mv.top.field_select = (xvmc_mb->motion_vertical_field_select & field_select_mask) ?
+ PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
+ mv.top.weight = weight;
+
+ mv.bottom.x = xvmc_mb->PMV[1][vector][0];
+ mv.bottom.y = xvmc_mb->PMV[1][vector][1];
+ mv.bottom.field_select = (xvmc_mb->motion_vertical_field_select & (field_select_mask << 2)) ?
+ PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
+ mv.bottom.weight = weight;
+ break;
+
+ default: // TODO: Support DUALPRIME and 16x8
+ break;
}
- memset(&template, 0, sizeof(struct pipe_texture));
- template.target = PIPE_TEXTURE_2D;
- /* XXX: Needs to match the drawable's format? */
- template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
- template.last_level = 0;
- template.width0 = width;
- template.height0 = height;
- template.depth0 = 1;
- template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
-
- tex = vpipe->screen->texture_create(vpipe->screen, &template);
- if (!tex)
- return false;
-
- *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
- PIPE_BUFFER_USAGE_GPU_READ |
- PIPE_BUFFER_USAGE_GPU_WRITE);
- pipe_texture_reference(&tex, NULL);
-
- if (!*backbuffer)
- return false;
-
- /* Clear the backbuffer in case the video doesn't cover the whole window */
- /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
- vpipe->clear_surface(vpipe, 0, 0, width, height, 0, *backbuffer);
-
- return true;
+ return mv;
}
static void
-MacroBlocksToPipe(const XvMCMacroBlockArray *xvmc_macroblocks,
+MacroBlocksToPipe(XvMCSurfacePrivate *surface,
+ unsigned int xvmc_picture_structure,
+ const XvMCMacroBlock *xvmc_mb,
const XvMCBlockArray *xvmc_blocks,
- unsigned int first_macroblock,
unsigned int num_macroblocks,
- struct pipe_mpeg12_macroblock *pipe_macroblocks)
+ struct pipe_mpeg12_macroblock *mb)
{
- unsigned int i, j, k, l;
- XvMCMacroBlock *xvmc_mb;
+ unsigned int i, j;
- assert(xvmc_macroblocks);
+ assert(xvmc_mb);
assert(xvmc_blocks);
- assert(pipe_macroblocks);
+ assert(mb);
assert(num_macroblocks);
- xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
-
for (i = 0; i < num_macroblocks; ++i) {
- pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
- pipe_macroblocks->mbx = xvmc_mb->x;
- pipe_macroblocks->mby = xvmc_mb->y;
- pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
- if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
- pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
- /* Get rid of Valgrind 'undefined' warnings */
- else
- pipe_macroblocks->mo_type = -1;
- pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
+ unsigned mv_pos = xvmc_mb->x + surface->mv_stride * xvmc_mb->y;
+ unsigned mv_weights[2];
+
+ mb->base.codec = PIPE_VIDEO_CODEC_MPEG12;
+ mb->mbx = xvmc_mb->x;
+ mb->mby = xvmc_mb->y;
+
+ mb->dct_intra = xvmc_mb->macroblock_type & XVMC_MB_TYPE_INTRA;
+ mb->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
+ mb->cbp = xvmc_mb->coded_block_pattern;
+ mb->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
+
+ MacroBlockTypeToPipeWeights(xvmc_mb, mv_weights);
- for (j = 0; j < 2; ++j)
- for (k = 0; k < 2; ++k)
- for (l = 0; l < 2; ++l)
- pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
+ for (j = 0; j < 2; ++j) {
+ if (!surface->ref[j].mv) continue;
- pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
- pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
+ surface->ref[j].mv[mv_pos] = MotionVectorToPipe
+ (
+ xvmc_mb, j,
+ j ? XVMC_SELECT_FIRST_BACKWARD : XVMC_SELECT_FIRST_FORWARD,
+ mv_weights[j]
+ );
- ++pipe_macroblocks;
+ }
+
+ ++mb;
++xvmc_mb;
}
}
+static void
+unmap_and_flush_surface(XvMCSurfacePrivate *surface)
+{
+ struct pipe_video_buffer *ref_frames[2];
+ XvMCContextPrivate *context_priv;
+ unsigned i;
+
+ assert(surface);
+
+ context_priv = surface->context->privData;
+
+ for ( i = 0; i < 2; ++i ) {
+ if (surface->ref[i].surface) {
+ XvMCSurfacePrivate *ref = surface->ref[i].surface->privData;
+
+ assert(ref);
+
+ unmap_and_flush_surface(ref);
+ surface->ref[i].surface = NULL;
+ ref_frames[i] = ref->video_buffer;
+ } else {
+ ref_frames[i] = NULL;
+ }
+ }
+
+ if (surface->mapped) {
+ surface->decode_buffer->unmap(surface->decode_buffer);
+ context_priv->decoder->flush_buffer(surface->decode_buffer,
+ ref_frames,
+ surface->video_buffer,
+ &surface->flush_fence);
+ surface->mapped = 0;
+ }
+}
+
+PUBLIC
Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
{
+ const enum pipe_format resource_formats[3] = {
+ PIPE_FORMAT_R8_SNORM,
+ PIPE_FORMAT_R8_SNORM,
+ PIPE_FORMAT_R8_SNORM
+ };
+
XvMCContextPrivate *context_priv;
struct pipe_video_context *vpipe;
XvMCSurfacePrivate *surface_priv;
- struct pipe_video_surface *vsfc;
+
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
assert(dpy);
return XvMCBadSurface;
context_priv = context->privData;
- vpipe = context_priv->vpipe;
+ vpipe = context_priv->vctx->vpipe;
surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
if (!surface_priv)
return BadAlloc;
- vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
- vpipe->width, vpipe->height);
- if (!vsfc) {
- FREE(surface_priv);
- return BadAlloc;
- }
-
- surface_priv->pipe_vsfc = vsfc;
+ surface_priv->decode_buffer = context_priv->decoder->create_buffer(context_priv->decoder);
+ surface_priv->mv_stride = surface_priv->decode_buffer->get_mv_stream_stride(surface_priv->decode_buffer);
+ surface_priv->video_buffer = vpipe->create_buffer(vpipe, PIPE_FORMAT_YV12, //TODO
+ resource_formats,
+ context_priv->decoder->chroma_format,
+ context_priv->decoder->width,
+ context_priv->decoder->height);
surface_priv->context = context;
surface->surface_id = XAllocID(dpy);
SyncHandle();
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface);
+
return Success;
}
+PUBLIC
Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
)
{
struct pipe_video_context *vpipe;
- struct pipe_surface *t_vsfc;
- struct pipe_surface *p_vsfc;
- struct pipe_surface *f_vsfc;
+ struct pipe_video_decode_buffer *t_buffer;
+
XvMCContextPrivate *context_priv;
XvMCSurfacePrivate *target_surface_priv;
XvMCSurfacePrivate *past_surface_priv;
XvMCSurfacePrivate *future_surface_priv;
+ XvMCMacroBlock *xvmc_mb;
+
+ unsigned i;
+
struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p, with past %p and future %p\n",
+ target_surface, past_surface, future_surface);
+
assert(dpy);
if (!context || !context->privData)
assert(!future_surface || future_surface_priv->context == context);
context_priv = context->privData;
- vpipe = context_priv->vpipe;
+ vpipe = context_priv->vctx->vpipe;
+
+ t_buffer = target_surface_priv->decode_buffer;
+
+ // enshure that all reference frames are flushed
+ // not really nessasary, but speeds ups rendering
+ if (past_surface)
+ unmap_and_flush_surface(past_surface->privData);
+
+ if (future_surface)
+ unmap_and_flush_surface(future_surface->privData);
- t_vsfc = target_surface_priv->pipe_vsfc;
- p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
- f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
+ xvmc_mb = macroblocks->macro_blocks + first_macroblock;
- MacroBlocksToPipe(macroblocks, blocks, first_macroblock,
- num_macroblocks, pipe_macroblocks);
+ /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
+ if (target_surface_priv->mapped && (
+ target_surface_priv->ref[0].surface != past_surface ||
+ target_surface_priv->ref[1].surface != future_surface ||
+ (xvmc_mb->x == 0 && xvmc_mb->y == 0))) {
+
+ // If they change anyway we need to clear our surface
+ unmap_and_flush_surface(target_surface_priv);
+ }
+
+ if (!target_surface_priv->mapped) {
+ t_buffer->map(t_buffer);
+
+ for (i = 0; i < 2; ++i) {
+ target_surface_priv->ref[i].surface = i == 0 ? past_surface : future_surface;
+
+ if (target_surface_priv->ref[i].surface)
+ target_surface_priv->ref[i].mv = t_buffer->get_mv_stream(t_buffer, i);
+ else
+ target_surface_priv->ref[i].mv = NULL;
+ }
+ target_surface_priv->mapped = 1;
+ }
- vpipe->set_decode_target(vpipe, t_vsfc);
- vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
- &pipe_macroblocks->base, target_surface_priv->render_fence);
+ MacroBlocksToPipe(target_surface_priv, picture_structure, xvmc_mb, blocks, num_macroblocks, pipe_macroblocks);
+
+ t_buffer->add_macroblocks(t_buffer, num_macroblocks, &pipe_macroblocks->base);
+
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
return Success;
}
+PUBLIC
Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
{
assert(dpy);
if (!surface)
return XvMCBadSurface;
+ // don't call flush here, because this is usually
+ // called once for every slice instead of every frame
+
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Flushing surface %p\n", surface);
+
return Success;
}
+PUBLIC
Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
{
assert(dpy);
if (!surface)
return XvMCBadSurface;
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Syncing surface %p\n", surface);
+
return Success;
}
+PUBLIC
Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
short srcx, short srcy, unsigned short srcw, unsigned short srch,
short destx, short desty, unsigned short destw, unsigned short desth,
int flags)
{
- Window root;
- int x, y;
- unsigned int width, height;
- unsigned int border_width;
- unsigned int depth;
+ static int dump_window = -1;
+
struct pipe_video_context *vpipe;
+ struct pipe_video_compositor *compositor;
+
XvMCSurfacePrivate *surface_priv;
XvMCContextPrivate *context_priv;
+ XvMCSubpicturePrivate *subpicture_priv;
XvMCContext *context;
struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
+ struct pipe_surface *drawable_surface;
+
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface);
assert(dpy);
if (!surface || !surface->privData)
return XvMCBadSurface;
- if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
+ surface_priv = surface->privData;
+ context = surface_priv->context;
+ context_priv = context->privData;
+
+ drawable_surface = vl_drawable_surface_get(context_priv->vctx, drawable);
+ if (!drawable_surface)
return BadDrawable;
assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
* until the app updates destw and desth.
*/
/*
- assert(destx + destw - 1 < width);
- assert(desty + desth - 1 < height);
+ assert(destx + destw - 1 < drawable_surface->width);
+ assert(desty + desth - 1 < drawable_surface->height);
*/
- surface_priv = surface->privData;
- context = surface_priv->context;
- context_priv = context->privData;
- vpipe = context_priv->vpipe;
+ subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
+ vpipe = context_priv->vctx->vpipe;
+ compositor = context_priv->compositor;
- if (!CreateOrResizeBackBuffer(vpipe, width, height, &context_priv->backbuffer))
- return BadAlloc;
+ unmap_and_flush_surface(surface_priv);
+
+ compositor->clear_layers(compositor);
+ compositor->set_buffer_layer(compositor, 0, surface_priv->video_buffer, &src_rect, NULL);
+
+ if (subpicture_priv) {
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
+
+ assert(subpicture_priv->surface == surface);
+
+ if (subpicture_priv->palette)
+ compositor->set_palette_layer(compositor, 1, subpicture_priv->sampler, subpicture_priv->palette,
+ &subpicture_priv->src_rect, &subpicture_priv->dst_rect);
+ else
+ compositor->set_rgba_layer(compositor, 1, subpicture_priv->sampler, &src_rect, &dst_rect);
+
+ surface_priv->subpicture = NULL;
+ subpicture_priv->surface = NULL;
+ }
+
+ compositor->render_picture(compositor, PictureToPipe(flags), drawable_surface, &dst_rect, &surface_priv->disp_fence);
- vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
- context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
- vl_video_bind_drawable(vpipe, drawable);
-
vpipe->screen->flush_frontbuffer
(
vpipe->screen,
- context_priv->backbuffer,
- vpipe->priv
+ drawable_surface->texture,
+ 0, 0,
+ vl_contextprivate_get(context_priv->vctx, drawable_surface)
);
+ pipe_surface_reference(&drawable_surface, NULL);
+
+ if(dump_window == -1) {
+ dump_window = debug_get_num_option("XVMC_DUMP", 0);
+ }
+
+ if(dump_window) {
+ static unsigned int framenum = 0;
+ char cmd[256];
+
+ sprintf(cmd, "xwd -id %d -out xvmc_frame_%08d.xwd", (int)drawable, ++framenum);
+ if (system(cmd) != 0)
+ XVMC_MSG(XVMC_ERR, "[XvMC] Dumping surface %p failed.\n", surface);
+ }
+
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
+
return Success;
}
+PUBLIC
Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
{
assert(dpy);
return Success;
}
+PUBLIC
Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
{
XvMCSurfacePrivate *surface_priv;
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface);
+
assert(dpy);
if (!surface || !surface->privData)
return XvMCBadSurface;
surface_priv = surface->privData;
- pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
+ surface_priv->decode_buffer->destroy(surface_priv->decode_buffer);
+ surface_priv->video_buffer->destroy(surface_priv->video_buffer);
FREE(surface_priv);
surface->privData = NULL;
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface);
+
return Success;
}
+PUBLIC
Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
{
assert(dpy);