decoder: h264: fix memory leak in PPS.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidecoder_h264.c
index a9ee127..7caabfb 100644 (file)
@@ -91,7 +91,9 @@ struct _GstVaapiParserInfoH264 {
         GstH264SliceHdr slice_hdr;
     }                   data;
     guint               state;
-    guint               flags; // Same as decoder unit flags (persistent)
+    guint               flags;      // Same as decoder unit flags (persistent)
+    guint               view_id;    // View ID of slice
+    guint               voc;        // View order index (VOIdx) of slice
 };
 
 static void
@@ -102,6 +104,9 @@ gst_vaapi_parser_info_h264_finalize(GstVaapiParserInfoH264 *pi)
     case GST_H264_NAL_SUBSET_SPS:
         gst_h264_sps_clear(&pi->data.sps);
         break;
+    case GST_H264_NAL_PPS:
+        gst_h264_pps_clear(&pi->data.pps);
+        break;
     case GST_H264_NAL_SEI:
         if (pi->data.sei) {
             g_array_unref(pi->data.sei);
@@ -146,6 +151,11 @@ gst_vaapi_parser_info_h264_new(void)
  * Extended picture flags:
  *
  * @GST_VAAPI_PICTURE_FLAG_IDR: flag that specifies an IDR picture
+ * @GST_VAAPI_PICTURE_FLAG_INTER_VIEW: flag that indicates the picture
+ *   may be used for inter-view prediction
+ * @GST_VAAPI_PICTURE_FLAG_ANCHOR: flag that specifies an anchor picture,
+ *   i.e. a picture that is decoded with only inter-view prediction,
+ *   and not inter prediction
  * @GST_VAAPI_PICTURE_FLAG_AU_START: flag that marks the start of an
  *   access unit (AU)
  * @GST_VAAPI_PICTURE_FLAG_AU_END: flag that marks the end of an
@@ -160,6 +170,8 @@ gst_vaapi_parser_info_h264_new(void)
 enum {
     GST_VAAPI_PICTURE_FLAG_IDR          = (GST_VAAPI_PICTURE_FLAG_LAST << 0),
     GST_VAAPI_PICTURE_FLAG_REFERENCE2   = (GST_VAAPI_PICTURE_FLAG_LAST << 1),
+    GST_VAAPI_PICTURE_FLAG_INTER_VIEW   = (GST_VAAPI_PICTURE_FLAG_LAST << 2),
+    GST_VAAPI_PICTURE_FLAG_ANCHOR       = (GST_VAAPI_PICTURE_FLAG_LAST << 3),
     GST_VAAPI_PICTURE_FLAG_AU_START     = (GST_VAAPI_PICTURE_FLAG_LAST << 4),
     GST_VAAPI_PICTURE_FLAG_AU_END       = (GST_VAAPI_PICTURE_FLAG_LAST << 5),
 
@@ -185,6 +197,12 @@ enum {
       GST_VAAPI_PICTURE_FLAGS_REFERENCE) ==                     \
      GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE)
 
+#define GST_VAAPI_PICTURE_IS_INTER_VIEW(picture) \
+    (GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_INTER_VIEW))
+
+#define GST_VAAPI_PICTURE_IS_ANCHOR(picture) \
+    (GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_ANCHOR))
+
 #define GST_VAAPI_PICTURE_H264(picture) \
     ((GstVaapiPictureH264 *)(picture))
 
@@ -271,6 +289,7 @@ struct _GstVaapiFrameStore {
     /*< private >*/
     GstVaapiMiniObject          parent_instance;
 
+    guint                       view_id;
     guint                       structure;
     GstVaapiPictureH264        *buffers[2];
     guint                       num_buffers;
@@ -302,6 +321,7 @@ gst_vaapi_frame_store_new(GstVaapiPictureH264 *picture)
     if (!fs)
         return NULL;
 
+    fs->view_id         = picture->base.view_id;
     fs->structure       = picture->structure;
     fs->buffers[0]      = gst_vaapi_picture_ref(picture);
     fs->buffers[1]      = NULL;
@@ -382,6 +402,18 @@ gst_vaapi_frame_store_has_reference(GstVaapiFrameStore *fs)
     return FALSE;
 }
 
+static gboolean
+gst_vaapi_frame_store_has_inter_view(GstVaapiFrameStore *fs)
+{
+    guint i;
+
+    for (i = 0; i < fs->num_buffers; i++) {
+        if (GST_VAAPI_PICTURE_IS_INTER_VIEW(fs->buffers[i]))
+            return TRUE;
+    }
+    return FALSE;
+}
+
 #define gst_vaapi_frame_store_ref(fs) \
     gst_vaapi_mini_object_ref(GST_VAAPI_MINI_OBJECT(fs))
 
