[Decoder/direct_video] Pipeline may allocate buffer for you already
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 7 Nov 2018 09:38:52 +0000 (18:38 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Wed, 14 Nov 2018 07:34:26 +0000 (16:34 +0900)
Do not die when the pipeline has already allocated the exact
size of buffer for you. Just reuse what's already there.

This fixes the 3rd subissue of #700.

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
gst/tensor_decoder/tensordec-directvideo.c
gst/tensor_decoder/tensordec.c

index 6072103..7f7588e 100644 (file)
@@ -108,12 +108,26 @@ dv_getOutputDim (GstTensorDec * self, const GstTensorConfig * config)
   return gst_caps_simplify (caps);
 }
 
+/** @brief get video output buffer size */
+static gsize
+_get_video_xraw_bufsize (tensor_dim dim)
+{
+  /* dim[0] is bpp and there is zeropadding only when dim[0]%4 > 0 */
+  return ((dim[0] * dim[1] - 1) / 4 + 1) * 4 * dim[2];
+}
+
 /** @brief tensordec-plugin's TensorDecDef callback */
 static gsize
 dv_getTransformSize (GstTensorDec * self, GstCaps * caps,
     gsize size, GstCaps * othercaps, GstPadDirection direction)
 {
-  return 0;                     /* I'll allocate. Do not allocate for me */
+  GstTensorConfig *config = &self->tensor_config;
+  uint32_t *dim = &(config->info.dimension[0]);
+
+  if (direction == GST_PAD_SINK)
+    return _get_video_xraw_bufsize (dim);
+  else
+    return 0; /** @todo NYI */
 }
 
 /** @brief tensordec-plugin's TensorDecDef callback */
@@ -125,26 +139,30 @@ dv_decode (GstTensorDec * self, const GstTensorMemory * input,
   GstMemory *out_mem;
   GstTensorConfig *config = &self->tensor_config;
   uint32_t *dim = &(config->info.dimension[0]);
+  size_t size = _get_video_xraw_bufsize (dim);
 
   g_assert (outbuf);
-  g_assert (gst_buffer_get_size (outbuf) == 0);
+  if (gst_buffer_get_size (outbuf) > 0 && gst_buffer_get_size (outbuf) != size) {
+    gst_buffer_set_size (outbuf, size);
+  }
   g_assert (config->info.type == _NNS_UINT8);
 
+  if (gst_buffer_get_size (outbuf) == size) {
+    /* Don't reallocate. Reuse what's already given */
+    out_mem = gst_buffer_get_all_memory (outbuf);
+  } else {
+    out_mem = gst_allocator_alloc (NULL, size, NULL);
+  }
+  g_assert (gst_memory_map (out_mem, &out_info, GST_MAP_WRITE));
+
   if (0 == ((dim[0] * dim[1]) % 4)) {
     /* No Padding Required */
-    out_mem = gst_allocator_alloc (NULL, input->size, NULL);
-    g_assert (gst_memory_map (out_mem, &out_info, GST_MAP_WRITE));
     memcpy (out_info.data, input->data, input->size);
   } else {
     /* Do Padding */
-    size_t size;
     int h;
     uint8_t *ptr, *inp;
 
-    size = ((dim[0] * dim[1] - 1) / 4 + 1) * 4 * dim[2];
-    out_mem = gst_allocator_alloc (NULL, size, NULL);
-    g_assert (gst_memory_map (out_mem, &out_info, GST_MAP_WRITE));
-
     ptr = (uint8_t *) out_info.data;
     inp = (uint8_t *) input->data;
     for (h = 0; h < dim[2]; h++) {
@@ -154,7 +172,9 @@ dv_decode (GstTensorDec * self, const GstTensorMemory * input,
     }
   }
   gst_memory_unmap (out_mem, &out_info);
-  gst_buffer_append_memory (outbuf, out_mem);
+
+  if (gst_buffer_get_size (outbuf) == 0)
+    gst_buffer_append_memory (outbuf, out_mem);
 
   /** @todo Caller of dv_decode in tensordec.c should call gst_memory_unmap to inbuf */
 
index 745b4ae..7b8fa5b 100644 (file)
@@ -587,6 +587,7 @@ gst_tensordec_class_init (GstTensorDecClass * klass)
 
   /** Refer: https://gstreamer.freedesktop.org/documentation/design/element-transform.html */
   trans_class->passthrough_on_same_caps = FALSE;
+  trans_class->transform_ip_on_passthrough = FALSE;
 
   /** Processing units */
   trans_class->transform = GST_DEBUG_FUNCPTR (gst_tensordec_transform);