jpuhelper.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * Copyright (c) 2018, Chips&Media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include "../jpuapi/regdefine.h"
  30. #include "../jpuapi/jpuapi.h"
  31. #include "../jpuapi/jpuapifunc.h"
  32. #include "../jpuapi/jputable.h"
  33. #include "jpulog.h"
  34. #include "main_helper.h"
  35. fb_context s_fb[MAX_NUM_INSTANCE];
  36. static int StoreYuvImageBurstFormat_V20(int chromaStride, Uint8 * dst, int picWidth, int picHeight, Uint32 bitDepth,Uint64 addrY, Uint64 addrCb, Uint64 addrCr, Uint32 stride, FrameFormat format, int endian, CbCrInterLeave interLeave, PackedFormat packed);
  37. // Figure A.6 - Zig-zag sequence of quantized DCT coefficients
  38. const int InvScanTable[64] = {
  39. 0, 1, 5, 6, 14, 15, 27, 28,
  40. 2, 4, 7, 13, 16, 26, 29, 42,
  41. 3, 8, 12, 17, 25, 30, 41, 43,
  42. 9, 11, 18, 24, 31, 40, 44, 53,
  43. 10, 19, 23, 32, 39, 45, 52, 54,
  44. 20, 22, 33, 38, 46, 51, 55, 60,
  45. 21, 34, 37, 47, 50, 56, 59, 61,
  46. 35, 36, 48, 49, 57, 58, 62, 63
  47. };
  48. const int ScanTable[64] = {
  49. 0, 1, 8, 16, 9, 2, 3, 10,
  50. 17, 24, 32, 25, 18, 11, 4, 5,
  51. 12, 19, 26, 33, 40, 48, 41, 34,
  52. 27, 20, 13, 6, 7, 14, 21, 28,
  53. 35, 42, 49, 56, 57, 50, 43, 36,
  54. 29, 22, 15, 23, 30, 37, 44, 51,
  55. 58, 59, 52, 45, 38, 31, 39, 46,
  56. 53, 60, 61, 54, 47, 55, 62, 63
  57. };
  58. /******************************************************************************
  59. DPB Image Data Control
  60. ******************************************************************************/
  61. #define MAX_ROW_IN_MCU 8
  62. static void TiledToLinear(Uint8* pSrc, Uint8* pDst, Uint32 width, Uint32 height, Uint32 cWidth, Uint32 cHeight, Uint32 lineWidth)
  63. {
  64. Uint32 picX, picY;
  65. Uint32 yuvAddr = 0;
  66. Uint32 convAddr = 0;
  67. Uint32 chromaBase = 0;
  68. Uint32 row = 0;
  69. Uint32 col = 0;
  70. Uint32 ncols = width / lineWidth;
  71. // Luma data.
  72. for (picY = 0; picY < height; picY++) {
  73. for (picX = 0; picX < width; picX += lineWidth) {
  74. convAddr = ((picY/MAX_ROW_IN_MCU)*MAX_ROW_IN_MCU*width) + (row*width) + col*lineWidth;
  75. memcpy(pDst + convAddr, pSrc + yuvAddr, lineWidth);
  76. yuvAddr += lineWidth;
  77. row++;
  78. row %= 8;
  79. if (row == 0) col++;
  80. col %= ncols;
  81. }
  82. }
  83. chromaBase = convAddr + lineWidth; // add last line
  84. // Chroma data.
  85. // lineWidth*2. only support Cb Cr interleaved format.
  86. lineWidth *= 2;
  87. col = row = 0;
  88. convAddr = 0;
  89. ncols = cWidth / lineWidth;
  90. for (picY = 0; picY < cHeight; picY++) {
  91. for (picX = 0; picX < cWidth; picX += lineWidth) {
  92. convAddr = ((picY/MAX_ROW_IN_MCU)*MAX_ROW_IN_MCU*cWidth) + (row*cWidth) + col*lineWidth;
  93. memcpy(pDst + chromaBase + convAddr, pSrc + yuvAddr, lineWidth);
  94. yuvAddr += lineWidth;
  95. row++;
  96. row %= 8;
  97. if (row == 0) col++;
  98. col %= ncols;
  99. }
  100. }
  101. }
  102. int SaveYuvImageHelperFormat_V20(
  103. FILE* yuvFp,
  104. Uint8* pYuv,
  105. FrameBuffer* fb,
  106. CbCrInterLeave interLeave,
  107. PackedFormat packed,
  108. Uint32 picWidth,
  109. Uint32 picHeight,
  110. Uint32 bitDepth,
  111. BOOL tiled2Linear
  112. )
  113. {
  114. int frameSize;
  115. if (pYuv == NULL) {
  116. JLOG(ERR, "%s:%d pYuv is NULL\n", __FUNCTION__, __LINE__);
  117. return 0;
  118. }
  119. frameSize = StoreYuvImageBurstFormat_V20(fb->strideC, pYuv, picWidth, picHeight, bitDepth,
  120. fb->bufY,
  121. fb->bufCb,
  122. fb->bufCr,
  123. fb->stride,
  124. fb->format,
  125. fb->endian,
  126. interLeave,
  127. packed);
  128. if (tiled2Linear == TRUE) {
  129. Uint32 Bpp = JPU_CEIL(8, bitDepth) / 8;
  130. Uint32 width = JPU_CEIL(picWidth,8) * Bpp;
  131. Uint32 height = JPU_CEIL(picHeight,8);
  132. Uint32 tiledFrameSize;
  133. Uint8* pConv = NULL;
  134. Uint8* base = NULL;
  135. Uint8* target = NULL;
  136. Uint32 cWidth = (fb->format == FORMAT_420 || fb->format == FORMAT_422) ? width / 2 : width;
  137. Uint32 cHeight = (fb->format == FORMAT_420 || fb->format == FORMAT_440) ? height / 2 : height;
  138. Uint32 lineWidth = 8 * Bpp;
  139. Uint32 y, linearChromaWidth, linearChromaHeight;
  140. switch (fb->format) {
  141. case FORMAT_400: linearChromaWidth = 0, linearChromaHeight = 0, tiledFrameSize = width * height; break;
  142. case FORMAT_420: linearChromaWidth = picWidth, linearChromaHeight = picHeight/2, tiledFrameSize = width * height * 3 /2; break;
  143. case FORMAT_422: linearChromaWidth = picWidth, linearChromaHeight = picHeight, tiledFrameSize = width * height * 2; break;
  144. case FORMAT_440: linearChromaWidth = picWidth*2, linearChromaHeight = picHeight/2, tiledFrameSize = width * height * 2; break;
  145. case FORMAT_444: linearChromaWidth = picWidth*2, linearChromaHeight = picHeight, tiledFrameSize = width * height * 3; break;
  146. default:
  147. JLOG(ERR, "%d:%d Unknown format:(%d)\n", __FUNCTION__, __LINE__, fb->format);
  148. return 0;
  149. }
  150. if ((pConv=(Uint8*)malloc(tiledFrameSize)) == NULL) {
  151. JLOG(ERR, "%s:%d Failed to allocate memory\n", __FUNCTION__, __LINE__);
  152. return 0;
  153. }
  154. if (fb->format == FORMAT_400) {
  155. cWidth = cHeight = 0;
  156. }
  157. cWidth *= 2; // Must be semi-planar.
  158. TiledToLinear(pYuv, pConv, width, height, cWidth, cHeight, lineWidth);
  159. base = pConv;
  160. target = pYuv;
  161. // Copy Luma
  162. for (y=0; y<picHeight; y++) {
  163. memcpy((void*)target, (void*)(base + y*width), picWidth);
  164. target += fb->stride;
  165. }
  166. base = pConv + (width*height);
  167. // Copy Chroma
  168. for (y=0; y<linearChromaHeight; y++) {
  169. memcpy((void*)target, (void*)(base+ y*cWidth), linearChromaWidth);
  170. target += fb->strideC;
  171. }
  172. free((void*)pConv);
  173. }
  174. if (yuvFp)
  175. {
  176. if( !fwrite(pYuv, sizeof(Uint8), frameSize , yuvFp) )
  177. {
  178. JLOG(ERR, "Frame Data fwrite failed file handle is 0x%x \n", yuvFp );
  179. return 0;
  180. }
  181. fflush(yuvFp);
  182. }
  183. return 1;
  184. }
  185. /******************************************************************************
  186. JPEG specific Helper
  187. ******************************************************************************/
  188. static int getTblElement(FILE *fp, char *str)
  189. {
  190. while (1) {
  191. if (fgets(str, 256, fp) == NULL)
  192. return 0;
  193. if ((str[0] != ';') && (str[0] != '/'))
  194. break;
  195. }
  196. return 1;
  197. }
  198. static int parseHuffmanTable(FILE * fp, EncMjpgParam *param, int prec)
  199. {
  200. char sLine[258] = {0,};
  201. unsigned int h[8] = {0,};
  202. BYTE *huffBit;
  203. BYTE *huffVal;
  204. int i, j;
  205. int max_ac = 162;
  206. if (prec)
  207. max_ac = 256;
  208. huffBit = param->huffBits[DC_TABLE_INDEX0];
  209. huffVal = param->huffVal[DC_TABLE_INDEX0];
  210. for (i=0; i<2; i++) // Luma DC BitLength
  211. {
  212. if (getTblElement(fp, sLine) == 0)
  213. return 0;
  214. memset(h, 0x00, 8*sizeof(unsigned int));
  215. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  216. for (j=0; j<8; j++)
  217. *huffBit++ = (BYTE)h[j];
  218. }
  219. for (i=0; i<(16/8); i++) // Luma DC HuffValue
  220. {
  221. if (getTblElement(fp, sLine) == 0)
  222. return 0;
  223. memset(h, 0x00, 8*sizeof(unsigned int));
  224. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  225. for (j=0; j<8; j++)
  226. *huffVal++ = (BYTE)h[j];
  227. }
  228. huffBit = param->huffBits[AC_TABLE_INDEX0];
  229. huffVal = param->huffVal[AC_TABLE_INDEX0];
  230. for (i=0; i<(16/8); i++) // Luma AC BitLength
  231. {
  232. if (getTblElement(fp, sLine) == 0)
  233. return 0;
  234. memset(h, 0x00, 8*sizeof(unsigned int));
  235. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  236. for (j=0; j<8; j++)
  237. *huffBit++ = (BYTE)h[j];
  238. }
  239. for (i=0; i<(max_ac/8); i++) // Luma AC HuffValue
  240. {
  241. if (getTblElement(fp, sLine) == 0)
  242. return 0;
  243. memset(h, 0x00, 8*sizeof(unsigned int));
  244. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  245. for (j=0; j<8; j++)
  246. *huffVal++ = (BYTE)h[j];
  247. }
  248. if (!prec){
  249. if (getTblElement(fp, sLine) == 0)
  250. return 0;
  251. memset(h, 0x00, 8*sizeof(unsigned int));
  252. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  253. for (j=0; j<2; j++)
  254. *huffVal++ = (BYTE)h[j];
  255. }
  256. huffBit = param->huffBits[DC_TABLE_INDEX1];
  257. huffVal = param->huffVal[DC_TABLE_INDEX1];
  258. for (i=0; i<(16/8); i++) // Chroma DC BitLength
  259. {
  260. if (getTblElement(fp, sLine) == 0)
  261. return 0;
  262. memset(h, 0x00, 8*sizeof(unsigned int));
  263. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  264. for (j=0; j<8; j++)
  265. *huffBit++ = (BYTE)h[j];
  266. }
  267. for (i=0; i<(16/8); i++) // Chroma DC HuffValue
  268. {
  269. if (getTblElement(fp, sLine) == 0)
  270. return 0;
  271. memset(h, 0x00, 8*sizeof(unsigned int));
  272. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  273. for (j=0; j<8; j++)
  274. *huffVal++ = (BYTE)h[j];
  275. }
  276. huffBit = param->huffBits[AC_TABLE_INDEX1];
  277. huffVal = param->huffVal[AC_TABLE_INDEX1];
  278. for (i=0; i<(16/8); i++) // Chroma AC BitLength
  279. {
  280. if (getTblElement(fp, sLine) == 0)
  281. return 0;
  282. memset(h, 0x00, 8*sizeof(unsigned int));
  283. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  284. for (j=0; j<8; j++)
  285. *huffBit++ = (BYTE)h[j];
  286. }
  287. for (i=0; i<(max_ac/8); i++) // Luma AC HuffValue
  288. {
  289. if (getTblElement(fp, sLine) == 0)
  290. return 0;
  291. memset(h, 0x00, 8*sizeof(unsigned int));
  292. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  293. for (j=0; j<8; j++)
  294. *huffVal++ = (BYTE)h[j];
  295. }
  296. if (!prec){
  297. if (getTblElement(fp, sLine) == 0)
  298. return 0;
  299. memset(h, 0x00, 8*sizeof(unsigned int));
  300. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  301. for (j=0; j<2; j++)
  302. *huffVal++ = (BYTE)h[j];
  303. }
  304. return 1;
  305. }
  306. static int parseQMatrix(FILE * fp, EncMjpgParam *param)
  307. {
  308. char sLine[256] = {0,};
  309. int i,j;
  310. unsigned int h[8] = {0,};
  311. short temp_dc[64], temp_ac[64];
  312. short *qTable_dc, *qTable_ac, *qTable;
  313. qTable = temp_dc;
  314. for (i=0; i < (64/8); i++)
  315. {
  316. if (getTblElement(fp, sLine) == 0)
  317. return 0;
  318. memset(h, 0x00, 8*sizeof(unsigned int));
  319. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  320. for (j=0; j<8; j++){
  321. if (param->lumaQ12bit == TRUE)
  322. *qTable++ = h[j] & 0xffff;
  323. else
  324. *qTable++ = h[j] & 0x00ff;
  325. }
  326. }
  327. qTable = temp_ac;
  328. for (i=0; i < (64/8); i++)
  329. {
  330. if (getTblElement(fp, sLine) == 0)
  331. return 0;
  332. memset(h, 0x00, 8*sizeof(unsigned int));
  333. sscanf(sLine, "%x %x %x %x %x %x %x %x", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7]);
  334. for (j=0; j<8; j++){
  335. if (param->chromaQ12bit == TRUE)
  336. *qTable++ = h[j] & 0xffff;
  337. else
  338. *qTable++ = h[j] & 0x00ff;
  339. }
  340. }
  341. qTable_dc = param->qMatTab[DC_TABLE_INDEX0];
  342. qTable_ac = param->qMatTab[AC_TABLE_INDEX0];
  343. for (i=0; i < 64; i++)
  344. {
  345. qTable_dc[InvScanTable[i]] = temp_dc[i];
  346. qTable_ac[InvScanTable[i]] = temp_ac[i];
  347. }
  348. memcpy(param->qMatTab[DC_TABLE_INDEX1], param->qMatTab[DC_TABLE_INDEX0], 64*sizeof(short));
  349. memcpy(param->qMatTab[AC_TABLE_INDEX1], param->qMatTab[AC_TABLE_INDEX0], 64*sizeof(short));
  350. return 1;
  351. }
  352. int jpgGetHuffTable(char *huffFileName, EncMjpgParam *param, int prec)
  353. {
  354. char huffFilePath[256];
  355. FILE *huffFp = NULL;
  356. if (huffFileName[0] != 0) {
  357. strcpy(huffFilePath, huffFileName);
  358. huffFp = fopen(huffFilePath, "rt");
  359. if (huffFp == NULL) {
  360. JLOG( ERR, "Can't open Huffman Table file %s \n", huffFilePath);
  361. return 0;
  362. }
  363. parseHuffmanTable(huffFp, param, prec);
  364. fclose(huffFp);
  365. }
  366. else {
  367. if (prec){
  368. memcpy(param->huffBits[DC_TABLE_INDEX0], lumaDcBits_ES, 16); // Luma DC BitLength
  369. memcpy(param->huffVal[DC_TABLE_INDEX0], lumaDcValue_ES, 16); // Luma DC HuffValue
  370. memcpy(param->huffBits[AC_TABLE_INDEX0], lumaAcBits_ES, 16); // Luma DC BitLength
  371. memcpy(param->huffVal[AC_TABLE_INDEX0], lumaAcValue_ES, 256); // Luma DC HuffValue
  372. memcpy(param->huffBits[DC_TABLE_INDEX1], chromaDcBits_ES, 16); // Chroma DC BitLength
  373. memcpy(param->huffVal[DC_TABLE_INDEX1], chromaDcValue_ES, 16); // Chroma DC HuffValue
  374. memcpy(param->huffBits[AC_TABLE_INDEX1], chromaAcBits_ES, 16); // Chroma AC BitLength
  375. memcpy(param->huffVal[AC_TABLE_INDEX1], chromaAcValue_ES, 256); // Chorma AC HuffValue
  376. } else {
  377. memcpy(param->huffBits[DC_TABLE_INDEX0], lumaDcBits, 16); // Luma DC BitLength
  378. memcpy(param->huffVal[DC_TABLE_INDEX0], lumaDcValue, 16); // Luma DC HuffValue
  379. memcpy(param->huffBits[AC_TABLE_INDEX0], lumaAcBits, 16); // Luma DC BitLength
  380. memcpy(param->huffVal[AC_TABLE_INDEX0], lumaAcValue, 162); // Luma DC HuffValue
  381. memcpy(param->huffBits[DC_TABLE_INDEX1], chromaDcBits, 16); // Chroma DC BitLength
  382. memcpy(param->huffVal[DC_TABLE_INDEX1], chromaDcValue, 16); // Chroma DC HuffValue
  383. memcpy(param->huffBits[AC_TABLE_INDEX1], chromaAcBits, 16); // Chroma AC BitLength
  384. memcpy(param->huffVal[AC_TABLE_INDEX1], chromaAcValue, 162); // Chorma AC HuffValue
  385. }
  386. }
  387. return 1;
  388. }
  389. int jpgGetQMatrix(char *qMatFileName, EncMjpgParam *param)
  390. {
  391. char qMatFilePath[256];
  392. FILE *qMatFp = NULL;
  393. int i;
  394. if ( qMatFileName[0] != 0 ) {
  395. strcpy(qMatFilePath, qMatFileName);
  396. qMatFp = fopen(qMatFilePath, "rt" );
  397. if ( !qMatFp ) {
  398. JLOG(ERR, "Can't open Q Matrix file %s \n", qMatFilePath);
  399. return 0;
  400. }
  401. parseQMatrix(qMatFp, param);
  402. fclose(qMatFp);
  403. }
  404. else {
  405. // Rearrange and insert pre-defined Q-matrix to deticated variable.
  406. for (i=0; i<64; i++){
  407. param->qMatTab[DC_TABLE_INDEX0][i] = lumaQ2[i] & 0x00ff;
  408. param->qMatTab[DC_TABLE_INDEX1][i] = lumaQ2[i] & 0x00ff;
  409. param->qMatTab[AC_TABLE_INDEX0][i] = chromaBQ2[i] & 0x00ff;
  410. param->qMatTab[AC_TABLE_INDEX1][i] = chromaBQ2[i] & 0x00ff;
  411. }
  412. }
  413. if (param->extendedSequence == TRUE) {
  414. /* CODAJ12 Constraint: The value of DC quantization should be greater or equal than 8. */
  415. /* FIXME: It might be wrong naming of DC_TABLE_INDEX and AC_TABLE_INDEX */
  416. if (param->qMatTab[DC_TABLE_INDEX0][0] < 8) {
  417. param->qMatTab[DC_TABLE_INDEX0][0] = 8;
  418. }
  419. if (param->qMatTab[AC_TABLE_INDEX0][0] < 8) {
  420. param->qMatTab[AC_TABLE_INDEX0][0] = 8;
  421. }
  422. if (param->qMatTab[DC_TABLE_INDEX1][0] < 8) {
  423. param->qMatTab[DC_TABLE_INDEX1][0] = 8;
  424. }
  425. if (param->qMatTab[AC_TABLE_INDEX1][0] < 8) {
  426. param->qMatTab[AC_TABLE_INDEX1][0] = 8;
  427. }
  428. }
  429. return 1;
  430. }
  431. /******************************************************************************
  432. EncOpenParam Initialization
  433. ******************************************************************************/
  434. int getJpgEncOpenParamDefault(JpgEncOpenParam *pEncOP, EncConfigParam *pEncConfig)
  435. {
  436. int ret;
  437. EncMjpgParam mjpgParam;
  438. memset(&mjpgParam, 0x00, sizeof(EncMjpgParam));
  439. pEncOP->jpg12bit = pEncConfig->extendedSequential;
  440. pEncOP->q_prec0 = FALSE;
  441. pEncOP->q_prec1 = FALSE;
  442. pEncOP->picWidth = pEncConfig->picWidth;
  443. pEncOP->picHeight = pEncConfig->picHeight;
  444. pEncOP->tiledModeEnable = pEncConfig->tiledModeEnable;
  445. pEncOP->sourceFormat = pEncConfig->sourceSubsample;
  446. pEncOP->restartInterval = 0;
  447. pEncOP->chromaInterleave = pEncConfig->chromaInterleave;
  448. pEncOP->packedFormat = pEncConfig->packedFormat;
  449. mjpgParam.sourceFormat = pEncConfig->sourceSubsample;
  450. ret = jpgGetHuffTable(pEncConfig->huffFileName, &mjpgParam, pEncOP->jpg12bit);
  451. if (ret == 0) return ret;
  452. ret = jpgGetQMatrix(pEncConfig->qMatFileName, &mjpgParam);
  453. if (ret == 0) return ret;
  454. memcpy(pEncOP->huffVal, mjpgParam.huffVal, 4*256);
  455. memcpy(pEncOP->huffBits, mjpgParam.huffBits, 4*256);
  456. memcpy(pEncOP->qMatTab, mjpgParam.qMatTab, 4*64*sizeof(short));
  457. //Currently only 2DC,2AC huffman table for 12-bit case
  458. //So, copy them to EX1 to EX2 to modeling rest 2 tables
  459. if (pEncOP->jpg12bit == TRUE){
  460. memcpy(&pEncOP->huffVal[4][0], &pEncOP->huffVal[0][0], 4*256);
  461. memcpy(&pEncOP->huffBits[4][0], &pEncOP->huffBits[0][0], 4*256);
  462. }
  463. return 1;
  464. }
  465. /**
  466. * To init EncOpenParam by CFG file
  467. * IN
  468. * EncConfigParam *pEncConfig
  469. * OUT
  470. * EncOpenParam *pEncOP
  471. * char *srcYuvFileName
  472. * @return TRUE on success, FALSE on failure.
  473. */
  474. BOOL GetJpgEncOpenParam(JpgEncOpenParam *pEncOP, EncConfigParam *pEncConfig)
  475. {
  476. ENC_CFG encCfg;
  477. Int32 ret;
  478. EncMjpgParam mjpgParam;
  479. char huffmanTablePath[MAX_FILE_PATH];
  480. char QMatTabPath[MAX_FILE_PATH];
  481. char cfgPath[MAX_FILE_PATH];
  482. char* srcYuvFileName = pEncConfig->yuvFileName;
  483. memset(&encCfg, 0x00, sizeof(ENC_CFG));
  484. memset(&mjpgParam, 0x00, sizeof(EncMjpgParam));
  485. encCfg.prec = pEncConfig->extendedSequential;
  486. snprintf(cfgPath, sizeof(cfgPath), "%s/%s", pEncConfig->strCfgDir, pEncConfig->cfgFileName);
  487. if (parseJpgCfgFile(&encCfg, cfgPath) == 0) {
  488. return FALSE;
  489. }
  490. if (strlen(srcYuvFileName) == 0) {
  491. strcpy(srcYuvFileName, encCfg.SrcFileName);
  492. }
  493. if (encCfg.FrmFormat == 0) {
  494. pEncConfig->chromaInterleave= CBCR_SEPARATED;
  495. }
  496. else if (encCfg.FrmFormat == 1) {
  497. pEncConfig->chromaInterleave = CBCR_INTERLEAVE;
  498. }
  499. else if (encCfg.FrmFormat == 2) {
  500. pEncConfig->chromaInterleave = CRCB_INTERLEAVE;
  501. }
  502. else {
  503. pEncConfig->chromaInterleave = CBCR_SEPARATED;
  504. }
  505. pEncConfig->mjpgFramerate = encCfg.FrameRate;
  506. /* Source format */
  507. pEncConfig->packedFormat = (PackedFormat)(encCfg.FrmFormat - 2);
  508. if (pEncConfig->packedFormat >= PACKED_FORMAT_MAX)
  509. pEncConfig->packedFormat = PACKED_FORMAT_NONE;
  510. pEncConfig->outNum = encCfg.NumFrame;
  511. if (pEncConfig->picWidth == 0) pEncConfig->picWidth = encCfg.PicX;
  512. if (pEncConfig->picHeight == 0) pEncConfig->picHeight = encCfg.PicY;
  513. if (pEncOP) {
  514. pEncOP->q_prec0 = encCfg.QMatPrec0;
  515. pEncOP->q_prec1 = encCfg.QMatPrec1;
  516. pEncOP->picWidth = encCfg.PicX;
  517. pEncOP->picHeight = encCfg.PicY;
  518. pEncOP->chromaInterleave = pEncConfig->chromaInterleave;
  519. pEncOP->packedFormat = pEncConfig->packedFormat;
  520. pEncOP->restartInterval = encCfg.RstIntval;
  521. pEncOP->tiledModeEnable = pEncConfig->tiledModeEnable;
  522. pEncOP->sourceFormat = encCfg.SrcFormat;
  523. mjpgParam.sourceFormat = encCfg.SrcFormat;
  524. pEncOP->jpg12bit = pEncConfig->extendedSequential;
  525. if (pEncOP->jpg12bit) {
  526. mjpgParam.lumaQ12bit = (BOOL)encCfg.QMatPrec0;
  527. mjpgParam.chromaQ12bit = (BOOL)encCfg.QMatPrec1;
  528. mjpgParam.extendedSequence = TRUE;
  529. }
  530. snprintf(huffmanTablePath, sizeof(huffmanTablePath), "%s/%s", pEncConfig->strCfgDir, encCfg.HuffTabName);
  531. ret = jpgGetHuffTable(huffmanTablePath, &mjpgParam, pEncOP->jpg12bit);
  532. if (ret == 0) {
  533. return ret;
  534. }
  535. snprintf(QMatTabPath, sizeof(QMatTabPath), "%s/%s", pEncConfig->strCfgDir, encCfg.QMatTabName);
  536. if ((ret=jpgGetQMatrix(QMatTabPath, &mjpgParam)) == FALSE) {
  537. return ret;
  538. }
  539. memcpy(pEncOP->huffVal, mjpgParam.huffVal, 4*256);
  540. memcpy(pEncOP->huffBits, mjpgParam.huffBits, 4*256);
  541. memcpy(pEncOP->qMatTab, mjpgParam.qMatTab, 4*64*sizeof(short));
  542. //Currently only 2DC,2AC huffman table for 12-bit case
  543. //So, copy them to EX1 to EX2 to modeling rest 2 tables
  544. if (pEncOP->jpg12bit){
  545. memcpy(&pEncOP->huffVal[4][0], &pEncOP->huffVal[0][0], 4*256);
  546. memcpy(&pEncOP->huffBits[4][0], &pEncOP->huffBits[0][0], 4*256);
  547. }
  548. }
  549. return TRUE;
  550. }
  551. //------------------------------------------------------------------------------
  552. // ENCODE PARAMETER PARSE FUNCSIONS
  553. //------------------------------------------------------------------------------
  554. // Parameter parsing helper
  555. static int GetValue(FILE *fp, char *para, char *value)
  556. {
  557. char lineStr[256];
  558. char paraStr[256];
  559. fseek(fp, 0, SEEK_SET);
  560. while (1) {
  561. if (fgets(lineStr, 256, fp) == NULL)
  562. return 0;
  563. sscanf(lineStr, "%s %s", paraStr, value);
  564. if (paraStr[0] != ';') {
  565. if (strcmp(para, paraStr) == 0)
  566. return 1;
  567. }
  568. }
  569. }
  570. int parseJpgCfgFile(ENC_CFG *pEncCfg, char *FileName)
  571. {
  572. FILE *Fp;
  573. char sLine[256] = {0,};
  574. int res = 0;
  575. Fp = fopen(FileName, "rt");
  576. if (Fp == NULL) {
  577. fprintf(stderr, " > ERROR: File not exist <%s>, %d\n", FileName, errno);
  578. goto end;
  579. }
  580. // source file name
  581. if (GetValue(Fp, "YUV_SRC_IMG", sLine) == 0)
  582. goto end;
  583. sscanf(sLine, "%s", (char *)pEncCfg->SrcFileName);
  584. // frame format
  585. // ; 0-planar, 1-NV12,NV16(CbCr interleave) 2-NV21,NV61(CbCr alternative)
  586. // ; 3-YUYV, 4-UYVY, 5-YVYU, 6-VYUY, 7-YUV packed (444 only)
  587. if (GetValue(Fp, "FRAME_FORMAT", sLine) == 0)
  588. goto end;
  589. sscanf(sLine, "%d", &pEncCfg->FrmFormat);
  590. // width
  591. if (GetValue(Fp, "PICTURE_WIDTH", sLine) == 0)
  592. goto end;
  593. sscanf(sLine, "%d", &pEncCfg->PicX);
  594. // height
  595. if (GetValue(Fp, "PICTURE_HEIGHT", sLine) == 0)
  596. goto end;
  597. sscanf(sLine, "%d", &pEncCfg->PicY);
  598. // frame_rate
  599. if (GetValue(Fp, "FRAME_RATE", sLine) == 0)
  600. goto end;
  601. {
  602. double frameRate;
  603. int timeRes, timeInc;
  604. frameRate = (double)(int)atoi(sLine);
  605. timeInc = 1;
  606. while ((int)frameRate != frameRate) {
  607. timeInc *= 10;
  608. frameRate *= 10;
  609. }
  610. timeRes = (int) frameRate;
  611. // divide 2 or 5
  612. if (timeInc%2 == 0 && timeRes%2 == 0) {
  613. timeInc /= 2;
  614. timeRes /= 2;
  615. }
  616. if (timeInc%5 == 0 && timeRes%5 == 0) {
  617. timeInc /= 5;
  618. timeRes /= 5;
  619. }
  620. if (timeRes == 2997 && timeInc == 100) {
  621. timeRes = 30000;
  622. timeInc = 1001;
  623. }
  624. pEncCfg->FrameRate = (timeInc - 1) << 16;
  625. pEncCfg->FrameRate |= timeRes;
  626. }
  627. // frame count
  628. if (GetValue(Fp, "FRAME_NUMBER_ENCODED", sLine) == 0)
  629. goto end;
  630. sscanf(sLine, "%d", &pEncCfg->NumFrame);
  631. if (GetValue(Fp, "VERSION_ID", sLine) == 0)
  632. goto end;
  633. sscanf(sLine, "%d", &pEncCfg->VersionID);
  634. if (GetValue(Fp, "RESTART_INTERVAL", sLine) == 0)
  635. goto end;
  636. sscanf(sLine, "%d", &pEncCfg->RstIntval);
  637. if (GetValue(Fp, "IMG_FORMAT", sLine) == 0)
  638. goto end;
  639. sscanf(sLine, "%d", (Int32*)&pEncCfg->SrcFormat);
  640. if (GetValue(Fp, "QMATRIX_TABLE", sLine) == 0)
  641. goto end;
  642. sscanf(sLine, "%s", (char *)pEncCfg->QMatTabName);
  643. if (GetValue(Fp, "QMATRIX_PREC0", sLine) == 0)
  644. pEncCfg->QMatPrec0 = 0;
  645. else
  646. sscanf(sLine, "%d", &pEncCfg->QMatPrec0);
  647. if (GetValue(Fp, "QMATRIX_PREC1", sLine) == 0)
  648. pEncCfg->QMatPrec1 = 0;
  649. else
  650. sscanf(sLine, "%d", &pEncCfg->QMatPrec1);
  651. if (GetValue(Fp, "HUFFMAN_TABLE", sLine) == 0)
  652. goto end;
  653. sscanf(sLine, "%s", (char *)pEncCfg->HuffTabName);
  654. res = 1;
  655. end:
  656. if(Fp)
  657. fclose(Fp);
  658. return res;
  659. }
  660. int StoreYuvImageBurstFormat_V20(int chromaStride, Uint8 * dst, int picWidth, int picHeight, Uint32 bitDepth,
  661. Uint64 addrY, Uint64 addrCb, Uint64 addrCr, Uint32 stride, FrameFormat format, int endian, CbCrInterLeave interLeave, PackedFormat packed)
  662. {
  663. int size;
  664. int y, nY = 0, nCb, nCr;
  665. Uint64 addr;
  666. int lumaSize, chromaSize = 0, chromaWidth = 0, chromaHeight =0;
  667. Uint8 * puc;
  668. int chromaStride_i = 0;
  669. Uint32 bytesPerPixel = (bitDepth + 7) / 8;
  670. chromaStride_i = chromaStride;
  671. switch (format)
  672. {
  673. case FORMAT_420:
  674. nY = picHeight;
  675. nCb = nCr = picHeight / 2;
  676. chromaSize = (picWidth/2) * (picHeight/2);
  677. chromaWidth = picWidth/2;
  678. chromaHeight = nY;
  679. break;
  680. case FORMAT_440:
  681. nY = picHeight;
  682. nCb = nCr = picHeight / 2;
  683. chromaSize = (picWidth) * (picHeight / 2);
  684. chromaWidth = picWidth;
  685. chromaHeight = nY;
  686. break;
  687. case FORMAT_422:
  688. nY = picHeight;
  689. nCb = nCr = picHeight;
  690. chromaSize = (picWidth/2) * picHeight ;
  691. chromaWidth = (picWidth/2);
  692. chromaHeight = nY*2;
  693. break;
  694. case FORMAT_444:
  695. nY = picHeight;
  696. nCb = nCr = picHeight;
  697. chromaSize = picWidth * picHeight;
  698. chromaWidth = picWidth;
  699. chromaHeight = nY*2;
  700. break;
  701. case FORMAT_400:
  702. nY = picHeight;
  703. nCb = nCr = 0;
  704. chromaSize = 0;
  705. chromaWidth = 0;
  706. chromaHeight = 0;
  707. break;
  708. default:
  709. return 0;
  710. }
  711. puc = dst;
  712. addr = addrY;
  713. if (packed)
  714. {
  715. if (packed == PACKED_FORMAT_444)
  716. picWidth *= 3;
  717. else
  718. picWidth *= 2;
  719. chromaSize = 0;
  720. }
  721. lumaSize = picWidth * nY;
  722. size = lumaSize + chromaSize*2;
  723. lumaSize *= bytesPerPixel;
  724. chromaSize *= bytesPerPixel;
  725. size *= bytesPerPixel;
  726. picWidth *= bytesPerPixel;
  727. chromaWidth *= bytesPerPixel;
  728. if (interLeave){
  729. chromaSize = chromaSize *2;
  730. chromaWidth = chromaWidth *2;
  731. chromaStride_i = chromaStride_i;
  732. }
  733. if ((picWidth == stride) && (chromaWidth == chromaStride_i))
  734. {
  735. JpuReadMem(addr, (Uint8 *)( puc ), lumaSize, endian);
  736. if(packed)
  737. return size;
  738. if (interLeave)
  739. {
  740. puc = dst + lumaSize;
  741. addr = addrCb;
  742. JpuReadMem(addr, (Uint8 *)(puc), chromaSize, endian);
  743. }
  744. else
  745. {
  746. puc = dst + lumaSize;
  747. addr = addrCb;
  748. JpuReadMem(addr, (Uint8 *)(puc), chromaSize, endian);
  749. puc = dst + lumaSize + chromaSize;
  750. addr = addrCr;
  751. JpuReadMem(addr, (Uint8 *)(puc), chromaSize, endian);
  752. }
  753. }
  754. else
  755. {
  756. for (y = 0; y < nY; ++y) {
  757. JpuReadMem(addr + stride * y, (Uint8 *)(puc + y * picWidth), picWidth, endian);
  758. }
  759. if(packed)
  760. return size;
  761. if (interLeave)
  762. {
  763. puc = dst + lumaSize;
  764. addr = addrCb;
  765. for (y = 0; y < (chromaHeight/2); ++y) {
  766. JpuReadMem(addr + (chromaStride_i)*y, (Uint8 *)(puc + y*(chromaWidth)), (chromaWidth), endian);
  767. }
  768. }
  769. else
  770. {
  771. puc = dst + lumaSize;
  772. addr = addrCb;
  773. for (y = 0; y < nCb; ++y) {
  774. JpuReadMem(addr + chromaStride_i * y, (Uint8 *)(puc + y * chromaWidth), chromaWidth, endian);
  775. }
  776. puc = dst + lumaSize + chromaSize;
  777. addr = addrCr;
  778. for (y = 0; y < nCr; ++y) {
  779. JpuReadMem(addr + chromaStride_i * y, (Uint8 *)(puc + y * chromaWidth), chromaWidth, endian);
  780. }
  781. }
  782. }
  783. return size;
  784. }//lint !e429
  785. void GetMcuUnitSize(int format, int *mcuWidth, int *mcuHeight)
  786. {
  787. switch(format)
  788. {
  789. case FORMAT_420:
  790. *mcuWidth = 16;
  791. *mcuHeight = 16;
  792. break;
  793. case FORMAT_422:
  794. *mcuWidth = 16;
  795. *mcuHeight = 8;
  796. break;
  797. case FORMAT_440:
  798. *mcuWidth = 8;
  799. *mcuHeight = 16;
  800. break;
  801. default://FORMAT_444,400
  802. *mcuWidth = 8;
  803. *mcuHeight = 8;
  804. break;
  805. }
  806. }
  807. int GetFrameBufSize(int framebufFormat, int picWidth, int picHeight)
  808. {
  809. int framebufSize = 0;
  810. int framebufWidth, framebufHeight;
  811. framebufWidth = picWidth;
  812. framebufHeight = picHeight;
  813. switch (framebufFormat)
  814. {
  815. case FORMAT_420:
  816. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + ((framebufWidth+1)/2)*((framebufHeight+1)/2)*2;
  817. break;
  818. case FORMAT_440:
  819. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + framebufWidth*((framebufHeight+1)/2)*2;
  820. break;
  821. case FORMAT_422:
  822. framebufSize = framebufWidth * framebufHeight + ((framebufWidth+1)/2)*framebufHeight*2;
  823. break;
  824. case FORMAT_444:
  825. framebufSize = framebufWidth * framebufHeight * 3;
  826. break;
  827. case FORMAT_400:
  828. framebufSize = framebufWidth * framebufHeight;
  829. break;
  830. }
  831. framebufSize = ((framebufSize+7)&~7);
  832. return framebufSize;
  833. }
  834. STATIC void GetFrameBufStride(FrameFormat subsample, CbCrInterLeave cbcrIntlv, PackedFormat packed, BOOL scalerOn,
  835. Uint32 width, Uint32 height, Uint32 bytePerPixel,
  836. Uint32* oLumaStride, Uint32* oLumaHeight, Uint32* oChromaStride, Uint32* oChromaHeight)
  837. {
  838. Uint32 lStride, cStride;
  839. Uint32 lHeight, cHeight;
  840. lStride = JPU_CEIL(8, width);
  841. lHeight = height;
  842. cHeight = height/2;
  843. if (packed == PACKED_FORMAT_NONE) {
  844. Uint32 chromaDouble = (cbcrIntlv == CBCR_SEPARATED) ? 1 : 2;
  845. switch (subsample) {
  846. case FORMAT_400: cStride = 0; cHeight = 0; break;
  847. case FORMAT_420: cStride = (lStride/2)*chromaDouble; cHeight = height/2; break;
  848. case FORMAT_422: cStride = (lStride/2)*chromaDouble; cHeight = height; break;
  849. case FORMAT_440: cStride = lStride*chromaDouble; cHeight = height/2; break;
  850. case FORMAT_444: cStride = lStride*chromaDouble; cHeight = height; break;
  851. default: cStride = 0; lStride = 0; cHeight = 0; break;
  852. }
  853. }
  854. else {
  855. switch (packed) {
  856. case PACKED_FORMAT_422_YUYV:
  857. case PACKED_FORMAT_422_UYVY:
  858. case PACKED_FORMAT_422_YVYU:
  859. case PACKED_FORMAT_422_VYUY: lStride = lStride*2; cStride = 0; cHeight = 0; break;
  860. case PACKED_FORMAT_444: lStride = lStride*3; cStride = 0; cHeight = 0; break;
  861. default: lStride = 0; cStride = 0; break;
  862. }
  863. }
  864. if (scalerOn == TRUE) {
  865. /* Luma stride */
  866. if (subsample == FORMAT_420 || subsample == FORMAT_422 || (PACKED_FORMAT_422_YUYV <= packed && packed <= PACKED_FORMAT_422_VYUY)) {
  867. lStride = JPU_CEIL(32, lStride);
  868. }
  869. else {
  870. lStride = JPU_CEIL(16, lStride);
  871. }
  872. /* Chroma stride */
  873. if (cbcrIntlv == CBCR_SEPARATED) {
  874. if (subsample == FORMAT_444) {
  875. cStride = JPU_CEIL(16, cStride);
  876. }
  877. else {
  878. cStride = JPU_CEIL(8, cStride);
  879. }
  880. }
  881. else {
  882. cStride = JPU_CEIL(32, cStride);
  883. }
  884. }
  885. else {
  886. lStride = JPU_CEIL(8, lStride);
  887. if (subsample == FORMAT_420 || subsample == FORMAT_422) {
  888. cStride = JPU_CEIL(16, cStride);
  889. }
  890. else {
  891. cStride = JPU_CEIL(8, cStride);
  892. }
  893. }
  894. lHeight = JPU_CEIL(8, lHeight);
  895. cHeight = JPU_CEIL(8, cHeight);
  896. lStride *= bytePerPixel;
  897. cStride *= bytePerPixel;
  898. if (oLumaStride) *oLumaStride = lStride;
  899. if (oLumaHeight) *oLumaHeight = lHeight;
  900. if (oChromaStride) *oChromaStride = cStride;
  901. if (oChromaHeight) *oChromaHeight = cHeight;
  902. }
  903. BOOL AllocateFrameBuffer(Uint32 instIdx, FrameFormat subsample, CbCrInterLeave cbcrIntlv, PackedFormat packed,
  904. Uint32 rotation, BOOL scalerOn, Uint32 width, Uint32 height, Uint32 bitDepth, Uint32 num)
  905. {
  906. fb_context *fb;
  907. Uint32 fbLumaStride, fbLumaHeight, fbChromaStride, fbChromaHeight;
  908. Uint32 fbLumaSize, fbChromaSize, fbSize;
  909. Uint32 i;
  910. Uint32 bytePerPixel = (bitDepth + 7)/8;
  911. if (rotation == 90 || rotation == 270) {
  912. if (subsample == FORMAT_422) subsample = FORMAT_440;
  913. else if (subsample == FORMAT_440) subsample = FORMAT_422;
  914. }
  915. GetFrameBufStride(subsample, cbcrIntlv, packed, scalerOn, width, height, bytePerPixel, &fbLumaStride, &fbLumaHeight, &fbChromaStride, &fbChromaHeight);
  916. fbLumaSize = fbLumaStride * fbLumaHeight;
  917. fbChromaSize = fbChromaStride * fbChromaHeight;
  918. if (cbcrIntlv == CBCR_SEPARATED) {
  919. /* fbChromaSize MUST be zero when format is packed mode */
  920. fbSize = fbLumaSize + 2*fbChromaSize;
  921. }
  922. else {
  923. /* Semi-planar */
  924. fbSize = fbLumaSize + fbChromaSize;
  925. }
  926. fb = &s_fb[instIdx];
  927. if (fb->vb_base.size == 0) {
  928. fb->vb_base.size = fbSize;
  929. fb->vb_base.size *= num;
  930. if (jdi_allocate_dma_memory(&fb->vb_base) < 0) {
  931. JLOG(ERR, "Fail to allocate frame buffer size=%d\n", fb->vb_base.size);
  932. return FALSE;
  933. }
  934. fb->last_addr = fb->vb_base.phys_addr;
  935. }
  936. for (i=fb->last_num; i<fb->last_num+num; i++) {
  937. fb->frameBuf[i].Format = subsample;
  938. fb->frameBuf[i].Index = i;
  939. fb->frameBuf[i].vbY.phys_addr = fb->last_addr;
  940. fb->frameBuf[i].vbY.size = fbLumaSize;
  941. fb->last_addr += fb->frameBuf[i].vbY.size;
  942. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  943. if (fbChromaSize) {
  944. fb->frameBuf[i].vbCb.phys_addr = fb->last_addr;
  945. fb->frameBuf[i].vbCb.size = fbChromaSize;
  946. fb->last_addr += fb->frameBuf[i].vbCb.size;
  947. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  948. fb->frameBuf[i].vbCr.phys_addr = (cbcrIntlv == CBCR_SEPARATED) ? fb->last_addr : 0;
  949. fb->frameBuf[i].vbCr.size = (cbcrIntlv == CBCR_SEPARATED) ? fbChromaSize : 0;
  950. fb->last_addr += fb->frameBuf[i].vbCr.size;
  951. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  952. }
  953. fb->frameBuf[i].strideY = fbLumaStride;
  954. fb->frameBuf[i].strideC = fbChromaStride;
  955. }
  956. fb->last_num += num;
  957. return TRUE;
  958. }
  959. BOOL UpdateFrameBuffers(Uint32 instIdx, Uint32 num, FRAME_BUF *frameBuf)
  960. {
  961. fb_context *fb = &s_fb[instIdx];
  962. Uint32 index = num - 1;
  963. fb->last_num = num;
  964. fb->last_addr = frameBuf[index].vbY.phys_addr;
  965. fb->last_addr += fb->frameBuf[index].vbCb.size;
  966. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  967. fb->last_addr += fb->frameBuf[index].vbCr.size;
  968. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  969. JLOG(INFO, "%s: Update %d buffers\r\n",__FUNCTION__, num);
  970. memcpy(fb->frameBuf, frameBuf, sizeof(FRAME_BUF) * num);
  971. return TRUE;
  972. }
  973. BOOL AttachOneFrameBuffer(Uint32 instIdx, FrameFormat subsample, CbCrInterLeave cbcrIntlv, PackedFormat packed,
  974. Uint32 rotation, BOOL scalerOn, Uint32 width, Uint32 height, Uint32 bitDepth,
  975. void *virtAddress, Uint32 size, Uint32 *bufferIndex)
  976. {
  977. fb_context *fb;
  978. Uint32 fbLumaStride, fbLumaHeight, fbChromaStride, fbChromaHeight;
  979. Uint32 fbLumaSize, fbChromaSize;
  980. Uint32 fbSize;
  981. Uint32 bytePerPixel = (bitDepth + 7)/8;
  982. Uint32 i;
  983. JLOG(INFO, "%s function in width, height = [%d, %d]\r\n", __FUNCTION__, width, height);
  984. if (rotation == 90 || rotation == 270) {
  985. if (subsample == FORMAT_422) subsample = FORMAT_440;
  986. else if (subsample == FORMAT_440) subsample = FORMAT_422;
  987. }
  988. GetFrameBufStride(subsample, cbcrIntlv, packed, scalerOn, width, height, bytePerPixel, &fbLumaStride, &fbLumaHeight, &fbChromaStride, &fbChromaHeight);
  989. fbLumaSize = fbLumaStride * fbLumaHeight;
  990. fbChromaSize = fbChromaStride * fbChromaHeight;
  991. if (cbcrIntlv == CBCR_SEPARATED) {
  992. /* fbChromaSize MUST be zero when format is packed mode */
  993. fbSize = fbLumaSize + 2*fbChromaSize;
  994. }
  995. else {
  996. /* Semi-planar */
  997. fbSize = fbLumaSize + fbChromaSize;
  998. }
  999. fb = &s_fb[instIdx];
  1000. fb->vb_base.virt_addr = (unsigned long)virtAddress;
  1001. fb->vb_base.size = fbSize;
  1002. if (jdi_attach_dma_memory(&fb->vb_base) < 0) {
  1003. JLOG(ERR, "Fail to attach frame buffer\n");
  1004. return FALSE;
  1005. }
  1006. fb->last_addr = fb->vb_base.phys_addr;
  1007. i = fb->last_num;
  1008. JLOG(INFO, "%s: store on index %d\r\n", __FUNCTION__, i);
  1009. fb->frameBuf[i].Format = subsample;
  1010. fb->frameBuf[i].Index = i;
  1011. fb->frameBuf[i].vbY.phys_addr = fb->vb_base.phys_addr;
  1012. fb->frameBuf[i].vbY.size = fbLumaSize;
  1013. // Store virt addr
  1014. fb->frameBuf[i].vbY.virt_addr = fb->vb_base.virt_addr;
  1015. fb->last_addr += fb->frameBuf[i].vbY.size;
  1016. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1017. JLOG(INFO, "%s: fbChromaSize = %d, fbLumaSize = %d\r\n", __FUNCTION__, fbChromaSize, fbLumaSize);
  1018. if (fbChromaSize) {
  1019. fb->frameBuf[i].vbCb.phys_addr = fb->last_addr;
  1020. fb->frameBuf[i].vbCb.size = fbChromaSize;
  1021. fb->last_addr += fb->frameBuf[i].vbCb.size;
  1022. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1023. fb->frameBuf[i].vbCr.phys_addr = (cbcrIntlv == CBCR_SEPARATED) ? fb->last_addr : 0;
  1024. fb->frameBuf[i].vbCr.size = (cbcrIntlv == CBCR_SEPARATED) ? fbChromaSize : 0;
  1025. fb->last_addr += fb->frameBuf[i].vbCr.size;
  1026. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1027. }
  1028. JLOG(INFO, "%s vbCb = %x size = %d, vbCr = %x size = %d\r\n", __FUNCTION__, fb->frameBuf[i].vbCb.phys_addr,
  1029. fb->frameBuf[i].vbCb.size, fb->frameBuf[i].vbCr.phys_addr, fb->frameBuf[i].vbCr.size);
  1030. fb->frameBuf[i].strideY = fbLumaStride;
  1031. fb->frameBuf[i].strideC = fbChromaStride;
  1032. *bufferIndex = fb->last_num;
  1033. fb->last_num += 1;
  1034. JLOG(INFO, "%s function OUT, number = %d\r\n", __FUNCTION__, fb->last_num);
  1035. return TRUE;
  1036. }
  1037. void *AllocateOneFrameBuffer(Uint32 instIdx, FrameFormat subsample, CbCrInterLeave cbcrIntlv, PackedFormat packed,
  1038. Uint32 rotation, BOOL scalerOn, Uint32 width, Uint32 height, Uint32 bitDepth, Uint32 *bufferIndex)
  1039. {
  1040. fb_context *fb;
  1041. Uint32 fbLumaStride, fbLumaHeight, fbChromaStride, fbChromaHeight;
  1042. Uint32 fbLumaSize, fbChromaSize, fbSize;
  1043. Uint32 i;
  1044. Uint32 bytePerPixel = (bitDepth + 7)/8;
  1045. void * virt_addr = NULL;
  1046. JLOG(INFO, "%s function in width, height = [%d, %d]\r\n", __FUNCTION__, width, height);
  1047. if (rotation == 90 || rotation == 270) {
  1048. if (subsample == FORMAT_422) subsample = FORMAT_440;
  1049. else if (subsample == FORMAT_440) subsample = FORMAT_422;
  1050. }
  1051. GetFrameBufStride(subsample, cbcrIntlv, packed, scalerOn, width, height, bytePerPixel, &fbLumaStride, &fbLumaHeight, &fbChromaStride, &fbChromaHeight);
  1052. fbLumaSize = fbLumaStride * fbLumaHeight;
  1053. fbChromaSize = fbChromaStride * fbChromaHeight;
  1054. if (cbcrIntlv == CBCR_SEPARATED) {
  1055. /* fbChromaSize MUST be zero when format is packed mode */
  1056. fbSize = fbLumaSize + 2*fbChromaSize;
  1057. }
  1058. else {
  1059. /* Semi-planar */
  1060. fbSize = fbLumaSize + fbChromaSize;
  1061. }
  1062. if (fbSize == 0)
  1063. {
  1064. switch (subsample)
  1065. {
  1066. case FORMAT_420:
  1067. fbSize = width * height * 3 / 2;
  1068. break;
  1069. case FORMAT_422:
  1070. case FORMAT_440:
  1071. fbSize = width * height * 2;
  1072. case FORMAT_444:
  1073. fbSize = width * height * 3;
  1074. /* code */
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. JLOG(WARN, "%s: fbSize == 0! Use defalut size %d\r\n", __FUNCTION__, fbSize);
  1080. }
  1081. fb = &s_fb[instIdx];
  1082. fb->vb_base.size = fbSize;
  1083. if (jdi_allocate_dma_memory(&fb->vb_base) < 0) {
  1084. JLOG(ERR, "Fail to allocate frame buffer size=%d\n", fb->vb_base.size);
  1085. return NULL;
  1086. }
  1087. fb->last_addr = fb->vb_base.phys_addr;
  1088. i = fb->last_num;
  1089. JLOG(INFO, "%s: store on index %d\r\n", __FUNCTION__, i);
  1090. fb->frameBuf[i].Format = subsample;
  1091. fb->frameBuf[i].Index = i;
  1092. fb->frameBuf[i].vbY.phys_addr = fb->vb_base.phys_addr;
  1093. fb->frameBuf[i].vbY.size = fbLumaSize;
  1094. // Store virt addr
  1095. fb->frameBuf[i].vbY.virt_addr = fb->vb_base.virt_addr;
  1096. fb->last_addr += fb->frameBuf[i].vbY.size;
  1097. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1098. JLOG(INFO, "%s: fbChromaSize = %d, fbLumaSize = %d\r\n", __FUNCTION__, fbChromaSize, fbLumaSize);
  1099. if (fbChromaSize) {
  1100. fb->frameBuf[i].vbCb.phys_addr = fb->last_addr;
  1101. fb->frameBuf[i].vbCb.size = fbChromaSize;
  1102. fb->last_addr += fb->frameBuf[i].vbCb.size;
  1103. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1104. fb->frameBuf[i].vbCr.phys_addr = (cbcrIntlv == CBCR_SEPARATED) ? fb->last_addr : 0;
  1105. fb->frameBuf[i].vbCr.size = (cbcrIntlv == CBCR_SEPARATED) ? fbChromaSize : 0;
  1106. fb->last_addr += fb->frameBuf[i].vbCr.size;
  1107. fb->last_addr = JPU_CEIL(8, fb->last_addr);
  1108. }
  1109. fb->frameBuf[i].strideY = fbLumaStride;
  1110. fb->frameBuf[i].strideC = fbChromaStride;
  1111. *bufferIndex = fb->last_num;
  1112. fb->last_num += 1;
  1113. virt_addr = (void *)fb->vb_base.virt_addr;
  1114. JLOG(INFO, "%s function OUT, number = %d, return = %p\r\n", __FUNCTION__, fb->last_num, virt_addr);
  1115. return virt_addr;
  1116. }
  1117. int GetDPBBufSize(int framebufFormat, int picWidth, int picHeight, int picWidth_C, int interleave)
  1118. {
  1119. int framebufSize = 0;
  1120. int framebufWidth, framebufHeight, framebufWidth_C;
  1121. framebufWidth = picWidth;
  1122. framebufHeight = picHeight;
  1123. framebufWidth_C = picWidth_C;
  1124. switch (framebufFormat)
  1125. {
  1126. case FORMAT_420:
  1127. if (interleave == 0)
  1128. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + framebufWidth_C *((framebufHeight+1)/2)*2;
  1129. else
  1130. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + framebufWidth_C *((framebufHeight+1)/2);
  1131. break;
  1132. case FORMAT_440:
  1133. if (interleave == 0)
  1134. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + framebufWidth_C*((framebufHeight+1)/2)*2 *2;
  1135. else
  1136. framebufSize = framebufWidth * ((framebufHeight+1)/2*2) + framebufWidth_C*((framebufHeight+1)/2)*2;
  1137. break;
  1138. case FORMAT_422:
  1139. if (interleave == 0)
  1140. framebufSize = framebufWidth * framebufHeight + framebufWidth_C*framebufHeight*2;
  1141. else
  1142. framebufSize = framebufWidth * framebufHeight + framebufWidth_C*framebufHeight;
  1143. break;
  1144. case FORMAT_444:
  1145. framebufSize = framebufWidth * framebufHeight * 3;
  1146. break;
  1147. case FORMAT_400:
  1148. framebufSize = framebufWidth * framebufHeight;
  1149. if (interleave != 0)
  1150. printf("Warning: 400 does not have interleave mode ! \n" );
  1151. break;
  1152. }
  1153. framebufSize = ((framebufSize+7)&~7);
  1154. return framebufSize;
  1155. }
  1156. BOOL ParseDecTestLongArgs(void* config, const char* argName, char* value)
  1157. {
  1158. BOOL ret = TRUE;
  1159. DecConfigParam* dec = (DecConfigParam*)config;
  1160. if (strcmp(argName, "output") == 0) {
  1161. strcpy(dec->yuvFileName, value);
  1162. }
  1163. else if (strcmp(argName, "input") == 0) {
  1164. strcpy(dec->bitstreamFileName, value);
  1165. }
  1166. else if (strcmp(argName, "stream-endian") == 0) {
  1167. dec->StreamEndian = atoi(value);
  1168. }
  1169. else if (strcmp(argName, "frame-endian") == 0) {
  1170. dec->FrameEndian = atoi(value);
  1171. }
  1172. else if (strcmp(argName, "pixelj") == 0) {
  1173. dec->pixelJustification = atoi(value);
  1174. if (dec->pixelJustification >= PIXEL_16BIT_JUSTIFICATION_MAX) {
  1175. JLOG(ERR, "Invalid pixel justification\n");
  1176. ret = FALSE;
  1177. }
  1178. }
  1179. else if (strcmp(argName, "bs-size") == 0) {
  1180. dec->bsSize = atoi(value);
  1181. JLOG(INFO, "bitstream buffer size is:%d\n",dec->bsSize);
  1182. if (dec->bsSize == 0) {
  1183. JLOG(ERR, "bitstream buffer size is 0\n");
  1184. ret = FALSE;
  1185. }
  1186. }
  1187. else if (strcmp(argName, "roi") == 0) {
  1188. char* val;
  1189. val = strtok(value, ",");
  1190. if (val == NULL) {
  1191. JLOG(ERR, "Invalid ROI option: %s\n", value);
  1192. ret = FALSE;
  1193. }
  1194. else {
  1195. dec->roiOffsetX = atoi(val);
  1196. }
  1197. val = strtok(NULL, ",");
  1198. if (val == NULL) {
  1199. JLOG(ERR, "Invalid ROI option: %s\n", value);
  1200. ret = FALSE;
  1201. }
  1202. else {
  1203. dec->roiOffsetY = atoi(val);
  1204. }
  1205. val = strtok(NULL, ",");
  1206. if (val == NULL) {
  1207. JLOG(ERR, "Invalid ROI option: %s\n", value);
  1208. ret = FALSE;
  1209. }
  1210. else {
  1211. dec->roiWidth = atoi(val);
  1212. }
  1213. val = strtok(NULL, ",");
  1214. if (val == NULL) {
  1215. JLOG(ERR, "Invalid ROI option: %s\n", value);
  1216. ret = FALSE;
  1217. }
  1218. else {
  1219. dec->roiHeight = atoi(val);
  1220. }
  1221. dec->roiEnable = TRUE;
  1222. }
  1223. else if (strcmp(argName, "subsample") == 0) {
  1224. if (strcasecmp(value, "none") == 0) {
  1225. dec->subsample = FORMAT_MAX;
  1226. }
  1227. else if (strcasecmp(value, "420") == 0) {
  1228. dec->subsample = FORMAT_420;
  1229. }
  1230. else if (strcasecmp(value, "422") == 0) {
  1231. dec->subsample = FORMAT_422;
  1232. }
  1233. else if (strcasecmp(value, "444") == 0) {
  1234. dec->subsample = FORMAT_444;
  1235. }
  1236. else {
  1237. JLOG(ERR, "Not supported sub-sample: %s\n", value);
  1238. ret = FALSE;
  1239. }
  1240. }
  1241. else if (strcmp(argName, "ordering") == 0) {
  1242. if (strcasecmp(value, "none") == 0) {
  1243. dec->cbcrInterleave = CBCR_SEPARATED;
  1244. dec->packedFormat = PACKED_FORMAT_NONE;
  1245. }
  1246. else if (strcasecmp(value, "nv12") == 0) {
  1247. dec->cbcrInterleave = CBCR_INTERLEAVE;
  1248. dec->packedFormat = PACKED_FORMAT_NONE;
  1249. }
  1250. else if (strcasecmp(value, "nv21") == 0) {
  1251. dec->cbcrInterleave = CRCB_INTERLEAVE;
  1252. dec->packedFormat = PACKED_FORMAT_NONE;
  1253. }
  1254. else if (strcasecmp(value, "yuyv") == 0) {
  1255. dec->cbcrInterleave = CBCR_SEPARATED;
  1256. dec->packedFormat = PACKED_FORMAT_422_YUYV;
  1257. }
  1258. else if (strcasecmp(value, "uyvy") == 0) {
  1259. dec->cbcrInterleave = CBCR_SEPARATED;
  1260. dec->packedFormat = PACKED_FORMAT_422_UYVY;
  1261. }
  1262. else if (strcasecmp(value, "yvyu") == 0) {
  1263. dec->cbcrInterleave = CBCR_SEPARATED;
  1264. dec->packedFormat = PACKED_FORMAT_422_YVYU;
  1265. }
  1266. else if (strcasecmp(value, "vyuy") == 0) {
  1267. dec->cbcrInterleave = CBCR_SEPARATED;
  1268. dec->packedFormat = PACKED_FORMAT_422_VYUY;
  1269. }
  1270. else if (strcasecmp(value, "ayuv") == 0) {
  1271. dec->cbcrInterleave = CBCR_SEPARATED;
  1272. dec->packedFormat = PACKED_FORMAT_444;
  1273. }
  1274. else {
  1275. JLOG(ERR, "Not supported ordering: %s\n", value);
  1276. ret = FALSE;
  1277. }
  1278. }
  1279. else if (strcmp(argName, "rotation") == 0) {
  1280. dec->rotation = atoi(value);
  1281. }
  1282. else if (strcmp(argName, "mirror") == 0) {
  1283. dec->mirror = (JpgMirrorDirection)atoi(value);
  1284. }
  1285. else if (strcmp(argName, "scaleH") == 0) {
  1286. dec->iHorScaleMode = atoi(value);
  1287. }
  1288. else if (strcmp(argName, "scaleV") == 0) {
  1289. dec->iVerScaleMode = atoi(value);
  1290. }
  1291. else {
  1292. JLOG(ERR, "Not defined option: %s\n", argName);
  1293. ret = FALSE;
  1294. }
  1295. return ret;
  1296. }
  1297. BOOL ParseEncTestLongArgs(void* config, const char* argName, char* value)
  1298. {
  1299. BOOL ret = TRUE;
  1300. EncConfigParam* enc = (EncConfigParam*)config;
  1301. if (strcmp(argName, "output") == 0) {
  1302. strcpy(enc->bitstreamFileName, value);
  1303. }
  1304. else if (strcmp(argName, "input") == 0) {
  1305. strcpy(enc->cfgFileName, value);
  1306. }
  1307. else if (strcmp(argName, "12bit") == 0) {
  1308. enc->extendedSequential = TRUE;
  1309. }
  1310. else if (strcmp(argName, "cfg-dir") == 0) {
  1311. strcpy(enc->strCfgDir, value);
  1312. }
  1313. else if (strcmp(argName, "yuv-dir") == 0) {
  1314. strcpy(enc->strYuvDir, value);
  1315. }
  1316. else if (strcmp(argName, "stream-endian") == 0) {
  1317. enc->StreamEndian = atoi(value);
  1318. }
  1319. else if (strcmp(argName, "frame-endian") == 0) {
  1320. enc->FrameEndian = atoi(value);
  1321. }
  1322. else if (strcmp(argName, "pixelj") == 0) {
  1323. enc->pixelJustification = atoi(value);
  1324. if (enc->pixelJustification >= PIXEL_16BIT_JUSTIFICATION_MAX) {
  1325. JLOG(ERR, "Invalid pixel justification\n");
  1326. ret = FALSE;
  1327. }
  1328. }
  1329. else if (strcmp(argName, "bs-size") == 0) {
  1330. enc->bsSize = atoi(value);
  1331. if (enc->bsSize == 0) {
  1332. JLOG(ERR, "bitstream buffer size is 0\n");
  1333. ret = FALSE;
  1334. }
  1335. }
  1336. else if (strcmp(argName, "quality") == 0) {
  1337. enc->encQualityPercentage = atoi(value);
  1338. if (enc->encQualityPercentage > 100) {
  1339. JLOG(ERR, "Invalid quality factor: %d\n", enc->encQualityPercentage);
  1340. ret = FALSE;
  1341. }
  1342. }
  1343. else if (strcmp(argName, "enable-tiledMode") == 0) {
  1344. enc->tiledModeEnable = (BOOL)atoi(value);
  1345. }
  1346. else if (strcmp(argName, "slice-height") == 0) {
  1347. enc->sliceHeight = atoi(value);
  1348. }
  1349. else if (strcmp(argName, "enable-slice-intr") == 0) {
  1350. enc->sliceInterruptEnable = atoi(value);
  1351. }
  1352. else if (strcmp(argName, "rotation") == 0) {
  1353. enc->rotation = atoi(value);
  1354. }
  1355. else if (strcmp(argName, "mirror") == 0) {
  1356. enc->mirror = (JpgMirrorDirection)atoi(value);
  1357. }
  1358. else {
  1359. JLOG(ERR, "Not defined option: %s\n", argName);
  1360. ret = FALSE;
  1361. }
  1362. return ret;
  1363. }
  1364. static BOOL ParseNoneTestLongArgs(void* config, const char* argName, char* value)
  1365. {
  1366. return FALSE;
  1367. }
  1368. static BOOL ParseMultipleTestArgs(TestMultiConfig* config, const char* argName, char* value)
  1369. {
  1370. char* tempArg;
  1371. Uint32 i;
  1372. BOOL (*parsers[3])(void* config, const char*, char*) = {
  1373. ParseEncTestLongArgs,
  1374. ParseDecTestLongArgs,
  1375. ParseNoneTestLongArgs,
  1376. };
  1377. tempArg = strtok(value, ",");
  1378. for(i = 0; i < MAX_NUM_INSTANCE; i++) {
  1379. if (parsers[config->type[i]]((void*)&config->u[i], argName, tempArg) == FALSE) {
  1380. return FALSE;
  1381. }
  1382. tempArg = strtok(NULL, ",");
  1383. if (tempArg == NULL) {
  1384. break;
  1385. }
  1386. }
  1387. return TRUE;
  1388. }
  1389. BOOL ParseMultiLongOptions(TestMultiConfig* config, const char* argName, char* value)
  1390. {
  1391. BOOL ret = TRUE;
  1392. Uint32 i;
  1393. char* tempArg;
  1394. if (strcmp(argName, "instance-num") == 0) {
  1395. config->numInstances = atoi(value);
  1396. }
  1397. else if (strcmp(argName, "input") == 0) {
  1398. ret = ParseMultipleTestArgs(config, argName, value);
  1399. }
  1400. else if (strcmp(argName, "output") == 0) {
  1401. ret = ParseMultipleTestArgs(config, argName, value);
  1402. }
  1403. else if (strcmp(argName, "stream-endian") == 0) {
  1404. ret = ParseMultipleTestArgs(config, argName, value);
  1405. }
  1406. else if (strcmp(argName, "frame-endian") == 0) {
  1407. ret = ParseMultipleTestArgs(config, argName, value);
  1408. }
  1409. else if (strcmp(argName, "pixelj") == 0) {
  1410. ret = ParseMultipleTestArgs(config, argName, value);
  1411. }
  1412. else if (strcmp(argName, "bs-size") == 0) {
  1413. ret = ParseMultipleTestArgs(config, argName, value);
  1414. }
  1415. else if (strcmp(argName, "cfg-dir") == 0) {
  1416. for (i=0; i<MAX_NUM_INSTANCE; i++) {
  1417. if (config->type[i] == JPU_ENCODER) {
  1418. ParseEncTestLongArgs((void*)&config->u[i], argName, value);
  1419. }
  1420. }
  1421. }
  1422. else if (strcmp(argName, "yuv-dir") == 0) {
  1423. for (i=0; i<MAX_NUM_INSTANCE; i++) {
  1424. if (config->type[i] == JPU_ENCODER) {
  1425. ParseEncTestLongArgs((void*)&config->u[i], argName, value);
  1426. }
  1427. }
  1428. }
  1429. else if (strcmp(argName, "slice-height") == 0) {
  1430. tempArg = strtok(value, ",");
  1431. for(i = 0; i < MAX_NUM_INSTANCE; i++) {
  1432. if (config->type[i] == JPU_ENCODER) {
  1433. ParseEncTestLongArgs((void*)&config->u[i], argName, tempArg);
  1434. }
  1435. else {
  1436. ParseDecTestLongArgs((void*)&config->u[i], argName, tempArg);
  1437. }
  1438. tempArg = strtok(NULL, ",");
  1439. if (tempArg == NULL)
  1440. break;
  1441. }
  1442. }
  1443. else if (strcmp(argName, "enable-slice-intr") == 0) {
  1444. tempArg = strtok(value, ",");
  1445. for(i = 0; i < MAX_NUM_INSTANCE; i++) {
  1446. if (config->type[i] == JPU_ENCODER) {
  1447. ParseEncTestLongArgs((void*)&config->u[i], argName, tempArg);
  1448. }
  1449. else {
  1450. ParseDecTestLongArgs((void*)&config->u[i], argName, tempArg);
  1451. }
  1452. tempArg = strtok(NULL, ",");
  1453. if (tempArg == NULL)
  1454. break;
  1455. }
  1456. }
  1457. else if (strcmp(argName, "enable-tiledMode") == 0) {
  1458. tempArg = strtok(value, ",");
  1459. for(i = 0; i < MAX_NUM_INSTANCE; i++) {
  1460. if (config->type[i] == JPU_ENCODER) {
  1461. ParseEncTestLongArgs((void*)&config->u[i], argName, tempArg);
  1462. }
  1463. tempArg = strtok(NULL, ",");
  1464. if (tempArg == NULL)
  1465. break;
  1466. }
  1467. }
  1468. else {
  1469. JLOG(ERR, "Not defined option: %s\n", argName);
  1470. ret = FALSE;
  1471. }
  1472. return ret;
  1473. }
  1474. int GetFrameBufBase(int instIdx)
  1475. {
  1476. fb_context *fb;
  1477. fb = &s_fb[instIdx];
  1478. return fb->vb_base.phys_addr;
  1479. }
  1480. int GetFrameBufAllocSize(int instIdx)
  1481. {
  1482. fb_context *fb;
  1483. fb = &s_fb[instIdx];
  1484. return (fb->last_addr - fb->vb_base.phys_addr);
  1485. }
  1486. FRAME_BUF *GetFrameBuffer(int instIdx, int idx)
  1487. {
  1488. fb_context *fb;
  1489. fb = &s_fb[instIdx];
  1490. return &fb->frameBuf[idx];
  1491. }
  1492. Uint32 GetFrameBufferCount(int instIdx)
  1493. {
  1494. fb_context *fb;
  1495. fb = &s_fb[instIdx];
  1496. return fb->last_num;
  1497. }
  1498. FRAME_BUF* FindFrameBuffer(int instIdx, PhysicalAddress addrY)
  1499. {
  1500. int i;
  1501. fb_context *fb;
  1502. fb = &s_fb[instIdx];
  1503. for (i=0; i <MAX_FRAME; i++)
  1504. {
  1505. if (fb->frameBuf[i].vbY.phys_addr == addrY)
  1506. {
  1507. return &fb->frameBuf[i];
  1508. }
  1509. }
  1510. return NULL;
  1511. }
  1512. void FreeFrameBuffer(int instIdx)
  1513. {
  1514. fb_context *fb;
  1515. fb = &s_fb[instIdx];
  1516. fb->last_num = 0;
  1517. fb->last_addr = -1;
  1518. if(fb->vb_base.base)
  1519. {
  1520. jdi_free_dma_memory(&fb->vb_base);
  1521. fb->vb_base.base = 0;
  1522. }
  1523. }
  1524. char* GetFileExtension(const char* filename)
  1525. {
  1526. Int32 len;
  1527. Int32 i;
  1528. len = strlen(filename);
  1529. for (i=len-1; i>=0; i--) {
  1530. if (filename[i] == '.') {
  1531. return (char*)&filename[i+1];
  1532. }
  1533. }
  1534. return NULL;
  1535. }