msdkdec: use video memory if there's another MSDK context in a pipeline

1\ If downstream's pool is MSDK bufferpool,
2\ If there's shared GstMsdkContext in the pipeline,

a decoder decides to use video memory.

This policy should be improved to handle more cases.

https://bugzilla.gnome.org/show_bug.cgi?id=790752
diff --git a/sys/msdk/gstmsdkdec.c b/sys/msdk/gstmsdkdec.c
index bf5c8b1..b323cb7 100644
--- a/sys/msdk/gstmsdkdec.c
+++ b/sys/msdk/gstmsdkdec.c
@@ -396,11 +396,6 @@
   output_state->caps = gst_video_info_to_caps (&output_state->info);
   gst_video_codec_state_unref (output_state);
 
-  /* TODO: If downstream accepts msdk memory or dmabuf,
-   * this should be TRUE and using MFX_IOPATTERN_OUT_VIDEO_MEMORY
-   */
-  thiz->use_video_memory = FALSE;
-
   return TRUE;
 }
 
@@ -500,6 +495,7 @@
   if (gst_msdk_context_prepare (GST_ELEMENT_CAST (thiz), &thiz->context)) {
     GST_INFO_OBJECT (thiz, "Found context %" GST_PTR_FORMAT " from neighbour",
         thiz->context);
+    thiz->use_video_memory = TRUE;
 
     if (gst_msdk_context_get_job_type (thiz->context) & GST_MSDK_JOB_DECODER) {
       GstMsdkContext *parent_context;
@@ -520,7 +516,6 @@
   } else {
     gst_msdk_context_ensure_context (GST_ELEMENT_CAST (thiz), thiz->hardware,
         GST_MSDK_JOB_DECODER);
-
     GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT,
         thiz->context);
   }
@@ -572,9 +567,6 @@
   if (!gst_msdkdec_set_src_caps (thiz))
     return FALSE;
 
-  if (!gst_msdkdec_init_decoder (thiz))
-    return FALSE;
-
   gst_msdkdec_set_latency (thiz);
   return TRUE;
 }
@@ -634,6 +626,9 @@
   GstMapInfo map_info;
   guint i;
 
+  if (!thiz->initialized)
+    gst_video_decoder_negotiate (decoder);
+
   if (!gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ))
     return GST_FLOW_ERROR;
 
@@ -812,6 +807,10 @@
   gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
   pool_config = gst_buffer_pool_get_config (pool);
 
+  /* If downstream's pool is MSDK bufferpool, decoder is using video memory */
+  if (GST_IS_MSDK_BUFFER_POOL (pool))
+    thiz->use_video_memory = TRUE;
+
   /* Get the caps of pool and increase the min and max buffers by async_depth,
    * we will always have that number of decode operations in-flight */
   gst_buffer_pool_config_get_params (pool_config, &pool_caps, &size,
@@ -885,6 +884,12 @@
   if (pool)
     gst_object_unref (pool);
 
+  /* Initialize MSDK decoder before new bufferpool tries to alloc each buffer,
+   * which requires information of frame allocation.
+   */
+  if (!gst_msdkdec_init_decoder (thiz))
+    return FALSE;
+
   return TRUE;
 
 failed_to_create_pool: