Add cudaNvSciNvMedia sample with/without nvsci* APIs, it takes RGBA image as input and produces YUV via nvmedia

this YUV is consumed by cuda which converts it to grayscale image which is written to file as output
This commit is contained in:
Mahesh Doijade
2020-11-24 16:28:04 +05:30
parent c4e2869a2b
commit 92b0568792
79 changed files with 7836 additions and 480 deletions

View File

@@ -0,0 +1,192 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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.
*/
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/* Nvidia headers */
#include "cmdline.h"
#include "log_utils.h"
#include "misc_utils.h"
#include "config_parser.h"
#include "helper_cuda.h"
/* see cmdline.h for details */
void PrintUsage()
{
printf("cudaNvSciNvMedia\n");
printf("Usage: cudaNvSciNvMedia [options]\n");
printf("Options:\n");
printf("-h Prints usage\n");
printf("-device=n (n >= 0 for cuda device ID)\n");
printf("-cf=[config] 2d config file. Path length limited to %u chars\n", FILE_NAME_SIZE);
printf("-iterations=n (n > 0 - num of iterations of NvMedia-CUDA operations to be launched)\n");
}
SectionMap sectionsMap[] = {
{SECTION_NONE, "", 0, 0} /* Has to be the last item - specifies the end of array */
};
/* see cmdline.h for details */
int ParseArgs(int argc, char *argv[], TestArgs *args)
{
NvMediaBool bLastArg = NVMEDIA_FALSE;
NvMediaBool bDataAvailable = NVMEDIA_FALSE;
NvMediaStatus status = NVMEDIA_STATUS_OK;
const char* filename = NULL;
int i;
args->srcSurfAllocAttrs[0].type = args->dstSurfAllocAttrs[0].type = NVM_SURF_ATTR_WIDTH;
args->srcSurfAllocAttrs[1].type = args->dstSurfAllocAttrs[1].type = NVM_SURF_ATTR_HEIGHT;
args->srcSurfAllocAttrs[2].type = args->dstSurfAllocAttrs[2].type = NVM_SURF_ATTR_EMB_LINES_TOP;
args->srcSurfAllocAttrs[3].type = args->dstSurfAllocAttrs[3].type = NVM_SURF_ATTR_EMB_LINES_BOTTOM;
args->srcSurfAllocAttrs[4].type = args->dstSurfAllocAttrs[4].type = NVM_SURF_ATTR_CPU_ACCESS;
args->srcSurfAllocAttrs[5].type = args->dstSurfAllocAttrs[5].type = NVM_SURF_ATTR_ALLOC_TYPE;
args->srcSurfAllocAttrs[6].type = args->dstSurfAllocAttrs[6].type = NVM_SURF_ATTR_SCAN_TYPE;
args->srcSurfAllocAttrs[7].type = args->dstSurfAllocAttrs[7].type = NVM_SURF_ATTR_COLOR_STD_TYPE;
args->numSurfAllocAttrs = 8;
args->srcSurfFormatAttrs[0].type = args->dstSurfFormatAttrs[0].type = NVM_SURF_ATTR_SURF_TYPE;
args->srcSurfFormatAttrs[1].type = args->dstSurfFormatAttrs[1].type = NVM_SURF_ATTR_LAYOUT;
args->srcSurfFormatAttrs[2].type = args->dstSurfFormatAttrs[2].type = NVM_SURF_ATTR_DATA_TYPE;
args->srcSurfFormatAttrs[3].type = args->dstSurfFormatAttrs[3].type = NVM_SURF_ATTR_MEMORY;
args->srcSurfFormatAttrs[4].type = args->dstSurfFormatAttrs[4].type = NVM_SURF_ATTR_SUB_SAMPLING_TYPE;
args->srcSurfFormatAttrs[5].type = args->dstSurfFormatAttrs[5].type = NVM_SURF_ATTR_BITS_PER_COMPONENT;
args->srcSurfFormatAttrs[6].type = args->dstSurfFormatAttrs[6].type = NVM_SURF_ATTR_COMPONENT_ORDER;
/* ConfigParamsMap
* See nvmedia_2d.h and sample config file(s) for details.
*/
ConfigParamsMap paramsMap[] = {
/*ParamName, &args->variableName, paramType, D, LimitType, Mn, Mx, CharSize, p2C, section */
{"transformMode", &args->blitParams.dstTransform, TYPE_UINT, 0, LIMITS_BOTH, 0, 7, 0, 0, SECTION_NONE},
{"filterMode", &args->blitParams.filter, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"colorStd", &args->blitParams.colorStandard, TYPE_UINT, 0, LIMITS_MIN, 0, 3, 0, 0, SECTION_NONE},
{"validOperations", &args->blitParams.validFields, TYPE_UINT, 0, LIMITS_BOTH, 0, 15, 0, 0, SECTION_NONE},
{"inputfile", &args->inputFileName, TYPE_CHAR_ARR, 0, LIMITS_NONE, 0, 0, FILE_NAME_SIZE, 0, SECTION_NONE},
/*src surface alloc attributes*/
{"srcWidth", &args->srcSurfAllocAttrs[0].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcHeight", &args->srcSurfAllocAttrs[1].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcCPUAccess", &args->srcSurfAllocAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcAllocType", &args->srcSurfAllocAttrs[5].value, TYPE_UINT, 0, LIMITS_BOTH, 0, 1, 0, 0, SECTION_NONE},
{"srcScanType", &args->srcSurfAllocAttrs[6].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 1, 0, 0, SECTION_NONE},
{"srcColorStd", &args->srcSurfAllocAttrs[7].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 12, 0, 0, SECTION_NONE},
/*src surface format attributes*/
{"srcSurfType", &args->srcSurfFormatAttrs[0].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcLayout", &args->srcSurfFormatAttrs[1].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 2, 0, 0, SECTION_NONE},
{"srcDataType", &args->srcSurfFormatAttrs[2].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"srcMemory", &args->srcSurfFormatAttrs[3].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcSubSamplingType", &args->srcSurfFormatAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 0, 4, 0, 0, SECTION_NONE},
{"srcBitsPerComponent", &args->srcSurfFormatAttrs[5].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 10, 0, 0, SECTION_NONE},
{"srcComponentOrder", &args->srcSurfFormatAttrs[6].value, TYPE_UINT, 2, LIMITS_BOTH, 1, 45, 0, 0, SECTION_NONE},
/*srcRect*/
{"srcRectx0", &args->srcRect.x0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRecty0", &args->srcRect.y0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRectx1", &args->srcRect.x1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRecty1", &args->srcRect.y1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
/*dst surface alloc attributes*/
{"dstWidth", &args->dstSurfAllocAttrs[0].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstHeight", &args->dstSurfAllocAttrs[1].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstCPUAccess", &args->dstSurfAllocAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstAllocType", &args->dstSurfAllocAttrs[5].value, TYPE_UINT, 0, LIMITS_BOTH, 0, 1, 0, 0, SECTION_NONE},
{"dstScanType", &args->dstSurfAllocAttrs[6].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 1, 0, 0, SECTION_NONE},
{"dstColorStd", &args->dstSurfAllocAttrs[7].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 12, 0, 0, SECTION_NONE},
/*dst surface format attributes*/
{"dstSurfType", &args->dstSurfFormatAttrs[0].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstLayout", &args->dstSurfFormatAttrs[1].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 2, 0, 0, SECTION_NONE},
{"dstDataType", &args->dstSurfFormatAttrs[2].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"dstMemory", &args->dstSurfFormatAttrs[3].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstSubSamplingType", &args->dstSurfFormatAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 0, 4, 0, 0, SECTION_NONE},
{"dstBitsPerComponent", &args->dstSurfFormatAttrs[5].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 10, 0, 0, SECTION_NONE},
{"dstComponentOrder", &args->dstSurfFormatAttrs[6].value, TYPE_UINT, 2, LIMITS_BOTH, 1, 45, 0, 0, SECTION_NONE},
/*dstRect*/
{"dstRectx0", &args->dstRect.x0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRecty0", &args->dstRect.y0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRectx1", &args->dstRect.x1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRecty1", &args->dstRect.y1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
/*End of the array */
{NULL, NULL, TYPE_UINT, 0, LIMITS_NONE, 0, 0, 0, 0, SECTION_NONE}
};
args->iterations = 100; // Set default iterations value.
if (checkCmdLineFlag(argc, (const char **)argv, "-h")) {
return -1;
}
if (checkCmdLineFlag(argc, (const char **)argv, "cf")) {
char *inputFileName = NULL;
getCmdLineArgumentString(argc, (const char **)argv, "cf", (char**)&inputFileName);
if (!inputFileName) {
printf("ERR: Invalid config file name\n");
return -1;
}
filename = sdkFindFilePath(inputFileName, ".");
}
if (checkCmdLineFlag(argc, (const char **)argv, "iterations")) {
args->iterations = getCmdLineArgumentInt(argc, (const char **)argv, "iterations");
}
if (filename == NULL) {
// Set default file to use if no config file given.
filename = sdkFindFilePath("sample.cfg", ".");
}
if (filename != NULL)
{
printf("Using config file %s\n", filename);
/* Init Parser Map*/
status = ConfigParser_InitParamsMap(paramsMap);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: ConfigParser_InitParamsMap failed! status:%x\n", status);
return -1;
}
status = ConfigParser_ParseFile(paramsMap, 1, sectionsMap, (char*)filename);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: Failed to parse config file. status:%x\n", status);
return -1;
}
}
LOG_INFO("ParseArgs: Validating params from config file\n");
status = ConfigParser_ValidateParams(paramsMap, sectionsMap);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: Some of the params in config file are invalid.\n");
return -1;
}
return 0;
}

View File

@@ -0,0 +1,90 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 _NVMEDIA_2D_CMD_LINE_H_
#define _NVMEDIA_2D_CMD_LINE_H_
/* Include header containing NvMedia2D declarations */
#include "nvmedia_2d.h"
/* Maximum length of the path including file name */
#define FILE_NAME_SIZE 1024
/* TestArgs contains all arguments required to run the 2D test */
typedef struct _TestArgs {
char inputFileName[FILE_NAME_SIZE];
NvMediaSurfAllocAttr srcSurfAllocAttrs[NVM_SURF_ALLOC_ATTR_MAX];
NvMediaSurfAllocAttr dstSurfAllocAttrs[NVM_SURF_ALLOC_ATTR_MAX];
uint32_t numSurfAllocAttrs;
NvMediaSurfFormatAttr srcSurfFormatAttrs[NVM_SURF_FMT_ATTR_MAX];
NvMediaSurfFormatAttr dstSurfFormatAttrs[NVM_SURF_FMT_ATTR_MAX];
NvMediaRect srcRect;
NvMediaRect dstRect;
NvMedia2DBlitParameters blitParams;
size_t iterations;
} TestArgs;
typedef struct {
NvMediaDevice *device;
/* I2D for 2D blit processing */
NvMedia2D *i2d;
NvMediaImage *srcImage;
NvMediaImage *dstImage;
NvMediaRect *srcRect;
NvMediaRect *dstRect;
uint8_t **dstBuff;
uint32_t *dstBuffPitches;
uint8_t *dstBuffer;
uint32_t numSurfaces;
uint32_t bytesPerPixel;
uint32_t heightSurface;
uint32_t widthSurface;
float *xScalePtr;
float *yScalePtr;
} Blit2DTest;
/* Prints application usage options */
void PrintUsage (void);
/* Parses command line arguments.
* Also parses any configuration files supplied in the command line arguments.
* Arguments:
* argc
* (in) Number of tokens in the command line
* argv
* (in) Command line tokens
* args
* (out) Pointer to test arguments structure
*/
int ParseArgs(int argc, char **argv, TestArgs *args);
#endif /* _NVMEDIA_2D_CMD_LINE_H_ */

View File

@@ -0,0 +1,558 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 <stdlib.h>
#include "config_parser.h"
#include "log_utils.h"
#if defined(__QNX__)
#include <strings.h>
#endif
static NvMediaStatus GetParamIndex(ConfigParamsMap *paramsMap, char *paramName, unsigned int *index)
{
int i = 0;
while(paramsMap[i].paramName != NULL) {
if (strcasecmp(paramsMap[i].paramName, paramName) == 0) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
return NVMEDIA_STATUS_BAD_PARAMETER;
}
NvMediaStatus ConfigParser_GetSectionIndexByName(SectionMap *sectionsMap, char *sectionName, unsigned int *index)
{
unsigned int i = 0;
while(sectionsMap[i].secType != SECTION_NONE) {
if(strcmp(sectionsMap[i].name, sectionName) == 0) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
return NVMEDIA_STATUS_BAD_PARAMETER;
}
NvMediaStatus ConfigParser_GetSectionIndexByType(SectionMap *sectionsMap, SectionType sectionType, unsigned int *index)
{
unsigned int i = 0;
while(sectionsMap[i].secType != SECTION_NONE) {
if(sectionsMap[i].secType == sectionType) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
*index = i;
return NVMEDIA_STATUS_OK;
}
static NvMediaStatus GetFileContent(char *filename, char **fileContentOut)
{
FILE *file;
char *fileCotent;
long fileSize;
file = fopen(filename, "r");
if(file == NULL) {
printf("Parser_GetFileContent: Cannot open configuration file %s\n", filename);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
if (fseek(file, 0, SEEK_END) != 0) {
printf("Parser_GetFileContent: Cannot fseek in configuration file %s\n", filename);
return NVMEDIA_STATUS_ERROR;
}
fileSize = ftell(file);
if(fileSize < 0 || fileSize > 150000) {
printf("Parser_GetFileContent: Unreasonable Filesize %ld encountered for file %s\n", fileSize, filename);
return NVMEDIA_STATUS_ERROR;
}
if(fseek (file, 0, SEEK_SET) != 0) {
printf("Parser_GetFileContent: Cannot fseek in configuration file %s\n", filename);
return NVMEDIA_STATUS_ERROR;
}
fileCotent = (char*)malloc(fileSize + 1);
if(fileCotent == NULL) {
printf("Parser_GetFileContent: Failed allocating buffer for file Content\n");
return NVMEDIA_STATUS_OUT_OF_MEMORY;
}
fileSize = (long)fread(fileCotent, 1, fileSize, file);
fileCotent[fileSize] = '\0';
*fileContentOut = fileCotent;
fclose(file);
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_ParseFile(ConfigParamsMap *paramsMap, unsigned int numParams, SectionMap *sectionsMap, char *fileName)
{
char *items[MAX_ITEMS_TO_PARSE] = {NULL};
int intValue, itemsCount = 0, i = 0, sectionIndex = 0;
double doubleValue;
float floatValue;
unsigned int currItemIndex, uintValue, sectionId = 0, currSectionId = 0, charValue, paramDefaultLength;
unsigned short ushortValue;
short shortValue;
unsigned long long ullValue;
NvMediaBool isInString = NVMEDIA_FALSE, isInItem = NVMEDIA_FALSE;
char *buffer, *bufferEnd, *param, *pParamLength;
char sectionName[100];
char currDigit;
char *configContentBuf = NULL;
unsigned int numSetsInSection = 0;
if(GetFileContent(fileName, &configContentBuf) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: Failed reading file %s", fileName);
return NVMEDIA_STATUS_ERROR;
}
buffer = configContentBuf;
bufferEnd = &configContentBuf[strlen(configContentBuf)];
// Stage one: Create items mapping in the content using "items" pointers array. For each parameter we have 3 items: param name, '=' char and the param value.
while(buffer < bufferEnd) {
if(itemsCount >= MAX_ITEMS_TO_PARSE) {
LOG_WARN("ConfigParser_ParseFile: Number of items in configuration file exceeded the maximum allowed (%d). Only %d items will be parsed.\n",
MAX_ITEMS_TO_PARSE, MAX_ITEMS_TO_PARSE);
itemsCount = MAX_ITEMS_TO_PARSE;
break;
}
switch(*buffer) {
// Carriage return
case 13:
++buffer;
break;
case '#':
*buffer = '\0'; // Replace '#' with '\0' in case of comment immediately following integer or string
while(*buffer != '\n' && buffer < bufferEnd) { // Skip till EOL or EOF
++buffer;
}
isInString = NVMEDIA_FALSE;
isInItem = NVMEDIA_FALSE;
break;
case '\n':
isInItem = NVMEDIA_FALSE;
isInString = NVMEDIA_FALSE;
*buffer++='\0';
break;
case ' ':
case '\t': // Skip whitespace, leave state unchanged
if(isInString)
buffer++;
else { // Terminate non-strings once whitespace is found
*buffer++ = '\0';
isInItem = NVMEDIA_FALSE;
}
break;
case '"': // Begin/End of String
*buffer++ = '\0';
if(!isInString) {
items[itemsCount++] = buffer;
isInItem = ~isInItem;
} else {
isInItem = NVMEDIA_FALSE;
}
isInString = ~isInString; // Toggle
break;
case '[':
*(buffer++) = '\0';
items[itemsCount++] = buffer;
while(*buffer != ' ' && *buffer != '\n' && buffer < bufferEnd) { // Skip till whitespace (after which is located the parsed section number) or EOL or EOF
sectionName[i++] = *(buffer++);
}
sectionName[i] = '\0';
i = 0;
while(*buffer == ' ') {
*(buffer++) = '\0';
}
items[itemsCount++] = buffer;
while(*buffer != ']' && *buffer != '\n' && buffer < bufferEnd) { // Read the section number
currDigit = *buffer;
sectionIndex = sectionIndex * 10 + (currDigit - '0');
buffer++;
}
*(buffer++) = '\0';
sectionIndex--;
if(ConfigParser_GetSectionIndexByName(sectionsMap, sectionName, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: SectionName couldn't be found in section map: '%s'.\n", sectionName);
}
numSetsInSection++;
sectionsMap[sectionId].lastSectionIndex = sectionIndex;
sectionIndex = 0;
isInString = NVMEDIA_FALSE;
isInItem = NVMEDIA_FALSE;
break;
default:
if(!isInItem) {
items[itemsCount++] = buffer;
isInItem = ~isInItem;
}
buffer++;
}
}
itemsCount--;
if(numSetsInSection > numParams) {
printf("%s: Not enough buffers allocated for parsing. Number of sets allocated: %d. Number of sets in config file: %d \n",
__func__, numParams, numSetsInSection);
if(configContentBuf) {
free(configContentBuf);
}
return NVMEDIA_STATUS_ERROR;
}
// Stage 2: Go through the list of items and save their values in parameters map
for(i = 0; i < itemsCount; i += 3) {
if(ConfigParser_GetSectionIndexByName(sectionsMap, items[i], &currItemIndex) == NVMEDIA_STATUS_OK) {
currSectionId = atoi(items[i + 1]);
currSectionId--;
LOG_DBG("ConfigParser_ParseFile: Parsing section %s index %d\n", items[i], currSectionId);
i -= 1;
continue;
}
if(GetParamIndex(paramsMap, items[i], &currItemIndex) != NVMEDIA_STATUS_OK) {
LOG_WARN("ConfigParser_ParseFile: Parameter Name '%s' is not recognized. Dismissing this parameter.\n", items[i]);
continue;
}
if(strcmp("=", items[i + 1])) {
printf("ConfigParser_ParseFile: '=' expected as the second token in each line. Error caught while parsing parameter '%s'.\n", items[i]);
i -= 2;
continue;
}
if(ConfigParser_GetSectionIndexByType(sectionsMap, paramsMap[currItemIndex].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: Section index couldn't be found in section map by type. Param Name: '%s'.\n", paramsMap[currItemIndex].paramName);
}
if(sectionsMap[sectionId].lastSectionIndex == 0) {
// Param is not part of a collection or collection includes only one item
currSectionId = 0;
}
param = (char *)paramsMap[currItemIndex].mappedLocation + currSectionId * sectionsMap[sectionId].sizeOfStruct;
pParamLength = (char *)paramsMap[currItemIndex].stringLengthAddr + currSectionId * sectionsMap[sectionId].sizeOfStruct;
paramDefaultLength = paramsMap[currItemIndex].stringLength;
// Interpret the Value
LOG_DBG("ConfigParser_ParseFile: Interpreting parameter %s\n", items[i]);
switch(paramsMap[currItemIndex].type) {
case TYPE_INT:
if(sscanf(items[i + 2], "%d", &intValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(int *)(void *)param = intValue;
break;
case TYPE_UINT:
if(sscanf(items[i + 2], "%u", &uintValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned int *)(void *)param = uintValue;
break;
case TYPE_UINT_HEX:
if(sscanf(items[i + 2], "%x", &uintValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned char value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned int *)(void *)param = uintValue;
break;
case TYPE_CHAR_ARR:
if(items[i + 2] == NULL)
memset(param, 0, (pParamLength != NULL && *pParamLength != 0) ? *pParamLength : paramDefaultLength);
else {
strncpy(param, items[i + 2], paramsMap[currItemIndex].stringLength);
param[strlen(items[i + 2])] = '\0';
}
break;
case TYPE_DOUBLE:
if(sscanf(items[i + 2], "%lf", &doubleValue) != 1) {
printf("ConfigParser_ParseFile: Expected double value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(double *)(void *)param = doubleValue;
break;
case TYPE_FLOAT:
if(sscanf(items[i + 2], "%f", &floatValue) != 1) {
printf("ConfigParser_ParseFile: Expected double value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(float *)(void *)param = floatValue;
break;
case TYPE_UCHAR:
if(sscanf(items[i + 2], "%u", &charValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned char value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned char *)(void *)param = charValue;
break;
case TYPE_USHORT:
if(sscanf(items[i + 2], "%hu", &ushortValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned short value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned short *)(void *)param = ushortValue;
break;
case TYPE_SHORT:
if(sscanf(items[i + 2], "%hd", &shortValue) != 1) {
printf("ConfigParser_ParseFile: Expected short value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(short *)(void *)param = shortValue;
break;
case TYPE_UCHAR_ARR:
if(items[i + 2] == NULL)
memset(param, 0, (pParamLength != NULL && *pParamLength != 0) ? *pParamLength : paramDefaultLength);
else {
strncpy(param, items[i + 2], paramsMap[currItemIndex].stringLength);
param[strlen(items[i + 2])] = '\0';
}
break;
case TYPE_ULLONG:
if(sscanf(items[i + 2], "%llu", &ullValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned long long *)(void *)param = ullValue;
break;
default:
printf("ConfigParser_ParseFile: Encountered unknown value type in the map\n");
}
}
if (configContentBuf)
free(configContentBuf);
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_InitParamsMap(ConfigParamsMap *paramsMap)
{
int i = 0;
while(paramsMap[i].paramName != NULL) {
if (paramsMap[i].mappedLocation == NULL) {
i++;
continue;
}
switch(paramsMap[i].type) {
case TYPE_UINT:
case TYPE_UINT_HEX:
*(unsigned int *)(paramsMap[i].mappedLocation) = (unsigned int)paramsMap[i].defaultValue;
break;
case TYPE_INT:
*(int *)(paramsMap[i].mappedLocation) = (int)paramsMap[i].defaultValue;
break;
case TYPE_DOUBLE:
*(double *)(paramsMap[i].mappedLocation) = (double)paramsMap[i].defaultValue;
break;
case TYPE_FLOAT:
*(float *)(paramsMap[i].mappedLocation) = (float)paramsMap[i].defaultValue;
break;
case TYPE_UCHAR:
*(unsigned char *)(paramsMap[i].mappedLocation) = (NvMediaBool)paramsMap[i].defaultValue;
break;
case TYPE_USHORT:
*(unsigned short *)(paramsMap[i].mappedLocation) = (unsigned short)paramsMap[i].defaultValue;
break;
case TYPE_SHORT:
*(short *)(paramsMap[i].mappedLocation) = (short)paramsMap[i].defaultValue;
break;
case TYPE_ULLONG:
*(unsigned long long *)(paramsMap[i].mappedLocation) = (unsigned long long)paramsMap[i].defaultValue;
break;
case TYPE_CHAR_ARR:
case TYPE_UCHAR_ARR:
default:
break;
}
i++;
}
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_ValidateParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap)
{
NvMediaStatus status = NVMEDIA_STATUS_OK;
unsigned int sectionId = 0, i = 0, j;
char *param;
while(paramsMap[i].paramName != NULL) {
if(ConfigParser_GetSectionIndexByType(sectionsMap, paramsMap[i].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ValidateParams: Section index couldn't be found in section map. Param Name: '%s'.\n", paramsMap[i].paramName);
}
for(j = 0; j <= sectionsMap[sectionId].lastSectionIndex; j++) {
if(paramsMap[i].paramLimits == 1 || paramsMap[i].paramLimits == 2) {
param = (char *)paramsMap[i].mappedLocation + j * sectionsMap[sectionId].sizeOfStruct;
if (param == NULL) {
i++;
continue;
}
switch (paramsMap[i].type) {
case TYPE_UINT:
case TYPE_UINT_HEX:
if(*(unsigned int *)(void *)param < (unsigned int)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned int *)(void *)param > (unsigned int)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_DOUBLE:
if(*(double *)(void *)param < (double)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(double *)(void *)param > (double)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_FLOAT:
if(*(float *)(void *)param < (float)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(float *)(void *)param > (float)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_INT:
if(*(int *)(void *)param < (int)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(int *)(void *)param > (int)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_USHORT:
if(*(unsigned short *)(void *)param < (unsigned short)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned short *)(void *)param > (unsigned short)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_SHORT:
if(*(short *)(void *)param < (short)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(short *)(void *)param > (short)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_ULLONG:
if(*(unsigned long long *)(void *)param < (unsigned long long)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned long long *)(void *)param > (unsigned long long)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
default:
break;
}
}
}
i++;
}
return status;
}
NvMediaStatus ConfigParser_DisplayParams(ConfigParamsMap *pParamsMap, SectionMap *pSectionsMap)
{
unsigned int i = 0, j, sectionId = 0;
char *param;
while(pParamsMap[i].paramName != NULL) {
if(ConfigParser_GetSectionIndexByType(pSectionsMap, pParamsMap[i].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_DisplayParams: Section index couldn't be found in section map by type. Param Name: '%s'.\n", pParamsMap[i].paramName);
}
for(j = 0; j <= pSectionsMap[sectionId].lastSectionIndex; j++) {
param = (char *)pParamsMap[i].mappedLocation + j * pSectionsMap[sectionId].sizeOfStruct;
if (param == NULL) {
i++;
continue;
}
switch(pParamsMap[i].type) {
case TYPE_UINT:
printf("(%d) %s = %u\n", j, pParamsMap[i].paramName, *(unsigned int *)(void *)param);
break;
case TYPE_DOUBLE:
printf("(%d) %s = %.2lf\n", j, pParamsMap[i].paramName, *(double *)(void *)param);
break;
case TYPE_FLOAT:
printf("(%d) %s = %.2f\n", j, pParamsMap[i].paramName, *(float *)(void *)param);
break;
case TYPE_UCHAR:
printf("(%d) %s = %d\n", j, pParamsMap[i].paramName, *(unsigned char *)(void *)param);
break;
case TYPE_USHORT:
printf("(%d) %s = %hu\n", j, pParamsMap[i].paramName, *(unsigned short *)(void *)param);
break;
case TYPE_SHORT:
printf("(%d) %s = %hd\n", j, pParamsMap[i].paramName, *(short *)(void *)param);
break;
case TYPE_ULLONG:
printf("(%d) %s = %llu\n", j, pParamsMap[i].paramName, *(unsigned long long *)(void *)param);
break;
case TYPE_CHAR_ARR:
printf("(%d) %s = ""%s""\n", j, pParamsMap[i].paramName, param);
break;
case TYPE_UCHAR_ARR:
printf("(%d) %s = ""%s""\n", j, pParamsMap[i].paramName, (unsigned char *)(void *)param);
break;
case TYPE_INT:
printf("(%d) %s = %d\n", j, pParamsMap[i].paramName, *(int *)(void *)param);
break;
case TYPE_UINT_HEX:
printf("(%d) %s = %x\n", j, pParamsMap[i].paramName, *(unsigned int *)(void *)param);
break;
default:
// Do nothing
break;
}
}
i++;
}
return NVMEDIA_STATUS_OK;
}

View File

@@ -0,0 +1,109 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 _NVMEDIA_TEST_CONFIG_PARSER_H_
#define _NVMEDIA_TEST_CONFIG_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "nvmedia_core.h"
#include "nvmedia_surface.h"
#define MAX_ITEMS_TO_PARSE 10000
typedef enum _ParamType {
TYPE_UINT = 0,
TYPE_UINT_HEX,
TYPE_INT,
TYPE_DOUBLE,
TYPE_FLOAT,
TYPE_UCHAR,
TYPE_ULLONG,
TYPE_USHORT,
TYPE_CHAR_ARR,
TYPE_UCHAR_ARR,
TYPE_SHORT
} ParamType;
typedef enum {
LIMITS_NONE = 0,
LIMITS_MIN = 1,
LIMITS_BOTH = 2
} LimitsType;
typedef enum {
SECTION_NONE,
SECTION_CAPTURE,
SECTION_QP,
SECTION_RC,
SECTION_ENCODE_PIC,
SECTION_ENCODE_PIC_H264,
SECTION_ENCODE_PIC_H265,
SECTION_MVC,
SECTION_PAYLOAD,
SECTION_2DPROCESSOR
} SectionType;
typedef struct {
SectionType secType;
const char *name;
unsigned int lastSectionIndex;
size_t sizeOfStruct;
} SectionMap;
typedef struct {
const char *paramName;
void *mappedLocation;
ParamType type;
double defaultValue;
LimitsType paramLimits;
double minLimit;
double maxLimit;
unsigned int stringLength; // string param size
unsigned int *stringLengthAddr; // address of string param size
SectionType sectionType;
} ConfigParamsMap;
NvMediaStatus ConfigParser_InitParamsMap(ConfigParamsMap *paramsMap);
NvMediaStatus ConfigParser_ParseFile(ConfigParamsMap *paramsMap, unsigned int numParams, SectionMap *sectionsMap, char *file);
NvMediaStatus ConfigParser_ValidateParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap);
NvMediaStatus ConfigParser_DisplayParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap);
NvMediaStatus ConfigParser_GetSectionIndexByName(SectionMap *sectionsMap, char *sectionName, unsigned int *index);
NvMediaStatus ConfigParser_GetSectionIndexByType(SectionMap *sectionsMap, SectionType sectionType, unsigned int *index);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_CONFIG_PARSER_H_ */

View File

@@ -0,0 +1,808 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "image_utils.h"
#include "misc_utils.h"
#include "nvmedia_surface.h"
#define MAXM_NUM_SURFACES 6
typedef struct {
float heightFactor[6];
float widthFactor[6];
unsigned int numSurfaces;
} ImgUtilSurfParams;
ImgUtilSurfParams ImgSurfParamsTable_RGBA = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
ImgUtilSurfParams ImgSurfParamsTable_RAW = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
ImgUtilSurfParams ImgSurfParamsTable_YUV[][4] = {
{ /* PLANAR */
{ /* 420 */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 444 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
},
{ /* SEMI_PLANAR */
{ /* 420 */
.heightFactor = {1, 0.5, 0, 0, 0, 0},
.widthFactor = {1, 0.5, 0, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 422 */
.heightFactor = {1, 1, 0, 0, 0, 0},
.widthFactor = {1, 0.5, 0, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 444 */
.heightFactor = {1, 1, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 0.5, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 0.5, 0, 0, 0},
.numSurfaces = 2,
},
},
{ /* PACKED */
{ /* 420 */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 444 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
},
};
ImgUtilSurfParams ImgSurfParamsTable_Packed = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
unsigned int ImgBytesPerPixelTable_RGBA[][6] = {
{4, 0, 0, 0, 0, 0}, /* 8 */
};
unsigned int ImgBytesPerPixelTable_RGBA16[][6] = {
{8, 0, 0, 0, 0, 0}, /* 16 */
};
unsigned int ImgBytesPerPixelTable_RG16[6] =
{4, 0, 0, 0, 0, 0};
unsigned int ImgBytesPerPixelTable_Alpha[][6] = {
{1, 0, 0, 0, 0, 0}, /* 8 */
{2, 0, 0, 0, 0, 0}, /* 10 */
{2, 0, 0, 0, 0, 0}, /* 12 */
{2, 0, 0, 0, 0, 0}, /* 14 */
{2, 0, 0, 0, 0, 0}, /* 16 */
{4, 0, 0, 0, 0, 0}, /* 32 */
};
unsigned int ImgBytesPerPixelTable_RAW[][6] = {
{1, 0, 0, 0, 0, 0}, /* 8 */
{2, 0, 0, 0, 0, 0}, /* 10 */
{2, 0, 0, 0, 0, 0}, /* 12 */
{2, 0, 0, 0, 0, 0}, /* 14 */
{2, 0, 0, 0, 0, 0}, /* 16 */
{4, 0, 0, 0, 0, 0}, /* 32 */
{4, 0, 0, 0, 0, 0}, /* 16_8_8 */
{4, 0, 0, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
{4, 0, 0, 0, 0, 0}, /* 20 */
};
unsigned int ImgBytesPerPixelTable_YUV[][9][6] = {
{ /* PLANAR */
{1, 1, 1, 0, 0, 0}, /* 8 */
{2, 2, 2, 0, 0, 0}, /* 10 */
{2, 2, 2, 0, 0, 0}, /* 12 */
{2, 2, 2, 0, 0, 0}, /* 14 */
{2, 2, 2, 0, 0, 0}, /* 16 */
{4, 4, 4, 0, 0, 0}, /* 32 */
{2, 1, 1, 0, 0, 0}, /* 16_8_8 */
{2, 1, 1, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
},
{ /* SEMI_PLANAR */
{1, 2, 0, 0, 0, 0}, /* 8 */
{2, 4, 0, 0, 0, 0}, /* 10 */
{2, 4, 0, 0, 0, 0}, /* 12 */
{2, 4, 0, 0, 0, 0}, /* 14 */
{2, 4, 0, 0, 0, 0}, /* 16 */
{4, 8, 0, 0, 0, 0}, /* 32 */
{2, 2, 0, 0, 0, 0}, /* 16_8_8 */
{2, 2, 0, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
}
};
static NvMediaStatus
GetBytesPerCompForPackedYUV(unsigned int surfBPCidx,
unsigned int *bytespercomp
)
{
switch(surfBPCidx) {
case NVM_SURF_ATTR_BITS_PER_COMPONENT_8:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_LAYOUT_2_10_10_10:
*bytespercomp = 1;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_16:
*bytespercomp = 2;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_20:
*bytespercomp = 3;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_32:
*bytespercomp = 4;
break;
default:
return NVMEDIA_STATUS_ERROR;
}
return NVMEDIA_STATUS_OK;
}
static NvMediaStatus
GetSurfParams(unsigned int surfaceType,
float **xScale,
float **yScale,
unsigned int **bytePerPixel,
uint32_t *numSurfacesVal)
{
NvMediaStatus status;
unsigned int surfType, surfMemoryType, surfSubSamplingType, surfBPC, surfCompOrder;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
uint32_t numSurfaces = 1;
static unsigned int yuvpackedtbl[6] = {1, 0, 0, 0, 0, 0};
unsigned int numcomps = 1;
status = NvMediaSurfaceFormatGetAttrs(surfaceType,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
return NVMEDIA_STATUS_ERROR;
}
surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
surfMemoryType = srcAttr[NVM_SURF_ATTR_MEMORY].value;
surfSubSamplingType = srcAttr[NVM_SURF_ATTR_SUB_SAMPLING_TYPE].value;
surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
surfCompOrder = srcAttr[NVM_SURF_ATTR_COMPONENT_ORDER].value;
switch(surfType) {
case NVM_SURF_ATTR_SURF_TYPE_YUV:
if (surfSubSamplingType == NVM_SURF_ATTR_SUB_SAMPLING_TYPE_NONE &&
surfMemoryType == NVM_SURF_ATTR_MEMORY_PACKED) {
xScalePtr = &ImgSurfParamsTable_Packed.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_Packed.heightFactor[0];
numSurfaces = ImgSurfParamsTable_Packed.numSurfaces;
if (NVMEDIA_STATUS_OK != GetBytesPerCompForPackedYUV(surfBPC, &yuvpackedtbl[0])) {
printf("Invalid Bits per component and Packed YUV combination\n");
return NVMEDIA_STATUS_ERROR;
}
switch(surfCompOrder) {
case NVM_SURF_ATTR_COMPONENT_ORDER_VUYX:
case NVM_SURF_ATTR_COMPONENT_ORDER_XYUV:
case NVM_SURF_ATTR_COMPONENT_ORDER_XUYV:
numcomps = 4;
break;
case NVM_SURF_ATTR_COMPONENT_ORDER_UYVY:
case NVM_SURF_ATTR_COMPONENT_ORDER_VYUY:
case NVM_SURF_ATTR_COMPONENT_ORDER_YVYU:
case NVM_SURF_ATTR_COMPONENT_ORDER_YUYV:
numcomps = 2;
break;
case NVM_SURF_ATTR_COMPONENT_ORDER_LUMA:
numcomps = 1;
break;
default:
printf("Invalid component Order and Packed YUV combination\n");
return NVMEDIA_STATUS_ERROR;
}
yuvpackedtbl[0] = yuvpackedtbl[0] * numcomps;
bytePerPixelPtr = &yuvpackedtbl[0];
} else {
xScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].widthFactor[0];
yScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].heightFactor[0];
numSurfaces = ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].numSurfaces;
bytePerPixelPtr = &ImgBytesPerPixelTable_YUV[0][surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
}
break;
case NVM_SURF_ATTR_SURF_TYPE_RGBA:
if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_ALPHA) {
bytePerPixelPtr = &ImgBytesPerPixelTable_Alpha[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
} else if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_RG) {
if(surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RG16[0];
} else {
printf("Invalid RGorder & Bitspercomp combination.Only RG16 is supported\n");
return NVMEDIA_STATUS_ERROR;
}
} else { /* RGBA, ARGB, BGRA */
if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA16[0][0];
} else if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_8) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA[0][0];
} else {
printf("RGBA orders with 8 and 16bits only is supported \n");
return NVMEDIA_STATUS_ERROR;
}
}
xScalePtr = &ImgSurfParamsTable_RGBA.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_RGBA.heightFactor[0];
numSurfaces = ImgSurfParamsTable_RGBA.numSurfaces;
break;
case NVM_SURF_ATTR_SURF_TYPE_RAW:
bytePerPixelPtr = &ImgBytesPerPixelTable_RAW[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
xScalePtr = &ImgSurfParamsTable_RAW.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_RAW.heightFactor[0];
numSurfaces = ImgSurfParamsTable_RAW.numSurfaces;
break;
default:
printf("%s: Unsupported Pixel Format %d", __func__, surfType);
return NVMEDIA_STATUS_ERROR;
}
if (xScale) {
*xScale = xScalePtr;
}
if (yScale) {
*yScale = yScalePtr;
}
if (bytePerPixel) {
*bytePerPixel = bytePerPixelPtr;
}
if (numSurfacesVal) {
*numSurfacesVal = numSurfaces;
}
return NVMEDIA_STATUS_OK;
}
NvMediaStatus
AllocateBufferToWriteImage(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag)
{
uint32_t imageSize = 0;
unsigned int size[3] ={0};
uint8_t *buffer = NULL;
uint32_t i, k, newk = 0;
unsigned int *bytePerPixelPtr = NULL;
ctx->numSurfaces = 1;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
uint32_t lineWidth, numRows, startOffset;
if(!image) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
ctx->dstBuff = (uint8_t**) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!ctx->dstBuff) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->dstBuffPitches = (uint32_t*) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!ctx->dstBuffPitches) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->heightSurface = surfaceMap.height;
ctx->widthSurface = surfaceMap.width;
status = GetSurfParams(image->type,
&ctx->xScalePtr,
&ctx->yScalePtr,
&bytePerPixelPtr,
&ctx->numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams() failed\n", __func__);
goto done;
}
imageSize = 0;
for(i = 0; i < ctx->numSurfaces; i++) {
size[i] = (ctx->widthSurface * ctx->xScalePtr[i] * ctx->heightSurface * ctx->yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += size[i];
ctx->dstBuffPitches[i] = (uint32_t)((float)ctx->widthSurface * ctx->xScalePtr[i]) * bytePerPixelPtr[i];
}
// Embedded data size needs to be included for RAW surftype
size[0] += image->embeddedDataTopSize;
size[0] += image->embeddedDataBottomSize;
imageSize += image->embeddedDataTopSize;
imageSize += image->embeddedDataBottomSize;
buffer = (uint8_t *) calloc(1, imageSize);
if(!buffer) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->dstBuffer = buffer;
memset(buffer, 0xFF, imageSize);
for(i = 0; i < ctx->numSurfaces; i++) {
ctx->dstBuff[i] = buffer;
buffer = buffer + (uint32_t)(ctx->heightSurface * ctx->yScalePtr[i] * ctx->dstBuffPitches[i]);
}
done:
return status;
}
NvMediaStatus
WriteImageToAllocatedBuffer(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag,
uint32_t bytesPerPixel)
{
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
goto done;
}
status = NvMediaImageGetBits(image, NULL, (void **)ctx->dstBuff, ctx->dstBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaVideoSurfaceGetBits() failed \n", __func__);
goto done;
}
done:
return status;
}
static NvMediaStatus
ReadImageNew(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment)
{
uint8_t **pBuff = NULL;
uint32_t *pBuffPitches = NULL;
uint32_t imageSize = 0,surfaceSize = 0;
uint8_t *buffer = NULL;
uint8_t *pBuffer = NULL;
uint32_t i, j, k, newk = 0;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
uint32_t numSurfaces = 1;
unsigned int uHeightSurface, uWidthSurface;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
FILE *file = NULL;
unsigned int count, index;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
unsigned int surfType, surfBPC;
if(!image || !fileName) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
uHeightSurface = surfaceMap.height;
uWidthSurface = surfaceMap.width;
if(width > uWidthSurface || height > uHeightSurface) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
pBuff = (uint8_t **) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!pBuff) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffPitches = (uint32_t *)calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!pBuffPitches) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
status = GetSurfParams(image->type,
&xScalePtr,
&yScalePtr,
&bytePerPixelPtr,
&numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams() failed\n", __func__);
goto done;
}
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
goto done;
}
surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
surfaceSize = 0;
imageSize = 0;
for(i = 0; i < numSurfaces; i++) {
surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
}
buffer = (uint8_t *)calloc(1, surfaceSize);
if(!buffer) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffer = buffer;
memset(buffer,0x10,surfaceSize);
for(i = 0; i < numSurfaces; i++) {
pBuff[i] = buffer;
if (i) {
memset(pBuff[i], 0x80, (uHeightSurface * yScalePtr[i] * pBuffPitches[i]));
}
buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
}
file = fopen(fileName, "rb");
if(!file) {
printf("%s: Error opening file: %s\n", __func__, fileName);
status = NVMEDIA_STATUS_ERROR;
goto done;
}
if(frameNum > 0) {
if(fseeko(file, frameNum * (off_t)imageSize, SEEK_SET)) {
printf("ReadImage: Error seeking file: %s\n", fileName);
status = NVMEDIA_STATUS_ERROR;
goto done;
}
}
if((surfType == NVM_SURF_ATTR_SURF_TYPE_RGBA ) && strstr(fileName, ".png")) {
printf("ReadImage: Does not support png format\n");
status = NVMEDIA_STATUS_ERROR;
goto done;
}
for(k = 0; k < numSurfaces; k++) {
for(j = 0; j < height*yScalePtr[k]; j++) {
newk = (!uvOrderFlag && k ) ? (numSurfaces - k) : k;
index = j * pBuffPitches[newk];
count = width * xScalePtr[newk] * bytePerPixelPtr[newk];
if (fread(pBuff[newk] + index, count, 1, file) != 1) {
status = NVMEDIA_STATUS_ERROR;
printf("ReadImage: Error reading file: %s\n", fileName);
goto done;
}
if((surfType == NVM_SURF_ATTR_SURF_TYPE_YUV) && (pixelAlignment == LSB_ALIGNED)) {
uint16_t *psrc = (uint16_t*)(pBuff[newk] + index);
switch(surfBPC) {
case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 10);
}
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 12);
}
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 14);
}
break;
default:
break;
}
}
}
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
goto done;
}
status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: Failed to put bits\n", __func__);
}
done:
if(pBuff) {
free(pBuff);
}
if (pBuffPitches) {
free(pBuffPitches);
}
if (pBuffer) {
free(pBuffer);
}
if(file) {
fclose(file);
}
return status;
}
NvMediaStatus
ReadImage(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment)
{
NvMediaStatus status;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status == NVMEDIA_STATUS_OK) {
return ReadImageNew(
fileName,
frameNum,
width,
height,
image,
uvOrderFlag,
bytesPerPixel,
pixelAlignment);
} else {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
return status;
}
}
NvMediaStatus
InitImage(
NvMediaImage *image,
uint32_t width,
uint32_t height)
{
uint8_t **pBuff = NULL;
uint32_t *pBuffPitches = NULL;
uint32_t imageSize = 0,surfaceSize = 0;
uint8_t *buffer = NULL;
uint8_t *pBuffer = NULL;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
uint32_t numSurfaces = 1;
uint32_t i;
unsigned int uHeightSurface, uWidthSurface;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
if(!image) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
uHeightSurface = surfaceMap.height;
uWidthSurface = surfaceMap.width;
if(width > uWidthSurface || height > uHeightSurface) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
pBuff = (uint8_t **) calloc(1,sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!pBuff) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffPitches = (uint32_t *) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!pBuffPitches) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
status = GetSurfParams(image->type,
&xScalePtr,
&yScalePtr,
&bytePerPixelPtr,
&numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams failed\n", __func__);
goto done;
}
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaSurfaceFormatGetAttrs failed\n", __func__);
goto done;
}
surfaceSize = 0;
imageSize = 0;
for(i = 0; i < numSurfaces; i++) {
surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
}
buffer = (uint8_t *)calloc(1, surfaceSize);
if(!buffer) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffer = buffer;
memset(buffer,0x00,surfaceSize);
for(i = 0; i < numSurfaces; i++) {
pBuff[i] = buffer;
buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock failed\n", __func__);
goto done;
}
status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImagePutBits failed\n", __func__);
}
done:
if(pBuff) {
free(pBuff);
}
if (pBuffPitches) {
free(pBuffPitches);
}
if (pBuffer) {
free(pBuffer);
}
return status;
}

View File

@@ -0,0 +1,141 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 _NVMEDIA_TEST_IMAGE_UTILS_H_
#define _NVMEDIA_TEST_IMAGE_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "misc_utils.h"
#include "nvmedia_core.h"
#include "nvmedia_surface.h"
#include "nvmedia_image.h"
#include "cmdline.h"
#if (NV_IS_SAFETY == 1)
#include "nvmedia_image_internal.h"
#endif
#define PACK_RGBA(R, G, B, A) (((uint32_t)(A) << 24) | ((uint32_t)(B) << 16) | \
((uint32_t)(G) << 8) | (uint32_t)(R))
#define DEFAULT_ALPHA 0x80
// ReadImage
//
// ReadImage() Read image from file
//
// Arguments:
//
// filename
// (in) Input file name
//
// frameNum
// (in) Frame number to read. Use for stream input files.
//
// width
// (in) Surface width
//
// height
// (in) Surface height
//
// image
// (out) Pointer to pre-allocated output surface
//
// uvOrderFlag
// (in) Flag for UV order. If true - UV; If false - VU;
//
// bytesPerPixel
// (in) Bytes per pixel. Nedded for RAW image types handling.
// RAW8 - 1 byte per pixel
// RAW10, RAW12, RAW14 - 2 bytes per pixel
//
// pixelAlignment
// (in) Alignment of bits in pixel.
// 0 - LSB Aligned
// 1 - MSB Aligned
NvMediaStatus
ReadImage(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment);
// InitImage
//
// InitImage() Init image data to zeros
//
// Arguments:
//
// image
// (in) image to initialize
//
// width
// (in) Surface width
//
// height
// (in) Surface height
NvMediaStatus
InitImage(
NvMediaImage *image,
uint32_t width,
uint32_t height);
NvMediaStatus
AllocateBufferToWriteImage(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag);
// WriteImageToBuffer
//
// WriteImageToBuffer() Save RGB or YUV image
//
NvMediaStatus
WriteImageToAllocatedBuffer(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag,
uint32_t bytesPerPixel);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_IMAGE_UTILS_H_ */

View File

@@ -0,0 +1,154 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 <stdio.h>
#include <string.h>
#ifdef NVMEDIA_ANDROID
#define LOG_TAG "nvmedia_common"
#define LOG_NDEBUG 1
#include <utils/Log.h>
#endif
#ifdef NVMEDIA_QNX
#include <sys/slog.h>
#endif
#include "log_utils.h"
#ifdef NVMEDIA_QNX
#define NV_SLOGCODE 0xAAAA
#endif
#define MAX_STATS_LEN 500
#define LOG_BUFFER_BYTES 1024
static enum LogLevel msg_level = LEVEL_ERR;
static enum LogStyle msg_style = LOG_STYLE_NORMAL;
static FILE *msg_file = NULL;
void SetLogLevel(enum LogLevel level)
{
if (level > LEVEL_DBG)
return;
msg_level = level;
}
void SetLogStyle(enum LogStyle style)
{
if (style > LOG_STYLE_FUNCTION_LINE)
return;
msg_style = style;
}
void SetLogFile(FILE *logFileHandle)
{
if(!logFileHandle)
return;
msg_file = logFileHandle;
}
void LogLevelMessage(enum LogLevel level, const char *functionName,
int lineNumber, const char *format, ...)
{
va_list ap;
char str[LOG_BUFFER_BYTES] = {'\0',};
FILE *logFile = msg_file ? msg_file : stdout;
if (level > msg_level)
return;
#ifndef NVMEDIA_ANDROID
/** In the case of Android ADB log, if LOG_TAG is defined,
* before 'Log.h' is included in source file,
* LOG_TAG is automatically concatenated at the beginning of log message,
* so, we don't copy 'nvmedia: ' into 'str'.
*/
strcpy(str, "nvmedia: ");
/** As LOG_TAG is concatednated, log level is also automatically concatenated,
* by calling different ADB log function such as ALOGE(for eror log message),
* ALOGW(for warning log message).
*/
switch (level) {
case LEVEL_ERR:
strcat(str, "ERROR: ");
break;
case LEVEL_WARN:
strcat(str, "WARNING: ");
break;
case LEVEL_INFO:
case LEVEL_DBG:
// Empty
break;
}
#endif
va_start(ap, format);
vsnprintf(str + strlen(str), sizeof(str) - strlen(str), format, ap);
if(msg_style == LOG_STYLE_NORMAL) {
// Add trailing new line char
if(strlen(str) && str[strlen(str) - 1] != '\n')
strcat(str, "\n");
} else if(msg_style == LOG_STYLE_FUNCTION_LINE) {
// Remove trailing new line char
if(strlen(str) && str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = 0;
// Add function and line info
snprintf(str + + strlen(str), sizeof(str) - strlen(str), " at %s():%d\n", functionName, lineNumber);
}
#ifdef NVMEDIA_ANDROID
switch (msg_level) {
case LEVEL_ERR:
ALOGE("%s", str);
break;
case LEVEL_WARN:
ALOGW("%s", str);
break;
case LEVEL_INFO:
ALOGI("%s", str);
break;
case LEVEL_DBG:
ALOGD("%s", str);
break;
}
#else
fprintf(logFile, "%s", str);
#endif
#ifdef NVMEDIA_QNX
/* send to system logger */
slogf(_SLOG_SETCODE(NV_SLOGCODE, 0), _SLOG_ERROR, str);
#endif
va_end(ap);
}

View File

@@ -0,0 +1,121 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 _NVMEDIA_TEST_LOG_UTILS_H_
#define _NVMEDIA_TEST_LOG_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <stdio.h>
enum LogLevel {
LEVEL_ERR = 0,
LEVEL_WARN = 1,
LEVEL_INFO = 2,
LEVEL_DBG = 3,
};
enum LogStyle {
LOG_STYLE_NORMAL = 0,
LOG_STYLE_FUNCTION_LINE
};
#define LINE_INFO __FUNCTION__, __LINE__
#define LOG_DBG(...) LogLevelMessage(LEVEL_DBG, LINE_INFO, __VA_ARGS__)
#define LOG_INFO(...) LogLevelMessage(LEVEL_INFO, LINE_INFO, __VA_ARGS__)
#define LOG_WARN(...) LogLevelMessage(LEVEL_WARN, LINE_INFO, __VA_ARGS__)
// SetLogLevel
//
// SetLogLevel() Set logging level
//
// Arguments:
//
// level
// (in) Logging level
void
SetLogLevel(
enum LogLevel level);
// SetLogStyle
//
// SetLogStyle() Set logging print slyle
//
// Arguments:
//
// level
// (in) Logging style
void
SetLogStyle(
enum LogStyle style);
// SetLogFile
//
// SetLogFile() Set logging file handle
//
// Arguments:
//
// level
// (in) Logging file handle
void
SetLogFile(
FILE *logFileHandle);
// LogLevelMessage
//
// LogLevelMessage() Print message if logging level is higher than message level
//
// Arguments:
//
// LogLevel
// (in) Message level
//
// format
// (in) Message format
//
// ...
// (in) Parameters list
void
LogLevelMessage(
enum LogLevel level,
const char *functionName,
int lineNumber,
const char *format,
...);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_LOG_UTILS_H_ */

View File

@@ -0,0 +1,67 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#if defined (__QNX__)
#include <sys/time.h>
#endif
#include "misc_utils.h"
uint32_t
u32(const uint8_t* ptr)
{
return ptr[0] | (ptr[1]<<8) | (ptr[2]<<16) | (ptr[3]<<24);
}
NvMediaStatus
GetTimeMicroSec(
uint64_t *uTime)
{
struct timespec t;
#if !(defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 && _POSIX_TIMERS > 0)
struct timeval tv;
#endif
if(!uTime)
return NVMEDIA_STATUS_BAD_PARAMETER;
#if !(defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 && _POSIX_TIMERS > 0)
gettimeofday(&tv, NULL);
t.tv_sec = tv.tv_sec;
t.tv_nsec = tv.tv_usec*1000L;
#else
clock_gettime(CLOCK_MONOTONIC, &t);
#endif
*uTime = (uint64_t)t.tv_sec * 1000000LL + (uint64_t)t.tv_nsec / 1000LL;
return NVMEDIA_STATUS_OK;
}

View File

@@ -0,0 +1,77 @@
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
* CONTRIBUTORS 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 _NVMEDIA_TEST_MISC_UTILS_H_
#define _NVMEDIA_TEST_MISC_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "nvmedia_core.h"
#include "nvmedia_common.h"
#ifndef __INTEGRITY
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
typedef enum {
LSB_ALIGNED,
MSB_ALIGNED
} PixelAlignment;
// u32
//
// u32() Reads 4 bytes from buffer and returns the read value
//
// Arguments:
//
// ptr
// (in) Input buffer
uint32_t u32(const uint8_t* ptr);
// GetTimeMicroSec
//
// GetTimeMicroSec() Returns current time in microseconds
//
// Arguments:
//
// uTime
// (out) Pointer to current time in microseconds
NvMediaStatus
GetTimeMicroSec(
uint64_t *uTime);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_MISC_UTILS_H_ */