diff -Naur a/configure b/configure
--- a/configure	2012-05-12 21:45:47.005596234 +0200
+++ b/configure	2012-05-12 21:46:03.902936436 +0200
@@ -113,6 +113,7 @@
   --enable-vdpau           enable VDPAU code [autodetect]
   --disable-dxva2          disable DXVA2 code
   --disable-vda            disable VDA code
+  --disable-xvba           disable XVBA code
   --enable-runtime-cpudetect detect cpu capabilities at runtime (bigger binary)
   --enable-hardcoded-tables use hardcoded tables instead of runtime generation
   --disable-safe-bitstream-reader
@@ -1084,6 +1085,7 @@
     vaapi
     vda
     vdpau
+    xvba
     version3
     x11grab
     zlib
@@ -1423,6 +1425,7 @@
 h264_vaapi_hwaccel_select="vaapi h264_decoder"
 h264_vda_hwaccel_deps="VideoDecodeAcceleration_VDADecoder_h pthreads"
 h264_vda_hwaccel_select="vda h264_decoder"
+h264_xvba_hwaccel_select="xvba"
 h264_vdpau_decoder_select="vdpau h264_decoder"
 imc_decoder_select="fft mdct sinewin"
 jpegls_decoder_select="golomb"
@@ -1459,6 +1462,7 @@
 mpeg4_decoder_select="h263_decoder mpeg4video_parser"
 mpeg4_encoder_select="h263_encoder"
 mpeg4_vaapi_hwaccel_select="vaapi mpeg4_decoder"
+mpeg2_xvba_hwaccel_select="xvba mpeg2video_decoder"
 mpeg4_vdpau_decoder_select="vdpau mpeg4_decoder"
 msmpeg4_crystalhd_decoder_select="crystalhd"
 msmpeg4v1_decoder_select="h263_decoder"
@@ -1501,6 +1505,7 @@
 vc1_dxva2_hwaccel_deps="dxva2api_h"
 vc1_dxva2_hwaccel_select="dxva2 vc1_decoder"
 vc1_vaapi_hwaccel_select="vaapi vc1_decoder"
+vc1_xvba_hwaccel_select="xvba vc1_decoder"
 vc1_vdpau_decoder_select="vdpau vc1_decoder"
 vc1image_decoder_select="vc1_decoder"
 vorbis_decoder_select="mdct"
@@ -1525,6 +1530,7 @@
 wmv3_vaapi_hwaccel_select="vc1_vaapi_hwaccel"
 wmv3_vdpau_decoder_select="vc1_vdpau_decoder"
 wmv3image_decoder_select="wmv3_decoder"
+wmv3_xvba_hwaccel_select="vc1_xvba_hwaccel"
 zlib_decoder_select="zlib"
 zlib_encoder_select="zlib"
 zmbv_decoder_select="zlib"
@@ -1533,6 +1539,7 @@
 crystalhd_deps="libcrystalhd_libcrystalhd_if_h"
 vaapi_deps="va_va_h"
 vda_deps="VideoDecodeAcceleration_VDADecoder_h pthreads"
+xvba_deps="amd_amdxvba_h"
 vdpau_deps="vdpau_vdpau_h vdpau_vdpau_x11_h"
 
 # parsers
@@ -3062,6 +3069,7 @@
 check_header termios.h
 check_header vdpau/vdpau.h
 check_header vdpau/vdpau_x11.h
+check_header amd/amdxvba.h
 check_cpp_condition vdpau/vdpau.h "defined(VDP_DECODER_PROFILE_MPEG4_PART2_SP)" && enable vdpau_mpeg4_support
 
 check_header X11/extensions/XvMClib.h
diff -Naur a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
--- a/libavcodec/allcodecs.c	2012-05-12 21:45:47.257601308 +0200
+++ b/libavcodec/allcodecs.c	2012-05-12 21:46:03.905936496 +0200
@@ -59,14 +59,18 @@
     REGISTER_HWACCEL (H264_VAAPI, h264_vaapi);
     REGISTER_HWACCEL (H264_VDA, h264_vda);
     REGISTER_HWACCEL (MPEG1_VDPAU, mpeg1_vdpau);
