commit 21375198b5f1ea284ae7c43869aa7a2aa61b25b7
Author: Nikhil Dasan <n-dasan@ti.com>
Date:   Tue Nov 28 14:40:53 2023 +0530

    Add Switch Channel

diff --git a/kernels/img_proc/c66/vx_draw_box_detections_target.c b/kernels/img_proc/c66/vx_draw_box_detections_target.c
index f0a77a4..831a7b5 100644
--- a/kernels/img_proc/c66/vx_draw_box_detections_target.c
+++ b/kernels/img_proc/c66/vx_draw_box_detections_target.c
@@ -61,7 +61,7 @@
 */
 
 
-
+#include <stdio.h>
 #include <TI/tivx.h>
 #include <TI/tivx_img_proc.h>
 #include <TI/tivx_target_kernel.h>
@@ -75,6 +75,17 @@
 
 static tivx_target_kernel vx_DrawBoxDetections_kernel = NULL;
 
+typedef struct {
+    uint32_t active_channel;
+} tivxDrawBoxDetectionsKernelParams;
+
+static tivxDrawBoxDetectionsKernelParams kernelprms;
+
+static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsControl(
+       tivx_target_kernel_instance kernel,
+       uint32_t node_cmd_id, tivx_obj_desc_t *obj_desc[],
+       uint16_t num_params, void *priv_arg);
+
 static vx_int32 clip_offset(vx_int32 input, vx_int32 max);
 static void drawBox(vx_uint8 *y_data, vx_uint8 *cbcr_data, vx_int32 width, vx_int32 height, vx_int32 xmin, vx_int32 ymin, vx_int32 xmax, vx_int32 ymax, vx_int32 label, vx_uint8 color_map[][3], vx_int32 max_classes);
 
@@ -134,6 +145,8 @@ static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsCreate
         }
     }
 
+    kernelprms.active_channel = 0;
+
     return(status);
 }
 
@@ -156,6 +169,71 @@ static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsDelete(
     return (status);
 }
 
