jpuapi.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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 "jpuapifunc.h"
  26. #include "jpuapi.h"
  27. #include "regdefine.h"
  28. #include "jpulog.h"
  29. static JPUCap g_JpuAttributes;
  30. int JPU_IsBusy(JpgHandle handle)
  31. {
  32. Uint32 val;
  33. JpgInst *pJpgInst = (JpgInst *)handle;
  34. Int32 instRegIndex;
  35. if (pJpgInst->sliceInstMode == TRUE) {
  36. instRegIndex = pJpgInst->instIndex;
  37. }
  38. else {
  39. instRegIndex = 0;
  40. }
  41. val = JpuReadInstReg(instRegIndex, MJPEG_PIC_STATUS_REG);
  42. if ((val & (1<<INT_JPU_DONE)) ||
  43. (val & (1<<INT_JPU_ERROR)))
  44. return 0;
  45. return 1;
  46. }
  47. void JPU_ClrStatus(JpgHandle handle, Uint32 val)
  48. {
  49. JpgInst *pJpgInst = (JpgInst *)handle;
  50. Int32 instRegIndex;
  51. if (pJpgInst->sliceInstMode == TRUE) {
  52. instRegIndex = pJpgInst->instIndex;
  53. }
  54. else {
  55. instRegIndex = 0;
  56. }
  57. if (val != 0)
  58. JpuWriteInstReg(instRegIndex, MJPEG_PIC_STATUS_REG, val);
  59. }
  60. Uint32 JPU_GetStatus(JpgHandle handle)
  61. {
  62. JpgInst *pJpgInst = (JpgInst *)handle;
  63. Int32 instRegIndex;
  64. if (pJpgInst->sliceInstMode == TRUE) {
  65. instRegIndex = pJpgInst->instIndex;
  66. }
  67. else {
  68. instRegIndex = 0;
  69. }
  70. return JpuReadInstReg(instRegIndex, MJPEG_PIC_STATUS_REG);
  71. }
  72. Uint32 JPU_IsInit()
  73. {
  74. jpu_instance_pool_t *pjip;
  75. pjip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  76. if (!pjip)
  77. return 0;
  78. return 1;
  79. }
  80. Int32 JPU_WaitInterrupt(JpgHandle handle, int timeout)
  81. {
  82. Uint32 val;
  83. Uint32 instPicStatusRegAddr;
  84. Int32 instRegIndex;
  85. Int32 reason = 0;
  86. JpgInst *pJpgInst = (JpgInst *)handle;
  87. if (pJpgInst->sliceInstMode == TRUE) {
  88. instRegIndex = pJpgInst->instIndex;
  89. }
  90. else {
  91. instRegIndex = 0;
  92. }
  93. instPicStatusRegAddr = ((instRegIndex*NPT_REG_SIZE) + MJPEG_PIC_STATUS_REG);
  94. reason = jdi_wait_interrupt(timeout, instPicStatusRegAddr, instRegIndex);
  95. if (reason == -1)
  96. return -1;
  97. if (reason & (1<<INT_JPU_DONE) || reason & (1<<INT_JPU_SLICE_DONE)) {
  98. val = JpuReadReg(MJPEG_INST_CTRL_STATUS_REG);
  99. if ((((val & 0xf) >> instRegIndex) & 0x01) == 0) {
  100. return -2;
  101. }
  102. }
  103. return reason;
  104. }
  105. int JPU_GetOpenInstanceNum()
  106. {
  107. jpu_instance_pool_t *pjip;
  108. pjip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  109. if (!pjip)
  110. return -1;
  111. return pjip->jpu_instance_num;
  112. }
  113. JpgRet JPU_Init()
  114. {
  115. jpu_instance_pool_t *pjip;
  116. Uint32 val;
  117. if (jdi_init() < 0) {
  118. return JPG_RET_FAILURE;
  119. }
  120. if (jdi_get_task_num() > 1) {
  121. return JPG_RET_CALLED_BEFORE;
  122. }
  123. JpgEnterLock();
  124. pjip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  125. if (!pjip) {
  126. JpgLeaveLock();
  127. return JPG_RET_FAILURE;
  128. }
  129. //jdi_log(JDI_LOG_CMD_INIT, 1, 0);
  130. JPU_SWReset(NULL);
  131. JpuWriteReg(MJPEG_INST_CTRL_START_REG, (1<<0));
  132. val = JpuReadInstReg(0, MJPEG_VERSION_INFO_REG);
  133. // JPU Capabilities
  134. g_JpuAttributes.productId = (val>>24) & 0xf;
  135. g_JpuAttributes.revisoin = (val & 0xffffff);
  136. g_JpuAttributes.support12bit = (val>>28) & 0x01;
  137. //jdi_log(JDI_LOG_CMD_INIT, 0, 0);
  138. JpgLeaveLock();
  139. return JPG_RET_SUCCESS;
  140. }
  141. void JPU_DeInit()
  142. {
  143. JpgEnterLock();
  144. if (jdi_get_task_num() == 1) {
  145. JpuWriteReg(MJPEG_INST_CTRL_START_REG, 0);
  146. }
  147. JpgLeaveLock();
  148. jdi_release();
  149. }
  150. JpgRet JPU_GetVersionInfo(Uint32 *apiVersion, Uint32 *hwRevision, Uint32 *hwProductId)
  151. {
  152. if (JPU_IsInit() == 0) {
  153. return JPG_RET_NOT_INITIALIZED;
  154. }
  155. JpgEnterLock();
  156. if (apiVersion) {
  157. *apiVersion = API_VERSION;
  158. }
  159. if (hwRevision) {
  160. *hwRevision = g_JpuAttributes.revisoin;
  161. }
  162. if (hwProductId) {
  163. *hwProductId = g_JpuAttributes.productId;
  164. }
  165. JpgLeaveLock();
  166. return JPG_RET_SUCCESS;
  167. }
  168. JpgRet JPU_DecOpen(JpgDecHandle * pHandle, JpgDecOpenParam * pop)
  169. {
  170. JpgInst* pJpgInst;
  171. JpgDecInfo* pDecInfo;
  172. JpgRet ret;
  173. ret = CheckJpgDecOpenParam(pop);
  174. if (ret != JPG_RET_SUCCESS) {
  175. return ret;
  176. }
  177. JpgEnterLock();
  178. ret = GetJpgInstance(&pJpgInst);
  179. if (ret == JPG_RET_FAILURE) {
  180. *pHandle = 0;
  181. JpgLeaveLock();
  182. return JPG_RET_FAILURE;
  183. }
  184. *pHandle = pJpgInst;
  185. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  186. memset(pDecInfo, 0x00, sizeof(JpgDecInfo));
  187. pDecInfo->streamWrPtr = pop->bitstreamBuffer;
  188. pDecInfo->streamRdPtr = pop->bitstreamBuffer;
  189. JLOG(INFO, "streamWrPtr = %x\r\n", pop->bitstreamBuffer);
  190. pDecInfo->streamBufStartAddr = pop->bitstreamBuffer;
  191. pDecInfo->streamBufSize = pop->bitstreamBufferSize;
  192. pDecInfo->streamBufEndAddr = pop->bitstreamBuffer + pop->bitstreamBufferSize;
  193. pDecInfo->pBitStream = pop->pBitStream;
  194. pDecInfo->streamEndian = pop->streamEndian;
  195. pDecInfo->frameEndian = pop->frameEndian;
  196. pDecInfo->chromaInterleave = pop->chromaInterleave;
  197. pDecInfo->packedFormat = pop->packedFormat;
  198. pDecInfo->roiEnable = pop->roiEnable;
  199. pDecInfo->roiWidth = pop->roiWidth;
  200. pDecInfo->roiHeight = pop->roiHeight;
  201. pDecInfo->roiOffsetX = pop->roiOffsetX;
  202. pDecInfo->roiOffsetY = pop->roiOffsetY;
  203. pDecInfo->pixelJustification = pop->pixelJustification;
  204. pDecInfo->intrEnableBit = pop->intrEnableBit;
  205. pJpgInst->sliceInstMode = 0; /* The decoder does not support the slice-decoding */
  206. pDecInfo->rotationIndex = pop->rotation / 90;
  207. pDecInfo->mirrorIndex = pop->mirror;
  208. /* convert output format */
  209. switch (pop->outputFormat) {
  210. case FORMAT_400: ret = JPG_RET_INVALID_PARAM; break;
  211. case FORMAT_420: pDecInfo->ofmt = O_FMT_420; break;
  212. case FORMAT_422: pDecInfo->ofmt = O_FMT_422; break;
  213. case FORMAT_440: ret = JPG_RET_INVALID_PARAM; break;
  214. case FORMAT_444: pDecInfo->ofmt = O_FMT_444; break;
  215. case FORMAT_MAX: pDecInfo->ofmt = O_FMT_NONE; break;
  216. default: ret = JPG_RET_INVALID_PARAM; break;
  217. }
  218. pDecInfo->userqMatTab = 0;
  219. pDecInfo->decIdx = 0;
  220. JpgLeaveLock();
  221. return ret;
  222. }
  223. JpgRet JPU_DecClose(JpgDecHandle handle)
  224. {
  225. JpgInst * pJpgInst;
  226. JpgRet ret;
  227. ret = CheckJpgInstValidity(handle);
  228. if (ret != JPG_RET_SUCCESS)
  229. return ret;
  230. pJpgInst = handle;
  231. JpgEnterLock();
  232. if (GetJpgPendingInstEx(pJpgInst->instIndex)) {
  233. JpgLeaveLock();
  234. return JPG_RET_FRAME_NOT_COMPLETE;
  235. }
  236. FreeJpgInstance(pJpgInst);
  237. JpgLeaveLock();
  238. return JPG_RET_SUCCESS;
  239. }
  240. JpgRet JPU_DecGetInitialInfo(JpgDecHandle handle, JpgDecInitialInfo * info)
  241. {
  242. JpgInst* pJpgInst;
  243. JpgDecInfo* pDecInfo;
  244. JpgRet ret;
  245. int err;
  246. ret = CheckJpgInstValidity(handle);
  247. if (ret != JPG_RET_SUCCESS)
  248. return ret;
  249. if (info == 0) {
  250. return JPG_RET_INVALID_PARAM;
  251. }
  252. pJpgInst = handle;
  253. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  254. if (0 >= (err=JpegDecodeHeader(pDecInfo))) {
  255. /* The value of -2 indicates that the bitstream in the buffer is not enough to decode a header. */
  256. return (-2 == err) ? JPG_RET_BIT_EMPTY : JPG_RET_FAILURE;
  257. }
  258. if (pDecInfo->jpg12bit == TRUE && g_JpuAttributes.support12bit == FALSE) {
  259. return JPG_RET_NOT_SUPPORT;
  260. }
  261. info->picWidth = pDecInfo->picWidth;
  262. info->picHeight = pDecInfo->picHeight;
  263. info->minFrameBufferCount = 1;
  264. info->sourceFormat = (FrameFormat)pDecInfo->format;
  265. info->ecsPtr = pDecInfo->ecsPtr;
  266. pDecInfo->initialInfoObtained = 1;
  267. pDecInfo->minFrameBufferNum = 1;
  268. if ((pDecInfo->packedFormat == PACKED_FORMAT_444) && (pDecInfo->format != FORMAT_444)) {
  269. return JPG_RET_INVALID_PARAM;
  270. }
  271. if (pDecInfo->roiEnable) {
  272. if (pDecInfo->format == FORMAT_400) {
  273. pDecInfo->roiMcuWidth = pDecInfo->roiWidth/8;
  274. }
  275. else {
  276. pDecInfo->roiMcuWidth = pDecInfo->roiWidth/pDecInfo->mcuWidth;
  277. }
  278. pDecInfo->roiMcuHeight = pDecInfo->roiHeight/pDecInfo->mcuHeight;
  279. pDecInfo->roiMcuOffsetX = pDecInfo->roiOffsetX/pDecInfo->mcuWidth;
  280. pDecInfo->roiMcuOffsetY = pDecInfo->roiOffsetY/pDecInfo->mcuHeight;
  281. if ((pDecInfo->roiOffsetX > pDecInfo->alignedWidth)
  282. || (pDecInfo->roiOffsetY > pDecInfo->alignedHeight)
  283. || (pDecInfo->roiOffsetX + pDecInfo->roiWidth > pDecInfo->alignedWidth)
  284. || (pDecInfo->roiOffsetY + pDecInfo->roiHeight > pDecInfo->alignedHeight))
  285. return JPG_RET_INVALID_PARAM;
  286. if (pDecInfo->format == FORMAT_400) {
  287. if (((pDecInfo->roiOffsetX + pDecInfo->roiWidth) < 8) || ((pDecInfo->roiOffsetY + pDecInfo->roiHeight) < pDecInfo->mcuHeight))
  288. return JPG_RET_INVALID_PARAM;
  289. }
  290. else {
  291. if (((pDecInfo->roiOffsetX + pDecInfo->roiWidth) < pDecInfo->mcuWidth) || ((pDecInfo->roiOffsetY + pDecInfo->roiHeight) < pDecInfo->mcuHeight))
  292. return JPG_RET_INVALID_PARAM;
  293. }
  294. if (pDecInfo->format == FORMAT_400)
  295. pDecInfo->roiFrameWidth = pDecInfo->roiMcuWidth * 8;
  296. else
  297. pDecInfo->roiFrameWidth = pDecInfo->roiMcuWidth * pDecInfo->mcuWidth;
  298. pDecInfo->roiFrameHeight = pDecInfo->roiMcuHeight*pDecInfo->mcuHeight;
  299. info->roiFrameWidth = pDecInfo->roiFrameWidth;
  300. info->roiFrameHeight = pDecInfo->roiFrameHeight;
  301. info->roiFrameOffsetX = pDecInfo->roiMcuOffsetX*pDecInfo->mcuWidth;
  302. info->roiFrameOffsetY = pDecInfo->roiMcuOffsetY*pDecInfo->mcuHeight;
  303. }
  304. info->bitDepth = pDecInfo->bitDepth;
  305. return JPG_RET_SUCCESS;
  306. }
  307. FrameBuffer *JPU_GetFrameBufPool(JpgDecHandle handle)
  308. {
  309. JpgInst * pJpgInst;
  310. JpgDecInfo * pDecInfo;
  311. JpgRet ret;
  312. ret = CheckJpgInstValidity(handle);
  313. if (ret != JPG_RET_SUCCESS)
  314. return NULL;
  315. pJpgInst = handle;
  316. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  317. return pDecInfo->frameBufPool;
  318. }
  319. JpgRet JPU_DecRegisterFrameBuffer(JpgDecHandle handle, FrameBuffer * bufArray, int num, int stride)
  320. {
  321. JpgInst * pJpgInst;
  322. JpgDecInfo * pDecInfo;
  323. JpgRet ret;
  324. ret = CheckJpgInstValidity(handle);
  325. if (ret != JPG_RET_SUCCESS)
  326. return ret;
  327. pJpgInst = handle;
  328. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  329. if (!pDecInfo->initialInfoObtained) {
  330. return JPG_RET_WRONG_CALL_SEQUENCE;
  331. }
  332. if (bufArray == 0) {
  333. return JPG_RET_INVALID_FRAME_BUFFER;
  334. }
  335. if (num < pDecInfo->minFrameBufferNum) {
  336. return JPG_RET_INSUFFICIENT_FRAME_BUFFERS;
  337. }
  338. if ((stride % 8) != 0) {
  339. return JPG_RET_INVALID_STRIDE;
  340. }
  341. pDecInfo->frameBufPool = bufArray;
  342. pDecInfo->numFrameBuffers = num;
  343. pDecInfo->stride = stride;
  344. pDecInfo->stride_c = bufArray[0].strideC;
  345. return JPG_RET_SUCCESS;
  346. }
  347. JpgRet JPU_DecRegisterFrameBuffer2(JpgDecHandle handle, FrameBuffer * bufArray, int stride)
  348. {
  349. JpgInst * pJpgInst;
  350. JpgDecInfo * pDecInfo;
  351. JpgRet ret;
  352. int number;
  353. ret = CheckJpgInstValidity(handle);
  354. if (ret != JPG_RET_SUCCESS)
  355. return ret;
  356. pJpgInst = handle;
  357. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  358. number = pDecInfo->numFrameBuffers;
  359. if (!pDecInfo->initialInfoObtained) {
  360. return JPG_RET_WRONG_CALL_SEQUENCE;
  361. }
  362. if (bufArray == 0) {
  363. return JPG_RET_INVALID_FRAME_BUFFER;
  364. }
  365. if ((stride % 8) != 0) {
  366. return JPG_RET_INVALID_STRIDE;
  367. }
  368. if (pDecInfo->frameBufPool == NULL)
  369. {
  370. //First register
  371. JLOG(INFO, "%s First register frame buffer\r\n", __FUNCTION__);
  372. pDecInfo->frameBufPool = bufArray;
  373. pDecInfo->stride = stride;
  374. pDecInfo->stride_c = bufArray->strideC;
  375. }
  376. //check wether frameBufPool and bufArray match
  377. if (pDecInfo->frameBufPool[number].bufY != bufArray->bufY)
  378. {
  379. JLOG(ERR, "Does not support discontinuous array!\n");
  380. return JPG_RET_FAILURE;
  381. }
  382. number ++;
  383. if (number >= MAX_FRAME)
  384. {
  385. JLOG(ERR, "Too lot of frame buffer! %d/%d\n", number, MAX_FRAME);
  386. return JPG_RET_FAILURE;
  387. }
  388. pDecInfo->numFrameBuffers = number;
  389. JLOG(INFO, "%s numFrameBuffers = %d\r\n", __FUNCTION__, pDecInfo->numFrameBuffers);
  390. return JPG_RET_SUCCESS;
  391. }
  392. JpgRet JPU_DecGetBitstreamBuffer(JpgDecHandle handle,
  393. PhysicalAddress * prdPtr,
  394. PhysicalAddress * pwrPtr,
  395. int * size)
  396. {
  397. JpgInst * pJpgInst;
  398. JpgDecInfo * pDecInfo;
  399. JpgRet ret;
  400. PhysicalAddress rdPtr;
  401. PhysicalAddress wrPtr;
  402. int room;
  403. Int32 instRegIndex;
  404. ret = CheckJpgInstValidity(handle);
  405. if (ret != JPG_RET_SUCCESS)
  406. return ret;
  407. pJpgInst = handle;
  408. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  409. if (pJpgInst->sliceInstMode == TRUE) {
  410. instRegIndex = pJpgInst->instIndex;
  411. }
  412. else {
  413. instRegIndex = 0;
  414. }
  415. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  416. rdPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG);
  417. }
  418. else {
  419. rdPtr = pDecInfo->streamRdPtr;
  420. }
  421. wrPtr = pDecInfo->streamWrPtr;
  422. if (wrPtr == pDecInfo->streamBufStartAddr) {
  423. if (pDecInfo->frameOffset == 0) {
  424. room = (pDecInfo->streamBufEndAddr-pDecInfo->streamBufStartAddr);
  425. }
  426. else {
  427. room = (pDecInfo->frameOffset);
  428. }
  429. }
  430. else {
  431. room = (pDecInfo->streamBufEndAddr - wrPtr);
  432. }
  433. room = ((room>>9)<<9); // multiple of 512
  434. if (prdPtr) *prdPtr = rdPtr;
  435. if (pwrPtr) *pwrPtr = wrPtr;
  436. if (size) *size = room;
  437. return JPG_RET_SUCCESS;
  438. }
  439. JpgRet JPU_DecUpdateBitstreamBuffer(JpgDecHandle handle, int size)
  440. {
  441. JpgInst * pJpgInst;
  442. JpgDecInfo * pDecInfo;
  443. PhysicalAddress wrPtr;
  444. PhysicalAddress rdPtr;
  445. JpgRet ret;
  446. int val = 0;
  447. Int32 instRegIndex;
  448. ret = CheckJpgInstValidity(handle);
  449. if (ret != JPG_RET_SUCCESS)
  450. return ret;
  451. pJpgInst = handle;
  452. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  453. wrPtr = pDecInfo->streamWrPtr;
  454. if (pJpgInst->sliceInstMode == TRUE) {
  455. instRegIndex = pJpgInst->instIndex;
  456. }
  457. else {
  458. instRegIndex = 0;
  459. }
  460. if (size == 0)
  461. {
  462. val = (wrPtr-pDecInfo->streamBufStartAddr) / 256;
  463. if ((wrPtr-pDecInfo->streamBufStartAddr) % 256)
  464. val = val + 1;
  465. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  466. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, (1UL << 31 | val));
  467. }
  468. pDecInfo->streamEndflag = 1;
  469. return JPG_RET_SUCCESS;
  470. }
  471. wrPtr = pDecInfo->streamWrPtr;
  472. wrPtr += size;
  473. if (wrPtr == pDecInfo->streamBufEndAddr) {
  474. wrPtr = pDecInfo->streamBufStartAddr;
  475. }
  476. pDecInfo->streamWrPtr = wrPtr;
  477. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  478. rdPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG);
  479. if (rdPtr >= pDecInfo->streamBufEndAddr) {
  480. JLOG(INFO, "!!!!! WRAP-AROUND !!!!!\n");
  481. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, 0);
  482. JpuWriteInstReg(instRegIndex, MJPEG_GBU_TCNT_REG, 0);
  483. JpuWriteInstReg(instRegIndex, (MJPEG_GBU_TCNT_REG+4), 0);
  484. }
  485. JpuWriteInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG, wrPtr);
  486. if (wrPtr == pDecInfo->streamBufStartAddr) {
  487. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, pDecInfo->streamBufEndAddr);
  488. }
  489. else {
  490. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, wrPtr);
  491. }
  492. }
  493. else {
  494. rdPtr = pDecInfo->streamRdPtr;
  495. }
  496. pDecInfo->streamRdPtr = rdPtr;
  497. return JPG_RET_SUCCESS;
  498. }
  499. JpgRet JPU_SWReset(JpgHandle handle)
  500. {
  501. Uint32 val;
  502. int clock_state;
  503. JpgInst *pJpgInst;
  504. pJpgInst = ((JpgInst *)handle);
  505. clock_state = jdi_get_clock_gate();
  506. if (clock_state == 0)
  507. jdi_set_clock_gate(1);
  508. if (handle)
  509. jdi_log(JDI_LOG_CMD_RESET, 1, pJpgInst->instIndex);
  510. val = JpuReadReg(MJPEG_INST_CTRL_START_REG);
  511. val |= (1<<2);
  512. JpuWriteReg(MJPEG_INST_CTRL_START_REG, val);
  513. if (jdi_wait_inst_ctrl_busy(JPU_INST_CTRL_TIMEOUT_MS, MJPEG_INST_CTRL_STATUS_REG, INST_CTRL_IDLE) == -1) {// wait for INST_CTRL become IDLE
  514. if (handle)
  515. jdi_log(JDI_LOG_CMD_RESET, 0, pJpgInst->instIndex);
  516. val &= ~(1<<2);
  517. JpuWriteReg(MJPEG_INST_CTRL_START_REG, val);
  518. if (clock_state == 0)
  519. jdi_set_clock_gate(0);
  520. return JPG_RET_INST_CTRL_ERROR;
  521. }
  522. val &= ~(1<<2);
  523. JpuWriteReg(MJPEG_INST_CTRL_START_REG, val);
  524. if (handle)
  525. jdi_log(JDI_LOG_CMD_RESET, 0, pJpgInst->instIndex);
  526. if (clock_state == 0)
  527. jdi_set_clock_gate(0);
  528. return JPG_RET_SUCCESS;
  529. }
  530. JpgRet JPU_HWReset()
  531. {
  532. if (jdi_hw_reset() < 0 )
  533. return JPG_RET_FAILURE;
  534. return JPG_RET_SUCCESS;
  535. }
  536. JpgRet JPU_DecSetRdPtr(JpgDecHandle handle, PhysicalAddress addr, BOOL updateWrPtr)
  537. {
  538. JpgInst* pJpgInst;
  539. JpgDecInfo* pDecInfo;
  540. JpgRet ret;
  541. ret = CheckJpgInstValidity(handle);
  542. if (ret != JPG_RET_SUCCESS)
  543. return ret;
  544. pJpgInst = (JpgInst*)handle;
  545. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  546. JpgEnterLock();
  547. if (GetJpgPendingInstEx(pJpgInst->instIndex)) {
  548. JpgLeaveLock();
  549. return JPG_RET_FRAME_NOT_COMPLETE;
  550. }
  551. pDecInfo->streamRdPtr = addr;
  552. if (updateWrPtr)
  553. pDecInfo->streamWrPtr = addr;
  554. pDecInfo->frameOffset = addr - pDecInfo->streamBufStartAddr;
  555. pDecInfo->consumeByte = 0;
  556. JpuWriteReg(MJPEG_BBC_RD_PTR_REG, pDecInfo->streamRdPtr);
  557. JpgLeaveLock();
  558. return JPG_RET_SUCCESS;
  559. }
  560. JpgRet JPU_DecSetRdPtrEx(JpgDecHandle handle, PhysicalAddress addr, BOOL updateWrPtr)
  561. {
  562. JpgInst* pJpgInst;
  563. JpgDecInfo* pDecInfo;
  564. JpgRet ret;
  565. ret = CheckJpgInstValidity(handle);
  566. if (ret != JPG_RET_SUCCESS)
  567. return ret;
  568. pJpgInst = (JpgInst*)handle;
  569. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  570. JpgEnterLock();
  571. if (GetJpgPendingInstEx(pJpgInst->instIndex)) {
  572. JpgLeaveLock();
  573. return JPG_RET_FRAME_NOT_COMPLETE;
  574. }
  575. pDecInfo->streamRdPtr = addr;
  576. pDecInfo->streamBufStartAddr = addr;
  577. if (updateWrPtr)
  578. pDecInfo->streamWrPtr = addr;
  579. pDecInfo->frameOffset = 0;
  580. pDecInfo->consumeByte = 0;
  581. JpuWriteReg(MJPEG_BBC_RD_PTR_REG, pDecInfo->streamRdPtr);
  582. JpgLeaveLock();
  583. return JPG_RET_SUCCESS;
  584. }
  585. JpgRet JPU_DecStartOneFrame(JpgDecHandle handle, JpgDecParam *param)
  586. {
  587. JpgInst * pJpgInst;
  588. JpgDecInfo * pDecInfo;
  589. JpgRet ret;
  590. Uint32 val;
  591. PhysicalAddress rdPtr, wrPtr;
  592. BOOL is12Bit = FALSE;
  593. BOOL ppuEnable = FALSE;
  594. Int32 instRegIndex;
  595. BOOL bTableInfoUpdate;
  596. ret = CheckJpgInstValidity(handle);
  597. if (ret != JPG_RET_SUCCESS)
  598. return ret;
  599. pJpgInst = handle;
  600. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  601. is12Bit = (pDecInfo->bitDepth == 8) ? FALSE : TRUE;
  602. if (pDecInfo->frameBufPool == 0) { // This means frame buffers have not been registered.
  603. return JPG_RET_WRONG_CALL_SEQUENCE;
  604. }
  605. if (pJpgInst->sliceInstMode == TRUE) {
  606. instRegIndex = pJpgInst->instIndex;
  607. }
  608. else {
  609. instRegIndex = 0;
  610. }
  611. JpgEnterLock();
  612. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  613. JpgLeaveLock();
  614. return JPG_RET_FRAME_NOT_COMPLETE;
  615. }
  616. if (pDecInfo->frameOffset < 0) {
  617. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  618. return JPG_RET_EOS;
  619. }
  620. pDecInfo->q_prec0 = 0;
  621. pDecInfo->q_prec1 = 0;
  622. pDecInfo->q_prec2 = 0;
  623. pDecInfo->q_prec3 = 0;
  624. //check for stream empty case
  625. if (pDecInfo->streamEndflag == 0) {
  626. rdPtr = pDecInfo->streamRdPtr;
  627. wrPtr = pDecInfo->streamWrPtr;
  628. if (wrPtr == pDecInfo->streamBufStartAddr)
  629. wrPtr = pDecInfo->streamBufEndAddr;
  630. if (rdPtr > wrPtr) { // wrap-around case
  631. if ( ((pDecInfo->streamBufEndAddr-rdPtr) + (wrPtr-pDecInfo->streamBufStartAddr)) <1024 ) {
  632. JpgLeaveLock();
  633. return JPG_RET_BIT_EMPTY;
  634. }
  635. }
  636. else {
  637. if (wrPtr - rdPtr < 1024) {
  638. JpgLeaveLock();
  639. return JPG_RET_BIT_EMPTY;
  640. }
  641. }
  642. }
  643. {
  644. val = JpegDecodeHeader(pDecInfo);
  645. if (val == 0) {
  646. JpgLeaveLock();
  647. return JPG_RET_FAILURE;
  648. }
  649. if (val == (Uint32)-2) { // wrap around case
  650. pDecInfo->frameOffset = 0;
  651. pDecInfo->ecsPtr = 0;
  652. val = JpegDecodeHeader(pDecInfo);
  653. if (val == 0) {
  654. JpgLeaveLock();
  655. return JPG_RET_FAILURE;
  656. }
  657. }
  658. if (val == (Uint32)-1) { //stream empty case
  659. if (pDecInfo->streamEndflag == 1) {
  660. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  661. pDecInfo->frameOffset = -1;
  662. if (pJpgInst->sliceInstMode == TRUE) {
  663. JpgLeaveLock();
  664. }
  665. return JPG_RET_EOS;
  666. }
  667. JpgLeaveLock();
  668. return JPG_RET_BIT_EMPTY;
  669. }
  670. }
  671. JpuWriteInstReg(instRegIndex, MJPEG_INTR_MASK_REG, ((~pDecInfo->intrEnableBit) & 0x3ff));
  672. /* The registers related to the slice encoding should be clear */
  673. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_INFO_REG, pDecInfo->alignedHeight);
  674. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_DPB_POS_REG, pDecInfo->alignedHeight);
  675. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_POS_REG, 0);
  676. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SETMB_REG, 0);
  677. if (pDecInfo->streamRdPtr == pDecInfo->streamBufEndAddr) {
  678. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, 0);
  679. JpuWriteInstReg(instRegIndex, MJPEG_GBU_TCNT_REG, 0);
  680. JpuWriteInstReg(instRegIndex, (MJPEG_GBU_TCNT_REG+4), 0);
  681. }
  682. JpuWriteInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG, pDecInfo->streamWrPtr);
  683. if (pDecInfo->streamWrPtr == pDecInfo->streamBufStartAddr) {
  684. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, pDecInfo->streamBufEndAddr);
  685. }
  686. else {
  687. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, JPU_CEIL(256, pDecInfo->streamWrPtr));
  688. }
  689. JpuWriteInstReg(instRegIndex, MJPEG_BBC_BAS_ADDR_REG, pDecInfo->streamBufStartAddr);
  690. JpuWriteInstReg(instRegIndex, MJPEG_GBU_TCNT_REG, 0);
  691. JpuWriteInstReg(instRegIndex, (MJPEG_GBU_TCNT_REG+4), 0);
  692. JpuWriteInstReg(instRegIndex, MJPEG_PIC_ERRMB_REG, 0);
  693. JpuWriteInstReg(instRegIndex, MJPEG_PIC_CTRL_REG, is12Bit << 31 | pDecInfo->q_prec0 << 30 | pDecInfo->q_prec1 << 29 | pDecInfo->q_prec2 << 28 | pDecInfo->q_prec3 << 27 |
  694. pDecInfo->huffAcIdx << 13 | pDecInfo->huffDcIdx << 7 | pDecInfo->userHuffTab << 6 |
  695. (JPU_CHECK_WRITE_RESPONSE_BVALID_SIGNAL << 2) | 0);
  696. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SIZE_REG, (pDecInfo->alignedWidth << 16) | pDecInfo->alignedHeight);
  697. JpuWriteInstReg(instRegIndex, MJPEG_OP_INFO_REG, pDecInfo->busReqNum);
  698. JpuWriteInstReg(instRegIndex, MJPEG_MCU_INFO_REG, (pDecInfo->mcuBlockNum&0x0f) << 17 | (pDecInfo->compNum&0x07) << 14 |
  699. (pDecInfo->compInfo[0]&0x3f) << 8 | (pDecInfo->compInfo[1]&0x0f) << 4 |
  700. (pDecInfo->compInfo[2]&0x0f));
  701. //enable intlv NV12: 10, NV21: 11
  702. //packedFormat:0 => 4'd0
  703. //packedFormat:1,2,3,4 => 4, 5, 6, 7,
  704. //packedFormat:5 => 8
  705. //packedFormat:6 => 9
  706. val = (pDecInfo->ofmt << 9) | (pDecInfo->frameEndian << 6) | ((pDecInfo->chromaInterleave==0)?0:(pDecInfo->chromaInterleave==1)?2:3);
  707. if (pDecInfo->packedFormat == PACKED_FORMAT_NONE) {
  708. val |= (0<<5) | (0<<4);
  709. }
  710. else if (pDecInfo->packedFormat == PACKED_FORMAT_444) {
  711. val |= (1<<5) | (0<<4) | (0<<2);
  712. }
  713. else {
  714. val |= (0<<5) | (1<<4) | ((pDecInfo->packedFormat-1)<<2);
  715. }
  716. val |= (pDecInfo->pixelJustification << 11);
  717. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CONFIG_REG, val);
  718. JpuWriteInstReg(instRegIndex, MJPEG_RST_INTVAL_REG, pDecInfo->rstIntval);
  719. if (param) {
  720. if (param->scaleDownRatioWidth > 0 )
  721. pDecInfo->iHorScaleMode = param->scaleDownRatioWidth;
  722. if (param->scaleDownRatioHeight > 0 )
  723. pDecInfo->iVerScaleMode = param->scaleDownRatioHeight;
  724. }
  725. if (pDecInfo->iHorScaleMode | pDecInfo->iVerScaleMode)
  726. val = ((pDecInfo->iHorScaleMode&0x3)<<2) | ((pDecInfo->iVerScaleMode&0x3)) | 0x10 ;
  727. else {
  728. val = 0;
  729. }
  730. JpuWriteInstReg(instRegIndex, MJPEG_SCL_INFO_REG, val);
  731. bTableInfoUpdate = FALSE;
  732. if (pDecInfo->userHuffTab) {
  733. bTableInfoUpdate = TRUE;
  734. }
  735. if (bTableInfoUpdate == TRUE) {
  736. if (is12Bit == TRUE){
  737. if (!JpgDecHuffTabSetUp_12b(pDecInfo, instRegIndex)) {
  738. JpgLeaveLock();
  739. return JPG_RET_INVALID_PARAM;
  740. }
  741. }else{
  742. if (!JpgDecHuffTabSetUp(pDecInfo, instRegIndex)) {
  743. JpgLeaveLock();
  744. return JPG_RET_INVALID_PARAM;
  745. }
  746. }
  747. }
  748. bTableInfoUpdate = TRUE; // it always should be TRUE for multi-instance
  749. if (bTableInfoUpdate == TRUE) {
  750. if (!JpgDecQMatTabSetUp(pDecInfo, instRegIndex)) {
  751. JpgLeaveLock();
  752. return JPG_RET_INVALID_PARAM;
  753. }
  754. }
  755. JpgDecGramSetup(pDecInfo, instRegIndex);
  756. if (pDecInfo->streamEndflag == 1) {
  757. val = JpuReadInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG);
  758. if ((val & (1UL << 31)) == 0 ) {
  759. val = (pDecInfo->streamWrPtr-pDecInfo->streamBufStartAddr) / 256;
  760. if ((pDecInfo->streamWrPtr-pDecInfo->streamBufStartAddr) % 256)
  761. val = val + 1;
  762. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, (1UL << 31 | val));
  763. }
  764. }
  765. else {
  766. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, 0);
  767. }
  768. JpuWriteInstReg(instRegIndex, MJPEG_RST_INDEX_REG, 0); // RST index at the beginning.
  769. JpuWriteInstReg(instRegIndex, MJPEG_RST_COUNT_REG, 0);
  770. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_Y_REG, 0);
  771. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_CB_REG, 0);
  772. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_CR_REG, 0);
  773. JpuWriteInstReg(instRegIndex, MJPEG_GBU_FF_RPTR_REG, pDecInfo->bitPtr);
  774. JpuWriteInstReg(instRegIndex, MJPEG_GBU_CTRL_REG, 3);
  775. ppuEnable = (pDecInfo->rotationIndex > 0) || (pDecInfo->mirrorIndex > 0);
  776. JpuWriteInstReg(instRegIndex, MJPEG_ROT_INFO_REG, (ppuEnable<<4) | (pDecInfo->mirrorIndex<<2) | pDecInfo->rotationIndex);
  777. val = (pDecInfo->frameIdx % pDecInfo->numFrameBuffers);
  778. // JLOG(INFO, "%s: bufY = %lx bufCb = %lx bufCr = %lx stride = %d stride_c = %d\r\n", __FUNCTION__,
  779. // pDecInfo->frameBufPool[val].bufY, pDecInfo->frameBufPool[val].bufCb, pDecInfo->frameBufPool[val].bufCr,
  780. // pDecInfo->stride, pDecInfo->stride_c);
  781. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE00_REG, pDecInfo->frameBufPool[val].bufY);
  782. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE01_REG, pDecInfo->frameBufPool[val].bufCb);
  783. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE02_REG, pDecInfo->frameBufPool[val].bufCr);
  784. JpuWriteInstReg(instRegIndex, MJPEG_DPB_YSTRIDE_REG, pDecInfo->stride);
  785. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CSTRIDE_REG, pDecInfo->stride_c);
  786. if (pDecInfo->roiEnable) {
  787. JpuWriteInstReg(instRegIndex, MJPEG_CLP_INFO_REG, 1);
  788. JpuWriteInstReg(instRegIndex, MJPEG_CLP_BASE_REG, pDecInfo->roiOffsetX << 16 | pDecInfo->roiOffsetY); // pixel unit
  789. JpuWriteInstReg(instRegIndex, MJPEG_CLP_SIZE_REG, (pDecInfo->roiFrameWidth << 16) | pDecInfo->roiFrameHeight); // pixel Unit
  790. }
  791. else {
  792. JpuWriteInstReg(instRegIndex, MJPEG_CLP_INFO_REG, 0);
  793. }
  794. if (pJpgInst->loggingEnable)
  795. jdi_log(JDI_LOG_CMD_PICRUN, 1, instRegIndex);
  796. if (pJpgInst->sliceInstMode == TRUE) {
  797. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, (1<<JPG_DEC_SLICE_ENABLE_START_PIC));
  798. }
  799. else {
  800. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, (1<<JPG_ENABLE_START_PIC));
  801. }
  802. pDecInfo->decIdx++;
  803. SetJpgPendingInstEx(pJpgInst, pJpgInst->instIndex);
  804. if (pJpgInst->sliceInstMode == TRUE) {
  805. JpgLeaveLock();
  806. }
  807. return JPG_RET_SUCCESS;
  808. }
  809. JpgRet JPU_DecStartOneFrameBySerialNum(JpgDecHandle handle, JpgDecParam *param,int bufferIndex)
  810. {
  811. JpgInst * pJpgInst;
  812. JpgDecInfo * pDecInfo;
  813. JpgRet ret;
  814. Uint32 val;
  815. PhysicalAddress rdPtr, wrPtr;
  816. BOOL is12Bit = FALSE;
  817. BOOL ppuEnable = FALSE;
  818. Int32 instRegIndex;
  819. BOOL bTableInfoUpdate;
  820. ret = CheckJpgInstValidity(handle);
  821. if (ret != JPG_RET_SUCCESS)
  822. return ret;
  823. pJpgInst = handle;
  824. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  825. is12Bit = (pDecInfo->bitDepth == 8) ? FALSE : TRUE;
  826. if (pDecInfo->frameBufPool == 0) { // This means frame buffers have not been registered.
  827. return JPG_RET_WRONG_CALL_SEQUENCE;
  828. }
  829. if (pJpgInst->sliceInstMode == TRUE) {
  830. instRegIndex = pJpgInst->instIndex;
  831. }
  832. else {
  833. instRegIndex = 0;
  834. }
  835. JpgEnterLock();
  836. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  837. JpgLeaveLock();
  838. return JPG_RET_FRAME_NOT_COMPLETE;
  839. }
  840. if (pDecInfo->frameOffset < 0) {
  841. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  842. return JPG_RET_EOS;
  843. }
  844. pDecInfo->q_prec0 = 0;
  845. pDecInfo->q_prec1 = 0;
  846. pDecInfo->q_prec2 = 0;
  847. pDecInfo->q_prec3 = 0;
  848. //check for stream empty case
  849. if (pDecInfo->streamEndflag == 0) {
  850. rdPtr = pDecInfo->streamRdPtr;
  851. wrPtr = pDecInfo->streamWrPtr;
  852. if (wrPtr == pDecInfo->streamBufStartAddr)
  853. wrPtr = pDecInfo->streamBufEndAddr;
  854. if (rdPtr > wrPtr) { // wrap-around case
  855. if ( ((pDecInfo->streamBufEndAddr-rdPtr) + (wrPtr-pDecInfo->streamBufStartAddr)) <1024 ) {
  856. JpgLeaveLock();
  857. return JPG_RET_BIT_EMPTY;
  858. }
  859. }
  860. else {
  861. if (wrPtr - rdPtr < 1024) {
  862. JpgLeaveLock();
  863. return JPG_RET_BIT_EMPTY;
  864. }
  865. }
  866. }
  867. {
  868. val = JpegDecodeHeader(pDecInfo);
  869. if (val == 0) {
  870. JpgLeaveLock();
  871. return JPG_RET_FAILURE;
  872. }
  873. if (val == (Uint32)-2) { // wrap around case
  874. pDecInfo->frameOffset = 0;
  875. pDecInfo->ecsPtr = 0;
  876. val = JpegDecodeHeader(pDecInfo);
  877. if (val == 0) {
  878. JpgLeaveLock();
  879. return JPG_RET_FAILURE;
  880. }
  881. }
  882. if (val == (Uint32)-1) { //stream empty case
  883. if (pDecInfo->streamEndflag == 1) {
  884. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  885. pDecInfo->frameOffset = -1;
  886. if (pJpgInst->sliceInstMode == TRUE) {
  887. JpgLeaveLock();
  888. }
  889. return JPG_RET_EOS;
  890. }
  891. JpgLeaveLock();
  892. return JPG_RET_BIT_EMPTY;
  893. }
  894. }
  895. JpuWriteInstReg(instRegIndex, MJPEG_INTR_MASK_REG, ((~pDecInfo->intrEnableBit) & 0x3ff));
  896. /* The registers related to the slice encoding should be clear */
  897. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_INFO_REG, pDecInfo->alignedHeight);
  898. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_DPB_POS_REG, pDecInfo->alignedHeight);
  899. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_POS_REG, 0);
  900. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SETMB_REG, 0);
  901. if (pDecInfo->streamRdPtr == pDecInfo->streamBufEndAddr) {
  902. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, 0);
  903. JpuWriteInstReg(instRegIndex, MJPEG_GBU_TCNT_REG, 0);
  904. JpuWriteInstReg(instRegIndex, (MJPEG_GBU_TCNT_REG+4), 0);
  905. }
  906. JpuWriteInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG, pDecInfo->streamWrPtr);
  907. if (pDecInfo->streamWrPtr == pDecInfo->streamBufStartAddr) {
  908. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, pDecInfo->streamBufEndAddr);
  909. }
  910. else {
  911. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, JPU_CEIL(256, pDecInfo->streamWrPtr));
  912. }
  913. JpuWriteInstReg(instRegIndex, MJPEG_BBC_BAS_ADDR_REG, pDecInfo->streamBufStartAddr);
  914. JpuWriteInstReg(instRegIndex, MJPEG_GBU_TCNT_REG, 0);
  915. JpuWriteInstReg(instRegIndex, (MJPEG_GBU_TCNT_REG+4), 0);
  916. JpuWriteInstReg(instRegIndex, MJPEG_PIC_ERRMB_REG, 0);
  917. JpuWriteInstReg(instRegIndex, MJPEG_PIC_CTRL_REG, is12Bit << 31 | pDecInfo->q_prec0 << 30 | pDecInfo->q_prec1 << 29 | pDecInfo->q_prec2 << 28 | pDecInfo->q_prec3 << 27 |
  918. pDecInfo->huffAcIdx << 13 | pDecInfo->huffDcIdx << 7 | pDecInfo->userHuffTab << 6 |
  919. (JPU_CHECK_WRITE_RESPONSE_BVALID_SIGNAL << 2) | 0);
  920. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SIZE_REG, (pDecInfo->alignedWidth << 16) | pDecInfo->alignedHeight);
  921. JpuWriteInstReg(instRegIndex, MJPEG_OP_INFO_REG, pDecInfo->busReqNum);
  922. JpuWriteInstReg(instRegIndex, MJPEG_MCU_INFO_REG, (pDecInfo->mcuBlockNum&0x0f) << 17 | (pDecInfo->compNum&0x07) << 14 |
  923. (pDecInfo->compInfo[0]&0x3f) << 8 | (pDecInfo->compInfo[1]&0x0f) << 4 |
  924. (pDecInfo->compInfo[2]&0x0f));
  925. //enable intlv NV12: 10, NV21: 11
  926. //packedFormat:0 => 4'd0
  927. //packedFormat:1,2,3,4 => 4, 5, 6, 7,
  928. //packedFormat:5 => 8
  929. //packedFormat:6 => 9
  930. val = (pDecInfo->ofmt << 9) | (pDecInfo->frameEndian << 6) | ((pDecInfo->chromaInterleave==0)?0:(pDecInfo->chromaInterleave==1)?2:3);
  931. if (pDecInfo->packedFormat == PACKED_FORMAT_NONE) {
  932. val |= (0<<5) | (0<<4);
  933. }
  934. else if (pDecInfo->packedFormat == PACKED_FORMAT_444) {
  935. val |= (1<<5) | (0<<4) | (0<<2);
  936. }
  937. else {
  938. val |= (0<<5) | (1<<4) | ((pDecInfo->packedFormat-1)<<2);
  939. }
  940. val |= (pDecInfo->pixelJustification << 11);
  941. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CONFIG_REG, val);
  942. JpuWriteInstReg(instRegIndex, MJPEG_RST_INTVAL_REG, pDecInfo->rstIntval);
  943. if (param) {
  944. if (param->scaleDownRatioWidth > 0 )
  945. pDecInfo->iHorScaleMode = param->scaleDownRatioWidth;
  946. if (param->scaleDownRatioHeight > 0 )
  947. pDecInfo->iVerScaleMode = param->scaleDownRatioHeight;
  948. }
  949. if (pDecInfo->iHorScaleMode | pDecInfo->iVerScaleMode)
  950. val = ((pDecInfo->iHorScaleMode&0x3)<<2) | ((pDecInfo->iVerScaleMode&0x3)) | 0x10 ;
  951. else {
  952. val = 0;
  953. }
  954. JpuWriteInstReg(instRegIndex, MJPEG_SCL_INFO_REG, val);
  955. bTableInfoUpdate = FALSE;
  956. if (pDecInfo->userHuffTab) {
  957. bTableInfoUpdate = TRUE;
  958. }
  959. if (bTableInfoUpdate == TRUE) {
  960. if (is12Bit == TRUE){
  961. if (!JpgDecHuffTabSetUp_12b(pDecInfo, instRegIndex)) {
  962. JpgLeaveLock();
  963. return JPG_RET_INVALID_PARAM;
  964. }
  965. }else{
  966. if (!JpgDecHuffTabSetUp(pDecInfo, instRegIndex)) {
  967. JpgLeaveLock();
  968. return JPG_RET_INVALID_PARAM;
  969. }
  970. }
  971. }
  972. bTableInfoUpdate = TRUE; // it always should be TRUE for multi-instance
  973. if (bTableInfoUpdate == TRUE) {
  974. if (!JpgDecQMatTabSetUp(pDecInfo, instRegIndex)) {
  975. JpgLeaveLock();
  976. return JPG_RET_INVALID_PARAM;
  977. }
  978. }
  979. JpgDecGramSetup(pDecInfo, instRegIndex);
  980. if (pDecInfo->streamEndflag == 1) {
  981. val = JpuReadInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG);
  982. if ((val & (1UL << 31)) == 0 ) {
  983. val = (pDecInfo->streamWrPtr-pDecInfo->streamBufStartAddr) / 256;
  984. if ((pDecInfo->streamWrPtr-pDecInfo->streamBufStartAddr) % 256)
  985. val = val + 1;
  986. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, (1UL << 31 | val));
  987. }
  988. }
  989. else {
  990. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, 0);
  991. }
  992. JpuWriteInstReg(instRegIndex, MJPEG_RST_INDEX_REG, 0); // RST index at the beginning.
  993. JpuWriteInstReg(instRegIndex, MJPEG_RST_COUNT_REG, 0);
  994. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_Y_REG, 0);
  995. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_CB_REG, 0);
  996. JpuWriteInstReg(instRegIndex, MJPEG_DPCM_DIFF_CR_REG, 0);
  997. JpuWriteInstReg(instRegIndex, MJPEG_GBU_FF_RPTR_REG, pDecInfo->bitPtr);
  998. JpuWriteInstReg(instRegIndex, MJPEG_GBU_CTRL_REG, 3);
  999. ppuEnable = (pDecInfo->rotationIndex > 0) || (pDecInfo->mirrorIndex > 0);
  1000. JpuWriteInstReg(instRegIndex, MJPEG_ROT_INFO_REG, (ppuEnable<<4) | (pDecInfo->mirrorIndex<<2) | pDecInfo->rotationIndex);
  1001. val = (Uint32)bufferIndex;
  1002. // JLOG(INFO, "%s: bufY = %lx bufCb = %lx bufCr = %lx stride = %d stride_c = %d\r\n", __FUNCTION__,
  1003. // pDecInfo->frameBufPool[val].bufY, pDecInfo->frameBufPool[val].bufCb, pDecInfo->frameBufPool[val].bufCr,
  1004. // pDecInfo->stride, pDecInfo->stride_c);
  1005. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE00_REG, pDecInfo->frameBufPool[val].bufY);
  1006. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE01_REG, pDecInfo->frameBufPool[val].bufCb);
  1007. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE02_REG, pDecInfo->frameBufPool[val].bufCr);
  1008. JpuWriteInstReg(instRegIndex, MJPEG_DPB_YSTRIDE_REG, pDecInfo->stride);
  1009. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CSTRIDE_REG, pDecInfo->stride_c);
  1010. if (pDecInfo->roiEnable) {
  1011. JpuWriteInstReg(instRegIndex, MJPEG_CLP_INFO_REG, 1);
  1012. JpuWriteInstReg(instRegIndex, MJPEG_CLP_BASE_REG, pDecInfo->roiOffsetX << 16 | pDecInfo->roiOffsetY); // pixel unit
  1013. JpuWriteInstReg(instRegIndex, MJPEG_CLP_SIZE_REG, (pDecInfo->roiFrameWidth << 16) | pDecInfo->roiFrameHeight); // pixel Unit
  1014. }
  1015. else {
  1016. JpuWriteInstReg(instRegIndex, MJPEG_CLP_INFO_REG, 0);
  1017. }
  1018. if (pJpgInst->loggingEnable)
  1019. jdi_log(JDI_LOG_CMD_PICRUN, 1, instRegIndex);
  1020. if (pJpgInst->sliceInstMode == TRUE) {
  1021. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, (1<<JPG_DEC_SLICE_ENABLE_START_PIC));
  1022. }
  1023. else {
  1024. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, (1<<JPG_ENABLE_START_PIC));
  1025. }
  1026. pDecInfo->decIdx++;
  1027. SetJpgPendingInstEx(pJpgInst, pJpgInst->instIndex);
  1028. if (pJpgInst->sliceInstMode == TRUE) {
  1029. JpgLeaveLock();
  1030. }
  1031. return JPG_RET_SUCCESS;
  1032. }
  1033. JpgRet JPU_DecGetOutputInfo(JpgDecHandle handle, JpgDecOutputInfo * info)
  1034. {
  1035. JpgInst* pJpgInst;
  1036. JpgDecInfo* pDecInfo;
  1037. JpgRet ret;
  1038. Uint32 val = 0;
  1039. Int32 instRegIndex;
  1040. ret = CheckJpgInstValidity(handle);
  1041. if (ret != JPG_RET_SUCCESS) {
  1042. return ret;
  1043. }
  1044. if (info == 0) {
  1045. return JPG_RET_INVALID_PARAM;
  1046. }
  1047. pJpgInst = handle;
  1048. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  1049. if (pJpgInst->sliceInstMode == TRUE) {
  1050. instRegIndex = pJpgInst->instIndex;
  1051. }
  1052. else {
  1053. instRegIndex = 0;
  1054. }
  1055. if (pJpgInst->sliceInstMode == TRUE) {
  1056. JpgEnterLock();
  1057. }
  1058. if (pJpgInst != GetJpgPendingInstEx(pJpgInst->instIndex)) {
  1059. JpgLeaveLock();
  1060. return JPG_RET_WRONG_CALL_SEQUENCE;
  1061. }
  1062. if (pDecInfo->frameOffset < 0)
  1063. {
  1064. info->numOfErrMBs = 0;
  1065. info->decodingSuccess = 1;
  1066. info->indexFrameDisplay = -1;
  1067. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  1068. JpgLeaveLock();
  1069. return JPG_RET_SUCCESS;
  1070. }
  1071. if (pDecInfo->roiEnable) {
  1072. info->decPicWidth = pDecInfo->roiMcuWidth*pDecInfo->mcuWidth;
  1073. info->decPicHeight = pDecInfo->roiMcuHeight*pDecInfo->mcuHeight;
  1074. }
  1075. else {
  1076. info->decPicWidth = pDecInfo->alignedWidth;
  1077. info->decPicHeight = pDecInfo->alignedHeight;
  1078. }
  1079. info->decPicWidth >>= pDecInfo->iHorScaleMode;
  1080. info->decPicHeight >>= pDecInfo->iVerScaleMode;
  1081. info->indexFrameDisplay = (pDecInfo->frameIdx%pDecInfo->numFrameBuffers);
  1082. info->consumedByte = (JpuReadInstReg(instRegIndex, MJPEG_GBU_TCNT_REG))/8;
  1083. pDecInfo->streamRdPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG);
  1084. pDecInfo->consumeByte = info->consumedByte - 16 - pDecInfo->ecsPtr;
  1085. /*
  1086. fix up the program may not be able to exit normally when decoder some pictures.
  1087. due to pDecInfo->consumeByte==0 when not first enter.
  1088. */
  1089. if(pDecInfo->consumeByte == 0)
  1090. pDecInfo->consumeByte += 2;
  1091. info->bytePosFrameStart = pDecInfo->frameOffset;
  1092. info->ecsPtr = pDecInfo->ecsPtr;
  1093. info->rdPtr = pDecInfo->streamRdPtr;
  1094. info->wrPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG);
  1095. pDecInfo->ecsPtr = 0;
  1096. pDecInfo->frameIdx++;
  1097. val = JpuReadInstReg(instRegIndex, MJPEG_PIC_STATUS_REG);
  1098. if (val & (1<<INT_JPU_DONE)) {
  1099. info->decodingSuccess = 1;
  1100. info->numOfErrMBs = 0;
  1101. }
  1102. else if (val & (1<<INT_JPU_ERROR)){
  1103. info->numOfErrMBs = JpuReadInstReg(instRegIndex, MJPEG_PIC_ERRMB_REG);
  1104. info->decodingSuccess = 0;
  1105. }
  1106. #ifndef JPU_IRQ_CONTROL
  1107. info->decodingSuccess = 1;
  1108. info->numOfErrMBs = 0;
  1109. #endif
  1110. info->frameCycle = JpuReadInstReg(instRegIndex, MJPEG_CYCLE_INFO_REG);
  1111. if (val != 0)
  1112. JpuWriteInstReg(instRegIndex, MJPEG_PIC_STATUS_REG, val);
  1113. if (pJpgInst->loggingEnable)
  1114. jdi_log(JDI_LOG_CMD_PICRUN, 0, instRegIndex);
  1115. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, 0);
  1116. val = JpuReadReg(MJPEG_INST_CTRL_STATUS_REG);
  1117. val &= ~(1UL<<instRegIndex);
  1118. JpuWriteReg(MJPEG_INST_CTRL_STATUS_REG, val);
  1119. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  1120. JpgLeaveLock();
  1121. return JPG_RET_SUCCESS;
  1122. }
  1123. JpgRet JPU_DecGiveCommand(
  1124. JpgDecHandle handle,
  1125. JpgCommand cmd,
  1126. void * param)
  1127. {
  1128. JpgInst * pJpgInst;
  1129. JpgDecInfo * pDecInfo;
  1130. JpgRet ret;
  1131. ret = CheckJpgInstValidity(handle);
  1132. if (ret != JPG_RET_SUCCESS)
  1133. return ret;
  1134. pJpgInst = handle;
  1135. pDecInfo = &pJpgInst->JpgInfo->decInfo;
  1136. switch (cmd)
  1137. {
  1138. case SET_JPG_SCALE_HOR:
  1139. {
  1140. int scale;
  1141. scale = *(int *)param;
  1142. if (pDecInfo->alignedWidth < 128 || pDecInfo->alignedHeight < 128) {
  1143. if (scale) {
  1144. return JPG_RET_INVALID_PARAM;
  1145. }
  1146. }
  1147. pDecInfo->iHorScaleMode = scale;
  1148. break;
  1149. }
  1150. case SET_JPG_SCALE_VER:
  1151. {
  1152. int scale;
  1153. scale = *(int *)param;
  1154. if (pDecInfo->alignedWidth < 128 || pDecInfo->alignedHeight < 128) {
  1155. if (scale) {
  1156. return JPG_RET_INVALID_PARAM;
  1157. }
  1158. }
  1159. pDecInfo->iVerScaleMode = scale;
  1160. break;
  1161. }
  1162. case ENABLE_LOGGING:
  1163. {
  1164. pJpgInst->loggingEnable = 1;
  1165. }
  1166. break;
  1167. case DISABLE_LOGGING:
  1168. {
  1169. pJpgInst->loggingEnable = 0;
  1170. }
  1171. break;
  1172. default:
  1173. return JPG_RET_INVALID_COMMAND;
  1174. }
  1175. return JPG_RET_SUCCESS;
  1176. }
  1177. JpgRet JPU_EncOpen(JpgEncHandle * pHandle, JpgEncOpenParam * pop)
  1178. {
  1179. JpgInst* pJpgInst;
  1180. JpgEncInfo* pEncInfo;
  1181. JpgRet ret;
  1182. Uint32 i;
  1183. Uint32 mcuWidth, mcuHeight;
  1184. Uint32 comp0McuWidth, comp0McuHeight;
  1185. BOOL rotation_90_270 = FALSE;
  1186. FrameFormat format;
  1187. ret = CheckJpgEncOpenParam(pop, &g_JpuAttributes);
  1188. if (ret != JPG_RET_SUCCESS) {
  1189. return ret;
  1190. }
  1191. JpgEnterLock();
  1192. ret = GetJpgInstance(&pJpgInst);
  1193. if (ret == JPG_RET_FAILURE) {
  1194. JpgLeaveLock();
  1195. return JPG_RET_FAILURE;
  1196. }
  1197. *pHandle = pJpgInst;
  1198. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1199. memset(pEncInfo, 0x00, sizeof(JpgEncInfo));
  1200. pEncInfo->streamRdPtr = pop->bitstreamBuffer;
  1201. pEncInfo->streamWrPtr = pop->bitstreamBuffer;
  1202. pEncInfo->sliceHeight = pop->sliceHeight;
  1203. pEncInfo->intrEnableBit = pop->intrEnableBit;
  1204. pEncInfo->streamBufStartAddr = pop->bitstreamBuffer;
  1205. pEncInfo->streamBufEndAddr = pop->bitstreamBuffer + pop->bitstreamBufferSize - (4096);
  1206. pEncInfo->streamEndian = pop->streamEndian;
  1207. pEncInfo->frameEndian = pop->frameEndian;
  1208. pEncInfo->chromaInterleave = pop->chromaInterleave;
  1209. pEncInfo->format = pop->sourceFormat;
  1210. pEncInfo->picWidth = pop->picWidth;
  1211. pEncInfo->picHeight = pop->picHeight;
  1212. if (pop->rotation == 90 || pop->rotation == 270) {
  1213. if (pEncInfo->format == FORMAT_422) format = FORMAT_440;
  1214. else if (pEncInfo->format == FORMAT_440) format = FORMAT_422;
  1215. else format = pEncInfo->format;
  1216. rotation_90_270 = TRUE;
  1217. }
  1218. else {
  1219. format = pEncInfo->format;
  1220. }
  1221. // Picture size alignment
  1222. if (format == FORMAT_420 || format == FORMAT_422) {
  1223. pEncInfo->alignedWidth = JPU_CEIL(16, pEncInfo->picWidth);
  1224. mcuWidth = 16;
  1225. }
  1226. else {
  1227. pEncInfo->alignedWidth = JPU_CEIL(8, pEncInfo->picWidth);
  1228. mcuWidth = (format == FORMAT_400) ? 32 : 8;
  1229. }
  1230. if (format == FORMAT_420 || format == FORMAT_440) {
  1231. pEncInfo->alignedHeight = JPU_CEIL(16, pEncInfo->picHeight);
  1232. mcuHeight = 16;
  1233. }
  1234. else {
  1235. pEncInfo->alignedHeight = JPU_CEIL(8, pEncInfo->picHeight);
  1236. mcuHeight = 8;
  1237. }
  1238. pEncInfo->mcuWidth = mcuWidth;
  1239. pEncInfo->mcuHeight = mcuHeight;
  1240. if (format == FORMAT_400) {
  1241. if (pop->rotation == 90 || pop->rotation == 270) {
  1242. pEncInfo->mcuWidth = mcuHeight;
  1243. pEncInfo->mcuHeight = mcuWidth;
  1244. }
  1245. }
  1246. comp0McuWidth = pEncInfo->mcuWidth;
  1247. comp0McuHeight = pEncInfo->mcuHeight;
  1248. if (rotation_90_270 == TRUE) {
  1249. if (pEncInfo->format == FORMAT_420 || pEncInfo->format == FORMAT_422) {
  1250. comp0McuWidth = 16;
  1251. }
  1252. else {
  1253. comp0McuWidth = 8;
  1254. }
  1255. if (pEncInfo->format == FORMAT_420 || pEncInfo->format == FORMAT_440) {
  1256. comp0McuHeight = 16;
  1257. }
  1258. else if (pEncInfo->format == FORMAT_400) {
  1259. comp0McuHeight = 32;
  1260. }
  1261. else {
  1262. comp0McuHeight = 8;
  1263. }
  1264. }
  1265. if (pop->sliceInstMode == TRUE) {
  1266. Uint32 ppuHeight = (rotation_90_270 == TRUE) ? pEncInfo->alignedWidth : pEncInfo->alignedHeight;
  1267. if (pop->sliceHeight % pEncInfo->mcuHeight) {
  1268. JpgLeaveLock();
  1269. return JPG_RET_INVALID_PARAM;
  1270. }
  1271. if (pop->sliceHeight > ppuHeight) {
  1272. JpgLeaveLock();
  1273. return JPG_RET_INVALID_PARAM;
  1274. }
  1275. if (pop->sliceHeight < pEncInfo->mcuHeight) {
  1276. JpgLeaveLock();
  1277. return JPG_RET_INVALID_PARAM;
  1278. }
  1279. }
  1280. pJpgInst->sliceInstMode = pop->sliceInstMode;
  1281. pEncInfo->rstIntval = pop->restartInterval;
  1282. pEncInfo->jpg12bit = pop->jpg12bit;
  1283. pEncInfo->q_prec0 = pop->q_prec0;
  1284. pEncInfo->q_prec1 = pop->q_prec1;
  1285. pEncInfo->pixelJustification = pop->pixelJustification;
  1286. if (pop->jpg12bit){
  1287. for (i=0; i<8; i++)
  1288. pEncInfo->pHuffVal[i] = pop->huffVal[i];
  1289. for (i=0; i<8; i++)
  1290. pEncInfo->pHuffBits[i] = pop->huffBits[i];
  1291. }else{
  1292. for (i=0; i<4; i++)
  1293. pEncInfo->pHuffVal[i] = pop->huffVal[i];
  1294. for (i=0; i<4; i++)
  1295. pEncInfo->pHuffBits[i] = pop->huffBits[i];
  1296. }
  1297. for (i=0; i<4; i++)
  1298. pEncInfo->pQMatTab[i] = pop->qMatTab[i];
  1299. pEncInfo->pCInfoTab[0] = sJpuCompInfoTable[2/*pEncInfo->format*/];
  1300. pEncInfo->pCInfoTab[1] = pEncInfo->pCInfoTab[0] + 6;
  1301. pEncInfo->pCInfoTab[2] = pEncInfo->pCInfoTab[1] + 6;
  1302. pEncInfo->pCInfoTab[3] = pEncInfo->pCInfoTab[2] + 6;
  1303. if (pop->packedFormat == PACKED_FORMAT_444 && pEncInfo->format != FORMAT_444) {
  1304. return JPG_RET_INVALID_PARAM;
  1305. }
  1306. pEncInfo->packedFormat = pop->packedFormat;
  1307. if (pEncInfo->format == FORMAT_400) {
  1308. pEncInfo->compInfo[1] = 0;
  1309. pEncInfo->compInfo[2] = 0;
  1310. }
  1311. else {
  1312. pEncInfo->compInfo[1] = 5;
  1313. pEncInfo->compInfo[2] = 5;
  1314. }
  1315. if (pEncInfo->format == FORMAT_400) {
  1316. pEncInfo->compNum = 1;
  1317. }
  1318. else
  1319. pEncInfo->compNum = 3;
  1320. if (pEncInfo->format == FORMAT_420) {
  1321. pEncInfo->mcuBlockNum = 6;
  1322. }
  1323. else if (pEncInfo->format == FORMAT_422) {
  1324. pEncInfo->mcuBlockNum = 4;
  1325. } else if (pEncInfo->format == FORMAT_440) { /* aka YUV440 */
  1326. pEncInfo->mcuBlockNum = 4;
  1327. } else if (pEncInfo->format == FORMAT_444) {
  1328. pEncInfo->mcuBlockNum = 3;
  1329. } else if (pEncInfo->format == FORMAT_400) {
  1330. Uint32 picHeight = (90 == pop->rotation || 270 == pop->rotation) ? pEncInfo->picWidth : pEncInfo->picHeight;
  1331. if (0 < pEncInfo->rstIntval && picHeight == pEncInfo->sliceHeight) {
  1332. pEncInfo->mcuBlockNum = 1;
  1333. comp0McuWidth = 8;
  1334. comp0McuHeight = 8;
  1335. }
  1336. else {
  1337. pEncInfo->mcuBlockNum = 4;
  1338. }
  1339. }
  1340. pEncInfo->compInfo[0] = (comp0McuWidth >> 3) << 3 | (comp0McuHeight >> 3);
  1341. pEncInfo->busReqNum = (pop->jpg12bit == FALSE) ? GetEnc8bitBusReqNum(pEncInfo->packedFormat, pEncInfo->format) :
  1342. GetEnc12bitBusReqNum(pEncInfo->packedFormat, pEncInfo->format);
  1343. pEncInfo->tiledModeEnable = pop->tiledModeEnable;
  1344. pEncInfo->encIdx = 0;
  1345. pEncInfo->encSlicePosY = 0;
  1346. pEncInfo->rotationIndex = pop->rotation/90;
  1347. pEncInfo->mirrorIndex = pop->mirror;
  1348. JpgLeaveLock();
  1349. return JPG_RET_SUCCESS;
  1350. }
  1351. JpgRet JPU_EncClose(JpgEncHandle handle)
  1352. {
  1353. JpgInst * pJpgInst;
  1354. JpgRet ret;
  1355. ret = CheckJpgInstValidity(handle);
  1356. if (ret != JPG_RET_SUCCESS)
  1357. return ret;
  1358. JpgEnterLock();
  1359. pJpgInst = handle;
  1360. if (GetJpgPendingInstEx(pJpgInst->instIndex)) {
  1361. JpgLeaveLock();
  1362. return JPG_RET_FRAME_NOT_COMPLETE;
  1363. }
  1364. FreeJpgInstance(pJpgInst);
  1365. JpgLeaveLock();
  1366. return JPG_RET_SUCCESS;
  1367. }
  1368. JpgRet JPU_EncGetBitstreamBuffer( JpgEncHandle handle,
  1369. PhysicalAddress * prdPtr,
  1370. PhysicalAddress * pwrPtr,
  1371. int * size)
  1372. {
  1373. JpgInst * pJpgInst;
  1374. JpgRet ret;
  1375. Int32 instRegIndex;
  1376. ret = CheckJpgInstValidity(handle);
  1377. if (ret != JPG_RET_SUCCESS)
  1378. return ret;
  1379. if ( prdPtr == 0 || pwrPtr == 0 || size == 0) {
  1380. return JPG_RET_INVALID_PARAM;
  1381. }
  1382. pJpgInst = handle;
  1383. if (pJpgInst->sliceInstMode == TRUE) {
  1384. instRegIndex = pJpgInst->instIndex;
  1385. }
  1386. else {
  1387. instRegIndex = 0;
  1388. }
  1389. *pwrPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG);
  1390. *prdPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG);
  1391. *size = *pwrPtr - *prdPtr;
  1392. return JPG_RET_SUCCESS;
  1393. }
  1394. JpgRet JPU_EncUpdateBitstreamBuffer(
  1395. JpgEncHandle handle,
  1396. int size)
  1397. {
  1398. JpgInst * pJpgInst;
  1399. JpgEncInfo * pEncInfo;
  1400. JpgRet ret;
  1401. Int32 instRegIndex;
  1402. ret = CheckJpgInstValidity(handle);
  1403. if (ret != JPG_RET_SUCCESS)
  1404. return ret;
  1405. pJpgInst = handle;
  1406. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1407. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  1408. if (pJpgInst->sliceInstMode == TRUE) {
  1409. instRegIndex = pJpgInst->instIndex;
  1410. }
  1411. else {
  1412. instRegIndex = 0;
  1413. }
  1414. pEncInfo->streamWrPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG);
  1415. pEncInfo->streamRdPtr += size;
  1416. if ((pEncInfo->streamWrPtr >= pEncInfo->streamBufEndAddr) || (size == 0)) { //Full Interrupt case. wrap to the start address
  1417. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, 0);
  1418. JpuWriteInstReg(instRegIndex, MJPEG_BBC_BAS_ADDR_REG, pEncInfo->streamBufStartAddr);
  1419. JpuWriteInstReg(instRegIndex, MJPEG_BBC_EXT_ADDR_REG, pEncInfo->streamBufStartAddr);
  1420. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, pEncInfo->streamBufEndAddr);
  1421. pEncInfo->streamRdPtr = pEncInfo->streamBufStartAddr;
  1422. pEncInfo->streamWrPtr = pEncInfo->streamBufStartAddr;
  1423. JpuWriteInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG, pEncInfo->streamRdPtr);
  1424. JpuWriteInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG, pEncInfo->streamWrPtr);
  1425. }
  1426. }
  1427. else {
  1428. pEncInfo->streamRdPtr = pEncInfo->streamBufStartAddr;
  1429. pEncInfo->streamWrPtr = pEncInfo->streamBufStartAddr;
  1430. }
  1431. return JPG_RET_SUCCESS;
  1432. }
  1433. JpgRet JPU_EncStartOneFrame(JpgEncHandle handle, JpgEncParam * param)
  1434. {
  1435. JpgInst * pJpgInst;
  1436. JpgEncInfo * pEncInfo;
  1437. FrameBuffer * pBasFrame;
  1438. JpgRet ret;
  1439. Uint32 val;
  1440. Int32 instRegIndex;
  1441. BOOL bTableInfoUpdate;
  1442. Uint32 rotMirEnable = 0;
  1443. Uint32 rotMirMode = 0;
  1444. ret = CheckJpgInstValidity(handle);
  1445. if (ret != JPG_RET_SUCCESS)
  1446. return ret;
  1447. pJpgInst = handle;
  1448. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1449. ret = CheckJpgEncParam(handle, param);
  1450. if (ret != JPG_RET_SUCCESS) {
  1451. return ret;
  1452. }
  1453. pBasFrame = param->sourceFrame;
  1454. JpgEnterLock();
  1455. if (GetJpgPendingInstEx(pJpgInst->instIndex) == pJpgInst) {
  1456. JpgLeaveLock();
  1457. return JPG_RET_FRAME_NOT_COMPLETE;
  1458. }
  1459. if (pJpgInst->sliceInstMode == TRUE) {
  1460. instRegIndex = pJpgInst->instIndex;
  1461. }
  1462. else {
  1463. instRegIndex = 0;
  1464. }
  1465. JpuWriteInstReg(instRegIndex, MJPEG_INTR_MASK_REG, ((~pEncInfo->intrEnableBit) & 0x3ff));
  1466. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_INFO_REG, pEncInfo->sliceHeight);
  1467. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_DPB_POS_REG, pEncInfo->picHeight); // assume that the all of source buffer is available
  1468. JpuWriteInstReg(instRegIndex, MJPEG_SLICE_POS_REG, pEncInfo->encSlicePosY);
  1469. val = (0 << 16) | (pEncInfo->encSlicePosY / pEncInfo->mcuHeight);
  1470. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SETMB_REG,val);
  1471. JpuWriteInstReg(instRegIndex, MJPEG_CLP_INFO_REG, 0); //off ROI enable due to not supported feature for encoder.
  1472. JpuWriteInstReg(instRegIndex, MJPEG_BBC_BAS_ADDR_REG, pEncInfo->streamBufStartAddr);
  1473. JpuWriteInstReg(instRegIndex, MJPEG_BBC_END_ADDR_REG, pEncInfo->streamBufEndAddr);
  1474. JpuWriteInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG, pEncInfo->streamWrPtr);
  1475. JpuWriteInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG, pEncInfo->streamRdPtr);
  1476. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, 0);
  1477. JpuWriteInstReg(instRegIndex, MJPEG_BBC_DATA_CNT_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  1478. JpuWriteInstReg(instRegIndex, MJPEG_BBC_EXT_ADDR_REG, pEncInfo->streamBufStartAddr);
  1479. JpuWriteInstReg(instRegIndex, MJPEG_BBC_INT_ADDR_REG, 0);
  1480. JpuWriteInstReg(instRegIndex, MJPEG_BBC_BAS_ADDR_REG, pEncInfo->streamWrPtr);
  1481. JpuWriteInstReg(instRegIndex, MJPEG_BBC_EXT_ADDR_REG, pEncInfo->streamWrPtr);
  1482. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BPTR_REG, 0);
  1483. JpuWriteInstReg(instRegIndex, MJPEG_GBU_WPTR_REG, 0);
  1484. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBSR_REG, 0);
  1485. JpuWriteInstReg(instRegIndex, MJPEG_GBU_CTRL_REG, 0);
  1486. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBER_REG, ((JPU_GBU_SIZE / 4) * 2) - 1);
  1487. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBIR_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  1488. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBHR_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  1489. #define DEFAULT_TDI_TAI_DATA 0x055
  1490. JpuWriteInstReg(instRegIndex, MJPEG_PIC_CTRL_REG, (pEncInfo->jpg12bit<<31) | (pEncInfo->q_prec0<<30) | (pEncInfo->q_prec1<<29) | (pEncInfo->tiledModeEnable<<19) |
  1491. (DEFAULT_TDI_TAI_DATA<<7) | 0x18 | (1<<6) | (JPU_CHECK_WRITE_RESPONSE_BVALID_SIGNAL<<2));
  1492. JpuWriteInstReg(instRegIndex, MJPEG_SCL_INFO_REG, 0);
  1493. val = 0;
  1494. //PackMode[3:0]: 0(NONE), 8(PACK444), 4,5,6,7(YUYV => VYUY)
  1495. val = (pEncInfo->frameEndian << 6) | ((pEncInfo->chromaInterleave==0)?0:(pEncInfo->chromaInterleave==1)?2:3);
  1496. if (pEncInfo->packedFormat == PACKED_FORMAT_NONE) {
  1497. val |= (0<<5) | (0<<4) | (0<<2);
  1498. }
  1499. else if (pEncInfo->packedFormat == PACKED_FORMAT_444) {
  1500. val |= (1<<5) | (0<<4) | (0<<2);
  1501. }
  1502. else {
  1503. val |= (0<<5) | (1<<4) | ((pEncInfo->packedFormat-1)<<2);
  1504. }
  1505. val |= (pEncInfo->pixelJustification << 11);
  1506. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CONFIG_REG, val);
  1507. JpuWriteInstReg(instRegIndex, MJPEG_RST_INTVAL_REG, pEncInfo->rstIntval);
  1508. if (pEncInfo->encSlicePosY == 0) {
  1509. JpuWriteInstReg(instRegIndex, MJPEG_RST_INDEX_REG, 0);// RST index from 0.
  1510. JpuWriteInstReg(instRegIndex, MJPEG_BBC_STRM_CTRL_REG, 0);// clear BBC ctrl status.
  1511. }
  1512. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CTRL_REG, (pEncInfo->streamEndian << 1) | 1);
  1513. JpuWriteInstReg(instRegIndex, MJPEG_OP_INFO_REG, pEncInfo->busReqNum);
  1514. bTableInfoUpdate = FALSE;
  1515. if (pEncInfo->encSlicePosY == 0) {
  1516. bTableInfoUpdate = TRUE; // if sliceMode is disabled. HW process frame by frame between instances. so Huff table can be updated between instances. instances can have the different HuffTable.
  1517. }
  1518. if (bTableInfoUpdate == TRUE) {
  1519. // Load HUFFTab
  1520. if (pEncInfo->jpg12bit){
  1521. if (!JpgEncLoadHuffTab_12b(pJpgInst, instRegIndex)) {
  1522. JpgLeaveLock();
  1523. return JPG_RET_INVALID_PARAM;
  1524. }
  1525. }else{
  1526. if (!JpgEncLoadHuffTab(pJpgInst, instRegIndex)) {
  1527. JpgLeaveLock();
  1528. return JPG_RET_INVALID_PARAM;
  1529. }
  1530. }
  1531. }
  1532. bTableInfoUpdate = FALSE;
  1533. if (pEncInfo->encSlicePosY == 0) {
  1534. bTableInfoUpdate = TRUE;
  1535. }
  1536. if (bTableInfoUpdate == TRUE) {
  1537. // Load QMATTab
  1538. if (!JpgEncLoadQMatTab(pJpgInst, instRegIndex)) {
  1539. JpgLeaveLock();
  1540. return JPG_RET_INVALID_PARAM;
  1541. }
  1542. }
  1543. JpuWriteInstReg(instRegIndex, MJPEG_PIC_SIZE_REG, pEncInfo->alignedWidth<<16 | pEncInfo->alignedHeight);
  1544. if (pEncInfo->rotationIndex || pEncInfo->mirrorIndex) {
  1545. rotMirEnable = 0x10;
  1546. rotMirMode = (pEncInfo->mirrorIndex << 2) | pEncInfo->rotationIndex;
  1547. }
  1548. JpuWriteInstReg(instRegIndex, MJPEG_ROT_INFO_REG, rotMirEnable | rotMirMode);
  1549. JpuWriteInstReg(instRegIndex, MJPEG_MCU_INFO_REG, (pEncInfo->mcuBlockNum&0x0f) << 17 | (pEncInfo->compNum&0x07) << 14 |
  1550. (pEncInfo->compInfo[0]&0x3f) << 8 | (pEncInfo->compInfo[1]&0x0f) << 4 |
  1551. (pEncInfo->compInfo[2]&0x0f));
  1552. //JpgEncGbuResetReg
  1553. JpuWriteInstReg(instRegIndex, MJPEG_GBU_CTRL_REG, pEncInfo->stuffByteEnable<<3); // stuffing "FF" data where frame end
  1554. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE00_REG, pBasFrame->bufY);
  1555. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE01_REG, pBasFrame->bufCb);
  1556. JpuWriteInstReg(instRegIndex, MJPEG_DPB_BASE02_REG, pBasFrame->bufCr);
  1557. JpuWriteInstReg(instRegIndex, MJPEG_DPB_YSTRIDE_REG, pBasFrame->stride);
  1558. JpuWriteInstReg(instRegIndex, MJPEG_DPB_CSTRIDE_REG, pBasFrame->strideC);
  1559. if (pJpgInst->loggingEnable)
  1560. jdi_log(JDI_LOG_CMD_PICRUN, 1, instRegIndex);
  1561. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, (1<<JPG_ENABLE_START_PIC));
  1562. pEncInfo->encIdx++;
  1563. SetJpgPendingInstEx(pJpgInst, pJpgInst->instIndex);
  1564. if (pJpgInst->sliceInstMode == TRUE) {
  1565. JpgLeaveLock();
  1566. }
  1567. return JPG_RET_SUCCESS;
  1568. }
  1569. JpgRet JPU_EncGetOutputInfo(
  1570. JpgEncHandle handle,
  1571. JpgEncOutputInfo * info
  1572. )
  1573. {
  1574. JpgInst * pJpgInst;
  1575. JpgEncInfo * pEncInfo;
  1576. Uint32 val;
  1577. Uint32 intReason;
  1578. JpgRet ret;
  1579. Int32 instRegIndex;
  1580. ret = CheckJpgInstValidity(handle);
  1581. if (ret != JPG_RET_SUCCESS) {
  1582. return ret;
  1583. }
  1584. if (info == 0) {
  1585. return JPG_RET_INVALID_PARAM;
  1586. }
  1587. pJpgInst = handle;
  1588. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1589. if (pJpgInst->sliceInstMode == TRUE) {
  1590. JpgEnterLock();
  1591. }
  1592. if (pJpgInst != GetJpgPendingInstEx(pJpgInst->instIndex)) {
  1593. JpgLeaveLock();
  1594. return JPG_RET_WRONG_CALL_SEQUENCE;
  1595. }
  1596. if (pJpgInst->sliceInstMode == TRUE) {
  1597. instRegIndex = pJpgInst->instIndex;
  1598. }
  1599. else {
  1600. instRegIndex = 0;
  1601. }
  1602. info->frameCycle = JpuReadInstReg(instRegIndex, MJPEG_CYCLE_INFO_REG);
  1603. intReason = JpuReadInstReg(instRegIndex, MJPEG_PIC_STATUS_REG);
  1604. if ((intReason & 0x4) >> 2) {
  1605. JpgLeaveLock();
  1606. return JPG_RET_WRONG_CALL_SEQUENCE;
  1607. }
  1608. info->encodedSliceYPos = JpuReadInstReg(instRegIndex, MJPEG_SLICE_POS_REG);
  1609. pEncInfo->encSlicePosY = info->encodedSliceYPos;
  1610. if (intReason & (1<<INT_JPU_DONE))
  1611. pEncInfo->encSlicePosY = 0;
  1612. pEncInfo->streamWrPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_WR_PTR_REG);
  1613. pEncInfo->streamRdPtr = JpuReadInstReg(instRegIndex, MJPEG_BBC_RD_PTR_REG);
  1614. info->bitstreamBuffer = pEncInfo->streamRdPtr;
  1615. info->bitstreamSize = pEncInfo->streamWrPtr - pEncInfo->streamRdPtr;
  1616. info->streamWrPtr = pEncInfo->streamWrPtr;
  1617. info->streamRdPtr = pEncInfo->streamRdPtr;
  1618. if (intReason != 0) {
  1619. JpuWriteInstReg(instRegIndex, MJPEG_PIC_STATUS_REG, intReason);
  1620. if (intReason & (1<<INT_JPU_SLICE_DONE))
  1621. info->encodeState = ENCODE_STATE_SLICE_DONE;
  1622. if (intReason & (1<<INT_JPU_DONE))
  1623. info->encodeState = ENCODE_STATE_FRAME_DONE;
  1624. }
  1625. #ifndef JPU_IRQ_CONTROL
  1626. info->encodeState = ENCODE_STATE_FRAME_DONE;
  1627. #endif
  1628. if (pJpgInst->loggingEnable)
  1629. jdi_log(JDI_LOG_CMD_PICRUN, 0, instRegIndex);
  1630. JpuWriteInstReg(instRegIndex, MJPEG_PIC_START_REG, 0);
  1631. val = JpuReadReg(MJPEG_INST_CTRL_STATUS_REG);
  1632. val &= ~(1UL<<instRegIndex);
  1633. JpuWriteReg(MJPEG_INST_CTRL_STATUS_REG, val);
  1634. SetJpgPendingInstEx(0, pJpgInst->instIndex);
  1635. JpgLeaveLock();
  1636. return JPG_RET_SUCCESS;
  1637. }
  1638. JpgRet JPU_EncGiveCommand(
  1639. JpgEncHandle handle,
  1640. JpgCommand cmd,
  1641. void * param)
  1642. {
  1643. JpgInst * pJpgInst;
  1644. JpgEncInfo * pEncInfo;
  1645. JpgRet ret;
  1646. ret = CheckJpgInstValidity(handle);
  1647. if (ret != JPG_RET_SUCCESS)
  1648. return ret;
  1649. pJpgInst = handle;
  1650. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1651. switch (cmd)
  1652. {
  1653. case ENC_JPG_GET_HEADER:
  1654. {
  1655. if (param == 0) {
  1656. return JPG_RET_INVALID_PARAM;
  1657. }
  1658. if (!JpgEncEncodeHeader(handle, param)) {
  1659. return JPG_RET_INVALID_PARAM;
  1660. }
  1661. break;
  1662. }
  1663. case SET_JPG_USE_STUFFING_BYTE_FF:
  1664. {
  1665. int enable;
  1666. enable = *(int *)param;
  1667. pEncInfo->stuffByteEnable = enable;
  1668. break;
  1669. }
  1670. case SET_JPG_QUALITY_FACTOR:
  1671. {
  1672. Uint32 encQualityPercentage;
  1673. encQualityPercentage = *(Uint32*)param;
  1674. JpgEncSetQualityFactor(handle, encQualityPercentage, TRUE);
  1675. break;
  1676. }
  1677. case ENABLE_LOGGING:
  1678. {
  1679. pJpgInst->loggingEnable = 1;
  1680. }
  1681. break;
  1682. case DISABLE_LOGGING:
  1683. {
  1684. pJpgInst->loggingEnable = 0;
  1685. }
  1686. break;
  1687. default:
  1688. return JPG_RET_INVALID_COMMAND;
  1689. }
  1690. return JPG_RET_SUCCESS;
  1691. }