+    REGISTER_HWACCEL (H264_XVBA, h264_xvba);
     REGISTER_HWACCEL (MPEG2_DXVA2, mpeg2_dxva2);
     REGISTER_HWACCEL (MPEG2_VAAPI, mpeg2_vaapi);
     REGISTER_HWACCEL (MPEG2_VDPAU, mpeg2_vdpau);
     REGISTER_HWACCEL (MPEG4_VAAPI, mpeg4_vaapi);
+    REGISTER_HWACCEL (MPEG2_XVBA, mpeg2_xvba);
     REGISTER_HWACCEL (VC1_DXVA2, vc1_dxva2);
     REGISTER_HWACCEL (VC1_VAAPI, vc1_vaapi);
+    REGISTER_HWACCEL (VC1_XVBA, vc1_xvba);
     REGISTER_HWACCEL (WMV3_DXVA2, wmv3_dxva2);
     REGISTER_HWACCEL (WMV3_VAAPI, wmv3_vaapi);
+    REGISTER_HWACCEL (WMV3_XVBA, wmv3_xvba);
 
     /* video codecs */
     REGISTER_ENCODER (A64MULTI, a64multi);
diff -Naur a/libavcodec/h264.c b/libavcodec/h264.c
--- a/libavcodec/h264.c	2012-05-12 21:45:47.757611376 +0200
+++ b/libavcodec/h264.c	2012-05-12 21:46:03.908936558 +0200
@@ -60,6 +60,7 @@
     PIX_FMT_DXVA2_VLD,
     PIX_FMT_VAAPI_VLD,
     PIX_FMT_VDA_VLD,
+    PIX_FMT_XVBA_VLD,
     PIX_FMT_YUVJ420P,
     PIX_FMT_NONE
 };
diff -Naur a/libavcodec/Makefile b/libavcodec/Makefile
--- a/libavcodec/Makefile	2012-05-12 21:45:47.927614798 +0200
+++ b/libavcodec/Makefile	2012-05-12 21:46:03.904936476 +0200
@@ -3,7 +3,7 @@
 NAME = avcodec
 FFLIBS = avutil
 
-HEADERS = avcodec.h avfft.h dxva2.h opt.h vaapi.h vda.h vdpau.h version.h xvmc.h
+HEADERS = avcodec.h avfft.h dxva2.h opt.h vaapi.h vda.h vdpau.h version.h xvmc.h xvba.h
 
 OBJS = allcodecs.o                                                      \
        audioconvert.o                                                   \
@@ -51,6 +51,7 @@
 OBJS-$(CONFIG_VAAPI)                   += vaapi.o
 OBJS-$(CONFIG_VDA)                     += vda.o
 OBJS-$(CONFIG_VDPAU)                   += vdpau.o
+OBJS-$(CONFIG_XVBA)                    += xvba.o
 
 # decoders/encoders/hardware accelerators
 OBJS-$(CONFIG_A64MULTI_ENCODER)        += a64multienc.o elbg.o
@@ -201,6 +202,7 @@
 OBJS-$(CONFIG_H264_DXVA2_HWACCEL)      += dxva2_h264.o
 OBJS-$(CONFIG_H264_VAAPI_HWACCEL)      += vaapi_h264.o
 OBJS-$(CONFIG_H264_VDA_HWACCEL)        += vda_h264.o
+OBJS-$(CONFIG_H264_XVBA_HWACCEL)       += xvba_h264.o
 OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o
 OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o
 OBJS-$(CONFIG_IDCIN_DECODER)           += idcinvideo.o
@@ -284,6 +286,7 @@
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL)     += dxva2_mpeg2.o
 OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL)     += vaapi_mpeg2.o
+OBJS-$(CONFIG_MPEG2_XVBA_HWACCEL)      += xvba_mpeg2.o
 OBJS-$(CONFIG_MPEG2VIDEO_DECODER)      += mpeg12.o mpeg12data.o \
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_MPEG2VIDEO_ENCODER)      += mpeg12enc.o mpegvideo_enc.o \
@@ -431,6 +434,7 @@
                                           intrax8.o intrax8dsp.o
 OBJS-$(CONFIG_VC1_DXVA2_HWACCEL)       += dxva2_vc1.o
 OBJS-$(CONFIG_VC1_VAAPI_HWACCEL)       += vaapi_vc1.o
