In machine learning applications, it is common to extract specific objects or regions from images or video frames for processing, and the number of objects to be cropped in each frame can vary. Pre-processing might also be required for the cropped object to be suitable for machine learning algorithms. The vvas_xmulticrop plug-in, based on the VVAS image processing library, is capable of resizing, color space conversion, preprocessing, and cropping one or multiple objects from a single input frame.
The vvas_xmulticrop plug-in supports two types of cropping: static cropping and dynamic cropping. In static cropping, you specify a fixed region in the frame to be cropped via the GStreamer properties, and the cropped buffer is pushed as an output buffer (main buffer) onto the source pad of the plug-in. In dynamic cropping, multiple objects or regions are cropped from a frame, and the cropped objects or buffers are attached as metadata to the output buffer pushed onto the source pad. The regions to be dynamically cropped are specified as part of the metadata attached to the input buffer. Both static and dynamic cropping can be used together.
In summary, video frames often need to be cropped in machine learning applications to process objects, and the number of objects to be cropped can vary. Pre-processing might also be required. The vvas_xmulticrop plug-in is capable of performing resizing, color space conversion, preprocessing, and both static and dynamic cropping to streamline the workflow for machine learning developers.
For insight into the implementation details, refer to the vvas_xmulticrop source code located at <vvas>/vvas-gst-plugins/sys/multicrop/.
This plug-in supports:
Single sink pad, single source pad
Color space conversion
Resize
Pre-processing
Static and dynamic cropping
The vvas_xmulticrop plug-in is similar to vvas_xabrscaler except the following features:
It has only one source pad
It supports dynamic cropping.
Static Cropping
Static cropping can be used for cropping only one object. The cropped object/buffer is sent onto the source pad. s-crop-x, s-crop-y, s-crop-width, s-crop-height are the plug-in properties to set the static crop parameters.
Dynamic Cropping
Dynamic cropping is for cropping multiple objects from the input frame. Dynamic crop coordinates must be attached to the input frame as GstVideoRegionOfInterestMeta metadata, and GstVideoRegionOfInterestMeta->roi_type must be set to “roi-crop-meta”. Each instance of GstVideoRegionOfInterestMeta in the buffer represents a single object for dynamic crop.
The vvas_xmulticrop plug-in dynamically crops the object and attaches the cropped objects/buffers to the output buffer in GstVideoRegionOfInterestMeta->params. GstVideoRegionOfInterestMeta->params is a GList* of GstStructure. The dynamically cropped buffer is attached to this field, with the name of the GstStructure as “roi-buffer”. This GstStructure has only one field “sub-buffer”, which is of a type GST_TYPE_BUFFER. You must extract this cropped buffer, use it, and then unref it.
You choose to resize these cropped buffers to some width and height by setting the d-height and d-width properties. If these properties are not set, then cropped buffers will not be resized.
You can also decide the format of dynamically cropped buffers by setting the d-format GStreamer property. If this property is not set, then all cropped buffers will have the same format as of the input buffer format.
The user-specified pre-processing is applied only to dynamically cropped buffers. If you want it to be applied to the output buffer/statically cropped buffer as well, set the ppe-on-main buffer GStreamer property.
Dynamic object is not cropped when crop coordinates or crop resolution is not proper in the attached GstVideoRegionOfInterestMeta metadata and that metadata is skipped in dynamic cropping. The vvas_xmulticrop plug-in might align the crop parameters as per the IP requirement, aligned values are updated in GstVideoRegionOfInterestMeta. GstVideoRegionOfInterestMeta, which are skipped, will have GstVideoRegionOfInterestMeta->params as NULL.
It is possible to perform both static and dynamic cropping simultaneously. As previously explained, the statically cropped buffer is sent on the source pad, while the dynamically cropped buffers are attached as metadata to the output buffer.
vvas_xmulticrop supports at max 39 dynamic crops. The memory for dynamically cropped buffers is allocated from a GStreamer buffer pool, and there is no upper limit on this buffer pool. However, if the buffers are not freed, new buffers will be allocated, potentially leading to increased memory consumption.
Prerequisite
To use this plug-in, VvasImageProcess must be available.
To use the libvvas_image_process_hw_lib.so image process library, the hardware design must have the image_processing kernel available, and it must be configured to support the required color formats and maximum resolution for your application. You can find the configuration details for the image_processing kernel, including the supported maximum resolution and color formats, in /run/media/mmcblk0p1/image_processing.cfg on the board.
Input and Output
All the formats and resolution supported by VvasImageProcess are also supported by vvas_xmulticrop. vvas_xmulticrop queries VvasImageProcess for the supported format and resolution of all the available image process library implementation, and it shows the superset caps of all the library capabilities during gst-inspect-1.0. However, caps negotiation happens only with the caps of image process library are selected through library-name, not with the superset caps. Say for an example library-name = libvvas_image_process_hw_lib.so then vvas_xmulticrop will do the caps negotiation only with the caps of the libvvas_image_process_hw_lib.so image process library.
Hardware image process library (libvvas_image_process_hw_lib.so) supported formats and resolution:
NV12
RGB
BGR
RGBx
BGRx
I420
Resolution: [16 x 16] - [3840 x 2160]
Note
Pad template caps shown in gst-inspect-1.0 are superset caps of all the available image process libraries. If any new library is added or removed, then remove GStreamer’s plug-in registry cache file and reinspect to create it again.
rm -rf ~/.cache/gstreamer-1.0
If the plug-in registry cache is not removed, caps shown in gst-inspect-1.0 might not reflect exact caps of the available image process libraries.
Important
Make sure that the color formats and resolution needed for your application are supported by VvasImageProcess.
Control Parameters and Plug-in Properties
The following table lists the GStreamer plug-in properties supported by the vvas_xmulticrop plug-in.
The following default value of interpolation-mode is for the default library-name, that is, libvvas_image_process_hw_lib.so. vvas_xmulticrop queries these values from VvasImageProcess.
Property Name |
Type |
Range |
Default |
Description |
|---|---|---|---|---|
avoid-output-copy |
Boolean |
true / false |
false |
Avoid output frames copy even when the downstream does not support the GstVideoMeta metadata. |
enable-pipeline |
Boolean |
true / false |
false |
Enable buffer pipelining to improve performance in non–zero-copy use cases. |
in-mem-bank |
Unsigned int |
0 – 65535 |
-1 |
VVAS input memory bank to allocate memory. If not set, it will be queried from the VVAS core module. |
out-mem-bank |
Unsigned int |
0 – 65535 |
-1 |
VVAS output memory bank to allocate memory. If not set, it will be queried from the VVAS core module. |
xclbin-location |
String |
N/A |
NULL |
The location of the xclbin. |
library-name |
String |
N/A |
libvvas_image
_process_hw_lib.so
|
Image process library name. |
library-config |
String |
N/A |
{“kernel-name”:
“image_processing:
{image_processing_1}
“}
|
Image process library configuration string. The default value is only valid for the libvvas_image_process_hw_lib.so library. |
interpolation-mode |
Integer |
0, 1, 2 |
0 |
Interpolation Mode to use:
0 – BILINEAR
1 – BICUBIC
2 – POLYPHASE
Default is 0 (BILINEAR) for the hardware image-process library ( |
alpha-b |
Float |
0 to 128 |
0 |
Mean subtraction for the Blue channel, needed only if PPE is required. |
alpha-g |
Float |
0 to 128 |
0 |
Mean subtraction for the Green channel, needed only if PPE is required. |
alpha-r |
Float |
0 to 128 |
0 |
Mean subtraction for the Red channel, needed only if PPE is required. |
beta-b |
Float |
0 to 1 |
1 |
Scaling for the Blue channel, needed only if PPE is required. |
beta-g |
Float |
0 to 1 |
1 |
Scaling for the Green channel, needed only if PPE is required. |
beta-r |
Float |
0 to 1 |
1 |
Scaling for the Red channel, needed only if PPE is required. |
s-crop-x |
Unsigned int |
0 to 3840 |
0 |
Crop X coordinate for static cropping. |
s-crop-y |
Unsigned int |
0 to 2160 |
0 |
Crop Y coordinate for static cropping. |
s-crop-width |
Unsigned int |
0 to 3840 |
0 |
Crop width for static cropping (minimum: 16).
When this is 0 or not set, it will be calculated as
input width –
s-crop-x. |
s-crop-height |
Unsigned int |
0 to 2160 |
0 |
Crop height for static cropping (minimum: 16).
When this is 0 or not set, it will be calculated as
input height –
s-crop-y. |
d-width |
Unsigned int |
0 to 3840 |
0 |
Width of dynamically cropped buffers. |
d-height |
Unsigned int |
0 to 2160 |
0 |
Height of dynamically cropped buffers. |
d-format |
Unsigned int |
0, 2, 4, 5, 7, 8, 11, 12, 15, 16, 23, 25, 41, 45, 48, 51, 78, 79, 83 |
0 |
Format of dynamically cropped buffers. |
ppe-on-main-buffer |
Boolean |
true / false |
false |
Apply pre-processing on the main buffer also. |
software-scaling |
Boolean |
true / false |
false |
Retained for GObject compatibility. The software image-process library is not supported in this release; keep |
Note
The image-processing IP has some alignment requirements, hence the user given parameters for crop are aligned as per the IP requirement; alignment ensures that it covers the region of crop specified, hence final cropped image might have extra pixels cropped. The crop width and height must be at least 16.
The maximum value of s-crop-x, s-crop-y, s-crop-width, s-crop-height, d-width, and d-height are as per the maximum resolution supported by VvasImageProcess. vvas_xmulticrop will query these values from VvasImageProcess and update them accordingly.
Example Pipelines
The following pipeline converts NV12 to RGB and performs scaling from 1920x1080 to 640x480.
gst-launch-1.0 -v \
videotestsrc num-buffers=10 ! video/x-raw,format=NV12,width=1920,height=1080 \
! vvas_xmulticrop xclbin-location=/run/media/mmcblk0p1/x_plus_ml.xclbin \
! video/x-raw,format=RGB,width=640,height=480 ! filesink location=out_480p.rgb
The following pipeline performs pre-processing along with color space conversion and scaling on output buffers.
gst-launch-1.0 -v \
videotestsrc num-buffers=10 ! video/x-raw,format=NV12,width=1920,height=1080 \
! vvas_xmulticrop ppe-on-main-buffer=true alpha-r=124 alpha-g=116 alpha-b=104 beta-r=0.547 beta-g=0.56 beta-b=0.557 xclbin-location=/run/media/mmcblk0p1/x_plus_ml.xclbin \
! video/x-raw,format=RGB,width=640,height=480 ! filesink location=out_480p.rgb
The pipeline performs static cropping at (x,y) = (100,80) and (width,height)= (1280,720); this cropped buffers gets scaled to 640x480 and converted to RGB.
gst-launch-1.0 -v \
videotestsrc num-buffers=10 ! video/x-raw,format=NV12,width=1920,height=1080 \
! vvas_xmulticrop s-crop-x=100 s-crop-y=80 s-crop-width=1280 s-crop-height=720 xclbin-location=/run/media/mmcblk0p1/x_plus_ml.xclbin \
! video/x-raw,format=RGB,width=640,height=480 ! filesink location=out_480p.rgb
The following code shows how to add GstVideoRegionOfInterestMeta for dynamic cropping.
GstVideoRegionOfInterestMeta *meta;
meta = gst_buffer_add_video_region_of_interest_meta (buffer, "roi-crop-meta", 0, 0, 0, 0);
if (meta) {
meta->id = id;
meta->parent_id = p_id;
meta->x = x;
meta->y = y;
meta->w = w;
meta->h = h;
printf("meta: x:%u y:%u, w:%u h:%u", meta->x, meta->y, meta->w, meta->h);
}
The following code shows how to read GstVideoRegionOfInterestMeta and how to extract a dynamically cropped buffer/object.
read_crop_meta (GstBuffer *buf) {
gpointer state = NULL;
GstMeta *_meta;
while ((_meta = gst_buffer_iterate_meta_filtered (buf, &state,
GST_VIDEO_REGION_OF_INTEREST_META_API_TYPE))) {
GstStructure *s = NULL;
GstVideoRegionOfInterestMeta *roi_meta =
(GstVideoRegionOfInterestMeta *) _meta;
if (g_strcmp0("roi-crop-meta", g_quark_to_string (roi_meta->roi_type))) {
//This is not the metadata we are looking for
continue;
}
//Got the ROI crop metadata, prepare output buffer
//Extract dynamically cropped buffer from this meta
s = gst_video_region_of_interest_meta_get_param (roi_meta, "roi-buffer");
if (s) {
GstBuffer *sub_buffer = NULL;
gst_structure_get (s, "sub-buffer", GST_TYPE_BUFFER, &sub_buffer, NULL);
if (sub_buffer) {
//use sub_buffer and unref it
dump_dynamically_cropped_buffer (sub_buffer);
} else {
printf("couldn't get sub buffer");
}
} else {
printf("couldn't get expected struct");
}
}
return TRUE;
}
The following code shows how to read/dump dynamically cropped buffers.
static gboolean
dump_dynamically_cropped_buffer (GstBuffer *sub_buffer)
{
//Read GstVideoMeta from the buffer, dump the buffer to file
GstVideoMeta *vmeta = NULL;
FILE *fp;
gchar name[256] = { 0 };
guint i, j, n_planes;
GstVideoFrame out_frame = { 0 };
GstVideoFrame new_frame = { 0 };
GstVideoInfo *vinfo;
GstBuffer *new_outbuf;
guint8 *data_ptr, *out_ptr;
guint w, h;
gint src_stride;
const GstVideoFormatInfo *finfo;
gint comp[GST_VIDEO_MAX_COMPONENTS];
const GstVideoInfo *sinfo;
if (!sub_buffer) {
return FALSE;
}
vmeta = gst_buffer_get_video_meta (sub_buffer);
if (!vmeta) {
printf ("couldn't get video meta\n");
return FALSE;
}
sprintf (name, "dynbuf_%ux%u_%s.yuv", vmeta->width, vmeta->height,
gst_video_format_to_string (vmeta->format));
fp = fopen (name, "wb");
if (!fp) {
return FALSE;
}
vinfo = gst_video_info_new ();
gst_video_info_set_format (vinfo, vmeta->format, vmeta->width, vmeta->height);
new_outbuf = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (vinfo));
if (!new_outbuf) {
printf ("couldn't allocate buffer\n");
gst_video_info_free (vinfo);
fclose (fp);
return FALSE;
}
gst_video_frame_map (&out_frame, vinfo, sub_buffer, GST_MAP_READ);
gst_video_frame_map (&new_frame, vinfo, new_outbuf,
GST_MAP_READ | GST_MAP_WRITE);
sinfo = &out_frame.info;
finfo = sinfo->finfo;
n_planes = finfo->n_planes;
for (i = 0; i < n_planes; i++) {
gst_video_format_info_component (finfo, i, comp);
data_ptr = out_frame.data[i];
out_ptr = new_frame.data[i];
w = GST_VIDEO_FRAME_COMP_WIDTH (&out_frame, comp[0])
* GST_VIDEO_FRAME_COMP_PSTRIDE (&out_frame, comp[0]);
h = GST_VIDEO_FRAME_COMP_HEIGHT (&out_frame, comp[0]);
src_stride = GST_VIDEO_INFO_PLANE_STRIDE (sinfo, i);
for (j = 0; j < h; j++) {
memcpy (out_ptr, data_ptr, w);
data_ptr += src_stride;
out_ptr += w;
}
fwrite (new_frame.data[i], 1, w * h, fp);
}
gst_video_frame_unmap (&out_frame);
gst_video_frame_unmap (&new_frame);
gst_video_info_free (vinfo);
gst_buffer_unref (new_outbuf);
fclose (fp);
gst_buffer_unref (sub_buffer);
return TRUE;
}