encoder: h264: fix multiple slices support in packed headers mode.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiencoder_h264.c
index 57b004b..0af8e94 100644 (file)
@@ -1,7 +1,9 @@
 /*
  *  gstvaapiencoder_h264.c - H.264 encoder
  *
- *  Copyright (C) 2012-2013 Intel Corporation
+ *  Copyright (C) 2012-2014 Intel Corporation
+ *    Author: Wind Yuan <feng.yuan@intel.com>
+ *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public License
@@ -23,6 +25,7 @@
 #include <va/va.h>
 #include <va/va_enc_h264.h>
 #include <gst/base/gstbitwriter.h>
+#include <gst/codecparsers/gsth264parser.h>
 #include "gstvaapicompat.h"
 #include "gstvaapiencoder_priv.h"
 #include "gstvaapiencoder_h264.h"
 #define DEBUG 1
 #include "gstvaapidebug.h"
 
+/* Define the maximum number of views supported */
+#define MAX_NUM_VIEWS 2
+
 /* Define the maximum IDR period */
 #define MAX_IDR_PERIOD 512
 
+/* Default CPB length (in milliseconds) */
+#define DEFAULT_CPB_LENGTH 1500
+
+/* Scale factor for CPB size (HRD cpb_size_scale: min = 4) */
+#define SX_CPB_SIZE 4
+
+/* Scale factor for bitrate (HRD bit_rate_scale: min = 6) */
+#define SX_BITRATE 6
+
 /* Define default rate control mode ("constant-qp") */
 #define DEFAULT_RATECONTROL GST_VAAPI_RATECONTROL_CQP
 
 /* Supported set of VA rate controls, within this implementation */
 #define SUPPORTED_RATECONTROLS                          \
-  (GST_VAAPI_RATECONTROL_MASK (NONE) |                  \
-   GST_VAAPI_RATECONTROL_MASK (CQP)  |                  \
+  (GST_VAAPI_RATECONTROL_MASK (CQP)  |                  \
    GST_VAAPI_RATECONTROL_MASK (CBR)  |                  \
    GST_VAAPI_RATECONTROL_MASK (VBR)  |                  \
    GST_VAAPI_RATECONTROL_MASK (VBR_CONSTRAINED))
   (GST_VAAPI_ENCODER_TUNE_MASK (NONE) |                 \
    GST_VAAPI_ENCODER_TUNE_MASK (HIGH_COMPRESSION))
 
-#define GST_VAAPI_ENCODER_H264_NAL_REF_IDC_NONE        0
-#define GST_VAAPI_ENCODER_H264_NAL_REF_IDC_LOW         1
-#define GST_VAAPI_ENCODER_H264_NAL_REF_IDC_MEDIUM      2
-#define GST_VAAPI_ENCODER_H264_NAL_REF_IDC_HIGH        3
+/* Supported set of VA packed headers, within this implementation */
+#define SUPPORTED_PACKED_HEADERS                \
+  (VA_ENC_PACKED_HEADER_SEQUENCE |              \
+   VA_ENC_PACKED_HEADER_PICTURE  |              \
+   VA_ENC_PACKED_HEADER_SLICE    |              \
+   VA_ENC_PACKED_HEADER_RAW_DATA)
 
-typedef enum
-{
-  GST_VAAPI_ENCODER_H264_NAL_UNKNOWN = 0,
-  GST_VAAPI_ENCODER_H264_NAL_NON_IDR = 1,
-  GST_VAAPI_ENCODER_H264_NAL_IDR = 5,   /* ref_idc != 0 */
-  GST_VAAPI_ENCODER_H264_NAL_SEI = 6,   /* ref_idc == 0 */
-  GST_VAAPI_ENCODER_H264_NAL_SPS = 7,
-  GST_VAAPI_ENCODER_H264_NAL_PPS = 8
-} GstVaapiEncoderH264NalType;
+#define GST_H264_NAL_REF_IDC_NONE        0
+#define GST_H264_NAL_REF_IDC_LOW         1
+#define GST_H264_NAL_REF_IDC_MEDIUM      2
+#define GST_H264_NAL_REF_IDC_HIGH        3
 
 typedef struct
 {
@@ -82,6 +93,23 @@ typedef enum
   GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES = 2
 } GstVaapiEncH264ReorderState;
 
+typedef struct _GstVaapiH264ViewRefPool
+{
+  GQueue ref_list;
+  guint max_ref_frames;
+  guint max_reflist0_count;
+  guint max_reflist1_count;
+} GstVaapiH264ViewRefPool;
+
+typedef struct _GstVaapiH264ViewReorderPool
+{
+  GQueue reorder_frame_list;
+  guint reorder_state;
+  guint frame_index;
+  guint cur_frame_num;
+  guint cur_present_index;
+} GstVaapiH264ViewReorderPool;
+
 static inline gboolean
 _poc_greater_than (guint poc1, guint poc2, guint max_poc)
 {
@@ -94,13 +122,13 @@ h264_get_slice_type (GstVaapiPictureType type)
 {
   switch (type) {
     case GST_VAAPI_PICTURE_TYPE_I:
-      return 2;
+      return GST_H264_I_SLICE;
     case GST_VAAPI_PICTURE_TYPE_P:
-      return 0;
+      return GST_H264_P_SLICE;
     case GST_VAAPI_PICTURE_TYPE_B:
-      return 1;
+      return GST_H264_B_SLICE;
     default:
-      return -1;
+      break;
   }
   return -1;
 }
@@ -123,6 +151,31 @@ h264_get_log2_max_frame_num (guint num)
   return ret;
 }
 
+/* Determines the cpbBrNalFactor based on the supplied profile */
+static guint
+h264_get_cpb_nal_factor (GstVaapiProfile profile)
+{
+  guint f;
+
+  /* Table A-2 */
+  switch (profile) {
+    case GST_VAAPI_PROFILE_H264_HIGH:
+      f = 1500;
+      break;
+    case GST_VAAPI_PROFILE_H264_HIGH10:
+      f = 3600;
+      break;
+    case GST_VAAPI_PROFILE_H264_HIGH_422:
+    case GST_VAAPI_PROFILE_H264_HIGH_444:
+      f = 4800;
+      break;
+    default:
+      f = 1200;
+      break;
+  }
+  return f;
+}
+
 /* ------------------------------------------------------------------------- */
 /* --- H.264 Bitstream Writer                                            --- */
 /* ------------------------------------------------------------------------- */
@@ -201,6 +254,44 @@ bs_error:
   }
 }
 
+/* Write the MVC NAL unit header extension */
+static gboolean
+bs_write_nal_header_mvc_extension (GstBitWriter * bs,
+    GstVaapiEncPicture * picture, guint32 view_id)
+{
+  guint32 svc_extension_flag = 0;
+  guint32 non_idr_flag = 1;
+  guint32 priority_id = 0;
+  guint32 temporal_id = 0;
+  guint32 anchor_pic_flag = 0;
+  guint32 inter_view_flag = 0;
+
+  if (GST_VAAPI_ENC_PICTURE_IS_IDR (picture))
+    non_idr_flag = 0;
+
+  if (picture->type == GST_VAAPI_PICTURE_TYPE_I)
+    anchor_pic_flag = 1;
+  /* svc_extension_flag == 0 for mvc stream */
+  WRITE_UINT32 (bs, svc_extension_flag, 1);
+
+  WRITE_UINT32 (bs, non_idr_flag, 1);
+  WRITE_UINT32 (bs, priority_id, 6);
+  WRITE_UINT32 (bs, view_id, 10);
+  WRITE_UINT32 (bs, temporal_id, 3);
+  WRITE_UINT32 (bs, anchor_pic_flag, 1);
+  WRITE_UINT32 (bs, inter_view_flag, 1);
+  WRITE_UINT32 (bs, 1, 1);
+
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write NAL unit header");
+    return FALSE;
+  }
+}
+
 /* Write the NAL unit trailing bits */
 static gboolean
 bs_write_trailing_bits (GstBitWriter * bs)
@@ -220,8 +311,9 @@ bs_error:
 
 /* Write an SPS NAL unit */
 static gboolean
