Debian Codegraph @

Samples/4_CUDA_Libraries/cudaNvSciNvMedia/nvmedia_utils/image_utils.cpp

raw
1 /* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of NVIDIA CORPORATION nor the names of its
12 * contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "image_utils.h"
35 #include "misc_utils.h"
36 #include "nvmedia_surface.h"
37
38 #define MAXM_NUM_SURFACES 6
39
40 typedef struct {
41 float heightFactor[6];
42 float widthFactor[6];
43 unsigned int numSurfaces;
44 } ImgUtilSurfParams;
45
46 ImgUtilSurfParams ImgSurfParamsTable_RGBA = {
47 .heightFactor = {1, 0, 0, 0, 0, 0},
48 .widthFactor = {1, 0, 0, 0, 0, 0},
49 .numSurfaces = 1,
50 };
51
52 ImgUtilSurfParams ImgSurfParamsTable_RAW = {
53 .heightFactor = {1, 0, 0, 0, 0, 0},
54 .widthFactor = {1, 0, 0, 0, 0, 0},
55 .numSurfaces = 1,
56 };
57
58 ImgUtilSurfParams ImgSurfParamsTable_YUV[][4] = {
59 { /* PLANAR */
60 { /* 420 */
61 .heightFactor = {1, 0.5, 0.5, 0, 0, 0},
62 .widthFactor = {1, 0.5, 0.5, 0, 0, 0},
63 .numSurfaces = 3,
64 },
65 { /* 422 */
66 .heightFactor = {1, 1, 1, 0, 0, 0},
67 .widthFactor = {1, 0.5, 0.5, 0, 0, 0},
68 .numSurfaces = 3,
69 },
70 { /* 444 */
71 .heightFactor = {1, 1, 1, 0, 0, 0},
72 .widthFactor = {1, 1, 1, 0, 0, 0},
73 .numSurfaces = 3,
74 },
75 { /* 422R */
76 .heightFactor = {1, 0.5, 0.5, 0, 0, 0},
77 .widthFactor = {1, 1, 1, 0, 0, 0},
78 .numSurfaces = 3,
79 },
80 },
81 { /* SEMI_PLANAR */
82 { /* 420 */
83 .heightFactor = {1, 0.5, 0, 0, 0, 0},
84 .widthFactor = {1, 0.5, 0, 0, 0, 0},
85 .numSurfaces = 2,
86 },
87 { /* 422 */
88 .heightFactor = {1, 1, 0, 0, 0, 0},
89 .widthFactor = {1, 0.5, 0, 0, 0, 0},
90 .numSurfaces = 2,
91 },
92 { /* 444 */
93 .heightFactor = {1, 1, 0.5, 0, 0, 0},
94 .widthFactor = {1, 1, 0.5, 0, 0, 0},
95 .numSurfaces = 2,
96 },
97 { /* 422R */
98 .heightFactor = {1, 0.5, 0.5, 0, 0, 0},
99 .widthFactor = {1, 1, 0.5, 0, 0, 0},
100 .numSurfaces = 2,
101 },
102 },
103 { /* PACKED */
104 { /* 420 */
105 .heightFactor = {1, 0.5, 0.5, 0, 0, 0},
106 .widthFactor = {1, 0.5, 0.5, 0, 0, 0},
107 .numSurfaces = 3,
108 },
109 { /* 422 */
110 .heightFactor = {1, 1, 1, 0, 0, 0},
111 .widthFactor = {1, 0.5, 0.5, 0, 0, 0},
112 .numSurfaces = 3,
113 },
114 { /* 444 */
115 .heightFactor = {1, 1, 1, 0, 0, 0},
116 .widthFactor = {1, 1, 1, 0, 0, 0},
117 .numSurfaces = 3,
118 },
119 { /* 422R */
120 .heightFactor = {1, 0.5, 0.5, 0, 0, 0},
121 .widthFactor = {1, 1, 1, 0, 0, 0},
122 .numSurfaces = 3,
123 },
124 },
125 };
126
127 ImgUtilSurfParams ImgSurfParamsTable_Packed = {
128 .heightFactor = {1, 0, 0, 0, 0, 0},
129 .widthFactor = {1, 0, 0, 0, 0, 0},
130 .numSurfaces = 1,
131 };
132
133
134 unsigned int ImgBytesPerPixelTable_RGBA[][6] = {
135 {4, 0, 0, 0, 0, 0}, /* 8 */
136 };
137
138 unsigned int ImgBytesPerPixelTable_RGBA16[][6] = {
139 {8, 0, 0, 0, 0, 0}, /* 16 */
140 };
141
142 unsigned int ImgBytesPerPixelTable_RG16[6] =
143 {4, 0, 0, 0, 0, 0};
144
145 unsigned int ImgBytesPerPixelTable_Alpha[][6] = {
146 {1, 0, 0, 0, 0, 0}, /* 8 */
147 {2, 0, 0, 0, 0, 0}, /* 10 */
148 {2, 0, 0, 0, 0, 0}, /* 12 */
149 {2, 0, 0, 0, 0, 0}, /* 14 */
150 {2, 0, 0, 0, 0, 0}, /* 16 */
151 {4, 0, 0, 0, 0, 0}, /* 32 */
152 };
153
154 unsigned int ImgBytesPerPixelTable_RAW[][6] = {
155 {1, 0, 0, 0, 0, 0}, /* 8 */
156 {2, 0, 0, 0, 0, 0}, /* 10 */
157 {2, 0, 0, 0, 0, 0}, /* 12 */
158 {2, 0, 0, 0, 0, 0}, /* 14 */
159 {2, 0, 0, 0, 0, 0}, /* 16 */
160 {4, 0, 0, 0, 0, 0}, /* 32 */
161 {4, 0, 0, 0, 0, 0}, /* 16_8_8 */
162 {4, 0, 0, 0, 0, 0}, /* 10_8_8 */
163 {4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
164 {4, 0, 0, 0, 0, 0}, /* 20 */
165 };
166
167 unsigned int ImgBytesPerPixelTable_YUV[][9][6] = {
168 { /* PLANAR */
169 {1, 1, 1, 0, 0, 0}, /* 8 */
170 {2, 2, 2, 0, 0, 0}, /* 10 */
171 {2, 2, 2, 0, 0, 0}, /* 12 */
172 {2, 2, 2, 0, 0, 0}, /* 14 */
173 {2, 2, 2, 0, 0, 0}, /* 16 */
174 {4, 4, 4, 0, 0, 0}, /* 32 */
175 {2, 1, 1, 0, 0, 0}, /* 16_8_8 */
176 {2, 1, 1, 0, 0, 0}, /* 10_8_8 */
177 {4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
178 },
179 { /* SEMI_PLANAR */
180 {1, 2, 0, 0, 0, 0}, /* 8 */
181 {2, 4, 0, 0, 0, 0}, /* 10 */
182 {2, 4, 0, 0, 0, 0}, /* 12 */
183 {2, 4, 0, 0, 0, 0}, /* 14 */
184 {2, 4, 0, 0, 0, 0}, /* 16 */
185 {4, 8, 0, 0, 0, 0}, /* 32 */
186 {2, 2, 0, 0, 0, 0}, /* 16_8_8 */
187 {2, 2, 0, 0, 0, 0}, /* 10_8_8 */
188 {4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
189 }
190 };
191
192 static NvMediaStatus
193 GetBytesPerCompForPackedYUV(unsigned int surfBPCidx,
194 unsigned int *bytespercomp
195 )
196 {
197 switch(surfBPCidx) {
198 case NVM_SURF_ATTR_BITS_PER_COMPONENT_8:
199 case NVM_SURF_ATTR_BITS_PER_COMPONENT_LAYOUT_2_10_10_10:
200 *bytespercomp = 1;
201 break;
202 case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
203 case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
204 case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
205 case NVM_SURF_ATTR_BITS_PER_COMPONENT_16:
206 *bytespercomp = 2;
207 break;
208 case NVM_SURF_ATTR_BITS_PER_COMPONENT_20:
209 *bytespercomp = 3;
210 break;
211 case NVM_SURF_ATTR_BITS_PER_COMPONENT_32:
212 *bytespercomp = 4;
213 break;
214 default:
215 return NVMEDIA_STATUS_ERROR;
216 }
217 return NVMEDIA_STATUS_OK;
218
219 }
220
221 static NvMediaStatus
222 GetSurfParams(unsigned int surfaceType,
223 float **xScale,
224 float **yScale,
225 unsigned int **bytePerPixel,
226 uint32_t *numSurfacesVal)
227 {
228 NvMediaStatus status;
229 unsigned int surfType, surfMemoryType, surfSubSamplingType, surfBPC, surfCompOrder;
230 float *xScalePtr = NULL, *yScalePtr = NULL;
231 unsigned int *bytePerPixelPtr = NULL;
232 NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
233 uint32_t numSurfaces = 1;
234 static unsigned int yuvpackedtbl[6] = {1, 0, 0, 0, 0, 0};
235 unsigned int numcomps = 1;
236
237 status = NvMediaSurfaceFormatGetAttrs(surfaceType,
238 srcAttr,
239 NVM_SURF_FMT_ATTR_MAX);
240 if (status != NVMEDIA_STATUS_OK) {
241 printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
242 return NVMEDIA_STATUS_ERROR;
243 }
244
245 surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
246 surfMemoryType = srcAttr[NVM_SURF_ATTR_MEMORY].value;
247 surfSubSamplingType = srcAttr[NVM_SURF_ATTR_SUB_SAMPLING_TYPE].value;
248 surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
249 surfCompOrder = srcAttr[NVM_SURF_ATTR_COMPONENT_ORDER].value;
250
251 switch(surfType) {
252 case NVM_SURF_ATTR_SURF_TYPE_YUV:
253 if (surfSubSamplingType == NVM_SURF_ATTR_SUB_SAMPLING_TYPE_NONE &&
254 surfMemoryType == NVM_SURF_ATTR_MEMORY_PACKED) {
255
256 xScalePtr = &ImgSurfParamsTable_Packed.widthFactor[0];
257 yScalePtr = &ImgSurfParamsTable_Packed.heightFactor[0];
258 numSurfaces = ImgSurfParamsTable_Packed.numSurfaces;
259
260 if (NVMEDIA_STATUS_OK != GetBytesPerCompForPackedYUV(surfBPC, &yuvpackedtbl[0])) {
261 printf("Invalid Bits per component and Packed YUV combination\n");
262 return NVMEDIA_STATUS_ERROR;
263 }
264
265 switch(surfCompOrder) {
266 case NVM_SURF_ATTR_COMPONENT_ORDER_VUYX:
267 case NVM_SURF_ATTR_COMPONENT_ORDER_XYUV:
268 case NVM_SURF_ATTR_COMPONENT_ORDER_XUYV:
269 numcomps = 4;
270 break;
271 case NVM_SURF_ATTR_COMPONENT_ORDER_UYVY:
272 case NVM_SURF_ATTR_COMPONENT_ORDER_VYUY:
273 case NVM_SURF_ATTR_COMPONENT_ORDER_YVYU:
274 case NVM_SURF_ATTR_COMPONENT_ORDER_YUYV:
275 numcomps = 2;
276 break;
277 case NVM_SURF_ATTR_COMPONENT_ORDER_LUMA:
278 numcomps = 1;
279 break;
280 default:
281 printf("Invalid component Order and Packed YUV combination\n");
282 return NVMEDIA_STATUS_ERROR;
283 }
284 yuvpackedtbl[0] = yuvpackedtbl[0] * numcomps;
285 bytePerPixelPtr = &yuvpackedtbl[0];
286
287 } else {
288 xScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].widthFactor[0];
289 yScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].heightFactor[0];
290 numSurfaces = ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].numSurfaces;
291 bytePerPixelPtr = &ImgBytesPerPixelTable_YUV[0][surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
292 }
293
294 break;
295 case NVM_SURF_ATTR_SURF_TYPE_RGBA:
296 if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_ALPHA) {
297 bytePerPixelPtr = &ImgBytesPerPixelTable_Alpha[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
298 } else if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_RG) {
299 if(surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
300 bytePerPixelPtr = &ImgBytesPerPixelTable_RG16[0];
301 } else {
302 printf("Invalid RGorder & Bitspercomp combination.Only RG16 is supported\n");
303 return NVMEDIA_STATUS_ERROR;
304 }
305 } else { /* RGBA, ARGB, BGRA */
306 if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
307 bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA16[0][0];
308 } else if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_8) {
309 bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA[0][0];
310 } else {
311 printf("RGBA orders with 8 and 16bits only is supported \n");
312 return NVMEDIA_STATUS_ERROR;
313 }
314 }
315 xScalePtr = &ImgSurfParamsTable_RGBA.widthFactor[0];
316 yScalePtr = &ImgSurfParamsTable_RGBA.heightFactor[0];
317 numSurfaces = ImgSurfParamsTable_RGBA.numSurfaces;
318 break;
319 case NVM_SURF_ATTR_SURF_TYPE_RAW:
320 bytePerPixelPtr = &ImgBytesPerPixelTable_RAW[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
321 xScalePtr = &ImgSurfParamsTable_RAW.widthFactor[0];
322 yScalePtr = &ImgSurfParamsTable_RAW.heightFactor[0];
323 numSurfaces = ImgSurfParamsTable_RAW.numSurfaces;
324 break;
325 default:
326 printf("%s: Unsupported Pixel Format %d", __func__, surfType);
327 return NVMEDIA_STATUS_ERROR;
328 }
329
330 if (xScale) {
331 *xScale = xScalePtr;
332 }
333 if (yScale) {
334 *yScale = yScalePtr;
335 }
336 if (bytePerPixel) {
337 *bytePerPixel = bytePerPixelPtr;
338 }
339 if (numSurfacesVal) {
340 *numSurfacesVal = numSurfaces;
341 }
342
343 return NVMEDIA_STATUS_OK;
344 }
345
346 NvMediaStatus
347 AllocateBufferToWriteImage(
348 Blit2DTest *ctx,
349 NvMediaImage *image,
350 NvMediaBool uvOrderFlag,
351 NvMediaBool appendFlag)
352 {
353 uint32_t imageSize = 0;
354 unsigned int size[3] ={0};
355 uint8_t *buffer = NULL;
356 uint32_t i, k, newk = 0;
357 unsigned int *bytePerPixelPtr = NULL;
358 ctx->numSurfaces = 1;
359 NvMediaImageSurfaceMap surfaceMap;
360 NvMediaStatus status = NVMEDIA_STATUS_ERROR;
361 uint32_t lineWidth, numRows, startOffset;
362
363 if(!image) {
364 printf("%s: Bad parameter\n", __func__);
365 return NVMEDIA_STATUS_BAD_PARAMETER;
366 }
367
368 status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
369 if(status != NVMEDIA_STATUS_OK) {
370 printf("%s: NvMediaImageLock() failed\n", __func__);
371 return status;
372 }
373 NvMediaImageUnlock(image);
374
375 ctx->dstBuff = (uint8_t**) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
376 if(!ctx->dstBuff) {
377 printf("%s: Out of memory\n", __func__);
378 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
379 goto done;
380 }
381
382 ctx->dstBuffPitches = (uint32_t*) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
383 if(!ctx->dstBuffPitches) {
384 printf("%s: Out of memory\n", __func__);
385 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
386 goto done;
387 }
388
389 ctx->heightSurface = surfaceMap.height;
390 ctx->widthSurface = surfaceMap.width;
391
392 status = GetSurfParams(image->type,
393 &ctx->xScalePtr,
394 &ctx->yScalePtr,
395 &bytePerPixelPtr,
396 &ctx->numSurfaces);
397 if(status != NVMEDIA_STATUS_OK) {
398 printf("%s: GetSurfParams() failed\n", __func__);
399 goto done;
400 }
401
402 imageSize = 0;
403 for(i = 0; i < ctx->numSurfaces; i++) {
404 size[i] = (ctx->widthSurface * ctx->xScalePtr[i] * ctx->heightSurface * ctx->yScalePtr[i] * bytePerPixelPtr[i]);
405 imageSize += size[i];
406 ctx->dstBuffPitches[i] = (uint32_t)((float)ctx->widthSurface * ctx->xScalePtr[i]) * bytePerPixelPtr[i];
407 }
408
409 // Embedded data size needs to be included for RAW surftype
410 size[0] += image->embeddedDataTopSize;
411 size[0] += image->embeddedDataBottomSize;
412 imageSize += image->embeddedDataTopSize;
413 imageSize += image->embeddedDataBottomSize;
414
415 buffer = (uint8_t *) calloc(1, imageSize);
416 if(!buffer) {
417 printf("%s: Out of memory\n", __func__);
418 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
419 goto done;
420 }
421
422 ctx->dstBuffer = buffer;
423 memset(buffer, 0xFF, imageSize);
424 for(i = 0; i < ctx->numSurfaces; i++) {
425 ctx->dstBuff[i] = buffer;
426 buffer = buffer + (uint32_t)(ctx->heightSurface * ctx->yScalePtr[i] * ctx->dstBuffPitches[i]);
427 }
428
429 done:
430 return status;
431 }
432
433 NvMediaStatus
434 WriteImageToAllocatedBuffer(
435 Blit2DTest *ctx,
436 NvMediaImage *image,
437 NvMediaBool uvOrderFlag,
438 NvMediaBool appendFlag,
439 uint32_t bytesPerPixel)
440 {
441 NvMediaImageSurfaceMap surfaceMap;
442
443 NvMediaStatus status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
444 if(status != NVMEDIA_STATUS_OK) {
445 printf("%s: NvMediaImageLock() failed\n", __func__);
446 goto done;
447 }
448 status = NvMediaImageGetBits(image, NULL, (void **)ctx->dstBuff, ctx->dstBuffPitches);
449 NvMediaImageUnlock(image);
450 if(status != NVMEDIA_STATUS_OK) {
451 printf("%s: NvMediaVideoSurfaceGetBits() failed \n", __func__);
452 goto done;
453 }
454
455 done:
456
457 return status;
458 }
459
460
461 static NvMediaStatus
462 ReadImageNew(
463 char *fileName,
464 uint32_t frameNum,
465 uint32_t width,
466 uint32_t height,
467 NvMediaImage *image,
468 NvMediaBool uvOrderFlag,
469 uint32_t bytesPerPixel,
470 uint32_t pixelAlignment)
471 {
472 uint8_t **pBuff = NULL;
473 uint32_t *pBuffPitches = NULL;
474 uint32_t imageSize = 0,surfaceSize = 0;
475 uint8_t *buffer = NULL;
476 uint8_t *pBuffer = NULL;
477 uint32_t i, j, k, newk = 0;
478 float *xScalePtr = NULL, *yScalePtr = NULL;
479 unsigned int *bytePerPixelPtr = NULL;
480 uint32_t numSurfaces = 1;
481 unsigned int uHeightSurface, uWidthSurface;
482 NvMediaImageSurfaceMap surfaceMap;
483 NvMediaStatus status = NVMEDIA_STATUS_ERROR;
484 FILE *file = NULL;
485 unsigned int count, index;
486 NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
487 unsigned int surfType, surfBPC;
488
489 if(!image || !fileName) {
490 printf("%s: Bad parameter\n", __func__);
491 return NVMEDIA_STATUS_BAD_PARAMETER;
492 }
493
494 status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
495 if(status != NVMEDIA_STATUS_OK) {
496 printf("%s: NvMediaImageLock() failed\n", __func__);
497 return status;
498 }
499 NvMediaImageUnlock(image);
500
501 uHeightSurface = surfaceMap.height;
502 uWidthSurface = surfaceMap.width;
503
504 if(width > uWidthSurface || height > uHeightSurface) {
505 printf("%s: Bad parameter\n", __func__);
506 return NVMEDIA_STATUS_BAD_PARAMETER;
507 }
508
509 pBuff = (uint8_t **) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
510 if(!pBuff) {
511 printf("%s: Out of memory\n", __func__);
512 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
513 goto done;
514 }
515
516 pBuffPitches = (uint32_t *)calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
517 if(!pBuffPitches) {
518 printf("%s: Out of memory\n", __func__);
519 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
520 goto done;
521 }
522
523 status = GetSurfParams(image->type,
524 &xScalePtr,
525 &yScalePtr,
526 &bytePerPixelPtr,
527 &numSurfaces);
528 if(status != NVMEDIA_STATUS_OK) {
529 printf("%s: GetSurfParams() failed\n", __func__);
530 goto done;
531 }
532
533 status = NvMediaSurfaceFormatGetAttrs(image->type,
534 srcAttr,
535 NVM_SURF_FMT_ATTR_MAX);
536 if (status != NVMEDIA_STATUS_OK) {
537 printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
538 goto done;
539 }
540 surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
541 surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
542
543 surfaceSize = 0;
544 imageSize = 0;
545 for(i = 0; i < numSurfaces; i++) {
546 surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
547 imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
548 pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
549 }
550
551 buffer = (uint8_t *)calloc(1, surfaceSize);
552 if(!buffer) {
553 printf("%s: Out of memory\n", __func__);
554 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
555 goto done;
556 }
557
558 pBuffer = buffer;
559 memset(buffer,0x10,surfaceSize);
560 for(i = 0; i < numSurfaces; i++) {
561 pBuff[i] = buffer;
562 if (i) {
563 memset(pBuff[i], 0x80, (uHeightSurface * yScalePtr[i] * pBuffPitches[i]));
564 }
565 buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
566 }
567
568 file = fopen(fileName, "rb");
569 if(!file) {
570 printf("%s: Error opening file: %s\n", __func__, fileName);
571 status = NVMEDIA_STATUS_ERROR;
572 goto done;
573 }
574
575 if(frameNum > 0) {
576 if(fseeko(file, frameNum * (off_t)imageSize, SEEK_SET)) {
577 printf("ReadImage: Error seeking file: %s\n", fileName);
578 status = NVMEDIA_STATUS_ERROR;
579 goto done;
580 }
581 }
582
583 if((surfType == NVM_SURF_ATTR_SURF_TYPE_RGBA ) && strstr(fileName, ".png")) {
584 printf("ReadImage: Does not support png format\n");
585 status = NVMEDIA_STATUS_ERROR;
586 goto done;
587 }
588 for(k = 0; k < numSurfaces; k++) {
589 for(j = 0; j < height*yScalePtr[k]; j++) {
590 newk = (!uvOrderFlag && k ) ? (numSurfaces - k) : k;
591 index = j * pBuffPitches[newk];
592 count = width * xScalePtr[newk] * bytePerPixelPtr[newk];
593 if (fread(pBuff[newk] + index, count, 1, file) != 1) {
594 status = NVMEDIA_STATUS_ERROR;
595 printf("ReadImage: Error reading file: %s\n", fileName);
596 goto done;
597 }
598 if((surfType == NVM_SURF_ATTR_SURF_TYPE_YUV) && (pixelAlignment == LSB_ALIGNED)) {
599 uint16_t *psrc = (uint16_t*)(pBuff[newk] + index);
600 switch(surfBPC) {
601 case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
602 for(i = 0; i < count/2; i++) {
603 *(psrc + i) = (*(psrc + i)) << (16 - 10);
604 }
605 break;
606 case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
607 for(i = 0; i < count/2; i++) {
608 *(psrc + i) = (*(psrc + i)) << (16 - 12);
609 }
610 break;
611 case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
612 for(i = 0; i < count/2; i++) {
613 *(psrc + i) = (*(psrc + i)) << (16 - 14);
614 }
615 break;
616 default:
617 break;
618 }
619 }
620 }
621 }
622
623 status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
624 if(status != NVMEDIA_STATUS_OK) {
625 printf("%s: NvMediaImageLock() failed\n", __func__);
626 goto done;
627 }
628 status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
629 NvMediaImageUnlock(image);
630 if(status != NVMEDIA_STATUS_OK) {
631 printf("%s: Failed to put bits\n", __func__);
632 }
633
634 done:
635 if(pBuff) {
636 free(pBuff);
637 }
638
639 if (pBuffPitches) {
640 free(pBuffPitches);
641 }
642
643 if (pBuffer) {
644 free(pBuffer);
645 }
646
647 if(file) {
648 fclose(file);
649 }
650
651 return status;
652 }
653
654 NvMediaStatus
655 ReadImage(
656 char *fileName,
657 uint32_t frameNum,
658 uint32_t width,
659 uint32_t height,
660 NvMediaImage *image,
661 NvMediaBool uvOrderFlag,
662 uint32_t bytesPerPixel,
663 uint32_t pixelAlignment)
664 {
665 NvMediaStatus status;
666 NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
667
668 status = NvMediaSurfaceFormatGetAttrs(image->type,
669 srcAttr,
670 NVM_SURF_FMT_ATTR_MAX);
671 if (status == NVMEDIA_STATUS_OK) {
672 return ReadImageNew(
673 fileName,
674 frameNum,
675 width,
676 height,
677 image,
678 uvOrderFlag,
679 bytesPerPixel,
680 pixelAlignment);
681 } else {
682 printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
683 return status;
684 }
685 }
686
687 NvMediaStatus
688 InitImage(
689 NvMediaImage *image,
690 uint32_t width,
691 uint32_t height)
692 {
693 uint8_t **pBuff = NULL;
694 uint32_t *pBuffPitches = NULL;
695 uint32_t imageSize = 0,surfaceSize = 0;
696 uint8_t *buffer = NULL;
697 uint8_t *pBuffer = NULL;
698 float *xScalePtr = NULL, *yScalePtr = NULL;
699 unsigned int *bytePerPixelPtr = NULL;
700 uint32_t numSurfaces = 1;
701 uint32_t i;
702 unsigned int uHeightSurface, uWidthSurface;
703 NvMediaImageSurfaceMap surfaceMap;
704 NvMediaStatus status = NVMEDIA_STATUS_ERROR;
705 NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
706
707 if(!image) {
708 printf("%s: Bad parameter\n", __func__);
709 return NVMEDIA_STATUS_BAD_PARAMETER;
710 }
711
712 status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
713 if(status != NVMEDIA_STATUS_OK) {
714 printf("%s: NvMediaImageLock failed\n", __func__);
715 return status;
716 }
717 NvMediaImageUnlock(image);
718
719
720 uHeightSurface = surfaceMap.height;
721 uWidthSurface = surfaceMap.width;
722
723 if(width > uWidthSurface || height > uHeightSurface) {
724 printf("%s: Bad parameter\n", __func__);
725 return NVMEDIA_STATUS_BAD_PARAMETER;
726 }
727
728 pBuff = (uint8_t **) calloc(1,sizeof(uint8_t*)*MAXM_NUM_SURFACES);
729 if(!pBuff) {
730 printf("%s: out of memory\n", __func__);
731 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
732 goto done;
733 }
734
735 pBuffPitches = (uint32_t *) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
736 if(!pBuffPitches) {
737 printf("%s: out of memory\n", __func__);
738 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
739 goto done;
740 }
741
742 status = GetSurfParams(image->type,
743 &xScalePtr,
744 &yScalePtr,
745 &bytePerPixelPtr,
746 &numSurfaces);
747 if(status != NVMEDIA_STATUS_OK) {
748 printf("%s: GetSurfParams failed\n", __func__);
749 goto done;
750 }
751
752 status = NvMediaSurfaceFormatGetAttrs(image->type,
753 srcAttr,
754 NVM_SURF_FMT_ATTR_MAX);
755 if (status != NVMEDIA_STATUS_OK) {
756 printf("%s: NvMediaSurfaceFormatGetAttrs failed\n", __func__);
757 goto done;
758 }
759
760 surfaceSize = 0;
761 imageSize = 0;
762 for(i = 0; i < numSurfaces; i++) {
763 surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
764 imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
765 pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
766 }
767
768 buffer = (uint8_t *)calloc(1, surfaceSize);
769 if(!buffer) {
770 printf("%s: out of memory\n", __func__);
771 status = NVMEDIA_STATUS_OUT_OF_MEMORY;
772 goto done;
773 }
774
775 pBuffer = buffer;
776 memset(buffer,0x00,surfaceSize);
777 for(i = 0; i < numSurfaces; i++) {
778 pBuff[i] = buffer;
779 buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
780 }
781
782 status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
783 if(status != NVMEDIA_STATUS_OK) {
784 printf("%s: NvMediaImageLock failed\n", __func__);
785 goto done;
786 }
787 status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
788 NvMediaImageUnlock(image);
789 if(status != NVMEDIA_STATUS_OK) {
790 printf("%s: NvMediaImagePutBits failed\n", __func__);
791 }
792
793 done:
794 if(pBuff) {
795 free(pBuff);
796 }
797
798 if (pBuffPitches) {
799 free(pBuffPitches);
800 }
801
802 if (pBuffer) {
803 free(pBuffer);
804 }
805
806 return status;
807 }
808