OSDN Git Service

VC1: No overlap smoothing if the overlap flag is 0
[android-x86/hardware-intel-common-vaapi.git] / src / gen8_mfd.c
index 3ce8ebe..398dd33 100644 (file)
@@ -74,13 +74,13 @@ gen8_mfd_init_avc_surface(VADriverContextP ctx,
 
     if (!gen7_avc_surface) {
         gen7_avc_surface = calloc(sizeof(GenAvcSurface), 1);
+        gen7_avc_surface->base.frame_store_id = -1;
         assert((obj_surface->size & 0x3f) == 0);
         obj_surface->private_data = gen7_avc_surface;
     }
 
-    gen7_avc_surface->dmv_bottom_flag = (pic_param->pic_fields.bits.field_pic_flag &&
-                                         !pic_param->seq_fields.bits.direct_8x8_inference_flag);
-
+    /* DMV buffers now relate to the whole frame, irrespective of
+       field coding modes */
     if (gen7_avc_surface->dmv_top == NULL) {
         gen7_avc_surface->dmv_top = dri_bo_alloc(i965->intel.bufmgr,
                                                  "direct mv w/r buffer",
@@ -88,15 +88,6 @@ gen8_mfd_init_avc_surface(VADriverContextP ctx,
                                                  0x1000);
         assert(gen7_avc_surface->dmv_top);
     }
-
-    if (gen7_avc_surface->dmv_bottom_flag &&
-        gen7_avc_surface->dmv_bottom == NULL) {
-        gen7_avc_surface->dmv_bottom = dri_bo_alloc(i965->intel.bufmgr,
-                                                    "direct mv w/r buffer",
-                                                    width_in_mbs * height_in_mbs * 128,                                                    
-                                                    0x1000);
-        assert(gen7_avc_surface->dmv_bottom);
-    }
 }
 
 static void
@@ -145,12 +136,16 @@ gen8_mfd_surface_state(VADriverContextP ctx,
     struct object_surface *obj_surface = decode_state->render_object;
     unsigned int y_cb_offset;
     unsigned int y_cr_offset;
+    unsigned int surface_format;
 
     assert(obj_surface);
 
     y_cb_offset = obj_surface->y_cb_offset;
     y_cr_offset = obj_surface->y_cr_offset;
 
+    surface_format = obj_surface->fourcc == VA_FOURCC_Y800 ?
+        MFX_SURFACE_MONOCHROME : MFX_SURFACE_PLANAR_420_8;
+
     BEGIN_BCS_BATCH(batch, 6);
     OUT_BCS_BATCH(batch, MFX_SURFACE_STATE | (6 - 2));
     OUT_BCS_BATCH(batch, 0);
@@ -158,7 +153,7 @@ gen8_mfd_surface_state(VADriverContextP ctx,
                   ((obj_surface->orig_height - 1) << 18) |
                   ((obj_surface->orig_width - 1) << 4));
     OUT_BCS_BATCH(batch,
-                  (MFX_SURFACE_PLANAR_420_8 << 28) | /* 420 planar YUV surface */
+                  (surface_format << 28) | /* 420 planar YUV surface */
                   ((standard_select != MFX_FORMAT_JPEG) << 27) | /* interleave chroma, set to 0 for JPEG */
                   (0 << 22) | /* surface object control state, ignored */
                   ((obj_surface->width - 1) << 3) | /* pitch */
@@ -287,7 +282,7 @@ gen8_mfd_ind_obj_base_addr_state(VADriverContextP ctx,
     OUT_BCS_BATCH(batch, 0);
     OUT_BCS_BATCH(batch, 0);
        /* Upper bound 4-5 */   
-    OUT_BCS_BATCH(batch, 0x80000000); /* must set, up to 2G */
+    OUT_BCS_BATCH(batch, 0);
     OUT_BCS_BATCH(batch, 0);
 
        /* MFX indirect MV 6-10 */
@@ -433,7 +428,7 @@ gen8_mfd_avc_img_state(VADriverContextP ctx,
     BEGIN_BCS_BATCH(batch, 17);
     OUT_BCS_BATCH(batch, MFX_AVC_IMG_STATE | (17 - 2));
     OUT_BCS_BATCH(batch, 
-                  width_in_mbs * height_in_mbs);
+                  (width_in_mbs * height_in_mbs - 1));
     OUT_BCS_BATCH(batch, 
                   ((height_in_mbs - 1) << 16) | 
                   ((width_in_mbs - 1) << 0));
@@ -495,25 +490,13 @@ gen8_mfd_avc_qm_state(VADriverContextP ctx,
     }
 }
 
-static void
+static inline void
 gen8_mfd_avc_picid_state(VADriverContextP ctx,
-                      struct decode_state *decode_state,
-                      struct gen7_mfd_context *gen7_mfd_context)
+    struct decode_state *decode_state,
+    struct gen7_mfd_context *gen7_mfd_context)
 {
-    struct intel_batchbuffer *batch = gen7_mfd_context->base.batch;
-
-    BEGIN_BCS_BATCH(batch, 10);
-    OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
-    OUT_BCS_BATCH(batch, 1); // disable Picture ID Remapping
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    ADVANCE_BCS_BATCH(batch);
+    gen75_send_avc_picid_state(gen7_mfd_context->base.batch,
+        gen7_mfd_context->reference_surface);
 }
 
 static void
@@ -527,7 +510,7 @@ gen8_mfd_avc_directmode_state(VADriverContextP ctx,
     struct object_surface *obj_surface;
     GenAvcSurface *gen7_avc_surface;
     VAPictureH264 *va_pic;
-    int i, j;
+    int i;
 
     BEGIN_BCS_BATCH(batch, 71);
     OUT_BCS_BATCH(batch, MFX_AVC_DIRECTMODE_STATE | (71 - 2));
@@ -568,26 +551,14 @@ gen8_mfd_avc_directmode_state(VADriverContextP ctx,
 
     /* POC List */
     for (i = 0; i < ARRAY_ELEMS(gen7_mfd_context->reference_surface); i++) {
-        if (gen7_mfd_context->reference_surface[i].surface_id != VA_INVALID_ID) {
-            int found = 0;
-
-            assert(gen7_mfd_context->reference_surface[i].obj_surface != NULL);
-
-            for (j = 0; j < ARRAY_ELEMS(pic_param->ReferenceFrames); j++) {
-                va_pic = &pic_param->ReferenceFrames[j];
-                
-                if (va_pic->flags & VA_PICTURE_H264_INVALID)
-                    continue;
+        obj_surface = gen7_mfd_context->reference_surface[i].obj_surface;
 
-                if (va_pic->picture_id == gen7_mfd_context->reference_surface[i].surface_id) {
-                    found = 1;
-                    break;
-                }
-            }
+        if (obj_surface) {
+            const VAPictureH264 * const va_pic = avc_find_picture(
+                obj_surface->base.id, pic_param->ReferenceFrames,
+                ARRAY_ELEMS(pic_param->ReferenceFrames));
 
-            assert(found == 1);
-            assert(!(va_pic->flags & VA_PICTURE_H264_INVALID));
-            
+            assert(va_pic != NULL);
             OUT_BCS_BATCH(batch, va_pic->TopFieldOrderCnt);
             OUT_BCS_BATCH(batch, va_pic->BottomFieldOrderCnt);
         } else {
@@ -604,6 +575,15 @@ gen8_mfd_avc_directmode_state(VADriverContextP ctx,
 }
 
 static void
+gen8_mfd_avc_phantom_slice_first(VADriverContextP ctx,
+                                 VAPictureParameterBufferH264 *pic_param,
+                                 VASliceParameterBufferH264 *next_slice_param,
+                                 struct gen7_mfd_context *gen7_mfd_context)
+{
+    gen6_mfd_avc_phantom_slice(ctx, pic_param, next_slice_param, gen7_mfd_context->base.batch);
+}
+
+static void
 gen8_mfd_avc_slice_state(VADriverContextP ctx,
                          VAPictureParameterBufferH264 *pic_param,
                          VASliceParameterBufferH264 *slice_param,
@@ -835,7 +815,8 @@ gen8_mfd_avc_decode_init(VADriverContextP ctx,
 
     assert(decode_state->pic_param && decode_state->pic_param->buffer);
     pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
-    intel_update_avc_frame_store_index(ctx, decode_state, pic_param, gen7_mfd_context->reference_surface);
+    gen75_update_avc_frame_store_index(ctx, decode_state, pic_param,
+        gen7_mfd_context->reference_surface);
     width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
     height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1;
     assert(width_in_mbs > 0 && width_in_mbs <= 256); /* 4K */
@@ -843,20 +824,12 @@ gen8_mfd_avc_decode_init(VADriverContextP ctx,
 
     /* Current decoded picture */
     obj_surface = decode_state->render_object;
-    obj_surface->flags &= ~SURFACE_REF_DIS_MASK;
-    obj_surface->flags |= (pic_param->pic_fields.bits.reference_pic_flag ? SURFACE_REFERENCED : 0);
-    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
-
-    /* initial uv component for YUV400 case */
-    if (pic_param->seq_fields.bits.chroma_format_idc == 0) {
-         unsigned int uv_offset = obj_surface->width * obj_surface->height; 
-         unsigned int uv_size   = obj_surface->width * obj_surface->height / 2; 
-
-         drm_intel_gem_bo_map_gtt(obj_surface->bo);
-         memset(obj_surface->bo->virtual + uv_offset, 0x80, uv_size);
-         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
-    }
+    if (pic_param->pic_fields.bits.reference_pic_flag)
+        obj_surface->flags |= SURFACE_REFERENCED;
+    else
+        obj_surface->flags &= ~SURFACE_REFERENCED;
 
+    avc_ensure_surface_bo(ctx, decode_state, obj_surface, pic_param);
     gen8_mfd_init_avc_surface(ctx, pic_param, obj_surface);
 
     dri_bo_unreference(gen7_mfd_context->post_deblocking_output.bo);
@@ -930,8 +903,8 @@ gen8_mfd_avc_decode_picture(VADriverContextP ctx,
     gen8_mfd_pipe_buf_addr_state(ctx, decode_state, MFX_FORMAT_AVC, gen7_mfd_context);
     gen8_mfd_bsp_buf_base_addr_state(ctx, decode_state, MFX_FORMAT_AVC, gen7_mfd_context);
     gen8_mfd_avc_qm_state(ctx, decode_state, gen7_mfd_context);
-    gen8_mfd_avc_img_state(ctx, decode_state, gen7_mfd_context);
     gen8_mfd_avc_picid_state(ctx, decode_state, gen7_mfd_context);
+    gen8_mfd_avc_img_state(ctx, decode_state, gen7_mfd_context);
 
     for (j = 0; j < decode_state->num_slice_params; j++) {
         assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
@@ -944,6 +917,9 @@ gen8_mfd_avc_decode_picture(VADriverContextP ctx,
         else
             next_slice_group_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j + 1]->buffer;
 
+        if (j == 0 && slice_param->first_mb_in_slice)
+            gen8_mfd_avc_phantom_slice_first(ctx, pic_param, slice_param, gen7_mfd_context); 
+
         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
             assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
             assert((slice_param->slice_type == SLICE_TYPE_I) ||
@@ -994,7 +970,7 @@ gen8_mfd_mpeg2_decode_init(VADriverContextP ctx,
 
     /* Current decoded picture */
     obj_surface = decode_state->render_object;
-    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
+    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
 
     dri_bo_unreference(gen7_mfd_context->pre_deblocking_output.bo);
     gen7_mfd_context->pre_deblocking_output.bo = obj_surface->bo;
@@ -1330,7 +1306,7 @@ gen8_mfd_vc1_decode_init(VADriverContextP ctx,
 
     /* Current decoded picture */
     obj_surface = decode_state->render_object;
-    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
+    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
     gen8_mfd_init_vc1_surface(ctx, pic_param, obj_surface);
 
     dri_bo_unreference(gen7_mfd_context->post_deblocking_output.bo);
@@ -1586,25 +1562,29 @@ gen8_mfd_vc1_pic_state(VADriverContextP ctx,
             brfd = 0;
     }
 
-    overlap = 0;
-    if (profile != GEN7_VC1_ADVANCED_PROFILE){
-        if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9 &&
-            pic_param->picture_fields.bits.picture_type != GEN7_VC1_B_PICTURE) {
-            overlap = 1; 
-        }
-    }else {
-        if (pic_param->picture_fields.bits.picture_type == GEN7_VC1_P_PICTURE &&
-             pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9){
-              overlap = 1; 
-        }
-        if (pic_param->picture_fields.bits.picture_type == GEN7_VC1_I_PICTURE ||
-            pic_param->picture_fields.bits.picture_type == GEN7_VC1_BI_PICTURE){
-             if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9){
-                overlap = 1; 
-             } else if (va_to_gen7_vc1_condover[pic_param->conditional_overlap_flag] == 2 ||
-                        va_to_gen7_vc1_condover[pic_param->conditional_overlap_flag] == 3) {
-                 overlap = 1;
-             }
+    overlap = pic_param->sequence_fields.bits.overlap;
+
+    if (overlap) {
+        overlap = 0;
+        if (profile != GEN7_VC1_ADVANCED_PROFILE){
+            if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9 &&
+                pic_param->picture_fields.bits.picture_type != GEN7_VC1_B_PICTURE) {
+                overlap = 1;
+            }
+        }else {
+            if (pic_param->picture_fields.bits.picture_type == GEN7_VC1_P_PICTURE &&
+                pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9){
+                overlap = 1;
+            }
+            if (pic_param->picture_fields.bits.picture_type == GEN7_VC1_I_PICTURE ||
+                pic_param->picture_fields.bits.picture_type == GEN7_VC1_BI_PICTURE){
+                if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9){
+                    overlap = 1;
+                } else if (va_to_gen7_vc1_condover[pic_param->conditional_overlap_flag] == 2 ||
+                           va_to_gen7_vc1_condover[pic_param->conditional_overlap_flag] == 3) {
+                    overlap = 1;
+                }
+            }
         }
     } 
 
@@ -1886,7 +1866,7 @@ gen8_mfd_jpeg_decode_init(VADriverContextP ctx,
     struct object_surface *obj_surface;
     VAPictureParameterBufferJPEGBaseline *pic_param;
     int subsampling = SUBSAMPLE_YUV420;
-    int fourcc = VA_FOURCC('I', 'M', 'C', '3');
+    int fourcc = VA_FOURCC_IMC3;
 
     pic_param = (VAPictureParameterBufferJPEGBaseline *)decode_state->pic_param->buffer;
 
@@ -1903,31 +1883,31 @@ gen8_mfd_jpeg_decode_init(VADriverContextP ctx,
         if (h1 == 2 && h2 == 1 && h3 == 1 &&
             v1 == 2 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV420;
-            fourcc = VA_FOURCC('I', 'M', 'C', '3');
+            fourcc = VA_FOURCC_IMC3;
         } else if (h1 == 2 && h2 == 1 && h3 == 1 &&
                    v1 == 1 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV422H;
-            fourcc = VA_FOURCC('4', '2', '2', 'H');
+            fourcc = VA_FOURCC_422H;
         } else if (h1 == 1 && h2 == 1 && h3 == 1 &&
                    v1 == 1 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV444;
-            fourcc = VA_FOURCC('4', '4', '4', 'P');
+            fourcc = VA_FOURCC_444P;
         } else if (h1 == 4 && h2 == 1 && h3 == 1 &&
                    v1 == 1 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV411;
-            fourcc = VA_FOURCC('4', '1', '1', 'P');
+            fourcc = VA_FOURCC_411P;
         } else if (h1 == 1 && h2 == 1 && h3 == 1 &&
                    v1 == 2 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV422V;
-            fourcc = VA_FOURCC('4', '2', '2', 'V');
+            fourcc = VA_FOURCC_422V;
         } else if (h1 == 2 && h2 == 1 && h3 == 1 &&
                    v1 == 2 && v2 == 2 && v3 == 2) {
             subsampling = SUBSAMPLE_YUV422H;
-            fourcc = VA_FOURCC('4', '2', '2', 'H');
+            fourcc = VA_FOURCC_422H;
         } else if (h2 == 2 && h2 == 2 && h3 == 2 &&
                    v1 == 2 && v2 == 1 && v3 == 1) {
             subsampling = SUBSAMPLE_YUV422V;
-            fourcc = VA_FOURCC('4', '2', '2', 'V');
+            fourcc = VA_FOURCC_422V;
         } else
             assert(0);
     }
@@ -2180,18 +2160,6 @@ gen8_mfd_jpeg_bsd_object(VADriverContextP ctx,
 /* Workaround for JPEG decoding on Ivybridge */
 #ifdef JPEG_WA
 
-VAStatus 
-i965_DestroySurfaces(VADriverContextP ctx,
-                     VASurfaceID *surface_list,
-                     int num_surfaces);
-VAStatus 
-i965_CreateSurfaces(VADriverContextP ctx,
-                    int width,
-                    int height,
-                    int format,
-                    int num_surfaces,
-                    VASurfaceID *surfaces);
-
 static struct {
     int width;
     int height;
@@ -2234,7 +2202,7 @@ gen8_jpeg_wa_init(VADriverContextP ctx,
 
     obj_surface = SURFACE(gen7_mfd_context->jpeg_wa_surface_id);
     assert(obj_surface);
-    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N', 'V', '1', '2'), SUBSAMPLE_YUV420);
+    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
     gen7_mfd_context->jpeg_wa_surface_object = obj_surface;
 
     if (!gen7_mfd_context->jpeg_wa_slice_data_bo) {
@@ -2529,7 +2497,7 @@ gen8_jpeg_wa_ind_obj_base_addr_state(VADriverContextP ctx,
                   gen7_mfd_context->jpeg_wa_slice_data_bo,
                   I915_GEM_DOMAIN_INSTRUCTION, 0,
                   0);
-    OUT_BCS_BATCH(batch, 0x80000000); /* must set, up to 2G */
+    OUT_BCS_BATCH(batch, 0);
     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
     OUT_BCS_BATCH(batch, 0);
     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
@@ -2770,9 +2738,14 @@ gen8_mfd_vp8_decode_init(VADriverContextP ctx,
     assert(width_in_mbs > 0 && width_in_mbs <= 256); /* 4K */
     assert(height_in_mbs > 0 && height_in_mbs <= 256);
 
+    intel_update_vp8_frame_store_index(ctx,
+                                       decode_state,
+                                       pic_param,
+                                       gen7_mfd_context->reference_surface);
+
     /* Current decoded picture */
     obj_surface = decode_state->render_object;
-    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
+    i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
 
     dri_bo_unreference(gen7_mfd_context->post_deblocking_output.bo);
     gen7_mfd_context->post_deblocking_output.bo = obj_surface->bo;
@@ -2784,6 +2757,9 @@ gen8_mfd_vp8_decode_init(VADriverContextP ctx,
     dri_bo_reference(gen7_mfd_context->pre_deblocking_output.bo);
     gen7_mfd_context->pre_deblocking_output.valid = pic_param->pic_fields.bits.loop_filter_disable;
 
+    intel_ensure_vp8_segmentation_buffer(ctx,
+        &gen7_mfd_context->segmentation_buffer, width_in_mbs, height_in_mbs);
+
     /* The same as AVC */
     dri_bo_unreference(gen7_mfd_context->intra_row_store_scratch_buffer.bo);
     bo = dri_bo_alloc(i965->intel.bufmgr,
@@ -2837,6 +2813,13 @@ gen8_mfd_vp8_pic_state(VADriverContextP ctx,
     int i, j,log2num;
     unsigned int quantization_value[4][6];
 
+    /* There is no safe way to error out if the segmentation buffer
+       could not be allocated. So, instead of aborting, simply decode
+       something even if the result may look totally inacurate */
+    const unsigned int enable_segmentation =
+        pic_param->pic_fields.bits.segmentation_enabled &&
+        gen7_mfd_context->segmentation_buffer.valid;
+        
     log2num = (int)log2(slice_param->num_of_partitions - 1);
 
     BEGIN_BCS_BATCH(batch, 38);
@@ -2853,9 +2836,11 @@ gen8_mfd_vp8_pic_state(VADriverContextP ctx,
                   pic_param->pic_fields.bits.mb_no_coeff_skip << 10 |
                   pic_param->pic_fields.bits.update_mb_segmentation_map << 9 |
                   pic_param->pic_fields.bits.segmentation_enabled << 8 |
-                  0 << 7 | /* segmentation id streamin disabled */
-                  0 << 6 | /* segmentation id streamout disabled */
-                  pic_param->pic_fields.bits.key_frame << 5 |
+                  (enable_segmentation &&
+                   !pic_param->pic_fields.bits.update_mb_segmentation_map) << 7 |
+                  (enable_segmentation &&
+                   pic_param->pic_fields.bits.update_mb_segmentation_map) << 6 |
+                  (pic_param->pic_fields.bits.key_frame == 0 ? 1 : 0) << 5 |    /* 0 indicate an intra frame in VP8 stream/spec($9.1)*/
                   pic_param->pic_fields.bits.filter_type << 4 |
                   (pic_param->pic_fields.bits.version == 3) << 1 | /* full pixel mode for version 3 */
                   !!pic_param->pic_fields.bits.version << 0); /* version 0: 6 tap */
@@ -2937,21 +2922,30 @@ gen8_mfd_vp8_pic_state(VADriverContextP ctx,
     }
 
     OUT_BCS_BATCH(batch,
-                  pic_param->loop_filter_deltas_ref_frame[3] << 24 |
-                  pic_param->loop_filter_deltas_ref_frame[2] << 16 |
-                  pic_param->loop_filter_deltas_ref_frame[1] <<  8 |
-                  pic_param->loop_filter_deltas_ref_frame[0] <<  0);
+                  (pic_param->loop_filter_deltas_ref_frame[3] & 0x7f) << 24 |
+                  (pic_param->loop_filter_deltas_ref_frame[2] & 0x7f) << 16 |
+                  (pic_param->loop_filter_deltas_ref_frame[1] & 0x7f) <<  8 |
+                  (pic_param->loop_filter_deltas_ref_frame[0] & 0x7f) <<  0);
 
     OUT_BCS_BATCH(batch,
-                  pic_param->loop_filter_deltas_mode[3] << 24 |
-                  pic_param->loop_filter_deltas_mode[2] << 16 |
-                  pic_param->loop_filter_deltas_mode[1] <<  8 |
-                  pic_param->loop_filter_deltas_mode[0] <<  0);
+                  (pic_param->loop_filter_deltas_mode[3] & 0x7f) << 24 |
+                  (pic_param->loop_filter_deltas_mode[2] & 0x7f) << 16 |
+                  (pic_param->loop_filter_deltas_mode[1] & 0x7f) <<  8 |
+                  (pic_param->loop_filter_deltas_mode[0] & 0x7f) <<  0);
 
     /* segmentation id stream base address, DW35-DW37 */
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
-    OUT_BCS_BATCH(batch, 0);
+    if (enable_segmentation) {
+        OUT_BCS_RELOC(batch, gen7_mfd_context->segmentation_buffer.bo,
+                      0, I915_GEM_DOMAIN_INSTRUCTION,
+                      0);
+        OUT_BCS_BATCH(batch, 0);
+        OUT_BCS_BATCH(batch, 0);
+    }
+    else {
+        OUT_BCS_BATCH(batch, 0);
+        OUT_BCS_BATCH(batch, 0);
+        OUT_BCS_BATCH(batch, 0);
+    }
     ADVANCE_BCS_BATCH(batch);
 }
 
@@ -2964,7 +2958,16 @@ gen8_mfd_vp8_bsd_object(VADriverContextP ctx,
 {
     struct intel_batchbuffer *batch = gen7_mfd_context->base.batch;
     int i, log2num;
-    unsigned int offset = slice_param->slice_data_offset;
+    unsigned int offset = slice_param->slice_data_offset + ((slice_param->macroblock_offset + 7 ) >> 3);
+    unsigned int used_bits = 8-pic_param->bool_coder_ctx.count;
+    unsigned int partition_size_0 = slice_param->partition_size[0];
+
+    assert(pic_param->bool_coder_ctx.count >= 0 && pic_param->bool_coder_ctx.count <= 7);
+    if (used_bits == 8) {
+        used_bits = 0;
+        offset += 1;
+        partition_size_0 -= 1;
+    }
 
     assert(slice_param->num_of_partitions >= 2);
     assert(slice_param->num_of_partitions <= 9);
@@ -2974,7 +2977,7 @@ gen8_mfd_vp8_bsd_object(VADriverContextP ctx,
     BEGIN_BCS_BATCH(batch, 22);
     OUT_BCS_BATCH(batch, MFD_VP8_BSD_OBJECT | (22 - 2));
     OUT_BCS_BATCH(batch,
-                  pic_param->bool_coder_ctx.count << 16 | /* Partition 0 CPBAC Entropy Count */
+                  used_bits << 16 | /* Partition 0 CPBAC Entropy Count */
                   pic_param->bool_coder_ctx.range <<  8 | /* Partition 0 Count Entropy Range */
                   log2num << 4 |
                   (slice_param->macroblock_offset & 0x7));
@@ -2982,7 +2985,11 @@ gen8_mfd_vp8_bsd_object(VADriverContextP ctx,
                   pic_param->bool_coder_ctx.value << 24 | /* Partition 0 Count Entropy Value */
                   0);
 
-    for (i = 0; i < 9; i++) {
+    OUT_BCS_BATCH(batch, partition_size_0);
+    OUT_BCS_BATCH(batch, offset);
+    //partion sizes in bytes are present after the above first partition when there are more than one token partition
+    offset += (partition_size_0 + 3 * (slice_param->num_of_partitions - 2));
+    for (i = 1; i < 9; i++) {
         if (i < slice_param->num_of_partitions) {
             OUT_BCS_BATCH(batch, slice_param->partition_size[i]);
             OUT_BCS_BATCH(batch, offset);
@@ -3015,12 +3022,18 @@ gen8_mfd_vp8_decode_picture(VADriverContextP ctx,
     pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
 
     /* one slice per frame */
-    assert(decode_state->num_slice_params == 1);
-    assert(decode_state->slice_params[0]->num_elements == 1);
-    assert(decode_state->slice_params && decode_state->slice_params[0]->buffer);
-    assert(decode_state->slice_datas[0]->bo);
+    if (decode_state->num_slice_params != 1 ||
+        (!decode_state->slice_params ||
+         !decode_state->slice_params[0] ||
+         (decode_state->slice_params[0]->num_elements != 1 || decode_state->slice_params[0]->buffer == NULL)) ||
+        (!decode_state->slice_datas ||
+         !decode_state->slice_datas[0] ||
+         !decode_state->slice_datas[0]->bo) ||
+        !decode_state->probability_data) {
+        WARN_ONCE("Wrong parameters for VP8 decoding\n");
 
-    assert(decode_state->probability_data);
+        return;
+    }
 
     slice_param = (VASliceParameterBufferVP8 *)decode_state->slice_params[0]->buffer;
     slice_data_bo = decode_state->slice_datas[0]->bo;
@@ -3068,6 +3081,8 @@ gen8_mfd_decode_picture(VADriverContextP ctx,
     case VAProfileH264ConstrainedBaseline:
     case VAProfileH264Main:
     case VAProfileH264High:
+    case VAProfileH264StereoHigh:
+    case VAProfileH264MultiviewHigh:
         gen8_mfd_avc_decode_picture(ctx, decode_state, gen7_mfd_context);
         break;
 
@@ -3122,6 +3137,9 @@ gen8_mfd_context_destroy(void *hw_context)
     dri_bo_unreference(gen7_mfd_context->bitplane_read_buffer.bo);
     gen7_mfd_context->bitplane_read_buffer.bo = NULL;
 
+    dri_bo_unreference(gen7_mfd_context->segmentation_buffer.bo);
+    gen7_mfd_context->segmentation_buffer.bo = NULL;
+
     dri_bo_unreference(gen7_mfd_context->jpeg_wa_slice_data_bo);
 
     intel_batchbuffer_free(gen7_mfd_context->base.batch);
@@ -3154,6 +3172,7 @@ gen8_dec_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
     }
 
     gen7_mfd_context->jpeg_wa_surface_id = VA_INVALID_SURFACE;
+    gen7_mfd_context->segmentation_buffer.valid = 0;
 
     switch (obj_config->profile) {
     case VAProfileMPEG2Simple:
@@ -3164,6 +3183,8 @@ gen8_dec_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
     case VAProfileH264ConstrainedBaseline:
     case VAProfileH264Main:
     case VAProfileH264High:
+    case VAProfileH264StereoHigh:
+    case VAProfileH264MultiviewHigh:
         gen8_mfd_avc_context_init(ctx, gen7_mfd_context);
         break;
     default: