va: clean the leftover of last freedesktop rebase

BZ: 104446

Change-Id: Ic21d03155eadcaa030b6e4c33a3d4b16f38bf922
Signed-off-by: Austin Yuan <[email protected]>
Reviewed-on: http://android.intel.com:8080/105680
Reviewed-by: cactus <[email protected]>
Reviewed-by: Shi, PingX <[email protected]>
Tested-by: Shi, PingX <[email protected]>
Reviewed-by: buildbot <[email protected]>
Tested-by: buildbot <[email protected]>
diff --git a/test/encode/h264encode.c b/test/encode/h264encode.c
index a9b0887..c39dcd1 100644
--- a/test/encode/h264encode.c
+++ b/test/encode/h264encode.c
@@ -70,30 +70,38 @@
 #define BITSTREAM_ALLOCATE_STEPPING     4096
 
 
-#define SRC_SURFACE_NUM 16 /* 16 surfaces for source YUV */
-#define REF_SURFACE_NUM 16 /* 16 surfaces for reference */
+#define SURFACE_NUM 16 /* 16 surfaces for source YUV */
+#define SURFACE_NUM 16 /* 16 surfaces for reference */
 static  VADisplay va_dpy;
 static  VAProfile h264_profile;
 static  VAConfigAttrib attrib[VAConfigAttribTypeMax];
 static  VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
 static  int config_attrib_num = 0;
-static  VASurfaceID src_surface[SRC_SURFACE_NUM];
-static  VABufferID coded_buf[SRC_SURFACE_NUM];
-static  VASurfaceID ref_surface[REF_SURFACE_NUM];
+static  VASurfaceID src_surface[SURFACE_NUM];
+static  VABufferID  coded_buf[SURFACE_NUM];
+static  VASurfaceID ref_surface[SURFACE_NUM];
 static  VAConfigID config_id;
 static  VAContextID context_id;
 static  VAEncSequenceParameterBufferH264 seq_param;
 static  VAEncPictureParameterBufferH264 pic_param;
 static  VAEncSliceParameterBufferH264 slice_param;
-static  VAPictureH264 LLastCurrPic={picture_id:VA_INVALID_SURFACE}, LastCurrPic={picture_id:VA_INVALID_SURFACE}, CurrentCurrPic; /* One reference for P and two for B */
+static  VAPictureH264 CurrentCurrPic;
+static  VAPictureH264 ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
 
+static  unsigned int MaxFrameNum = (2<<16);
+static  unsigned int MaxPicOrderCntLsb = (2<<8);
+static  unsigned int Log2MaxFrameNum = 16;
+static  unsigned int Log2MaxPicOrderCntLsb = 8;
+
+static  unsigned int num_ref_frames = 2;
+static  unsigned int numShortTerm = 0;
 static  int constraint_set_flag = 0;
 static  int h264_packedheader = 0; /* support pack header? */
-static  int h264_maxref = 3;
+static  int h264_maxref = (1<<16|1);
 static  char *coded_fn = NULL, *srcyuv_fn = NULL, *recyuv_fn = NULL;
 static  FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
 static  unsigned long long srcyuv_frames = 0;
-static  unsigned int srcyuv_fourcc = VA_FOURCC_NV12;
+static  int srcyuv_fourcc = VA_FOURCC_NV12;
 
 static  int frame_width = 176;
 static  int frame_height = 144;
@@ -102,17 +110,19 @@
 static  unsigned int frame_coded = 0;
 static  unsigned int frame_bitrate = 0;
 static  unsigned int frame_slices = 1;
+static  double frame_size = 0;
 static  int initial_qp = 28;
 static  int minimal_qp = 0;
 static  int intra_period = 30;
 static  int intra_idr_period = 60;
 static  int ip_period = 1;
 static  int rc_mode = VA_RC_VBR;
-static  int slice_refoverride = 1; /* use VAEncSliceParameterBufferH264:RefPicList0/1 */
 static  unsigned long long current_frame_encoding = 0;
 static  unsigned long long current_frame_display = 0;
+static  unsigned long long current_IDR_display = 0;
 static  int current_frame_num = 0;
 static  int current_frame_type;