+static vx_status tivxSwitchChannel(const tivx_obj_desc_user_data_object_t *usr_data_obj)
+{
+    vx_status                            status = (vx_status)VX_SUCCESS;
+    tivx_select_channel_t                *ch_prms = NULL;
+    void                                 *target_ptr;
+
+    if (NULL != usr_data_obj)
+    {
+        target_ptr = tivxMemShared2TargetPtr(&usr_data_obj->mem_ptr);
+
+        tivxCheckStatus(&status, tivxMemBufferMap(target_ptr, usr_data_obj->mem_size,
+            (vx_enum)VX_MEMORY_TYPE_HOST, (vx_enum)VX_READ_ONLY));
+
+        if (sizeof(tivx_select_channel_t) ==
+                usr_data_obj->mem_size)
+        {
+            ch_prms = (tivx_select_channel_t *)target_ptr;
+            kernelprms.active_channel = ch_prms->active_channel;
+        }
+        else
+        {
+            VX_PRINT(VX_ZONE_ERROR, "Invalid Size \n");
+            status = (vx_status)VX_ERROR_INVALID_PARAMETERS;
+        }
+
+        tivxCheckStatus(&status, tivxMemBufferUnmap(target_ptr, usr_data_obj->mem_size,
+            (vx_enum)VX_MEMORY_TYPE_HOST, (vx_enum)VX_READ_ONLY));
+    }
+    else
+    {
+        VX_PRINT(VX_ZONE_ERROR, "User Data Object is NULL \n");
+        status = (vx_status)VX_ERROR_INVALID_PARAMETERS;
+    }
+
+    return (status);
+}
+
+static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsControl(
+       tivx_target_kernel_instance kernel,
+       uint32_t node_cmd_id, tivx_obj_desc_t *obj_desc[],
+       uint16_t num_params, void *priv_arg)
+{
+    vx_status status = (vx_status)VX_SUCCESS;
+
+    if ((vx_status)VX_SUCCESS == status)
+    {
+        switch (node_cmd_id)
+        {
+            case TIVX_SWITCH_POST_CHANNEL:
+            {
+                status = tivxSwitchChannel((tivx_obj_desc_user_data_object_t *)obj_desc[0U]);
+                printf("Reached Control function of DL post Proc\n");
+                break;
+            }
+            default:
+            {
+                VX_PRINT(VX_ZONE_ERROR, "Invalid Command Id\n");
+                status = (vx_status)VX_FAILURE;
+                break;
+            }
+        }
+    }
+    return status;
+}
+
 static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsProcess
 (
     tivx_target_kernel_instance kernel,
@@ -204,7 +282,22 @@ static vx_status VX_CALLBACK tivxKernelDrawBoxDetectionsProcess
         input_tensor_target_ptr = tivxMemShared2TargetPtr(&input_tensor_desc->mem_ptr);
         tivxMemBufferMap(input_tensor_target_ptr, input_tensor_desc->mem_size, VX_MEMORY_TYPE_HOST,VX_READ_ONLY);
 
-        input_image_desc = (tivx_obj_desc_image_t *)obj_desc[TIVX_KERNEL_DRAW_BOX_DETECTIONS_INPUT_IMAGE_IDX];
+        if((vx_status)VX_SUCCESS == status)
+        {
+            uint32_t active_channel;
+
+            active_channel = kernelprms.active_channel;
+            // printf("active_channel post = %d\n", prms->active_channel);
+
+            input_image_desc = (tivx_obj_desc_image_t *)
+                tivxGetObjDescElement(obj_desc[TIVX_KERNEL_DRAW_BOX_DETECTIONS_INPUT_IMAGE_IDX],
+                                      active_channel);
+
+            if (input_image_desc == NULL)
+            {
+                status = (vx_status)VX_FAILURE;
+            }
+        }
         input_image_target_ptr[0] = tivxMemShared2TargetPtr(&input_image_desc->mem_ptr[0]);
         tivxMemBufferMap(input_image_target_ptr[0], input_image_desc->mem_size[0], VX_MEMORY_TYPE_HOST,VX_READ_ONLY);
         input_image_target_ptr[1] = NULL;
@@ -332,7 +425,7 @@ void tivxAddTargetKernelDrawBoxDetections()
                                         tivxKernelDrawBoxDetectionsProcess,
                                         tivxKernelDrawBoxDetectionsCreate,
                                         tivxKernelDrawBoxDetectionsDelete,
-                                        NULL,
+                                        tivxKernelDrawBoxDetectionsControl,
                                         NULL
                                      );
     }
diff --git a/kernels/img_proc/c66/vx_image_preprocessing_target.c b/kernels/img_proc/c66/vx_image_preprocessing_target.c
index 56d27ca..81d289b 100644
--- a/kernels/img_proc/c66/vx_image_preprocessing_target.c
+++ b/kernels/img_proc/c66/vx_image_preprocessing_target.c
@@ -61,7 +61,7 @@
 */
 
 
-
+#include <stdio.h>
 #include <TI/tivx.h>
 #include <TI/tivx_img_proc.h>
 #include <TI/tivx_target_kernel.h>
@@ -86,6 +86,11 @@
 
 static tivx_target_kernel vx_img_preproc_target_kernel = NULL;
 
