# Pass crop downstream

**URL:** <https://forums.developer.nvidia.com/t/pass-crop-downstream/164179>\
**Category:** DeepStream SDK\
**Created:** [December 20, 2020, 1:50pm UTC](https://forums.developer.nvidia.com/t/pass-crop-downstream/164179 "2020-12-20T13:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nors](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nors](https://forums.developer.nvidia.com/u/nors)\
**Post date:** [December 20, 2020, 1:50pm UTC](https://forums.developer.nvidia.com/t/pass-crop-downstream/164179/1 "2020-12-20T13:50:16Z")

</div>

**• Hardware Platform (Jetson / GPU)**: GPU  
**• DeepStream Version** : 5.0  
**• TensorRT Version** 7.1.3.4  
**• NVIDIA GPU Driver Version (valid for GPU only)** 440.33.01  
**• Issue Type( questions, new requirements, bugs)** questions  
**• How to reproduce the issue ?**

I want to write a custom plugin. The size of the buffer will be changed in the plugin, so I’m not using transform\_ip but transform. I found that therefore dsexample is of limited use. I have problems generating valid output buffers.

```
static GstFlowReturn
gst_generateseq_transform (GstBaseTransform * btrans, GstBuffer * inbuf, GstBuffer * outbuf)
{
	GstGenerateSeq *generateseq = GST_GENERATESEQ (btrans);
	GstMapInfo in_map_info, out_map_info;
	GstFlowReturn flow_ret = GST_FLOW_ERROR;
	//gdouble scale_ratio = 1.0;
	//GenerateSeqOutput *output;

	NvBufSurface *surface = NULL;
	NvBufSurface *outsurface = NULL;
	NvDsBatchMeta *batch_meta = NULL;
	NvDsFrameMeta *frame_meta = NULL;
	NvDsMeta *meta = NULL;

	guint i = 0;
	int width, height, r;

	NvBufSurface *output_surface;
	NvBufSurfaceCreateParams params;
	int batch_size = 1;

	generateseq->frame_num++;

	g_print("mapping\n");

	memset (&in_map_info, 0, sizeof (in_map_info));
	if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
		g_print ("Error: Failed to map gst buffer\n");
		goto error;
	}

	nvds_set_input_system_timestamp (inbuf, GST_ELEMENT_NAME (generateseq));
	surface = (NvBufSurface *) in_map_info.data;

	std::cout << "num surfaces " << surface->batchSize << std::endl;

	params.gpuId = surface->gpuId;
	params.width = 100;
	params.height = 100;

	params.size = 0;
	params.colorFormat = NVBUF_COLOR_FORMAT_RGBA;
	params.layout = NVBUF_LAYOUT_PITCH;
	params.memType = NVBUF_MEM_CUDA_UNIFIED;

	r = NvBufSurfaceCreate(&output_surface, batch_size, &params);
	std::cout << r << std::endl;
	///* read some infos for debugging
	std::cout << "-- new surface " << std::endl;
	std::cout << "batch info " << std::endl;
	std::cout << "mem type " << output_surface->memType << std::endl;
	std::cout << "out size " << params.size << std::endl;
	std::cout << "out size " << output_surface->surfaceList[0].dataSize << std::endl;
	std::cout << "dataPtr " << output_surface->surfaceList[0].dataPtr << std::endl;
	std::cout << "mappedAddr " << output_surface->surfaceList[0].mappedAddr.addr[0] << std::endl;

	outbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_ZERO_PREFIXED,
			output_surface,
			sizeof(NvBufSurface), 0,
			sizeof(NvBufSurface), NULL, NULL);

	memset (&out_map_info, 0, sizeof (out_map_info));
	if (!gst_buffer_map (outbuf, &out_map_info, GST_MAP_READWRITE)) {
		g_print ("Error: Failed to out map gst buffer\n");
		goto error;
	}
	outsurface = (NvBufSurface *) out_map_info.data;
	NvBufSurfaceMemSet(outsurface, 0, 0, 0);

	g_print("surface batchsize %u\n", outsurface->batchSize);
	flow_ret = GST_FLOW_OK;

	error:

	nvds_set_output_system_timestamp (outbuf, GST_ELEMENT_NAME (generateseq));

	gst_buffer_unmap (inbuf, &in_map_info);
	gst_buffer_unmap (outbuf, &out_map_info);

	g_print("exit\n");

	return flow_ret;
}

```

When I map the created buffer with `gst_buffer_map`, I can read out the correct number of surfaces `NvBufSurfaceCreate`. I then return the buffer associated to this surface.

For testing, I duplicated the element `generateseq` in my pipline

`.... ! generateseq ! generateseq ! ...`

The second element outputs the following when receiving the buffer from the first element and then crashes.

```
num surfaces 2432761856
Cuda failure: status=101
-1
-- new surface 
batch info 
Caught SIGSEGV

```

I was expecting one surface. I assume, somehow the allocated buffers were freed? I also assume that I don’t have to sync the surface from GPU to CPU, since I’m not doing any CPU operations on it? What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Fiona.Chen](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/fiona.chen/32/15508_2.png) [@Fiona.Chen](https://forums.developer.nvidia.com/u/Fiona.Chen)\
**Post date:** [December 24, 2020, 12:17pm UTC](https://forums.developer.nvidia.com/t/pass-crop-downstream/164179/3 "2020-12-24T12:17:23Z")

</div>

This is a example of transform plugin [Qustion of memory leak gst-plugin based on dsexample - Intelligent Video Analytics / DeepStream SDK - NVIDIA Developer Forums](https://forums.developer.nvidia.com/t/qustion-of-memory-leak-gst-plugin-based-on-dsexample/145151)

---

<div class="post-metadata">

**Author:** ![TomNVIDIA](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/tomnvidia/32/14181_2.png) [@TomNVIDIA](https://forums.developer.nvidia.com/u/TomNVIDIA)\
**Post date:** [October 12, 2021, 4:15pm UTC](https://forums.developer.nvidia.com/t/pass-crop-downstream/164179/5 "2021-10-12T16:15:38Z")

</div>