+#define current_slot (current_frame_display % SURFACE_NUM)
 
 /* thread to save coded data/upload source YUV */
 struct storage_task_t {
@@ -123,12 +133,12 @@
 static  struct storage_task_t *storage_task_header = NULL, *storage_task_tail = NULL;
 #define SRC_SURFACE_IN_ENCODING 0
 #define SRC_SURFACE_IN_STORAGE  1
-static  int srcsurface_status[SRC_SURFACE_NUM];
+static  int srcsurface_status[SURFACE_NUM];
 static  int encode_syncmode = 0;
 static  pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
 static  pthread_cond_t  encode_cond = PTHREAD_COND_INITIALIZER;
 static  pthread_t encode_thread;
-
+    
 /* for performance profiling */
 static unsigned int UploadPictureTicks=0;
 static unsigned int BeginPictureTicks=0;
@@ -601,7 +611,7 @@
 {
     switch (fourcc) {
     case VA_FOURCC_NV12:
-        return "NONE";
+        return "NV12";
     case VA_FOURCC_IYUV:
         return "IYUV";
     case VA_FOURCC_YV12:
@@ -670,7 +680,7 @@
     else if (!strncmp(str, "VBR_CONSTRAINED", 15))
         rc_mode = VA_RC_VBR_CONSTRAINED;
     else {
-        printf("Unknow RC mode\n");
+        printf("Unknown RC mode\n");
         rc_mode = -1;
     }
     return rc_mode;
@@ -691,7 +701,6 @@
     printf("   --initialqp <number>\n");
     printf("   --minqp <number>\n");
     printf("   --rcmode <NONE|CBR|VBR|VCM|CQP|VBR_CONTRAINED>\n");
-    printf("   --refoverride: use VAEncSliceParameterBufferH264 to override reference frames\n");
     printf("   --syncmode: sequentially upload source, encoding, save result, no multi-thread\n");
     printf("   --srcyuv <filename> load YUV from a file\n");
     printf("   --fourcc <NV12|IYUV|I420|YV12> source YUV fourcc\n");
@@ -710,7 +719,6 @@
         {"idr_period", required_argument, NULL, 5 },
         {"ip_period", required_argument, NULL, 6 },
         {"rcmode", required_argument, NULL, 7 },
-        {"refoverride",  required_argument, NULL, 8 },
         {"srcyuv", required_argument, NULL, 9 },
         {"fourcc", required_argument, NULL, 10 },
         {"syncmode", no_argument, NULL, 11 },
@@ -759,9 +767,6 @@
                 exit(1);
             }
             break;
-        case 8:
-            slice_refoverride = atoi(optarg);
-            break;
         case 9:
             srcyuv_fn = strdup(optarg);
             break;
@@ -795,10 +800,9 @@
         exit(0);        
     }
 
-    if (frame_bitrate == 0) {
+    if (frame_bitrate == 0)
         frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
-        printf("Set bitrate to %dbps\n", frame_bitrate);
-    }
+        
     /* open source file */
     if (srcyuv_fn) {
         srcyuv_fp = fopen(srcyuv_fn,"r");
@@ -1031,7 +1035,7 @@
     /* create source surfaces */
     va_status = vaCreateSurfaces(va_dpy,
                                  VA_RT_FORMAT_YUV420, frame_width, frame_height,
-                                 &src_surface[0], SRC_SURFACE_NUM,
+                                 &src_surface[0], SURFACE_NUM,
                                  NULL, 0);
     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
 
@@ -1039,27 +1043,27 @@
     va_status = vaCreateSurfaces(
             va_dpy,
             VA_RT_FORMAT_YUV420, frame_width, frame_height,
-            &ref_surface[0], h264_maxref,
+            &ref_surface[0], SURFACE_NUM,
             NULL, 0
             );
     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
 
-    tmp_surfaceid = calloc(SRC_SURFACE_NUM + h264_maxref, sizeof(VASurfaceID));
-    memcpy(tmp_surfaceid, src_surface, SRC_SURFACE_NUM * sizeof(VASurfaceID));
-    memcpy(tmp_surfaceid + SRC_SURFACE_NUM, ref_surface, h264_maxref * sizeof(VASurfaceID));
+    tmp_surfaceid = calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
+    memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
+    memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
     
     /* Create a context for this encode pipe */
     va_status = vaCreateContext(va_dpy, config_id,
                                 frame_width, frame_height,
                                 VA_PROGRESSIVE,
-                                tmp_surfaceid, SRC_SURFACE_NUM + h264_maxref,
+                                tmp_surfaceid, 2 * SURFACE_NUM,
                                 &context_id);
     CHECK_VASTATUS(va_status, "vaCreateContext");
     free(tmp_surfaceid);
 
     codedbuf_size = (frame_width * frame_height * 400) / (16*16);
 
-    for (i = 0; i < SRC_SURFACE_NUM; i++) {
+    for (i = 0; i < SURFACE_NUM; i++) {
         /* create coded buffer once for all
          * other VA buffers which won't be used again after vaRenderPicture.
          * so APP can always vaCreateBuffer for every frame
@@ -1074,6 +1078,117 @@
     return 0;
 }
 
+
+
+#define partition(ref, field, key, ascending)   \
+    while (i <= j) {                            \
+        if (ascending) {                        \
+            while (ref[i].field < key)          \
+                i++;                            \
+            while (ref[j].field > key)          \
+                j--;                            \
+        } else {                                \
+            while (ref[i].field > key)          \
+                i++;                            \
+            while (ref[j].field < key)          \
+                j--;                            \
+        }                                       \
+        if (i <= j) {                           \
+            tmp = ref[i];                       \
+            ref[i] = ref[j];                    \
+            ref[j] = tmp;                       \
+            i++;                                \
+            j--;                                \
+        }                                       \
+    }                                           \
+
+static void sort_one(VAPictureH264 ref[], int left, int right,
+                     int ascending, int frame_idx)
+{
+    int i = left, j = right;
+    unsigned int key;
+    VAPictureH264 tmp;
+
+    if (frame_idx) {
+        key = ref[(left + right) / 2].frame_idx;
+        partition(ref, frame_idx, key, ascending);
+    } else {
+        key = ref[(left + right) / 2].TopFieldOrderCnt;
+        partition(ref, TopFieldOrderCnt, key, ascending);
+    }
+    
+    /* recursion */
+    if (left < j)
+        sort_one(ref, left, j, ascending, frame_idx);
+    
+    if (i < right)
+        sort_one(ref, i, right, ascending, frame_idx);
+}
+
+static void sort_two(VAPictureH264 ref[], int left, int right, int key, int frame_idx,
+                       int divide_ascending, int list0_ascending, int list1_ascending)
+{
+    int i = left, j = right;
+    VAPictureH264 tmp;
+
+    if (frame_idx) {
+        partition(ref, frame_idx, key, divide_ascending);
+    } else {
+        partition(ref, TopFieldOrderCnt, key, divide_ascending);
+    }
+    
+
+    sort_one(ref, left, i-1, list0_ascending, frame_idx);
+    sort_one(ref, j+1, right, list1_ascending, frame_idx);
+}
+
+static int update_ReferenceFrames(void)
+{
+    int i;
+    
+    if (current_frame_type == FRAME_B)
+        return 0;
+
+    CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+    numShortTerm++;
+    if (numShortTerm > num_ref_frames)
+        numShortTerm = num_ref_frames;
+    for (i=numShortTerm-1; i>0; i--)
+        ReferenceFrames[i] = ReferenceFrames[i-1];
+    ReferenceFrames[0] = CurrentCurrPic;
+    
+    if (current_frame_type != FRAME_B)
+        current_frame_num++;
+    if (current_frame_num > MaxFrameNum)
+        current_frame_num = 0;
+    
+    return 0;
+}
+
+
+static int update_RefPicList(void)
+{
+    unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
+    
+    if (current_frame_type == FRAME_P) {
+        memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
+        sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
+    }
+    
+    if (current_frame_type == FRAME_B) {
+        memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
+        sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
+                 1, 0, 1);
+
+        memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
+        sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
+                 0, 1, 0);
+    }
+    
+    return 0;
+}
+
+
 static int render_sequence(void)
 {
     VABufferID seq_param_buf, rc_param_buf, render_id[2];
@@ -1090,11 +1205,12 @@
     seq_param.intra_idr_period = intra_idr_period;
     seq_param.ip_period = ip_period;
 
-    seq_param.max_num_ref_frames = h264_maxref;
+    seq_param.max_num_ref_frames = num_ref_frames;
     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
     seq_param.time_scale = 900;
     seq_param.num_units_in_tick = 15; /* Tc = num_units_in_tick / time_sacle */
-    seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = 2;
+    seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
+    seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
     
     va_status = vaCreateBuffer(va_dpy, context_id,
@@ -1127,61 +1243,66 @@
     return 0;
 }
 
+static int calc_poc(unsigned int pic_order_cnt_lsb)
+{
+    static int PicOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
+    int prevPicOrderCntMsb, prevPicOrderCntLsb;
+    int PicOrderCntMsb, TopFieldOrderCnt;
+    
+    if (current_frame_type == FRAME_IDR)
+        prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
+    else {
+        prevPicOrderCntMsb = PicOrderCntMsb_ref;
+        prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
+    }
+    
+    if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
+        ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)))
+        PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
+    else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
+             ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)))
+        PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
+    else
+        PicOrderCntMsb = prevPicOrderCntMsb;
+    
+    TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
+
+    if (current_frame_type != FRAME_B) {
+        PicOrderCntMsb_ref = PicOrderCntMsb;
+        pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
+    }
+    
+    return TopFieldOrderCnt;
+}
+
 static int render_picture(void)
 {
     VABufferID pic_param_buf;
     VAStatus va_status;
     int i = 0;
 
-    /* use frame_num as reference frame index */
-    pic_param.CurrPic.picture_id = ref_surface[current_frame_num % h264_maxref];
-    //pic_param.CurrPic.frame_idx = current_frame_num % h264_maxref;
+    pic_param.CurrPic.picture_id = ref_surface[current_slot];
+    pic_param.CurrPic.frame_idx = current_frame_num;
     pic_param.CurrPic.flags = 0;
-    pic_param.CurrPic.TopFieldOrderCnt = 2 * current_frame_display;
-    pic_param.CurrPic.BottomFieldOrderCnt = 0;
-    if (current_frame_type != FRAME_B)
-        CurrentCurrPic = pic_param.CurrPic; /* save it */
+    pic_param.CurrPic.TopFieldOrderCnt = calc_poc((current_frame_display - current_IDR_display) % MaxPicOrderCntLsb);
+    pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
+    CurrentCurrPic = pic_param.CurrPic;
 
-    if (slice_refoverride) {
-        /* always setup all reference frame into encoder */
-        for (i = 0; i < h264_maxref; i++) {
-            pic_param.ReferenceFrames[i].picture_id = ref_surface[i];
-            pic_param.ReferenceFrames[i].frame_idx = i;
-            pic_param.ReferenceFrames[i].flags = 0;
-            if (pic_param.CurrPic.picture_id == pic_param.ReferenceFrames[i].picture_id) {
-                pic_param.ReferenceFrames[i].TopFieldOrderCnt = 2 * current_frame_encoding;
-                //pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
-                //pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
-            }
-            pic_param.ReferenceFrames[i].BottomFieldOrderCnt = 0;
-        }
-    } else  {
-        if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR)
-            i = 0;
-        else if (current_frame_type == FRAME_P) {
-            pic_param.ReferenceFrames[0] = LastCurrPic;
-            pic_param.ReferenceFrames[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-            i = 1;
-            //#if 0
-            if (LLastCurrPic.picture_id == VA_INVALID_SURFACE)
-                pic_param.ReferenceFrames[1] = LastCurrPic;
-            else
-                pic_param.ReferenceFrames[1] = LLastCurrPic;
-            pic_param.ReferenceFrames[1].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-            i = 2;
-            //#endif
+    if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
+        update_RefPicList(); /* calc RefPicList */
+        memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
+        if (current_frame_type == FRAME_P) {
+            pic_param.ReferenceFrames[0] = RefPicList0_P[0];
         } else if (current_frame_type == FRAME_B) {
-            pic_param.ReferenceFrames[0] = LLastCurrPic;
-            pic_param.ReferenceFrames[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-            pic_param.ReferenceFrames[1] = LastCurrPic;
-            pic_param.ReferenceFrames[1].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-            i = 2;
+            pic_param.ReferenceFrames[0] = RefPicList0_B[0];
+            pic_param.ReferenceFrames[1] = RefPicList1_B[0];
         }
-    }
-    
-    for (; i < REF_SURFACE_NUM; i++) {
-        pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
-        pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
+    } else {
+        memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
+        for (i = numShortTerm; i < SURFACE_NUM; i++) {
+            pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
+            pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
+        }
     }
     
     pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
@@ -1189,9 +1310,7 @@
     pic_param.pic_fields.bits.entropy_coding_mode_flag = 1;
     pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
     pic_param.frame_num = current_frame_num;
-    if (current_frame_type != FRAME_B)
-        current_frame_num++;
-    pic_param.coded_buf = coded_buf[current_frame_display % SRC_SURFACE_NUM];
+    pic_param.coded_buf = coded_buf[current_slot];
     pic_param.last_picture = (current_frame_encoding == frame_count);
     pic_param.pic_init_qp = initial_qp;
 
@@ -1385,38 +1504,43 @@
     VABufferID slice_param_buf;
     VAStatus va_status;
     int i;
+
+    update_RefPicList();
     
     /* one frame, one slice */
     slice_param.macroblock_address = 0;
     slice_param.num_macroblocks = frame_width*frame_height/(16*16); /* Measured by MB */
     slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
+    if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR) {
+        ;
+    } else if (current_frame_type == FRAME_P) {
+        int refpiclist0_max = h264_maxref & 0xffff;
+        memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
 
-    for (i = 0; i < 32; i++) {
-        slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
-        slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
-        slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
-        slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
-    }
+        for (i = refpiclist0_max; i < 32; i++) {
+            slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
+            slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
+        }
+    } else if (current_frame_type == FRAME_B) {
+        int refpiclist0_max = h264_maxref & 0xffff;
+        int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
 
-    /* cause issue on some implementation if slice_refoverride = 1 */
-    //slice_param.num_ref_idx_active_override_flag = 1;
-    if (slice_refoverride) {
-        /* set the real reference frame */
-        if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR) {
-            ;
-        } else if (current_frame_type == FRAME_P) {
-            slice_param.RefPicList0[0] = LastCurrPic;
-            slice_param.RefPicList0[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-        } else if (current_frame_type == FRAME_B) {
-            slice_param.RefPicList0[0] = LLastCurrPic;
-            slice_param.RefPicList0[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
-            slice_param.RefPicList1[0] = LastCurrPic;
-            slice_param.RefPicList1[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+        memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
+        for (i = refpiclist0_max; i < 32; i++) {
+            slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
+            slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
+        }
+
+        memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
+        for (i = refpiclist1_max; i < 32; i++) {
+            slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
+            slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
         }
     }
+
     slice_param.slice_alpha_c0_offset_div2 = 2;
     slice_param.slice_beta_offset_div2 = 2;
-    slice_param.pic_order_cnt_lsb = current_frame_display % 64;
+    slice_param.pic_order_cnt_lsb = current_frame_display % MaxPicOrderCntLsb;
     
     va_status = vaCreateBuffer(va_dpy,context_id,VAEncSliceParameterBufferType,
                                sizeof(slice_param),1,&slice_param,&slice_param_buf);
@@ -1429,25 +1553,13 @@
 }
 
 
-static int update_reflist(void)
-{
-    if (current_frame_type == FRAME_B)
-        return 0;
-
-    LLastCurrPic = LastCurrPic;
-    LastCurrPic = CurrentCurrPic;
-
-    return 0;
-}
-
-
 static int upload_source_YUV_once_for_all()
 {
     int box_width=8;
     int row_shift=0;
     int i;
 
-    for (i = 0; i < SRC_SURFACE_NUM; i++) {
+    for (i = 0; i < SURFACE_NUM; i++) {
         printf("\rLoading data into surface %d.....", i);
         upload_surface(va_dpy, src_surface[i], box_width, row_shift, 0);
 
@@ -1463,8 +1575,8 @@
 static int load_surface(VASurfaceID surface_id, unsigned long long display_order)
 {
     VAImage surface_image;
-    unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL,*V_start=NULL;
-    int Y_pitch=0, U_pitch=0, row, V_pitch;
+    unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL,*V_start=NULL, *uv_ptr;
+    int Y_pitch=0, U_pitch=0, row, V_pitch, uv_size;
     VAStatus va_status;
 
     if (srcyuv_fp == NULL)
@@ -1513,7 +1625,7 @@
     }
 
     /* copy Y plane */
-    for (row=0;row<surface_image.height;row++) {
+    for (row=0;row<frame_height;row++) {
         unsigned char *Y_row = Y_start + row * Y_pitch;
         (void)fread(Y_row, 1, surface_image.width, srcyuv_fp);
     }
@@ -1524,30 +1636,42 @@
     fseek(srcyuv_fp,
           display_order * frame_width * frame_height * 1.5 + frame_width * frame_height,
           SEEK_SET);
+
+    uv_size = 2 * (frame_width/2) * (frame_height/2);
+    uv_ptr = malloc(uv_size);
+    fread(uv_ptr, uv_size, 1, srcyuv_fp);
     
-    for (row =0; row < surface_image.height/2; row++) {
+    for (row =0; row < frame_height/2; row++) {
         unsigned char *U_row = U_start + row * U_pitch;
-        //unsigned char *V_row = V_start + row * V_pitch;
+        unsigned char *u_ptr, *v_ptr;
+        int j;
         switch (surface_image.format.fourcc) {
         case VA_FOURCC_NV12:
-            if (srcyuv_fourcc == VA_FOURCC_NV12)
-                (void)fread(U_row, 1, surface_image.width, srcyuv_fp);
-            else if (srcyuv_fourcc == VA_FOURCC_IYUV) {
-                /* tbd */
+            if (srcyuv_fourcc == VA_FOURCC_NV12) {
+                memcpy(U_row, uv_ptr + row * frame_width, frame_width);
+                break;
+            } else if (srcyuv_fourcc == VA_FOURCC_IYUV) {
+                u_ptr = uv_ptr + row * (frame_width/2);
+                v_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
+            } else if (srcyuv_fourcc == VA_FOURCC_YV12) {
+                v_ptr = uv_ptr + row * (frame_height/2);
+                u_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
+            }
+            for(j = 0; j < frame_width/2; j++) {
+                U_row[2*j] = u_ptr[j];
+                U_row[2*j+1] = v_ptr[j];
             }
             break;
+        case VA_FOURCC_IYUV:
         case VA_FOURCC_YV12:
-            /* tbd */
-            break;
         case VA_FOURCC_YUY2:
-            // see above. it is set with Y update.
-            break;
         default:
             printf("unsupported fourcc in load_surface\n");
             assert(0);
         }
     }
-        
+    free(uv_ptr);
+    
     vaUnmapBuffer(va_dpy,surface_image.buf);
 
     vaDestroyImage(va_dpy,surface_image.image_id);
@@ -1562,13 +1686,15 @@
     VAStatus va_status;
     unsigned int coded_size = 0;
 
-    va_status = vaMapBuffer(va_dpy,coded_buf[display_order % SRC_SURFACE_NUM],(void **)(&buf_list));
+    va_status = vaMapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM],(void **)(&buf_list));
     CHECK_VASTATUS(va_status,"vaMapBuffer");
     while (buf_list != NULL) {
         coded_size += fwrite(buf_list->buf, 1, buf_list->size, coded_fp);
         buf_list = (VACodedBufferSegment *) buf_list->next;
+
+        frame_size += coded_size;
     }
-    vaUnmapBuffer(va_dpy,coded_buf[display_order % SRC_SURFACE_NUM]);
+    vaUnmapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM]);
 
     printf("\r      "); /* return back to startpoint */
     switch (encode_order % 4) {
@@ -1636,7 +1762,7 @@
         storage_task_tail = tmp;
     }
 
-    srcsurface_status[display_order % SRC_SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
+    srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
     pthread_cond_signal(&encode_cond);
     
     pthread_mutex_unlock(&encode_mutex);
@@ -1644,13 +1770,13 @@
     return 0;
 }
 
-static void storage_task(unsigned long long display_order, unsigned long encode_order)
+static void storage_task(unsigned long long display_order, unsigned long long encode_order)
 {
     unsigned int tmp;
     VAStatus va_status;
     
     tmp = GetTickCount();
-    va_status = vaSyncSurface(va_dpy, src_surface[display_order % SRC_SURFACE_NUM]);
+    va_status = vaSyncSurface(va_dpy, src_surface[display_order % SURFACE_NUM]);
     CHECK_VASTATUS(va_status,"vaSyncSurface");
     SyncPictureTicks += GetTickCount() - tmp;
     tmp = GetTickCount();
@@ -1661,11 +1787,11 @@
     /* reload a new frame data */
     tmp = GetTickCount();
     if (srcyuv_fp != NULL)
-        load_surface(src_surface[display_order % SRC_SURFACE_NUM], display_order + SRC_SURFACE_NUM);
+        load_surface(src_surface[display_order % SURFACE_NUM], display_order + SURFACE_NUM);
     UploadPictureTicks += GetTickCount() - tmp;
 
     pthread_mutex_lock(&encode_mutex);
-    srcsurface_status[display_order % SRC_SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
+    srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
     pthread_mutex_unlock(&encode_mutex);
 }
 
@@ -1705,7 +1831,7 @@
     /* upload RAW YUV data into all surfaces */
     tmp = GetTickCount();
     if (srcyuv_fp != NULL) {
-        for (i = 0; i < SRC_SURFACE_NUM; i++)
+        for (i = 0; i < SURFACE_NUM; i++)
             load_surface(src_surface[i], i);
     } else
         upload_source_YUV_once_for_all();
@@ -1724,17 +1850,20 @@
     for (current_frame_encoding = 0; current_frame_encoding < frame_count; current_frame_encoding++) {
         encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
                                &current_frame_display, &current_frame_type);
-
+        if (current_frame_type == FRAME_IDR) {
+            numShortTerm = 0;
+            current_frame_num = 0;
+            current_IDR_display = current_frame_display;
+        }
         /* check if the source frame is ready */
-        while (srcsurface_status[current_frame_display % SRC_SURFACE_NUM] != SRC_SURFACE_IN_ENCODING);
-
+        while (srcsurface_status[current_slot] != SRC_SURFACE_IN_ENCODING);
         tmp = GetTickCount();
-        va_status = vaBeginPicture(va_dpy, context_id, src_surface[current_frame_display % SRC_SURFACE_NUM]);
+        va_status = vaBeginPicture(va_dpy, context_id, src_surface[current_slot]);
         CHECK_VASTATUS(va_status,"vaBeginPicture");
         BeginPictureTicks += GetTickCount() - tmp;
         
         tmp = GetTickCount();
-        if (current_frame_encoding  == 0) {
+        if (current_frame_type == FRAME_IDR) {
             render_sequence();
             render_picture();            
             if (h264_packedheader) {
@@ -1766,11 +1895,10 @@
 
         /* how to process skipped frames
            surface_status = (VASurfaceStatus) 0;
-           va_status = vaQuerySurfaceStatus(va_dpy, src_surface[i%SRC_SURFACE_NUM],&surface_status);
+           va_status = vaQuerySurfaceStatus(va_dpy, src_surface[i%SURFACE_NUM],&surface_status);
            frame_skipped = (surface_status & VASurfaceSkipped);
         */
-
-        update_reflist();        
+        update_ReferenceFrames();        
     }
 
     if (encode_syncmode == 0) {
@@ -1786,10 +1914,10 @@
 {
     int i;
     
-    vaDestroySurfaces(va_dpy,&src_surface[0],SRC_SURFACE_NUM);
-    vaDestroySurfaces(va_dpy,&ref_surface[0],h264_maxref);
+    vaDestroySurfaces(va_dpy,&src_surface[0],SURFACE_NUM);
+    vaDestroySurfaces(va_dpy,&ref_surface[0],SURFACE_NUM);
 
-    for (i = 0; i < SRC_SURFACE_NUM; i++)
+    for (i = 0; i < SURFACE_NUM; i++)
         vaDestroyBuffer(va_dpy,coded_buf[i]);
     
     vaDestroyContext(va_dpy,context_id);
@@ -1822,13 +1950,13 @@
     printf("INPUT: IpPeriod     : %d\n", ip_period);
     printf("INPUT: Initial QP   : %d\n", initial_qp);
     printf("INPUT: Min QP       : %d\n", minimal_qp);
-    printf("INPUT: Source YUV   : %s", srcyuv_fn?"FILE":"AUTO generated");
+    printf("INPUT: Source YUV   : %s", srcyuv_fp?"FILE":"AUTO generated");
     if (srcyuv_fp) 
         printf(":%s (fourcc %s)\n", srcyuv_fn, fourcc_to_string(srcyuv_fourcc));
     else
         printf("\n");
     printf("INPUT: Coded Clip   : %s\n", coded_fn);
-    if (srcyuv_fp == NULL)
+    if (recyuv_fp == NULL)
         printf("INPUT: Rec   Clip   : %s\n", "Not save reconstructed frame");
     else
         printf("INPUT: Rec   Clip   : Save reconstructed frame into %s (fourcc %s)\n", recyuv_fn,
@@ -1843,15 +1971,17 @@
 static int print_performance(unsigned int PictureCount)
 {
     unsigned int others = 0;
+    double total_size = frame_width * frame_height * 1.5 * frame_count;
 
     others = TotalTicks - UploadPictureTicks - BeginPictureTicks
         - RenderPictureTicks - EndPictureTicks - SyncPictureTicks - SavePictureTicks;
-    
+
     printf("\n\n");
 
     printf("PERFORMANCE:   Frame Rate           : %.2f fps (%d frames, %d ms (%.2f ms per frame))\n",
            (double) 1000*PictureCount / TotalTicks, PictureCount,
            TotalTicks, ((double)  TotalTicks) / (double) PictureCount);
+    printf("PERFORMANCE:   Compression ratio    : %d:1\n", (unsigned int)(total_size / frame_size));
 
     printf("PERFORMANCE:     UploadPicture      : %d ms (%.2f, %.2f%% percent)\n",
            (int) UploadPictureTicks, ((double)  UploadPictureTicks) / (double) PictureCount,
@@ -1871,11 +2001,13 @@
     printf("PERFORMANCE:     SavePicture        : %d ms (%.2f, %.2f%% percent)\n",
            (int) SavePictureTicks, ((double)  SavePictureTicks) / (double) PictureCount,
            SavePictureTicks/(double) TotalTicks/0.01);
-
     printf("PERFORMANCE:     Others             : %d ms (%.2f, %.2f%% percent)\n",
            (int) others, ((double) others) / (double) PictureCount,
            others/(double) TotalTicks/0.01);
-    
+
+    if (encode_syncmode == 0)
+        printf("(Multithread enabled, the profiling is only for reference)\n");
+        
     return 0;
 }
 
diff --git a/test/loadsurface.h b/test/loadsurface.h
old mode 100644
new mode 100755
index 6f7bce2..0755572
--- a/test/loadsurface.h
+++ b/test/loadsurface.h
@@ -210,7 +210,7 @@
         }
     }
 
-    if (getenv("AUTO_NOUV"))
+    if (getenv("AUTO_UV") == 0)
         return 0;
 
     if (getenv("AUTO_ALPHA"))
diff --git a/test/putsurface/putsurface_common.c b/test/putsurface/putsurface_common.c
old mode 100644
new mode 100755
diff --git a/va/Android.mk b/va/Android.mk
index 241f599..e4da966 100755
--- a/va/Android.mk
+++ b/va/Android.mk
@@ -63,11 +63,10 @@
 	va.h \
 	va_version.h \
 	va_dec_jpeg.h \
+	va_dec_vp8.h \
 	va_enc.h \
 	va_enc_h264.h \
 	va_enc_vp8.h \
-	va_dec_vp8.h \
-	va_dec_jpeg.h \
 	va_backend.h \
 	va_drmcommon.h \
 	va_vpp.h \
diff --git a/va/Makefile.am b/va/Makefile.am
index a0de1f9..9a3cd2f 100644
--- a/va/Makefile.am
+++ b/va/Makefile.am
@@ -47,9 +47,9 @@
 	va_dec_jpeg.h		\
 	va_dec_vp8.h		\
 	va_drmcommon.h		\
-	va_dummy.h		\
 	va_enc.h		\
 	va_enc_h264.h		\
+	va_enc_vp8.h		\
 	va_enc_mpeg2.h		\
 	va_tpi.h		\
 	va_version.h		\
diff --git a/va/android/Makefile.am b/va/android/Makefile.am
deleted file mode 100644
index 8e532ac..0000000
--- a/va/android/Makefile.am
+++ /dev/null
@@ -1,32 +0,0 @@
-# Copyright (c) 2007 Intel Corporation. All Rights Reserved.
-#
-# Permission is hereby granted, free of charge, to any person obtaining a
-# copy of this software and associated documentation files (the
-# "Software"), to deal in the Software without restriction, including
-# without limitation the rights to use, copy, modify, merge, publish,
-# distribute, sub license, and/or sell copies of the Software, and to
-# permit persons to whom the Software is furnished to do so, subject to
-# the following conditions:
-# 
-# The above copyright notice and this permission notice (including the
-# next paragraph) shall be included in all copies or substantial portions
-# of the Software.
-# 
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
-# IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
-# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-AM_CFLAGS = -DLINUX -I$(top_srcdir)/va -I$(top_srcdir)/va/x11 $(DRM_CFLAGS)
-
-noinst_LTLIBRARIES = libva_dummy.la	
-
-libva_dummy_la_LIBADD = $(LIBVA_LIBS) -ldl -ludev
-
-libva_dummyincludedir = ${includedir}/va
-
-libva_dummy_la_SOURCES = va_dummy.c drmtest.c
-
diff --git a/va/glx/va_glx_impl.c b/va/glx/va_glx_impl.c
index aacefc8..4307001 100644
--- a/va/glx/va_glx_impl.c
+++ b/va/glx/va_glx_impl.c
@@ -151,12 +151,12 @@
     dlerror();
     get_proc_func = (GLXGetProcAddressProc)
         dlsym(RTLD_DEFAULT, "glXGetProcAddress");
-    if (!dlerror())
+    if (!dlerror() && get_proc_func)
         return get_proc_func;
 
     get_proc_func = (GLXGetProcAddressProc)
         dlsym(RTLD_DEFAULT, "glXGetProcAddressARB");
-    if (!dlerror())
+    if (!dlerror() && get_proc_func)
         return get_proc_func;
 
     return get_proc_address_default;
diff --git a/va/va.c b/va/va.c
index 58d4e19..919b498 100755
--- a/va/va.c
+++ b/va/va.c
@@ -458,23 +458,29 @@
     va_infoMessage("VA-API version %s\n", VA_VERSION_S);
 
     vaStatus = va_getDriverName(dpy, &driver_name);
-    va_infoMessage("va_getDriverName() returns %d, driver_name is %s.\n", vaStatus, driver_name);
+    va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
 
     driver_name_env = getenv("LIBVA_DRIVER_NAME");
-    if (driver_name_env && geteuid() == getuid()) {
+    if ((VA_STATUS_SUCCESS == vaStatus) &&
+        driver_name_env && (geteuid() == getuid())) {
         /* Don't allow setuid apps to use LIBVA_DRIVER_NAME */
+        if (driver_name) /* memory is allocated in va_getDriverName */
+            free(driver_name);
+        
         driver_name = strdup(driver_name_env);
         vaStatus = VA_STATUS_SUCCESS;
         va_infoMessage("User requested driver '%s'\n", driver_name);
     }
 
-    if (VA_STATUS_SUCCESS == vaStatus) {
+    if ((VA_STATUS_SUCCESS == vaStatus) && (driver_name != NULL)) {
         vaStatus = va_openDriver(dpy, driver_name);
-        va_infoMessage("va_openDriver() returns %d.\n", vaStatus);
+        va_infoMessage("va_openDriver() returns %d\n", vaStatus);
 
         *major_version = VA_MAJOR_VERSION;
         *minor_version = VA_MINOR_VERSION;
-    }
+    } else
+        va_errorMessage("va_getDriverName() failed with %s,driver_name=%s\n",
+                        vaErrorStr(vaStatus), driver_name);
 
     if (driver_name)
         free(driver_name);
@@ -635,7 +641,7 @@
   vaStatus = ctx->vtable->vaCreateConfig ( ctx, profile, entrypoint, attrib_list, num_attribs, config_id );
 
   /* record the current entrypoint for further trace/fool determination */
-  VA_TRACE_FUNC(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
+  VA_TRACE_LOG(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
   VA_FOOL_FUNC(va_FoolCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
   
   return vaStatus;
@@ -766,7 +772,7 @@
                                       flag, render_targets, num_render_targets, context );
 
   /* keep current encode/decode resoluton */
-  VA_TRACE_FUNC(va_TraceCreateContext, dpy, config_id, picture_width, picture_height, flag, render_targets, num_render_targets, context);
+  VA_TRACE_LOG(va_TraceCreateContext, dpy, config_id, picture_width, picture_height, flag, render_targets, num_render_targets, context);
 
   return vaStatus;
 }
@@ -910,7 +916,7 @@
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
 
-  VA_TRACE_FUNC(va_TraceBeginPicture, dpy, context, render_target);
+  VA_TRACE_LOG(va_TraceBeginPicture, dpy, context, render_target);
   VA_FOOL_RETURN();
   
   va_status = ctx->vtable->vaBeginPicture( ctx, context, render_target );
diff --git a/va/va.h b/va/va.h
index 7f697b7..1d664ee 100755
--- a/va/va.h
+++ b/va/va.h
@@ -911,6 +911,7 @@
     VAEncPackedHeaderDataBufferType     = 26,
     VAEncMiscParameterBufferType	= 27,
     VAEncMacroblockParameterBufferType	= 28,
+    VAEncMacroblockMapBufferType        = 29,
 /* Following are video processing buffer types */
     /**
      * \brief Video processing pipeline parameter buffer.
diff --git a/va/va_dec_jpeg.h b/va/va_dec_jpeg.h
index 091a072..9936692 100644
--- a/va/va_dec_jpeg.h
+++ b/va/va_dec_jpeg.h
@@ -70,22 +70,7 @@
     }                   components[255];
     /** \brief Number of components in frame (Nf). */
     unsigned char       num_components;
-
-    /** \brief Sample precision (P). */
-    unsigned char       sample_precision;
-
-    /** @name Cropping (JPEG-2000) */
-    /**@{*/
-    unsigned char   frame_cropping_flag;
-    unsigned short  frame_crop_left_offset;
-    unsigned short  frame_crop_right_offset;
-    unsigned short  frame_crop_top_offset;
-    unsigned short  frame_crop_bottom_offset;
-    /**@}*/
-
-    /** \brief Rotation (JPEG-2000). See \c VA_ROTATION_xxx. */
-    unsigned int        rotation;
-} VAPictureParameterBufferJPEGBaseline, VAPictureParameterBufferJPEG;
+} VAPictureParameterBufferJPEGBaseline;
 
 /**
  * \brief Quantization table for JPEG decoding.
@@ -104,7 +89,7 @@
     unsigned char       load_quantiser_table[4];
     /** \brief Quanziation tables indexed by table identifier (Tqi). */
     unsigned char       quantiser_table[4][64];
-} VAIQMatrixBufferJPEGBaseline, VAIQMatrixParameterBufferJPEG;
+} VAIQMatrixBufferJPEGBaseline;
 
 /**
  * \brief Huffman table for JPEG decoding.
@@ -140,7 +125,7 @@
         unsigned char   pad[2];
         /**@}*/
     }                   huffman_table[2];
-} VAHuffmanTableBufferJPEGBaseline, VAHuffmanTableParameterBufferJPEG;
+} VAHuffmanTableBufferJPEGBaseline;
 
 /**
  * \brief Slice parameter for JPEG decoding.
@@ -180,7 +165,7 @@
     unsigned short      restart_interval;
     /** \brief Number of MCUs in a scan. */
     unsigned int        num_mcus;
-} VASliceParameterBufferJPEGBaseline, VASliceParameterBufferJPEG;
+} VASliceParameterBufferJPEGBaseline;
 
 /**@}*/
 
diff --git a/va/va_dec_vp8.h b/va/va_dec_vp8.h
index 0a16edd..a3526f7 100644
--- a/va/va_dec_vp8.h
+++ b/va/va_dec_vp8.h
@@ -154,6 +154,7 @@
     unsigned char mv_probs[2][19];
 
     VABoolCoderContextVPX bool_coder_ctx;
+
 } VAPictureParameterBufferVP8;
 
 /**
@@ -186,12 +187,6 @@
     /* Partitions */
     unsigned char num_of_partitions;
     unsigned int partition_size[9];
-    /*
-     * slice data buffer of VASliceDataBufferType is used to send the
-     * partition data. This field specifies the offset to the first byte of
-     * partition data in the buffer.
-     */
-    unsigned int partition_data_offset;
 } VASliceParameterBufferVP8;
 
 /**
diff --git a/va/va_enc_vp8.h b/va/va_enc_vp8.h
index 80011d2..31dfe3e 100644
--- a/va/va_enc_vp8.h
+++ b/va/va_enc_vp8.h
@@ -65,6 +65,7 @@
     /* keyframe maximum interval */
     unsigned int kf_max_dist;
 
+
     /* RC related fields. RC modes are set with VAConfigAttribRateControl */
     /* For VP8, CBR implies HRD conformance and VBR implies no HRD conformance */
 
@@ -85,11 +86,22 @@
     /* HRD buffer optimal fullness */
     unsigned int hrd_buf_optimal_fullness;
 
+    /* up to 3 modes are honored, quality (1), normal (2) and performance (3) */
+    unsigned char target_usage;
+    /* only valid for avbr mode */
+    unsigned int user_max_frame_size;
+    /**
+     * min number of frames for bit rate to converge. 
+     * value should be >= 100. used for avbr mode 
+     */
+    unsigned int avbr_convergence;
+
     /* reference and reconstructed frame buffers*/
     VASurfaceID reference_frames[4];
 
 } VAEncSequenceParameterBufferVP8;
 
+
 /**
  * \brief VP8 Encoding Picture Parameter Buffer Structure
  *
@@ -100,36 +112,203 @@
 {
     /* surface to store reconstructed frame  */
     VASurfaceID reconstructed_frame;
+
     /* buffer to store coded data */
     VABufferID coded_buf;
-    /* equivalent to VP8E_SET_CPUUSED */
-    unsigned int cpu_used;
 
     union {
         struct {
-	    /* force this frame to be a keyframe */
-            unsigned int force_kf		: 1;
+            /* force this frame to be a keyframe */
+            unsigned int force_kf                       : 1;
 	    /* don't reference the last frame */
-            unsigned int no_ref_last		: 1;
+            unsigned int no_ref_last                    : 1;
 	    /* don't reference the golden frame */
-            unsigned int no_ref_gf		: 1;
+            unsigned int no_ref_gf                      : 1;
 	    /* don't reference the alternate reference frame */
-            unsigned int no_ref_arf		: 1;
-	    /*  0: bicubic, 1: bilinear, other: none */
-            unsigned int recon_filter_type       :2;
-	    /*  only invalid if recon_filter_type set to bilinear.
-             *   0: no loop fitler, 1: simple loop filter */
-            unsigned int loop_filter_type           :1;
-	    /* 1: enable, 0: disabled */
-            unsigned int auto_partitions            :1;
-	    /* number of token partitions */
-            unsigned int num_token_partitions :2;
+            unsigned int no_ref_arf                     : 1;
+            /*  0: bicubic, 1: bilinear, other: none */
+            unsigned int recon_filter_type              : 2;
+            /*  0: no loop fitler, 1: simple loop filter */
+            unsigned int loop_filter_type               : 2;
+            /* 0: disabled, 1: normal, 2: simple */
+            unsigned int auto_partitions                : 1;
+            /* number of token partitions */
+            unsigned int num_token_partitions           : 2;
+
+            /** 
+             * The following fields correspond to the same VP8 syntax elements 
+             * in the frame header.
+             */
+	    /**
+             * 0: clamping of reconstruction pixels is disabled,
+             * 1: clamping enabled.
+             */
+            unsigned int clamping_type                  : 1;
+            /* indicate segmentation is enabled for the current frame. */
+            unsigned int segmentation_enabled           : 1;
+            /**
+             * Determines if the MB segmentation map is updated in the current 
+             * frame.
+             */
+            unsigned int update_mb_segmentation_map     : 1;
+            /**
+             * Indicates if the segment feature data is updated in the current 
+             * frame.
+             */
+            unsigned int update_segment_feature_data    : 1;
+            /**
+             * indicates if the MB level loop filter adjustment is enabled for 
+             * the current frame (0 off, 1 on).  
+             */
+	    unsigned int loop_filter_adj_enable         : 1;
+            /**
+             * Determines whether updated token probabilities are used only for 
+             * this frame or until further update. 
+             * It may be used by application to enable error resilient mode. 
+             * In this mode probability updates are allowed only at Key Frames.
+             */
+            unsigned int refresh_entropy_probs          : 1;
+            /**
+             * Determines if the current decoded frame refreshes the golden frame.
+             */
+            unsigned int refresh_golden_frame           : 1;
+            /** 
+             * Determines if the current decoded frame refreshes the alternate 
+             * reference frame.
+             */
+            unsigned int refresh_alternate_frame        : 1;
+            /**
+             * Determines if the current decoded frame refreshes the last frame 
+             * reference buffer.
+             */
+            unsigned int refresh_last                   : 1;
+            /**
+             * Determines if the golden reference is replaced by another reference.
+             */
+            unsigned int copy_buffer_to_golden          : 2;
+            /**
+             * Determines if the alternate reference is replaced by another reference.
+             */
+            unsigned int copy_buffer_to_alternate       : 2;
+            /** 
+             * Controls the sign of motion vectors when the golden frame is referenced.  
+             */
+            unsigned int sign_bias_golden               : 1;
+            /**
+             * Controls the sign of motion vectors when the alternate frame is 
+             * referenced. 
+             */
+	    unsigned int sign_bias_alternate            : 1;
+            /**
+             * Enables or disables the skipping of macroblocks containing no 
+             * non-zero coefficients. 
+             */
+	    unsigned int mb_no_coeff_skip               : 1;
+            /** 
+             * Enforces unconditional per-MB loop filter delta update setting frame 
+             * header flags mode_ref_lf_delta_update, all mb_mode_delta_update_flag[4], 
+             * and all ref_frame_delta_update_flag[4] to 1. 
+	     * Since loop filter deltas are not automatically refreshed to default 
+             * values at key frames, dropped frame with delta update may prevent 
+             * correct decoding from the next key frame. 
+	     * Encoder application is advised to set this flag to 1 at key frames.
+	     */
+            unsigned int forced_lf_adjustment           : 1;
+	    unsigned int reserved                       : 4;
         } bits;
         unsigned int value;
     } pic_flags;
 
+    /* quality setting, equivalent to VP8E_SET_CPUUSED */
+    unsigned int quality_setting;
+
+    /**
+     * Contains a list of 4 loop filter level values (updated value if applicable)
+     * controlling the deblocking filter strength. Each entry represents a segment.
+     * When segmentation is disabled, use entry 0. 
+     * When loop_filter_level is 0, loop filter shall be disabled. 
+     */
+    char loop_filter_level[4];
+
+    /** 
+     * Contains a list of 4 delta values for reference frame based MB-level 
+     * loop filter adjustment.  
+     * If no update, then set to 0.
+     */
+    char ref_lf_delta[4];
+
+    /**
+     * Contains a list of 4 delta values for coding mode based MB-level loop
+     * filter adjustment.  
+     * If no update, then set to 0. 
+     */
+    char mode_lf_delta[4];
+	
+    /**
+     * Controls the deblocking filter sensitivity. 
+     * Corresponds to the same VP8 syntax element in frame header.
+     */
+    unsigned char sharpness_level;
+	
+    /** 
+     * Application supplied maximum clamp value for Qindex used in quantization.  
+     * Qindex will not be allowed to exceed this value.  
+     * It has a valid range [0..127] inclusive.  
+     */
+    unsigned char clamp_qindex_high;
+	
+    /**
+     * Application supplied minimum clamp value for Qindex used in quantization.  
+     * Qindex will not be allowed to be lower than this value.  
+     * It has a valid range [0..127] inclusive.  
+     * Condition clamp_qindex_low <= clamp_qindex_high must be guaranteed, 
+     * otherwise they are ignored. 
+     */
+    unsigned char clamp_qindex_low;
+	
 } VAEncPictureParameterBufferVP8;
 
+
+/**
+ * \brief VP8 MB Segmentation ID Buffer
+ *
+ * application provides buffer containing the initial segmentation id for each 
+ * MB, in raster scan order. Rate control may reassign it.
+ * For an 640x480 video, the buffer has 1200 entries. 
+ * the value of each entry should be in the range [0..3], inclusive.
+ * If segmentation is not enabled, application does not need to provide it. 
+ */
+typedef struct _VAEncMBMapBufferVP8
+{
+    /** 
+     * number of MBs in the frame.
+     * It is also the number of entries of mb_segment_id[];
+     */
+    unsigned int num_mbs;
+    /**
+     * per MB Segmentation ID Buffer
+     */
+    unsigned char *mb_segment_id;
+} VAEncMBMapBufferVP8;
+
+
+/**
+ * \brief VP8 Quantization Matrix Buffer Structure
+ *
+ * Contains quantization indices for yac(0),ydc(1),y2dc(2),y2ac(3),uvdc(4),
+ * uvac(5) for each segment (0-3). When segmentation is disabled, only  
+ * quantization_index[0][] will be used. This structure is sent once per frame.
+ */
+typedef struct _VAQMatrixBufferVP8
+{
+    /* 
+     * array first dimensional is segment and 2nd dimensional is Q index
+     */
+    unsigned short quantization_index[4][6];
+} VAQMatrixBufferVP8;
+
+
+
 /**@}*/
 
 #ifdef __cplusplus
diff --git a/va/va_trace.c b/va/va_trace.c
index 3e02824..2d390bc 100755
--- a/va/va_trace.c
+++ b/va/va_trace.c
@@ -817,13 +817,6 @@
         va_TraceMsg(idx,"\t\t[%d] v_sampling_factor = %u\n", i, p->components[i].v_sampling_factor);
         va_TraceMsg(idx,"\t\t[%d] quantiser_table_selector = %u\n", i, p->components[i].quantiser_table_selector);
     }
