Support cut property for omxmjpegdec

Signed-off-by: Leo Lu <leo.lu@starfivetech.com>

diff -purN a/omx/gstomxmjpegdec.c b/omx/gstomxmjpegdec.c
--- a/omx/gstomxmjpegdec.c	2022-10-14 14:48:24.207804442 +0800
+++ b/omx/gstomxmjpegdec.c	2022-10-14 14:46:42.979857812 +0800
@@ -34,6 +34,8 @@ GST_DEBUG_CATEGORY_STATIC (gst_omx_mjpeg
 #define DEFAULT_ROTATION                 0
 #define DEFAULT_SCALEH                   0
 #define DEFAULT_SCALEV                   0
+#define DEFAULT_ROI                     ""
+
 #endif
 /* prototypes */
 
@@ -43,6 +45,8 @@ static gboolean
 gst_omx_jpegdec_set_rotation (GstOMXVideoDec * self);
 static gboolean
 gst_omx_jpegdec_set_mirror (GstOMXVideoDec * self);
+static gboolean
+gst_omx_jpegdec_set_roi (GstOMXVideoDec * self);
 static gboolean gst_omx_mjpeg_dec_is_format_change (GstOMXVideoDec * dec,
     GstOMXPort * port, GstVideoCodecState * state);
 static gboolean gst_omx_mjpeg_dec_set_format (GstOMXVideoDec * dec,
@@ -65,6 +69,7 @@ enum
   PROP_SCALEH,
   PROP_SCALEV,
   PROP_ROTATION,
+  PROP_ROI,
 };
 
 /* class initialization */
@@ -194,6 +199,17 @@ gst_omx_mjpeg_dec_class_init (GstOMXMJPE
         GST_OMX_MJPEG_DEC_SCALE_MODE, DEFAULT_SCALEV,
         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   
+  g_object_class_install_property (gobject_class, PROP_ROI,
+      g_param_spec_string ("roi", "ROI",
+        "Cutting image roi=<x>,<y>,<w>,<h> .\n                 \
+        roi coord and width/height(from left top).\n                 \
+        <x>   X Coordinate of the top left corner of the rectangle.\n                 \
+        <y>   Y Coordinate of the top left corner of the rectangle.\n                 \
+        <w>   Width of the rectangle.\n                 \
+        <h>   Height of the rectangle.\n                 \
+        EX:omxmjpegdec roi=0,0,800,480",
+        DEFAULT_ROI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gst_omx_set_default_role (&videodec_class->cdata, "video_decoder.mjpeg");
 }
 
@@ -204,6 +220,7 @@ gst_omx_mjpeg_dec_init (GstOMXMJPEGDec *
   self->rotation = DEFAULT_ROTATION;
   self->scaleH = DEFAULT_SCALEH;
   self->scaleV = DEFAULT_SCALEV;
+  memset (self->roi, 0, ROI_ARRAY_SIZE);
 }
 
 static gboolean
@@ -240,6 +257,9 @@ gst_omx_mjpeg_dec_set_format (GstOMXVide
 
   if (!gst_omx_jpegdec_set_scale (dec))
     return FALSE;
+  
+  if (!gst_omx_jpegdec_set_roi (dec))
+    return FALSE;
 
   gst_omx_port_get_port_definition (port, &port_def);
   port_def.format.video.eCompressionFormat = OMX_VIDEO_CodingMJPEG;
@@ -269,6 +289,9 @@ gst_omx_mjpeg_dec_set_property (GObject
     case PROP_SCALEV:
       mjpeg_dec->scaleV = g_value_get_enum (value);
       break;
+    case PROP_ROI:
+      strncpy (mjpeg_dec->roi, g_value_get_string (value), ROI_ARRAY_SIZE);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -296,6 +319,9 @@ gst_omx_mjpeg_dec_get_property (GObject
     case PROP_SCALEV:
       g_value_set_enum (value, mjpeg_dec->scaleV);
       break;
+    case PROP_ROI:
+      g_value_set_string (value, mjpeg_dec->roi);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -444,4 +470,81 @@ gst_omx_jpegdec_set_scale (GstOMXVideoDe
   
   GST_OBJECT_UNLOCK (self);
   return result;
-}
\ No newline at end of file
+}
+
+static gboolean
+gst_omx_jpegdec_set_roi (GstOMXVideoDec *self)
+{
+  OMX_ERRORTYPE err;
+  OMX_CONFIG_RECTTYPE RectConfig;
+  gboolean result = TRUE;
+  GstOMXMJPEGDec * mjpeg_dec = GST_OMX_MJPEG_DEC (self);
+  gchar* val;
+  gchar roi_tmp[ROI_ARRAY_SIZE];
+  strncpy (roi_tmp, mjpeg_dec->roi, ROI_ARRAY_SIZE);
+
+  GST_OBJECT_LOCK (self);
+  GST_OMX_INIT_STRUCT (&RectConfig);
+  RectConfig.nPortIndex = self->dec_out_port->index;
+
+  err = 
+      gst_omx_component_get_config (self->dec,
+      OMX_IndexConfigCommonScale, &RectConfig);
+  if (err == OMX_ErrorNone) {
+    if (strlen (mjpeg_dec->roi) > 0) {    
+      val = strtok (roi_tmp, ",");
+      if (val == NULL) {
+        goto ROI_PARAM_ERROR;
+      } else {
+        RectConfig.nLeft = atoi (val);
+      }
+      val = strtok (NULL, ",");
+      if (val == NULL) {
+        goto ROI_PARAM_ERROR;
+      } else {
+        RectConfig.nTop = atoi (val);
+      }
+      val = strtok (NULL, ",");
+      if (val == NULL) {
+        goto ROI_PARAM_ERROR;
+      } else {
+        RectConfig.nWidth = atoi (val);
+      }
+      val = strtok (NULL, ",");
+      if (val == NULL) {
+        goto ROI_PARAM_ERROR;
+      } else {
+        RectConfig.nHeight = atoi (val);
+      }
+    } else {
+      GST_OBJECT_UNLOCK (self);
+      return TRUE;
+    }
+
+    err = 
+        gst_omx_component_set_config (self->dec,
+        OMX_IndexConfigCommonOutputCrop, &RectConfig);
+    if (err == OMX_ErrorUnsupportedIndex) {
+      GST_WARNING_OBJECT (self,
+          "Setting a ROI not supported by the component");
+    } else if (err == OMX_ErrorUnsupportedSetting) {
+      GST_WARNING_OBJECT (self,
+          "Setting ROI settings %s  not supported by the component", mjpeg_dec->roi);
+    } else if (err != OMX_ErrorNone) {
+      GST_ERROR_OBJECT (self,
+          "Failed to set ROI parameters: %s (0x%08x)", gst_omx_error_to_string (err), err);
+      result = FALSE;
+    }
+  } else {
+    GST_ERROR_OBJECT (self, "Failed to get ROI parameters: %s (0x%08x)",
+        gst_omx_error_to_string (err), err);
+  }
+
+  GST_OBJECT_UNLOCK (self);
+  return result;
+
+ROI_PARAM_ERROR:
+  GST_ERROR_OBJECT (self, "Invalid ROI parameter: %s", mjpeg_dec->roi);
+  GST_OBJECT_UNLOCK (self);
+  return FALSE;  
+}
diff -purN a/omx/gstomxmjpegdec.h b/omx/gstomxmjpegdec.h
--- a/omx/gstomxmjpegdec.h	2022-10-14 14:48:24.207804442 +0800
+++ b/omx/gstomxmjpegdec.h	2022-10-14 14:42:09.876591323 +0800
@@ -42,6 +42,8 @@ G_BEGIN_DECLS
 typedef struct _GstOMXMJPEGDec GstOMXMJPEGDec;
 typedef struct _GstOMXMJPEGDecClass GstOMXMJPEGDecClass;
 
+#define ROI_ARRAY_SIZE 128
+
 struct _GstOMXMJPEGDec
 {
   GstOMXVideoDec parent;
@@ -59,6 +61,8 @@ struct _GstOMXMJPEGDec
   guint32 scaleH;
   guint32 scaleV;
   guint32 rotation;
+  gchar roi[ROI_ARRAY_SIZE];
+
 };
 
 struct _GstOMXMJPEGDecClass
