main_helper.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. //--=========================================================================--
  3. // This file is a part of VPU Reference API project
  4. //-----------------------------------------------------------------------------
  5. //
  6. // This confidential and proprietary software may be used only
  7. // as authorized by a licensing agreement from Chips&Media Inc.
  8. // In the event of publication, the following notice is applicable:
  9. //
  10. // (C) COPYRIGHT 2006 - 2014 CHIPS&MEDIA INC.
  11. // ALL RIGHTS RESERVED
  12. //
  13. // The entire notice above must be reproduced on all authorized
  14. // copies.
  15. //
  16. //--=========================================================================--
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <errno.h>
  22. #include "vpuapifunc.h"
  23. #include "coda9/coda9_regdefine.h"
  24. #include "wave/common/common_vpuconfig.h"
  25. #include "wave/common/common_regdefine.h"
  26. #include "wave/wave4/wave4_regdefine.h"
  27. #include "wave/wave5/wave5_regdefine.h"
  28. #include "wave/coda7q/coda7q_regdefine.h"
  29. #include "vpuerror.h"
  30. #include "main_helper.h"
  31. #ifdef PLATFORM_LINUX
  32. #include <getopt.h>
  33. #endif
  34. #define BIT_DUMMY_READ_GEN 0x06000000
  35. #define BIT_READ_LATENCY 0x06000004
  36. #define W4_SET_READ_DELAY 0x01000000
  37. #define W4_SET_WRITE_DELAY 0x01000004
  38. #define MAX_CODE_BUF_SIZE (512*1024)
  39. #ifdef PLATFORM_WIN32
  40. #pragma warning(disable : 4996) //!<< disable waring C4996: The POSIX name for this item is deprecated.
  41. #endif
  42. char* EncPicTypeStringH264[] = {
  43. "IDR/I",
  44. "P",
  45. };
  46. char* EncPicTypeStringMPEG4[] = {
  47. "I",
  48. "P",
  49. };
  50. char* productNameList[] = {
  51. "CODA980",
  52. "CODA960",
  53. "CODA7503",
  54. "WAVE320",
  55. "WAVE410",
  56. "WAVE4102",
  57. "WAVE420",
  58. "WAVE412",
  59. "WAVE7Q",
  60. "WAVE420L",
  61. "WAVE510",
  62. "WAVE512",
  63. "WAVE515",
  64. "WAVE520",
  65. "Unknown",
  66. "Unknown",
  67. };
  68. #if defined(PLATFORM_LINUX) || defined(PLATFORM_QNX)
  69. #include <sys/stat.h>
  70. #include <unistd.h>
  71. #endif
  72. Int32 LoadFirmware(
  73. Int32 productId,
  74. Uint8** retFirmware,
  75. Uint32* retSizeInWord,
  76. const char* path
  77. )
  78. {
  79. Int32 nread;
  80. Uint32 totalRead, allocSize, readSize = 1024*1024;
  81. Uint8* firmware = NULL;
  82. osal_file_t fp;
  83. if ((fp=osal_fopen(path, "rb")) == NULL)
  84. {
  85. VLOG(ERR, "Failed to open %s\n", path);
  86. return -1;
  87. }
  88. totalRead = 0;
  89. if (PRODUCT_ID_W_SERIES(productId))
  90. {
  91. firmware = (Uint8*)osal_malloc(readSize);
  92. allocSize = readSize;
  93. nread = 0;
  94. while (TRUE)
  95. {
  96. if (allocSize < (totalRead+readSize))
  97. {
  98. allocSize += 2*nread;
  99. firmware = (Uint8*)realloc(firmware, allocSize);
  100. }
  101. nread = osal_fread((void*)&firmware[totalRead], 1, readSize, fp);
  102. totalRead += nread;
  103. if (nread < (Int32)readSize) break;
  104. }
  105. *retSizeInWord = (totalRead+1)/2;
  106. }
  107. else
  108. {
  109. Uint16* pusBitCode;
  110. pusBitCode = (Uint16 *)osal_malloc(MAX_CODE_BUF_SIZE);
  111. firmware = (Uint8*)pusBitCode;
  112. if (pusBitCode)
  113. {
  114. int code;
  115. while (!osal_feof(fp) || totalRead < (MAX_CODE_BUF_SIZE/2)) {
  116. code = -1;
  117. if (fscanf(fp, "%x", &code) <= 0) {
  118. /* matching failure or EOF */
  119. break;
  120. }
  121. pusBitCode[totalRead++] = (Uint16)code;
  122. }
  123. }
  124. *retSizeInWord = totalRead;
  125. }
  126. osal_fclose(fp);
  127. *retFirmware = firmware;
  128. return 0;
  129. }
  130. void PrintVpuVersionInfo(
  131. Uint32 core_idx
  132. )
  133. {
  134. Uint32 version;
  135. Uint32 revision;
  136. Uint32 productId;
  137. VPU_GetVersionInfo(core_idx, &version, &revision, &productId);
  138. VLOG(INFO, "VPU coreNum : [%d]\n", core_idx);
  139. VLOG(INFO, "Firmware : CustomerCode: %04x | version : %d.%d.%d rev.%d\n",
  140. (Uint32)(version>>16), (Uint32)((version>>(12))&0x0f), (Uint32)((version>>(8))&0x0f), (Uint32)((version)&0xff), revision);
  141. VLOG(INFO, "Hardware : %04x\n", productId);
  142. VLOG(INFO, "API : %d.%d.%d\n\n", API_VERSION_MAJOR, API_VERSION_MINOR, API_VERSION_PATCH);
  143. }
  144. BOOL OpenDisplayBufferFile(char *outputPath, VpuRect rcDisplay, TiledMapType mapType, FILE *fp[])
  145. {
  146. char strFile[MAX_FILE_PATH];
  147. int width;
  148. int height;
  149. width = rcDisplay.right - rcDisplay.left;
  150. height = rcDisplay.bottom - rcDisplay.top;
  151. if (mapType == LINEAR_FRAME_MAP) {
  152. if ((fp[0]=osal_fopen(outputPath, "wb")) == NULL) {
  153. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, outputPath);
  154. goto ERR_OPEN_DISP_BUFFER_FILE;
  155. }
  156. }
  157. else {
  158. width = VPU_ALIGN16(width);
  159. height = VPU_ALIGN16(height);
  160. sprintf(strFile, "%s_%dx%d_fbc_data_y.bin", outputPath, width, height);
  161. if ((fp[0]=osal_fopen(strFile, "wb")) == NULL) {
  162. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, strFile);
  163. goto ERR_OPEN_DISP_BUFFER_FILE;
  164. }
  165. sprintf(strFile, "%s_%dx%d_fbc_data_c.bin", outputPath, width, height);
  166. if ((fp[1]=osal_fopen(strFile, "wb")) == NULL) {
  167. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, strFile);
  168. goto ERR_OPEN_DISP_BUFFER_FILE;
  169. }
  170. sprintf(strFile, "%s_%dx%d_fbc_table_y.bin", outputPath, width, height);
  171. if ((fp[2]=osal_fopen(strFile, "wb")) == NULL) {
  172. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, strFile);
  173. goto ERR_OPEN_DISP_BUFFER_FILE;
  174. }
  175. sprintf(strFile, "%s_%dx%d_fbc_table_c.bin", outputPath, width, height);
  176. if ((fp[3]=osal_fopen(strFile, "wb")) == NULL) {
  177. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, strFile);
  178. goto ERR_OPEN_DISP_BUFFER_FILE;
  179. }
  180. }
  181. return TRUE;
  182. ERR_OPEN_DISP_BUFFER_FILE:
  183. CloseDisplayBufferFile(fp);
  184. return FALSE;
  185. }
  186. void CloseDisplayBufferFile(FILE *fp[])
  187. {
  188. int i;
  189. for (i=0; i < OUTPUT_FP_NUMBER; i++) {
  190. if (fp[i] != NULL) {
  191. osal_fclose(fp[i]);
  192. fp[i] = NULL;
  193. }
  194. }
  195. }
  196. void SaveDisplayBufferToFile(DecHandle handle, FrameBuffer dispFrame, VpuRect rcDisplay, FILE *fp[])
  197. {
  198. Uint32 width;
  199. Uint32 height;
  200. Uint32 bpp;
  201. DecGetFramebufInfo fbInfo;
  202. if (dispFrame.myIndex < 0)
  203. return;
  204. if (dispFrame.mapType != COMPRESSED_FRAME_MAP) {
  205. //VLOG(INFO, "---> DUMP LINEAR(WTL) FRAMEBUFFER #%d\n", dispFrame.myIndex);
  206. size_t sizeYuv;
  207. Uint8* pYuv;
  208. pYuv = GetYUVFromFrameBuffer(handle, &dispFrame, rcDisplay, &width, &height, &bpp, &sizeYuv);
  209. osal_fwrite(pYuv, 1, sizeYuv, fp[0]);
  210. osal_free(pYuv);
  211. }
  212. else {
  213. int lumaTblSize;
  214. int chromaTblSize;
  215. int lSize;
  216. int cSize;
  217. Uint32 addr;
  218. Uint32 fbc_endian;
  219. unsigned char *buf;
  220. int coreIdx = VPU_HANDLE_CORE_INDEX(handle);
  221. //VLOG(INFO, "---> DUMP COMPRESSED FRAMEBUFFER #%d\n", dispFrame.myIndex);
  222. VPU_DecGiveCommand(handle, DEC_GET_FRAMEBUF_INFO, (void*)&fbInfo);
  223. width = rcDisplay.right - rcDisplay.left;
  224. height = rcDisplay.bottom - rcDisplay.top;
  225. width = VPU_ALIGN16(width);
  226. height = VPU_ALIGN16(height);
  227. switch (dispFrame.format) {
  228. case FORMAT_420_P10_16BIT_LSB:
  229. case FORMAT_420_P10_16BIT_MSB:
  230. bpp = 2;
  231. break;
  232. case FORMAT_420_P10_32BIT_LSB:
  233. case FORMAT_420_P10_32BIT_MSB:
  234. bpp = 1;
  235. break;
  236. default:
  237. bpp = 1;
  238. break;
  239. }
  240. fbc_endian = vdi_convert_endian(coreIdx, dispFrame.endian);
  241. lSize = width * height * bpp;
  242. cSize = width * height * bpp / 2;
  243. /* Dump Y compressed data */
  244. buf = osal_malloc(lSize);
  245. if (buf)
  246. {
  247. vdi_read_memory(coreIdx, dispFrame.bufY, buf, lSize, fbc_endian);
  248. osal_fwrite((void *)buf, sizeof(Uint8), lSize, fp[0]);
  249. osal_fflush(fp[0]);
  250. osal_free(buf);
  251. }
  252. /* Dump C compressed data */
  253. buf = osal_malloc(cSize);
  254. if (buf)
  255. {
  256. vdi_read_memory(coreIdx, dispFrame.bufCb, buf, cSize, fbc_endian);
  257. osal_fwrite((void *)buf, sizeof(Uint8), cSize, fp[1]);
  258. osal_fflush(fp[1]);
  259. osal_free(buf);
  260. }
  261. /* Dump Y Offset table */
  262. VPU_GetFBCOffsetTableSize(STD_HEVC, (int)width, (int)height, (int*)&lumaTblSize, (int*)&chromaTblSize);
  263. addr = fbInfo.vbFbcYTbl[dispFrame.myIndex].phys_addr;
  264. buf = osal_malloc(lumaTblSize);
  265. if (buf)
  266. {
  267. vdi_read_memory(coreIdx, addr, buf, lumaTblSize, fbc_endian);
  268. osal_fwrite((void *)buf, sizeof(Uint8), lumaTblSize, fp[2]);
  269. osal_fflush(fp[2]);
  270. osal_free(buf);
  271. }
  272. /* Dump C Offset table */
  273. addr = fbInfo.vbFbcCTbl[dispFrame.myIndex].phys_addr;
  274. buf = osal_malloc(chromaTblSize);
  275. if (buf)
  276. {
  277. vdi_read_memory(coreIdx, addr, buf, chromaTblSize, fbc_endian);
  278. osal_fwrite((void *)buf, sizeof(Uint8), chromaTblSize, fp[3]);
  279. osal_fflush(fp[3]);
  280. osal_free(buf);
  281. }
  282. }
  283. }
  284. void FreePreviousFramebuffer(
  285. Uint32 coreIdx,
  286. DecGetFramebufInfo* fb
  287. )
  288. {
  289. int i;
  290. if (fb->vbFrame.size > 0) {
  291. vdi_free_dma_memory(coreIdx, &fb->vbFrame);
  292. osal_memset((void*)&fb->vbFrame, 0x00, sizeof(vpu_buffer_t));
  293. }
  294. if (fb->vbWTL.size > 0) {
  295. vdi_free_dma_memory(coreIdx, &fb->vbWTL);
  296. osal_memset((void*)&fb->vbWTL, 0x00, sizeof(vpu_buffer_t));
  297. }
  298. for ( i=0 ; i<MAX_REG_FRAME; i++) {
  299. if (fb->vbFbcYTbl[i].size > 0) {
  300. vdi_free_dma_memory(coreIdx, &fb->vbFbcYTbl[i]);
  301. osal_memset((void*)&fb->vbFbcYTbl, 0x00, sizeof(vpu_buffer_t));
  302. }
  303. if (fb->vbFbcCTbl[i].size > 0) {
  304. vdi_free_dma_memory(coreIdx, &fb->vbFbcCTbl[i]);
  305. osal_memset((void*)&fb->vbFbcCTbl, 0x00, sizeof(vpu_buffer_t));
  306. }
  307. }
  308. }
  309. void PrintDecSeqWarningMessages(
  310. Uint32 productId,
  311. DecInitialInfo* seqInfo
  312. )
  313. {
  314. if (PRODUCT_ID_W_SERIES(productId))
  315. {
  316. if (seqInfo->seqInitErrReason&0x00000001) VLOG(WARN, "sps_max_sub_layer_minus1 shall be 0 to 6\n");
  317. if (seqInfo->seqInitErrReason&0x00000002) VLOG(WARN, "general_reserved_zero_44bits shall be 0.\n");
  318. if (seqInfo->seqInitErrReason&0x00000004) VLOG(WARN, "reserved_zero_2bits shall be 0\n");
  319. if (seqInfo->seqInitErrReason&0x00000008) VLOG(WARN, "sub_layer_reserved_zero_44bits shall be 0");
  320. if (seqInfo->seqInitErrReason&0x00000010) VLOG(WARN, "general_level_idc shall have one of level of Table A.1\n");
  321. if (seqInfo->seqInitErrReason&0x00000020) VLOG(WARN, "sps_max_dec_pic_buffering[i] <= MaxDpbSize\n");
  322. if (seqInfo->seqInitErrReason&0x00000040) VLOG(WARN, "trailing bits shall be 1000... pattern, 7.3.2.1\n");
  323. if (seqInfo->seqInitErrReason&0x00100000) VLOG(WARN, "Not supported or undefined profile: %d\n", seqInfo->profile);
  324. if (seqInfo->seqInitErrReason&0x00200000) VLOG(WARN, "Spec over level(%d)\n", seqInfo->level);
  325. }
  326. }
  327. void DisplayDecodedInformationForHevc(
  328. DecHandle handle,
  329. Uint32 frameNo,
  330. DecOutputInfo* decodedInfo
  331. )
  332. {
  333. Int32 logLevel;
  334. if (decodedInfo == NULL) {
  335. if (handle->productId == PRODUCT_ID_510 || handle->productId == PRODUCT_ID_512) {
  336. VLOG(TRACE, "I NO T POC NAL DECO DISP PRESCAN DISPFLAG RD_PTR WR_PTR FRM_START FRM_END WxH SEQ TEMP CYCLE (Seek, Parse, Dec)\n");
  337. }
  338. else {
  339. VLOG(TRACE, "I NO T POC NAL DECO DISP PRESCAN DISPFLAG RD_PTR WR_PTR FRM_START FRM_END WxH SEQ TEMP CYCLE \n");
  340. }
  341. VLOG(TRACE, "------------------------------------------------------------------------------------------------------------\n");
  342. }
  343. else {
  344. logLevel = (decodedInfo->decodingSuccess&0x01) == 0 ? ERR : TRACE;
  345. if (handle->productId == PRODUCT_ID_4102) {
  346. logLevel = (decodedInfo->indexFramePrescan == -2) ? TRACE : logLevel;
  347. }
  348. // Print informations
  349. if (handle->productId == PRODUCT_ID_510 || handle->productId == PRODUCT_ID_512) {
  350. VLOG(logLevel, "%02d %5d %d %4d(%4d) %3d %2d(%2d) %2d(%2d) %7d %08x %08x %08x %08x %08x %4dx%-4d %4d %4d %d(%d,%d,%d)\n",
  351. handle->instIndex, frameNo, decodedInfo->picType,
  352. decodedInfo->h265Info.decodedPOC, decodedInfo->h265Info.displayPOC, decodedInfo->nalType,
  353. decodedInfo->indexFrameDecoded, decodedInfo->indexFrameDecodedForTiled,
  354. decodedInfo->indexFrameDisplay, decodedInfo->indexFrameDisplayForTiled,
  355. decodedInfo->indexFramePrescan,
  356. decodedInfo->frameDisplayFlag,decodedInfo->rdPtr, decodedInfo->wrPtr,
  357. decodedInfo->bytePosFrameStart, decodedInfo->bytePosFrameEnd,
  358. decodedInfo->dispPicWidth, decodedInfo->dispPicHeight, decodedInfo->sequenceNo,
  359. decodedInfo->h265Info.temporalId, decodedInfo->frameCycle, decodedInfo->seekCycle, decodedInfo->parseCycle, decodedInfo->decodeCycle);
  360. }
  361. else {
  362. VLOG(logLevel, "%02d %5d %d %4d(%4d) %3d %2d(%2d) %2d(%2d) %7d %08x %08x %08x %08x %08x %4dx%-4d %4d %4d %d\n",
  363. handle->instIndex, frameNo, decodedInfo->picType,
  364. decodedInfo->h265Info.decodedPOC, decodedInfo->h265Info.displayPOC, decodedInfo->nalType,
  365. decodedInfo->indexFrameDecoded, decodedInfo->indexFrameDecodedForTiled,
  366. decodedInfo->indexFrameDisplay, decodedInfo->indexFrameDisplayForTiled,
  367. decodedInfo->indexFramePrescan,
  368. decodedInfo->frameDisplayFlag,decodedInfo->rdPtr, decodedInfo->wrPtr,
  369. decodedInfo->bytePosFrameStart, decodedInfo->bytePosFrameEnd,
  370. decodedInfo->dispPicWidth, decodedInfo->dispPicHeight, decodedInfo->sequenceNo,
  371. decodedInfo->h265Info.temporalId, decodedInfo->frameCycle);
  372. }
  373. if (logLevel == ERR) {
  374. VLOG(ERR, "\t>>ERROR REASON: 0x%08x(0x%08x)\n", decodedInfo->errorReason, decodedInfo->errorReasonExt);
  375. }
  376. if (decodedInfo->numOfErrMBs) {
  377. VLOG(WARN, "\t>> ErrorBlock: %d\n", decodedInfo->numOfErrMBs);
  378. }
  379. }
  380. }
  381. void DisplayDecodedInformationForVP9(DecHandle handle, Uint32 frameNo, DecOutputInfo* decodedInfo)
  382. {
  383. Int32 logLevel;
  384. if (decodedInfo == NULL) {
  385. // Print header
  386. VLOG(TRACE, "I NO T DECO DISP PRESCAN DISPFLAG RD_PTR WR_PTR FRM_START FRM_END WxH SEQ CYCLE\n");
  387. VLOG(TRACE, "--------------------------------------------------------------------------------------------\n");
  388. }
  389. else {
  390. logLevel = (decodedInfo->decodingSuccess&0x01) == 0 ? ERR : TRACE;
  391. // Print informations
  392. VLOG(logLevel, "%02d %5d %d %2d(%2d) %2d(%2d) %7d %08x %08x %08x %08x %08x %4dx%-4d %4d %d\n",
  393. handle->instIndex, frameNo, decodedInfo->picType,
  394. decodedInfo->indexFrameDecoded, decodedInfo->indexFrameDecodedForTiled,
  395. decodedInfo->indexFrameDisplay, decodedInfo->indexFrameDisplayForTiled,
  396. decodedInfo->indexFramePrescan,
  397. decodedInfo->frameDisplayFlag,decodedInfo->rdPtr, decodedInfo->wrPtr,
  398. decodedInfo->bytePosFrameStart, decodedInfo->bytePosFrameEnd,
  399. decodedInfo->dispPicWidth, decodedInfo->dispPicHeight,
  400. decodedInfo->sequenceNo, decodedInfo->frameCycle
  401. );
  402. if (logLevel == ERR) {
  403. VLOG(ERR, "\t>>ERROR REASON: 0x%08x(0x%08x)\n", decodedInfo->errorReason, decodedInfo->errorReasonExt);
  404. }
  405. if (decodedInfo->numOfErrMBs) {
  406. VLOG(WARN, "\t>> ErrorBlock: %d\n", decodedInfo->numOfErrMBs);
  407. }
  408. }
  409. }
  410. void DisplayDecodedInformationCommon(DecHandle handle, CodStd codec, Uint32 frameNo, DecOutputInfo* decodedInfo)
  411. {
  412. Int32 logLevel;
  413. if (decodedInfo == NULL) {
  414. // Print header
  415. VLOG(TRACE, "I NO T TID DEC_POC DECO DISP DISPFLAG RD_PTR WR_PTR FRM_START FRM_END WxH\n");
  416. VLOG(TRACE, "---------------------------------------------------------------------------------------\n");
  417. }
  418. else {
  419. VpuRect rc = decodedInfo->rcDisplay;
  420. Uint32 width = rc.right - rc.left;
  421. Uint32 height = rc.bottom - rc.top;
  422. char strTemporalId[16];
  423. char strPoc[16];
  424. logLevel = (decodedInfo->decodingSuccess&0x01) == 0 ? ERR : TRACE;
  425. // Print informations
  426. VLOG(logLevel, "%02d %5d %d %s %s %2d(%2d) %2d(%2d) %08x %08x %08x %08x %08x %dx%d\n",
  427. handle->instIndex, frameNo, decodedInfo->picType, strTemporalId, strPoc,
  428. decodedInfo->indexFrameDecoded, decodedInfo->indexFrameDecodedForTiled,
  429. decodedInfo->indexFrameDisplay, decodedInfo->indexFrameDisplayForTiled,
  430. decodedInfo->frameDisplayFlag,decodedInfo->rdPtr, decodedInfo->wrPtr,
  431. decodedInfo->bytePosFrameStart, decodedInfo->bytePosFrameEnd,
  432. width, height);
  433. if (logLevel == ERR) {
  434. VLOG(ERR, "\t>>ERROR REASON: 0x%08x(0x%08x)\n", decodedInfo->errorReason, decodedInfo->errorReasonExt);
  435. }
  436. if (decodedInfo->numOfErrMBs) {
  437. VLOG(WARN, "\t>> ErrorBlock: %d\n", decodedInfo->numOfErrMBs);
  438. }
  439. }
  440. }
  441. /**
  442. * \brief Print out decoded information such like RD_PTR, WR_PTR, PIC_TYPE, ..
  443. * \param decodedInfo If this parameter is not NULL then print out decoded informations
  444. * otherwise print out header.
  445. */
  446. void DisplayDecodedInformation(DecHandle handle, CodStd codec, Uint32 frameNo, DecOutputInfo* decodedInfo)
  447. {
  448. switch (codec)
  449. {
  450. case STD_HEVC:
  451. DisplayDecodedInformationForHevc(handle, frameNo, decodedInfo);
  452. break;
  453. case STD_VP9:
  454. DisplayDecodedInformationForVP9(handle, frameNo, decodedInfo);
  455. break;
  456. default:
  457. DisplayDecodedInformationCommon(handle, codec, frameNo, decodedInfo);
  458. break;
  459. }
  460. return;
  461. }
  462. static void Wave4DisplayEncodedInformation(
  463. EncHandle handle,
  464. CodStd codec,
  465. Uint32 frameNo,
  466. EncOutputInfo* encodedInfo,
  467. Int32 srcEndFlag,
  468. Int32 srcFrameIdx
  469. )
  470. {
  471. if (encodedInfo == NULL) {
  472. VLOG(INFO, "------------------------------------------------------------------------------\n");
  473. VLOG(INFO, "I NO T RECON RD_PTR WR_PTR BYTES SRCIDX USEDSRCIDX Cycle \n");
  474. VLOG(INFO, "------------------------------------------------------------------------------\n");
  475. } else {
  476. VLOG(INFO, "%02d %5d %5d %5d %08x %08x %8x %2d %2d %8d\n",
  477. handle->instIndex, encodedInfo->encPicCnt, encodedInfo->picType, encodedInfo->reconFrameIndex, encodedInfo->rdPtr, encodedInfo->wrPtr,
  478. encodedInfo->bitstreamSize, (srcEndFlag == 1 ? -1 : srcFrameIdx), encodedInfo->encSrcIdx, encodedInfo->frameCycle);
  479. }
  480. }
  481. static void Wave5DisplayEncodedInformation(
  482. EncHandle handle,
  483. CodStd codec,
  484. Uint32 frameNo,
  485. EncOutputInfo* encodedInfo,
  486. Int32 srcEndFlag,
  487. Int32 srcFrameIdx
  488. )
  489. {
  490. if (encodedInfo == NULL) {
  491. VLOG(INFO, "--------------------------------------------------------------------------------------------------------------\n");
  492. VLOG(INFO, " | Cycle\n");
  493. VLOG(INFO, "I NO T RECON RD_PTR WR_PTR BYTES SRCIDX USEDSRCIDX | FRAME PREPARING PROCESSING ENCODING\n");
  494. VLOG(INFO, "--------------------------------------------------------------------------------------------------------------\n");
  495. } else {
  496. VLOG(INFO, "%02d %5d %5d %5d %08x %08x %8x %2d %2d %8d %8d %8d %8d\n",
  497. handle->instIndex, encodedInfo->encPicCnt, encodedInfo->picType, encodedInfo->reconFrameIndex, encodedInfo->rdPtr, encodedInfo->wrPtr,
  498. encodedInfo->bitstreamSize, (srcEndFlag == 1 ? -1 : srcFrameIdx), encodedInfo->encSrcIdx,
  499. encodedInfo->frameCycle, encodedInfo->encPrepareCycle, encodedInfo->encProcessingCycle, encodedInfo->encEncodingCycle);
  500. }
  501. }
  502. static void Coda9DisplayEncodedInformation(
  503. DecHandle handle,
  504. CodStd codec,
  505. Uint32 frameNo,
  506. EncOutputInfo* encodedInfo
  507. )
  508. {
  509. if (encodedInfo == NULL) {
  510. // Print header
  511. VLOG(INFO, "I NO T RECON RD_PTR WR_PTR \n");
  512. VLOG(INFO, "-------------------------------------\n");
  513. }
  514. else {
  515. char** picTypeArray = (codec==STD_AVC ? EncPicTypeStringH264 : EncPicTypeStringMPEG4);
  516. char* strPicType;
  517. if (encodedInfo->picType > 2) strPicType = "?";
  518. else strPicType = picTypeArray[encodedInfo->picType];
  519. // Print informations
  520. VLOG(INFO, "%02d %5d %5s %5d %08x %08x\n",
  521. handle->instIndex, frameNo, strPicType,
  522. encodedInfo->reconFrameIndex, encodedInfo->rdPtr, encodedInfo->wrPtr);
  523. }
  524. }
  525. /*lint -esym(438, ap) */
  526. void
  527. DisplayEncodedInformation(
  528. EncHandle handle,
  529. CodStd codec,
  530. Uint32 frameNo,
  531. EncOutputInfo* encodedInfo,
  532. ...
  533. )
  534. {
  535. int srcEndFlag;
  536. int srcFrameIdx;
  537. va_list ap;
  538. switch (codec) {
  539. case STD_HEVC:
  540. va_start(ap, encodedInfo);
  541. srcEndFlag = va_arg(ap, Uint32);
  542. srcFrameIdx = va_arg(ap, Uint32);
  543. va_end(ap);
  544. if(handle->productId == PRODUCT_ID_520 )
  545. Wave5DisplayEncodedInformation(handle, codec, frameNo, encodedInfo, srcEndFlag , srcFrameIdx);
  546. else
  547. Wave4DisplayEncodedInformation(handle, codec, frameNo, encodedInfo, srcEndFlag , srcFrameIdx);
  548. break;
  549. default:
  550. Coda9DisplayEncodedInformation(handle, codec, frameNo, encodedInfo);
  551. break;
  552. }
  553. return;
  554. }
  555. /*lint +esym(438, ap) */
  556. void ChangePathStyle(
  557. char *str
  558. )
  559. {
  560. }
  561. void ReleaseVideoMemory(
  562. Uint32 coreIndex,
  563. vpu_buffer_t* memoryArr,
  564. Uint32 count
  565. )
  566. {
  567. Uint32 index;
  568. for (index=0; index<count; index++) {
  569. if (memoryArr[index].size)
  570. vdi_free_dma_memory(coreIndex, &memoryArr[index]);
  571. }
  572. }
  573. BOOL AllocateDecFrameBuffer(
  574. DecHandle decHandle,
  575. TestDecConfig* config,
  576. Uint32 tiledFbCount,
  577. Uint32 linearFbCount,
  578. FrameBuffer* retFbArray,
  579. vpu_buffer_t* retFbAddrs,
  580. Uint32* retStride
  581. )
  582. {
  583. Uint32 framebufSize;
  584. Uint32 totalFbCount;
  585. Uint32 coreIndex;
  586. Uint32 index;
  587. FrameBufferFormat format = config->wtlFormat;
  588. DecInitialInfo seqInfo;
  589. FrameBufferAllocInfo fbAllocInfo;
  590. RetCode ret;
  591. vpu_buffer_t* pvb;
  592. size_t framebufStride;
  593. size_t framebufHeight;
  594. Uint32 productId;
  595. DRAMConfig* pDramCfg = NULL;
  596. DRAMConfig dramCfg = {0};
  597. coreIndex = VPU_HANDLE_CORE_INDEX(decHandle);
  598. productId = VPU_HANDLE_PRODUCT_ID(decHandle);
  599. VPU_DecGiveCommand(decHandle, DEC_GET_SEQ_INFO, (void*)&seqInfo);
  600. if (productId == PRODUCT_ID_960) {
  601. pDramCfg = &dramCfg;
  602. ret = VPU_DecGiveCommand(decHandle, GET_DRAM_CONFIG, pDramCfg);
  603. }
  604. totalFbCount = tiledFbCount + linearFbCount;
  605. if (productId == PRODUCT_ID_4102 || productId == PRODUCT_ID_420 || productId == PRODUCT_ID_412 || productId == PRODUCT_ID_420L ||
  606. productId == PRODUCT_ID_510 || productId == PRODUCT_ID_512 || productId == PRODUCT_ID_515) {
  607. format = (seqInfo.lumaBitdepth > 8 || seqInfo.chromaBitdepth > 8) ? FORMAT_420_P10_16BIT_LSB : FORMAT_420;
  608. }
  609. else if (productId == PRODUCT_ID_7Q) {
  610. if (decHandle->codecMode == HEVC_DEC)
  611. format = (seqInfo.lumaBitdepth > 8 || seqInfo.chromaBitdepth > 8) ? FORMAT_420_P10_16BIT_LSB : FORMAT_420;
  612. else
  613. format = FORMAT_420;
  614. }
  615. if (decHandle->codecMode == C7_VP9_DEC) {
  616. framebufStride = CalcStride(VPU_ALIGN64(seqInfo.picWidth), seqInfo.picHeight, format, config->cbcrInterleave, config->mapType, TRUE);
  617. framebufHeight = VPU_ALIGN64(seqInfo.picHeight);
  618. framebufSize = VPU_GetFrameBufSize(decHandle->coreIdx, framebufStride, framebufHeight,
  619. config->mapType, format, config->cbcrInterleave, NULL);
  620. *retStride = framebufStride;
  621. }
  622. else if (productId == PRODUCT_ID_7Q && decHandle->codecMode != C7_HEVC_DEC) {
  623. framebufStride = CalcStride(seqInfo.picWidth, seqInfo.picHeight, format, config->cbcrInterleave, config->mapType, FALSE);
  624. framebufHeight = seqInfo.interlace ? VPU_ALIGN32(seqInfo.picHeight) : VPU_ALIGN16(seqInfo.picHeight);
  625. framebufSize = VPU_GetFrameBufSize(decHandle->coreIdx, framebufStride, framebufHeight,
  626. config->mapType, format, config->cbcrInterleave, NULL);
  627. *retStride = framebufStride;
  628. }
  629. else if (decHandle->codecMode == C7_AVS2_DEC) {
  630. framebufStride = CalcStride(seqInfo.picWidth, seqInfo.picHeight, format, config->cbcrInterleave, config->mapType, FALSE);
  631. framebufHeight = VPU_ALIGN8(seqInfo.picHeight);
  632. framebufSize = VPU_GetFrameBufSize(decHandle->coreIdx, framebufStride, framebufHeight,
  633. config->mapType, format, config->cbcrInterleave, pDramCfg);
  634. *retStride = framebufStride;
  635. }
  636. else {
  637. *retStride = VPU_ALIGN32(seqInfo.picWidth);
  638. framebufStride = CalcStride(seqInfo.picWidth, seqInfo.picHeight, format, config->cbcrInterleave, config->mapType, FALSE);
  639. framebufHeight = seqInfo.picHeight;
  640. framebufSize = VPU_GetFrameBufSize(decHandle->coreIdx, framebufStride, seqInfo.picHeight,
  641. config->mapType, format, config->cbcrInterleave, pDramCfg);
  642. }
  643. osal_memset((void*)&fbAllocInfo, 0x00, sizeof(fbAllocInfo));
  644. osal_memset((void*)retFbArray, 0x00, sizeof(FrameBuffer)*totalFbCount);
  645. fbAllocInfo.format = format;
  646. fbAllocInfo.cbcrInterleave = config->cbcrInterleave;
  647. fbAllocInfo.mapType = config->mapType;
  648. fbAllocInfo.stride = framebufStride;
  649. fbAllocInfo.height = framebufHeight;
  650. fbAllocInfo.size = framebufSize;
  651. fbAllocInfo.lumaBitDepth = seqInfo.lumaBitdepth;
  652. fbAllocInfo.chromaBitDepth = seqInfo.chromaBitdepth;
  653. fbAllocInfo.num = tiledFbCount;
  654. fbAllocInfo.endian = config->frameEndian;
  655. fbAllocInfo.type = FB_TYPE_CODEC;
  656. osal_memset((void*)retFbAddrs, 0x00, sizeof(vpu_buffer_t)*totalFbCount);
  657. for (index=0; index<tiledFbCount; index++) {
  658. pvb = &retFbAddrs[index];
  659. pvb->size = framebufSize;
  660. if (vdi_allocate_dma_memory(coreIndex, pvb) < 0) {
  661. VLOG(ERR, "%s:%d fail to allocate frame buffer\n", __FUNCTION__, __LINE__);
  662. ReleaseVideoMemory(coreIndex, retFbAddrs, totalFbCount);
  663. return FALSE;
  664. }
  665. retFbArray[index].bufY = pvb->phys_addr;
  666. retFbArray[index].bufCb = (PhysicalAddress)-1;
  667. retFbArray[index].bufCr = (PhysicalAddress)-1;
  668. retFbArray[index].updateFbInfo = TRUE;
  669. retFbArray[index].size = framebufSize;
  670. }
  671. if (tiledFbCount != 0) {
  672. if ((ret=VPU_DecAllocateFrameBuffer(decHandle, fbAllocInfo, retFbArray)) != RETCODE_SUCCESS) {
  673. VLOG(ERR, "%s:%d failed to VPU_DecAllocateFrameBuffer(), ret(%d)\n",
  674. __FUNCTION__, __LINE__, ret);
  675. ReleaseVideoMemory(coreIndex, retFbAddrs, totalFbCount);
  676. return FALSE;
  677. }
  678. }
  679. if (config->enableWTL == TRUE || linearFbCount != 0) {
  680. size_t linearStride;
  681. size_t picWidth;
  682. size_t picHeight;
  683. size_t fbHeight;
  684. Uint32 mapType = LINEAR_FRAME_MAP;
  685. FrameBufferFormat outFormat = config->wtlFormat;
  686. picWidth = seqInfo.picWidth;
  687. picHeight = seqInfo.picHeight;
  688. fbHeight = picHeight;
  689. if (decHandle->codecMode == C7_VP9_DEC) {
  690. fbHeight = VPU_ALIGN64(picHeight);
  691. }
  692. else if (decHandle->codecMode == C7_AVS2_DEC) {
  693. fbHeight = VPU_ALIGN8(picHeight);
  694. }
  695. else if (productId == PRODUCT_ID_7Q && decHandle->codecMode != C7_HEVC_DEC)
  696. {
  697. fbHeight = seqInfo.interlace ? VPU_ALIGN32(picHeight) : VPU_ALIGN16(picHeight);
  698. }
  699. else if (productId == PRODUCT_ID_960 || productId == PRODUCT_ID_980)
  700. {
  701. fbHeight = VPU_ALIGN32(picHeight);
  702. }
  703. if (decHandle->codecMode == C7_VP9_DEC) {
  704. linearStride = CalcStride(VPU_ALIGN64(picWidth), picHeight, outFormat, config->cbcrInterleave, (TiledMapType)mapType, TRUE);
  705. }
  706. else {
  707. linearStride = CalcStride(picWidth, picHeight, outFormat, config->cbcrInterleave, (TiledMapType)mapType, FALSE);
  708. }
  709. framebufSize = VPU_GetFrameBufSize(coreIndex, linearStride, fbHeight, (TiledMapType)mapType, outFormat, config->cbcrInterleave, pDramCfg);
  710. for (index=tiledFbCount; index<totalFbCount; index++) {
  711. pvb = &retFbAddrs[index];
  712. pvb->size = framebufSize;
  713. if (vdi_allocate_dma_memory(coreIndex, pvb) < 0) {
  714. VLOG(ERR, "%s:%d fail to allocate frame buffer\n", __FUNCTION__, __LINE__);
  715. ReleaseVideoMemory(coreIndex, retFbAddrs, totalFbCount);
  716. return FALSE;
  717. }
  718. retFbArray[index].bufY = pvb->phys_addr;
  719. retFbArray[index].bufCb = -1;
  720. retFbArray[index].bufCr = -1;
  721. retFbArray[index].updateFbInfo = TRUE;
  722. retFbArray[index].size = framebufSize;
  723. }
  724. fbAllocInfo.nv21 = config->nv21;
  725. fbAllocInfo.format = outFormat;
  726. fbAllocInfo.num = linearFbCount;
  727. fbAllocInfo.mapType = (TiledMapType)mapType;
  728. fbAllocInfo.stride = linearStride;
  729. fbAllocInfo.height = fbHeight;
  730. ret = VPU_DecAllocateFrameBuffer(decHandle, fbAllocInfo, &retFbArray[tiledFbCount]);
  731. if (ret != RETCODE_SUCCESS) {
  732. VLOG(ERR, "%s:%d failed to VPU_DecAllocateFrameBuffer() ret:%d\n",
  733. __FUNCTION__, __LINE__, ret);
  734. ReleaseVideoMemory(coreIndex, retFbAddrs, totalFbCount);
  735. return FALSE;
  736. }
  737. }
  738. return TRUE;
  739. }
  740. #if defined(_WIN32) || defined(__MSDOS__)
  741. #define DOS_FILESYSTEM
  742. #define IS_DIR_SEPARATOR(__c) ((__c == '/') || (__c == '\\'))
  743. #else
  744. /* UNIX style */
  745. #define IS_DIR_SEPARATOR(__c) (__c == '/')
  746. #endif
  747. char* GetDirname(
  748. const char* path
  749. )
  750. {
  751. int length;
  752. int i;
  753. char* upper_dir;
  754. if (path == NULL) return NULL;
  755. length = strlen(path);
  756. for (i=length-1; i>=0; i--) {
  757. if (IS_DIR_SEPARATOR(path[i])) break;
  758. }
  759. if (i<0) {
  760. upper_dir = strdup(".");
  761. } else {
  762. upper_dir = strdup(path);
  763. upper_dir[i] = 0;
  764. }
  765. return upper_dir;
  766. }
  767. char* GetBasename(
  768. const char* pathname
  769. )
  770. {
  771. const char* base = NULL;
  772. const char* p = pathname;
  773. if (p == NULL) {
  774. return NULL;
  775. }
  776. #if defined(DOS_FILESYSTEM)
  777. if (isalpha((int)p[0]) && p[1] == ':') {
  778. p += 2;
  779. }
  780. #endif
  781. for (base=p; *p; p++) {
  782. if (IS_DIR_SEPARATOR(*p)) {
  783. base = p+1;
  784. }
  785. }
  786. return (char*)base;
  787. }
  788. char* GetFileExtension(
  789. const char* filename
  790. )
  791. {
  792. Uint32 len;
  793. Uint32 i;
  794. len = strlen(filename);
  795. for (i=len-1; i>=0; i--) {
  796. if (filename[i] == '.') {
  797. return (char*)&filename[i+1];
  798. }
  799. }
  800. return NULL;
  801. }
  802. void byte_swap(unsigned char* data, int len)
  803. {
  804. Uint8 temp;
  805. Int32 i;
  806. for (i=0; i<len; i+=2) {
  807. temp = data[i];
  808. data[i] = data[i+1];
  809. data[i+1] = temp;
  810. }
  811. }
  812. BOOL IsEndOfFile(FILE* fp)
  813. {
  814. BOOL result = FALSE;
  815. Int32 index = 0;
  816. char cTemp;
  817. // Check current fp pos
  818. if (osal_feof(fp) != 0) {
  819. result = TRUE;
  820. }
  821. // Check next fp pos
  822. // Ignore newline character
  823. do {
  824. cTemp = fgetc(fp);
  825. index++;
  826. if (osal_feof(fp) != 0) {
  827. result = TRUE;
  828. break;
  829. }
  830. } while (cTemp == '\n' || cTemp == '\r');
  831. // Revert fp pos
  832. index *= (-1);
  833. osal_fseek(fp, index, SEEK_CUR);
  834. return result;
  835. }
  836. BOOL CalcYuvSize(
  837. Int32 format,
  838. Int32 picWidth,
  839. Int32 picHeight,
  840. Int32 cbcrInterleave,
  841. size_t *lumaSize,
  842. size_t *chromaSize,
  843. size_t *frameSize,
  844. Int32 *bitDepth,
  845. Int32 *packedFormat,
  846. Int32 *yuv3p4b)
  847. {
  848. Int32 temp_picWidth;
  849. Int32 chromaWidth;
  850. if ( bitDepth != 0)
  851. *bitDepth = 0;
  852. if ( packedFormat != 0)
  853. *packedFormat = 0;
  854. if ( yuv3p4b != 0)
  855. *yuv3p4b = 0;
  856. switch (format)
  857. {
  858. case FORMAT_420:
  859. if (lumaSize) *lumaSize = picWidth * picHeight;
  860. if (chromaSize) *chromaSize = picWidth * picHeight / 2;
  861. if (frameSize) *frameSize = picWidth * picHeight * 3 /2;
  862. break;
  863. case FORMAT_YUYV:
  864. case FORMAT_YVYU:
  865. case FORMAT_UYVY:
  866. case FORMAT_VYUY:
  867. if ( packedFormat != 0)
  868. *packedFormat = 1;
  869. if (lumaSize) *lumaSize = picWidth * picHeight;
  870. if (chromaSize) *chromaSize = picWidth * picHeight;
  871. if (frameSize) *frameSize = *lumaSize + *chromaSize;
  872. break;
  873. case FORMAT_224:
  874. if (lumaSize) *lumaSize = picWidth * picHeight;
  875. if (chromaSize) *chromaSize = picWidth * picHeight;
  876. if (frameSize) *frameSize = picWidth * picHeight * 4 / 2;
  877. break;
  878. case FORMAT_422:
  879. if (lumaSize) *lumaSize = picWidth * picHeight;
  880. if (chromaSize) *chromaSize = picWidth * picHeight;
  881. if (frameSize) *frameSize = picWidth * picHeight * 4 / 2;
  882. break;
  883. case FORMAT_444:
  884. if (lumaSize) *lumaSize = picWidth * picHeight;
  885. if (chromaSize) *chromaSize = picWidth * picHeight * 2;
  886. if (frameSize) *frameSize = picWidth * picHeight * 3;
  887. break;
  888. case FORMAT_400:
  889. if (lumaSize) *lumaSize = picWidth * picHeight;
  890. if (chromaSize) *chromaSize = 0;
  891. if (frameSize) *frameSize = picWidth * picHeight;
  892. break;
  893. case FORMAT_422_P10_16BIT_MSB:
  894. case FORMAT_422_P10_16BIT_LSB:
  895. if ( bitDepth != NULL) {
  896. *bitDepth = 10;
  897. }
  898. if (lumaSize) *lumaSize = picWidth * picHeight * 2;
  899. if (chromaSize) *chromaSize = *lumaSize;
  900. if (frameSize) *frameSize = *lumaSize + *chromaSize;
  901. break;
  902. case FORMAT_420_P10_16BIT_MSB:
  903. case FORMAT_420_P10_16BIT_LSB:
  904. if ( bitDepth != 0)
  905. *bitDepth = 10;
  906. if (lumaSize) *lumaSize = picWidth * picHeight * 2;
  907. if (chromaSize) *chromaSize = picWidth * picHeight;
  908. if (frameSize) *frameSize = *lumaSize + *chromaSize;
  909. break;
  910. case FORMAT_YUYV_P10_16BIT_MSB: // 4:2:2 10bit packed
  911. case FORMAT_YUYV_P10_16BIT_LSB:
  912. case FORMAT_YVYU_P10_16BIT_MSB:
  913. case FORMAT_YVYU_P10_16BIT_LSB:
  914. case FORMAT_UYVY_P10_16BIT_MSB:
  915. case FORMAT_UYVY_P10_16BIT_LSB:
  916. case FORMAT_VYUY_P10_16BIT_MSB:
  917. case FORMAT_VYUY_P10_16BIT_LSB:
  918. if ( bitDepth != 0)
  919. *bitDepth = 10;
  920. if ( packedFormat != 0)
  921. *packedFormat = 1;
  922. if (lumaSize) *lumaSize = picWidth * picHeight * 2;
  923. if (chromaSize) *chromaSize = picWidth * picHeight * 2;
  924. if (frameSize) *frameSize = *lumaSize + *chromaSize;
  925. break;
  926. case FORMAT_420_P10_32BIT_MSB:
  927. case FORMAT_420_P10_32BIT_LSB:
  928. if ( bitDepth != 0)
  929. *bitDepth = 10;
  930. if ( yuv3p4b != 0)
  931. *yuv3p4b = 1;
  932. temp_picWidth = VPU_ALIGN32(picWidth);
  933. chromaWidth = ((VPU_ALIGN16(temp_picWidth/2*(1<<cbcrInterleave))+2)/3*4);
  934. if ( cbcrInterleave == 1)
  935. {
  936. if (lumaSize) *lumaSize = (temp_picWidth+2)/3*4 * picHeight;
  937. if (chromaSize) *chromaSize = chromaWidth * picHeight/2;
  938. } else {
  939. if (lumaSize) *lumaSize = (temp_picWidth+2)/3*4 * picHeight;
  940. if (chromaSize) *chromaSize = chromaWidth * picHeight/2*2;
  941. }
  942. if (frameSize) *frameSize = *lumaSize + *chromaSize;
  943. break;
  944. case FORMAT_YUYV_P10_32BIT_MSB:
  945. case FORMAT_YUYV_P10_32BIT_LSB:
  946. case FORMAT_YVYU_P10_32BIT_MSB:
  947. case FORMAT_YVYU_P10_32BIT_LSB:
  948. case FORMAT_UYVY_P10_32BIT_MSB:
  949. case FORMAT_UYVY_P10_32BIT_LSB:
  950. case FORMAT_VYUY_P10_32BIT_MSB:
  951. case FORMAT_VYUY_P10_32BIT_LSB:
  952. if ( bitDepth != 0)
  953. *bitDepth = 10;
  954. if ( packedFormat != 0)
  955. *packedFormat = 1;
  956. if ( yuv3p4b != 0)
  957. *yuv3p4b = 1;
  958. if (frameSize) *frameSize = ((picWidth*2)+2)/3*4 * picHeight;
  959. if (lumaSize) *lumaSize = *frameSize/2;
  960. if (chromaSize) *chromaSize = *frameSize/2;
  961. break;
  962. default:
  963. if (frameSize) *frameSize = picWidth * picHeight * 3 / 2;
  964. VLOG(ERR, "%s:%d Not supported format(%d)\n", __FILE__, __LINE__, format);
  965. return FALSE;
  966. }
  967. return TRUE;
  968. }
  969. int GetPackedFormat (
  970. int srcBitDepth,
  971. int packedType,
  972. int p10bits,
  973. int msb)
  974. {
  975. int format = FORMAT_YUYV;
  976. // default pixel format = P10_16BIT_LSB (p10bits = 16, msb = 0)
  977. if (srcBitDepth == 8) {
  978. switch(packedType) {
  979. case PACKED_YUYV:
  980. format = FORMAT_YUYV;
  981. break;
  982. case PACKED_YVYU:
  983. format = FORMAT_YVYU;
  984. break;
  985. case PACKED_UYVY:
  986. format = FORMAT_UYVY;
  987. break;
  988. case PACKED_VYUY:
  989. format = FORMAT_VYUY;
  990. break;
  991. default:
  992. format = -1;
  993. }
  994. }
  995. else if (srcBitDepth == 10) {
  996. switch(packedType) {
  997. case PACKED_YUYV:
  998. if (p10bits == 16) {
  999. format = (msb == 0) ? FORMAT_YUYV_P10_16BIT_LSB : FORMAT_YUYV_P10_16BIT_MSB;
  1000. }
  1001. else if (p10bits == 32) {
  1002. format = (msb == 0) ? FORMAT_YUYV_P10_32BIT_LSB : FORMAT_YUYV_P10_32BIT_MSB;
  1003. }
  1004. else {
  1005. format = -1;
  1006. }
  1007. break;
  1008. case PACKED_YVYU:
  1009. if (p10bits == 16) {
  1010. format = (msb == 0) ? FORMAT_YVYU_P10_16BIT_LSB : FORMAT_YVYU_P10_16BIT_MSB;
  1011. }
  1012. else if (p10bits == 32) {
  1013. format = (msb == 0) ? FORMAT_YVYU_P10_32BIT_LSB : FORMAT_YVYU_P10_32BIT_MSB;
  1014. }
  1015. else {
  1016. format = -1;
  1017. }
  1018. break;
  1019. case PACKED_UYVY:
  1020. if (p10bits == 16) {
  1021. format = (msb == 0) ? FORMAT_UYVY_P10_16BIT_LSB : FORMAT_UYVY_P10_16BIT_MSB;
  1022. }
  1023. else if (p10bits == 32) {
  1024. format = (msb == 0) ? FORMAT_UYVY_P10_32BIT_LSB : FORMAT_UYVY_P10_32BIT_MSB;
  1025. }
  1026. else {
  1027. format = -1;
  1028. }
  1029. break;
  1030. case PACKED_VYUY:
  1031. if (p10bits == 16) {
  1032. format = (msb == 0) ? FORMAT_VYUY_P10_16BIT_LSB : FORMAT_VYUY_P10_16BIT_MSB;
  1033. }
  1034. else if (p10bits == 32) {
  1035. format = (msb == 0) ? FORMAT_VYUY_P10_32BIT_LSB : FORMAT_VYUY_P10_32BIT_MSB;
  1036. }
  1037. else {
  1038. format = -1;
  1039. }
  1040. break;
  1041. default:
  1042. format = -1;
  1043. }
  1044. }
  1045. else {
  1046. format = -1;
  1047. }
  1048. return format;
  1049. }
  1050. void GenRegionToMap(
  1051. VpuRect *region, /**< The size of the ROI region for H.265 (start X/Y in CTU, end X/Y int CTU) */
  1052. int *roiLevel,
  1053. int num,
  1054. Uint32 mapWidth,
  1055. Uint32 mapHeight,
  1056. Uint8 *roiCtuMap)
  1057. {
  1058. Int32 roi_id, blk_addr;
  1059. Uint32 roi_map_size = mapWidth * mapHeight;
  1060. //init roi map
  1061. for (blk_addr=0; blk_addr<(Int32)roi_map_size; blk_addr++)
  1062. roiCtuMap[blk_addr] = 0;
  1063. //set roi map. roi_entry[i] has higher priority than roi_entry[i+1]
  1064. for (roi_id=(Int32)num-1; roi_id>=0; roi_id--)
  1065. {
  1066. Uint32 x, y;
  1067. VpuRect *roi = region + roi_id;
  1068. for (y=roi->top; y<=roi->bottom; y++)
  1069. {
  1070. for (x=roi->left; x<=roi->right; x++)
  1071. {
  1072. roiCtuMap[y*mapWidth + x] = *(roiLevel + roi_id);
  1073. }
  1074. }
  1075. }
  1076. }
  1077. void GenRegionToQpMap(
  1078. VpuRect *region, /**< The size of the ROI region for H.265 (start X/Y in CTU, end X/Y int CTU) */
  1079. int *roiLevel,
  1080. int num,
  1081. int initQp,
  1082. Uint32 mapWidth,
  1083. Uint32 mapHeight,
  1084. Uint8 *roiCtuMap)
  1085. {
  1086. Int32 roi_id, blk_addr;
  1087. Uint32 roi_map_size = mapWidth * mapHeight;
  1088. //init roi map
  1089. for (blk_addr=0; blk_addr<(Int32)roi_map_size; blk_addr++)
  1090. roiCtuMap[blk_addr] = initQp;
  1091. //set roi map. roi_entry[i] has higher priority than roi_entry[i+1]
  1092. for (roi_id=(Int32)num-1; roi_id>=0; roi_id--)
  1093. {
  1094. Uint32 x, y;
  1095. VpuRect *roi = region + roi_id;
  1096. for (y=roi->top; y<=roi->bottom; y++)
  1097. {
  1098. for (x=roi->left; x<=roi->right; x++)
  1099. {
  1100. roiCtuMap[y*mapWidth + x] = *(roiLevel + roi_id);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. Int32 writeVuiRbsp(int coreIdx, TestEncConfig *encConfig, EncOpenParam *encOP, vpu_buffer_t *vbVuiRbsp)
  1106. {
  1107. if (encOP->encodeVuiRbsp == TRUE) {
  1108. vbVuiRbsp->size = VUI_HRD_RBSP_BUF_SIZE;
  1109. if (vdi_allocate_dma_memory(coreIdx, vbVuiRbsp) < 0) {
  1110. VLOG(ERR, "fail to allocate VUI rbsp buffer\n" );
  1111. return FALSE;
  1112. }
  1113. encOP->vuiRbspDataAddr = vbVuiRbsp->phys_addr;
  1114. if (encConfig) {
  1115. Uint8 *pVuiRbspBuf;
  1116. Int32 rbspSizeInByte = (encOP->vuiRbspDataSize+7)>>3;
  1117. ChangePathStyle(encConfig->vui_rbsp_file_name);
  1118. if ((encConfig->vui_rbsp_fp = osal_fopen(encConfig->vui_rbsp_file_name, "r")) == NULL) {
  1119. VLOG(ERR, "fail to open Prefix SEI NAL Data file, %s\n", encConfig->vui_rbsp_file_name);
  1120. return FALSE;
  1121. }
  1122. if (rbspSizeInByte > (Uint32)VUI_HRD_RBSP_BUF_SIZE)
  1123. VLOG(ERR, "VUI Rbsp size is bigger than buffer size\n");
  1124. pVuiRbspBuf = (Uint8*)osal_malloc(VUI_HRD_RBSP_BUF_SIZE);
  1125. osal_memset(pVuiRbspBuf, 0, VUI_HRD_RBSP_BUF_SIZE);
  1126. osal_fread(pVuiRbspBuf, 1, rbspSizeInByte, encConfig->vui_rbsp_fp);
  1127. vdi_write_memory(coreIdx, encOP->vuiRbspDataAddr, pVuiRbspBuf, rbspSizeInByte, encOP->streamEndian);
  1128. osal_free(pVuiRbspBuf);
  1129. }
  1130. }
  1131. return TRUE;
  1132. }
  1133. Int32 writeHrdRbsp(int coreIdx, TestEncConfig *encConfig, EncOpenParam *encOP, vpu_buffer_t *vbHrdRbsp)
  1134. {
  1135. if (encOP->encodeHrdRbspInVPS || encOP->encodeHrdRbspInVUI)
  1136. {
  1137. vbHrdRbsp->size = VUI_HRD_RBSP_BUF_SIZE;
  1138. if (vdi_allocate_dma_memory(coreIdx, vbHrdRbsp) < 0) {
  1139. VLOG(ERR, "fail to allocate HRD rbsp buffer\n" );
  1140. return FALSE;
  1141. }
  1142. encOP->hrdRbspDataAddr = vbHrdRbsp->phys_addr;
  1143. if (encConfig) {
  1144. Uint8 *pHrdRbspBuf;
  1145. Int32 rbspSizeInByte = (encOP->hrdRbspDataSize+7)>>3;
  1146. ChangePathStyle(encConfig->hrd_rbsp_file_name);
  1147. if ((encConfig->hrd_rbsp_fp = osal_fopen(encConfig->hrd_rbsp_file_name, "r")) == NULL) {
  1148. VLOG(ERR, "fail to open Prefix SEI NAL Data file, %s\n", encConfig->hrd_rbsp_file_name);
  1149. return FALSE;
  1150. }
  1151. if (rbspSizeInByte > (Uint32)VUI_HRD_RBSP_BUF_SIZE)
  1152. VLOG(ERR, "HRD Rbsp size is bigger than buffer size\n");
  1153. pHrdRbspBuf = (Uint8*)osal_malloc(VUI_HRD_RBSP_BUF_SIZE);
  1154. osal_memset(pHrdRbspBuf, 0, VUI_HRD_RBSP_BUF_SIZE);
  1155. osal_fread(pHrdRbspBuf, 1, rbspSizeInByte, encConfig->hrd_rbsp_fp);
  1156. vdi_write_memory(coreIdx, encOP->hrdRbspDataAddr, pHrdRbspBuf, rbspSizeInByte, encOP->streamEndian);
  1157. osal_free(pHrdRbspBuf);
  1158. }
  1159. }
  1160. return TRUE;
  1161. }
  1162. #ifdef TEST_ENCODE_CUSTOM_HEADER
  1163. Int32 writeCustomHeader(int coreIdx, EncOpenParam *encOP, vpu_buffer_t *vbCustomHeader, hrd_t *hrd)
  1164. {
  1165. Uint32 rbspBitSize;
  1166. Uint8 *pRbspBuf;
  1167. Uint32 rbspByteSize;
  1168. vui_t vui;
  1169. encOP->encodeVuiRbsp = 1;
  1170. vbCustomHeader->size = VUI_HRD_RBSP_BUF_SIZE;
  1171. if (vdi_allocate_dma_memory(coreIdx, vbCustomHeader) < 0) {
  1172. VLOG(ERR, "fail to allocate VUI rbsp buffer\n" );
  1173. return FALSE;
  1174. }
  1175. encOP->vuiRbspDataAddr = vbCustomHeader->phys_addr;
  1176. pRbspBuf = (Uint8 *)osal_malloc(VUI_HRD_RBSP_BUF_SIZE);
  1177. if (pRbspBuf) {
  1178. osal_memset(pRbspBuf, 0, VUI_HRD_RBSP_BUF_SIZE);
  1179. vui.vui_parameters_presesent_flag = 1;
  1180. vui.vui_timing_info_present_flag = 1;
  1181. vui.vui_num_units_in_tick = 1000;
  1182. vui.vui_time_scale = (encOP->frameRateInfo)*1000.0;
  1183. vui.vui_hrd_parameters_present_flag = 1;
  1184. vui.def_disp_win_left_offset = 1;
  1185. vui.def_disp_win_right_offset = 1;
  1186. vui.def_disp_win_top_offset = 1;
  1187. vui.def_disp_win_bottom_offset = 1;
  1188. // HRD param : refer xInitHrdParameters in HM
  1189. {
  1190. int useSubCpbParams =0;
  1191. int bitRate = encOP->bitRate;
  1192. int isRandomAccess;
  1193. int cpbSize = bitRate; // Adjusting value to be equal to TargetBitrate
  1194. int bitRateScale;
  1195. int cpbSizeScale;
  1196. int i, j, numSubLayersM1;
  1197. Uint32 bitrateValue, cpbSizeValue;
  1198. Uint32 duCpbSizeValue;
  1199. Uint32 duBitRateValue = 0;
  1200. if (bitRate >0) {
  1201. hrd->nal_hrd_parameters_present_flag = 1;
  1202. hrd->vcl_hrd_parameters_present_flag = 1;
  1203. }
  1204. else {
  1205. hrd->nal_hrd_parameters_present_flag = 0;
  1206. hrd->vcl_hrd_parameters_present_flag = 0;
  1207. }
  1208. if (encOP->EncStdParam.hevcParam.independSliceMode != 0)
  1209. useSubCpbParams = 1;
  1210. if (encOP->EncStdParam.hevcParam.intraPeriod > 0)
  1211. isRandomAccess = 1;
  1212. else
  1213. isRandomAccess = 0;
  1214. hrd->sub_pic_hrd_params_present_flag = useSubCpbParams;
  1215. if (useSubCpbParams) {
  1216. hrd->tick_divisor_minus2 = 100-2;
  1217. hrd->du_cpb_removal_delay_increment_length_minus1 = 7; // 8-bit precision ( plus 1 for last DU in AU )
  1218. hrd->sub_pic_cpb_params_in_pic_timing_sei_flag = 1;
  1219. hrd->dpb_output_delay_du_length_minus1 = 5+7; // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
  1220. }
  1221. else
  1222. hrd->sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
  1223. // calculate scale value of bitrate and initial delay
  1224. bitRateScale = calcScale(bitRate);
  1225. if ( bitRateScale <= 6 )
  1226. hrd->bit_rate_scale = 0;
  1227. else
  1228. hrd->bit_rate_scale = bitRateScale -6;
  1229. cpbSizeScale = calcScale(cpbSize);
  1230. if ( cpbSizeScale <= 4 )
  1231. hrd->cpb_size_scale = 0;
  1232. else
  1233. hrd->cpb_size_scale = cpbSizeScale -4;
  1234. hrd->cpb_size_du_scale = 6; // in units of 2^( 4 + 6 ) = 1,024 bit
  1235. hrd->initial_cpb_removal_delay_length_minus1 = 15; // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
  1236. if (isRandomAccess > 0) {
  1237. hrd->au_cpb_removal_delay_length_minus1 = 5;
  1238. hrd->dpb_output_delay_length_minus1 = 5;
  1239. }
  1240. else {
  1241. hrd->au_cpb_removal_delay_length_minus1 = 9;
  1242. hrd->dpb_output_delay_length_minus1 = 9;
  1243. }
  1244. numSubLayersM1 = 0;
  1245. if (encOP->EncStdParam.hevcParam.gopPresetIdx == 0) { // custom GOP
  1246. for(i=0; i<encOP->EncStdParam.hevcParam.gopParam.customGopSize; i++) {
  1247. if (numSubLayersM1 < encOP->EncStdParam.hevcParam.gopParam.picParam[i].temporalId)
  1248. numSubLayersM1 = encOP->EncStdParam.hevcParam.gopParam.picParam[i].temporalId;
  1249. }
  1250. }
  1251. hrd->vps_max_sub_layers_minus1 = numSubLayersM1;
  1252. // sub_layer_hrd_parameters
  1253. // BitRate[ i ] = ( bit_rate_value_minus1[ i ] + 1 ) * 2^( 6 + bit_rate_scale )
  1254. bitrateValue = bitRate / (1 << (6 + hrd->bit_rate_scale) ); // bitRate is in bits, so it needs to be scaled down
  1255. // CpbSize[ i ] = ( cpb_size_value_minus1[ i ] + 1 ) * 2^( 4 + cpb_size_scale )
  1256. cpbSizeValue = cpbSize / (1 << (4 + hrd->cpb_size_scale) ); // using bitRate results in 1 second CPB size
  1257. // DU CPB size could be smaller (i.e. bitrateValue / number of DUs), but we don't know
  1258. // in how many DUs the slice segment settings will result
  1259. duCpbSizeValue = bitrateValue;
  1260. duBitRateValue = cpbSizeValue;
  1261. for(i=0; i<hrd->vps_max_sub_layers_minus1; i++) {
  1262. hrd->fixed_pic_rate_general_flag[i] = 1;
  1263. hrd->fixed_pic_rate_within_cvs_flag[i] = 1; // fixed_pic_rate_general_flag[ i ] is equal to 1, the value of fixed_pic_rate_within_cvs_flag[ i ] is inferred to be equal to 1
  1264. hrd->elemental_duration_in_tc_minus1[i] = 0;
  1265. hrd->low_delay_hrd_flag[i] = 0;
  1266. hrd->cpb_cnt_minus1[i] = 0;
  1267. if (hrd->nal_hrd_parameters_present_flag) {
  1268. for (j=0; hrd->cpb_cnt_minus1[i]; j++) {
  1269. hrd->bit_rate_value_minus1[j][i] = bitrateValue-1;
  1270. hrd->cpb_size_value_minus1[j][i] = cpbSize -1;
  1271. hrd->cpb_size_du_value_minus1[j][i] = duCpbSizeValue -1;
  1272. hrd->bit_rate_du_value_minus1[j][i] = duBitRateValue -1;
  1273. hrd->cbr_flag[j][i] = 0;
  1274. }
  1275. }
  1276. if (hrd->vcl_hrd_parameters_present_flag) {
  1277. for (j=0; hrd->cpb_cnt_minus1[i]; j++) {
  1278. hrd->bit_rate_value_minus1[j][i] = bitrateValue-1;
  1279. hrd->cpb_size_value_minus1[j][i] = cpbSize -1;
  1280. hrd->cpb_size_du_value_minus1[j][i] = duCpbSizeValue -1;
  1281. hrd->bit_rate_du_value_minus1[j][i] = duBitRateValue -1;
  1282. hrd->cbr_flag[j][i] = 0;
  1283. }
  1284. }
  1285. }
  1286. }
  1287. EncodeVUI(hrd, &vui, pRbspBuf, VUI_HRD_RBSP_BUF_SIZE, &rbspByteSize, &rbspBitSize, encOP->frameRateInfo);
  1288. encOP->vuiRbspDataSize = rbspBitSize;
  1289. vdi_write_memory(coreIdx, encOP->vuiRbspDataAddr, pRbspBuf, rbspByteSize, encOP->streamEndian);
  1290. osal_free(pRbspBuf);
  1291. }
  1292. return TRUE;
  1293. }
  1294. Int32 allocateSeiNalDataBuf(int coreIdx, vpu_buffer_t *vbSeiNal, int srcFbNum)
  1295. {
  1296. Int32 i;
  1297. for (i = 0; i < srcFbNum; i++) { // the number of roi buffer should be the same as source buffer num.
  1298. vbSeiNal[i].size = SEI_NAL_DATA_BUF_SIZE;
  1299. if (vdi_allocate_dma_memory(coreIdx, &vbSeiNal[i]) < 0) {
  1300. VLOG(ERR, "fail to allocate SeiNalData buffer\n");
  1301. return FALSE;
  1302. }
  1303. }
  1304. return TRUE;
  1305. }
  1306. Int32 writeSeiNalData(EncHandle handle, int streamEndian, PhysicalAddress prefixSeiNalAddr, hrd_t *hrd)
  1307. {
  1308. sei_buffering_period_t buffering_period_sei;
  1309. Uint8 *pSeiBuf;
  1310. sei_active_parameter_t active_parameter_sei;
  1311. Int32 seiByteSize = 0;
  1312. HevcSEIDataEnc seiDataEnc;
  1313. sei_pic_timing_t pic_timing_sei;
  1314. pSeiBuf = (Uint8 *)osal_malloc(SEI_NAL_DATA_BUF_SIZE);
  1315. if (pSeiBuf)
  1316. {
  1317. osal_memset(pSeiBuf, 0x00, SEI_NAL_DATA_BUF_SIZE);
  1318. osal_memset(&seiDataEnc, 0x00, sizeof(seiDataEnc));
  1319. seiDataEnc.prefixSeiNalEnable = 1;
  1320. seiDataEnc.prefixSeiDataEncOrder = 0;
  1321. seiDataEnc.prefixSeiNalAddr = prefixSeiNalAddr;
  1322. active_parameter_sei.active_video_parameter_set_id = 0; // vps_video_parameter_set_id of the VPS. wave420 is 0
  1323. active_parameter_sei.self_contained_cvs_flag = 0;
  1324. active_parameter_sei.no_parameter_set_update_flag = 0;
  1325. active_parameter_sei.num_sps_ids_minus1 = 0;
  1326. active_parameter_sei.active_seq_parameter_set_id[0] = 0;// sps_seq_parameter_set_id of the SPS. wave420 is 0
  1327. // put sei_pic_timing
  1328. pic_timing_sei.pic_struct = 0;
  1329. pic_timing_sei.source_scan_type = 1;
  1330. pic_timing_sei.duplicate_flag = 0;
  1331. buffering_period_sei.nal_initial_cpb_removal_delay[0] = 2229;
  1332. buffering_period_sei.nal_initial_cpb_removal_offset[0] = 0;
  1333. seiByteSize = EncodePrefixSEI(&active_parameter_sei, &pic_timing_sei, &buffering_period_sei, hrd, pSeiBuf, SEI_NAL_DATA_BUF_SIZE);
  1334. seiDataEnc.prefixSeiDataSize = seiByteSize;
  1335. vdi_write_memory(handle->coreIdx, seiDataEnc.prefixSeiNalAddr, pSeiBuf, seiDataEnc.prefixSeiDataSize, streamEndian);
  1336. free(pSeiBuf);
  1337. }
  1338. VPU_EncGiveCommand(handle, ENC_SET_SEI_NAL_DATA, &seiDataEnc);
  1339. return TRUE;
  1340. }
  1341. #endif
  1342. int setRoiMap(int coreIdx, TestEncConfig *encConfig, EncOpenParam encOP, PhysicalAddress addrRoiMap,
  1343. Uint8 *roiMapBuf, int srcFrameWidth, int srcFrameHeight, EncParam *encParam, int maxCtuNum)
  1344. {
  1345. int i;
  1346. if (encConfig->roi_enable && encParam->srcEndFlag != 1) {
  1347. int roiNum=0;
  1348. // sample code to convert ROI coordinate to ROI map.
  1349. osal_memset(&encConfig->region[0], 0, sizeof(VpuRect)*MAX_ROI_NUMBER);
  1350. osal_memset(&encConfig->roiLevel[0], 0, sizeof(int)*MAX_ROI_NUMBER);
  1351. if (encConfig) {
  1352. char lineStr[256] = {0, };
  1353. int val;
  1354. fgets(lineStr, 256, encConfig->roi_file);
  1355. sscanf(lineStr, "%x\n", &val);
  1356. if (val != 0xFFFF)
  1357. VLOG(ERR, "can't find the picture delimiter \n");
  1358. fgets(lineStr, 256, encConfig->roi_file);
  1359. sscanf(lineStr, "%d\n", &val); // picture index
  1360. fgets(lineStr, 256, encConfig->roi_file);
  1361. sscanf(lineStr, "%d\n", &roiNum); // number of roi regions
  1362. for (i = 0; i < roiNum; i++) {
  1363. fgets(lineStr, 256, encConfig->roi_file);
  1364. if (parseRoiCtuModeParam(lineStr, &encConfig->region[i], &encConfig->roiLevel[i], srcFrameWidth, srcFrameHeight) == 0 ) {
  1365. VLOG(ERR, "CFG file error : %s value is not available. \n", encConfig->roi_file_name);
  1366. }
  1367. }
  1368. }
  1369. encParam->ctuOptParam.addrRoiCtuMap = addrRoiMap;
  1370. encParam->ctuOptParam.mapEndian = VDI_LITTLE_ENDIAN;
  1371. encParam->ctuOptParam.mapStride = ((encOP.picWidth+63)&~63)>>6;
  1372. encParam->ctuOptParam.roiEnable = (roiNum != 0) ? encConfig->roi_enable : 0;
  1373. encParam->ctuOptParam.roiDeltaQp = encConfig->roi_delta_qp;
  1374. if (encParam->ctuOptParam.roiEnable) {
  1375. GenRegionToMap(&encConfig->region[0], &encConfig->roiLevel[0], roiNum, encParam->ctuOptParam.mapStride , ((encOP.picHeight+63)&~63)>>6, &roiMapBuf[0]);
  1376. vdi_write_memory(coreIdx, encParam->ctuOptParam.addrRoiCtuMap, roiMapBuf, maxCtuNum, encParam->ctuOptParam.mapEndian);
  1377. }
  1378. }
  1379. return TRUE;
  1380. }
  1381. int checkParamRestriction(Uint32 productId, TestEncConfig *encConfig)
  1382. {
  1383. if ( (encConfig->compare_type & (1<<MODE_SAVE_ENCODED)) && encConfig->bitstreamFileName[0] == 0) {
  1384. encConfig->compare_type &= ~(1<<MODE_SAVE_ENCODED);
  1385. VLOG(ERR,"You want to Save bitstream data. Set the path\n");
  1386. }
  1387. if ( (encConfig->compare_type & (1<<MODE_COMP_ENCODED)) && encConfig->ref_stream_path[0] == 0) {
  1388. encConfig->compare_type &= ~(1<<MODE_COMP_ENCODED);
  1389. VLOG(ERR,"You want to Compare bitstream data. Set the path\n");
  1390. }
  1391. /* CHECK PARAMETERS */
  1392. if (productId == PRODUCT_ID_420L) {
  1393. if (encConfig->rotAngle != 0) {
  1394. VLOG(ERR, "WAVE420L Not support rotation option\n");
  1395. return FALSE;
  1396. }
  1397. if (encConfig->mirDir != 0) {
  1398. VLOG(ERR, "WAVE420L Not support mirror option\n");
  1399. return FALSE;
  1400. }
  1401. if (encConfig->srcFormat3p4b == TRUE) {
  1402. VLOG(ERR, "WAVE420L Not support 3 pixel 4byte format option\n");
  1403. return FALSE;
  1404. }
  1405. if (encConfig->ringBufferEnable == TRUE) {
  1406. encConfig->ringBufferEnable = FALSE;
  1407. VLOG(ERR, "WAVE420L doesn't support ring-buffer.\n");
  1408. }
  1409. }
  1410. return TRUE;
  1411. }
  1412. int openRoiMapFile(TestEncConfig *encConfig)
  1413. {
  1414. if (encConfig->roi_enable) {
  1415. if (encConfig) {
  1416. ChangePathStyle(encConfig->roi_file_name);
  1417. if ((encConfig->roi_file = osal_fopen(encConfig->roi_file_name, "r")) == NULL) {
  1418. VLOG(ERR, "fail to open ROI file, %s\n", encConfig->roi_file_name);
  1419. return FALSE;
  1420. }
  1421. }
  1422. }
  1423. return TRUE;
  1424. }
  1425. int allocateRoiMapBuf(int coreIdx, TestEncConfig encConfig, vpu_buffer_t *vbRoi, int srcFbNum, int ctuNum)
  1426. {
  1427. int i;
  1428. if (encConfig.roi_enable) {
  1429. //number of roi buffer should be the same as source buffer num.
  1430. for (i = 0; i < srcFbNum ; i++) {
  1431. vbRoi[i].size = ctuNum;
  1432. if (vdi_allocate_dma_memory(coreIdx, &vbRoi[i]) < 0) {
  1433. VLOG(ERR, "fail to allocate ROI buffer\n" );
  1434. return FALSE;
  1435. }
  1436. }
  1437. }
  1438. return TRUE;
  1439. }
  1440. int setCtuQpMap(int coreIdx, TestEncConfig *encConfig, EncOpenParam encOP, PhysicalAddress addrCtuQpMap,
  1441. Uint8 *ctuQpMapBuf, int srcFrameWidth, int srcFrameHeight, EncParam *encParam, int maxCtuNum)
  1442. {
  1443. int i;
  1444. if (encConfig->ctu_qp_enable && encParam->srcEndFlag != 1) {
  1445. int ctuQpNum=0;
  1446. // sample code to convert CTU coordinate to CTU MODE map.
  1447. osal_memset(&encConfig->region[0], 0, sizeof(VpuRect)*MAX_ROI_NUMBER);
  1448. osal_memset(&encConfig->ctuQp[0], 0, sizeof(int)*MAX_ROI_NUMBER);
  1449. if (encConfig) {
  1450. char lineStr[256] = {0, };
  1451. int val;
  1452. fgets(lineStr, 256, encConfig->ctuqp_file);
  1453. sscanf(lineStr, "%x\n", &val);
  1454. if (val != 0xFFFF)
  1455. VLOG(ERR, "can't find the picture delimiter \n");
  1456. fgets(lineStr, 256, encConfig->ctuqp_file);
  1457. sscanf(lineStr, "%d\n", &val); // picture index
  1458. fgets(lineStr, 256, encConfig->ctuqp_file);
  1459. sscanf(lineStr, "%d\n", &ctuQpNum); // number of roi regions
  1460. for (i = 0; i < ctuQpNum; i++) {
  1461. fgets(lineStr, 256, encConfig->ctuqp_file);
  1462. if (parseRoiCtuModeParam(lineStr, &encConfig->region[i], &encConfig->ctuQp[i], srcFrameWidth, srcFrameHeight) == 0 ) {
  1463. VLOG(ERR, "CFG file error : %s value is not available. \n", encConfig->ctuqp_file_name);
  1464. }
  1465. }
  1466. }
  1467. encParam->ctuOptParam.addrCtuQpMap = addrCtuQpMap;
  1468. encParam->ctuOptParam.mapEndian = VDI_LITTLE_ENDIAN;
  1469. encParam->ctuOptParam.mapStride = ((encOP.picWidth+63)&~63)>>6;
  1470. encParam->ctuOptParam.ctuQpEnable = encConfig->ctu_qp_enable;
  1471. if (encParam->ctuOptParam.ctuQpEnable) {
  1472. GenRegionToQpMap(&encConfig->region[0], &encConfig->ctuQp[0], ctuQpNum, encOP.EncStdParam.hevcParam.intraQP, encParam->ctuOptParam.mapStride , ((encOP.picHeight+63)&~63)>>6, &ctuQpMapBuf[0]);
  1473. vdi_write_memory(coreIdx, encParam->ctuOptParam.addrCtuQpMap, ctuQpMapBuf, maxCtuNum, encParam->ctuOptParam.mapEndian);
  1474. }
  1475. }
  1476. return TRUE;
  1477. }
  1478. int openCtuQpMapFile(TestEncConfig *encConfig)
  1479. {
  1480. if (encConfig->ctu_qp_enable) {
  1481. if (encConfig->ctuqp_file_name[0]) {
  1482. ChangePathStyle(encConfig->ctuqp_file_name);
  1483. if ((encConfig->ctuqp_file = osal_fopen(encConfig->ctuqp_file_name, "r")) == NULL) {
  1484. VLOG(ERR, "fail to open CTU QP file, %s\n", encConfig->ctuqp_file_name);
  1485. return FALSE;
  1486. }
  1487. }
  1488. }
  1489. return TRUE;
  1490. }
  1491. int allocateCtuQpMapBuf(int coreIdx, TestEncConfig encConfig, vpu_buffer_t *vbCtuQp, int srcFbNum, int ctuNum) {
  1492. int i;
  1493. if (encConfig.ctu_qp_enable) {
  1494. // the number of CTU mode buffer should be the same as source buffer num.
  1495. for (i = 0; i < srcFbNum; i++) {
  1496. vbCtuQp[i].size = ctuNum;
  1497. if (vdi_allocate_dma_memory(coreIdx, &vbCtuQp[i]) < 0) {
  1498. VLOG(ERR, "fail to allocate CTU QP buffer\n" );
  1499. return FALSE;
  1500. }
  1501. }
  1502. }
  1503. return TRUE;
  1504. }
  1505. // Define tokens for parsing scaling list file
  1506. const char* MatrixType[SCALING_LIST_SIZE_NUM][SL_NUM_MATRIX] =
  1507. {
  1508. {"INTRA4X4_LUMA", "INTRA4X4_CHROMAU", "INTRA4X4_CHROMAV", "INTER4X4_LUMA", "INTER4X4_CHROMAU", "INTER4X4_CHROMAV"},
  1509. {"INTRA8X8_LUMA", "INTRA8X8_CHROMAU", "INTRA8X8_CHROMAV", "INTER8X8_LUMA", "INTER8X8_CHROMAU", "INTER8X8_CHROMAV"},
  1510. {"INTRA16X16_LUMA", "INTRA16X16_CHROMAU", "INTRA16X16_CHROMAV", "INTER16X16_LUMA", "INTER16X16_CHROMAU", "INTER16X16_CHROMAV"},
  1511. {"INTRA32X32_LUMA", "INTRA32X32_CHROMAU_FROM16x16_CHROMAU", "INTRA32X32_CHROMAV_FROM16x16_CHROMAV","INTER32X32_LUMA", "INTER32X32_CHROMAU_FROM16x16_CHROMAU", "INTER32X32_CHROMAV_FROM16x16_CHROMAV"}
  1512. };
  1513. const char* MatrixType_DC[SCALING_LIST_SIZE_NUM - 2][SL_NUM_MATRIX] =
  1514. {
  1515. {"INTRA16X16_LUMA_DC", "INTRA16X16_CHROMAU_DC", "INTRA16X16_CHROMAV_DC", "INTER16X16_LUMA_DC", "INTER16X16_CHROMAU_DC", "INTER16X16_CHROMAV_DC"},
  1516. {"INTRA32X32_LUMA_DC", "INTRA32X32_CHROMAU_DC_FROM16x16_CHROMAU", "INTRA32X32_CHROMAV_DC_FROM16x16_CHROMAV", "INTER32X32_LUMA_DC","INTER32X32_CHROMAU_DC_FROM16x16_CHROMAU","INTER32X32_CHROMAV_DC_FROM16x16_CHROMAV"},
  1517. };
  1518. static Uint8* get_sl_addr(UserScalingList* sl, Uint32 size_id, Uint32 mat_id)
  1519. {
  1520. Uint8* addr = NULL;
  1521. switch(size_id)
  1522. {
  1523. case SCALING_LIST_4x4:
  1524. addr = sl->s4[mat_id];
  1525. break;
  1526. case SCALING_LIST_8x8:
  1527. addr = sl->s8[mat_id];
  1528. break;
  1529. case SCALING_LIST_16x16:
  1530. addr = sl->s16[mat_id];
  1531. break;
  1532. case SCALING_LIST_32x32:
  1533. addr = sl->s32[mat_id];
  1534. break;
  1535. }
  1536. return addr;
  1537. }
  1538. int parse_user_scaling_list(UserScalingList* sl, FILE* fp_sl)
  1539. {
  1540. #define LINE_SIZE (1024)
  1541. const Uint32 scaling_list_size[SCALING_LIST_SIZE_NUM] = {16, 64, 64, 64};
  1542. char line[LINE_SIZE];
  1543. Uint32 i;
  1544. Uint32 size_id, mat_id, data, num_coef = 0;
  1545. Uint8* src = NULL;
  1546. Uint8* ref = NULL;
  1547. char* ret;
  1548. const char* type_str;
  1549. for(size_id = 0; size_id < SCALING_LIST_SIZE_NUM; size_id++) // for 4, 8, 16, 32
  1550. {
  1551. num_coef = scaling_list_size[size_id];
  1552. for(mat_id = 0; mat_id < SL_NUM_MATRIX; mat_id++) // for intra_y, intra_cb, intra_cr, inter_y, inter_cb, inter_cr
  1553. {
  1554. src = get_sl_addr(sl, size_id, mat_id);
  1555. if(size_id == SCALING_LIST_32x32 && (mat_id % 3)) // derive scaling list of chroma32x32 from that of chrom16x16
  1556. {
  1557. ref = get_sl_addr(sl, size_id - 1, mat_id);
  1558. for(i = 0; i < num_coef; i++)
  1559. src[i] = ref[i];
  1560. }
  1561. else
  1562. {
  1563. fseek(fp_sl,0,0);
  1564. type_str = MatrixType[size_id][mat_id];
  1565. do
  1566. {
  1567. ret = fgets(line, LINE_SIZE, fp_sl);
  1568. if((ret == NULL) || (strstr(line, type_str) == NULL && feof(fp_sl)))
  1569. {
  1570. printf("Error: can't read a scaling list matrix(%s)\n", type_str);
  1571. return 0;
  1572. }
  1573. } while (strstr(line, type_str) == NULL);
  1574. // get all coeff
  1575. for(i = 0; i < num_coef; i++)
  1576. {
  1577. if(fscanf(fp_sl, "%d,", &data) != 1)
  1578. {
  1579. printf("Error: can't read a scaling list matrix(%s)\n", type_str);
  1580. return 0;
  1581. }
  1582. src[i] = data;
  1583. }
  1584. // get DC coeff for 16, 32
  1585. if(size_id > SCALING_LIST_8x8)
  1586. {
  1587. fseek(fp_sl,0,0);
  1588. type_str = MatrixType_DC[size_id - 2][mat_id];
  1589. do
  1590. {
  1591. ret = fgets(line, LINE_SIZE, fp_sl);
  1592. if((ret == NULL) || (strstr(line, type_str) == NULL && feof(fp_sl)))
  1593. {
  1594. printf("Error: can't read a scaling list matrix(%s)\n", type_str);
  1595. return 0;
  1596. }
  1597. } while (strstr(line, type_str) == NULL);
  1598. if(fscanf(fp_sl, "%d,", &data) != 1)
  1599. {
  1600. printf("Error: can't read a scaling list matrix(%s)\n", type_str);
  1601. return 0;
  1602. }
  1603. // overwrite DC value
  1604. src[0] = data;
  1605. }
  1606. }
  1607. } // for matrix id
  1608. } // for size id
  1609. return 1;
  1610. }
  1611. int parse_custom_lambda(Uint32 buf[NUM_CUSTOM_LAMBDA], FILE* fp)
  1612. {
  1613. int i, j = 0;
  1614. char lineStr[256] = {0, };
  1615. for(i = 0; i < 52; i++)
  1616. {
  1617. if( NULL == fgets(lineStr, 256, fp) )
  1618. {
  1619. printf("Error: can't read custom_lambda\n");
  1620. return 0;
  1621. }
  1622. else
  1623. {
  1624. sscanf(lineStr, "%d\n", &buf[j++]);
  1625. }
  1626. }
  1627. for(i = 0; i < 52; i++)
  1628. {
  1629. if( NULL == fgets(lineStr, 256, fp) )
  1630. {
  1631. printf("Error: can't read custom_lambda\n");
  1632. return 0;
  1633. }
  1634. else
  1635. sscanf(lineStr, "%d\n", &buf[j++]);
  1636. }
  1637. return 1;
  1638. }
  1639. #ifdef PLATFORM_LINUX
  1640. struct option* ConvertOptions(
  1641. struct OptionExt* cnmOpt,
  1642. Uint32 nItems
  1643. )
  1644. {
  1645. struct option* opt;
  1646. Uint32 i;
  1647. opt = (struct option*)osal_malloc(sizeof(struct option) * nItems);
  1648. if (opt == NULL) {
  1649. return NULL;
  1650. }
  1651. for (i=0; i<nItems; i++) {
  1652. osal_memcpy((void*)&opt[i], (void*)&cnmOpt[i], sizeof(struct option));
  1653. }
  1654. return opt;
  1655. }
  1656. int mkdir_recursive(
  1657. char *path,
  1658. mode_t omode
  1659. )
  1660. {
  1661. struct stat sb;
  1662. mode_t numask, oumask;
  1663. int first, last, retval;
  1664. char *p;
  1665. p = path;
  1666. oumask = 0;
  1667. retval = 0;
  1668. if (p[0] == '/') /* Skip leading '/'. */
  1669. ++p;
  1670. for (first = 1, last = 0; !last ; ++p) {
  1671. if (p[0] == '\0')
  1672. last = 1;
  1673. else if (p[0] != '/')
  1674. continue;
  1675. *p = '\0';
  1676. if (p[1] == '\0')
  1677. last = 1;
  1678. if (first) {
  1679. /*
  1680. * POSIX 1003.2:
  1681. * For each dir operand that does not name an existing
  1682. * directory, effects equivalent to those cased by the
  1683. * following command shall occcur:
  1684. *
  1685. * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
  1686. * mkdir [-m mode] dir
  1687. *
  1688. * We change the user's umask and then restore it,
  1689. * instead of doing chmod's.
  1690. */
  1691. oumask = umask(0);
  1692. numask = oumask & ~(S_IWUSR | S_IXUSR);
  1693. (void)umask(numask);
  1694. first = 0;
  1695. }
  1696. if (last)
  1697. (void)umask(oumask);
  1698. if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
  1699. if (errno == EEXIST || errno == EISDIR) {
  1700. if (stat(path, &sb) < 0) {
  1701. VLOG(INFO, "%s", path);
  1702. retval = 1;
  1703. break;
  1704. } else if (!S_ISDIR(sb.st_mode)) {
  1705. if (last)
  1706. errno = EEXIST;
  1707. else
  1708. errno = ENOTDIR;
  1709. VLOG(INFO, "%s", path);
  1710. retval = 1;
  1711. break;
  1712. }
  1713. } else {
  1714. VLOG(INFO, "%s", path);
  1715. retval = 1;
  1716. break;
  1717. }
  1718. } else if (1)
  1719. {
  1720. VLOG(INFO, "%s", path);
  1721. chmod(path, omode);
  1722. }
  1723. if (!last)
  1724. *p = '/';
  1725. }
  1726. if (!first && !last)
  1727. (void)umask(oumask);
  1728. return (retval);
  1729. }
  1730. #endif
  1731. BOOL TestMachineSetup(
  1732. TestMachine* machine
  1733. )
  1734. {
  1735. BOOL success = TRUE;
  1736. osal_init_keyboard();
  1737. InitializeDebugEnv(0, machine->testEnvOptions);
  1738. return success;
  1739. }
  1740. void TestMachineCleanUp(
  1741. TestMachine* machine
  1742. )
  1743. {
  1744. Uint32 i;
  1745. Listener* o;
  1746. for (i=0; i<machine->numObservers; i++) {
  1747. o = &machine->observers[i];
  1748. o->destruct(o);
  1749. }
  1750. ReleaseDebugEnv();
  1751. osal_close_keyboard();
  1752. }
  1753. void TestMachineAddListener(
  1754. TestMachine* machine,
  1755. Listener observer,
  1756. void* handle
  1757. )
  1758. {
  1759. Uint32 i = machine->numObservers;
  1760. if (i == MAX_OBSERVERS) {
  1761. return;
  1762. }
  1763. observer.construct(&observer, handle);
  1764. osal_memcpy(&machine->observers[i], (void*)&observer, sizeof(Listener));
  1765. machine->numObservers++;
  1766. }
  1767. BOOL TestMachineSetData(
  1768. TestMachine* machine,
  1769. void* data
  1770. )
  1771. {
  1772. Listener* o = NULL;
  1773. Uint32 i;
  1774. BOOL success = TRUE;
  1775. for (i=0; i<machine->numObservers; i++) {
  1776. o = &machine->observers[i];
  1777. if (o->update(o, data) == FALSE) {
  1778. success = FALSE;
  1779. }
  1780. }
  1781. return success;
  1782. }
  1783. int file_exist(
  1784. char* path
  1785. )
  1786. {
  1787. #ifdef _MSC_VER
  1788. DWORD attributes;
  1789. char temp[4096];
  1790. LPCTSTR lp_path = (LPCTSTR)temp;
  1791. if (path == NULL) {
  1792. return False;
  1793. }
  1794. strcpy(temp, path);
  1795. replace_character(temp, '/', '\\');
  1796. attributes = GetFileAttributes(lp_path);
  1797. return (attributes != (DWORD)-1);
  1798. #else
  1799. return !access(path, F_OK);
  1800. #endif
  1801. }
  1802. BOOL MkDir(
  1803. char* path
  1804. )
  1805. {
  1806. #ifdef _MSC_VER
  1807. char cmd[4096];
  1808. #endif
  1809. if (file_exist(path))
  1810. return TRUE;
  1811. #ifdef _MSC_VER
  1812. sprintf(cmd, "mkdir %s", path);
  1813. replace_character(cmd, '/', '\\');
  1814. if (system(cmd)) {
  1815. return FALSE;
  1816. }
  1817. return True;
  1818. #else
  1819. return mkdir_recursive(path, S_IRWXU | S_IRWXG | S_IRWXO);
  1820. #endif
  1821. }