TIOVX User Guide
phase_rgb_user_kernel.c
Go to the documentation of this file.
1 /*
2 *
3 * Copyright (c) 2017 Texas Instruments Incorporated
4 *
5 * All rights reserved not granted herein.
6 *
7 * Limited License.
8 *
9 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10 * license under copyrights and patents it now or hereafter owns or controls to make,
11 * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12 * terms herein. With respect to the foregoing patent license, such license is granted
13 * solely to the extent that any such patent is necessary to Utilize the software alone.
14 * The patent license shall not apply to any combinations which include this software,
15 * other than combinations with devices manufactured by or for TI ("TI Devices").
16 * No hardware patent is licensed hereunder.
17 *
18 * Redistributions must preserve existing copyright notices and reproduce this license
19 * (including the above copyright notice and the disclaimer and (if applicable) source
20 * code license limitations below) in the documentation and/or other materials provided
21 * with the distribution
22 *
23 * Redistribution and use in binary form, without modification, are permitted provided
24 * that the following conditions are met:
25 *
26 * * No reverse engineering, decompilation, or disassembly of this software is
27 * permitted with respect to any software provided in binary form.
28 *
29 * * any redistribution and use are licensed by TI for use only with TI Devices.
30 *
31 * * Nothing shall obligate TI to provide you with source code for the software
32 * licensed and provided to you in object code.
33 *
34 * If software source code is provided to you, modification and redistribution of the
35 * source code are permitted provided that the following conditions are met:
36 *
37 * * any redistribution and use of the source code, including any resulting derivative
38 * works, are licensed by TI for use only with TI Devices.
39 *
40 * * any redistribution and use of any object code compiled from the source code
41 * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42 *
43 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44 *
45 * may be used to endorse or promote products derived from this software without
46 * specific prior written permission.
47 *
48 * DISCLAIMER.
49 *
50 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
60 *
61 */
62 
99 #include <stdio.h>
100 #include <VX/vx.h>
101 #include <TI/tivx.h>
103 
105 #define PHASE_RGB_IN0_IMG_IDX (0u)
106 
108 #define PHASE_RGB_OUT0_IMG_IDX (1u)
109 
111 #define PHASE_RGB_MAX_PARAMS (2u)
112 
114  const vx_reference parameters[ ],
115  vx_uint32 num);
117  const vx_reference parameters[ ],
118  vx_uint32 num);
120  const vx_reference parameters[ ],
121  vx_uint32 num);
123  const vx_reference parameters[ ],
124  vx_uint32 num,
125  vx_meta_format metas[]);
126 
129 
132 
135 
136 
145 {
146  vx_kernel kernel = NULL;
147  vx_status status;
148  uint32_t index;
149 
156  if(add_as_target_kernel==(vx_bool)vx_false_e)
157  {
159  }
160  if(add_as_target_kernel==(vx_bool)vx_true_e)
161  {
163  }
170  status = vxGetStatus((vx_reference)kernel);
172  if ( status == (vx_status)VX_SUCCESS)
173  {
184  index = 0;
185 
186  if ( status == (vx_status)VX_SUCCESS)
187  {
188  status = vxAddParameterToKernel(kernel,
189  index,
190  (vx_enum)VX_INPUT,
193  );
194  index++;
195  }
196  if ( status == (vx_status)VX_SUCCESS)
197  {
198  status = vxAddParameterToKernel(kernel,
199  index,
203  );
204  index++;
205  }
211  if ( status == (vx_status)VX_SUCCESS)
212  {
213  status = vxFinalizeKernel(kernel);
214  }
216  if( status != (vx_status)VX_SUCCESS)
217  {
218  printf(" phase_rgb_user_kernel_add: ERROR: vxAddParameterToKernel, vxFinalizeKernel failed (%d)!!!\n", status);
219  vxReleaseKernel(&kernel);
220  kernel = NULL;
221  }
222  }
223  else
224  {
225  kernel = NULL;
226  printf(" phase_rgb_user_kernel_add: ERROR: vxAddUserKernel failed (%d)!!!\n", status);
227  }
228 
229  if(status==(vx_status)VX_SUCCESS)
230  {
237  phase_rgb_user_kernel = kernel;
239  printf(" phase_rgb_user_kernel_add: SUCCESS !!!\n");
240  }
241 
242  return status;
243 }
244 
253 {
254  vx_kernel kernel = NULL;
255  vx_status status;
256 
265  if(status!=(vx_status)VX_SUCCESS)
266  {
267  printf(" phase_rgb_user_kernel_add_as_user_kernel: ERROR: vxAllocateUserKernelId failed (%d)!!!\n", status);
268  }
269  if(status==(vx_status)VX_SUCCESS)
270  {
284  kernel = vxAddUserKernel(
285  context,
289  2, /* number of parameters objects for this user function */
294  }
295 
296  return kernel;
297 }
298 
307 {
308  vx_kernel kernel = NULL;
309  vx_status status;
310 
319  if(status!=(vx_status)VX_SUCCESS)
320  {
321  printf(" phase_rgb_user_kernel_add_as_target_kernel: ERROR: vxAllocateUserKernelId failed (%d)!!!\n", status);
322  }
323  if(status==(vx_status)VX_SUCCESS)
324  {
340  kernel = vxAddUserKernel(
341  context,
344  NULL,
345  2, /* number of parameters objects for this user function */
347  NULL,
348  NULL);
349 
350  status = vxGetStatus((vx_reference)kernel);
353  if ( status == (vx_status)VX_SUCCESS)
354  {
361  #ifndef SOC_AM62A
363  #endif
364 
365  }
366  }
367  return kernel;
368 }
369 
376 {
377  vx_status status;
378 
385  phase_rgb_user_kernel = NULL;
388  if(status!=(vx_status)VX_SUCCESS)
389  {
390  printf(" phase_rgb_user_kernel_remove: Unable to remove kernel (%d)!!!\n", status);
391  }
392 
393  if(status==(vx_status)VX_SUCCESS)
394  {
395  printf(" phase_rgb_user_kernel_remove: SUCCESS !!!\n");
396  }
397 
398  return status;
399 }
400 
418  const vx_reference parameters[ ],
419  vx_uint32 num,
420  vx_meta_format metas[])
421 {
422  vx_status status = (vx_status)VX_SUCCESS;
426  vx_uint32 out_w, out_h, i;
427  vx_df_image fmt[PHASE_RGB_MAX_PARAMS], out_fmt;
428 
429  if (num != PHASE_RGB_MAX_PARAMS)
430  {
431  printf(" phase_rgb_user_kernel_validate: ERROR: Number of parameters dont match !!!\n");
433  }
434 
435  for (i = 0U; i < PHASE_RGB_MAX_PARAMS; i ++)
436  {
437  img[i] = (vx_image)parameters[i];
438 
439  /* Check for NULL */
440  if (NULL == img[i])
441  {
442  printf(" phase_rgb_user_kernel_validate: ERROR: Parameter %d is NULL !!!\n", i);
443  status = (vx_status)VX_ERROR_NO_MEMORY;
444  break;
445  }
446  }
447  if ((vx_status)VX_SUCCESS == status)
448  {
449  /* Get the image width/heigh and format */
450  status = vxQueryImage(img[PHASE_RGB_IN0_IMG_IDX],
452  sizeof(vx_df_image));
453  status |= vxQueryImage(img[PHASE_RGB_IN0_IMG_IDX],
455  sizeof(vx_uint32));
456  status |= vxQueryImage(img[PHASE_RGB_IN0_IMG_IDX],
458  sizeof(vx_uint32));
459 
460  if(status!=(vx_status)VX_SUCCESS)
461  {
462  printf(" phase_rgb_user_kernel_validate: ERROR: Unable to query input image !!!\n");
463  }
464  }
465  if (((vx_status)VX_SUCCESS == status) &&
467  parameters[PHASE_RGB_OUT0_IMG_IDX])))
468  {
469  /* Get the image width/heigh and format */
470  status = vxQueryImage(img[PHASE_RGB_OUT0_IMG_IDX],
472  sizeof(vx_df_image));
473  status |= vxQueryImage(img[PHASE_RGB_OUT0_IMG_IDX],
475  sizeof(vx_uint32));
476  status |= vxQueryImage(img[PHASE_RGB_OUT0_IMG_IDX],
478  sizeof(vx_uint32));
479 
480  if(status!=(vx_status)VX_SUCCESS)
481  {
482  printf(" phase_rgb_user_kernel_validate: ERROR: Unable to query output image !!!\n");
483  }
484  }
485 
486  if ((vx_status)VX_SUCCESS == status)
487  {
488  /* Check for validity of data format */
491  )
492  {
494  printf(" phase_rgb_user_kernel_validate: ERROR: Input/Output image format not correct !!!\n");
495  }
496 
497  /* Check for frame sizes */
499  parameters[PHASE_RGB_OUT0_IMG_IDX]))
500  {
501  if ((w[PHASE_RGB_IN0_IMG_IDX] !=
503  (h[PHASE_RGB_IN0_IMG_IDX] !=
505  {
507  printf(" phase_rgb_user_kernel_validate: ERROR: Input/Output image wxh do not match !!!\n");
508  }
509  }
510  }
511 
512  if ((vx_status)VX_SUCCESS == status)
513  {
514  out_fmt = (vx_df_image)VX_DF_IMAGE_RGB;
515  out_w = w[PHASE_RGB_OUT0_IMG_IDX];
516  out_h = h[PHASE_RGB_OUT0_IMG_IDX];
517 
519  sizeof(out_fmt));
521  sizeof(out_w));
523  sizeof(out_h));
524  }
525 
526  if(status==(vx_status)VX_SUCCESS)
527  {
528  printf(" phase_rgb_user_kernel_validate: SUCCESS !!!\n");
529  }
530 
531  return status;
532 }
533 
548  const vx_reference parameters[ ],
549  vx_uint32 num)
550 {
551  printf(" phase_rgb_user_kernel_init: SUCCESS !!!\n");
552  return (vx_status)VX_SUCCESS;
553 }
554 
569  const vx_reference parameters[ ],
570  vx_uint32 num)
571 {
572  vx_image in_image = (vx_image)parameters[PHASE_RGB_IN0_IMG_IDX];
573  vx_image out_image = (vx_image)parameters[PHASE_RGB_OUT0_IMG_IDX];
574  vx_rectangle_t rect = { 0 };
576  vx_imagepatch_addressing_t in_image_addr, out_image_addr;
577  vx_uint32 out_w=0, out_h=0;
578  vx_status status=0;
579  void *in_data_ptr, *out_data_ptr;
580 
581  status |= vxQueryImage(out_image,
582  (vx_enum)VX_IMAGE_WIDTH, &out_w,
583  sizeof(vx_uint32));
584  status |= vxQueryImage(out_image,
585  (vx_enum)VX_IMAGE_HEIGHT, &out_h,
586  sizeof(vx_uint32));
587 
588  rect.start_x = 0;
589  rect.start_y = 0;
590  rect.end_x = out_w;
591  rect.end_y = out_h;
592 
593  if(status==(vx_status)VX_SUCCESS)
594  {
595  status = vxMapImagePatch(in_image,
596  &rect,
597  0,
598  &in_map_id,
599  &in_image_addr,
600  (void**)&in_data_ptr,
602  }
603 
604  if(status==(vx_status)VX_SUCCESS)
605  {
606  status = vxMapImagePatch(out_image,
607  &rect,
608  0,
609  &out_map_id,
610  &out_image_addr,
611  (void**)&out_data_ptr,
613  }
614  if(status==(vx_status)VX_SUCCESS)
615  {
616  vx_uint32 x, y;
617 
618  for(y=0; y<out_image_addr.dim_y; y++)
619  {
620  for(x=0; x<out_image_addr.dim_x; x++)
621  {
622  vx_uint8 *in_pixel = (vx_uint8 *)vxFormatImagePatchAddress2d(in_data_ptr, x, y, &in_image_addr);
623  vx_uint8 *out_pixel = (vx_uint8 *)vxFormatImagePatchAddress2d(out_data_ptr, x, y, &out_image_addr);
624 
625  if(*in_pixel<64)
626  {
627  out_pixel[0] = 0xFF;
628  out_pixel[1] = 0x00;
629  out_pixel[2] = 0x00;
630  }
631  else
632  if(*in_pixel<128)
633  {
634  out_pixel[0] = 0x00;
635  out_pixel[1] = 0xFF;
636  out_pixel[2] = 0x00;
637  }
638  else
639  if(*in_pixel<192)
640  {
641  out_pixel[0] = 0x00;
642  out_pixel[1] = 0x00;
643  out_pixel[2] = 0xFF;
644  }
645  else
646  {
647  out_pixel[0] = 0xFF;
648  out_pixel[1] = 0xFF;
649  out_pixel[2] = 0xFF;
650  }
651  }
652  }
653 
654  }
655 
657  {
658  vxUnmapImagePatch(in_image, in_map_id);
659  }
661  {
662  vxUnmapImagePatch(out_image, out_map_id);
663  }
664 
665  if(status!=(vx_status)VX_SUCCESS)
666  {
667  printf(" phase_rgb_user_kernel_run: ERROR: Run failed (%d)!!!\n", status);
668  }
669  return status;
670 }
671 
685  const vx_reference parameters[ ],
686  vx_uint32 num)
687 {
688  printf(" phase_rgb_user_kernel_deinit: SUCCESS !!!\n");
689  return (vx_status)VX_SUCCESS;
690 }
691 
708 {
709  vx_node node;
714  vx_reference refs[] = {(vx_reference)in, (vx_reference)out};
721  node = tivxCreateNodeByKernelEnum(graph,
723  refs, sizeof(refs)/sizeof(refs[0])
724  );
725  vxSetReferenceName((vx_reference)node, "PHASE_RGB");
728  return node;
729 }
730 
731 
struct _vx_image * vx_image
vx_bool
vx_false_e
#define TIVX_TUTORIAL_KERNEL_PHASE_RGB_NAME
Converts Phase output to RGB image.
vx_status VX_API_CALL vxQueryImage(vx_image image, vx_enum attribute, void *ptr, vx_size size)
VX_INPUT
static vx_status VX_CALLBACK phase_rgb_user_kernel_deinit(vx_node node, const vx_reference parameters[], vx_uint32 num)
User/target kernel de-init function.
uint8_t vx_uint8
VX_MEMORY_TYPE_HOST
#define PHASE_RGB_MAX_PARAMS
Total number of parameters for this function.
Interface to TI extension APIs.
int32_t vx_enum
vx_kernel VX_API_CALL vxAddUserKernel(vx_context context, const vx_char name[VX_MAX_KERNEL_NAME], vx_enum enumeration, vx_kernel_f func_ptr, vx_uint32 numParams, vx_kernel_validate_f validate, vx_kernel_initialize_f init, vx_kernel_deinitialize_f deinit)
vx_status phase_rgb_user_kernel_remove(vx_context context)
Remove user/target kernel from context.
static vx_kernel phase_rgb_user_kernel_add_as_target_kernel(vx_context context)
Add kernel as target kernel.
VX_SUCCESS
vx_status VX_API_CALL vxUnmapImagePatch(vx_image image, vx_map_id map_id)
vx_true_e
VX_DF_IMAGE_U8
static vx_enum phase_rgb_user_kernel_id
Kernel ID of the registered user kernel. Used to create a node for the kernel function [static global...
vx_enum vx_status
vx_status VX_API_CALL vxRemoveKernel(vx_kernel kernel)
struct _vx_context * vx_context
struct _vx_meta_format * vx_meta_format
vx_uint32 start_x
#define PHASE_RGB_IN0_IMG_IDX
Index of input image in parameter list.
struct _vx_reference * vx_reference
#define TIVX_TARGET_DSP2
Name for DSP target class, instance 2.
uint32_t vx_df_image
static vx_kernel phase_rgb_user_kernel_add_as_user_kernel(vx_context context)
Add kernel as user kernel.
VX_DF_IMAGE_RGB
VX_IMAGE_WIDTH
VX_WRITE_ONLY
vx_node tivxCreateNodeByKernelEnum(vx_graph graph, vx_enum kernelenum, vx_reference params[], vx_uint32 num)
Utility function to create a node given parameter references and kernel enum.
VX_READ_ONLY
VX_NOGAP_X
vx_status VX_API_CALL vxMapImagePatch(vx_image image, const vx_rectangle_t *rect, vx_uint32 plane_index, vx_map_id *map_id, vx_imagepatch_addressing_t *addr, void **ptr, vx_enum usage, vx_enum mem_type, vx_uint32 flags)
#define TIVX_TARGET_DSP1
Name for DSP target class, instance 1.
VX_ERROR_NO_MEMORY
VX_ERROR_INVALID_PARAMETERS
vx_status VX_API_CALL vxSetReferenceName(vx_reference ref, const vx_char *name)
VX_IMAGE_FORMAT
struct _vx_kernel * vx_kernel
vx_uint32 start_y
vx_status phase_rgb_user_kernel_add(vx_context context, vx_bool add_as_target_kernel)
Add user/target kernel to OpenVX context.
vx_status VX_API_CALL vxReleaseKernel(vx_kernel *kernel)
static vx_status VX_CALLBACK phase_rgb_user_kernel_run(vx_node node, const vx_reference parameters[], vx_uint32 num)
User/target kernel execute or run function.
vx_uint32 end_x
#define PHASE_RGB_OUT0_IMG_IDX
Index of output image in parameter list.
vx_status VX_API_CALL vxFinalizeKernel(vx_kernel kernel)
#define VX_CALLBACK
VX_TYPE_IMAGE
VX_OUTPUT
VX_API_ENTRY vx_status VX_API_CALL tivxAddKernelTarget(vx_kernel kernel, const char *target_name)
Associate a target with a kernel.
VX_PARAMETER_STATE_REQUIRED
static vx_kernel phase_rgb_user_kernel
Handle to the registered user kernel [static global].
vx_node phase_rgb_user_kernel_node(vx_graph graph, vx_image in, vx_image out)
User/target kernel node create function.
uint32_t vx_uint32
static vx_status VX_CALLBACK phase_rgb_user_kernel_init(vx_node node, const vx_reference parameters[], vx_uint32 num)
User/target kernel init function.
static vx_status VX_CALLBACK phase_rgb_user_kernel_validate(vx_node node, const vx_reference parameters[], vx_uint32 num, vx_meta_format metas[])
User/target kernel validate function.
struct _vx_graph * vx_graph
vx_status VX_API_CALL vxAllocateUserKernelId(vx_context context, vx_enum *pKernelEnumId)
vx_bool tivxIsReferenceVirtual(vx_reference ref)
Utility function to check if the given reference is virtual or not.
struct _vx_node * vx_node
uintptr_t vx_map_id
vx_status VX_API_CALL vxAddParameterToKernel(vx_kernel kernel, vx_uint32 index, vx_enum dir, vx_enum data_type, vx_enum state)
vx_status VX_API_CALL vxSetMetaFormatAttribute(vx_meta_format meta, vx_enum attribute, const void *ptr, vx_size size)
vx_uint32 end_y
void *VX_API_CALL vxFormatImagePatchAddress2d(void *ptr, vx_uint32 x, vx_uint32 y, const vx_imagepatch_addressing_t *addr)
VX_IMAGE_HEIGHT
vx_status VX_API_CALL vxGetStatus(vx_reference reference)