From 1cd1c0574beca66469d3c32d4c5a62916d3ad5de Mon Sep 17 00:00:00 2001
From: Brijesh Jadav <Brijesh.Jadav@ti.com>
Date: Wed, 19 Jul 2023 19:02:39 +0530
Subject: [PATCH 1/1] Added Format Conversion module at end of mosaic in
 Multi-Camera applicaton.

Signed-off-by: Brijesh Jadav <Brijesh.Jadav@ti.com>
---
 apps/basic_demos/app_multi_cam/main.c |  28 ++++-
 modules/include/app_fmt_cnv_module.h  |  85 +++++++++++++
 modules/src/app_fmt_cnv_module.c      | 164 ++++++++++++++++++++++++++
 modules/src/concerto.mak              |   1 +
 4 files changed, 274 insertions(+), 4 deletions(-)
 create mode 100755 modules/include/app_fmt_cnv_module.h
 create mode 100755 modules/src/app_fmt_cnv_module.c

diff --git a/apps/basic_demos/app_multi_cam/main.c b/apps/basic_demos/app_multi_cam/main.c
index b4c8b13..240aa18 100755
--- a/apps/basic_demos/app_multi_cam/main.c
+++ b/apps/basic_demos/app_multi_cam/main.c
@@ -75,6 +75,7 @@
 #include "app_aewb_module.h"
 #include "app_ldc_module.h"
 #include "app_img_mosaic_module.h"
+#include "app_fmt_cnv_module.h"
 #include "app_display_module.h"
 #include "app_test.h"
 
@@ -94,6 +95,7 @@ typedef struct {
     AEWBObj       aewbObj1;
     LDCObj        ldcObj1;
     ImgMosaicObj  imgMosaicObj;
+    FmtConvObj    fmtConvObj;
     DisplayObj    displayObj;
 
     vx_char output_file_path[APP_MAX_FILE_PATH];
@@ -829,7 +831,7 @@ static vx_status app_init(AppObj *obj)
         {
             status = app_init_ldc(obj->context, &obj->ldcObj1, &obj->sensorObj, "ldc_obj",obj->objArrSplitObj.output1_num_elements);
             APP_PRINTF("LDC init done!\n");
-        }        
+        }
     }
 
     if((obj->enable_mosaic == 1) && (status == VX_SUCCESS))
@@ -838,6 +840,11 @@ static vx_status app_init(AppObj *obj)
         APP_PRINTF("Img Mosaic init done!\n");
     }
 
+    if (status == VX_SUCCESS)
+    {
+        obj->fmtConvObj.color_format = VX_DF_IMAGE_UYVY;
+        status = app_init_fmt_conv(obj->context, &obj->fmtConvObj, 1920, 1080, 1, "Format Conversion Object");
+    }
     if (status == VX_SUCCESS)
     {
         status = app_init_display(obj->context, &obj->displayObj, "display_obj");
@@ -907,6 +914,8 @@ static void app_deinit(AppObj *obj)
         APP_PRINTF("Img Mosaic deinit done!\n");
     }
 
+    app_deinit_fmt_conv(&obj->fmtConvObj);
+
     app_deinit_display(&obj->displayObj);
     APP_PRINTF("Display deinit done!\n");
 
@@ -944,7 +953,7 @@ static void app_delete_graph(AppObj *obj)
         APP_PRINTF("VISS delete done!\n");
 
         app_delete_aewb(&obj->aewbObj1);
-        APP_PRINTF("AEWB delete done!\n");  
+        APP_PRINTF("AEWB delete done!\n");
     }
 
     if(obj->sensorObj.enable_ldc == 1)
@@ -961,6 +970,9 @@ static void app_delete_graph(AppObj *obj)
     app_delete_img_mosaic(&obj->imgMosaicObj);
     APP_PRINTF("Img Mosaic delete done!\n");
 
+    app_delete_fmt_conv(&obj->fmtConvObj);
+    APP_PRINTF("Format Conversion done!\n");
+
     app_delete_display(&obj->displayObj);
     APP_PRINTF("Display delete done!\n");
 
@@ -1124,7 +1136,14 @@ static vx_status app_create_graph(AppObj *obj)
             status = app_create_graph_img_mosaic(obj->graph, &obj->imgMosaicObj, NULL);
             APP_PRINTF("Img Mosaic graph done!\n");
         }
-        display_in_image = obj->imgMosaicObj.output_image[0];
+
+        if(status == VX_SUCCESS)
+        {
+            status = app_create_graph_fmt_conv(obj->graph, &obj->fmtConvObj, obj->imgMosaicObj.output_image[0]);
+        }
+
+        //display_in_image = obj->imgMosaicObj.output_image[0];
+        display_in_image = obj->fmtConvObj.output;
     }
     else
     {
@@ -1215,6 +1234,7 @@ static vx_status app_create_graph(AppObj *obj)
                 APP_PRINTF("Pipeline params setup done!\n");
             }
         }
+        status = tivxSetNodeParameterNumBufByIndex(obj->fmtConvObj.node, 2, APP_BUFFER_Q_DEPTH);
     }
 
     return status;
@@ -1584,7 +1604,7 @@ static void app_update_param_set(AppObj *obj)
 {
     vx_uint16 resized_width, resized_height;
     appIssGetResizeParams(obj->sensorObj.image_width, obj->sensorObj.image_height, DISPLAY_WIDTH, DISPLAY_HEIGHT, &resized_width, &resized_height);
-    
+
     if ( (obj->sensorObj.num_cameras_enabled == 1))
     {
         obj->objArrSplitObj.output0_num_elements = 1;
diff --git a/modules/include/app_fmt_cnv_module.h b/modules/include/app_fmt_cnv_module.h
new file mode 100755
index 0000000..2c85097
--- /dev/null
+++ b/modules/include/app_fmt_cnv_module.h
@@ -0,0 +1,85 @@
+/*
+ *
+ * Copyright (c) 2021 Texas Instruments Incorporated
+ *
+ * All rights reserved not granted herein.
+ *
+ * Limited License.
+ *
+ * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
+ * license under copyrights and patents it now or hereafter owns or controls to make,
+ * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
+ * terms herein.  With respect to the foregoing patent license, such license is granted
+ * solely to the extent that any such patent is necessary to Utilize the software alone.
+ * The patent license shall not apply to any combinations which include this software,
+ * other than combinations with devices manufactured by or for TI ("TI Devices").
+ * No hardware patent is licensed hereunder.
+ *
+ * Redistributions must preserve existing copyright notices and reproduce this license
+ * (including the above copyright notice and the disclaimer and (if applicable) source
+ * code license limitations below) in the documentation and/or other materials provided
+ * with the distribution
+ *
+ * Redistribution and use in binary form, without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * *       No reverse engineering, decompilation, or disassembly of this software is
+ * permitted with respect to any software provided in binary form.
+ *
+ * *       any redistribution and use are licensed by TI for use only with TI Devices.
+ *
+ * *       Nothing shall obligate TI to provide you with source code for the software
+ * licensed and provided to you in object code.
+ *
+ * If software source code is provided to you, modification and redistribution of the
+ * source code are permitted provided that the following conditions are met:
+ *
+ * *       any redistribution and use of the source code, including any resulting derivative
+ * works, are licensed by TI for use only with TI Devices.
+ *
+ * *       any redistribution and use of any object code compiled from the source code
+ * and any resulting derivative works, are licensed by TI for use only with TI Devices.
+ *
+ * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
+ *
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * DISCLAIMER.
+ *
+ * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#ifndef _APP_FMT_CONV_MODULE
+#define _APP_FMT_CONV_MODULE
+
+#include "app_modules.h"
+#include "app_sensor_module.h"
+
+typedef struct
+{
+    vx_node                         node;
+    vx_image                        output;
+    vx_object_array                 arr;
+    tivx_display_m2m_params_t       m2m_params;
+    vx_user_data_object             m2m_obj;
+    vx_char                         objName[APP_MODULES_MAX_OBJ_NAME_SIZE];
+    vx_int32                        num_ch;
+    vx_int32                        color_format;
+} FmtConvObj;
+
+vx_status app_init_fmt_conv(
+    vx_context context, FmtConvObj *fmtObj, uint32_t out_width, uint32_t out_height, uint32_t num_ch, char *objName);
+void app_deinit_fmt_conv(FmtConvObj *obj);
+void app_delete_fmt_conv(FmtConvObj *obj);
+vx_status app_create_graph_fmt_conv(vx_graph graph, FmtConvObj *obj, vx_image in_img);
+#endif
diff --git a/modules/src/app_fmt_cnv_module.c b/modules/src/app_fmt_cnv_module.c
new file mode 100755
index 0000000..611b1b9
--- /dev/null
+++ b/modules/src/app_fmt_cnv_module.c
@@ -0,0 +1,164 @@
+/*
+ *
+ * Copyright (c) 2017 Texas Instruments Incorporated
+ *
+ * All rights reserved not granted herein.
+ *
+ * Limited License.
+ *
+ * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
+ * license under copyrights and patents it now or hereafter owns or controls to make,
+ * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
+ * terms herein.  With respect to the foregoing patent license, such license is granted
+ * solely to the extent that any such patent is necessary to Utilize the software alone.
+ * The patent license shall not apply to any combinations which include this software,
+ * other than combinations with devices manufactured by or for TI ("TI Devices").
+ * No hardware patent is licensed hereunder.
+ *
+ * Redistributions must preserve existing copyright notices and reproduce this license
+ * (including the above copyright notice and the disclaimer and (if applicable) source
+ * code license limitations below) in the documentation and/or other materials provided
+ * with the distribution
+ *
+ * Redistribution and use in binary form, without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * *       No reverse engineering, decompilation, or disassembly of this software is
+ * permitted with respect to any software provided in binary form.
+ *
+ * *       any redistribution and use are licensed by TI for use only with TI Devices.
+ *
+ * *       Nothing shall obligate TI to provide you with source code for the software
+ * licensed and provided to you in object code.
+ *
+ * If software source code is provided to you, modification and redistribution of the
+ * source code are permitted provided that the following conditions are met:
+ *
+ * *       any redistribution and use of the source code, including any resulting derivative
+ * works, are licensed by TI for use only with TI Devices.
+ *
+ * *       any redistribution and use of any object code compiled from the source code
+ * and any resulting derivative works, are licensed by TI for use only with TI Devices.
+ *
+ * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
+ *
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * DISCLAIMER.
+ *
+ * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include "app_fmt_cnv_module.h"
+
+vx_status app_init_fmt_conv(
+    vx_context context, FmtConvObj *fmtObj, uint32_t out_width, uint32_t out_height, uint32_t num_ch, char *objName)
+{
+    vx_status status = VX_SUCCESS;
+    vx_image out_img;
+
+    fmtObj->num_ch = num_ch;//;
+
+    printf ("[FMT-CONV-MODULE]: Num Channels %d WxH %dx%d\n",
+        fmtObj->num_ch, out_width, out_height);
+
+    out_img  = vxCreateImage(context, out_width, out_height, fmtObj->color_format);
+    status = vxGetStatus((vx_reference)out_img);
+
+    if(status == VX_SUCCESS)
+    {
+        fmtObj->arr  = vxCreateObjectArray(context, (vx_reference)out_img, fmtObj->num_ch);
+        vxReleaseImage(&out_img);
+
+        status = vxGetStatus((vx_reference)fmtObj->arr);
+        if(status != VX_SUCCESS)
+        {
+            printf("[FMT-CONV-MODULE] Unable to create output array! \n");
+        }
+        else
+        {
+            tivx_display_m2m_params_init(&fmtObj->m2m_params);
+            fmtObj->m2m_params.instId     = 0u;
+            /* Only one pipeline is supported */
+            fmtObj->m2m_params.numPipe    = 1u;
+            fmtObj->m2m_params.pipeId[0U] = 3u;
+            fmtObj->m2m_params.overlayId  = 3u;
+
+            fmtObj->m2m_obj = vxCreateUserDataObject(
+                context, "tivx_display_m2m_params_t",
+                sizeof(tivx_display_m2m_params_t), &fmtObj->m2m_params);
+
+            status = vxGetStatus((vx_reference)fmtObj->m2m_obj);
+            if(status != VX_SUCCESS)
+            {
+                printf("[FMT-CONV-MODULE] Unable to create m2m object! \n");
+            }
+        }
+    }
+    else
+    {
+        printf("[FMT-CONV-MODULE] Unable to create output image template! \n");
+    }
+
+    return status;
+}
+
+void app_deinit_fmt_conv(FmtConvObj *obj)
+{
+    vxReleaseUserDataObject(&obj->m2m_obj);
+
+    vxReleaseObjectArray(&obj->arr);
+}
+
+void app_delete_fmt_conv(FmtConvObj *obj)
+{
+    if(obj->node != NULL)
+    {
+        vxReleaseNode(&obj->node);
+    }
+}
+
+vx_status app_create_graph_fmt_conv(vx_graph graph, FmtConvObj *obj, vx_image in_img)
+{
+    vx_status status = VX_SUCCESS;
+    vx_image input, output;
+    vx_bool replicate[] = {vx_false_e, vx_false_e, vx_true_e};
+
+    input = in_img;
+    if(obj->arr != NULL)
+    {
+        obj->output = output = (vx_image)vxGetObjectArrayItem((vx_object_array)obj->arr, 0);
+    }
+
+    obj->node = tivxDisplayM2MNode(graph, obj->m2m_obj, input, output);
+    status = vxGetStatus((vx_reference)obj->node);
+
+    if(status == VX_SUCCESS)
+    {
+        vxSetNodeTarget(obj->node, VX_TARGET_STRING, TIVX_TARGET_DISPLAY_M2M1);
+        vxSetReferenceName((vx_reference)obj->node, "FmtConvNode");
+
+        vxReplicateNode(graph, obj->node, replicate, 3);
+    }
+    else
+    {
+        printf("[FMT-CONV-MODULE] Unable to create scaler node! \n");
+    }
+
+    vxReleaseImage(&input);
+    vxReleaseImage(&output);
+
+    return status;
+}
+
+
diff --git a/modules/src/concerto.mak b/modules/src/concerto.mak
index cccbb3f..f959842 100755
--- a/modules/src/concerto.mak
+++ b/modules/src/concerto.mak
@@ -18,6 +18,7 @@ CSOURCES    += app_scaler_module.c
 CSOURCES    += app_tidl_module.c
 CSOURCES    += app_img_mosaic_module.c
 CSOURCES    += app_obj_arr_split_module.c
+CSOURCES    += app_fmt_cnv_module.c
 
 IDIRS       += $(IMAGING_IDIRS)
 IDIRS       += $(VISION_APPS_PATH)/kernels/img_proc/include
-- 
2.17.1

