va: Add and use common decode negotiate vmethod.
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvah265dec.c
index 45b6c78..53be567 100644 (file)
@@ -88,10 +88,6 @@ struct _GstVaH265Dec
 {
   GstVaBaseDec parent;
 
-  GstFlowReturn last_ret;
-
-  gint coded_width;
-  gint coded_height;
   gint dpb_size;
 
   VAPictureParameterBufferHEVCExtension pic_param;
@@ -232,7 +228,9 @@ gst_va_h265_dec_output_picture (GstH265Decoder * decoder,
 {
   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
   GstVaH265Dec *self = GST_VA_H265_DEC (decoder);
+  GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
   GstVaDecodePicture *va_pic;
+  gboolean ret;
 
   va_pic = gst_h265_picture_get_user_data (picture);
   g_assert (va_pic->gstbuffer);
@@ -240,21 +238,14 @@ gst_va_h265_dec_output_picture (GstH265Decoder * decoder,
   GST_LOG_OBJECT (self,
       "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
 
-  if (self->last_ret != GST_FLOW_OK) {
-    gst_h265_picture_unref (picture);
-    _replace_previous_slice (self, NULL, 0);
-    gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self), frame);
-    return self->last_ret;
-  }
-
   gst_buffer_replace (&frame->output_buffer, va_pic->gstbuffer);
 
-  if (base->copy_frames)
-    gst_va_base_dec_copy_output_buffer (base, frame);
-
+  ret = gst_va_base_dec_process_output (base, frame, picture->buffer_flags);
   gst_h265_picture_unref (picture);
 
-  return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
+  if (ret)
+    return gst_video_decoder_finish_frame (vdec, frame);
+  return GST_FLOW_ERROR;
 }
 
 static void
@@ -860,6 +851,7 @@ gst_va_h265_dec_new_picture (GstH265Decoder * decoder,
   GstVaDecodePicture *pic;
   GstBuffer *output_buffer;
   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
+  GstFlowReturn ret = GST_FLOW_ERROR;
 
   if (base->need_negotiation) {
     if (!gst_video_decoder_negotiate (vdec)) {
@@ -869,11 +861,8 @@ gst_va_h265_dec_new_picture (GstH265Decoder * decoder,
   }
 
   output_buffer = gst_video_decoder_allocate_output_buffer (vdec);
-  if (!output_buffer) {
-    self->last_ret = GST_FLOW_ERROR;
+  if (!output_buffer)
     goto error;
-  }
-  self->last_ret = GST_FLOW_OK;
 
   pic = gst_va_decode_picture_new (base->decoder, output_buffer);
   gst_buffer_unref (output_buffer);
@@ -890,8 +879,8 @@ error:
   {
     GST_WARNING_OBJECT (self,
         "Failed to allocated output buffer, return %s",
-        gst_flow_get_name (self->last_ret));
-    return self->last_ret;
+        gst_flow_get_name (ret));
+    return ret;
   }
 }
 
@@ -1056,6 +1045,7 @@ gst_va_h265_dec_new_sequence (GstH265Decoder * decoder, const GstH265SPS * sps,
 {
   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
   GstVaH265Dec *self = GST_VA_H265_DEC (decoder);
+  GstVideoInfo *info = &base->output_info;
   VAProfile profile;
   gint display_width;
   gint display_height;
@@ -1092,26 +1082,26 @@ gst_va_h265_dec_new_sequence (GstH265Decoder * decoder, const GstH265SPS * sps,
           rt_format, sps->width, sps->height)) {
     base->profile = profile;
     base->rt_format = rt_format;
-    self->coded_width = sps->width;
-    self->coded_height = sps->height;
+    base->width = sps->width;
+    base->height = sps->height;
 
     negotiation_needed = TRUE;
     GST_INFO_OBJECT (self, "Format changed to %s [%x] (%dx%d)",
-        gst_va_profile_name (profile), rt_format, self->coded_width,
-        self->coded_height);
+        gst_va_profile_name (profile), rt_format, base->width, base->height);
   }
 
-  if (base->width != display_width || base->height != display_height) {
-    base->width = display_width;
-    base->height = display_height;
+  if (GST_VIDEO_INFO_WIDTH (info) != display_width ||
+      GST_VIDEO_INFO_HEIGHT (info) != display_height) {
+    GST_VIDEO_INFO_WIDTH (info) = display_width;
+    GST_VIDEO_INFO_HEIGHT (info) = display_height;
 
     negotiation_needed = TRUE;
-    GST_INFO_OBJECT (self, "Resolution changed to %dx%d", base->width,
-        base->height);
+    GST_INFO_OBJECT (self, "Resolution changed to %dx%d",
+        GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
   }
 
-  base->need_valign = base->width < self->coded_width
-      || base->height < self->coded_height;
+  base->need_valign = GST_VIDEO_INFO_WIDTH (info) < base->width ||
+      GST_VIDEO_INFO_HEIGHT (info) < base->height;
   if (base->need_valign) {
     /* *INDENT-OFF* */
     if (base->valign.padding_left != padding_left ||
@@ -1132,8 +1122,9 @@ gst_va_h265_dec_new_sequence (GstH265Decoder * decoder, const GstH265SPS * sps,
   }
 
   base->min_buffers = self->dpb_size + 4;       /* dpb size + scratch surfaces */
-
   base->need_negotiation = negotiation_needed;
+  g_clear_pointer (&base->input_state, gst_video_codec_state_unref);
+  base->input_state = gst_video_codec_state_ref (decoder->input_state);
 
   {
     /* FIXME: We don't have parser API for sps_range_extension, so
@@ -1205,54 +1196,6 @@ gst_va_h265_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
   return caps;
 }
 
-static gboolean
-gst_va_h265_dec_negotiate (GstVideoDecoder * decoder)
-{
-  GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
-  GstVaH265Dec *self = GST_VA_H265_DEC (decoder);
-  GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
-  GstCapsFeatures *capsfeatures = NULL;
-  GstH265Decoder *h265dec = GST_H265_DECODER (decoder);
-
-  /* Ignore downstream renegotiation request. */
-  if (!base->need_negotiation)
-    return TRUE;
-
-  base->need_negotiation = FALSE;
-
-  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, self->coded_width,
-          self->coded_height))
-    return FALSE;
-
-  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_output_state (decoder, format,
-      base->width, base->height, h265dec->input_state);
-
-  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 (self, "Negotiated caps %" GST_PTR_FORMAT,
-      base->output_state->caps);
-
-  return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
-}
-
 static void
 gst_va_h265_dec_dispose (GObject * object)
 {
@@ -1305,7 +1248,6 @@ gst_va_h265_dec_class_init (gpointer g_class, gpointer class_data)
   gobject_class->dispose = gst_va_h265_dec_dispose;
 
   decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_h265_dec_getcaps);
-  decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_h265_dec_negotiate);
 
   h265decoder_class->new_sequence =
       GST_DEBUG_FUNCPTR (gst_va_h265_dec_new_sequence);