-    va_TraceMsg(idx,"\tsample_precision = %u\n", p->sample_precision);
-    va_TraceMsg(idx,"\tframe_cropping_flag = %u\n", p->frame_cropping_flag);
-    va_TraceMsg(idx,"\tframe_crop_left_offset = %u\n", p->frame_crop_left_offset);
-    va_TraceMsg(idx,"\tframe_crop_right_offset = %u\n", p->frame_crop_right_offset);
-    va_TraceMsg(idx,"\tframe_crop_top_offset = %u\n", p->frame_crop_top_offset);
-    va_TraceMsg(idx,"\tframe_crop_bottom_offset = %u\n", p->frame_crop_bottom_offset);
-    va_TraceMsg(idx,"\trotation = %u\n", p->rotation);
 }
 
 static void va_TraceVAIQMatrixBufferJPEG(
@@ -1065,7 +1058,7 @@
     va_TraceMsg(idx, "VAEncPictureParameterBufferMPEG4\n");
     va_TraceMsg(idx, "\treference_picture = 0x%08x\n", p->reference_picture);
     va_TraceMsg(idx, "\treconstructed_picture = 0x%08x\n", p->reconstructed_picture);
-    va_TraceMsg(idx, "\tcoded_buf = %08x\n", p->coded_buf);
+    va_TraceMsg(idx, "\tcoded_buf = 0x%08x\n", p->coded_buf);
     va_TraceMsg(idx, "\tpicture_width = %d\n", p->picture_width);
     va_TraceMsg(idx, "\tpicture_height = %d\n", p->picture_height);
     va_TraceMsg(idx, "\tmodulo_time_base = %d\n", p->modulo_time_base);
@@ -1144,17 +1137,19 @@
     va_TraceMsg(idx, "\tCurrPic.TopFieldOrderCnt = %d\n", p->CurrPic.TopFieldOrderCnt);
     va_TraceMsg(idx, "\tCurrPic.BottomFieldOrderCnt = %d\n", p->CurrPic.BottomFieldOrderCnt);
 
-    va_TraceMsg(idx, "\tReferenceFrames (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx:\n");
+    va_TraceMsg(idx, "\tReferenceFrames (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx-flags:\n");
     for (i = 0; i < 16; i++)
     {
-        if (p->ReferenceFrames[i].flags != VA_PICTURE_H264_INVALID) {
-            va_TraceMsg(idx, "\t\t%d-%d-0x%08x-%d\n",
+        if ((p->ReferenceFrames[i].picture_id != VA_INVALID_SURFACE) &&
+            ((p->ReferenceFrames[i].flags & VA_PICTURE_H264_INVALID) == 0)) {
+            va_TraceMsg(idx, "\t\t%08d-%08d-0x%08x-%08d-0x%08x\n",
                         p->ReferenceFrames[i].TopFieldOrderCnt,
                         p->ReferenceFrames[i].BottomFieldOrderCnt,
                         p->ReferenceFrames[i].picture_id,
-                        p->ReferenceFrames[i].frame_idx);
+                        p->ReferenceFrames[i].frame_idx,
+                        p->ReferenceFrames[i].flags);
         } else
-            va_TraceMsg(idx, "\t\tinv-inv-inv-inv\n");
+            va_TraceMsg(idx, "\t\tinv-inv-inv-inv-inv\n");
     }
     va_TraceMsg(idx, "\n");
     
@@ -1229,13 +1224,13 @@
     if (p->slice_type == 0 || p->slice_type == 1) {
         va_TraceMsg(idx, "\tRefPicList0 =");
         for (i = 0; (i < p->num_ref_idx_l0_active_minus1 + 1 && i < 32); i++) {
-            va_TraceMsg(idx, "%d-%d-0x%08x-%d\n", p->RefPicList0[i].TopFieldOrderCnt, p->RefPicList0[i].BottomFieldOrderCnt, p->RefPicList0[i].picture_id, p->RefPicList0[i].frame_idx);
+            va_TraceMsg(idx, "%08d-%08d-0x%08x-%08d-0x%08x\n", p->RefPicList0[i].TopFieldOrderCnt, p->RefPicList0[i].BottomFieldOrderCnt, p->RefPicList0[i].picture_id, p->RefPicList0[i].frame_idx,  p->RefPicList0[i].flags);
         }
         if (p->slice_type == 1) {
             va_TraceMsg(idx, "\tRefPicList1 =");
             for (i = 0; (i < p->num_ref_idx_l1_active_minus1 + 1 && i < 32); i++)
             {
-                va_TraceMsg(idx, "%d-%d-0x%08x-%d\n", p->RefPicList1[i].TopFieldOrderCnt, p->RefPicList1[i].BottomFieldOrderCnt, p->RefPicList1[i].picture_id, p->RefPicList1[i].frame_idx);
+                va_TraceMsg(idx, "%08d-%08d-0x%08x-%08d-0x%08x\n", p->RefPicList1[i].TopFieldOrderCnt, p->RefPicList1[i].BottomFieldOrderCnt, p->RefPicList1[i].picture_id, p->RefPicList1[i].frame_idx, p->RefPicList1[i].flags);
             }
         }
     }
@@ -1407,17 +1402,20 @@
     va_TraceMsg(idx, "\tCurrPic.flags = %d\n", p->CurrPic.flags);
     va_TraceMsg(idx, "\tCurrPic.TopFieldOrderCnt = %d\n", p->CurrPic.TopFieldOrderCnt);
     va_TraceMsg(idx, "\tCurrPic.BottomFieldOrderCnt = %d\n", p->CurrPic.BottomFieldOrderCnt);
-    va_TraceMsg(idx, "\tReferenceFrames (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx:\n");
+    va_TraceMsg(idx, "\tReferenceFrames (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx-flags):\n");
     for (i = 0; i < 16; i++)
     {
-        if (p->ReferenceFrames[i].flags != VA_PICTURE_H264_INVALID) {
-            va_TraceMsg(idx, "\t\t%d-%d-0x%08x-%d\n",
+        if ((p->ReferenceFrames[i].picture_id != VA_INVALID_SURFACE) &&
+            ((p->ReferenceFrames[i].flags & VA_PICTURE_H264_INVALID) == 0)) {
+            va_TraceMsg(idx, "\t\t%08d-%08d-0x%08x-%08d-0x%08x\n",
                         p->ReferenceFrames[i].TopFieldOrderCnt,
                         p->ReferenceFrames[i].BottomFieldOrderCnt,
                         p->ReferenceFrames[i].picture_id,
-                        p->ReferenceFrames[i].frame_idx);
+                        p->ReferenceFrames[i].frame_idx,
+                        p->ReferenceFrames[i].flags
+                        );
         } else
-            va_TraceMsg(idx, "\t\tinv-inv-inv-inv\n");
+            va_TraceMsg(idx, "\t\tinv-inv-inv-inv-inv\n");
     }
     va_TraceMsg(idx, "\tcoded_buf = %08x\n", p->coded_buf);
     va_TraceMsg(idx, "\tpic_parameter_set_id = %d\n", p->pic_parameter_set_id);
@@ -1506,19 +1504,38 @@
     va_TraceMsg(idx, "\tslice_beta_offset_div2 = %d\n", p->slice_beta_offset_div2);
 
     if (p->slice_type == 0 || p->slice_type == 1) {
-        va_TraceMsg(idx, "\tRefPicList0 =");
-        for (i = 0; i < p->num_ref_idx_l0_active_minus1 + 1 && i < 32; i++) {
-            va_TraceMsg(idx, "%d-%d-0x%08x-%d\n", p->RefPicList0[i].TopFieldOrderCnt, p->RefPicList0[i].BottomFieldOrderCnt, p->RefPicList0[i].picture_id, p->RefPicList0[i].frame_idx);
-        }
-        if (p->slice_type == 1) {
-            va_TraceMsg(idx, "\tRefPicList1 =");
-            for (i = 0; i < p->num_ref_idx_l1_active_minus1 + 1 && i < 32; i++)
-            {
-                va_TraceMsg(idx, "%d-%d-0x%08x-%d\n", p->RefPicList1[i].TopFieldOrderCnt, p->RefPicList1[i].BottomFieldOrderCnt, p->RefPicList1[i].picture_id, p->RefPicList1[i].frame_idx);
-            }
+        va_TraceMsg(idx, "\tRefPicList0 (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx-flags):\n");
+        for (i = 0; i < 32; i++) {
+            if ((p->RefPicList0[i].picture_id != VA_INVALID_SURFACE) &&
+                ((p->RefPicList0[i].flags & VA_PICTURE_H264_INVALID) == 0))
+                va_TraceMsg(idx, "\t\t%08d-%08d-0x%08x-%08d-0x%08x\n",
+                            p->RefPicList0[i].TopFieldOrderCnt,
+                            p->RefPicList0[i].BottomFieldOrderCnt,
+                            p->RefPicList0[i].picture_id,
+                            p->RefPicList0[i].frame_idx,
+                            p->RefPicList0[i].flags);
+            else
+                break;
         }
     }
-
+    
+    if (p->slice_type == 1) {
+        va_TraceMsg(idx, "\tRefPicList1 (TopFieldOrderCnt-BottomFieldOrderCnt-picture_id-frame_idx-flags):\n");
+        for (i = 0; i < 32; i++) {
+            if ((p->RefPicList1[i].picture_id != VA_INVALID_SURFACE) &&
+                ((p->RefPicList1[i].flags & VA_PICTURE_H264_INVALID) == 0))
+                va_TraceMsg(idx, "\t\t%08d-%08d-0x%08x-%08d-0x%08d\n",
+                            p->RefPicList1[i].TopFieldOrderCnt,
+                            p->RefPicList1[i].BottomFieldOrderCnt,
+                            p->RefPicList1[i].picture_id,
+                            p->RefPicList1[i].frame_idx,
+                            p->RefPicList1[i].flags
+                            );
+            else
+                break;
+        }
+    }
+    
     va_TraceMsg(idx, "\tluma_log2_weight_denom = %d\n", p->luma_log2_weight_denom);
     va_TraceMsg(idx, "\tchroma_log2_weight_denom = %d\n", p->chroma_log2_weight_denom);
     va_TraceMsg(idx, "\tluma_weight_l0_flag = %d\n", p->luma_weight_l0_flag);
diff --git a/va/va_trace.h b/va/va_trace.h
index 6803c28..f7af9b5 100755
--- a/va/va_trace.h
+++ b/va/va_trace.h
@@ -43,17 +43,13 @@
                                        VA_TRACE_FLAG_SURFACE_ENCODE | \
                                        VA_TRACE_FLAG_SURFACE_JPEG)
 
-#define VA_TRACE_FUNC(trace_func,...)           \
-    if (trace_flag) {                           \
-        trace_func(__VA_ARGS__);                \
-    }
 #define VA_TRACE_LOG(trace_func,...)            \
-    if (trace_flag & VA_TRACE_FLAG_LOG) {            \
+    if (trace_flag & VA_TRACE_FLAG_LOG) {       \
         trace_func(__VA_ARGS__);                \
     }
-#define VA_TRACE_SURFACE(trace_func,...)        \
-    if (trace_flag & (VA_TRACE_FLAG_SURFACE | VA_TRACE_FLAG_CODEDBUF)) {  \
-        trace_func(__VA_ARGS__);                \
+#define VA_TRACE_SURFACE(trace_func,...)                                \
+    if (trace_flag & (VA_TRACE_FLAG_SURFACE | VA_TRACE_FLAG_CODEDBUF)) { \
+        trace_func(__VA_ARGS__);                                        \
     }
 
 void va_TraceInit(VADisplay dpy);
diff --git a/va/va_vpp.h b/va/va_vpp.h
index e985bba..55ab978 100644
--- a/va/va_vpp.h
+++ b/va/va_vpp.h
@@ -251,9 +251,6 @@
     VAProcFilterColorStandard,
     /** \brief Frame rate conversion. */
     VAProcFilterFrameRateConversion,
-    VAProcFilterColorEnhancement,
-    VAProcFilterProcAmp,
-    VAProcFilterComposition,
     /** \brief Skin Tone Enhancement. */
     VAProcFilterSkinToneEnhancement,
     /** \brief Total Color Correction. */
@@ -682,13 +679,11 @@
  * if this is not set then assumes top field first.
  */
 #define VA_DEINTERLACING_BOTTOM_FIELD_FIRST	0x0001
-#define VA_DEINTERLACING_INPUT_BOTTOM_FIELD_FIRST VA_DEINTERLACING_BOTTOM_FIELD_FIRST
 /** 
  * \brief Bottom field used in deinterlacing. 
  * if this is not set then assumes top field is used.
  */
 #define VA_DEINTERLACING_BOTTOM_FIELD		0x0002
-#define VA_DEINTERLACING_BOB_BOTTOM_FIELD       VA_DEINTERLACING_BOTTOM_FIELD
 /** 
  * \brief A single field is stored in the input frame. 
  * if this is not set then assumes the frame contains two interleaved fields.
@@ -841,15 +836,6 @@
     VAProcColorStandardType     type;
 } VAProcFilterCapColorStandard;
 
-/** \brief Capabilities specification for the amplifier filter. */
-typedef struct _VAProcFilterCapProcAmp
-{
-    VAProcFilterValueRange brightness_range;
-    VAProcFilterValueRange contrast_range;
-    VAProcFilterValueRange saturation_range;
-    VAProcFilterValueRange hue_range;
-} VAProcFilterCapProcAmp;
-
 /**
  * \brief Queries video processing filters.
  *
diff --git a/va/x11/dri2_util.c b/va/x11/dri2_util.c
index 0a2ac45..3d490d0 100644
--- a/va/x11/dri2_util.c
+++ b/va/x11/dri2_util.c
@@ -192,7 +192,7 @@
 
 
     if (!VA_DRI2Connect(ctx->native_dpy, RootWindow(ctx->native_dpy, ctx->x11_screen),
-                     driver_name, &device_name))
+                     driver_name, &device_name) || !device_name)
         goto err_out;
 
     dri_state->base.fd = open(device_name, O_RDWR);
@@ -216,8 +216,7 @@
     dri_state->close = dri2Close;
     gsDRI2SwapAvailable = (minor >= 2);
 
-    if (device_name)
-        Xfree(device_name);
+    Xfree(device_name);
 
     return True;
 
diff --git a/va/x11/va_fglrx.c b/va/x11/va_fglrx.c
index 77149cb..a437bce 100644
--- a/va/x11/va_fglrx.c
+++ b/va/x11/va_fglrx.c
@@ -153,27 +153,27 @@
     dlerror();
     ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE)
         dlsym(libadl_handle,"ADL_Main_Control_Create");
-    if (dlerror())
+    if (dlerror() || !ADL_Main_Control_Create)
         goto end;
 
     ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY)
         dlsym(libadl_handle,"ADL_Main_Control_Destroy");
-    if (dlerror())
+    if (dlerror() || !ADL_Main_Control_Destroy)
         goto end;
 
     ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
         dlsym(libadl_handle,"ADL_Adapter_NumberOfAdapters_Get");
-    if (dlerror())
+    if (dlerror() || !ADL_Adapter_NumberOfAdapters_Get)
         goto end;
 
     ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET)
         dlsym(libadl_handle,"ADL_Adapter_AdapterInfo_Get");
-    if (dlerror())
+    if (dlerror() || !ADL_Adapter_AdapterInfo_Get)
         goto end;
 
     ADL_Adapter_XScreenInfo_Get = (ADL_ADAPTER_XSCREENINFO_GET)
         dlsym(libadl_handle,"ADL_Adapter_XScreenInfo_Get");
-    if (dlerror())
+    if (dlerror() || !ADL_Adapter_XScreenInfo_Get)
         goto end;
 
     if (ADL_Main_Control_Create(ADL_Main_Memory_Alloc, 1) != ADL_OK)
diff --git a/va/x11/va_x11.c b/va/x11/va_x11.c
index dfb9d07..c78bd8a 100644
--- a/va/x11/va_x11.c
+++ b/va/x11/va_x11.c
@@ -256,9 +256,9 @@
   CHECK_DISPLAY(dpy);
   ctx = CTX(dpy);
   
-  VA_TRACE_FUNC(va_TracePutSurface, dpy, surface, (void *)draw, srcx, srcy, srcw, srch,
-                destx, desty, destw, desth,
-           cliprects, number_cliprects, flags );
+  VA_TRACE_LOG(va_TracePutSurface, dpy, surface, (void *)draw, srcx, srcy, srcw, srch,
+               destx, desty, destw, desth,
+               cliprects, number_cliprects, flags );
   
   return ctx->vtable->vaPutSurface( ctx, surface, (void *)draw, srcx, srcy, srcw, srch,
                                    destx, desty, destw, desth,