Example: NvMedia Tensor Allocation with NvSciBuf

Following is an example of how to allocate an NvMedia Tensor with NvSciBuf:


NvMediaStatus status;
NvSciError  err;
NvSciBufModule module;
NvSciBufAttrList attrlist;
NvSciBufAttrList conflictlist;
NvSciBufObj bufObj;
NvMediaTensor *tensor;
uint32_t numTensorAttr = NVM_TENSOR_ATTR_MAX;
NVM_TENSOR_DEFINE_ATTR(tensorAttr);
/*NvMedia related initialization. */

status = NvMediaTensorNvSciBufInit();
/*NvSciBuf related initialization. */  
err = NvSciBufModuleOpen(&module);
NvSciBufAttrKeyValuePair attr_kvp =  {NvSciBufGeneralAttrKey_RequiredPerm, &access_perm,
        sizeof(access_perm)};
/*Create NvSciBuf attribute list. */
err = NvSciBufAttrListCreate(module, &attrlist);
err = NvSciBufAttrListSetAttrs(attrlist, &attr_kvp, 1);
/* Initialize tensorAttrs as required. */
NVM_TENSOR_SET_ATTR_4D(tensorAttr, n, c, h, w, NCHW, INT, 8,   UNCACHED, NONE, x);
/* Ask NvMedia to fill NvSciBufAttrs corresponding to 
tensorAttrs. */
status = NvMediaTensorFillNvSciBufAttrs(NULL,
                                      tensorAttr,
                                      numTensorAttr,
                                      0,
                                      attrlist);
/* Reconcile the NvSciBufAttrs and then allocate an NvSciBufObj. */
err = NvSciBufAttrListReconcileAndObjAlloc(&attrlist, 1, bufobj,  &conflictlist);
/* Create NvMediaTensor from NvSciBufObj. */
status = NvMediaTensorCreateFromNvSciBuf(NULL, bufobj, &tensor);
/* Free the NvSciBufAttrList which is no longer required. */
err = NvSciBufAttrListFree(attrlist);
/* Use the tensor as input or output as supported. */
....
....
/* Free the resources after use. */
/* Destroy NvMediaTensor. */
NvMediaTensorDestroy(tensor);
/* NvMedia related Deinit. */
NvMediaTensorNvSciBufDeinit();

/* NvSciBuf related deinit. */
NvSciBufObjFree(bufobj);
NvSciBufModuleClose(module);