+OBJS-$(CONFIG_VC1_XVBA_HWACCEL)        += xvba_vc1.o
 OBJS-$(CONFIG_VCR1_DECODER)            += vcr1.o
 OBJS-$(CONFIG_VCR1_ENCODER)            += vcr1.o
 OBJS-$(CONFIG_VMDAUDIO_DECODER)        += vmdav.o
@@ -732,6 +736,7 @@
 SKIPHEADERS-$(CONFIG_LIBSCHROEDINGER)  += libschroedinger.h
 SKIPHEADERS-$(CONFIG_VAAPI)            += vaapi_internal.h
 SKIPHEADERS-$(CONFIG_VDA)              += vda_internal.h
+SKIPHEADERS-$(CONFIG_XVBA)             += xvba_internal.h
 SKIPHEADERS-$(CONFIG_VDPAU)            += vdpau.h
 SKIPHEADERS-$(CONFIG_XVMC)             += xvmc.h
 SKIPHEADERS-$(HAVE_W32THREADS)         += w32pthreads.h
diff -Naur a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
--- a/libavcodec/mpegvideo.c	2012-05-12 21:45:48.027616812 +0200
+++ b/libavcodec/mpegvideo.c	2012-05-12 21:46:03.910936598 +0200
@@ -136,6 +136,7 @@
     PIX_FMT_DXVA2_VLD,
     PIX_FMT_VAAPI_VLD,
     PIX_FMT_VDA_VLD,
+    PIX_FMT_XVBA_VLD,
     PIX_FMT_YUV420P,
     PIX_FMT_NONE
 };
