va: Add and use common decode negotiate vmethod.
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvabasedec.c
index 4510fc0..4b168bf 100644 (file)
@@ -101,9 +101,8 @@ gst_va_base_dec_stop (GstVideoDecoder * decoder)
   if (!gst_va_decoder_close (base->decoder))
     return FALSE;
 
-  if (base->output_state)
-    gst_video_codec_state_unref (base->output_state);
-  base->output_state = NULL;
+  g_clear_pointer (&base->output_state, gst_video_codec_state_unref);
+  g_clear_pointer (&base->input_state, gst_video_codec_state_unref);
 
   if (base->other_pool)
     gst_buffer_pool_set_active (base->other_pool, FALSE);
@@ -662,10 +661,41 @@ gst_va_base_dec_set_context (GstElement * element, GstContext * context)
       (element))->set_context (element, context);
 }
 
+static gboolean
+gst_va_base_dec_negotiate (GstVideoDecoder * decoder)
+{
+  GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
+
+  /* Ignore downstream renegotiation request. */
+  if (!base->need_negotiation)
+    return TRUE;
+
+  base->need_negotiation = FALSE;
+
+  if (!gst_va_decoder_config_is_equal (base->decoder, base->profile,
+          base->rt_format, base->width, base->height)) {
+    if (gst_va_decoder_is_open (base->decoder) &&
+        !gst_va_decoder_close (base->decoder))
+      return FALSE;
+    if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
+      return FALSE;
+    if (!gst_va_decoder_set_frame_size (base->decoder, base->width,
+            base->height))
+      return FALSE;
+  }
+
+  if (!gst_va_base_dec_set_output_state (base))
+    return FALSE;
+
+  return GST_VIDEO_DECODER_CLASS (GST_VA_BASE_DEC_GET_PARENT_CLASS (decoder))
+      ->negotiate (decoder);
+}
+
 void
 gst_va_base_dec_init (GstVaBaseDec * base, GstDebugCategory * cat)
 {
   base->debug_category = cat;
+  gst_video_info_init (&base->output_info);
 }
 
 void
@@ -713,6 +743,7 @@ gst_va_base_dec_class_init (GstVaBaseDecClass * klass, GstVaCodecs codec,
   decoder_class->sink_query = GST_DEBUG_FUNCPTR (gst_va_base_dec_sink_query);
   decoder_class->decide_allocation =
       GST_DEBUG_FUNCPTR (gst_va_base_dec_decide_allocation);
+  decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_base_dec_negotiate);
 
   g_object_class_install_property (object_class, GST_VA_DEC_PROP_DEVICE_PATH,
       g_param_spec_string ("device-path", "Device Path",
@@ -978,7 +1009,7 @@ gst_va_base_dec_copy_output_buffer (GstVaBaseDec * base,
 
   src_vinfo = &base->output_state->info;
   gst_video_info_set_format (&dest_vinfo, GST_VIDEO_INFO_FORMAT (src_vinfo),
-      base->width, base->height);
+      GST_VIDEO_INFO_WIDTH (src_vinfo), GST_VIDEO_INFO_HEIGHT (src_vinfo));
 
   ret = gst_buffer_pool_acquire_buffer (base->other_pool, &buffer, NULL);
   if (ret != GST_FLOW_OK)
@@ -1003,8 +1034,8 @@ gst_va_base_dec_copy_output_buffer (GstVaBaseDec * base,
   } else {
     /* gst_video_frame_copy can crop this, but does not know, so let
      * make it think it's all right */
-    GST_VIDEO_INFO_WIDTH (&src_frame.info) = base->width;
-    GST_VIDEO_INFO_HEIGHT (&src_frame.info) = base->height;
+    GST_VIDEO_INFO_WIDTH (&src_frame.info) = GST_VIDEO_INFO_WIDTH (src_vinfo);
+    GST_VIDEO_INFO_HEIGHT (&src_frame.info) = GST_VIDEO_INFO_HEIGHT (src_vinfo);
 
     if (!gst_video_frame_copy (&dest_frame, &src_frame)) {
       gst_video_frame_unmap (&src_frame);
@@ -1070,3 +1101,35 @@ gst_va_base_dec_prepare_output_frame (GstVaBaseDec * base,
     return gst_video_decoder_allocate_output_frame (vdec, frame);
   return GST_FLOW_OK;
 }
+
+gboolean
+gst_va_base_dec_set_output_state (GstVaBaseDec * base)
+{
+  GstVideoDecoder *decoder = GST_VIDEO_DECODER (base);
+  GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
+  GstCapsFeatures *capsfeatures = NULL;
+  GstVideoInfo *info = &base->output_info;
+
+  if (base->output_state)
+    gst_video_codec_state_unref (base->output_state);
+
+  gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
+      &capsfeatures);
+  if (format == GST_VIDEO_FORMAT_UNKNOWN)
+    return FALSE;
+
+  base->output_state =
+      gst_video_decoder_set_interlaced_output_state (decoder, format,
+      GST_VIDEO_INFO_INTERLACE_MODE (info), GST_VIDEO_INFO_WIDTH (info),
+      GST_VIDEO_INFO_HEIGHT (info), base->input_state);
+
+  /* set caps feature */
+  base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
+  if (capsfeatures)
+    gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
+
+  GST_INFO_OBJECT (base, "Negotiated caps %" GST_PTR_FORMAT,
+      base->output_state->caps);
+
+  return TRUE;
+}