@@ -416,6 +448,7 @@ struct _GstVaapiDecoderH264Private {
     GstH264NalParser           *parser;
     guint                       parser_state;
     guint                       decoder_state;
+    GstVaapiStreamAlignH264     stream_alignment;
     GstVaapiPictureH264        *current_picture;
     GstVaapiParserInfoH264     *sps[GST_H264_MAX_SPS_COUNT];
     GstVaapiParserInfoH264     *active_sps;
@@ -423,7 +456,8 @@ struct _GstVaapiDecoderH264Private {
     GstVaapiParserInfoH264     *active_pps;
     GstVaapiParserInfoH264     *prev_pi;
     GstVaapiParserInfoH264     *prev_slice_pi;
-    GstVaapiFrameStore         *prev_frame;
+    GstVaapiFrameStore        **prev_frames;
+    guint                       prev_frames_alloc;
     GstVaapiFrameStore        **dpb;
     guint                       dpb_count;
     guint                       dpb_size;
@@ -432,6 +466,7 @@ struct _GstVaapiDecoderH264Private {
     GstVaapiProfile             profile;
     GstVaapiEntrypoint          entrypoint;
     GstVaapiChromaType          chroma_type;
+    GPtrArray                  *inter_views;
     GstVaapiPictureH264        *short_ref[32];
     guint                       short_ref_count;
     GstVaapiPictureH264        *long_ref[32];
@@ -483,6 +518,17 @@ struct _GstVaapiDecoderH264Class {
 static gboolean
 exec_ref_pic_marking(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture);
 
+static gboolean
+is_inter_view_reference_for_next_pictures(GstVaapiDecoderH264 *decoder,
+    GstVaapiPictureH264 *picture);
+
+static inline gboolean
+is_inter_view_reference_for_next_frames(GstVaapiDecoderH264 *decoder,
+    GstVaapiFrameStore *fs)
+{
+    return is_inter_view_reference_for_next_pictures(decoder, fs->buffers[0]);
+}
+
 /* Determines if the supplied profile is one of the MVC set */
 static gboolean
 is_mvc_profile(GstH264Profile profile)
@@ -491,6 +537,13 @@ is_mvc_profile(GstH264Profile profile)
         profile == GST_H264_PROFILE_STEREO_HIGH;
 }
 
+/* Determines the view_id from the supplied NAL unit */
+static inline guint
+get_view_id(GstH264NalUnit *nalu)
+{
+    return GST_H264_IS_MVC_NALU(nalu) ? nalu->extension.mvc.view_id : 0;
+}
+
 /* Determines the view order index (VOIdx) from the supplied view_id */
 static gint
 get_view_order_index(GstH264SPS *sps, guint16 view_id)
@@ -639,11 +692,11 @@ dpb_output(
 {
     picture->output_needed = FALSE;
 
-    if (fs) {
-        if (--fs->output_needed > 0)
-            return TRUE;
-        picture = fs->buffers[0];
-    }
+    if (--fs->output_needed > 0)
+        return TRUE;
+
+    if (!GST_VAAPI_PICTURE_IS_COMPLETE(picture))
+        return TRUE;
     return gst_vaapi_picture_output(GST_VAAPI_PICTURE_CAST(picture));
 }
 
@@ -657,6 +710,23 @@ dpb_evict(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture, guint i)
         dpb_remove_index(decoder, i);
 }
 
+/* Finds the frame store holding the supplied picture */
+static gint
+dpb_find_picture(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    gint i, j;
+
+    for (i = 0; i < priv->dpb_count; i++) {
+        GstVaapiFrameStore * const fs = priv->dpb[i];
+        for (j = 0; j < fs->num_buffers; j++) {
+            if (fs->buffers[j] == picture)
+                return i;
+        }
+    }
+    return -1;
+}
+
 /* Finds the picture with the lowest POC that needs to be output */
 static gint
 dpb_find_lowest_poc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
@@ -670,11 +740,42 @@ dpb_find_lowest_poc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
         GstVaapiFrameStore * const fs = priv->dpb[i];
         if (!fs->output_needed)
             continue;
+        if (picture && picture->base.view_id != fs->view_id)
+            continue;
         for (j = 0; j < fs->num_buffers; j++) {
             GstVaapiPictureH264 * const pic = fs->buffers[j];
             if (!pic->output_needed)
                 continue;
-            if (!found_picture || found_picture->base.poc > pic->base.poc)
+            if (!found_picture || found_picture->base.poc > pic->base.poc ||
+                (found_picture->base.poc == pic->base.poc &&
+                 found_picture->base.voc > pic->base.voc))
+                found_picture = pic, found_index = i;
+        }
+    }
+
+    if (found_picture_ptr)
+        *found_picture_ptr = found_picture;
+    return found_picture ? found_index : -1;
+}
+
+/* Finds the picture with the lowest VOC that needs to be output */
+static gint
+dpb_find_lowest_voc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
+    GstVaapiPictureH264 **found_picture_ptr)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    GstVaapiPictureH264 *found_picture = NULL;
+    guint i, j, found_index;
+
+    for (i = 0; i < priv->dpb_count; i++) {
+        GstVaapiFrameStore * const fs = priv->dpb[i];
+        if (!fs->output_needed || fs->view_id == picture->base.view_id)
+            continue;
+        for (j = 0; j < fs->num_buffers; j++) {
+            GstVaapiPictureH264 * const pic = fs->buffers[j];
+            if (!pic->output_needed || pic->base.poc != picture->base.poc)
+                continue;
+            if (!found_picture || found_picture->base.voc > pic->base.voc)
                 found_picture = pic, found_index = i;
         }
     }
@@ -685,6 +786,34 @@ dpb_find_lowest_poc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
 }
 
 static gboolean
+dpb_output_other_views(GstVaapiDecoderH264 *decoder,
+    GstVaapiPictureH264 *picture, guint voc)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    GstVaapiPictureH264 *found_picture;
+    gint found_index;
+    gboolean success;
+
+    if (priv->max_views == 1)
+        return TRUE;
+
+    /* Emit all other view components that were in the same access
+       unit than the picture we have just found */
+    found_picture = picture;
+    for (;;) {
+        found_index = dpb_find_lowest_voc(decoder, found_picture,
+            &found_picture);
+        if (found_index < 0 || found_picture->base.voc >= voc)
+            break;
+        success = dpb_output(decoder, priv->dpb[found_index], found_picture);
+        dpb_evict(decoder, found_picture, found_index);
+        if (!success)
+            return FALSE;
+    }
+    return TRUE;
+}
+
+static gboolean
 dpb_bump(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 {
     GstVaapiDecoderH264Private * const priv = &decoder->priv;
@@ -696,8 +825,16 @@ dpb_bump(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
     if (found_index < 0)
         return FALSE;
 
+    if (picture && picture->base.poc != found_picture->base.poc)
+        dpb_output_other_views(decoder, found_picture, found_picture->base.voc);
+
     success = dpb_output(decoder, priv->dpb[found_index], found_picture);
     dpb_evict(decoder, found_picture, found_index);
+    if (priv->max_views == 1)
+        return success;
+
+    if (picture && picture->base.poc != found_picture->base.poc)
+        dpb_output_other_views(decoder, found_picture, G_MAXUINT32);
     return success;
 }
 
@@ -705,13 +842,34 @@ static void
 dpb_clear(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 {
     GstVaapiDecoderH264Private * const priv = &decoder->priv;
-    guint i;
+    guint i, n;
 
-    for (i = 0; i < priv->dpb_count; i++)
+    for (i = 0; i < priv->dpb_count; i++) {
+        if (picture && picture->base.view_id != priv->dpb[i]->view_id)
+            continue;
         gst_vaapi_frame_store_replace(&priv->dpb[i], NULL);
-    priv->dpb_count = 0;
+    }
 
-    gst_vaapi_frame_store_replace(&priv->prev_frame, NULL);
+    /* Compact the resulting DPB, i.e. remove holes */
+    for (i = 0, n = 0; i < priv->dpb_count; i++) {
+        if (priv->dpb[i]) {
+            if (i != n) {
+                priv->dpb[n] = priv->dpb[i];
+                priv->dpb[i] = NULL;
+            }
+            n++;
+        }
+    }
+    priv->dpb_count = n;
+
+    /* Clear previous frame buffers only if this is a "flush-all" operation,
+       or if the picture is the first one in the access unit */
+    if (priv->prev_frames && (!picture ||
+            GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
+                GST_VAAPI_PICTURE_FLAG_AU_START))) {
+        for (i = 0; i < priv->max_views; i++)
+            gst_vaapi_frame_store_replace(&priv->prev_frames[i], NULL);
+    }
 }
 
 static void
@@ -722,6 +880,28 @@ dpb_flush(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
     dpb_clear(decoder, picture);
 }
 
+static void
+dpb_prune_mvc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    const gboolean is_last_picture = /* in the access unit */
+        GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_AU_END);
+    guint i;
+
+    // Remove all unused inter-view only reference components of the current AU
+    i = 0;
+    while (i < priv->dpb_count) {
+        GstVaapiFrameStore * const fs = priv->dpb[i];
+        if (fs->view_id != picture->base.view_id &&
+            !fs->output_needed && !gst_vaapi_frame_store_has_reference(fs) &&
+            (is_last_picture ||
+             !is_inter_view_reference_for_next_frames(decoder, fs)))
+            dpb_remove_index(decoder, i);
+        else
+            i++;
+    }
+}
+
 static gboolean
 dpb_add(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 {
@@ -729,12 +909,16 @@ dpb_add(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
     GstVaapiFrameStore *fs;
     guint i;
 
+    if (priv->max_views > 1)
+        dpb_prune_mvc(decoder, picture);
+
     // Remove all unused pictures
     if (!GST_VAAPI_PICTURE_IS_IDR(picture)) {
         i = 0;
         while (i < priv->dpb_count) {
             GstVaapiFrameStore * const fs = priv->dpb[i];
-            if (!fs->output_needed && !gst_vaapi_frame_store_has_reference(fs))
+            if (fs->view_id == picture->base.view_id &&
+                !fs->output_needed && !gst_vaapi_frame_store_has_reference(fs))
                 dpb_remove_index(decoder, i);
             else
                 i++;
@@ -742,17 +926,34 @@ dpb_add(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
     }
 
     // Check if picture is the second field and the first field is still in DPB
-    fs = priv->prev_frame;
-    if (fs && !gst_vaapi_frame_store_has_frame(fs))
-        return gst_vaapi_frame_store_add(fs, picture);
+    if (GST_VAAPI_PICTURE_IS_INTERLACED(picture) &&
+        !GST_VAAPI_PICTURE_IS_FIRST_FIELD(picture)) {
+        const gint found_index = dpb_find_picture(decoder,
+            GST_VAAPI_PICTURE_H264(picture->base.parent_picture));
+        if (found_index >= 0)
+            return gst_vaapi_frame_store_add(priv->dpb[found_index], picture);
+
+        // ... also check the previous picture that was immediately output
+        fs = priv->prev_frames[picture->base.voc];
+        if (fs && &fs->buffers[0]->base == picture->base.parent_picture) {
+            if (!gst_vaapi_frame_store_add(fs, picture))
+                return FALSE;
+            return dpb_output(decoder, fs, picture);
+        }
+    }
 
     // Create new frame store, and split fields if necessary
     fs = gst_vaapi_frame_store_new(picture);
     if (!fs)
         return FALSE;
-    gst_vaapi_frame_store_replace(&priv->prev_frame, fs);
+    gst_vaapi_frame_store_replace(&priv->prev_frames[picture->base.voc], fs);
     gst_vaapi_frame_store_unref(fs);
 
+    if (picture->output_flag) {
+        picture->output_needed = TRUE;
+        fs->output_needed++;
+    }
+
     if (!priv->progressive_sequence && gst_vaapi_frame_store_has_frame(fs)) {
         if (!gst_vaapi_frame_store_split_fields(fs))
             return FALSE;
@@ -768,21 +969,25 @@ dpb_add(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 
     // C.4.5.2 - Storage and marking of a non-reference decoded picture into the DPB
     else {
-        if (!picture->output_flag)
+        const gboolean StoreInterViewOnlyRefFlag =
+            !GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
+                GST_VAAPI_PICTURE_FLAG_AU_END) &&
+            GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
+                GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
+        if (!picture->output_flag && !StoreInterViewOnlyRefFlag)
             return TRUE;
         while (priv->dpb_count == priv->dpb_size) {
-            if (dpb_find_lowest_poc(decoder, picture, NULL) < 0)
-                return dpb_output(decoder, NULL, picture);
+            GstVaapiPictureH264 *found_picture;
+            if (!StoreInterViewOnlyRefFlag) {
+                if (dpb_find_lowest_poc(decoder, picture, &found_picture) < 0 ||
+                    found_picture->base.poc > picture->base.poc)
+                    return dpb_output(decoder, fs, picture);
+            }
             if (!dpb_bump(decoder, picture))
                 return FALSE;
         }
     }
-
     gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
-    if (picture->output_flag) {
-        picture->output_needed = TRUE;
-        fs->output_needed++;
-    }
     return TRUE;
 }
 
@@ -791,9 +996,6 @@ dpb_reset(GstVaapiDecoderH264 *decoder, guint dpb_size)
 {
     GstVaapiDecoderH264Private * const priv = &decoder->priv;
 
-    if (dpb_size < priv->dpb_count)
-        return FALSE;
-
     if (dpb_size > priv->dpb_size_max) {
         priv->dpb = g_try_realloc_n(priv->dpb, dpb_size, sizeof(*priv->dpb));
         if (!priv->dpb)
@@ -802,16 +1004,52 @@ dpb_reset(GstVaapiDecoderH264 *decoder, guint dpb_size)
             (dpb_size - priv->dpb_size_max) * sizeof(*priv->dpb));
         priv->dpb_size_max = dpb_size;
     }
-
-    if (priv->dpb_size < dpb_size)
-        priv->dpb_size = dpb_size;
-    else if (dpb_size < priv->dpb_count)
-        return FALSE;
+    priv->dpb_size = dpb_size;
 
     GST_DEBUG("DPB size %u", priv->dpb_size);
     return TRUE;
 }
 
+static void
+unref_inter_view(GstVaapiPictureH264 *picture)
+{
+    if (!picture)
+        return;
+    GST_VAAPI_PICTURE_FLAG_UNSET(picture, GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
+    gst_vaapi_picture_unref(picture);
+}
+
+/* Resets MVC resources */
+static gboolean
+mvc_reset(GstVaapiDecoderH264 *decoder)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    guint i;
+
+    // Resize array of inter-view references
+    if (!priv->inter_views) {
+        priv->inter_views = g_ptr_array_new_full(priv->max_views,
+            (GDestroyNotify)unref_inter_view);
+        if (!priv->inter_views)
+            return FALSE;
+    }
+
+    // Resize array of previous frame buffers
+    for (i = priv->max_views; i < priv->prev_frames_alloc; i++)
+        gst_vaapi_frame_store_replace(&priv->prev_frames[i], NULL);
+
+    priv->prev_frames = g_try_realloc_n(priv->prev_frames, priv->max_views,
+        sizeof(*priv->prev_frames));
+    if (!priv->prev_frames) {
+        priv->prev_frames_alloc = 0;
+        return FALSE;
+    }
+    for (i = priv->prev_frames_alloc; i < priv->max_views; i++)
+        priv->prev_frames[i] = NULL;
+    priv->prev_frames_alloc = priv->max_views;
+    return TRUE;
+}
+
 static GstVaapiDecoderStatus
 get_status(GstH264ParserResult result)
 {
@@ -845,6 +1083,11 @@ gst_vaapi_decoder_h264_close(GstVaapiDecoderH264 *decoder)
 
     dpb_clear(decoder, NULL);
 
+    if (priv->inter_views) {
+        g_ptr_array_unref(priv->inter_views);
+        priv->inter_views = NULL;
+    }
+
     if (priv->parser) {
         gst_h264_nal_parser_free(priv->parser);
         priv->parser = NULL;
@@ -878,6 +1121,10 @@ gst_vaapi_decoder_h264_destroy(GstVaapiDecoder *base_decoder)
     priv->dpb = NULL;
     priv->dpb_size = 0;
 
+    g_free(priv->prev_frames);
+    priv->prev_frames = NULL;
+    priv->prev_frames_alloc = 0;
+
     for (i = 0; i < G_N_ELEMENTS(priv->pps); i++)
         gst_vaapi_parser_info_h264_replace(&priv->pps[i], NULL);
     gst_vaapi_parser_info_h264_replace(&priv->active_pps, NULL);
@@ -959,8 +1206,39 @@ fill_profiles(GstVaapiProfile profiles[16], guint *n_profiles_ptr,
     *n_profiles_ptr = n_profiles;
 }
 
+/* Fills in compatible profiles for MVC decoding */
+static void
+fill_profiles_mvc(GstVaapiDecoderH264 *decoder, GstVaapiProfile profiles[16],
+    guint *n_profiles_ptr, guint dpb_size)
+{
+    const gchar * const vendor_string =
+        gst_vaapi_display_get_vendor_string(GST_VAAPI_DECODER_DISPLAY(decoder));
+
+    gboolean add_high_profile = FALSE;
+    struct map {
+        const gchar *str;
+        guint str_len;
+    };
+    const struct map *m;
+
+    // Drivers that support slice level decoding
+    if (vendor_string && dpb_size <= 16) {
+        static const struct map drv_names[] = {
+            { "Intel i965 driver", 17 },
+            { NULL, 0 }
+        };
+        for (m = drv_names; m->str != NULL && !add_high_profile; m++) {
+            if (g_ascii_strncasecmp(vendor_string, m->str, m->str_len) == 0)
+                add_high_profile = TRUE;
+        }
+    }
+
+    if (add_high_profile)
+        fill_profiles(profiles, n_profiles_ptr, GST_VAAPI_PROFILE_H264_HIGH);
+}
+
 static GstVaapiProfile
-get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
+get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps, guint dpb_size)
 {
     GstVaapiDecoderH264Private * const priv = &decoder->priv;
     GstVaapiDisplay * const display = GST_VAAPI_DECODER_DISPLAY(decoder);
@@ -987,6 +1265,20 @@ get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
                 GST_VAAPI_PROFILE_H264_MAIN);
         }
         break;
+    case GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH:
+        if (priv->max_views == 2) {
+            fill_profiles(profiles, &n_profiles,
+                GST_VAAPI_PROFILE_H264_STEREO_HIGH);
+        }
+        fill_profiles_mvc(decoder, profiles, &n_profiles, dpb_size);
+        break;
+    case GST_VAAPI_PROFILE_H264_STEREO_HIGH:
+        if (sps->frame_mbs_only_flag) {
+            fill_profiles(profiles, &n_profiles,
+                GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH);
+        }
+        fill_profiles_mvc(decoder, profiles, &n_profiles, dpb_size);
+        break;
     default:
         break;
     }
@@ -1012,7 +1304,13 @@ ensure_context(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
     GstVaapiProfile profile;
     GstVaapiChromaType chroma_type;
     gboolean reset_context = FALSE;
-    guint mb_width, mb_height, dpb_size;
+    guint mb_width, mb_height, dpb_size, num_views;
+
+    num_views = get_num_views(sps);
+    if (priv->max_views < num_views) {
+        priv->max_views = num_views;
+        GST_DEBUG("maximum number of views changed to %u", num_views);
+    }
 
     dpb_size = get_max_dec_frame_buffering(sps);
     if (priv->dpb_size < dpb_size) {
@@ -1020,13 +1318,13 @@ ensure_context(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
         reset_context = TRUE;
     }
 
-    profile = get_profile(decoder, sps);
+    profile = get_profile(decoder, sps, dpb_size);
     if (!profile) {
         GST_ERROR("unsupported profile_idc %u", sps->profile_idc);
         return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
     }
 
-    if (priv->profile != profile) {
+    if (!priv->profile || (priv->profile != profile && priv->max_views == 1)) {
         GST_DEBUG("profile changed");
         reset_context = TRUE;
         priv->profile = profile;
@@ -1055,10 +1353,7 @@ ensure_context(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
     }
 
     priv->progressive_sequence = sps->frame_mbs_only_flag;
-#if 0
-    /* XXX: we only output complete frames for now */
     gst_vaapi_decoder_set_interlaced(base_decoder, !priv->progressive_sequence);
-#endif
 
     gst_vaapi_decoder_set_pixel_aspect_ratio(
         base_decoder,
@@ -1084,6 +1379,10 @@ ensure_context(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
     /* Reset DPB */
     if (!dpb_reset(decoder, dpb_size))
         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
+
+    /* Reset MVC data */
+    if (!mvc_reset(decoder))
+        return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
     return GST_VAAPI_DECODER_STATUS_SUCCESS;
 }
 
@@ -1098,7 +1397,7 @@ fill_iq_matrix_4x4(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps,
     g_assert(G_N_ELEMENTS(iq_matrix->ScalingList4x4[0]) == 16);
 
     for (i = 0; i < G_N_ELEMENTS(iq_matrix->ScalingList4x4); i++)
-        gst_h264_video_quant_matrix_4x4_get_raster_from_zigzag(
+        gst_h264_quant_matrix_4x4_get_raster_from_zigzag(
             iq_matrix->ScalingList4x4[i], pps->scaling_lists_4x4[i]);
 }
 
@@ -1117,7 +1416,7 @@ fill_iq_matrix_8x8(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps,
 
     n = (sps->chroma_format_idc != 3) ? 2 : 6;
     for (i = 0; i < n; i++) {
-        gst_h264_video_quant_matrix_8x8_get_raster_from_zigzag(
+        gst_h264_quant_matrix_8x8_get_raster_from_zigzag(
             iq_matrix->ScalingList8x8[i], pps->scaling_lists_8x8[i]);
     }
 }
@@ -1167,14 +1466,13 @@ decode_current_picture(GstVaapiDecoderH264 *decoder)
     if (!picture)
         return GST_VAAPI_DECODER_STATUS_SUCCESS;
 
+    if (!gst_vaapi_picture_decode(GST_VAAPI_PICTURE_CAST(picture)))
+        goto error;
     if (!exec_ref_pic_marking(decoder, picture))
         goto error;
     if (!dpb_add(decoder, picture))
         goto error;
-    if (!gst_vaapi_picture_decode(GST_VAAPI_PICTURE_CAST(picture)))
-        goto error;
-    if (priv->prev_frame && gst_vaapi_frame_store_has_frame(priv->prev_frame))
-        gst_vaapi_picture_replace(&priv->current_picture, NULL);
+    gst_vaapi_picture_replace(&priv->current_picture, NULL);
     return GST_VAAPI_DECODER_STATUS_SUCCESS;
 
 error:
@@ -1207,9 +1505,6 @@ parse_sps(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     if (result != GST_H264_PARSER_OK)
         return get_status(result);
 
-    /* Reset defaults */
-    priv->max_views = 1;
-
     priv->parser_state |= GST_H264_VIDEO_STATE_GOT_SPS;
     return GST_VAAPI_DECODER_STATUS_SUCCESS;
 }
@@ -1287,8 +1582,8 @@ parse_slice(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     GstVaapiParserInfoH264 * const pi = unit->parsed_info;
     GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
     GstH264NalUnit * const nalu = &pi->nalu;
+    GstH264SPS *sps;
     GstH264ParserResult result;
-    guint num_views;
 
     GST_DEBUG("parse slice");
 
@@ -1333,11 +1628,11 @@ parse_slice(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     if (result != GST_H264_PARSER_OK)
         return get_status(result);
 
-    num_views = get_num_views(slice_hdr->pps->sequence);
-    if (priv->max_views < num_views) {
-        priv->max_views = num_views;
-        GST_DEBUG("maximum number of views changed to %u", num_views);
-    }
+    sps = slice_hdr->pps->sequence;
+
+    /* Update MVC data */
+    pi->view_id = get_view_id(&pi->nalu);
+    pi->voc = get_view_order_index(sps, pi->view_id);
 
     priv->parser_state |= GST_H264_VIDEO_STATE_GOT_SLICE;
     return GST_VAAPI_DECODER_STATUS_SUCCESS;
@@ -1385,6 +1680,7 @@ decode_pps(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
 static GstVaapiDecoderStatus
 decode_sequence_end(GstVaapiDecoderH264 *decoder)
 {
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
     GstVaapiDecoderStatus status;
 
     GST_DEBUG("decode sequence-end");
@@ -1394,6 +1690,9 @@ decode_sequence_end(GstVaapiDecoderH264 *decoder)
         return status;
 
     dpb_flush(decoder, NULL);
+
+    /* Reset defaults, should there be a new sequence available next */
+    priv->max_views = 1;
     return GST_VAAPI_DECODER_STATUS_SUCCESS;
 }
 
@@ -1684,6 +1983,10 @@ init_picture_refs_pic_num(
     for (i = 0; i < priv->short_ref_count; i++) {
         GstVaapiPictureH264 * const pic = priv->short_ref[i];
 
+        // (H.8.2)
+        if (pic->base.view_id != picture->base.view_id)
+            continue;
+
         // (8-27)
         if (pic->frame_num > priv->frame_num)
             pic->frame_num_wrap = pic->frame_num - MaxFrameNum;
@@ -1704,6 +2007,10 @@ init_picture_refs_pic_num(
     for (i = 0; i < priv->long_ref_count; i++) {
         GstVaapiPictureH264 * const pic = priv->long_ref[i];
 
+        // (H.8.2)
+        if (pic->base.view_id != picture->base.view_id)
+            continue;
+
         // (8-29, 8-32, 8-33)
         if (GST_VAAPI_PICTURE_IS_FRAME(picture))
             pic->long_term_pic_num = pic->long_term_frame_idx;
@@ -1772,6 +2079,152 @@ init_picture_refs_fields(
     *RefPicList_count = n;
 }
 
+/* Finds the inter-view reference picture with the supplied view id */
+static GstVaapiPictureH264 *
+find_inter_view_reference(GstVaapiDecoderH264 *decoder, guint16 view_id)
+{
+    GPtrArray * const inter_views = decoder->priv.inter_views;
+    guint i;
+
+    for (i = 0; i < inter_views->len; i++) {
+        GstVaapiPictureH264 * const picture = g_ptr_array_index(inter_views, i);
+        if (picture->base.view_id == view_id)
+            return picture;
+    }
+
+    GST_WARNING("failed to find inter-view reference picture for view_id: %d",
+        view_id);
+    return NULL;
+}
+
+/* Checks whether the view id exists in the supplied list of view ids */
+static gboolean
+find_view_id(guint16 view_id, const guint16 *view_ids, guint num_view_ids)
+{
+    guint i;
+
+    for (i = 0; i < num_view_ids; i++) {
+        if (view_ids[i] == view_id)
+            return TRUE;
+    }
+    return FALSE;
+}
+
+static gboolean
+find_view_id_in_view(guint16 view_id, const GstH264SPSExtMVCView *view,
+    gboolean is_anchor)
+{
+    if (is_anchor)
+        return (find_view_id(view_id, view->anchor_ref_l0,
+                    view->num_anchor_refs_l0) ||
+                find_view_id(view_id, view->anchor_ref_l1,
+                    view->num_anchor_refs_l1));
+
+    return (find_view_id(view_id, view->non_anchor_ref_l0,
+                view->num_non_anchor_refs_l0) ||
+            find_view_id(view_id, view->non_anchor_ref_l1,
+                view->num_non_anchor_refs_l1));
+}
+
+/* Checks whether the inter-view reference picture with the supplied
+   view id is used for decoding the current view component picture */
+static gboolean
+is_inter_view_reference_for_picture(GstVaapiDecoderH264 *decoder,
+    guint16 view_id, GstVaapiPictureH264 *picture)
+{
+    const GstH264SPS * const sps = get_sps(decoder);
+    gboolean is_anchor;
+
+    if (!GST_VAAPI_PICTURE_IS_MVC(picture) ||
+        sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
+        return FALSE;
+
+    is_anchor = GST_VAAPI_PICTURE_IS_ANCHOR(picture);
+    return find_view_id_in_view(view_id,
+        &sps->extension.mvc.view[picture->base.voc], is_anchor);
+}
+
+/* Checks whether the supplied inter-view reference picture is used
+   for decoding the next view component pictures */
+static gboolean
+is_inter_view_reference_for_next_pictures(GstVaapiDecoderH264 *decoder,
+    GstVaapiPictureH264 *picture)
+{
+    const GstH264SPS * const sps = get_sps(decoder);
+    gboolean is_anchor;
+    guint i, num_views;
+
+    if (!GST_VAAPI_PICTURE_IS_MVC(picture) ||
+        sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
+        return FALSE;
+
+    is_anchor = GST_VAAPI_PICTURE_IS_ANCHOR(picture);
+    num_views = sps->extension.mvc.num_views_minus1 + 1;
+    for (i = picture->base.voc + 1; i < num_views; i++) {
+        const GstH264SPSExtMVCView * const view = &sps->extension.mvc.view[i];
+        if (find_view_id_in_view(picture->base.view_id, view, is_anchor))
+            return TRUE;
+    }
+    return FALSE;
+}
+
+/* H.8.2.1 - Initialization process for inter-view prediction references */
+static void
+init_picture_refs_mvc_1(GstVaapiDecoderH264 *decoder,
+    GstVaapiPictureH264 **ref_list, guint *ref_list_count_ptr, guint num_refs,
+    const guint16 *view_ids, guint num_view_ids)
+{
+    guint j, n;
+
+    n = *ref_list_count_ptr;
+    for (j = 0; j < num_view_ids && n < num_refs; j++) {
+        GstVaapiPictureH264 * const pic =
+            find_inter_view_reference(decoder, view_ids[j]);
+        if (pic)
+            ref_list[n++] = pic;
+    }
+    *ref_list_count_ptr = n;
+}
+
+static inline void
+init_picture_refs_mvc(GstVaapiDecoderH264 *decoder,
+    GstVaapiPictureH264 *picture, GstH264SliceHdr *slice_hdr, guint list)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    const GstH264SPS * const sps = get_sps(decoder);
+    const GstH264SPSExtMVCView *view;
+
+    GST_DEBUG("initialize reference picture list for inter-view prediction");
+
+    if (sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
+        return;
+    view = &sps->extension.mvc.view[picture->base.voc];
+
+#define INVOKE_INIT_PICTURE_REFS_MVC(ref_list, view_list) do {          \
+        init_picture_refs_mvc_1(decoder,                                \
+            priv->RefPicList##ref_list,                                 \
+            &priv->RefPicList##ref_list##_count,                        \
+            slice_hdr->num_ref_idx_l##ref_list##_active_minus1 + 1,     \
+            view->view_list##_l##ref_list,                              \
+            view->num_##view_list##s_l##ref_list);                      \
+    } while (0)
+
+    if (list == 0) {
+        if (GST_VAAPI_PICTURE_IS_ANCHOR(picture))
+            INVOKE_INIT_PICTURE_REFS_MVC(0, anchor_ref);
+        else
+            INVOKE_INIT_PICTURE_REFS_MVC(0, non_anchor_ref);
+    }
+    else {
+        if (GST_VAAPI_PICTURE_IS_ANCHOR(picture))
+            INVOKE_INIT_PICTURE_REFS_MVC(1, anchor_ref);
+        else
+            INVOKE_INIT_PICTURE_REFS_MVC(1, non_anchor_ref);
+    }
+
+#undef INVOKE_INIT_PICTURE_REFS_MVC
+}
+
 static void
 init_picture_refs_p_slice(
     GstVaapiDecoderH264 *decoder,
@@ -1831,6 +2284,11 @@ init_picture_refs_p_slice(
             long_ref,           long_ref_count
         );
     }
+
+    if (GST_VAAPI_PICTURE_IS_MVC(picture)) {
+        /* RefPicList0 */
+        init_picture_refs_mvc(decoder, picture, slice_hdr, 0);
+    }
 }
 
 static void
@@ -1987,6 +2445,14 @@ init_picture_refs_b_slice(
         priv->RefPicList1[0] = priv->RefPicList1[1];
         priv->RefPicList1[1] = tmp;
     }
+
+    if (GST_VAAPI_PICTURE_IS_MVC(picture)) {
+        /* RefPicList0 */
+        init_picture_refs_mvc(decoder, picture, slice_hdr, 0);
+
+        /* RefPicList1 */
+        init_picture_refs_mvc(decoder, picture, slice_hdr, 1);
+    }
 }
 
 #undef SORT_REF_LIST
@@ -2035,9 +2501,10 @@ exec_picture_refs_modification_1(
     guint num_ref_pic_list_modifications;
     GstVaapiPictureH264 **ref_list;
     guint *ref_list_count_ptr, ref_list_count, ref_list_idx = 0;
-    guint i, j, n, num_refs;
+    const guint16 *view_ids = NULL;
+    guint i, j, n, num_refs, num_view_ids = 0;
     gint found_ref_idx;
-    gint32 MaxPicNum, CurrPicNum, picNumPred;
+    gint32 MaxPicNum, CurrPicNum, picNumPred, picViewIdxPred;
 
     GST_DEBUG("modification process of reference picture list %u", list);
 
@@ -2047,6 +2514,20 @@ exec_picture_refs_modification_1(
         ref_list                       = priv->RefPicList0;
         ref_list_count_ptr             = &priv->RefPicList0_count;
         num_refs                       = slice_hdr->num_ref_idx_l0_active_minus1 + 1;
+
+        if (GST_VAAPI_PICTURE_IS_MVC(picture) &&
+            sps->extension_type == GST_H264_NAL_EXTENSION_MVC) {
+            const GstH264SPSExtMVCView * const view =
+                &sps->extension.mvc.view[picture->base.voc];
+            if (GST_VAAPI_PICTURE_IS_ANCHOR(picture)) {
+                view_ids = view->anchor_ref_l0;
+                num_view_ids = view->num_anchor_refs_l0;
+            }
+            else {
+                view_ids = view->non_anchor_ref_l0;
+                num_view_ids = view->num_non_anchor_refs_l0;
+            }
+        }
     }
     else {
         ref_pic_list_modification      = slice_hdr->ref_pic_list_modification_l1;
@@ -2054,6 +2535,20 @@ exec_picture_refs_modification_1(
         ref_list                       = priv->RefPicList1;
         ref_list_count_ptr             = &priv->RefPicList1_count;
         num_refs                       = slice_hdr->num_ref_idx_l1_active_minus1 + 1;
+
+        if (GST_VAAPI_PICTURE_IS_MVC(picture) &&
+            sps->extension_type == GST_H264_NAL_EXTENSION_MVC) {
+            const GstH264SPSExtMVCView * const view =
+                &sps->extension.mvc.view[picture->base.voc];
+            if (GST_VAAPI_PICTURE_IS_ANCHOR(picture)) {
+                view_ids = view->anchor_ref_l1;
+                num_view_ids = view->num_anchor_refs_l1;
+            }
+            else {
+                view_ids = view->non_anchor_ref_l1;
+                num_view_ids = view->num_non_anchor_refs_l1;
+            }
+        }
     }
     ref_list_count = *ref_list_count_ptr;
 
@@ -2067,6 +2562,7 @@ exec_picture_refs_modification_1(
     }
 
     picNumPred = CurrPicNum;
+    picViewIdxPred = -1;
 
     for (i = 0; i < num_ref_pic_list_modifications; i++) {
         GstH264RefPicListModification * const l = &ref_pic_list_modification[i];
@@ -2112,7 +2608,8 @@ exec_picture_refs_modification_1(
                 PicNumF =
                     GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(ref_list[j]) ?
                     ref_list[j]->pic_num : MaxPicNum;
-                if (PicNumF != picNum)
+                if (PicNumF != picNum ||
+                    ref_list[j]->base.view_id != picture->base.view_id)
                     ref_list[n++] = ref_list[j];
             }
         }
@@ -2134,7 +2631,49 @@ exec_picture_refs_modification_1(
                 LongTermPicNumF =
                     GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(ref_list[j]) ?
                     ref_list[j]->long_term_pic_num : INT_MAX;
-                if (LongTermPicNumF != l->value.long_term_pic_num)
+                if (LongTermPicNumF != l->value.long_term_pic_num ||
+                    ref_list[j]->base.view_id != picture->base.view_id)
+                    ref_list[n++] = ref_list[j];
+            }
+        }
+
+        /* H.8.2.2.3 - Inter-view prediction reference pictures */
+        else if ((GST_VAAPI_PICTURE_IS_MVC(picture) &&
+                  sps->extension_type == GST_H264_NAL_EXTENSION_MVC) &&
+                 (l->modification_of_pic_nums_idc == 4 ||
+                  l->modification_of_pic_nums_idc == 5)) {
+            gint32 abs_diff_view_idx = l->value.abs_diff_view_idx_minus1 + 1;
+            gint32 picViewIdx, targetViewId;
+
+            // (H-6)
+            if (l->modification_of_pic_nums_idc == 4) {
+                picViewIdx = picViewIdxPred - abs_diff_view_idx;
+                if (picViewIdx < 0)
+                    picViewIdx += num_view_ids;
+            }
+
+            // (H-7)
+            else {
+                picViewIdx = picViewIdxPred + abs_diff_view_idx;
+                if (picViewIdx >= num_view_ids)
+                    picViewIdx -= num_view_ids;
+            }
+            picViewIdxPred = picViewIdx;
+
+            // (H-8, H-9)
+            targetViewId = view_ids[picViewIdx];
+
+            // (H-10)
+            for (j = num_refs; j > ref_list_idx; j--)
+                ref_list[j] = ref_list[j - 1];
+            ref_list[ref_list_idx++] =
+                find_inter_view_reference(decoder, targetViewId);
+            n = ref_list_idx;
+            for (j = ref_list_idx; j <= num_refs; j++) {
+                if (!ref_list[j])
+                    continue;
+                if (ref_list[j]->base.view_id != targetViewId ||
+                    ref_list[j]->base.poc != picture->base.poc)
                     ref_list[n++] = ref_list[j];
             }
         }
@@ -2185,6 +2724,8 @@ init_picture_ref_lists(GstVaapiDecoderH264 *decoder,
             if (!gst_vaapi_frame_store_has_frame(fs))
                 continue;
             pic = fs->buffers[0];
+            if (pic->base.view_id != picture->base.view_id)
+                continue;
             if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(pic))
                 priv->short_ref[short_ref_count++] = pic;
             else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(pic))
@@ -2198,6 +2739,8 @@ init_picture_ref_lists(GstVaapiDecoderH264 *decoder,
             GstVaapiFrameStore * const fs = priv->dpb[i];
             for (j = 0; j < fs->num_buffers; j++) {
                 GstVaapiPictureH264 * const pic = fs->buffers[j];
+                if (pic->base.view_id != picture->base.view_id)
+                    continue;
                 if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(pic))
                     priv->short_ref[short_ref_count++] = pic;
                 else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(pic))
@@ -2283,9 +2826,27 @@ init_picture(
     picture->output_flag        = TRUE; /* XXX: conformant to Annex A only */
     base_picture->pts           = GST_VAAPI_DECODER_CODEC_FRAME(decoder)->pts;
     base_picture->type          = GST_VAAPI_PICTURE_TYPE_NONE;
+    base_picture->view_id       = pi->view_id;
+    base_picture->voc           = pi->voc;
+
+    /* Initialize extensions */
+    switch (pi->nalu.extension_type) {
+    case GST_H264_NAL_EXTENSION_MVC: {
+        GstH264NalUnitExtensionMVC * const mvc = &pi->nalu.extension.mvc;
+
+        GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_MVC);
+        if (mvc->inter_view_flag)
+            GST_VAAPI_PICTURE_FLAG_SET(picture,
+                GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
+        if (mvc->anchor_pic_flag)
+            GST_VAAPI_PICTURE_FLAG_SET(picture,
+                GST_VAAPI_PICTURE_FLAG_ANCHOR);
+        break;
+    }
+    }
 
     /* Reset decoder state for IDR pictures */
-    if (pi->nalu.type == GST_H264_NAL_SLICE_IDR) {
+    if (pi->nalu.idr_pic_flag) {
         GST_DEBUG("<IDR>");
         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_IDR);
         dpb_flush(decoder, picture);
@@ -2600,6 +3161,9 @@ exec_ref_pic_marking(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
     priv->prev_pic_has_mmco5 = FALSE;
     priv->prev_pic_structure = picture->structure;
 
+    if (GST_VAAPI_PICTURE_IS_INTER_VIEW(picture))
+        g_ptr_array_add(priv->inter_views, gst_vaapi_picture_ref(picture));
+
     if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture))
         return TRUE;
 
@@ -2666,6 +3230,23 @@ vaapi_fill_picture(VAPictureH264 *pic, GstVaapiPictureH264 *picture,
     }
 }
 
+static void
+vaapi_fill_picture_for_RefPicListX(VAPictureH264 *pic,
+    GstVaapiPictureH264 *picture)
+{
+    vaapi_fill_picture(pic, picture, 0);
+
+    /* H.8.4 - MVC inter prediction and inter-view prediction process */
+    if (GST_VAAPI_PICTURE_IS_INTER_VIEW(picture)) {
+        /* The inter-view reference components and inter-view only
+           reference components that are included in the reference
+           picture lists are considered as not being marked as "used for
+           short-term reference" or "used for long-term reference" */
+        pic->flags &= ~(VA_PICTURE_H264_SHORT_TERM_REFERENCE|
+                        VA_PICTURE_H264_LONG_TERM_REFERENCE);
+    }
+}
+
 static gboolean
 fill_picture(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 {
@@ -2681,9 +3262,14 @@ fill_picture(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
 
     for (i = 0, n = 0; i < priv->dpb_count; i++) {
         GstVaapiFrameStore * const fs = priv->dpb[i];
-        if (gst_vaapi_frame_store_has_reference(fs))
+        if ((gst_vaapi_frame_store_has_reference(fs) &&
+             fs->view_id == picture->base.view_id) ||
+            (gst_vaapi_frame_store_has_inter_view(fs) &&
+             is_inter_view_reference_for_picture(decoder, fs->view_id, picture)))
             vaapi_fill_picture(&pic_param->ReferenceFrames[n++],
                 fs->buffers[0], fs->structure);
+        if (n >= G_N_ELEMENTS(pic_param->ReferenceFrames))
+            break;
     }
     for (; n < G_N_ELEMENTS(pic_param->ReferenceFrames); n++)
         vaapi_init_picture(&pic_param->ReferenceFrames[n]);
@@ -2763,7 +3349,7 @@ is_new_picture(GstVaapiParserInfoH264 *pi, GstVaapiParserInfoH264 *prev_pi)
 
     /* view_id differs in value and VOIdx of current slice_hdr is less
        than the VOIdx of the prev_slice_hdr */
-    CHECK_VALUE(&pi->nalu.extension.mvc, &prev_pi->nalu.extension.mvc, view_id);
+    CHECK_VALUE(pi, prev_pi, view_id);
 
     /* frame_num differs in value, regardless of inferred values to 0 */
     CHECK_VALUE(slice_hdr, prev_slice_hdr, frame_num);
@@ -2811,29 +3397,32 @@ is_new_picture(GstVaapiParserInfoH264 *pi, GstVaapiParserInfoH264 *prev_pi)
 
 /* Detection of a new access unit, assuming we are already in presence
    of a new picture */
-static gboolean
+static inline gboolean
 is_new_access_unit(GstVaapiParserInfoH264 *pi, GstVaapiParserInfoH264 *prev_pi)
 {
-    GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
-    GstH264SliceHdr *prev_slice_hdr;
-    GstH264NalUnitExtensionMVC *mvc, *prev_mvc;
-    gint voc, prev_voc;
+    if (!prev_pi || prev_pi->view_id == pi->view_id)
+        return TRUE;
+    return pi->voc < prev_pi->voc;
+}
 
-    g_return_val_if_fail(is_new_picture(pi, prev_pi), FALSE);
+/* Finds the first field picture corresponding to the supplied picture */
+static GstVaapiPictureH264 *
+find_first_field(GstVaapiDecoderH264 *decoder, GstVaapiParserInfoH264 *pi)
+{
+    GstVaapiDecoderH264Private * const priv = &decoder->priv;
+    GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
+    GstVaapiFrameStore *fs;
 
-    if (!prev_pi)
-        return TRUE;
-    prev_slice_hdr = &prev_pi->data.slice_hdr;
+    if (!slice_hdr->field_pic_flag)
+        return NULL;
 
-    mvc = &pi->nalu.extension.mvc;
-    prev_mvc = &prev_pi->nalu.extension.mvc;
-    if (mvc->view_id == prev_mvc->view_id)
-        return TRUE;
+    fs = priv->prev_frames[pi->voc];
+    if (!fs || gst_vaapi_frame_store_has_frame(fs))
+        return NULL;
 
-    voc = get_view_order_index(slice_hdr->pps->sequence, mvc->view_id);
-    prev_voc = get_view_order_index(prev_slice_hdr->pps->sequence,
-        prev_mvc->view_id);
-    return voc < prev_voc;
+    if (fs->buffers[0]->frame_num == slice_hdr->frame_num)
+        return fs->buffers[0];
+    return NULL;
 }
 
 static GstVaapiDecoderStatus
@@ -2844,7 +3433,7 @@ decode_picture(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
     GstH264PPS * const pps = ensure_pps(decoder, slice_hdr->pps);
     GstH264SPS * const sps = ensure_sps(decoder, slice_hdr->pps->sequence);
-    GstVaapiPictureH264 *picture;
+    GstVaapiPictureH264 *picture, *first_field;
     GstVaapiDecoderStatus status;
 
     g_return_val_if_fail(pps != NULL, GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN);
@@ -2854,7 +3443,7 @@ decode_picture(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     switch (sps->profile_idc) {
     case GST_H264_PROFILE_MULTIVIEW_HIGH:
     case GST_H264_PROFILE_STEREO_HIGH:
-        if (1) {
+        if (0) {
             GST_DEBUG("drop picture from substream");
             return GST_VAAPI_DECODER_STATUS_DROP_FRAME;
         }
@@ -2867,9 +3456,10 @@ decode_picture(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
 
     priv->decoder_state = 0;
 
-    if (priv->current_picture) {
+    first_field = find_first_field(decoder, pi);
+    if (first_field) {
         /* Re-use current picture where the first field was decoded */
-        picture = gst_vaapi_picture_h264_new_field(priv->current_picture);
+        picture = gst_vaapi_picture_h264_new_field(first_field);
         if (!picture) {
             GST_ERROR("failed to allocate field picture");
             return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
@@ -2886,6 +3476,11 @@ decode_picture(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
     gst_vaapi_picture_replace(&priv->current_picture, picture);
     gst_vaapi_picture_unref(picture);
 
+    /* Clear inter-view references list if this is the primary coded
+       picture of the current access unit */
+    if (pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_START)
+        g_ptr_array_set_size(priv->inter_views, 0);
+
     /* Update cropping rectangle */
     if (sps->frame_cropping_flag) {
         GstVaapiRectangle crop_rect;
@@ -3014,7 +3609,8 @@ fill_RefPicList(GstVaapiDecoderH264 *decoder,
         slice_hdr->num_ref_idx_l0_active_minus1;
 
     for (i = 0; i < priv->RefPicList0_count && priv->RefPicList0[i]; i++)
-        vaapi_fill_picture(&slice_param->RefPicList0[i], priv->RefPicList0[i], 0);
+        vaapi_fill_picture_for_RefPicListX(&slice_param->RefPicList0[i],
+            priv->RefPicList0[i]);
     for (; i <= slice_param->num_ref_idx_l0_active_minus1; i++)
         vaapi_init_picture(&slice_param->RefPicList0[i]);
 
@@ -3025,7 +3621,8 @@ fill_RefPicList(GstVaapiDecoderH264 *decoder,
         slice_hdr->num_ref_idx_l1_active_minus1;
 
     for (i = 0; i < priv->RefPicList1_count && priv->RefPicList1[i]; i++)
-        vaapi_fill_picture(&slice_param->RefPicList1[i], priv->RefPicList1[i], 0);
+        vaapi_fill_picture_for_RefPicListX(&slice_param->RefPicList1[i],
+            priv->RefPicList1[i]);
     for (; i <= slice_param->num_ref_idx_l1_active_minus1; i++)
         vaapi_init_picture(&slice_param->RefPicList1[i]);
     return TRUE;
@@ -3293,12 +3890,21 @@ gst_vaapi_decoder_h264_parse(GstVaapiDecoder *base_decoder,
     guint i, size, buf_size, nalu_size, flags;
     guint32 start_code;
     gint ofs, ofs2;
+    gboolean at_au_end = FALSE;
 
     status = ensure_decoder(decoder);
     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
         return status;
 
-    size = gst_adapter_available(adapter);
+    switch (priv->stream_alignment) {
+    case GST_VAAPI_STREAM_ALIGN_H264_NALU:
+    case GST_VAAPI_STREAM_ALIGN_H264_AU:
+        size = gst_adapter_available_fast(adapter);
+        break;
+    default:
+        size = gst_adapter_available(adapter);
+        break;
+    }
 
     if (priv->is_avcC) {
         if (size < priv->nal_length_size)
@@ -3315,35 +3921,44 @@ gst_vaapi_decoder_h264_parse(GstVaapiDecoder *base_decoder,
         buf_size = priv->nal_length_size + nalu_size;
         if (size < buf_size)
             return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
+        else if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_AU)
+            at_au_end = (buf_size == size);
     }
     else {
         if (size < 4)
             return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
 
-        ofs = scan_for_start_code(adapter, 0, size, NULL);
-        if (ofs < 0)
-            return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
-
-        if (ofs > 0) {
-            gst_adapter_flush(adapter, ofs);
-            size -= ofs;
-        }
+        if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_NALU)
+            buf_size = size;
+        else {
+            ofs = scan_for_start_code(adapter, 0, size, NULL);
+            if (ofs < 0)
+                return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
 
-        ofs2 = ps->input_offset2 - ofs - 4;
-        if (ofs2 < 4)
-            ofs2 = 4;
+            if (ofs > 0) {
+                gst_adapter_flush(adapter, ofs);
+                size -= ofs;
+            }
 
-        ofs = G_UNLIKELY(size < ofs2 + 4) ? -1 :
-            scan_for_start_code(adapter, ofs2, size - ofs2, NULL);
-        if (ofs < 0) {
-            // Assume the whole NAL unit is present if end-of-stream
-            if (!at_eos) {
-                ps->input_offset2 = size;
-                return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
+            ofs2 = ps->input_offset2 - ofs - 4;
+            if (ofs2 < 4)
+                ofs2 = 4;
+
+            ofs = G_UNLIKELY(size < ofs2 + 4) ? -1 :
+                scan_for_start_code(adapter, ofs2, size - ofs2, NULL);
+            if (ofs < 0) {
+                // Assume the whole NAL unit is present if end-of-stream
+                // or stream buffers aligned on access unit boundaries
+                if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_AU)
+                    at_au_end = TRUE;
+                else if (!at_eos) {
+                    ps->input_offset2 = size;
+                    return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
+                }
+                ofs = size;
             }
-            ofs = size;
+            buf_size = ofs;
         }
-        buf_size = ofs;
     }
     ps->input_offset2 = 0;
 
@@ -3401,6 +4016,10 @@ gst_vaapi_decoder_h264_parse(GstVaapiDecoder *base_decoder,
         return status;
 
     flags = 0;
+    if (at_au_end) {
+        flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END |
+            GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
+    }
     switch (pi->nalu.type) {
     case GST_H264_NAL_AU_DELIMITER:
         flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
@@ -3432,7 +4051,12 @@ gst_vaapi_decoder_h264_parse(GstVaapiDecoder *base_decoder,
     case GST_H264_NAL_SLICE_IDR:
     case GST_H264_NAL_SLICE:
         flags |= GST_VAAPI_DECODER_UNIT_FLAG_SLICE;
-        if (is_new_picture(pi, priv->prev_slice_pi)) {
+        if (priv->prev_pi &&
+            (priv->prev_pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_END)) {
+            flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
+                GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
+        }
+        else if (is_new_picture(pi, priv->prev_slice_pi)) {
             flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
             if (is_new_access_unit(pi, priv->prev_slice_pi))
                 flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
@@ -3546,6 +4170,24 @@ gst_vaapi_decoder_h264_class(void)
 }
 
 /**
+ * gst_vaapi_decoder_h264_set_alignment:
+ * @decoder: a #GstVaapiDecoderH264
+ * @alignment: the #GstVaapiStreamAlignH264
+ *
+ * Specifies how stream buffers are aligned / fed, i.e. the boundaries
+ * of each buffer that is supplied to the decoder. This could be no
+ * specific alignment, NAL unit boundaries, or access unit boundaries.
+ */
+void
+gst_vaapi_decoder_h264_set_alignment(GstVaapiDecoderH264 *decoder,
+    GstVaapiStreamAlignH264 alignment)
+{
+    g_return_if_fail(decoder != NULL);
+
+    decoder->priv.stream_alignment = alignment;
+}
+
+/**
  * gst_vaapi_decoder_h264_new:
  * @display: a #GstVaapiDisplay
  * @caps: a #GstCaps holding codec information