diff -Naur a/libavcodec/xvba.c b/libavcodec/xvba.c
--- a/libavcodec/xvba.c	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba.c	2012-05-12 21:46:03.918936758 +0200
@@ -0,0 +1,66 @@
+/*
+ * HW decode acceleration for MPEG-2, H.264 and VC-1
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+/**
+ * \addtogroup XVBA_Decoding
+ *
+ * @{
+ */
+
+#include <stdint.h>
+#include "xvba.h"
+#include "xvba_internal.h"
+#include "avcodec.h"
+
+int ff_xvba_translate_profile(int profile) {
+
+  if (profile == 66)
+    return 1;
+  else if (profile == 77)
+    return 2;
+  else if (profile == 100)
+    return 3;
+  else if (profile == 0)
+    return 4;
+  else if (profile == 1)
+    return 5;
+  else if (profile == 3)
+    return 6;
+  else
+    return -1;
+}
+
+void ff_xvba_add_slice_data(struct xvba_render_state *render, const uint8_t *buffer, uint32_t size) {
+
+  render->buffers = av_fast_realloc(
+         render->buffers,
+         &render->buffers_alllocated,
+         sizeof(struct xvba_bitstream_buffers)*(render->num_slices + 1)
+  );
+
+  render->buffers[render->num_slices].buffer = buffer;
+  render->buffers[render->num_slices].size = size;
+
+  render->num_slices++;
+}
+
diff -Naur a/libavcodec/xvba.h b/libavcodec/xvba.h
--- a/libavcodec/xvba.h	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba.h	2012-05-12 21:46:03.918936758 +0200
@@ -0,0 +1,71 @@
+/*
+ * HW decode acceleration for MPEG-2, H.264 and VC-1
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_XVBA_H
+#define AVCODEC_XVBA_H
+
+#include <stdint.h>
+#include <X11/Xlib.h>
+#include <amd/amdxvba.h>
+
+
+/**
+ * \defgroup XVBA_Decoding VA API Decoding
+ * \ingroup Decoder
+ * @{
+ */
+
+/** \brief The videoSurface is used for rendering. */
+#define FF_XVBA_STATE_USED_FOR_RENDER 1
+
+/**
+ * \brief The videoSurface is needed for reference/prediction.
+ * The codec manipulates this.
+ */
+#define FF_XVBA_STATE_USED_FOR_REFERENCE 2
+
+/**
+ * \brief The videoSurface holds a decoded frame.
+ * The codec manipulates this.
+ */
+#define FF_XVBA_STATE_DECODED 4
+
+/* @} */
+
+struct xvba_bitstream_buffers
+{
+  const void *buffer;
+  unsigned int size;
+};
+
+struct xvba_render_state {
+
+  int state; ///< Holds FF_XVBA_STATE_* values.
+  void *surface;
+  XVBAPictureDescriptor *picture_descriptor;
+  XVBAQuantMatrixAvc *iq_matrix;
+  int num_slices;
+  struct xvba_bitstream_buffers *buffers;
+  uint32_t buffers_alllocated;
+};
+
+#endif /* AVCODEC_XVBA_H */
diff -Naur a/libavcodec/xvba_h264.c b/libavcodec/xvba_h264.c
--- a/libavcodec/xvba_h264.c	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba_h264.c	2012-05-12 21:46:03.919936778 +0200
@@ -0,0 +1,180 @@
+/*
+ * H.264 HW decode acceleration through XVBA
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "xvba.h"
+#include "xvba_internal.h"
+#include "h264.h"
+#include <assert.h>
+
+/** @file
+ *  This file implements the glue code between FFmpeg's and XvBA API's
+ *  structures for H.264 decoding.
+ */
+
+
+/** Initialize and start decoding a frame with XVBA. */
+static int start_frame(AVCodecContext          *avctx,
+                       av_unused const uint8_t *buffer,
+                       av_unused uint32_t       size)
+{
+  H264Context * const h = avctx->priv_data;
+  MpegEncContext * const s = &h->s;
+  struct xvba_render_state *render;
+  XVBAPictureDescriptor *pic_descriptor;
+  int i;
+
+  render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+  assert(render);
+
+  if (render->picture_descriptor == 0)
+    return -1;
+
+  pic_descriptor = render->picture_descriptor;
+
+  for (i = 0; i < 2; ++i) {
+      int foc = s->current_picture_ptr->field_poc[i];
+      if (foc == INT_MAX)
+          foc = 0;
+      pic_descriptor->avc_curr_field_order_cnt_list[i] = foc;
+  }
+
+  pic_descriptor->avc_frame_num = h->frame_num;
+
+  render->num_slices = 0;
+
+  return 0;
+}
+
+/** End a hardware decoding based frame. */
+static int end_frame(AVCodecContext *avctx)
+{
+  H264Context * const h = avctx->priv_data;
+  MpegEncContext * const s = &h->s;
+  struct xvba_render_state *render;
+  XVBAPictureDescriptor *pic_descriptor;
+  XVBAQuantMatrixAvc *iq_matrix;
+
+  render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+  assert(render);
+
+  if (render->picture_descriptor == 0 || render->iq_matrix == 0)
+    return -1;
+
+  pic_descriptor = render->picture_descriptor;
+  iq_matrix = render->iq_matrix;
+
+  av_dlog(avctx, "end_frame()\n");
+
+  /* Fill in Picture Parameters*/
+  pic_descriptor->profile                                     = ff_xvba_translate_profile(avctx->profile);
+  pic_descriptor->level                                       = avctx->level;
+  pic_descriptor->width_in_mb                                 = s->mb_width;
+  pic_descriptor->height_in_mb                                = s->mb_height;
+  pic_descriptor->picture_structure                           = s->picture_structure;
+  pic_descriptor->chroma_format                               = s->chroma_format ? s->chroma_format : 1;
+  pic_descriptor->avc_intra_flag                              = (h->slice_type == FF_I_TYPE) ? 1 : 0;
+  pic_descriptor->avc_reference                               = (s->current_picture_ptr->f.reference & 3) ? 1 : 0;
+
+  pic_descriptor->avc_bit_depth_luma_minus8                   = h->sps.bit_depth_luma - 8;
+  pic_descriptor->avc_bit_depth_chroma_minus8                 = h->sps.bit_depth_chroma - 8;
+  pic_descriptor->avc_log2_max_frame_num_minus4               = h->sps.log2_max_frame_num -4;
+  pic_descriptor->avc_pic_order_cnt_type                      = h->sps.poc_type;
+  pic_descriptor->avc_log2_max_pic_order_cnt_lsb_minus4       = h->sps.log2_max_poc_lsb - 4;
+  pic_descriptor->avc_num_ref_frames                          = h->sps.ref_frame_count;
+  pic_descriptor->avc_reserved_8bit                           = 0;
+
+  pic_descriptor->avc_num_slice_groups_minus1                 = h->pps.slice_group_count - 1;
+  pic_descriptor->avc_num_ref_idx_l0_active_minus1            = h->pps.ref_count[0] - 1;
+  pic_descriptor->avc_num_ref_idx_l1_active_minus1            = h->pps.ref_count[1] - 1;
+
+  pic_descriptor->avc_pic_init_qp_minus26                     = h->pps.init_qp - 26;
+  pic_descriptor->avc_pic_init_qs_minus26                     = h->pps.init_qs - 26;
+  pic_descriptor->avc_chroma_qp_index_offset                  = h->pps.chroma_qp_index_offset[0];
+  pic_descriptor->avc_second_chroma_qp_index_offset           = h->pps.chroma_qp_index_offset[1];
+  pic_descriptor->avc_slice_group_change_rate_minus1          = 0; // not implemented in ffmpeg
+  pic_descriptor->avc_reserved_16bit                          = 0; // must be 0
+  memset(pic_descriptor->avc_field_order_cnt_list,0,sizeof(pic_descriptor->avc_field_order_cnt_list)); // must be 0
+  memset(pic_descriptor->avc_slice_group_map,0,sizeof(pic_descriptor->avc_slice_group_map)); // must be 0
+
+  // sps
+  pic_descriptor->sps_info.avc.delta_pic_always_zero_flag     = h->sps.delta_pic_order_always_zero_flag;
+  pic_descriptor->sps_info.avc.direct_8x8_inference_flag      = h->sps.direct_8x8_inference_flag;
+  pic_descriptor->sps_info.avc.frame_mbs_only_flag            = h->sps.frame_mbs_only_flag;
+  pic_descriptor->sps_info.avc.gaps_in_frame_num_value_allowed_flag = h->sps.gaps_in_frame_num_allowed_flag;
+  pic_descriptor->sps_info.avc.mb_adaptive_frame_field_flag   = h->sps.mb_aff;
+  pic_descriptor->sps_info.avc.residual_colour_transform_flag = h->sps.residual_color_transform_flag;
+  pic_descriptor->sps_info.avc.xvba_avc_sps_reserved          = 0;
+
+  // pps
+  pic_descriptor->pps_info.avc.entropy_coding_mode_flag       = h->pps.cabac;
+  pic_descriptor->pps_info.avc.pic_order_present_flag         = h->pps.pic_order_present;
+  pic_descriptor->pps_info.avc.weighted_pred_flag             = h->pps.weighted_pred;
+  pic_descriptor->pps_info.avc.weighted_bipred_idc            = h->pps.weighted_bipred_idc;
+  pic_descriptor->pps_info.avc.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
+  pic_descriptor->pps_info.avc.constrained_intra_pred_flag    = h->pps.constrained_intra_pred;
+  pic_descriptor->pps_info.avc.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
+  pic_descriptor->pps_info.avc.transform_8x8_mode_flag        = h->pps.transform_8x8_mode;
+  pic_descriptor->pps_info.avc.xvba_avc_pps_reserved          = 0; // must be 0
+
+  memcpy(iq_matrix->bScalingLists4x4, h->pps.scaling_matrix4, sizeof(iq_matrix->bScalingLists4x4));
+  memcpy(iq_matrix->bScalingLists8x8, h->pps.scaling_matrix8, sizeof(iq_matrix->bScalingLists8x8));
+
+  // Wait for an I-frame before start decoding. Workaround for ATI UVD and UVD+ GPUs
+  if (!h->got_first_iframe) {
+      if (h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE)
+          return -1;
+      h->got_first_iframe = 1;
+  }
+
+  ff_draw_horiz_band(s, 0, s->avctx->height);
+
+  return 0;
+}
+
+/** Decode the given H.264 slice with XVBA. */
+static int decode_slice(AVCodecContext *avctx,
+                        const uint8_t  *buffer,
+                        uint32_t        size)
+{
+  H264Context * const h = avctx->priv_data;
+  MpegEncContext * const s = &h->s;
+  struct xvba_render_state *render;
+
+  render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+  assert(render);
+
+  ff_xvba_add_slice_data(render, buffer, size);
+
+  return 0;
+}
+
+AVHWAccel ff_h264_xvba_hwaccel = {
+    .name           = "h264_xvba",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_H264,
+    .pix_fmt        = PIX_FMT_XVBA_VLD,
+    .capabilities   = 0,
+    .start_frame    = start_frame,
+    .end_frame      = end_frame,
+    .decode_slice   = decode_slice,
+    .priv_data_size = 0,
+};
diff -Naur a/libavcodec/xvba_internal.h b/libavcodec/xvba_internal.h
--- a/libavcodec/xvba_internal.h	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba_internal.h	2012-05-12 21:46:03.920936798 +0200
@@ -0,0 +1,24 @@
+/*
+ * HW decode acceleration for MPEG-2, H.264 and VC-1
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+int ff_xvba_translate_profile(int profile);
+void ff_xvba_add_slice_data(struct xvba_render_state *render, const uint8_t *buffer, uint32_t size);
diff -Naur a/libavcodec/xvba_mpeg2.c b/libavcodec/xvba_mpeg2.c
--- a/libavcodec/xvba_mpeg2.c	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba_mpeg2.c	2012-05-12 21:46:03.920936798 +0200
@@ -0,0 +1,52 @@
+/*
+ * MPEG-2 HW decode acceleration through XVBA
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "dsputil.h"
+
+static int start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
+{
+    struct MpegEncContext * const s = avctx->priv_data;
+    return 0;
+}
+
+static int end_frame(AVCodecContext *avctx)
+{
+    return 0;
+}
+
+static int decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+    struct MpegEncContext * const s = avctx->priv_data;
+    return 0;
+}
+
+AVHWAccel ff_mpeg2_xvba_hwaccel = {
+    .name           = "mpeg2_xvba",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_MPEG2VIDEO,
+    .pix_fmt        = PIX_FMT_XVBA_VLD,
+    .capabilities   = 0,
+    .start_frame    = start_frame,
+    .end_frame      = end_frame,
+    .decode_slice   = decode_slice,
+    .priv_data_size = 0,
+};
diff -Naur a/libavcodec/xvba_vc1.c b/libavcodec/xvba_vc1.c
--- a/libavcodec/xvba_vc1.c	1970-01-01 01:00:00.000000000 +0100
+++ b/libavcodec/xvba_vc1.c	2012-05-12 21:46:03.921936819 +0200
@@ -0,0 +1,194 @@
+/*
+ * VC-1 HW decode acceleration through XVBA
+ *
+ * Copyright (C) 2005-2011 Team XBMC
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "xvba.h"
+#include "xvba_internal.h"
+#include "vc1.h"
+#include "vc1data.h"
+#include <assert.h>
+
+
+/** @file
+ * Implement structures of ffmpeg <-> XvBA
+ */
+
+/* Initialize and start decoding a frame with XvBA */
+static int start_frame(AVCodecContext *avctx,
+				av_unused const uint8_t *buffer, 
+				av_unused uint32_t size)
+{
+    VC1Context * const v = avctx->priv_data;
+    MpegEncContext * const s = &v->s;
+    struct xvba_render_state *render;
+
+    render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+    assert(render);
+    
+    render->num_slices = 0;
+    return 0;
+}
+
+/* End a hardware decoding based frame */
+static int end_frame(AVCodecContext *avctx)
+{
+    VC1Context* const v = avctx->priv_data;
+    MpegEncContext* const s = &v->s;
+    struct xvba_render_state *render, *last, *next;
+    XVBAPictureDescriptor *pic_descriptor;
+
+    render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+    assert(render);
+
+    if (render->picture_descriptor == 0)
+      return -1;
+
+    pic_descriptor = render->picture_descriptor;
+    
+    av_dlog(avctx, "xvba_vc1_end_frame()\n");
+    
+    memset(pic_descriptor, 0, sizeof(*pic_descriptor));
+
+    /* Fill in Parameters - for reference see AMD sdk documentation */
+    pic_descriptor->profile                                 = ff_xvba_translate_profile(v->profile);
+    pic_descriptor->level                                   = v->level;
+    //done like in va-driver and vaapi
+    if (v->profile == PROFILE_ADVANCED) {
+      pic_descriptor->width_in_mb                           = s->avctx->coded_width;
+      pic_descriptor->height_in_mb                          = s->avctx->coded_height;
+    } else {
+      pic_descriptor->width_in_mb                           = s->mb_width;
+      pic_descriptor->height_in_mb                          = s->mb_height;
+    }
+    pic_descriptor->picture_structure                       = s->picture_structure;
+    // xvba-video set this to 1 only 4:2:0 supported
+    // doc says: if not set, choose 1 - we try this
+    pic_descriptor->chroma_format                           = 1;
+    pic_descriptor->avc_intra_flag                          = s->pict_type == FF_I_TYPE || v->bi_type == 1;
+    pic_descriptor->avc_reference                           = (s->current_picture_ptr->f.reference & 3) ? 1 : 0;
+    
+    // VC-1 explicit parameters see page 30 of sdk
+    // sps_info
+    pic_descriptor->sps_info.vc1.postprocflag               = v->postprocflag;
+    
+    // done as in vaapi
+    pic_descriptor->sps_info.vc1.pulldown                   = v->broadcast;
+    pic_descriptor->sps_info.vc1.interlace                  = v->interlace;
+    pic_descriptor->sps_info.vc1.tfcntrflag                 = v->tfcntrflag;
+    pic_descriptor->sps_info.vc1.finterpflag                = v->finterpflag;
+    pic_descriptor->sps_info.vc1.reserved                   = 1;
+    // eventually check if this makes sense together with interlace
+    pic_descriptor->sps_info.vc1.psf                        = v->psf;
+    // what about if it is a frame (page 31)
+    // looked at xvba-driver
+    pic_descriptor->sps_info.vc1.second_field               = !s->first_field;
+    pic_descriptor->sps_info.vc1.xvba_vc1_sps_reserved      = 0;
+    
+    // VC-1 explicit parameters see page 30 of sdk
+    // pps_info
+    pic_descriptor->pps_info.vc1.panscan_flag               = v->panscanflag;
+    pic_descriptor->pps_info.vc1.refdist_flag               = v->refdist_flag;
+    pic_descriptor->pps_info.vc1.loopfilter                 = s->loop_filter;
+    pic_descriptor->pps_info.vc1.fastuvmc                   = v->fastuvmc;
+    pic_descriptor->pps_info.vc1.extended_mv                = v->extended_mv;
+    pic_descriptor->pps_info.vc1.dquant                     = v->dquant;
+    pic_descriptor->pps_info.vc1.vstransform                = v->vstransform;
+    pic_descriptor->pps_info.vc1.overlap                    = v->overlap;
+    pic_descriptor->pps_info.vc1.quantizer                  = v->quantizer_mode;
+    pic_descriptor->pps_info.vc1.extended_dmv               = v->extended_dmv;   
+    pic_descriptor->pps_info.vc1.maxbframes                 = s->avctx->max_b_frames;
+    pic_descriptor->pps_info.vc1.rangered                   = (pic_descriptor->profile == PROFILE_SIMPLE) ? 0 : v->rangered;   
+    pic_descriptor->pps_info.vc1.syncmarker                 = (pic_descriptor->profile == PROFILE_SIMPLE) ? 0 : s->resync_marker;
+    pic_descriptor->pps_info.vc1.multires                   = v->multires;
+    pic_descriptor->pps_info.vc1.reserved                   = 1;
+    pic_descriptor->pps_info.vc1.range_mapy_flag            = v->range_mapy_flag;
+    pic_descriptor->pps_info.vc1.range_mapy                 = v->range_mapy;
+    pic_descriptor->pps_info.vc1.range_mapuv_flag           = v->range_mapuv_flag;
+    pic_descriptor->pps_info.vc1.range_mapuv                = v->range_mapuv;
+    pic_descriptor->pps_info.vc1.xvba_vc1_pps_reserved      = 0;
+    
+    pic_descriptor->past_surface                            = 0;
+    pic_descriptor->future_surface                          = 0;
+    switch (s->pict_type) {
+    case FF_B_TYPE:
+        next = (struct xvba_render_state *)s->next_picture.f.data[0];
+        assert(next);
+        if (next)
+          pic_descriptor->past_surface = next->surface;
+        // fall-through
+    case FF_P_TYPE:
+        last = (struct xvba_render_state *)s->last_picture.f.data[0];
+        assert(last);
+        if (last)
+          pic_descriptor->future_surface = last->surface;
+        break;
+    }
+
+    ff_draw_horiz_band(s, 0, s->avctx->height);
+
+    return 0;
+}
+
+static int decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+  VC1Context* const v = avctx->priv_data;
+  MpegEncContext* const s = &v->s;
+  struct xvba_render_state *render;
+
+  render = (struct xvba_render_state *)s->current_picture_ptr->f.data[0];
+  assert(render);
+
+  if (avctx->codec_id == CODEC_ID_VC1 &&
+      size >= 4 && IS_MARKER(AV_RB32(buffer))) {
+      buffer += 4;
+      size   -= 4;
+  }
+
+  ff_xvba_add_slice_data(render, buffer, size);
+
+  return 0;
+}
+
+#if CONFIG_WMV3_XVBA_HWACCEL
+AVHWAccel ff_wmv3_xvba_hwaccel = {
+    .name           = "wmv3_xvba",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_WMV3,
+    .pix_fmt        = PIX_FMT_XVBA_VLD,
+    .capabilities   = 0,
+    .start_frame    = start_frame,
+    .end_frame      = end_frame,
+    .decode_slice   = decode_slice,
+    .priv_data_size = 0,
+};
+#endif
+
+AVHWAccel ff_vc1_xvba_hwaccel = {
+    .name           = "vc1_xvba",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_VC1,
+    .pix_fmt        = PIX_FMT_XVBA_VLD,
+    .capabilities   = 0,
+    .start_frame    = start_frame,
+    .end_frame      = end_frame,
+    .decode_slice   = decode_slice,
+    .priv_data_size = 0,
+};
diff -Naur a/libavcodec/xvmc_internal.h b/libavcodec/xvmc_internal.h
--- a/libavcodec/xvmc_internal.h	2012-05-12 21:45:48.572627784 +0200
+++ b/libavcodec/xvmc_internal.h	2012-05-12 21:46:03.922936840 +0200
@@ -1,5 +1,7 @@
 /*
- * XVideo Motion Compensation internal functions
+ * HW decode acceleration for MPEG-2, H.264 and VC-1
+ *
+ * Copyright (C) 2005-2011 Team XBMC
  *
  * This file is part of FFmpeg.
  *
diff -Naur a/libavutil/pixdesc.c b/libavutil/pixdesc.c
--- a/libavutil/pixdesc.c	2012-05-12 21:45:49.442645301 +0200
+++ b/libavutil/pixdesc.c	2012-05-12 21:46:03.923936860 +0200
@@ -874,6 +874,12 @@
         .log2_chroma_h = 1,
         .flags = PIX_FMT_HWACCEL,
     },
+    [PIX_FMT_XVBA_VLD] = {
+        .name = "xvba_vld",
+        .log2_chroma_w = 1,
+        .log2_chroma_h = 1,
+        .flags = PIX_FMT_HWACCEL,
+    },
     [PIX_FMT_YUV420P9LE] = {
         .name = "yuv420p9le",
         .nb_components = 3,
diff -Naur a/libavutil/pixfmt.h b/libavutil/pixfmt.h
--- a/libavutil/pixfmt.h	2012-05-12 21:45:49.445645361 +0200
+++ b/libavutil/pixfmt.h	2012-05-12 21:46:03.924936880 +0200
@@ -129,6 +129,7 @@
     PIX_FMT_YUV444P16BE,  ///< planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
     PIX_FMT_VDPAU_MPEG4,  ///< MPEG4 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers
     PIX_FMT_DXVA2_VLD,    ///< HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer
+    PIX_FMT_XVBA_VLD,     ///< HW decoding through DXVA2, Picture.data[3] contains a vaapi_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers
 
     PIX_FMT_RGB444LE,  ///< packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), little-endian, most significant bits to 0
     PIX_FMT_RGB444BE,  ///< packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), big-endian, most significant bits to 0
