**************************************************************************/
#include <assert.h>
+#include <stdio.h>
#include <X11/Xlibint.h>
#include <vl_winsys.h>
#include <pipe/p_video_context.h>
return -1;
}
-static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
+static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, unsigned int xvmc_picture_structure)
{
switch (xvmc_motion_type) {
case XVMC_PREDICTION_FRAME:
- if (xvmc_dct_type == XVMC_DCT_TYPE_FIELD)
- return PIPE_MPEG12_MOTION_TYPE_16x8;
- else if (xvmc_dct_type == XVMC_DCT_TYPE_FRAME)
+ if (xvmc_picture_structure == XVMC_FRAME_PICTURE)
return PIPE_MPEG12_MOTION_TYPE_FRAME;
+ else
+ return PIPE_MPEG12_MOTION_TYPE_16x8;
break;
case XVMC_PREDICTION_FIELD:
return PIPE_MPEG12_MOTION_TYPE_FIELD;
assert(0);
}
- XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with DCT type 0x%08X).\n", xvmc_motion_type, xvmc_dct_type);
+ XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with picture structure 0x%08X).\n", xvmc_motion_type, xvmc_picture_structure);
return -1;
}
-static bool
-CreateOrResizeBackBuffer(struct vl_context *vctx, unsigned int width, unsigned int height,
- struct pipe_surface **backbuffer)
-{
- struct pipe_video_context *vpipe;
- struct pipe_resource template;
- struct pipe_resource *tex;
-
- assert(vctx);
-
- vpipe = vctx->vpipe;
-
- if (*backbuffer) {
- if ((*backbuffer)->width != width || (*backbuffer)->height != height)
- pipe_surface_reference(backbuffer, NULL);
- else
- return true;
- }
-
- memset(&template, 0, sizeof(struct pipe_resource));
- template.target = PIPE_TEXTURE_2D;
- template.format = vctx->vscreen->format;
- template.last_level = 0;
- template.width0 = width;
- template.height0 = height;
- template.depth0 = 1;
- template.usage = PIPE_USAGE_DEFAULT;
- template.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_BLIT_SOURCE;
- template.flags = 0;
-
- tex = vpipe->screen->resource_create(vpipe->screen, &template);
- if (!tex)
- return false;
-
- *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
- template.bind);
- pipe_resource_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->surface_fill(vpipe, *backbuffer, 0, 0, width, height, 0);
-
- return true;
-}
-
static void
MacroBlocksToPipe(struct pipe_screen *screen,
+ unsigned int xvmc_picture_structure,
const XvMCMacroBlockArray *xvmc_macroblocks,
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;
+ unsigned int i, j;
XvMCMacroBlock *xvmc_mb;
assert(xvmc_macroblocks);
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);
+ mb->base.codec = PIPE_VIDEO_CODEC_MPEG12;
+ mb->mbx = xvmc_mb->x;
+ mb->mby = xvmc_mb->y;
+ mb->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
+ if (mb->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
+ mb->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_picture_structure);
/* Get rid of Valgrind 'undefined' warnings */
else
- pipe_macroblocks->mo_type = -1;
- pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
+ mb->mo_type = -1;
+ mb->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
- 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) {
+ mb->mv[j].top.x = xvmc_mb->PMV[0][j][0];
+ mb->mv[j].top.y = xvmc_mb->PMV[0][j][1];
+ mb->mv[j].bottom.x = xvmc_mb->PMV[1][j][0];
+ mb->mv[j].bottom.y = xvmc_mb->PMV[1][j][1];
+ }
+
+ mb->mv[0].top.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_FORWARD;
+ mb->mv[1].top.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_BACKWARD;
+ mb->mv[0].bottom.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_FORWARD;
+ mb->mv[1].bottom.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_BACKWARD;
- pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
- pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
+ mb->cbp = xvmc_mb->coded_block_pattern;
+ mb->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
- ++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_surfaces[i]) {
+ XvMCSurfacePrivate *ref = surface->ref_surfaces[i]->privData;
+
+ assert(ref);
+
+ unmap_and_flush_surface(ref);
+ surface->ref_surfaces[i] = 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)
{
XvMCContextPrivate *context_priv;
struct pipe_video_context *vpipe;
XvMCSurfacePrivate *surface_priv;
- struct pipe_resource template;
- struct pipe_resource *vsfc_tex;
- struct pipe_surface *vsfc;
XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
if (!surface_priv)
return BadAlloc;
- memset(&template, 0, sizeof(struct pipe_resource));
- template.target = PIPE_TEXTURE_2D;
- template.format = (enum pipe_format)vpipe->get_param(vpipe, PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT);
- template.last_level = 0;
- if (vpipe->is_format_supported(vpipe, template.format,
- PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
- PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)) {
- template.width0 = context->width;
- template.height0 = context->height;
- }
- else {
- assert(vpipe->is_format_supported(vpipe, template.format,
- PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
- PIPE_TEXTURE_GEOM_NON_SQUARE));
- template.width0 = util_next_power_of_two(context->width);
- template.height0 = util_next_power_of_two(context->height);
- }
- template.depth0 = 1;
- template.usage = PIPE_USAGE_DEFAULT;
- template.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
- template.flags = 0;
- vsfc_tex = vpipe->screen->resource_create(vpipe->screen, &template);
- if (!vsfc_tex) {
- FREE(surface_priv);
- return BadAlloc;
- }
-
- vsfc = vpipe->screen->get_tex_surface(vpipe->screen, vsfc_tex, 0, 0, 0,
- PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
- pipe_resource_reference(&vsfc_tex, NULL);
- 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->video_buffer = vpipe->create_buffer(vpipe, PIPE_FORMAT_YV12, //TODO
+ context_priv->decoder->chroma_format,
+ context_priv->decoder->width,
+ context_priv->decoder->height);
surface_priv->context = context;
surface->surface_id = XAllocID(dpy);
)
{
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;
struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
- unsigned int i;
- XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p.\n", target_surface);
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p, with past %p and future %p\n",
+ target_surface, past_surface, future_surface);
assert(dpy);
context_priv = context->privData;
vpipe = context_priv->vctx->vpipe;
- 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;
+ t_buffer = target_surface_priv->decode_buffer;
- MacroBlocksToPipe(vpipe->screen, macroblocks, blocks, first_macroblock,
+ // 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);
+
+ /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
+ if (target_surface_priv->mapped && (
+ target_surface_priv->ref_surfaces[0] != past_surface ||
+ target_surface_priv->ref_surfaces[1] != future_surface)) {
+
+ // If they change anyway we need to flush our surface
+ unmap_and_flush_surface(target_surface_priv);
+ }
+
+ MacroBlocksToPipe(vpipe->screen, picture_structure, macroblocks, blocks, first_macroblock,
num_macroblocks, pipe_macroblocks);
- 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);
+ if (!target_surface_priv->mapped) {
+ t_buffer->map(t_buffer);
+ target_surface_priv->ref_surfaces[0] = past_surface;
+ target_surface_priv->ref_surfaces[1] = future_surface;
+ target_surface_priv->mapped = 1;
+ }
+
+ t_buffer->add_macroblocks(t_buffer, num_macroblocks, &pipe_macroblocks->base);
XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
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;
}
if (!surface)
return XvMCBadSurface;
+ XVMC_MSG(XVMC_TRACE, "[XvMC] Syncing surface %p\n", surface);
+
return Success;
}
short destx, short desty, unsigned short destw, unsigned short desth,
int flags)
{
+ 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};
- void *displaytarget;
- unsigned width, height;
+ struct pipe_surface *drawable_surface;
XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface);
context = surface_priv->context;
context_priv = context->privData;
- displaytarget = vl_displaytarget_get(context_priv->vctx->vscreen, drawable, &width, &height);
- if (!displaytarget)
+ 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);
*/
subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
vpipe = context_priv->vctx->vpipe;
+ compositor = context_priv->compositor;
- if (!CreateOrResizeBackBuffer(context_priv->vctx, 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) {
struct pipe_video_rect src_rect = {surface_priv->subx, surface_priv->suby, surface_priv->subw, surface_priv->subh};
struct pipe_video_rect dst_rect = {surface_priv->surfx, surface_priv->surfy, surface_priv->surfw, surface_priv->surfh};
- struct pipe_video_rect *src_rects[1] = {&src_rect};
- struct pipe_video_rect *dst_rects[1] = {&dst_rect};
XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
assert(subpicture_priv->surface == surface);
- vpipe->set_picture_layers(vpipe, &subpicture_priv->sfc, &src_rects, &dst_rects, 1);
+ if (subpicture_priv->palette)
+ compositor->set_palette_layer(compositor, 1, subpicture_priv->sampler, subpicture_priv->palette, &src_rect, &dst_rect);
+ else
+ compositor->set_rgba_layer(compositor, 1, subpicture_priv->sampler, &src_rect, &dst_rect);
surface_priv->subpicture = NULL;
subpicture_priv->surface = NULL;
}
- else
- vpipe->set_picture_layers(vpipe, NULL, NULL, NULL, 0);
- vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
- context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
+ compositor->render_picture(compositor, PictureToPipe(flags), drawable_surface, &dst_rect, &surface_priv->disp_fence);
XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
vpipe->screen->flush_frontbuffer
(
vpipe->screen,
- context_priv->backbuffer,
- displaytarget
+ 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);
+ system(cmd);
+ }
+
XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
return Success;
return XvMCBadSurface;
surface_priv = surface->privData;
- pipe_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;