+static vx_status VX_CALLBACK tivxKernelImgPreProcControl(
+       tivx_target_kernel_instance kernel,
+       uint32_t node_cmd_id, tivx_obj_desc_t *obj_desc[],
+       uint16_t num_params, void *priv_arg);
+
 typedef enum {
     DATA_COPY_CPU = 0,
     DATA_COPY_DMA = 1,
@@ -111,6 +116,8 @@ typedef struct {
 typedef struct {
     tivxImgPreProcParams nodeParams;
 
+    uint32_t active_channel;
+
     DMAObj dmaObjRGB;
     DMAObj dmaObjY;
     DMAObj dmaObjC;
@@ -192,6 +199,44 @@ static vx_status img_proc_execute_16bit_RgbiToRgbp
     void *out_tensor_target_ptr
 );
 
+static vx_status tivxSwitchChannel(tivxImgPreProcKernelParams *prms,
+    const tivx_obj_desc_user_data_object_t *usr_data_obj)
+{
+    vx_status                            status = (vx_status)VX_SUCCESS;
+    tivx_select_channel_t                *ch_prms = NULL;
+    void                                 *target_ptr;
+
+    if (NULL != usr_data_obj)
+    {
+        target_ptr = tivxMemShared2TargetPtr(&usr_data_obj->mem_ptr);
+
+        tivxCheckStatus(&status, tivxMemBufferMap(target_ptr, usr_data_obj->mem_size,
+            (vx_enum)VX_MEMORY_TYPE_HOST, (vx_enum)VX_READ_ONLY));
+
+        if (sizeof(tivx_select_channel_t) ==
+                usr_data_obj->mem_size)
+        {
+            ch_prms = (tivx_select_channel_t *)target_ptr;
+            prms->active_channel = ch_prms->active_channel;
+        }
+        else
+        {
+            VX_PRINT(VX_ZONE_ERROR, "Invalid Size \n");
+            status = (vx_status)VX_ERROR_INVALID_PARAMETERS;
+        }
+
+        tivxCheckStatus(&status, tivxMemBufferUnmap(target_ptr, usr_data_obj->mem_size,
+            (vx_enum)VX_MEMORY_TYPE_HOST, (vx_enum)VX_READ_ONLY));
+    }
+    else
+    {
+        VX_PRINT(VX_ZONE_ERROR, "User Data Object is NULL \n");
+        status = (vx_status)VX_ERROR_INVALID_PARAMETERS;
+    }
+
+    return (status);
+}
+
 static vx_status dma_create(DMAObj *dmaObj, vx_size transfer_type, vx_uint32 dma_ch)
 {
     vx_status status = VX_SUCCESS;
@@ -371,6 +416,45 @@ static vx_status dma_delete(DMAObj *dmaObj)
     return status;
 }
 
+static vx_status VX_CALLBACK tivxKernelImgPreProcControl(
+       tivx_target_kernel_instance kernel,
+       uint32_t node_cmd_id, tivx_obj_desc_t *obj_desc[],
+       uint16_t num_params, void *priv_arg)
+{
+    vx_status status = (vx_status)VX_SUCCESS;
+
+    uint32_t size;
+    tivxImgPreProcKernelParams *prms = NULL;
+
+    status = tivxGetTargetKernelInstanceContext(kernel,
+            (void **)&prms, &size);
+    if ((VX_SUCCESS != status) || (NULL == prms) ||
+        (sizeof(tivxImgPreProcKernelParams) != size))
+    {
+        status = VX_FAILURE;
+    }
+
+    if ((vx_status)VX_SUCCESS == status)
+    {
+        switch (node_cmd_id)
+        {
+            case TIVX_SWITCH_CHANNEL:
+            {
+                status = tivxSwitchChannel(prms,(tivx_obj_desc_user_data_object_t *)obj_desc[0U]);
+                printf("Reached Control function of DL pre Proc\n");
+                break;
+            }
+            default:
+            {
+                VX_PRINT(VX_ZONE_ERROR, "Invalid Command Id\n");
+                status = (vx_status)VX_FAILURE;
+                break;
+            }
+        }
+    }
+    return status;
+}
+
 static vx_status VX_CALLBACK tivxKernelImgPreProcCreate
 (
     tivx_target_kernel_instance kernel,
@@ -517,6 +601,8 @@ static vx_status VX_CALLBACK tivxKernelImgPreProcCreate
         VX_PRINT(VX_ZONE_INFO, "avail_size = %d\n", avail_size);
         VX_PRINT(VX_ZONE_INFO, "\n");
 
+        kernelParams->active_channel = 0;
+
         kernelParams->l2_heap_id = TIVX_MEM_INTERNAL_L2; /* TIVX_MEM_INTERNAL_L2 or TIVX_MEM_EXTERNAL */
         kernelParams->l2_global_base = 0;
 
@@ -699,7 +785,23 @@ static vx_status VX_CALLBACK tivxKernelImgPreProcProcess
         configuration_target_ptr = tivxMemShared2TargetPtr(&configuration_desc->mem_ptr);
         tivxMemBufferMap(configuration_target_ptr, configuration_desc->mem_size, VX_MEMORY_TYPE_HOST,VX_READ_ONLY);
 
-        in_img_desc  = (tivx_obj_desc_image_t *)obj_desc[TIVX_KERNEL_IMG_PREPROCESS_INPUT_IMAGE_IDX];
+        if((vx_status)VX_SUCCESS == status)
+        {
+            uint32_t active_channel;
+
+            active_channel = prms->active_channel;
+            // printf("active_channel = %d\n", prms->active_channel);
+
+            in_img_desc = (tivx_obj_desc_image_t *)
+                tivxGetObjDescElement(obj_desc[TIVX_KERNEL_IMG_PREPROCESS_INPUT_IMAGE_IDX],
+                                      active_channel);
+
+            if (in_img_desc == NULL)
+            {
+                status = (vx_status)VX_FAILURE;
+            }
+        }
+
         in_image_target_ptr[0]  = tivxMemShared2TargetPtr(&in_img_desc->mem_ptr[0]);
         tivxMemBufferMap(in_image_target_ptr[0], in_img_desc->mem_size[0], TIVX_MEMORY_TYPE_DMA, VX_READ_ONLY);
         in_image_target_ptr[1]  = NULL;
@@ -790,7 +892,7 @@ void tivxAddTargetKernelImgPreProc()
                                             tivxKernelImgPreProcProcess,
                                             tivxKernelImgPreProcCreate,
                                             tivxKernelImgPreProcDelete,
-                                            NULL,
+                                            tivxKernelImgPreProcControl,
                                             NULL
                                         );
     }
diff --git a/kernels/img_proc/include/TI/tivx_img_proc.h b/kernels/img_proc/include/TI/tivx_img_proc.h
index 22957f6..717a57e 100644
--- a/kernels/img_proc/include/TI/tivx_img_proc.h
+++ b/kernels/img_proc/include/TI/tivx_img_proc.h
@@ -67,5 +67,8 @@
 #include <TI/tivx_img_proc_kernels.h>
 #include <TI/tivx_img_proc_nodes.h>
 
+#define TIVX_SWITCH_CHANNEL     0x1
+#define TIVX_SWITCH_POST_CHANNEL     0x2
+
 
 #endif /* TIVX_IMG_PROC_H_ */
diff --git a/kernels/img_proc/include/TI/tivx_img_proc_kernels.h b/kernels/img_proc/include/TI/tivx_img_proc_kernels.h
index 2b4f206..0429cea 100644
--- a/kernels/img_proc/include/TI/tivx_img_proc_kernels.h
+++ b/kernels/img_proc/include/TI/tivx_img_proc_kernels.h
@@ -259,6 +259,17 @@ typedef struct{
 
 }tivxImgPreProcParams;
 
+typedef struct
+{
+    /*! Id of the active channel to be displayed */
+    uint32_t                   active_channel;
+} tivx_select_channel_t;
+typedef struct
+{
+    /*! Id of the active channel to be displayed */
+    uint32_t                   active_channel;
+} tivx_select_postchannel_t;
+
 /*!
  * \brief
  * \ingroup group_vision_apps_kernels_img_proc
