jpuapi.c 63 KB

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