jpuhelper.c 61 KB

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