-bs_write_sps (GstBitWriter * bs,
-    const VAEncSequenceParameterBufferH264 * seq_param, GstVaapiProfile profile)
+bs_write_sps_data (GstBitWriter * bs,
+    const VAEncSequenceParameterBufferH264 * seq_param, GstVaapiProfile profile,
+    const VAEncMiscParameterHRD * hrd_params)
 {
   guint8 profile_idc;
   guint32 constraint_set0_flag, constraint_set1_flag;
@@ -265,7 +357,9 @@ bs_write_sps (GstBitWriter * bs,
   /* seq_parameter_set_id */
   WRITE_UE (bs, seq_param->seq_parameter_set_id);
 
-  if (profile == GST_VAAPI_PROFILE_H264_HIGH) {
+  if (profile == GST_VAAPI_PROFILE_H264_HIGH ||
+      profile == GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH ||
+      profile == GST_VAAPI_PROFILE_H264_STEREO_HIGH) {
     /* for high profile */
     /* chroma_format_idc  = 1, 4:2:0 */
     WRITE_UE (bs, seq_param->seq_fields.bits.chroma_format_idc);
@@ -382,22 +476,22 @@ bs_write_sps (GstBitWriter * bs,
       WRITE_UINT32 (bs, 1, 1);  /* fixed_frame_rate_flag */
     }
 
-    nal_hrd_parameters_present_flag =
-        (seq_param->bits_per_second > 0 ? TRUE : FALSE);
     /* nal_hrd_parameters_present_flag */
+    nal_hrd_parameters_present_flag = seq_param->bits_per_second > 0;
+    nal_hrd_parameters_present_flag = FALSE;    /* XXX: disabled for now */
     WRITE_UINT32 (bs, nal_hrd_parameters_present_flag, 1);
     if (nal_hrd_parameters_present_flag) {
       /* hrd_parameters */
       /* cpb_cnt_minus1 */
       WRITE_UE (bs, 0);
-      WRITE_UINT32 (bs, 4, 4);  /* bit_rate_scale */
-      WRITE_UINT32 (bs, 6, 4);  /* cpb_size_scale */
+      WRITE_UINT32 (bs, SX_BITRATE - 6, 4);     /* bit_rate_scale */
+      WRITE_UINT32 (bs, SX_CPB_SIZE - 4, 4);    /* cpb_size_scale */
 
       for (i = 0; i < 1; ++i) {
         /* bit_rate_value_minus1[0] */
-        WRITE_UE (bs, seq_param->bits_per_second / 1000 - 1);
+        WRITE_UE (bs, (seq_param->bits_per_second >> SX_BITRATE) - 1);
         /* cpb_size_value_minus1[0] */
-        WRITE_UE (bs, seq_param->bits_per_second / 1000 * 8 - 1);
+        WRITE_UE (bs, (hrd_params->buffer_size >> SX_CPB_SIZE) - 1);
         /* cbr_flag[0] */
         WRITE_UINT32 (bs, 1, 1);
       }
@@ -410,8 +504,10 @@ bs_write_sps (GstBitWriter * bs,
       /* time_offset_length  */
       WRITE_UINT32 (bs, 23, 5);
     }
+
     /* vcl_hrd_parameters_present_flag */
     WRITE_UINT32 (bs, 0, 1);
+
     if (nal_hrd_parameters_present_flag
         || 0 /*vcl_hrd_parameters_present_flag */ ) {
       /* low_delay_hrd_flag */
@@ -422,6 +518,110 @@ bs_write_sps (GstBitWriter * bs,
     /* bs_restriction_flag */
     WRITE_UINT32 (bs, 0, 1);
   }
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write SPS NAL unit");
+    return FALSE;
+  }
+}
+
+static gboolean
+bs_write_sps (GstBitWriter * bs,
+    const VAEncSequenceParameterBufferH264 * seq_param, GstVaapiProfile profile,
+    const VAEncMiscParameterHRD * hrd_params)
+{
+  if (!bs_write_sps_data (bs, seq_param, profile, hrd_params))
+    return FALSE;
+
+  /* rbsp_trailing_bits */
+  bs_write_trailing_bits (bs);
+
+  return FALSE;
+}
+
+static gboolean
+bs_write_subset_sps (GstBitWriter * bs,
+    const VAEncSequenceParameterBufferH264 * seq_param, GstVaapiProfile profile,
+    guint num_views, const VAEncMiscParameterHRD * hrd_params)
+{
+  guint32 i, j, k;
+
+  if (!bs_write_sps_data (bs, seq_param, profile, hrd_params))
+    return FALSE;
+
+  if (profile == GST_VAAPI_PROFILE_H264_STEREO_HIGH ||
+      profile == GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH) {
+    guint32 num_views_minus1, num_level_values_signalled_minus1;
+
+    num_views_minus1 = num_views - 1;
+    g_assert (num_views_minus1 < 1024);
+
+    /* bit equal to one */
+    WRITE_UINT32 (bs, 1, 1);
+
+    WRITE_UE (bs, num_views_minus1);
+
+    for (i = 0; i <= num_views_minus1; i++)
+      WRITE_UE (bs, i);
+
+    for (i = 1; i <= num_views_minus1; i++) {
+      guint32 num_anchor_refs_l0 = 15;
+      WRITE_UE (bs, num_anchor_refs_l0);
+      for (j = 0; j < num_anchor_refs_l0; j++)
+        WRITE_UE (bs, 0);
+
+      guint32 num_anchor_refs_l1 = 15;
+      WRITE_UE (bs, num_anchor_refs_l1);
+      for (j = 0; j < num_anchor_refs_l1; j++)
+        WRITE_UE (bs, 0);
+    }
+
+    for (i = 1; i <= num_views_minus1; i++) {
+      guint num_non_anchor_refs_l0 = 15;
+      WRITE_UE (bs, num_non_anchor_refs_l0);
+      for (j = 0; j < num_non_anchor_refs_l0; j++)
+        WRITE_UE (bs, 0);
+
+      guint num_non_anchor_refs_l1 = 15;
+      WRITE_UE (bs, num_non_anchor_refs_l1);
+      for (j = 0; j < num_non_anchor_refs_l1; j++)
+        WRITE_UE (bs, 0);
+    }
+
+    /* num level values signalled minus1 */
+    num_level_values_signalled_minus1 = 0;
+    g_assert (num_level_values_signalled_minus1 < 64);
+
+    for (i = 0; i <= num_level_values_signalled_minus1; i++) {
+      guint16 num_applicable_ops_minus1 = 0;
+      g_assert (num_applicable_ops_minus1 < 1024);
+
+      WRITE_UINT32 (bs, seq_param->level_idc, 8);
+      WRITE_UE (bs, num_applicable_ops_minus1);
+
+      for (j = 0; j <= num_applicable_ops_minus1; j++) {
+        guint8 temporal_id = 0;
+        guint16 num_target_views_minus1 = 1;
+
+        WRITE_UINT32 (bs, temporal_id, 3);
+        WRITE_UE (bs, num_target_views_minus1);
+
+        for (k = 0; k <= num_target_views_minus1; k++)
+          WRITE_UE (bs, k);
+
+        WRITE_UE (bs, num_views_minus1);
+      }
+    }
+
+    /* mvc_vui_parameters_present_flag */
+    WRITE_UINT32 (bs, 0, 1);
+  }
+
+  /* additional_extension2_flag */
+  WRITE_UINT32 (bs, 0, 1);
 
   /* rbsp_trailing_bits */
   bs_write_trailing_bits (bs);
@@ -430,9 +630,10 @@ bs_write_sps (GstBitWriter * bs,
   /* ERRORS */
 bs_error:
   {
-    GST_WARNING ("failed to write SPS NAL unit");
+    GST_WARNING ("failed to write subset SPS NAL unit");
     return FALSE;
   }
+  return FALSE;
 }
 
 /* Write a PPS NAL unit */
@@ -531,53 +732,178 @@ struct _GstVaapiEncoderH264
   guint32 mb_height;
   gboolean use_cabac;
   gboolean use_dct8x8;
-
-  /* re-ordering */
-  GQueue reorder_frame_list;
-  guint reorder_state;
-  guint frame_index;
-  guint cur_frame_num;
-  guint cur_present_index;
   GstClockTime cts_offset;
 
-  /* reference list */
-  GQueue ref_list;
-  guint max_ref_frames;
-  /* max reflist count */
-  guint max_reflist0_count;
-  guint max_reflist1_count;
-
   /* frame, poc */
   guint32 max_frame_num;
   guint32 log2_max_frame_num;
   guint32 max_pic_order_cnt;
   guint32 log2_max_pic_order_cnt;
   guint32 idr_num;
+  guint8 pic_order_cnt_type;
+  guint8 delta_pic_order_always_zero_flag;
 
   GstBuffer *sps_data;
+  GstBuffer *subset_sps_data;
   GstBuffer *pps_data;
+
+  guint bitrate_bits;           // bitrate (bits)
+  guint cpb_length;             // length of CPB buffer (ms)
+  guint cpb_length_bits;        // length of CPB buffer (bits)
+
+  /* MVC */
+  gboolean is_mvc;
+  guint32 view_idx;
+  guint32 num_views;
+  GstVaapiH264ViewRefPool ref_pools[MAX_NUM_VIEWS];
+  GstVaapiH264ViewReorderPool reorder_pools[MAX_NUM_VIEWS];
 };
 
+/* Write a Slice NAL unit */
+static gboolean
+bs_write_slice (GstBitWriter * bs,
+    const VAEncSliceParameterBufferH264 * slice_param,
+    GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
+{
+  const VAEncPictureParameterBufferH264 *const pic_param = picture->param;
+  guint32 field_pic_flag = 0;
+  guint32 ref_pic_list_modification_flag_l0 = 0;
+  guint32 ref_pic_list_modification_flag_l1 = 0;
+  guint32 no_output_of_prior_pics_flag = 0;
+  guint32 long_term_reference_flag = 0;
+  guint32 adaptive_ref_pic_marking_mode_flag = 0;
+
+  /* first_mb_in_slice */
+  WRITE_UE (bs, slice_param->macroblock_address);
+  /* slice_type */
+  WRITE_UE (bs, slice_param->slice_type);
+  /* pic_parameter_set_id */
+  WRITE_UE (bs, slice_param->pic_parameter_set_id);
+  /* frame_num */
+  WRITE_UINT32 (bs, picture->frame_num, encoder->log2_max_frame_num);
+
+  /* XXX: only frames (i.e. non-interlaced) are supported for now */
+  /* frame_mbs_only_flag == 0 */
+
+  /* idr_pic_id */
+  if (GST_VAAPI_ENC_PICTURE_IS_IDR (picture))
+    WRITE_UE (bs, slice_param->idr_pic_id);
+
+  /* XXX: only POC type 0 is supported */
+  if (!encoder->pic_order_cnt_type) {
+    WRITE_UINT32 (bs, slice_param->pic_order_cnt_lsb,
+        encoder->log2_max_pic_order_cnt);
+    /* bottom_field_pic_order_in_frame_present_flag is FALSE */
+    if (pic_param->pic_fields.bits.pic_order_present_flag && !field_pic_flag)
+      WRITE_SE (bs, slice_param->delta_pic_order_cnt_bottom);
+  } else if (encoder->pic_order_cnt_type == 1 &&
+      !encoder->delta_pic_order_always_zero_flag) {
+    WRITE_SE (bs, slice_param->delta_pic_order_cnt[0]);
+    if (pic_param->pic_fields.bits.pic_order_present_flag && !field_pic_flag)
+      WRITE_SE (bs, slice_param->delta_pic_order_cnt[1]);
+  }
+  /* redundant_pic_cnt_present_flag is FALSE, no redundant coded pictures */
+
+  /* only works for B-frames */
+  if (slice_param->slice_type == 1)
+    WRITE_UINT32 (bs, slice_param->direct_spatial_mv_pred_flag, 1);
+
+  /* not supporting SP slices */
+  if (slice_param->slice_type == 0 || slice_param->slice_type == 1) {
+    WRITE_UINT32 (bs, slice_param->num_ref_idx_active_override_flag, 1);
+    if (slice_param->num_ref_idx_active_override_flag) {
+      WRITE_UE (bs, slice_param->num_ref_idx_l0_active_minus1);
+      if (slice_param->slice_type == 1)
+        WRITE_UE (bs, slice_param->num_ref_idx_l1_active_minus1);
+    }
+  }
+  /* XXX: not supporting custom reference picture list modifications */
+  if ((slice_param->slice_type != 2) && (slice_param->slice_type != 4))
+    WRITE_UINT32 (bs, ref_pic_list_modification_flag_l0, 1);
+  if (slice_param->slice_type == 1)
+    WRITE_UINT32 (bs, ref_pic_list_modification_flag_l1, 1);
+
+  /* we have: weighted_pred_flag == FALSE and */
+  /*        : weighted_bipred_idc == FALSE */
+  if ((pic_param->pic_fields.bits.weighted_pred_flag &&
+          (slice_param->slice_type == 0)) ||
+      ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
+          (slice_param->slice_type == 1))) {
+    /* XXXX: add pred_weight_table() */
+  }
+
+  /* dec_ref_pic_marking() */
+  if (slice_param->slice_type == 0 || slice_param->slice_type == 2) {
+    if (GST_VAAPI_ENC_PICTURE_IS_IDR (picture)) {
+      /* no_output_of_prior_pics_flag = 0 */
+      WRITE_UINT32 (bs, no_output_of_prior_pics_flag, 1);
+      /* long_term_reference_flag = 0 */
+      WRITE_UINT32 (bs, long_term_reference_flag, 1);
+    } else {
+      /* only sliding_window reference picture marking mode is supported */
+      /* adpative_ref_pic_marking_mode_flag = 0 */
+      WRITE_UINT32 (bs, adaptive_ref_pic_marking_mode_flag, 1);
+    }
+  }
+
+  /* cabac_init_idc */
+  if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
+      slice_param->slice_type != 2)
+    WRITE_UE (bs, slice_param->cabac_init_idc);
+  /*slice_qp_delta */
+  WRITE_SE (bs, slice_param->slice_qp_delta);
+
+  /* XXX: only supporting I, P and B type slices */
+  /* no sp_for_switch_flag and no slice_qs_delta */
+
+  if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
+    /* disable_deblocking_filter_idc */
+    WRITE_UE (bs, slice_param->disable_deblocking_filter_idc);
+    if (slice_param->disable_deblocking_filter_idc != 1) {
+      WRITE_SE (bs, slice_param->slice_alpha_c0_offset_div2);
+      WRITE_SE (bs, slice_param->slice_beta_offset_div2);
+    }
+  }
+
+  /* XXX: unsupported arbitrary slice ordering (ASO) */
+  /* num_slic_groups_minus1 should be zero */
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write Slice NAL unit");
+    return FALSE;
+  }
+}
+
 static inline void
 _check_sps_pps_status (GstVaapiEncoderH264 * encoder,
     const guint8 * nal, guint32 size)
 {
   guint8 nal_type;
   gsize ret;
+  gboolean has_subset_sps;
 
   g_assert (size);
 
-  if (encoder->sps_data && encoder->pps_data)
+  has_subset_sps = !encoder->is_mvc || (encoder->subset_sps_data != NULL);
+  if (encoder->sps_data && encoder->pps_data && has_subset_sps)
     return;
 
   nal_type = nal[0] & 0x1F;
   switch (nal_type) {
-    case GST_VAAPI_ENCODER_H264_NAL_SPS:
+    case GST_H264_NAL_SPS:
       encoder->sps_data = gst_buffer_new_allocate (NULL, size, NULL);
       ret = gst_buffer_fill (encoder->sps_data, 0, nal, size);
       g_assert (ret == size);
       break;
-    case GST_VAAPI_ENCODER_H264_NAL_PPS:
+    case GST_H264_NAL_SUBSET_SPS:
+      encoder->subset_sps_data = gst_buffer_new_allocate (NULL, size, NULL);
+      ret = gst_buffer_fill (encoder->subset_sps_data, 0, nal, size);
+      g_assert (ret == size);
+      break;
+    case GST_H264_NAL_PPS:
       encoder->pps_data = gst_buffer_new_allocate (NULL, size, NULL);
       ret = gst_buffer_fill (encoder->pps_data, 0, nal, size);
       g_assert (ret == size);
@@ -709,6 +1035,12 @@ ensure_profile (GstVaapiEncoderH264 * encoder)
   if (encoder->use_dct8x8)
     profile = GST_VAAPI_PROFILE_H264_HIGH;
 
+  /* MVC profiles coding tools */
+  if (encoder->num_views == 2)
+    profile = GST_VAAPI_PROFILE_H264_STEREO_HIGH;
+  else if (encoder->num_views > 2)
+    profile = GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH;
+
   encoder->profile = profile;
   encoder->profile_idc = gst_vaapi_utils_h264_get_profile_idc (profile);
   return TRUE;
@@ -718,7 +1050,7 @@ ensure_profile (GstVaapiEncoderH264 * encoder)
 static gboolean
 ensure_level (GstVaapiEncoderH264 * encoder)
 {
-  const guint bitrate = GST_VAAPI_ENCODER_CAST (encoder)->bitrate;
+  const guint cpb_factor = h264_get_cpb_nal_factor (encoder->profile);
   const GstVaapiH264LevelLimits *limits_table;
   guint i, num_limits, PicSizeMbs, MaxDpbMbs, MaxMBPS;
 
@@ -732,7 +1064,10 @@ ensure_level (GstVaapiEncoderH264 * encoder)
     const GstVaapiH264LevelLimits *const limits = &limits_table[i];
     if (PicSizeMbs <= limits->MaxFS &&
         MaxDpbMbs <= limits->MaxDpbMbs &&
-        MaxMBPS <= limits->MaxMBPS && (!bitrate || bitrate <= limits->MaxBR))
+        MaxMBPS <= limits->MaxMBPS && (!encoder->bitrate_bits
+            || encoder->bitrate_bits <= (limits->MaxBR * cpb_factor)) &&
+        (!encoder->cpb_length_bits ||
+            encoder->cpb_length_bits <= (limits->MaxCPB * cpb_factor)))
       break;
   }
   if (i == num_limits)
@@ -764,7 +1099,7 @@ ensure_tuning_high_compression (GstVaapiEncoderH264 * encoder)
     profile_idc = encoder->max_profile_idc;
 
   /* Tuning options to enable Main profile */
-  if (profile_idc >= 77) {
+  if (profile_idc >= 77 && profile_idc != 88) {
     encoder->use_cabac = TRUE;
     if (!encoder->num_bframes)
       encoder->num_bframes = 1;
@@ -798,38 +1133,50 @@ ensure_tuning (GstVaapiEncoderH264 * encoder)
 static void
 reset_gop_start (GstVaapiEncoderH264 * encoder)
 {
+  GstVaapiH264ViewReorderPool *const reorder_pool =
+      &encoder->reorder_pools[encoder->view_idx];
+
+  reorder_pool->frame_index = 1;
+  reorder_pool->cur_frame_num = 0;
+  reorder_pool->cur_present_index = 0;
   ++encoder->idr_num;
-  encoder->frame_index = 1;
-  encoder->cur_frame_num = 0;
-  encoder->cur_present_index = 0;
 }
 
 /* Marks the supplied picture as a B-frame */
 static void
 set_b_frame (GstVaapiEncPicture * pic, GstVaapiEncoderH264 * encoder)
 {
+  GstVaapiH264ViewReorderPool *const reorder_pool =
+      &encoder->reorder_pools[encoder->view_idx];
+
   g_assert (pic && encoder);
   g_return_if_fail (pic->type == GST_VAAPI_PICTURE_TYPE_NONE);
   pic->type = GST_VAAPI_PICTURE_TYPE_B;
-  pic->frame_num = (encoder->cur_frame_num % encoder->max_frame_num);
+  pic->frame_num = (reorder_pool->cur_frame_num % encoder->max_frame_num);
 }
 
 /* Marks the supplied picture as a P-frame */
 static void
 set_p_frame (GstVaapiEncPicture * pic, GstVaapiEncoderH264 * encoder)
 {
+  GstVaapiH264ViewReorderPool *const reorder_pool =
+      &encoder->reorder_pools[encoder->view_idx];
+
   g_return_if_fail (pic->type == GST_VAAPI_PICTURE_TYPE_NONE);
   pic->type = GST_VAAPI_PICTURE_TYPE_P;
-  pic->frame_num = (encoder->cur_frame_num % encoder->max_frame_num);
+  pic->frame_num = (reorder_pool->cur_frame_num % encoder->max_frame_num);
 }
 
 /* Marks the supplied picture as an I-frame */
 static void
 set_i_frame (GstVaapiEncPicture * pic, GstVaapiEncoderH264 * encoder)
 {
+  GstVaapiH264ViewReorderPool *const reorder_pool =
+      &encoder->reorder_pools[encoder->view_idx];
+
   g_return_if_fail (pic->type == GST_VAAPI_PICTURE_TYPE_NONE);
   pic->type = GST_VAAPI_PICTURE_TYPE_I;
-  pic->frame_num = (encoder->cur_frame_num % encoder->max_frame_num);
+  pic->frame_num = (reorder_pool->cur_frame_num % encoder->max_frame_num);
 
   g_assert (pic->frame);
   GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (pic->frame);
@@ -865,10 +1212,8 @@ set_key_frame (GstVaapiEncPicture * picture,
 static void
 fill_hrd_params (GstVaapiEncoderH264 * encoder, VAEncMiscParameterHRD * hrd)
 {
-  GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
-
-  if (base_encoder->bitrate > 0) {
-    hrd->buffer_size = base_encoder->bitrate * 1000 * 8;
+  if (encoder->bitrate_bits > 0) {
+    hrd->buffer_size = encoder->cpb_length_bits;
     hrd->initial_buffer_fullness = hrd->buffer_size / 2;
   } else {
     hrd->buffer_size = 0;
@@ -886,14 +1231,27 @@ add_packed_sequence_header (GstVaapiEncoderH264 * encoder,
   GstBitWriter bs;
   VAEncPackedHeaderParameterBuffer packed_seq_param = { 0 };
   const VAEncSequenceParameterBufferH264 *const seq_param = sequence->param;
+  GstVaapiProfile profile = encoder->profile;
+
+  VAEncMiscParameterHRD hrd_params;
   guint32 data_bit_size;
   guint8 *data;
 
+  fill_hrd_params (encoder, &hrd_params);
+
   gst_bit_writer_init (&bs, 128 * 8);
   WRITE_UINT32 (&bs, 0x00000001, 32);   /* start code */
-  bs_write_nal_header (&bs,
-      GST_VAAPI_ENCODER_H264_NAL_REF_IDC_HIGH, GST_VAAPI_ENCODER_H264_NAL_SPS);
-  bs_write_sps (&bs, seq_param, encoder->profile);
+  bs_write_nal_header (&bs, GST_H264_NAL_REF_IDC_HIGH, GST_H264_NAL_SPS);
+
+  /* Set High profile for encoding the MVC base view. Otherwise, some
+     traditional decoder cannot recognize MVC profile streams with
+     only the base view in there */
+  if (profile == GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH ||
+      profile == GST_VAAPI_PROFILE_H264_STEREO_HIGH)
+    profile = GST_VAAPI_PROFILE_H264_HIGH;
+
+  bs_write_sps (&bs, seq_param, profile, &hrd_params);
+
   g_assert (GST_BIT_WRITER_BIT_SIZE (&bs) % 8 == 0);
   data_bit_size = GST_BIT_WRITER_BIT_SIZE (&bs);
   data = GST_BIT_WRITER_DATA (&bs);
@@ -924,6 +1282,58 @@ bs_error:
   }
 }
 
+static gboolean
+add_packed_sequence_header_mvc (GstVaapiEncoderH264 * encoder,
+    GstVaapiEncPicture * picture, GstVaapiEncSequence * sequence)
+{
+  GstVaapiEncPackedHeader *packed_seq;
+  GstBitWriter bs;
+  VAEncPackedHeaderParameterBuffer packed_header_param_buffer = { 0 };
+  const VAEncSequenceParameterBufferH264 *const seq_param = sequence->param;
+  VAEncMiscParameterHRD hrd_params;
+  guint32 data_bit_size;
+  guint8 *data;
+
+  fill_hrd_params (encoder, &hrd_params);
+
+  /* non-base layer, pack one subset sps */
+  gst_bit_writer_init (&bs, 128 * 8);
+  WRITE_UINT32 (&bs, 0x00000001, 32);   /* start code */
+  bs_write_nal_header (&bs, GST_H264_NAL_REF_IDC_HIGH, GST_H264_NAL_SUBSET_SPS);
+
+  bs_write_subset_sps (&bs, seq_param, encoder->profile, encoder->num_views,
+      &hrd_params);
+
+  g_assert (GST_BIT_WRITER_BIT_SIZE (&bs) % 8 == 0);
+  data_bit_size = GST_BIT_WRITER_BIT_SIZE (&bs);
+  data = GST_BIT_WRITER_DATA (&bs);
+
+  packed_header_param_buffer.type = VAEncPackedHeaderSequence;
+  packed_header_param_buffer.bit_length = data_bit_size;
+  packed_header_param_buffer.has_emulation_bytes = 0;
+
+  packed_seq = gst_vaapi_enc_packed_header_new (GST_VAAPI_ENCODER (encoder),
+      &packed_header_param_buffer, sizeof (packed_header_param_buffer),
+      data, (data_bit_size + 7) / 8);
+  g_assert (packed_seq);
+
+  gst_vaapi_enc_picture_add_packed_header (picture, packed_seq);
+  gst_vaapi_mini_object_replace ((GstVaapiMiniObject **) & packed_seq, NULL);
+
+  /* store subset sps data */
+  _check_sps_pps_status (encoder, data + 4, data_bit_size / 8 - 4);
+  gst_bit_writer_clear (&bs, TRUE);
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write SPS NAL unit");
+    gst_bit_writer_clear (&bs, TRUE);
+    return FALSE;
+  }
+}
+
 /* Adds the supplied picture header (PPS) to the list of packed
    headers to pass down as-is to the encoder */
 static gboolean
@@ -939,8 +1349,7 @@ add_packed_picture_header (GstVaapiEncoderH264 * encoder,
 
   gst_bit_writer_init (&bs, 128 * 8);
   WRITE_UINT32 (&bs, 0x00000001, 32);   /* start code */
-  bs_write_nal_header (&bs,
-      GST_VAAPI_ENCODER_H264_NAL_REF_IDC_HIGH, GST_VAAPI_ENCODER_H264_NAL_PPS);
+  bs_write_nal_header (&bs, GST_H264_NAL_REF_IDC_HIGH, GST_H264_NAL_PPS);
   bs_write_pps (&bs, pic_param, encoder->profile);
   g_assert (GST_BIT_WRITER_BIT_SIZE (&bs) % 8 == 0);
   data_bit_size = GST_BIT_WRITER_BIT_SIZE (&bs);
@@ -972,6 +1381,133 @@ bs_error:
   }
 }
 
+static gboolean
+get_nal_hdr_attributes (GstVaapiEncPicture * picture,
+    guint8 * nal_ref_idc, guint8 * nal_unit_type)
+{
+  switch (picture->type) {
+    case GST_VAAPI_PICTURE_TYPE_I:
+      *nal_ref_idc = GST_H264_NAL_REF_IDC_HIGH;
+      if (GST_VAAPI_ENC_PICTURE_IS_IDR (picture))
+        *nal_unit_type = GST_H264_NAL_SLICE_IDR;
+      else
+        *nal_unit_type = GST_H264_NAL_SLICE;
+      break;
+    case GST_VAAPI_PICTURE_TYPE_P:
+      *nal_ref_idc = GST_H264_NAL_REF_IDC_MEDIUM;
+      *nal_unit_type = GST_H264_NAL_SLICE;
+      break;
+    case GST_VAAPI_PICTURE_TYPE_B:
+      *nal_ref_idc = GST_H264_NAL_REF_IDC_NONE;
+      *nal_unit_type = GST_H264_NAL_SLICE;
+      break;
+    default:
+      return FALSE;
+  }
+  return TRUE;
+}
+
+/* Adds the supplied prefix nal header to the list of packed
+   headers to pass down as-is to the encoder */
+static gboolean
+add_packed_prefix_nal_header (GstVaapiEncoderH264 * encoder,
+    GstVaapiEncPicture * picture, GstVaapiEncSlice * slice)
+{
+  GstVaapiEncPackedHeader *packed_prefix_nal;
+  GstBitWriter bs;
+  VAEncPackedHeaderParameterBuffer packed_prefix_nal_param = { 0 };
+  guint32 data_bit_size;
+  guint8 *data;
+  guint8 nal_ref_idc, nal_unit_type;
+
+  gst_bit_writer_init (&bs, 128 * 8);
+  WRITE_UINT32 (&bs, 0x00000001, 32);   /* start code */
+
+  if (!get_nal_hdr_attributes (picture, &nal_ref_idc, &nal_unit_type))
+    goto bs_error;
+  nal_unit_type = GST_H264_NAL_PREFIX_UNIT;
+
+  bs_write_nal_header (&bs, nal_ref_idc, nal_unit_type);
+  bs_write_nal_header_mvc_extension (&bs, picture, encoder->view_idx);
+  g_assert (GST_BIT_WRITER_BIT_SIZE (&bs) % 8 == 0);
+  data_bit_size = GST_BIT_WRITER_BIT_SIZE (&bs);
+  data = GST_BIT_WRITER_DATA (&bs);
+
+  packed_prefix_nal_param.type = VAEncPackedHeaderRawData;
+  packed_prefix_nal_param.bit_length = data_bit_size;
+  packed_prefix_nal_param.has_emulation_bytes = 0;
+
+  packed_prefix_nal =
+      gst_vaapi_enc_packed_header_new (GST_VAAPI_ENCODER (encoder),
+      &packed_prefix_nal_param, sizeof (packed_prefix_nal_param), data,
+      (data_bit_size + 7) / 8);
+  g_assert (packed_prefix_nal);
+
+  gst_vaapi_enc_slice_add_packed_header (slice, packed_prefix_nal);
+  gst_vaapi_codec_object_replace (&packed_prefix_nal, NULL);
+
+  gst_bit_writer_clear (&bs, TRUE);
+
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write Prefix NAL unit header");
+    gst_bit_writer_clear (&bs, TRUE);
+    return FALSE;
+  }
+}
+
+/* Adds the supplied slice header to the list of packed
+   headers to pass down as-is to the encoder */
+static gboolean
+add_packed_slice_header (GstVaapiEncoderH264 * encoder,
+    GstVaapiEncPicture * picture, GstVaapiEncSlice * slice)
+{
+  GstVaapiEncPackedHeader *packed_slice;
+  GstBitWriter bs;
+  VAEncPackedHeaderParameterBuffer packed_slice_param = { 0 };
+  const VAEncSliceParameterBufferH264 *const slice_param = slice->param;
+  guint32 data_bit_size;
+  guint8 *data;
+  guint8 nal_ref_idc, nal_unit_type;
+
+  gst_bit_writer_init (&bs, 128 * 8);
+  WRITE_UINT32 (&bs, 0x00000001, 32);   /* start code */
+
+  if (!get_nal_hdr_attributes (picture, &nal_ref_idc, &nal_unit_type))
+    goto bs_error;
+  bs_write_nal_header (&bs, nal_ref_idc, nal_unit_type);
+
+  bs_write_slice (&bs, slice_param, encoder, picture);
+  data_bit_size = GST_BIT_WRITER_BIT_SIZE (&bs);
+  data = GST_BIT_WRITER_DATA (&bs);
+
+  packed_slice_param.type = VAEncPackedHeaderSlice;
+  packed_slice_param.bit_length = data_bit_size;
+  packed_slice_param.has_emulation_bytes = 0;
+
+  packed_slice = gst_vaapi_enc_packed_header_new (GST_VAAPI_ENCODER (encoder),
+      &packed_slice_param, sizeof (packed_slice_param),
+      data, (data_bit_size + 7) / 8);
+  g_assert (packed_slice);
+
+  gst_vaapi_enc_slice_add_packed_header (slice, packed_slice);
+  gst_vaapi_codec_object_replace (&packed_slice, NULL);
+
+  gst_bit_writer_clear (&bs, TRUE);
+  return TRUE;
+
+  /* ERRORS */
+bs_error:
+  {
+    GST_WARNING ("failed to write Slice NAL unit header");
+    gst_bit_writer_clear (&bs, TRUE);
+    return FALSE;
+  }
+}
+
 /* Reference picture management */
 static void
 reference_pic_free (GstVaapiEncoderH264 * encoder, GstVaapiEncoderH264Ref * ref)
@@ -1000,20 +1536,24 @@ reference_list_update (GstVaapiEncoderH264 * encoder,
     GstVaapiEncPicture * picture, GstVaapiSurfaceProxy * surface)
 {
   GstVaapiEncoderH264Ref *ref;
+  GstVaapiH264ViewRefPool *const ref_pool =
+      &encoder->ref_pools[encoder->view_idx];
 
   if (GST_VAAPI_PICTURE_TYPE_B == picture->type) {
     gst_vaapi_encoder_release_surface (GST_VAAPI_ENCODER (encoder), surface);
     return TRUE;
   }
   if (GST_VAAPI_ENC_PICTURE_IS_IDR (picture)) {
-    while (!g_queue_is_empty (&encoder->ref_list))
-      reference_pic_free (encoder, g_queue_pop_head (&encoder->ref_list));
-  } else if (g_queue_get_length (&encoder->ref_list) >= encoder->max_ref_frames) {
-    reference_pic_free (encoder, g_queue_pop_head (&encoder->ref_list));
+    while (!g_queue_is_empty (&ref_pool->ref_list))
+      reference_pic_free (encoder, g_queue_pop_head (&ref_pool->ref_list));
+  } else if (g_queue_get_length (&ref_pool->ref_list) >=
+      ref_pool->max_ref_frames) {
+    reference_pic_free (encoder, g_queue_pop_head (&ref_pool->ref_list));
   }
   ref = reference_pic_create (encoder, picture, surface);
-  g_queue_push_tail (&encoder->ref_list, ref);
-  g_assert (g_queue_get_length (&encoder->ref_list) <= encoder->max_ref_frames);
+  g_queue_push_tail (&ref_pool->ref_list, ref);
+  g_assert (g_queue_get_length (&ref_pool->ref_list) <=
+      ref_pool->max_ref_frames);
   return TRUE;
 }
 
@@ -1025,8 +1565,9 @@ reference_list_init (GstVaapiEncoderH264 * encoder,
     GstVaapiEncoderH264Ref ** reflist_1, guint * reflist_1_count)
 {
   GstVaapiEncoderH264Ref *tmp;
+  GstVaapiH264ViewRefPool *const ref_pool =
+      &encoder->ref_pools[encoder->view_idx];
   GList *iter, *list_0_start = NULL, *list_1_start = NULL;
-  guint max_pic_order_cnt = (1 << encoder->log2_max_pic_order_cnt);
   guint count;
 
   *reflist_0_count = 0;
@@ -1034,11 +1575,11 @@ reference_list_init (GstVaapiEncoderH264 * encoder,
   if (picture->type == GST_VAAPI_PICTURE_TYPE_I)
     return TRUE;
 
-  iter = g_queue_peek_tail_link (&encoder->ref_list);
+  iter = g_queue_peek_tail_link (&ref_pool->ref_list);
   for (; iter; iter = g_list_previous (iter)) {
     tmp = (GstVaapiEncoderH264Ref *) iter->data;
     g_assert (tmp && tmp->poc != picture->poc);
-    if (_poc_greater_than (picture->poc, tmp->poc, max_pic_order_cnt)) {
+    if (_poc_greater_than (picture->poc, tmp->poc, encoder->max_pic_order_cnt)) {
       list_0_start = iter;
       list_1_start = g_list_next (iter);
       break;
@@ -1073,20 +1614,18 @@ reference_list_init (GstVaapiEncoderH264 * encoder,
 static gboolean
 fill_sequence (GstVaapiEncoderH264 * encoder, GstVaapiEncSequence * sequence)
 {
-  GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
   VAEncSequenceParameterBufferH264 *const seq_param = sequence->param;
+  GstVaapiH264ViewRefPool *const ref_pool =
+      &encoder->ref_pools[encoder->view_idx];
 
   memset (seq_param, 0, sizeof (VAEncSequenceParameterBufferH264));
-  seq_param->seq_parameter_set_id = 0;
+  seq_param->seq_parameter_set_id = encoder->view_idx;
   seq_param->level_idc = encoder->level_idc;
   seq_param->intra_period = GST_VAAPI_ENCODER_KEYFRAME_PERIOD (encoder);
-  seq_param->ip_period = 0;     // ?
-  if (base_encoder->bitrate > 0)
-    seq_param->bits_per_second = base_encoder->bitrate * 1000;
-  else
-    seq_param->bits_per_second = 0;
+  seq_param->ip_period = 1 + encoder->num_bframes;
+  seq_param->bits_per_second = encoder->bitrate_bits;
 
-  seq_param->max_num_ref_frames = encoder->max_ref_frames;
+  seq_param->max_num_ref_frames = ref_pool->max_ref_frames;
   seq_param->picture_width_in_mbs = encoder->mb_width;
   seq_param->picture_height_in_mbs = encoder->mb_height;
 
@@ -1102,7 +1641,8 @@ fill_sequence (GstVaapiEncoderH264 * encoder, GstVaapiEncSequence * sequence)
   seq_param->seq_fields.bits.log2_max_frame_num_minus4 =
       encoder->log2_max_frame_num - 4;
   /* picture order count */
-  seq_param->seq_fields.bits.pic_order_cnt_type = 0;
+  encoder->pic_order_cnt_type = seq_param->seq_fields.bits.pic_order_cnt_type =
+      0;
   g_assert (encoder->log2_max_pic_order_cnt >= 4);
   seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 =
       encoder->log2_max_pic_order_cnt - 4;
@@ -1112,7 +1652,8 @@ fill_sequence (GstVaapiEncoderH264 * encoder, GstVaapiEncSequence * sequence)
 
   /* not used if pic_order_cnt_type == 0 */
   if (seq_param->seq_fields.bits.pic_order_cnt_type == 1) {
-    seq_param->seq_fields.bits.delta_pic_order_always_zero_flag = TRUE;
+    encoder->delta_pic_order_always_zero_flag =
+        seq_param->seq_fields.bits.delta_pic_order_always_zero_flag = TRUE;
     seq_param->num_ref_frames_in_pic_order_cnt_cycle = 0;
     seq_param->offset_for_non_ref_pic = 0;
     seq_param->offset_for_top_to_bottom_field = 0;
@@ -1162,6 +1703,8 @@ fill_picture (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
     GstVaapiCodedBuffer * codedbuf, GstVaapiSurfaceProxy * surface)
 {
   VAEncPictureParameterBufferH264 *const pic_param = picture->param;
+  GstVaapiH264ViewRefPool *const ref_pool =
+      &encoder->ref_pools[encoder->view_idx];
   GstVaapiEncoderH264Ref *ref_pic;
   GList *reflist;
   guint i;
@@ -1173,7 +1716,7 @@ fill_picture (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
   pic_param->CurrPic.TopFieldOrderCnt = picture->poc;
   i = 0;
   if (picture->type != GST_VAAPI_PICTURE_TYPE_I) {
-    for (reflist = g_queue_peek_head_link (&encoder->ref_list);
+    for (reflist = g_queue_peek_head_link (&ref_pool->ref_list);
         reflist; reflist = g_list_next (reflist)) {
       ref_pic = reflist->data;
       g_assert (ref_pic && ref_pic->pic &&
@@ -1183,22 +1726,22 @@ fill_picture (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
           GST_VAAPI_SURFACE_PROXY_SURFACE_ID (ref_pic->pic);
       ++i;
     }
-    g_assert (i <= 16 && i <= encoder->max_ref_frames);
+    g_assert (i <= 16 && i <= ref_pool->max_ref_frames);
   }
   for (; i < 16; ++i) {
     pic_param->ReferenceFrames[i].picture_id = VA_INVALID_ID;
   }
   pic_param->coded_buf = GST_VAAPI_OBJECT_ID (codedbuf);
 
-  pic_param->pic_parameter_set_id = 0;
-  pic_param->seq_parameter_set_id = 0;
+  pic_param->pic_parameter_set_id = encoder->view_idx;
+  pic_param->seq_parameter_set_id = encoder->view_idx;
   pic_param->last_picture = 0;  /* means last encoding picture */
   pic_param->frame_num = picture->frame_num;
   pic_param->pic_init_qp = encoder->init_qp;
   pic_param->num_ref_idx_l0_active_minus1 =
-      (encoder->max_reflist0_count ? (encoder->max_reflist0_count - 1) : 0);
+      (ref_pool->max_reflist0_count ? (ref_pool->max_reflist0_count - 1) : 0);
   pic_param->num_ref_idx_l1_active_minus1 =
-      (encoder->max_reflist1_count ? (encoder->max_reflist1_count - 1) : 0);
+      (ref_pool->max_reflist1_count ? (ref_pool->max_reflist1_count - 1) : 0);
   pic_param->chroma_qp_index_offset = 0;
   pic_param->second_chroma_qp_index_offset = 0;
 
@@ -1260,7 +1803,7 @@ add_slice_headers (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
     slice_param->macroblock_info = VA_INVALID_ID;
     slice_param->slice_type = h264_get_slice_type (picture->type);
     g_assert (slice_param->slice_type != -1);
-    slice_param->pic_parameter_set_id = 0;
+    slice_param->pic_parameter_set_id = encoder->view_idx;
     slice_param->idr_pic_id = encoder->idr_num;
     slice_param->pic_order_cnt_lsb = picture->poc;
 
@@ -1343,45 +1886,85 @@ add_slice_headers (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
     /* set calculation for next slice */
     last_mb_index += cur_slice_mbs;
 
+    if (encoder->is_mvc &&
+        (GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) & VA_ENC_PACKED_HEADER_RAW_DATA)
+        && !add_packed_prefix_nal_header (encoder, picture, slice))
+      goto error_create_packed_prefix_nal_hdr;
+    if ((GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) &
+            VA_ENC_PACKED_HEADER_SLICE)
+        && !add_packed_slice_header (encoder, picture, slice))
+      goto error_create_packed_slice_hdr;
+
     gst_vaapi_enc_picture_add_slice (picture, slice);
     gst_vaapi_codec_object_replace (&slice, NULL);
   }
   g_assert (last_mb_index == mb_size);
   return TRUE;
+
+error_create_packed_slice_hdr:
+  {
+    GST_ERROR ("failed to create packed slice header buffer");
+    gst_vaapi_codec_object_replace (&slice, NULL);
+    return FALSE;
+  }
+error_create_packed_prefix_nal_hdr:
+  {
+    GST_ERROR ("failed to create packed prefix nal header buffer");
+    gst_vaapi_codec_object_replace (&slice, NULL);
+    return FALSE;
+  }
 }
 
 /* Generates and submits SPS header accordingly into the bitstream */
 static gboolean
 ensure_sequence (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
 {
-  GstVaapiEncSequence *sequence;
+  GstVaapiEncSequence *sequence = NULL;
 
-  g_assert (picture);
-  sequence = GST_VAAPI_ENC_SEQUENCE_NEW (H264, encoder);
-  g_assert (sequence);
-  if (!sequence)
-    goto error;
+  // Submit an SPS header before every new I-frame
+  if (picture->type != GST_VAAPI_PICTURE_TYPE_I)
+    return TRUE;
 
-  if (!fill_sequence (encoder, sequence))
-    goto error;
+  sequence = GST_VAAPI_ENC_SEQUENCE_NEW (H264, encoder);
+  if (!sequence || !fill_sequence (encoder, sequence))
+    goto error_create_seq_param;
+
+  /* add subset sps for non-base view and sps for base view */
+  if (encoder->is_mvc && encoder->view_idx) {
+    if ((GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) & VAEncPackedHeaderH264_SPS)
+        && !add_packed_sequence_header_mvc (encoder, picture, sequence))
+      goto error_create_packed_seq_hdr;
+  } else {
+    if ((GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) & VAEncPackedHeaderH264_SPS)
+        && !add_packed_sequence_header (encoder, picture, sequence))
+      goto error_create_packed_seq_hdr;
+  }
 
-  if (picture->type == GST_VAAPI_PICTURE_TYPE_I &&
-      !add_packed_sequence_header (encoder, picture, sequence))
-    goto error;
-  gst_vaapi_enc_picture_set_sequence (picture, sequence);
-  gst_vaapi_codec_object_replace (&sequence, NULL);
+  if (sequence) {
+    gst_vaapi_enc_picture_set_sequence (picture, sequence);
+    gst_vaapi_codec_object_replace (&sequence, NULL);
+  }
   return TRUE;
 
-error:
-  gst_vaapi_codec_object_replace (&sequence, NULL);
-  return FALSE;
+  /* ERRORS */
+error_create_seq_param:
+  {
+    GST_ERROR ("failed to create sequence parameter buffer (SPS)");
+    gst_vaapi_codec_object_replace (&sequence, NULL);
+    return FALSE;
+  }
+error_create_packed_seq_hdr:
+  {
+    GST_ERROR ("failed to create packed sequence header buffer");
+    gst_vaapi_codec_object_replace (&sequence, NULL);
+    return FALSE;
+  }
 }
 
 /* Generates additional control parameters */
 static gboolean
 ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
 {
-  GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
   GstVaapiEncMiscParam *misc = NULL;
   VAEncMiscParameterRateControl *rate_control;
 
@@ -1403,12 +1986,9 @@ ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
       return FALSE;
     rate_control = misc->data;
     memset (rate_control, 0, sizeof (VAEncMiscParameterRateControl));
-    if (base_encoder->bitrate)
-      rate_control->bits_per_second = base_encoder->bitrate * 1000;
-    else
-      rate_control->bits_per_second = 0;
+    rate_control->bits_per_second = encoder->bitrate_bits;
     rate_control->target_percentage = 70;
-    rate_control->window_size = 500;
+    rate_control->window_size = encoder->cpb_length;
     rate_control->initial_qp = encoder->init_qp;
     rate_control->min_qp = encoder->min_qp;
     rate_control->basic_unit_size = 0;
@@ -1425,12 +2005,16 @@ ensure_picture (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture,
 {
   GstVaapiCodedBuffer *const codedbuf =
       GST_VAAPI_CODED_BUFFER_PROXY_BUFFER (codedbuf_proxy);
+  gboolean res = FALSE;
 
-  if (!fill_picture (encoder, picture, codedbuf, surface))
+  res = fill_picture (encoder, picture, codedbuf, surface);
+
+  if (!res)
     return FALSE;
 
   if (picture->type == GST_VAAPI_PICTURE_TYPE_I &&
-      !add_packed_picture_header (encoder, picture)) {
+      (GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) & VAEncPackedHeaderH264_PPS)
+      && !add_packed_picture_header (encoder, picture)) {
     GST_ERROR ("set picture packed header failed");
     return FALSE;
   }
@@ -1443,6 +2027,8 @@ ensure_slices (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
 {
   GstVaapiEncoderH264Ref *reflist_0[16];
   GstVaapiEncoderH264Ref *reflist_1[16];
+  GstVaapiH264ViewRefPool *const ref_pool =
+      &encoder->ref_pools[encoder->view_idx];
   guint reflist_0_count = 0, reflist_1_count = 0;
 
   g_assert (picture);
@@ -1454,11 +2040,11 @@ ensure_slices (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
     return FALSE;
   }
 
-  g_assert (reflist_0_count + reflist_1_count <= encoder->max_ref_frames);
-  if (reflist_0_count > encoder->max_reflist0_count)
-    reflist_0_count = encoder->max_reflist0_count;
-  if (reflist_1_count > encoder->max_reflist1_count)
-    reflist_1_count = encoder->max_reflist1_count;
+  g_assert (reflist_0_count + reflist_1_count <= ref_pool->max_ref_frames);
+  if (reflist_0_count > ref_pool->max_reflist0_count)
+    reflist_0_count = ref_pool->max_reflist0_count;
+  if (reflist_1_count > ref_pool->max_reflist1_count)
+    reflist_1_count = ref_pool->max_reflist1_count;
 
   if (!add_slice_headers (encoder, picture,
           reflist_0, reflist_0_count, reflist_1, reflist_1_count))
@@ -1467,6 +2053,32 @@ ensure_slices (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
   return TRUE;
 }
 
+/* Normalizes bitrate (and CPB size) for HRD conformance */
+static void
+ensure_bitrate_hrd (GstVaapiEncoderH264 * encoder)
+{
+  GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
+  guint bitrate, cpb_size;
+
+  if (!base_encoder->bitrate) {
+    encoder->bitrate_bits = 0;
+    return;
+  }
+
+  /* Round down bitrate. This is a hard limit mandated by the user */
+  g_assert (SX_BITRATE >= 6);
+  bitrate = (base_encoder->bitrate * 1000) & ~((1U << SX_BITRATE) - 1);
+  GST_DEBUG ("HRD bitrate: %u bits/sec", bitrate);
+  encoder->bitrate_bits = bitrate;
+
+  /* Round up CPB size. This is an HRD compliance detail */
+  g_assert (SX_CPB_SIZE >= 4);
+  cpb_size = gst_util_uint64_scale (bitrate, encoder->cpb_length, 1000) &
+      ~((1U << SX_CPB_SIZE) - 1);
+  GST_DEBUG ("HRD CPB size: %u bits", cpb_size);
+  encoder->cpb_length_bits = cpb_size;
+}
+
 /* Estimates a good enough bitrate if none was supplied */
 static void
 ensure_bitrate (GstVaapiEncoderH264 * encoder)
@@ -1500,6 +2112,7 @@ ensure_bitrate (GstVaapiEncoderH264 * encoder)
       base_encoder->bitrate = 0;
       break;
   }
+  ensure_bitrate_hrd (encoder);
 }
 
 /* Constructs profile and level information based on user-defined limits */
@@ -1511,14 +2124,16 @@ ensure_profile_and_level (GstVaapiEncoderH264 * encoder)
   if (!ensure_profile (encoder) || !ensure_profile_limits (encoder))
     return GST_VAAPI_ENCODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
 
-  if (!ensure_level (encoder))
-    return GST_VAAPI_ENCODER_STATUS_ERROR_OPERATION_FAILED;
-
   /* Check HW constraints */
   if (!ensure_hw_profile_limits (encoder))
     return GST_VAAPI_ENCODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
   if (encoder->profile_idc > encoder->hw_max_profile_idc)
     return GST_VAAPI_ENCODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
+
+  /* Ensure bitrate if not set already and derive the right level to use */
+  ensure_bitrate (encoder);
+  if (!ensure_level (encoder))
+    return GST_VAAPI_ENCODER_STATUS_ERROR_OPERATION_FAILED;
   return GST_VAAPI_ENCODER_STATUS_SUCCESS;
 }
 
@@ -1526,7 +2141,7 @@ static void
 reset_properties (GstVaapiEncoderH264 * encoder)
 {
   GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
-  guint mb_size;
+  guint mb_size, i;
 
   if (encoder->idr_period < base_encoder->keyframe_period)
     encoder->idr_period = base_encoder->keyframe_period;
@@ -1559,13 +2174,20 @@ reset_properties (GstVaapiEncoderH264 * encoder)
   encoder->max_frame_num = (1 << encoder->log2_max_frame_num);
   encoder->log2_max_pic_order_cnt = encoder->log2_max_frame_num + 1;
   encoder->max_pic_order_cnt = (1 << encoder->log2_max_pic_order_cnt);
-
-  encoder->frame_index = 0;
   encoder->idr_num = 0;
-  encoder->max_reflist0_count = 1;
-  encoder->max_reflist1_count = encoder->num_bframes > 0;
-  encoder->max_ref_frames =
-      encoder->max_reflist0_count + encoder->max_reflist1_count;
+
+  encoder->is_mvc = encoder->num_views > 1;
+  for (i = 0; i < encoder->num_views; i++) {
+    GstVaapiH264ViewRefPool *const ref_pool = &encoder->ref_pools[i];
+    ref_pool->max_reflist0_count = 1;
+    ref_pool->max_reflist1_count = encoder->num_bframes > 0;
+    ref_pool->max_ref_frames = ref_pool->max_reflist0_count
+        + ref_pool->max_reflist1_count;
+
+    GstVaapiH264ViewReorderPool *const reorder_pool =
+        &encoder->reorder_pools[i];
+    reorder_pool->frame_index = 0;
+  }
 }
 
 static GstVaapiEncoderStatus
@@ -1608,16 +2230,23 @@ gst_vaapi_encoder_h264_flush (GstVaapiEncoder * base_encoder)
 {
   GstVaapiEncoderH264 *const encoder =
       GST_VAAPI_ENCODER_H264_CAST (base_encoder);
+  GstVaapiH264ViewReorderPool *reorder_pool;
   GstVaapiEncPicture *pic;
+  guint i;
 
-  encoder->frame_index = 0;
-  encoder->cur_frame_num = 0;
-  encoder->cur_present_index = 0;
-  while (!g_queue_is_empty (&encoder->reorder_frame_list)) {
-    pic = g_queue_pop_head (&encoder->reorder_frame_list);
-    gst_vaapi_enc_picture_unref (pic);
+  for (i = 0; i < encoder->num_views; i++) {
+    reorder_pool = &encoder->reorder_pools[i];
+    reorder_pool->frame_index = 0;
+    reorder_pool->cur_frame_num = 0;
+    reorder_pool->cur_present_index = 0;
+
+    while (!g_queue_is_empty (&reorder_pool->reorder_frame_list)) {
+      pic = (GstVaapiEncPicture *)
+          g_queue_pop_head (&reorder_pool->reorder_frame_list);
+      gst_vaapi_enc_picture_unref (pic);
+    }
+    g_queue_clear (&reorder_pool->reorder_frame_list);
   }
-  g_queue_clear (&encoder->reorder_frame_list);
 
   return GST_VAAPI_ENCODER_STATUS_SUCCESS;
 }
@@ -1719,24 +2348,34 @@ gst_vaapi_encoder_h264_reordering (GstVaapiEncoder * base_encoder,
 {
   GstVaapiEncoderH264 *const encoder =
       GST_VAAPI_ENCODER_H264_CAST (base_encoder);
+  GstVaapiH264ViewReorderPool *reorder_pool = NULL;
   GstVaapiEncPicture *picture;
   gboolean is_idr = FALSE;
 
   *output = NULL;
 
+  /* encoding views alternatively for MVC */
+  if (encoder->is_mvc) {
+    if (frame)
+      encoder->view_idx = frame->system_frame_number % MAX_NUM_VIEWS;
+    else
+      encoder->view_idx = (encoder->view_idx + 1) % MAX_NUM_VIEWS;
+  }
+  reorder_pool = &encoder->reorder_pools[encoder->view_idx];
+
   if (!frame) {
-    if (encoder->reorder_state != GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES)
+    if (reorder_pool->reorder_state != GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES)
       return GST_VAAPI_ENCODER_STATUS_NO_SURFACE;
 
     /* reorder_state = GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES
        dump B frames from queue, sometime, there may also have P frame or I frame */
     g_assert (encoder->num_bframes > 0);
-    g_return_val_if_fail (!g_queue_is_empty (&encoder->reorder_frame_list),
+    g_return_val_if_fail (!g_queue_is_empty (&reorder_pool->reorder_frame_list),
         GST_VAAPI_ENCODER_STATUS_ERROR_UNKNOWN);
-    picture = g_queue_pop_head (&encoder->reorder_frame_list);
+    picture = g_queue_pop_head (&reorder_pool->reorder_frame_list);
     g_assert (picture);
-    if (g_queue_is_empty (&encoder->reorder_frame_list)) {
-      encoder->reorder_state = GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES;
+    if (g_queue_is_empty (&reorder_pool->reorder_frame_list)) {
+      reorder_pool->reorder_state = GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES;
     }
     goto end;
   }
@@ -1748,60 +2387,60 @@ gst_vaapi_encoder_h264_reordering (GstVaapiEncoder * base_encoder,
         GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts));
     return GST_VAAPI_ENCODER_STATUS_ERROR_ALLOCATION_FAILED;
   }
-  ++encoder->cur_present_index;
-  picture->poc = ((encoder->cur_present_index * 2) %
+  ++reorder_pool->cur_present_index;
+  picture->poc = ((reorder_pool->cur_present_index * 2) %
       encoder->max_pic_order_cnt);
 
-  is_idr = (encoder->frame_index == 0 ||
-      encoder->frame_index >= encoder->idr_period);
+  is_idr = (reorder_pool->frame_index == 0 ||
+      reorder_pool->frame_index >= encoder->idr_period);
 
   /* check key frames */
   if (is_idr || GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame) ||
-      (encoder->frame_index % GST_VAAPI_ENCODER_KEYFRAME_PERIOD (encoder)) ==
-      0) {
-    ++encoder->cur_frame_num;
-    ++encoder->frame_index;
+      (reorder_pool->frame_index %
+          GST_VAAPI_ENCODER_KEYFRAME_PERIOD (encoder)) == 0) {
+    ++reorder_pool->cur_frame_num;
+    ++reorder_pool->frame_index;
 
     /* b frame enabled,  check queue of reorder_frame_list */
     if (encoder->num_bframes
-        && !g_queue_is_empty (&encoder->reorder_frame_list)) {
+        && !g_queue_is_empty (&reorder_pool->reorder_frame_list)) {
       GstVaapiEncPicture *p_pic;
 
-      p_pic = g_queue_pop_tail (&encoder->reorder_frame_list);
+      p_pic = g_queue_pop_tail (&reorder_pool->reorder_frame_list);
       set_p_frame (p_pic, encoder);
-      g_queue_foreach (&encoder->reorder_frame_list,
+      g_queue_foreach (&reorder_pool->reorder_frame_list,
           (GFunc) set_b_frame, encoder);
-      ++encoder->cur_frame_num;
+      ++reorder_pool->cur_frame_num;
       set_key_frame (picture, encoder, is_idr);
-      g_queue_push_tail (&encoder->reorder_frame_list, picture);
+      g_queue_push_tail (&reorder_pool->reorder_frame_list, picture);
       picture = p_pic;
-      encoder->reorder_state = GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES;
+      reorder_pool->reorder_state = GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES;
     } else {                    /* no b frames in queue */
       set_key_frame (picture, encoder, is_idr);
-      g_assert (g_queue_is_empty (&encoder->reorder_frame_list));
+      g_assert (g_queue_is_empty (&reorder_pool->reorder_frame_list));
       if (encoder->num_bframes)
-        encoder->reorder_state = GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES;
+        reorder_pool->reorder_state = GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES;
     }
     goto end;
   }
 
   /* new p/b frames coming */
-  ++encoder->frame_index;
-  if (encoder->reorder_state == GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES &&
-      g_queue_get_length (&encoder->reorder_frame_list) <
+  ++reorder_pool->frame_index;
+  if (reorder_pool->reorder_state == GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES &&
+      g_queue_get_length (&reorder_pool->reorder_frame_list) <
       encoder->num_bframes) {
-    g_queue_push_tail (&encoder->reorder_frame_list, picture);
+    g_queue_push_tail (&reorder_pool->reorder_frame_list, picture);
     return GST_VAAPI_ENCODER_STATUS_NO_SURFACE;
   }
 
-  ++encoder->cur_frame_num;
+  ++reorder_pool->cur_frame_num;
   set_p_frame (picture, encoder);
 
-  if (encoder->reorder_state == GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES) {
-    g_queue_foreach (&encoder->reorder_frame_list, (GFunc) set_b_frame,
+  if (reorder_pool->reorder_state == GST_VAAPI_ENC_H264_REORD_WAIT_FRAMES) {
+    g_queue_foreach (&reorder_pool->reorder_frame_list, (GFunc) set_b_frame,
         encoder);
-    encoder->reorder_state = GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES;
-    g_assert (!g_queue_is_empty (&encoder->reorder_frame_list));
+    reorder_pool->reorder_state = GST_VAAPI_ENC_H264_REORD_DUMP_FRAMES;
+    g_assert (!g_queue_is_empty (&reorder_pool->reorder_frame_list));
   }
 
 end:
@@ -1836,7 +2475,8 @@ set_context_info (GstVaapiEncoder * base_encoder)
     return GST_VAAPI_ENCODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
 
   base_encoder->num_ref_frames =
-      (encoder->num_bframes ? 2 : 1) + DEFAULT_SURFACES_COUNT;
+      ((encoder->num_bframes ? 2 : 1) + DEFAULT_SURFACES_COUNT)
+      * encoder->num_views;
 
   /* Only YUV 4:2:0 formats are supported for now. This means that we
      have a limit of 3200 bits per macroblock. */
@@ -1874,8 +2514,6 @@ gst_vaapi_encoder_h264_reconfigure (GstVaapiEncoder * base_encoder)
   if (status != GST_VAAPI_ENCODER_STATUS_SUCCESS)
     return status;
 
-  ensure_bitrate (encoder);
-
   reset_properties (encoder);
   return set_context_info (base_encoder);
 }
@@ -1885,16 +2523,32 @@ gst_vaapi_encoder_h264_init (GstVaapiEncoder * base_encoder)
 {
   GstVaapiEncoderH264 *const encoder =
       GST_VAAPI_ENCODER_H264_CAST (base_encoder);
+  guint32 i;
+
+  /* Multi-view coding information */
+  encoder->is_mvc = FALSE;
+  encoder->num_views = 1;
+  encoder->view_idx = 0;
+
+  /* re-ordering  list initialize */
+  for (i = 0; i < MAX_NUM_VIEWS; i++) {
+    GstVaapiH264ViewReorderPool *const reorder_pool =
+        &encoder->reorder_pools[i];
+    g_queue_init (&reorder_pool->reorder_frame_list);
+    reorder_pool->reorder_state = GST_VAAPI_ENC_H264_REORD_NONE;
+    reorder_pool->frame_index = 0;
+    reorder_pool->cur_frame_num = 0;
+    reorder_pool->cur_present_index = 0;
+  }
 
-  /* re-ordering */
-  g_queue_init (&encoder->reorder_frame_list);
-  encoder->reorder_state = GST_VAAPI_ENC_H264_REORD_NONE;
-
-  /* reference frames */
-  g_queue_init (&encoder->ref_list);
-  encoder->max_ref_frames = 0;
-  encoder->max_reflist0_count = 1;
-  encoder->max_reflist1_count = 1;
+  /* reference list info initialize */
+  for (i = 0; i < MAX_NUM_VIEWS; i++) {
+    GstVaapiH264ViewRefPool *const ref_pool = &encoder->ref_pools[i];
+    g_queue_init (&ref_pool->ref_list);
+    ref_pool->max_ref_frames = 0;
+    ref_pool->max_reflist0_count = 1;
+    ref_pool->max_reflist1_count = 1;
+  }
 
   return TRUE;
 }
@@ -1907,22 +2561,33 @@ gst_vaapi_encoder_h264_finalize (GstVaapiEncoder * base_encoder)
       GST_VAAPI_ENCODER_H264_CAST (base_encoder);
   GstVaapiEncPicture *pic;
   GstVaapiEncoderH264Ref *ref;
+  guint32 i;
 
   gst_buffer_replace (&encoder->sps_data, NULL);
+  gst_buffer_replace (&encoder->subset_sps_data, NULL);
   gst_buffer_replace (&encoder->pps_data, NULL);
 
-  while (!g_queue_is_empty (&encoder->ref_list)) {
-    ref = g_queue_pop_head (&encoder->ref_list);
-    reference_pic_free (encoder, ref);
+  /* reference list info de-init */
+  for (i = 0; i < MAX_NUM_VIEWS; i++) {
+    GstVaapiH264ViewRefPool *const ref_pool = &encoder->ref_pools[i];
+    while (!g_queue_is_empty (&ref_pool->ref_list)) {
+      ref = (GstVaapiEncoderH264Ref *) g_queue_pop_head (&ref_pool->ref_list);
+      reference_pic_free (encoder, ref);
+    }
+    g_queue_clear (&ref_pool->ref_list);
   }
-  g_queue_clear (&encoder->ref_list);
 
-  while (!g_queue_is_empty (&encoder->reorder_frame_list)) {
-    pic = g_queue_pop_head (&encoder->reorder_frame_list);
-    gst_vaapi_enc_picture_unref (pic);
+  /* re-ordering  list initialize */
+  for (i = 0; i < MAX_NUM_VIEWS; i++) {
+    GstVaapiH264ViewReorderPool *const reorder_pool =
+        &encoder->reorder_pools[i];
+    while (!g_queue_is_empty (&reorder_pool->reorder_frame_list)) {
+      pic = (GstVaapiEncPicture *)
+          g_queue_pop_head (&reorder_pool->reorder_frame_list);
+      gst_vaapi_enc_picture_unref (pic);
+    }
+    g_queue_clear (&reorder_pool->reorder_frame_list);
   }
-  g_queue_clear (&encoder->reorder_frame_list);
-
 }
 
 static GstVaapiEncoderStatus
@@ -1951,6 +2616,12 @@ gst_vaapi_encoder_h264_set_property (GstVaapiEncoder * base_encoder,
     case GST_VAAPI_ENCODER_H264_PROP_DCT8X8:
       encoder->use_dct8x8 = g_value_get_boolean (value);
       break;
+    case GST_VAAPI_ENCODER_H264_PROP_CPB_LENGTH:
+      encoder->cpb_length = g_value_get_uint (value);
+      break;
+    case GST_VAAPI_ENCODER_H264_PROP_NUM_VIEWS:
+      encoder->num_views = g_value_get_uint (value);
+      break;
     default:
       return GST_VAAPI_ENCODER_STATUS_ERROR_INVALID_PARAMETER;
   }
@@ -2079,6 +2750,30 @@ gst_vaapi_encoder_h264_get_default_properties (void)
           "Enable adaptive use of 8x8 transforms in I-frames",
           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GstVaapiEncoderH264:cpb-length:
+   *
+   * The size of the CPB buffer in milliseconds.
+   */
+  GST_VAAPI_ENCODER_PROPERTIES_APPEND (props,
+      GST_VAAPI_ENCODER_H264_PROP_CPB_LENGTH,
+      g_param_spec_uint ("cpb-length",
+          "CPB Length", "Length of the CPB buffer in milliseconds",
+          1, 10000, DEFAULT_CPB_LENGTH,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
+   * GstVaapiEncoderH264:num-views:
+   *
+   * The number of views for MVC encoding .
+   */
+  GST_VAAPI_ENCODER_PROPERTIES_APPEND (props,
+      GST_VAAPI_ENCODER_H264_PROP_NUM_VIEWS,
+      g_param_spec_uint ("num-views",
+          "Number of Views",
+          "Number of Views for MVC encoding",
+          1, MAX_NUM_VIEWS, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   return props;
 }