drv_jpegcodec.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-4-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include "rtthread.h"
  13. #include "NuMicro.h"
  14. #include "nu_jpeg.h"
  15. #include "drv_sys.h"
  16. struct nu_jpeg
  17. {
  18. struct rt_device dev;
  19. char *name;
  20. IRQn_Type irqn;
  21. E_SYS_IPRST rstidx;
  22. E_SYS_IPCLK clkidx;
  23. struct rt_mutex lock;
  24. };
  25. typedef struct nu_jpeg *nu_jpeg_t;
  26. static BOOL volatile g_bWait = FALSE, g_jpegError = FALSE, g_bScale = FALSE, g_OutputWait = FALSE, g_InputWait = FALSE, g_u32WindowDec = FALSE, g_bEncThumbnailDownScale = FALSE, g_bEncPrimaryDownScale = FALSE;
  27. static UINT32 volatile g_u32Stride, g_u32ScaleWidth, g_u32ScaleHeight, g_u32OpMode, g_u32EncRotate = 0;
  28. static UINT32 volatile g_u32BufferCount, g_u32DecInputWaitAddr;
  29. static UINT16 volatile g_u16BufferSize, g_u16ReserveSize;
  30. static UINT32 volatile g_u32OutputFormat, g_u32windowSizeX, g_u32windowSizeY;
  31. static PFN_JPEG_CALLBACK pfnJpegDecodeComplete = NULL;
  32. static PFN_JPEG_CALLBACK pfnJpegDecodeError = NULL;
  33. static PFN_JPEG_CALLBACK pfnJpegEncodeComplete = NULL;
  34. static PFN_JPEG_HEADERDECODE_CALLBACK pfnJpegHeaderDecode = NULL;
  35. static PFN_JPEG_DECWAIT_CALLBACK pfnJpegDecInputWait = NULL;
  36. static PFN_JPEG_DECWAIT_CALLBACK pfnJpegDecOutputWait = NULL;
  37. static JPEG_INFO_T jpegInfo;
  38. /* Default Quantization-Table 0 ~ 2 */
  39. static UINT8 g_au8QTable0[64] = { 0x06, 0x04, 0x04, 0x05, 0x04, 0x04, 0x06, 0x05,
  40. 0x05, 0x05, 0x06, 0x06, 0x06, 0x07, 0x08, 0x0E,
  41. 0x09, 0x08, 0x08, 0x08, 0x08, 0x11, 0x0C, 0x0D,
  42. 0x0A, 0x0E, 0x14, 0x11, 0x15, 0x14, 0x13, 0x11,
  43. 0x13, 0x13, 0x16, 0x18, 0x1F, 0x1A, 0x16, 0x17,
  44. 0x1D, 0x17, 0x13, 0x13, 0x1B, 0x25, 0x1B, 0x1D,
  45. 0x20, 0x21, 0x23, 0x23, 0x23, 0x15, 0x1A, 0x26,
  46. 0x29, 0x26, 0x22, 0x28, 0x1F, 0x22, 0x23, 0x21
  47. },
  48. g_au8QTable1[64] = { 0x06, 0x06, 0x06, 0x08, 0x07, 0x08, 0x10, 0x09,
  49. 0x09, 0x10, 0x21, 0x16, 0x13, 0x16, 0x21, 0x21,
  50. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
  51. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
  52. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
  53. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
  54. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
  55. 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21
  56. };
  57. static struct nu_jpeg g_sNuJpeg =
  58. {
  59. .name = "jpegcodec",
  60. .irqn = JPEG_IRQn,
  61. .rstidx = JPEGRST,
  62. .clkidx = JPEGCKEN
  63. };
  64. /* Interrupt Service Routine for H/W JPEG CODEC */
  65. static void nu_jpeg_isr(int vector, void *param)
  66. {
  67. UINT32 u32interruptStatus;
  68. /* Get the interrupt status */
  69. u32interruptStatus = JPEG_GET_INT_STATUS();
  70. /* It's Header Decode Complete Interrupt */
  71. if (u32interruptStatus & DHE_INTS)
  72. {
  73. UINT32 u32YuvFormat;
  74. UINT16 u16Height, u16Width, UVHeight, UVWidth;
  75. UINT16 u16WidthTmp, u16HeightTmp;
  76. /* Get the JPEG format */
  77. u32YuvFormat = JPEG_DEC_GET_DECODED_IMAGE_FORMAT();
  78. /* Get the decoded image dimension */
  79. jpegGetDecodedDimension(&u16Height, &u16Width);
  80. jpegInfo.jpeg_width = u16Width;
  81. jpegInfo.jpeg_height = u16Height;
  82. jpegInfo.yuvformat = u32YuvFormat;
  83. if (pfnJpegHeaderDecode != NULL)
  84. {
  85. if (!pfnJpegHeaderDecode())
  86. {
  87. jpegInit();
  88. g_bWait = FALSE;
  89. return;
  90. }
  91. }
  92. if (g_bScale)
  93. {
  94. UINT16 u16RatioH, u16RatioW;
  95. if (jpegCalScalingFactor(
  96. g_u32OpMode, //Up / Down Scaling
  97. u16Height, //Original Height
  98. u16Width, //Original Width
  99. g_u32ScaleHeight, //Scaled Height
  100. g_u32ScaleWidth, //Scaled Width
  101. &u16RatioH, //Horizontal Ratio
  102. &u16RatioW //Vertical Ratio
  103. ) != E_SUCCESS)
  104. {
  105. g_bWait = FALSE;
  106. g_jpegError = TRUE;
  107. }
  108. else
  109. {
  110. jpegSetScalingFactor(g_u32OpMode, u16RatioH, u16RatioW);
  111. u16Width = g_u32ScaleWidth;
  112. u16Height = g_u32ScaleHeight;
  113. }
  114. }
  115. else
  116. {
  117. if (u32YuvFormat == JPEG_DEC_YUV411)
  118. {
  119. /* 32-pixel alignment for YUV411 raw data */
  120. if (u16Width % 32)
  121. u16Width = (u16Width & 0xFFFFFFE0) + 32;
  122. }
  123. else if ((u32YuvFormat == JPEG_DEC_YUV444) || (u32YuvFormat == JPEG_DEC_YUV422T))
  124. {
  125. /* 8-pixel alignment for YUV444 raw data */
  126. if (u16Width % 8)
  127. u16Width = (u16Width & 0xFFFFFFF8) + 8;
  128. }
  129. else
  130. {
  131. /* 16-pixel alignment for YUV422 or YUV420 raw data */
  132. if (u16Width % 16)
  133. u16Width = (u16Width & 0xFFFFFFF0) + 16;
  134. }
  135. }
  136. if (g_u32Stride >= u16Width)
  137. {
  138. jpegInfo.stride = g_u32Stride;
  139. g_u32Stride = g_u32Stride - u16Width;
  140. JPEG_SET_YSTRIDE(g_u32Stride);
  141. u16Width = jpegInfo.stride;
  142. }
  143. else
  144. {
  145. g_u32Stride = 0;
  146. JPEG_SET_YSTRIDE(0);
  147. jpegInfo.stride = 0;
  148. }
  149. if (g_u32OutputFormat == JPEG_DEC_PRIMARY_PLANAR_YUV || g_u32OutputFormat == JPEG_DEC_THUMBNAIL_PLANAR_YUV)
  150. {
  151. if (g_u32WindowDec)
  152. {
  153. u16WidthTmp = u16Width;
  154. u16HeightTmp = u16Height;
  155. u16Width = g_u32windowSizeX;
  156. u16Height = g_u32windowSizeY;
  157. }
  158. if (u32YuvFormat == JPEG_DEC_YUV411)
  159. {
  160. /* For YUV411 raw data */
  161. UVWidth = u16Width / 4;
  162. }
  163. else if ((u32YuvFormat == JPEG_DEC_YUV444) || (u32YuvFormat == JPEG_DEC_YUV422T))
  164. {
  165. /* For YUV444 raw data */
  166. UVWidth = u16Width;
  167. }
  168. /* Set the U-component and V-componente width for YUV422 or YUV420 raw data */
  169. else if (u16Width % 2)
  170. UVWidth = u16Width / 2 + 1;
  171. else
  172. UVWidth = u16Width / 2;
  173. /* Sets the height of U and V for YUV420 image */
  174. if (u32YuvFormat == JPEG_DEC_YUV420)
  175. {
  176. /* 16-pixel alignment for YUV422 or YUV420 raw data */
  177. if (u16Height % 16)
  178. u16Height = (u16Height & 0xFFFFFFF0) + 16;
  179. UVHeight = u16Height / 2;
  180. }
  181. else if (u32YuvFormat == JPEG_DEC_YUV422)
  182. {
  183. /* 8-pixel alignment for YUV444 raw data */
  184. if (u16Height % 8)
  185. u16Height = (u16Height & 0xFFFFFFF8) + 8;
  186. UVHeight = u16Height;
  187. }
  188. else if (u32YuvFormat == JPEG_DEC_YUV444)
  189. {
  190. /* 8-pixel alignment for YUV444 raw data */
  191. if (u16Height % 8)
  192. u16Height = (u16Height & 0xFFFFFFF8) + 8;
  193. UVHeight = u16Height;
  194. }
  195. else if (u32YuvFormat == JPEG_DEC_YUV411)
  196. {
  197. /* 8-pixel alignment for YUV411 raw data */
  198. if (u16Height % 8)
  199. u16Height = (u16Height & 0xFFFFFFF8) + 8;
  200. UVHeight = u16Height;
  201. }
  202. else if (u32YuvFormat == JPEG_DEC_YUV422T)
  203. {
  204. /* 16-pixel alignment for YUV422 or YUV420 raw data */
  205. if (u16Height % 16)
  206. u16Height = (u16Height & 0xFFFFFFF0) + 16;
  207. UVHeight = u16Height / 2;
  208. }
  209. else
  210. {
  211. /* 8-pixel alignment for raw data */
  212. if (u16Height % 8)
  213. u16Height = (u16Height & 0xFFFFFFF8) + 8;
  214. UVHeight = u16Height;
  215. }
  216. JPEG_SET_UADDR(JPEG_GET_YADDR() + u16Width * u16Height);
  217. JPEG_SET_VADDR(JPEG_GET_UADDR() + UVWidth * UVHeight);
  218. if (u32YuvFormat == JPEG_DEC_GRAY)
  219. jpegInfo.image_size[0] = u16Width * u16Height;
  220. else
  221. jpegInfo.image_size[0] = u16Width * u16Height + 2 * UVWidth * UVHeight;
  222. if (g_u32WindowDec)
  223. {
  224. u16Width = u16WidthTmp;
  225. u16Height = u16HeightTmp;
  226. }
  227. }
  228. else
  229. {
  230. if (g_u32WindowDec)
  231. {
  232. u16WidthTmp = u16Width;
  233. u16HeightTmp = u16Height;
  234. u16Width = g_u32windowSizeX;
  235. u16Height = g_u32windowSizeY;
  236. }
  237. if (jpegInfo.stride)
  238. jpegInfo.image_size[0] = jpegInfo.stride * u16Height;
  239. else
  240. jpegInfo.image_size[0] = u16Width * u16Height;
  241. if (g_u32OutputFormat == JPEG_DEC_PRIMARY_PACKET_RGB888)
  242. jpegInfo.image_size[0] = 4 * jpegInfo.image_size[0];
  243. else
  244. jpegInfo.image_size[0] = 2 * jpegInfo.image_size[0];
  245. if (g_u32WindowDec)
  246. {
  247. u16Width = u16WidthTmp;
  248. u16Height = u16HeightTmp;
  249. }
  250. }
  251. /* Set the image dimension */
  252. jpegSetDimension(u16Height, u16Width);
  253. outp32(JITCR, inp32(JITCR) | Dec_Scatter_Gather);
  254. /* Clear interrupt status */
  255. JPEG_CLEAR_INT(DHE_INTS);
  256. }
  257. /* It's Decode Output Wait Interrupt */
  258. else if (u32interruptStatus & JPG_DOW_INTS)
  259. {
  260. if (pfnJpegDecOutputWait != NULL)
  261. {
  262. pfnJpegDecOutputWait(inp32(JYADDR0), inp32(JDOWFBS));
  263. }
  264. JPEG_CLEAR_INT(JPG_DOW_INTS);
  265. outp32(JITCR, inp32(JITCR) | Dec_Scatter_Gather);
  266. }
  267. /* It's Encode Complete Interrupt */
  268. else if (u32interruptStatus & ENC_INTS)
  269. {
  270. /* Get the Encode Bit stream length */
  271. jpegInfo.image_size[0] = JPEG_GET_ENC_PRIMARY_BITSTREAM_SIZE();
  272. jpegInfo.image_size[1] = JPEG_GET_ENC_THUMBNAIL_BITSTREAM_SIZE();
  273. /* Clear interrupt status */
  274. JPEG_CLEAR_INT(ENC_INTS);
  275. g_bWait = FALSE;
  276. if (pfnJpegEncodeComplete != NULL)
  277. pfnJpegEncodeComplete();
  278. }
  279. /* It's Decode Complete Interrupt */
  280. else if (u32interruptStatus & DEC_INTS)
  281. {
  282. UINT16 imageWidth, imageHeight;
  283. /* Get the image dimension */
  284. jpegGetDimension(&imageHeight, &imageWidth);
  285. if (g_u32Stride != 0)
  286. {
  287. imageWidth = imageWidth - g_u32Stride;
  288. }
  289. jpegInfo.width = imageWidth;
  290. jpegInfo.height = imageHeight;
  291. /* Clear interrupt status */
  292. JPEG_CLEAR_INT(DEC_INTS);
  293. JPEG_CLEAR_INT(JPG_DOW_INTS);
  294. g_bWait = FALSE;
  295. if (pfnJpegDecodeComplete != NULL)
  296. pfnJpegDecodeComplete();
  297. }
  298. /* It's Decode Error Interrupt */
  299. else if (u32interruptStatus & DER_INTS)
  300. {
  301. /* Clear interrupt status */
  302. JPEG_CLEAR_INT(DER_INTS);
  303. g_bWait = FALSE;
  304. g_jpegError = TRUE;
  305. if (pfnJpegDecodeError != NULL)
  306. pfnJpegDecodeError();
  307. }
  308. else if (u32interruptStatus & IPW_INTS)
  309. {
  310. /* Clear interrupt status */
  311. JPEG_CLEAR_INT(IPW_INTS);
  312. JPEG_DEC_RESUME_INPUT_WAIT();
  313. if (pfnJpegDecInputWait != NULL)
  314. {
  315. if (!pfnJpegDecInputWait((g_u32DecInputWaitAddr + (g_u32BufferCount % 2) * g_u16BufferSize), g_u16BufferSize))
  316. {
  317. jpegInit();
  318. g_bWait = FALSE;
  319. return;
  320. }
  321. g_u32BufferCount++;
  322. }
  323. else
  324. while (1);
  325. }
  326. }
  327. static INT jpegAdjustQTAB(UINT8 u8Mode, UINT8 u8Qadjust, UINT8 u8Qscaling)
  328. {
  329. UINT32 u32Addr;
  330. if (u8Mode == JPEG_ENC_PRIMARY)
  331. u32Addr = REG_JPRIQC;
  332. else if (u8Mode == JPEG_ENC_THUMBNAIL)
  333. u32Addr = REG_JTHBQC;
  334. else
  335. return E_JPEG_INVALID_PARAM;
  336. outp32(u32Addr, ((u8Qadjust & 0xF) << 4) | (u8Qscaling & 0xF));
  337. return E_SUCCESS;
  338. }
  339. #if 0
  340. //Poll the interrupt status and get if the interrupt is generated
  341. static BOOL jpegPollInt(UINT32 u32Intflag)
  342. {
  343. if (JPEG_GET_INT_STATUS() & u32Intflag)
  344. return 1;
  345. else
  346. return 0;
  347. }
  348. static UINT32 jpegPower(UINT32 u32Index, UINT32 u32Exp)
  349. {
  350. if (u32Exp == 0)
  351. return 1;
  352. else
  353. {
  354. UINT32 u32Idx;
  355. for (u32Idx = 1; u32Idx < u32Exp; u32Idx = u32Idx + 1)
  356. {
  357. u32Index = 2 * u32Index;
  358. }
  359. }
  360. return u32Index;
  361. }
  362. static VOID jpegGetScalingFactor(
  363. UINT8 u8Mode, //Up / Down Scaling
  364. PUINT16 pu16FactorH, //Vertical Scaling Factor
  365. PUINT16 pu16FactorW //Horizontal Scaling Factor
  366. )
  367. {
  368. if (u8Mode == JPEG_DEC_PLANAR_DOWNSCALE_MODE)
  369. {
  370. *pu16FactorH = inp32(REG_JPSCALD) & 0x3F;
  371. *pu16FactorW = (inp32(REG_JPSCALD) >> 8) & 0x1F;
  372. }
  373. else
  374. {
  375. *pu16FactorH = (inp32(REG_JUPRAT) >> 16) & 0x3FFF;
  376. *pu16FactorW = inp32(REG_JUPRAT) & 0x3FFF;
  377. }
  378. }
  379. #endif
  380. static INT jpegSetEncodeMode(UINT8 u8SourceFormat, UINT16 u16JpegFormat)
  381. {
  382. UINT8 u8Gray = 0;
  383. switch (u16JpegFormat)
  384. {
  385. case JPEG_ENC_PRIMARY_YUV420:
  386. case JPEG_ENC_PRIMARY_YUV422:
  387. case JPEG_ENC_THUMBNAIL_YUV420:
  388. case JPEG_ENC_THUMBNAIL_YUV422:
  389. case (JPEG_ENC_PRIMARY_YUV420 | JPEG_ENC_THUMBNAIL_YUV420):
  390. case (JPEG_ENC_PRIMARY_YUV422 | JPEG_ENC_THUMBNAIL_YUV422):
  391. outp32(REG_JMCR, (inp32(REG_JMCR) & WIN_DEC) | u16JpegFormat);
  392. u8Gray = 0;
  393. break;
  394. case JPEG_ENC_PRIMARY_GRAY:
  395. case JPEG_ENC_THUMBNAIL_GRAY:
  396. case (JPEG_ENC_PRIMARY_GRAY | JPEG_ENC_THUMBNAIL_GRAY):
  397. if (u8SourceFormat == JPEG_ENC_SOURCE_PACKET)
  398. return E_JPEG_INVALID_PARAM;
  399. else
  400. {
  401. if (u16JpegFormat == (JPEG_ENC_PRIMARY_GRAY | JPEG_ENC_THUMBNAIL_GRAY))
  402. outp32(REG_JMCR, 0xB0);
  403. else
  404. outp32(REG_JMCR, 0xA0);
  405. }
  406. u8Gray = EY_ONLY;
  407. break;
  408. default:
  409. return E_JPEG_INVALID_PARAM;
  410. }
  411. g_u32OpMode = JPEG_ENC_UPSCALE_MODE;
  412. if (g_bEncPrimaryDownScale)
  413. g_u32OpMode = JPEG_ENC_PLANAR_PRIMARY_DOWNSCALE_MODE;
  414. if (g_bEncThumbnailDownScale)
  415. g_u32OpMode = JPEG_ENC_PLANAR_THUMBNAIL_DOWNSCALE_MODE;
  416. if (u8SourceFormat == JPEG_ENC_SOURCE_PLANAR)
  417. outp32(REG_JITCR, (inp32(REG_JITCR) & (0x8 | ROTATE)) | PLANAR_ON | u8Gray);
  418. else if (u8SourceFormat == JPEG_ENC_SOURCE_PACKET)
  419. outp32(REG_JITCR, inp32(REG_JITCR) & ~(PLANAR_ON | ROTATE));
  420. else
  421. return E_JPEG_INVALID_PARAM;
  422. return E_SUCCESS;
  423. }
  424. static INT jpegSetDecodeMode(UINT32 u32OutputFormat)
  425. {
  426. switch (u32OutputFormat)
  427. {
  428. case JPEG_DEC_PRIMARY_PLANAR_YUV:
  429. case JPEG_DEC_PRIMARY_PACKET_YUV422:
  430. case JPEG_DEC_PRIMARY_PACKET_RGB555:
  431. case JPEG_DEC_PRIMARY_PACKET_RGB555R1:
  432. case JPEG_DEC_PRIMARY_PACKET_RGB555R2:
  433. case JPEG_DEC_THUMBNAIL_PLANAR_YUV:
  434. case JPEG_DEC_THUMBNAIL_PACKET_YUV422:
  435. case JPEG_DEC_THUMBNAIL_PACKET_RGB555:
  436. case JPEG_DEC_PRIMARY_PACKET_RGB565:
  437. case JPEG_DEC_PRIMARY_PACKET_RGB565R1:
  438. case JPEG_DEC_PRIMARY_PACKET_RGB565R2:
  439. case JPEG_DEC_PRIMARY_PACKET_RGB888:
  440. outp32(REG_JITCR, u32OutputFormat);
  441. outp32(REG_JMCR, inp32(REG_JMCR) & ~ENC_DEC);
  442. g_u32OpMode = JPEG_DEC_PACKET_DOWNSCALE_MODE;
  443. g_u32OutputFormat = u32OutputFormat;
  444. if (u32OutputFormat == JPEG_DEC_PRIMARY_PLANAR_YUV || u32OutputFormat == JPEG_DEC_THUMBNAIL_PLANAR_YUV)
  445. g_u32OpMode = JPEG_DEC_PLANAR_DOWNSCALE_MODE;
  446. break;
  447. default:
  448. return E_JPEG_INVALID_PARAM;
  449. }
  450. return E_SUCCESS;
  451. }
  452. static VOID jpegDecodeTrigger(void)
  453. {
  454. g_bWait = TRUE;
  455. g_jpegError = FALSE;
  456. rt_memset(&jpegInfo, 0, sizeof(jpegInfo));
  457. /* Decode Complete /Decode Header End/Decode Error Interrupt Enable and clear the Decode Complete /Decode Header End/Decode Error Interrupt */
  458. if (g_InputWait)
  459. {
  460. g_u32BufferCount = 0;
  461. g_u32DecInputWaitAddr = JPEG_GET_BITSTREAM_ADDR();
  462. if (g_OutputWait)
  463. JPEG_INT_ENABLE(DEC_INTE | DER_INTE | DHE_INTE | IPW_INTE | JPG_DOW_INTE);
  464. else
  465. JPEG_INT_ENABLE(DEC_INTE | DER_INTE | DHE_INTE | IPW_INTE);
  466. }
  467. else if (g_OutputWait)
  468. {
  469. JPEG_INT_ENABLE(DEC_INTE | DER_INTE | DHE_INTE | JPG_DOW_INTE);
  470. }
  471. else
  472. JPEG_INT_ENABLE(DEC_INTE | DER_INTE | DHE_INTE);
  473. JPEG_CLEAR_INT(DEC_INTS | JPEG_DER_INTS | JPEG_DHE_INTS | JPEG_IPW_INTS | JPG_DOW_INTS);
  474. outp32(REG_JMCR, JPG_EN | inp32(REG_JMCR));
  475. outp32(REG_JMCR, ~JPG_EN & inp32(REG_JMCR));
  476. }
  477. static VOID jpegEncodeTrigger(void)
  478. {
  479. g_bWait = TRUE;
  480. g_jpegError = FALSE;
  481. g_u32OpMode = JPEG_ENC_UPSCALE_MODE;
  482. if (g_bEncPrimaryDownScale)
  483. g_u32OpMode = JPEG_ENC_PLANAR_PRIMARY_DOWNSCALE_MODE;
  484. if (g_bEncThumbnailDownScale)
  485. g_u32OpMode = JPEG_ENC_PLANAR_THUMBNAIL_DOWNSCALE_MODE;
  486. rt_memset(&jpegInfo, 0, sizeof(jpegInfo));
  487. if (g_u32EncRotate != 0)
  488. {
  489. UINT16 u16Height, u16Width;
  490. if (((inp32(REG_JITCR) & (PLANAR_ON | EY_ONLY)) != (PLANAR_ON)) && ((inp32(REG_JMCR) & EY422) != 0))
  491. {
  492. g_jpegError = TRUE;
  493. g_bWait = FALSE;
  494. return;
  495. }
  496. jpegGetDimension(&u16Width, &u16Height);
  497. if (g_u32EncRotate == JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_LEFT)
  498. {
  499. JPEG_SET_YADDR((JPEG_GET_YADDR() + (u16Width - 1)));
  500. JPEG_SET_UADDR((JPEG_GET_UADDR() + (u16Width / 2 - 1)));
  501. JPEG_SET_VADDR((JPEG_GET_VADDR() + (u16Width / 2 - 1)));
  502. }
  503. else
  504. {
  505. JPEG_SET_YADDR((JPEG_GET_YADDR() + ((u16Height - 1) * u16Width)));
  506. u16Width = JPEG_GET_USTRIDE();
  507. JPEG_SET_UADDR((JPEG_GET_UADDR() + ((u16Height - 2) * u16Width / 2)));
  508. JPEG_SET_VADDR((JPEG_GET_VADDR() + ((u16Height - 2) * u16Width / 2)));
  509. }
  510. }
  511. if (g_bScale)
  512. {
  513. UINT16 u16Height, u16Width, u16ratioH, u16ratioW;
  514. if (g_u32EncRotate != 0)
  515. jpegGetDimension(&u16Width, &u16Height);
  516. else
  517. jpegGetDimension(&u16Height, &u16Width);
  518. if (jpegCalScalingFactor(
  519. g_u32OpMode, //Up / Down Scaling
  520. u16Height, //Original Height
  521. u16Width, //Original Width
  522. g_u32ScaleHeight, //Scaled Height
  523. g_u32ScaleWidth, //Scaled Width
  524. &u16ratioH, //Horizontal Ratio
  525. &u16ratioW //Vertical Ratio
  526. ) != E_SUCCESS)
  527. {
  528. g_jpegError = TRUE;
  529. g_bWait = FALSE;
  530. return;
  531. }
  532. else
  533. {
  534. jpegSetScalingFactor(g_u32OpMode, u16ratioH, u16ratioW);
  535. if (g_bEncThumbnailDownScale)
  536. outp32(REG_JTHBWH, ((g_u32ScaleHeight & 0x1FFF) << 16) | (g_u32ScaleWidth & 0x1FFF));
  537. else
  538. {
  539. if (g_u32EncRotate != 0)
  540. jpegSetDimension(g_u32ScaleWidth, g_u32ScaleHeight);
  541. else
  542. jpegSetDimension(g_u32ScaleHeight, g_u32ScaleWidth);
  543. }
  544. }
  545. }
  546. /* Encode Complete Interrupt Enable and clear the Encode Complete Interrupt */
  547. JPEG_INT_ENABLE(ENC_INTE);
  548. JPEG_CLEAR_INT(ENC_INTS);
  549. outp32(REG_JMCR, JPG_EN | inp32(REG_JMCR));
  550. outp32(REG_JMCR, ~JPG_EN & inp32(REG_JMCR));
  551. }
  552. static INT jpegCalScalingFactor(
  553. UINT8 u8Mode, //Up / Down Scaling
  554. UINT16 u16Height, //Original Height
  555. UINT16 u16Width, //Original Width
  556. UINT16 u16ScalingHeight, //Scaled Height
  557. UINT16 u16ScalingWidth, //Scaled Width
  558. PUINT16 pu16RatioH, //Horizontal Ratio
  559. PUINT16 pu16RatioW //Vertical Ratio
  560. )
  561. {
  562. if (u8Mode == JPEG_ENC_UPSCALE_MODE)
  563. {
  564. if (u16ScalingHeight < u16Height || u16ScalingWidth < u16Width)
  565. return E_JPEG_INVALID_PARAM;
  566. *pu16RatioW = (UINT32)((float)(u16ScalingWidth - 1) / (float)(u16Width - 2) * 1024);
  567. *pu16RatioH = (UINT32)((float)(u16ScalingHeight - 1) / (float)(u16Height - 2) * 1024);
  568. }
  569. else if (u8Mode == JPEG_DEC_PACKET_DOWNSCALE_MODE)
  570. {
  571. if (u16ScalingHeight > u16Height || u16ScalingWidth > u16Width)
  572. return E_JPEG_INVALID_PARAM;
  573. *pu16RatioW = (UINT32)(((float)(u16ScalingWidth) / (u16Width - 1) * 8192));
  574. if (*pu16RatioW > 8192)
  575. *pu16RatioW = 8192;
  576. *pu16RatioH = (UINT32)((float)(u16ScalingHeight) / (u16Height - 1) * 8192);
  577. if (*pu16RatioH > 8192)
  578. *pu16RatioH = 8192;
  579. }
  580. else if (u8Mode == JPEG_DEC_PLANAR_DOWNSCALE_MODE || u8Mode == JPEG_ENC_PLANAR_PRIMARY_DOWNSCALE_MODE || u8Mode == JPEG_ENC_PLANAR_THUMBNAIL_DOWNSCALE_MODE)
  581. {
  582. UINT16 u16RatioW, u16RatioH;
  583. if (u16ScalingHeight > u16Height || u16ScalingWidth > u16Width)
  584. return E_JPEG_INVALID_PARAM;
  585. if (u16Height % u16ScalingHeight)
  586. return E_JPEG_INVALID_PARAM;
  587. if (u16Width % u16ScalingWidth)
  588. return E_JPEG_INVALID_PARAM;
  589. u16RatioW = u16Width / u16ScalingWidth;
  590. u16RatioW = u16RatioW / 2 - 1;
  591. if ((u16RatioW != 0) && (u16RatioW != 1) && (u16RatioW != 3))
  592. return E_JPEG_INVALID_PARAM;
  593. u16RatioH = u16Height / u16ScalingHeight - 1;
  594. if ((u16RatioH != 0) && (u16RatioH != 1) && (u16RatioH != 3) && (u16RatioH != 7))
  595. return E_JPEG_INVALID_PARAM;
  596. *pu16RatioW = u16RatioW;
  597. *pu16RatioH = u16RatioH;
  598. }
  599. else
  600. return E_JPEG_INVALID_PARAM;
  601. return E_SUCCESS;
  602. }
  603. static INT jpegSetScalingFactor(
  604. UINT8 u8Mode, //Up / Down Scaling
  605. UINT16 u16FactorH, //Vertical Scaling Factor
  606. UINT16 u16FactorW //Horizontal Scaling Factor
  607. )
  608. {
  609. if (u8Mode == JPEG_ENC_UPSCALE_MODE)
  610. {
  611. JPEG_DEC_DISABLE_DOWNSCALING();
  612. JPEG_ENC_ENABLE_UPSCALING();
  613. }
  614. else if (u8Mode == JPEG_DEC_PACKET_DOWNSCALE_MODE || u8Mode == JPEG_DEC_PLANAR_DOWNSCALE_MODE || u8Mode == JPEG_ENC_PLANAR_PRIMARY_DOWNSCALE_MODE)
  615. {
  616. JPEG_DEC_ENABLE_DOWNSCALING();
  617. JPEG_ENC_DISABLE_UPSCALING();
  618. }
  619. else if (u8Mode == JPEG_ENC_PLANAR_THUMBNAIL_DOWNSCALE_MODE)
  620. {
  621. outp32(REG_JTSCALD, TSX_ON);
  622. }
  623. if (u8Mode == JPEG_DEC_PLANAR_DOWNSCALE_MODE || u8Mode == JPEG_ENC_PLANAR_PRIMARY_DOWNSCALE_MODE)
  624. outp32(REG_JPSCALD, (inp32(REG_JPSCALD) & ~(PSCALX_F | PSCALY_F)) | ((u16FactorW & 0x1F) << 8) | (u16FactorH & 0x1F));
  625. else if (u8Mode == JPEG_ENC_PLANAR_THUMBNAIL_DOWNSCALE_MODE)
  626. outp32(REG_JTSCALD, (inp32(REG_JTSCALD) & ~(TSCALX_F | TSCALY_F)) | ((u16FactorW & 0x1F) << 8) | (u16FactorH & 0x1F));
  627. else
  628. {
  629. outp32(REG_JPSCALD, inp32(REG_JPSCALD) & ~(PSCALX_F | PSCALY_F));
  630. outp32(REG_JUPRAT, ((u16FactorH & 0x3FFF) << 16) | (u16FactorW & 0x3FFF));
  631. }
  632. return E_SUCCESS;
  633. }
  634. static VOID jpegGetDecodedDimension(
  635. PUINT16 pu16Height, //Decode/Encode Height
  636. PUINT16 pu16Width //Decode/Encode Width
  637. )
  638. {
  639. *pu16Width = inp32(REG_JDECWH) & 0x0000FFFF;
  640. *pu16Height = inp32(REG_JDECWH) >> 16;
  641. }
  642. static VOID jpegSetDimension(
  643. UINT16 u16Height, //Decode/Encode Height
  644. UINT16 u16Width //Decode/Encode Width
  645. )
  646. {
  647. outp32(REG_JPRIWH, ((u16Height & 0x1FFF) << 16) | (u16Width & 0x1FFF));
  648. }
  649. static VOID jpegGetDimension(
  650. PUINT16 pu16Height, //Decoded Height from bit stream
  651. PUINT16 pu16Width //Decoded Width from bit stream
  652. )
  653. {
  654. *pu16Height = inp32(REG_JPRIWH) >> 16;
  655. *pu16Width = inp32(REG_JPRIWH) & 0x1FFF;
  656. }
  657. static INT jpegSetWindowDecode(
  658. UINT16 u16StartMCUX, //Start X MCU
  659. UINT16 u16StartMCUY, //Horizontal Scaling Factor
  660. UINT16 u16EndMCUX, //Vertical Scaling Factor
  661. UINT16 u16EndMCUY, //Horizontal Scaling Factor
  662. UINT32 u32Stride //Decode Output Stride
  663. )
  664. {
  665. if (u16StartMCUX >= u16EndMCUX || u16StartMCUY >= u16EndMCUY)
  666. return E_JPEG_INVALID_PARAM;
  667. outp32(REG_JWINDEC0, u16StartMCUY << 16 | u16StartMCUX);
  668. outp32(REG_JWINDEC1, u16EndMCUY << 16 | u16EndMCUX);
  669. outp32(REG_JWINDEC2, u32Stride);
  670. outp32(REG_JMCR, WIN_DEC);
  671. //sysprintf("\tJWINDEC0 0x%X\n", inp32(REG_JWINDEC0));
  672. //sysprintf("\tJWINDEC1 0x%X\n", inp32(REG_JWINDEC1));
  673. //sysprintf("\tJWINDEC2 0x%X\n", inp32(REG_JWINDEC2));
  674. return E_SUCCESS;
  675. }
  676. /**
  677. * @brief The function is used to set JPEG Q Table.
  678. *
  679. * @param[in] puQTable0: Q Table 0
  680. * @param[in] puQTable1: Q Table 1
  681. * @param[in] puQTable2: Q Table 2
  682. * @param[in] u8num: Q Table number
  683. *
  684. * @return E_JPEG_TIMEOUT: Time-out \n
  685. * E_SUCCESS: success
  686. */
  687. static INT _jpegSetQTAB(PUINT8 puQTable0, PUINT8 puQTable1, PUINT8 puQTable2, UINT8 u8num)
  688. {
  689. UINT32 u32value;
  690. UINT32 u32TimeOut;
  691. int i;
  692. u32TimeOut = 0xFFFFFF;
  693. for (i = 0; i < 64; i = i + 4)
  694. {
  695. while ((inp32(REG_JMCR) & QT_BUSY) & u32TimeOut)
  696. u32TimeOut--;
  697. if (!u32TimeOut)
  698. return E_JPEG_TIMEOUT;
  699. u32value = puQTable0[i] | (puQTable0[i + 1] << 8) | (puQTable0[i + 2] << 16) | (puQTable0[i + 3] << 24);
  700. outp32((REG_JQTAB0 + i), u32value);
  701. }
  702. u32TimeOut = 0xFFFFFF;
  703. for (i = 0; i < 64; i = i + 4)
  704. {
  705. while ((inp32(REG_JMCR) & QT_BUSY) & u32TimeOut)
  706. u32TimeOut--;
  707. if (!u32TimeOut)
  708. return E_JPEG_TIMEOUT;
  709. u32value = puQTable1[i] | (puQTable1[i + 1] << 8) | (puQTable1[i + 2] << 16) | (puQTable1[i + 3] << 24);
  710. outp32((REG_JQTAB1 + i), u32value);
  711. }
  712. if (u8num < 3)
  713. return E_SUCCESS;
  714. outp32(JITCR, inp32(JITCR) | 0x8);
  715. u32TimeOut = 0xFFFFFF;
  716. for (i = 0; i < 64; i = i + 4)
  717. {
  718. while ((inp32(REG_JMCR) & QT_BUSY) & u32TimeOut)
  719. u32TimeOut--;
  720. if (!u32TimeOut)
  721. return E_JPEG_TIMEOUT;
  722. u32value = puQTable2[i] | (puQTable2[i + 1] << 8) | (puQTable2[i + 2] << 16) | (puQTable2[i + 3] << 24);
  723. outp32((REG_JQTAB2 + i), u32value);
  724. }
  725. u32TimeOut = 0xFFFFFF;
  726. while ((inp32(REG_JMCR) & QT_BUSY) & u32TimeOut)
  727. u32TimeOut--;
  728. if (!u32TimeOut)
  729. return E_JPEG_TIMEOUT;
  730. else
  731. return E_SUCCESS;
  732. }
  733. INT jpegSetQTAB(PUINT8 puQTable0, PUINT8 puQTable1, PUINT8 puQTable2, UINT8 u8num)
  734. {
  735. INT ret = 0;
  736. struct nu_jpeg_ioctl sNuJpegIoctl = {0};
  737. struct nu_jpeg_qtab sNuJpegQTab = {0};
  738. sNuJpegQTab.puQTable0 = puQTable0;
  739. sNuJpegQTab.puQTable1 = puQTable1;
  740. sNuJpegQTab.puQTable2 = puQTable2;
  741. sNuJpegQTab.u8num = u8num;
  742. sNuJpegIoctl.arg0 = (UINT32)&sNuJpegQTab;
  743. sNuJpegIoctl.arg1 = (UINT32)&ret;
  744. rt_device_control(&g_sNuJpeg.dev, JPEG_IOCTL_SET_QTAB, (void *)&sNuJpegIoctl);
  745. return ret;
  746. }
  747. /// @endcond HIDDEN_SYMBOLS
  748. /** @addtogroup N9H30_JPEG_EXPORTED_FUNCTIONS JPEG Exported Functions
  749. @{
  750. */
  751. /**
  752. * @brief The function is used to initial device parameters and register.
  753. *
  754. * @return 0
  755. */
  756. static VOID _jpegInit(void)
  757. {
  758. /* Set the default values of the JPEG registers */
  759. g_bScale = FALSE;
  760. g_u32Stride = 0;
  761. g_u32BufferCount = 0;
  762. g_u16ReserveSize = 0;
  763. g_u32WindowDec = FALSE;
  764. g_u32windowSizeX = 0;
  765. g_u32windowSizeY = 0;
  766. g_InputWait = FALSE;
  767. g_bEncThumbnailDownScale = FALSE;
  768. g_bEncPrimaryDownScale = FALSE;
  769. g_OutputWait = FALSE;
  770. g_u32EncRotate = 0;
  771. pfnJpegHeaderDecode = NULL;
  772. pfnJpegDecInputWait = NULL;
  773. pfnJpegDecOutputWait = NULL;
  774. outp32(REG_JPRIQC, 0x000000F4);
  775. outp32(REG_JTHBQC, 0x000000F4);
  776. outp32(REG_JPRST, 0x00000004);
  777. outp32(REG_JTRST, 0x00000004);
  778. outp32(REG_JITCR, 0x00000000);
  779. outp32(REG_JINTCR, 0x00000000);
  780. outp32(JDOWFBS, 0xFFFFFFFF);
  781. // Disable the Primary Up-scaling & Scaling-down
  782. outp32(REG_JPSCALU, 0x00000000);
  783. outp32(REG_JPSCALD, 0x00000000);
  784. // Reset JUPRAT and JSRCH
  785. outp32(REG_JUPRAT, 0x00000000);
  786. outp32(REG_JSRCH, 0x00000FFF);
  787. //-------------------------------------------
  788. /* Reset JPEG (JMCR [1]) */
  789. outp32(REG_JMCR, 0x00000002);
  790. outp32(REG_JMCR, 0x00000000);
  791. outp32(REG_JMACR, 0x00400000); //Can't use single buffer
  792. }
  793. VOID jpegInit(void)
  794. {
  795. rt_device_control(&g_sNuJpeg.dev, JPEG_IOCTL_INITIAL_CODEC, (void *)RT_NULL);
  796. }
  797. /**
  798. * @brief The function is used to check JPEG engine not busy.
  799. *
  800. * @return TRUE: JPEG engine busy \n
  801. * FALSE: JPEG engine not busy
  802. */
  803. static BOOL _jpegIsReady(void)
  804. {
  805. if (g_bWait == FALSE)
  806. return TRUE;
  807. else
  808. return FALSE;
  809. }
  810. BOOL jpegIsReady(void)
  811. {
  812. UINT32 u32IsReady = 0;
  813. struct nu_jpeg_ioctl sNuJpegIoctl = {0};
  814. sNuJpegIoctl.arg0 = (UINT32)&u32IsReady;
  815. rt_device_control(&g_sNuJpeg.dev, JPEG_IOCTL_IS_READY, (void *)&sNuJpegIoctl);
  816. return (BOOL)u32IsReady;
  817. }
  818. /**
  819. * @brief The function is used to get JPEG information.
  820. *
  821. * @param[out] *info: JPEG encode/decode information.
  822. *
  823. * @return 0
  824. */
  825. static VOID _jpegGetInfo(JPEG_INFO_T *info)
  826. {
  827. rt_memcpy(info, &jpegInfo, sizeof(JPEG_INFO_T));
  828. }
  829. VOID jpegGetInfo(JPEG_INFO_T *info)
  830. {
  831. struct nu_jpeg_ioctl sNuJpegIoctl = {0};
  832. sNuJpegIoctl.arg0 = (UINT32)info;
  833. rt_device_control(&g_sNuJpeg.dev, JPEG_IOCTL_GET_INFO, (void *)&sNuJpegIoctl);
  834. }
  835. /**
  836. * @brief The function is used to wait JPEG engine, until JPEG engine not busy.
  837. *
  838. * @return E_SUCCESS: JPEG engine encode/decode complete \n
  839. * FALSE: JPEG engine error
  840. */
  841. static INT _jpegWait(void)
  842. {
  843. while (1)
  844. {
  845. if (g_bWait == FALSE)
  846. break;
  847. }
  848. if (g_jpegError)
  849. return E_FAIL;
  850. return E_SUCCESS;
  851. }
  852. INT jpegWait(void)
  853. {
  854. INT ret = 0;
  855. struct nu_jpeg_ioctl sNuJpegIoctl;
  856. sNuJpegIoctl.arg0 = (UINT32)&ret;
  857. sNuJpegIoctl.arg1 = 0;
  858. rt_device_control(&g_sNuJpeg.dev, JPEG_IOCTL_WAITDONE, (void *)&sNuJpegIoctl);
  859. return ret;
  860. }
  861. /**
  862. * @brief The function is used to config and reset JPEG IP.
  863. *
  864. * @return E_SUCCESS: success
  865. */
  866. INT jpegOpen(void)
  867. {
  868. return (rt_device_open(&g_sNuJpeg.dev, RT_DEVICE_FLAG_RDWR) == RT_EOK) ? E_SUCCESS : E_FAIL;
  869. }
  870. /**
  871. * @brief Support some JPEG driver commands for application.
  872. *
  873. * @param[in] cmd: Command.
  874. *
  875. * @param[in] arg0: Arguments for the command.
  876. *
  877. * @param[in] arg1: Arguments for the command.
  878. *
  879. * @return 0
  880. *
  881. */
  882. VOID jpegIoctl(UINT32 cmd, UINT32 arg0, UINT32 arg1)
  883. {
  884. struct nu_jpeg_ioctl sNuJpegIoctl;
  885. sNuJpegIoctl.arg0 = arg0;
  886. sNuJpegIoctl.arg1 = arg1;
  887. rt_device_control(&g_sNuJpeg.dev, cmd, (void *)&sNuJpegIoctl);
  888. }
  889. /**
  890. * @brief JPEG function close.
  891. *
  892. * @return 0
  893. *
  894. */
  895. VOID jpegClose(void)
  896. {
  897. rt_device_close(&g_sNuJpeg.dev);
  898. }
  899. static rt_err_t nu_jpeg_init(rt_device_t dev)
  900. {
  901. UINT32 u32JPGDiv = 0;
  902. UINT32 u32JPGSource;
  903. UINT32 u32HclkHz;
  904. nu_jpeg_t psNuJpeg = (nu_jpeg_t)dev;
  905. RT_ASSERT(dev != RT_NULL);
  906. /* Set JPEG engine clock */
  907. u32HclkHz = sysGetClock(SYS_HCLK234) * 1000000;
  908. u32JPGSource = u32HclkHz / (((inp32(REG_CLK_DIVCTL3) & 0xf0000000) >> 28) + 1);
  909. if (u32JPGSource > 75000000)
  910. {
  911. if (u32JPGSource % 75000000)
  912. {
  913. u32JPGDiv = (u32JPGSource / 75000000);
  914. }
  915. else
  916. u32JPGDiv = (u32JPGSource / 75000000) - 1;
  917. }
  918. outp32(REG_CLK_DIVCTL3, (inp32(REG_CLK_DIVCTL3) & ~(0xf0000000)) | ((u32JPGDiv & 0xf) << 28));
  919. rt_kprintf("JPEG Engine clock frequency is %d MHz\n", u32JPGSource / (u32JPGDiv + 1) / 1000000);
  920. /* Register ISR and Response JPEG Interrupt. */
  921. rt_hw_interrupt_install(psNuJpeg->irqn, nu_jpeg_isr, (void *)psNuJpeg, psNuJpeg->name);
  922. rt_hw_interrupt_umask(psNuJpeg->irqn);
  923. return RT_EOK;
  924. }
  925. static rt_err_t nu_jpeg_open(rt_device_t dev, rt_uint16_t oflag)
  926. {
  927. rt_err_t result;
  928. nu_jpeg_t psNuJpeg = (nu_jpeg_t)dev;
  929. result = rt_mutex_take(&psNuJpeg->lock, RT_WAITING_FOREVER);
  930. RT_ASSERT(result == RT_EOK);
  931. /* Enable JPEG engine clock */
  932. nu_sys_ipclk_enable(psNuJpeg->clkidx);
  933. /* Reset JPEG codec and internal variables. */
  934. nu_sys_ip_reset(psNuJpeg->rstidx);
  935. jpegInit();
  936. result = rt_mutex_release(&psNuJpeg->lock);
  937. RT_ASSERT(result == RT_EOK);
  938. return RT_EOK;
  939. }
  940. static rt_err_t nu_jpeg_close(rt_device_t dev)
  941. {
  942. rt_err_t result;
  943. nu_jpeg_t psNuJpeg = (nu_jpeg_t)dev;
  944. RT_ASSERT(dev != RT_NULL);
  945. result = rt_mutex_take(&psNuJpeg->lock, RT_WAITING_FOREVER);
  946. RT_ASSERT(result == RT_EOK);
  947. /* Reset JPEG (JMCR [1]) */
  948. outp32(REG_JMCR, 0x00000002);
  949. outp32(REG_JMCR, 0x00000000);
  950. /* Disable JPEG engine clock */
  951. nu_sys_ipclk_disable(psNuJpeg->clkidx);
  952. result = rt_mutex_release(&psNuJpeg->lock);
  953. RT_ASSERT(result == RT_EOK);
  954. return RT_EOK;
  955. }
  956. static rt_err_t nu_jpeg_control(rt_device_t dev, int cmd, void *args)
  957. {
  958. JPEG_WINDOW_DECODE_T *winDecode;
  959. PUINT32 pu32Tmp;
  960. UINT32 arg0 = 0, arg1 = 0;
  961. rt_err_t result;
  962. nu_jpeg_t psNuJpeg = (nu_jpeg_t)dev;
  963. nu_jpeg_ioctl_t psNuJpegIoctl = (nu_jpeg_ioctl_t)args;
  964. RT_ASSERT(dev != RT_NULL);
  965. result = rt_mutex_take(&psNuJpeg->lock, RT_WAITING_FOREVER);
  966. RT_ASSERT(result == RT_EOK);
  967. if (psNuJpegIoctl != RT_NULL)
  968. {
  969. arg0 = psNuJpegIoctl->arg0;
  970. arg1 = psNuJpegIoctl->arg1;
  971. }
  972. switch (cmd)
  973. {
  974. case JPEG_IOCTL_SET_YADDR:
  975. JPEG_SET_YADDR(arg0);
  976. break;
  977. case JPEG_IOCTL_SET_UADDR:
  978. JPEG_SET_UADDR(arg0);
  979. break;
  980. case JPEG_IOCTL_SET_VADDR:
  981. JPEG_SET_VADDR(arg0);
  982. break;
  983. case JPEG_IOCTL_SET_YSTRIDE:
  984. JPEG_SET_YSTRIDE(arg0);
  985. break;
  986. case JPEG_IOCTL_SET_USTRIDE:
  987. JPEG_SET_USTRIDE(arg0);
  988. break;
  989. case JPEG_IOCTL_SET_VSTRIDE:
  990. JPEG_SET_VSTRIDE(arg0);
  991. break;
  992. case JPEG_IOCTL_SET_BITSTREAM_ADDR:
  993. JPEG_SET_BITSTREAM_ADDR(arg0);
  994. break;
  995. case JPEG_IOCTL_SET_SOURCE_IMAGE_HEIGHT:
  996. JPEG_SET_SOURCE_IMAGE_HEIGHT(arg0);
  997. break;
  998. case JPEG_IOCTL_ENC_SET_HEADER_CONTROL:
  999. JPEG_ENC_SET_HEADER_CONTROL(arg0);
  1000. break;
  1001. case JPEG_IOCTL_SET_DEFAULT_QTAB:
  1002. jpegSetQTAB(g_au8QTable0, g_au8QTable1, 0, 2);
  1003. break;
  1004. case JPEG_IOCTL_SET_DECODE_MODE:
  1005. jpegSetDecodeMode(arg0);
  1006. break;
  1007. case JPEG_IOCTL_SET_ENCODE_MODE:
  1008. jpegSetEncodeMode(arg0, arg1);
  1009. break;
  1010. case JPEG_IOCTL_SET_DIMENSION:
  1011. jpegSetDimension(arg0, arg1);
  1012. break;
  1013. case JPEG_IOCTL_ENCODE_TRIGGER:
  1014. jpegEncodeTrigger();
  1015. if (g_u16ReserveSize != 0)
  1016. {
  1017. UINT32 u32Addr = JPEG_GET_BITSTREAM_ADDR();
  1018. outp8(u32Addr + 2, 0xFF);
  1019. outp8(u32Addr + 3, 0xE0);
  1020. outp8(u32Addr + 4, ((g_u16ReserveSize - 4) & 0xFF00) >> 8);
  1021. outp8(u32Addr + 5, (g_u16ReserveSize - 4) & 0xFF);
  1022. }
  1023. break;
  1024. case JPEG_IOCTL_DECODE_TRIGGER:
  1025. jpegDecodeTrigger();
  1026. break;
  1027. case JPEG_IOCTL_WINDOW_DECODE:
  1028. winDecode = (JPEG_WINDOW_DECODE_T *)arg0;
  1029. jpegSetWindowDecode(winDecode->u16StartMCUX, winDecode->u16StartMCUY,
  1030. winDecode->u16EndMCUX, winDecode->u16EndMCUY, winDecode->u32Stride);
  1031. g_u32WindowDec = TRUE;
  1032. g_u32windowSizeX = winDecode->u32Stride;
  1033. g_u32windowSizeY = 16 * (winDecode->u16EndMCUY - winDecode->u16StartMCUY + 1);
  1034. break;
  1035. case JPEG_IOCTL_SET_DECODE_STRIDE:
  1036. g_u32Stride = arg0;
  1037. break;
  1038. case JPEG_IOCTL_SET_DECODE_DOWNSCALE:
  1039. g_bScale = TRUE;
  1040. g_u32ScaleWidth = arg1;
  1041. g_u32ScaleHeight = arg0;
  1042. break;
  1043. case JPEG_IOCTL_SET_ENCODE_UPSCALE:
  1044. g_bScale = TRUE;
  1045. g_u32ScaleWidth = arg1;
  1046. g_u32ScaleHeight = arg0;
  1047. break;
  1048. case JPEG_IOCTL_SET_HEADERDECODE_CALBACKFUN:
  1049. pfnJpegHeaderDecode = (PFN_JPEG_HEADERDECODE_CALLBACK) arg0;
  1050. break;
  1051. case JPEG_IOCTL_SET_DECINPUTWAIT_CALBACKFUN:
  1052. g_InputWait = TRUE;
  1053. pfnJpegDecInputWait = (PFN_JPEG_DECWAIT_CALLBACK) arg0;
  1054. JPEG_DEC_SET_INPUT_WAIT(((UINT16) arg1 / 2048));
  1055. g_u16BufferSize = arg1 / 2;
  1056. break;
  1057. case JPEG_IOCTL_ADJUST_QTAB:
  1058. jpegAdjustQTAB(arg0, ((arg1 & 0xFF00) >> 4), (arg1 & 0xFF));
  1059. break;
  1060. case JPEG_IOCTL_ENC_RESERVED_FOR_SOFTWARE:
  1061. if (arg0 > 0)
  1062. {
  1063. UINT32 u32Tmp;
  1064. u32Tmp = arg0 + 4;
  1065. if (u32Tmp % 2)
  1066. u32Tmp++;
  1067. if ((u32Tmp % 4) == 0)
  1068. u32Tmp += 2;
  1069. if (u32Tmp >= 0xFFFF)
  1070. u32Tmp = 65534;
  1071. outp32(REG_JPSCALU, inp32(REG_JPSCALU) | A_JUMP);
  1072. outp32(JRESERVE, u32Tmp);
  1073. g_u16ReserveSize = u32Tmp;
  1074. }
  1075. break;
  1076. case JPEG_IOCTL_SET_ENCODE_PRIMARY_RESTART_INTERVAL:
  1077. outp32(REG_JPRST, arg0);
  1078. break;
  1079. case JPEG_IOCTL_SET_ENCODE_THUMBNAIL_RESTART_INTERVAL:
  1080. outp32(REG_JTRST, arg0);
  1081. break;
  1082. case JPEG_IOCTL_GET_ENCODE_PRIMARY_RESTART_INTERVAL:
  1083. pu32Tmp = (PUINT32) arg0;
  1084. *pu32Tmp = inp32(REG_JPRST);
  1085. break;
  1086. case JPEG_IOCTL_GET_ENCODE_THUMBNAIL_RESTART_INTERVAL:
  1087. pu32Tmp = (PUINT32) arg0;
  1088. *pu32Tmp = inp32(REG_JTRST);
  1089. break;
  1090. case JPEG_IOCTL_SET_THUMBNAIL_DIMENSION:
  1091. outp32(REG_JTHBWH, ((arg0 & 0x1FFF) << 16) | (arg1 & 0x1FFF));
  1092. break;
  1093. case JPEG_IOCTL_SET_ENCODE_SW_OFFSET:
  1094. outp32(REG_JOFFSET, arg0);
  1095. break;
  1096. case JPEG_IOCTL_GET_THUMBNAIL_DIMENSION:
  1097. pu32Tmp = (PUINT32) arg0;
  1098. *pu32Tmp = inp32(REG_JTHBWH) >> 16;
  1099. pu32Tmp = (PUINT32) arg1;
  1100. *pu32Tmp = inp32(REG_JTHBWH) & 0x1FFF;
  1101. break;
  1102. case JPEG_IOCTL_GET_ENCODE_SW_OFFSET:
  1103. pu32Tmp = (PUINT32) arg0;
  1104. *pu32Tmp = inp32(REG_JOFFSET);
  1105. break;
  1106. case JPEG_IOCTL_SET_ENCODE_PRIMARY_DOWNSCALE:
  1107. g_bScale = TRUE;
  1108. g_bEncPrimaryDownScale = TRUE;
  1109. g_u32ScaleWidth = arg1;
  1110. g_u32ScaleHeight = arg0;
  1111. break;
  1112. case JPEG_IOCTL_SET_ENCODE_THUMBNAIL_DOWNSCALE:
  1113. g_bScale = TRUE;
  1114. g_bEncThumbnailDownScale = TRUE;
  1115. g_u32ScaleWidth = arg1;
  1116. g_u32ScaleHeight = arg0;
  1117. break;
  1118. case JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_RIGHT:
  1119. g_u32EncRotate = JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_RIGHT;
  1120. outp32(REG_JITCR, (inp32(REG_JITCR) & ~ROTATE) | ROTATE);
  1121. break;
  1122. case JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_LEFT:
  1123. g_u32EncRotate = JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_LEFT;
  1124. outp32(REG_JITCR, (inp32(REG_JITCR) & ~ROTATE) | 0x1000);
  1125. break;
  1126. case JPEG_IOCTL_SET_ENCODE_PRIMARY_ROTATE_NORMAL:
  1127. g_u32EncRotate = 0;
  1128. outp32(REG_JITCR, (inp32(REG_JITCR) & ~ROTATE));
  1129. break;
  1130. case JPEG_IOCTL_SET_DECOUTPUTWAIT_CALBACKFUN:
  1131. pfnJpegDecOutputWait = (PFN_JPEG_DECWAIT_CALLBACK) arg0;
  1132. break;
  1133. case JPEG_IOCTL_SET_DECOUTPUTWAIT:
  1134. outp32(JYADDR0, arg0);
  1135. outp32(JDOWFBS, arg1);
  1136. g_OutputWait = TRUE;
  1137. break;
  1138. case JPEG_IOCTL_GET_DECOUTPUTWAIT_ADDR:
  1139. pu32Tmp = (PUINT32) arg0;
  1140. *pu32Tmp = inp32(JYADDR0);
  1141. break;
  1142. case JPEG_IOCTL_GET_DECOUTPUTWAIT_SIZE:
  1143. pu32Tmp = (PUINT32) arg0;
  1144. *pu32Tmp = inp32(JDOWFBS);
  1145. break;
  1146. case JPEG_IOCTL_SET_DECODE_COMPLETE_CALBACKFUN:
  1147. pfnJpegDecodeComplete = (PFN_JPEG_CALLBACK) arg0;
  1148. break;
  1149. case JPEG_IOCTL_SET_ENCODE_COMPLETE_CALBACKFUN:
  1150. pfnJpegEncodeComplete = (PFN_JPEG_CALLBACK) arg0;
  1151. break;
  1152. case JPEG_IOCTL_SET_DECODE_ERROR_CALBACKFUN:
  1153. pfnJpegDecodeError = (PFN_JPEG_CALLBACK) arg0;
  1154. break;
  1155. /* Extended IOCTL command */
  1156. case JPEG_IOCTL_SET_QTAB:
  1157. {
  1158. nu_jpeg_qtab_t psJpegQtab = (nu_jpeg_qtab_t)arg0;
  1159. INT *pi32ret = (INT *)arg1;
  1160. *pi32ret = _jpegSetQTAB(psJpegQtab->puQTable0, psJpegQtab->puQTable1, psJpegQtab->puQTable2, psJpegQtab->u8num);
  1161. }
  1162. break;
  1163. case JPEG_IOCTL_INITIAL_CODEC:
  1164. _jpegInit();
  1165. break;
  1166. case JPEG_IOCTL_GET_INFO:
  1167. _jpegGetInfo((JPEG_INFO_T *)arg0);
  1168. break;
  1169. case JPEG_IOCTL_IS_READY:
  1170. {
  1171. UINT32 *pu32ret = (UINT32 *)arg0;
  1172. *pu32ret = _jpegIsReady();
  1173. }
  1174. break;
  1175. case JPEG_IOCTL_WAITDONE:
  1176. {
  1177. INT *pi32ret = (INT *)arg0;
  1178. *pi32ret = _jpegWait();
  1179. }
  1180. break;
  1181. default:
  1182. break;
  1183. }
  1184. result = rt_mutex_release(&psNuJpeg->lock);
  1185. RT_ASSERT(result == RT_EOK);
  1186. return RT_EOK;
  1187. }
  1188. /* Hardward JPEG codec init */
  1189. static int rt_hw_jpeg_init(void)
  1190. {
  1191. rt_err_t ret = RT_EOK;
  1192. /* Register sdcard device */
  1193. g_sNuJpeg.dev.type = RT_Device_Class_Miscellaneous;
  1194. g_sNuJpeg.dev.init = nu_jpeg_init;
  1195. g_sNuJpeg.dev.open = nu_jpeg_open;
  1196. g_sNuJpeg.dev.close = nu_jpeg_close;
  1197. g_sNuJpeg.dev.read = RT_NULL;
  1198. g_sNuJpeg.dev.write = RT_NULL;
  1199. g_sNuJpeg.dev.control = nu_jpeg_control;
  1200. /* Private */
  1201. g_sNuJpeg.dev.user_data = (void *)&g_sNuJpeg;
  1202. ret = rt_mutex_init(&g_sNuJpeg.lock, "jpeg_lock", RT_IPC_FLAG_PRIO);
  1203. RT_ASSERT(ret == RT_EOK);
  1204. /* Only support single opening. */
  1205. ret = rt_device_register(&g_sNuJpeg.dev, g_sNuJpeg.name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  1206. RT_ASSERT(ret == RT_EOK);
  1207. return (int)ret;
  1208. }
  1209. INIT_DEVICE_EXPORT(rt_hw_jpeg_init);