jpuhelper.c 61 KB

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