jpeg_core.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  14. * MA 02110-1301, USA.
  15. *
  16. * Name:
  17. * ark_jpeg_core.c
  18. *
  19. * Description:
  20. *
  21. *
  22. * Author:
  23. * Sim
  24. *
  25. * Remarks:
  26. *
  27. */
  28. #include <linux/version.h>
  29. #include <linux/kernel.h>
  30. #include <linux/cdev.h>
  31. #include <linux/device.h>
  32. #include <linux/fs.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/wait.h>
  36. #include <linux/fb.h>
  37. #include <linux/matroxfb.h>
  38. #include <linux/mm.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/dma-mapping.h>
  41. #include "ark_jpeg_io.h"
  42. #include "jpeg.h"
  43. #include "jpeg_priv.h"
  44. static struct ark_jpeg_context *core_jpeg_context;
  45. //static struct ark_jpeg_context *arkjpeg_tasklet_data = NULL;
  46. static int head_size;
  47. static int jpeg_left_len;
  48. static int JpegLeftLine = 0;
  49. static int JpegBlkCount = 0;
  50. static int JpegFrameStart = 0;
  51. static unsigned int JpegSrcWidth = 0;
  52. static unsigned int JpegSrcHeight = 0;
  53. static unsigned int JpegBlkHeight = 0;
  54. static unsigned int JpegPutWidth = 0;
  55. static unsigned int JpegPutHeight = 0;
  56. //static unsigned char *JpegDecPutAddr=NULL;
  57. static void *JpegDecPutAddr = NULL;
  58. static bool JpegScalerEn = false;
  59. unsigned char *JpegScaBuf1 = NULL;
  60. unsigned char *JpegScaBuf2 = NULL;
  61. unsigned char *JpegScaBuf3 = NULL;
  62. unsigned char *JpegBlkBuf1 = NULL;
  63. unsigned char *JpegBlkBuf2 = NULL;
  64. unsigned char *JpegBlkBuf3 = NULL;
  65. unsigned int JpegBlkBuf1PhyAddr;
  66. unsigned int JpegBlkBuf2PhyAddr;
  67. unsigned int JpegBlkBuf3PhyAddr;
  68. static bool hard_head_parser;
  69. static bool head_parser_finished;
  70. //static JpegStartAddr *decode_addr;
  71. int ark_prescale_wait_block_int(void)
  72. {
  73. struct ark_jpeg_context *context = core_jpeg_context;
  74. int ret;
  75. ret = wait_for_completion_timeout(&context->psblockint_completion, msecs_to_jiffies(1000));
  76. if(ret == 0)
  77. {
  78. printk(KERN_ALERT "ark_prescale_wait_block_int timeout.\n");
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. int ark_prescale_wait_frame_int(void)
  84. {
  85. struct ark_jpeg_context *context = core_jpeg_context;
  86. int ret;
  87. ret = wait_for_completion_timeout(&context->psframeint_completion, msecs_to_jiffies(1000));
  88. if(ret == 0)
  89. {
  90. printk(KERN_ALERT "ark_prescale_wait_frame_int timeout.\n");
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. int ark_prescale_set_parameters(struct ark_prescale_cfg_arg *para)
  96. {
  97. unsigned int hblank,vblank;
  98. struct ark_jpeg_context *context = core_jpeg_context;
  99. if(para->dst_width & 1) {
  100. printk(KERN_ALERT "PreScalerInit() : the dest width is not even.\n");
  101. return -1;
  102. }
  103. if(para->dst_width == 0 || para->dst_height == 0 || para->block_height == 0) {
  104. printk(KERN_ALERT "PreScalerInit() : illegal paremeter.\r\n");
  105. return -1;
  106. }
  107. hblank = para->left_blank + para->right_blank;
  108. vblank = para->up_blank + para->down_blank;
  109. writel(para->src_addr, context->ps_mmio_base + PRESCALE_SRC_ADDR);
  110. writel(para->dst_addr, context->ps_mmio_base + PRESCALE_DES_ADDR);
  111. writel(para->dst_addr, context->ps_mmio_base + PRESCALE_DES_ADDR2);
  112. writel(para->hv_addr1, context->ps_mmio_base + PRESCALE_HV_ADDR1);
  113. writel(para->hv_addr2, context->ps_mmio_base + PRESCALE_HV_ADDR2);
  114. writel(para->hv_addr3, context->ps_mmio_base + PRESCALE_HV_ADDR3);
  115. writel((para->src_width/(para->dst_width+hblank)<<10) +
  116. (para->src_width%(para->dst_width+hblank))*1024/(para->dst_width+hblank),
  117. context->ps_mmio_base + PRESCALE_COEF_H);
  118. writel((para->src_height/(para->dst_height+vblank)<<10) +
  119. (para->src_height%(para->dst_height+vblank))*1024/(para->dst_height+vblank),
  120. context->ps_mmio_base + PRESCALE_COEF_V);
  121. writel(0xf, context->ps_mmio_base + PRESCALE_FILTER_CTL);
  122. writel((para->dst_width<<16) | para->src_width, context->ps_mmio_base + PRESCALE_LINE_CTL);
  123. writel((para->dst_height<<16) | para->src_height, context->ps_mmio_base + PRESCALE_HEIGHT_CTL);
  124. writel(para->block_height, context->ps_mmio_base + PRESCALE_BLK_HEIGHT);
  125. if(para->src_height%para->block_height) {
  126. writel(para->src_height / para->block_height + 1, context->ps_mmio_base + PRESCALE_BLK_NUM);
  127. writel(para->src_height % para->block_height, context->ps_mmio_base + PRESCALE_ROW_NUM_LAST_BLK);
  128. } else {
  129. writel(para->src_height / para->block_height, context->ps_mmio_base + PRESCALE_BLK_NUM);
  130. writel(para->block_height, context->ps_mmio_base + PRESCALE_ROW_NUM_LAST_BLK);
  131. }
  132. writel(ARKPRESCAL_WORKMODE_MIX, context->ps_mmio_base + PRESCALE_WORK_MODE);
  133. writel(ARKPRESCAL_VFORMAT_YUV422, context->ps_mmio_base + PRESCALE_SRC_MODE);
  134. writel(para->hori_format, context->ps_mmio_base + PRESCALE_CTL);
  135. //if(pPrsCtx->ppCtx.dwHformat==PRESCALER_HFORMAT_RGB || pPrsCtx->ppCtx.dwHformat==PRESCALER_HFORMAT_YUV)
  136. //pPrsCtx->pPreScaReg->Pre_SCAL_CTL |= (1<<3);
  137. writel((0<<11) | para->left_blank, context->ps_mmio_base + PRESCALE_HDATA_VALID);
  138. writel((0<<11) | para->up_blank, context->ps_mmio_base + PRESCALE_VDATA_VALID_VIDEO);
  139. return 0;
  140. }
  141. void ark_prescale_set_frame_start(void)
  142. {
  143. struct ark_jpeg_context *context = core_jpeg_context;
  144. writel(1, context->ps_mmio_base + PRESCALE_FRAME_START);
  145. }
  146. void ark_prescale_set_block_start(void)
  147. {
  148. struct ark_jpeg_context *context = core_jpeg_context;
  149. writel(1, context->ps_mmio_base + PRESCALE_BLK_START);
  150. }
  151. void ark_prescale_set_src_addr(unsigned int addr)
  152. {
  153. struct ark_jpeg_context *context = core_jpeg_context;
  154. writel(addr, context->ps_mmio_base + PRESCALE_SRC_ADDR);
  155. }
  156. irqreturn_t ark_prescale_intr_handler(int irq, void *dev_id)
  157. {
  158. unsigned int intr_sts;
  159. struct ark_jpeg_context *context = core_jpeg_context;
  160. intr_sts = readl(context->ps_mmio_base + PRESCALE_INT_STATUS);
  161. writel(intr_sts, context->ps_mmio_base + PRESCALE_INT_CLR);
  162. switch (intr_sts & 0x3) {
  163. case 1: /* block finish */
  164. complete(&context->psblockint_completion);
  165. break;
  166. case 2: /* frame finish */
  167. case 3:
  168. complete(&context->psframeint_completion);
  169. break;
  170. }
  171. return IRQ_HANDLED;
  172. }
  173. int ark_jpeg_reg_check(struct ark_jpeg_context *context)
  174. {
  175. int ret = 0;
  176. return ret;
  177. }
  178. /* This function programs hardware registers according to the settings
  179. * stored in the context
  180. */
  181. int ark_jpeg_init(void)
  182. {
  183. return 0;
  184. }
  185. int ark_jpeg_do_intr_check(struct ark_jpeg_context *context)
  186. {
  187. return 0;
  188. }
  189. /* This is a tasklet function. It calls the driver functions to delver data
  190. * between the video post processing driver and output ring pairs.
  191. *
  192. * Arguments:
  193. * un_used : un-used input
  194. *
  195. * Return:
  196. * none
  197. */
  198. /*void ark_jpeg_do_tasklet(unsigned long un_used)
  199. {
  200. if (!arkjpeg_tasklet_data)
  201. return;
  202. ARKJPEG_DBGPRTK("%s %d: enter\n",
  203. __FUNCTION__, __LINE__);
  204. }
  205. DECLARE_TASKLET(arkjpeg_tasklet, ark_jpeg_do_tasklet, 0);*/
  206. //#ifndef ARK_JPEG_USE_HW_EMULATION
  207. /* This function is an interrupt service routine
  208. *
  209. * Arguments:
  210. *
  211. * Return:
  212. *
  213. */
  214. irqreturn_t ark_jpeg_intr_handler(int irq, void *dev_id)
  215. {
  216. unsigned int intr_sts;
  217. struct ark_jpeg_context *context = core_jpeg_context;
  218. intr_sts = readl(context->mmio_base + JPEG_INTCTRL);
  219. ARKJPEG_DBGPRTK("%s %d: irq status 0x%x\n", __FUNCTION__, __LINE__, intr_sts);
  220. context->intr_status = intr_sts;
  221. writel(intr_sts, context->mmio_base + JPEG_INTCLR);
  222. queue_work(context->wrok_queue, &context->jpeg_work);
  223. return IRQ_HANDLED;
  224. }
  225. int ark_jpeg_dev_init(struct ark_jpeg_context *context)
  226. {
  227. ARKJPEG_DBGPRTK("%s %d: enter\n", __FUNCTION__, __LINE__);
  228. core_jpeg_context = context;
  229. /* initialize lock */
  230. spin_lock_init(&context->lock);
  231. /* initialize wait queue */
  232. init_waitqueue_head(&context->waitq);
  233. writel(0x3f, context->mmio_base + JPEG_INTCLR);
  234. return 0;
  235. }
  236. void JpegReset(void)
  237. {
  238. unsigned int val;
  239. val = readl(core_jpeg_context->mmio_base + JPEG_CTRL);
  240. val |= (3 << 0);
  241. writel(val, core_jpeg_context->mmio_base + JPEG_CTRL);
  242. val &= ~(3 << 0);
  243. writel(val, core_jpeg_context->mmio_base + JPEG_CTRL);
  244. }
  245. static int SoftHeaderConfigReg(JPEG_SOF_PARA * SOF_PARA, JPEG_SOS_PARA * SOS_PARA, JPEG_DRI_PARA * DRI_PARA)
  246. {
  247. unsigned int nBLK0, nBLK1, nBLK2;
  248. unsigned int format;
  249. JpegReset();
  250. format = (SOF_PARA->SOF_NS_PARA[0].H << 4) + SOF_PARA->SOF_NS_PARA[0].V;
  251. switch (format) {
  252. case 0x22: //411
  253. nBLK0 = 3;
  254. nBLK1 = 0;
  255. nBLK2 = 0;
  256. break;
  257. case 0x12: //422a
  258. case 0x21: //422b
  259. nBLK0 = 1;
  260. nBLK1 = 0;
  261. nBLK2 = 0;
  262. break;
  263. case 0x11: //444,100
  264. nBLK0 = 0;
  265. nBLK1 = 0;
  266. nBLK2 = 0;
  267. break;
  268. default:
  269. nBLK0 = 0;
  270. nBLK1 = 0;
  271. nBLK2 = 0;
  272. printk(KERN_ALERT "\nunknown jpeg format !!!!!!!!!!!\r\n");
  273. return 0;
  274. }
  275. switch (SOF_PARA->Nf) {
  276. case 4:
  277. //colspa = 4;
  278. break;
  279. case 3:
  280. //colspa = 2;
  281. break;
  282. case 1:
  283. //colspa = 1;
  284. break;
  285. default:
  286. return 1;
  287. }
  288. writel(((SOF_PARA->Y) << 16) | (0 << 8) | ((SOS_PARA->Ns -
  289. 1) << 6) |
  290. (SOF_PARA->Colspctype << 4) | (1 << 3) | (DRI_PARA->restart_flag << 2) | ((SOF_PARA->Nf - 1) << 0),
  291. core_jpeg_context->mmio_base + JPEG_1);
  292. writel(((SOF_PARA->X) << 16) | ((DRI_PARA->restart_interval - 1) << 0), core_jpeg_context->mmio_base + JPEG_3);
  293. writel((SOF_PARA->SOF_NS_PARA[0].H << 12) | (SOF_PARA->SOF_NS_PARA[0].V << 8) | (nBLK0 << 4) | (SOF_PARA->
  294. SOF_NS_PARA[0].
  295. Tq << 2) |
  296. (SOS_PARA->SOS_NS_PARA[0].Td << 1)
  297. | (SOS_PARA->SOS_NS_PARA[0].Ta << 0), core_jpeg_context->mmio_base + JPEG_4);
  298. writel((SOF_PARA->SOF_NS_PARA[1].H << 12) | (SOF_PARA->SOF_NS_PARA[1].V << 8) | (nBLK1 << 4) | (SOF_PARA->
  299. SOF_NS_PARA[1].
  300. Tq << 2) |
  301. (SOS_PARA->SOS_NS_PARA[1].Td << 1)
  302. | (SOS_PARA->SOS_NS_PARA[1].Ta << 0), core_jpeg_context->mmio_base + JPEG_5);
  303. writel((SOF_PARA->SOF_NS_PARA[2].H << 12) | (SOF_PARA->SOF_NS_PARA[2].V << 8) | (nBLK2 << 4) | (SOF_PARA->
  304. SOF_NS_PARA[2].
  305. Tq << 2) |
  306. (SOS_PARA->SOS_NS_PARA[2].Td << 1)
  307. | (SOS_PARA->SOS_NS_PARA[2].Ta << 0), core_jpeg_context->mmio_base + JPEG_6);
  308. writel((SOF_PARA->SOF_NS_PARA[3].H << 12) | (SOF_PARA->SOF_NS_PARA[3].V << 8) | (nBLK2 << 4) | (SOF_PARA->
  309. SOF_NS_PARA[3].
  310. Tq << 2) |
  311. (SOS_PARA->SOS_NS_PARA[3].Td << 1)
  312. | (SOS_PARA->SOS_NS_PARA[3].Ta << 0), core_jpeg_context->mmio_base + JPEG_7);
  313. return 1;
  314. }
  315. #define GET_STREAM_BYTE(a) (*a++)
  316. static int quan_table[4][64];
  317. static int hea[64], heb[336];
  318. static JPEG_SOF_PARA SOF_PARA = { 0 };
  319. static int SoftJpegHead(unsigned int fileAddr, unsigned int fileSize)
  320. {
  321. JPEG_SOS_PARA SOS_PARA = { 0 };
  322. JPEG_DRI_PARA DRI_PARA = { 0 };
  323. unsigned char HiByte, LoByte;
  324. unsigned char tmpBuf[10];
  325. int table_num;
  326. int len = 0;
  327. int i, j;
  328. signed char Tq; //Pq removed waring
  329. int sof_find = 0, huff_find = 0, quan_find = 0, sos_find = 0;
  330. int v, code, hid;
  331. int dc, ht, abase, bbase; //,hbase,hb; removed waring
  332. int min[64];
  333. int l[16];
  334. unsigned char min0_tb, min1_tb, min2_tb, min3_tb, min4_tb, min5_tb, min6_tb, min7_tb;
  335. unsigned char min8_tb, min9_tb, min10_tb, min11_tb, min12_tb, min13_tb, min14_tb, min15_tb;
  336. unsigned int colspa;
  337. unsigned char *jpegFile = (unsigned char *)fileAddr;
  338. colspa = 0;
  339. DRI_PARA.restart_flag = 0;
  340. DRI_PARA.restart_interval = 1;
  341. for (i = 0; i < 336; i++) {
  342. heb[i] = 0;
  343. }
  344. if ((GET_STREAM_BYTE(jpegFile) != 0xff)
  345. || (GET_STREAM_BYTE(jpegFile) != 0xd8)) {
  346. printk(KERN_ALERT "JpegHeadSoftDec begin error!\n");
  347. return -1;
  348. }
  349. while (1) {
  350. if (((unsigned int)jpegFile - fileAddr) > fileSize)
  351. return -1;
  352. HiByte = GET_STREAM_BYTE(jpegFile);
  353. if (HiByte == 0xff) {
  354. LoByte = GET_STREAM_BYTE(jpegFile);
  355. if (LoByte == 0xc2) //not baseline
  356. {
  357. printk(KERN_ALERT "JpegHeadSoftDec format error!\n");
  358. return -1;
  359. }
  360. if ((LoByte != 0xff) && (LoByte != 0xc0)
  361. && (LoByte != 0xd9) && (LoByte != 0xd8)
  362. && (LoByte != 0xc4) && (LoByte != 0xdb)
  363. && (LoByte != 0xda) && ((LoByte != 0xdd))) {
  364. HiByte = GET_STREAM_BYTE(jpegFile);
  365. LoByte = GET_STREAM_BYTE(jpegFile);
  366. len = (HiByte << 8) + LoByte - 2;
  367. jpegFile += len;
  368. }
  369. //jpeg frame start
  370. else if (LoByte == 0xc0) {
  371. sof_find = 1;
  372. for (i = 0; i < 8; i++) {
  373. tmpBuf[i] = GET_STREAM_BYTE(jpegFile);
  374. }
  375. SOF_PARA.Lf = (tmpBuf[0] << 8) + tmpBuf[1];
  376. SOF_PARA.P = tmpBuf[2];
  377. SOF_PARA.Y = (tmpBuf[3] << 8) + tmpBuf[4];
  378. SOF_PARA.X = (tmpBuf[5] << 8) + tmpBuf[6];
  379. SOF_PARA.Nf = tmpBuf[7];
  380. if ((SOF_PARA.Y == 0) || (SOF_PARA.X == 0)) {
  381. printk(KERN_ALERT "abnormal jpeg size !\n");
  382. return -1;
  383. }
  384. if ((SOF_PARA.Nf != 3) && (SOF_PARA.Nf != 1)) {
  385. printk(KERN_ALERT "unknown colspctype !\n");
  386. return -1;
  387. }
  388. if (SOF_PARA.Nf > 255)
  389. return -1;
  390. for (i = 0; i < (SOF_PARA.Nf * 3); i += 3) {
  391. SOF_PARA.SOF_NS_PARA[i / 3].C = GET_STREAM_BYTE(jpegFile);
  392. tmpBuf[0] = GET_STREAM_BYTE(jpegFile);
  393. SOF_PARA.SOF_NS_PARA[i / 3].H = ((tmpBuf[0] & 0xf0) >> 4);
  394. SOF_PARA.SOF_NS_PARA[i / 3].V = tmpBuf[0] & 0x0f;
  395. SOF_PARA.SOF_NS_PARA[i / 3].Tq = GET_STREAM_BYTE(jpegFile);
  396. }
  397. }
  398. //jpeg huffman table
  399. else if (LoByte == 0xc4) {
  400. huff_find = 1;
  401. for (i = 0; i < 2; i++) {
  402. tmpBuf[i] = GET_STREAM_BYTE(jpegFile);
  403. }
  404. len = (tmpBuf[0] << 8) + tmpBuf[1];
  405. while ((len - 2) > 0) {
  406. v = GET_STREAM_BYTE(jpegFile);
  407. len -= 1;
  408. hid = ((v >> 4) != 0) ? 2 : 0;
  409. hid |= ((v & 15) != 0) ? 1 : 0;
  410. switch (hid) {
  411. case 1:
  412. // hbase=368;
  413. break;
  414. case 2:
  415. // hbase=0;
  416. break;
  417. case 3:
  418. // hbase=176;
  419. break;
  420. default:
  421. // hbase=352;
  422. break;
  423. }
  424. if ((v >> 4) != 0)
  425. abase = 0;
  426. else
  427. abase = 1;
  428. dc = abase;
  429. ht = v & 15;
  430. abase |= (ht << 1);
  431. switch (abase) {
  432. case 1:
  433. case 3:
  434. bbase = 162;
  435. break;
  436. case 2:
  437. bbase = 174;
  438. break;
  439. default:
  440. bbase = 0;
  441. break;
  442. }
  443. abase <<= 4;
  444. for (i = abase; i < abase + 16; i++) {
  445. if (i >= 64)
  446. return -1;
  447. hea[i] = 255;
  448. }
  449. for (i = 0; i < 16; i++) {
  450. l[i] = GET_STREAM_BYTE(jpegFile);
  451. len -= 1;
  452. }
  453. code = 0;
  454. for (i = 0; i < 16; i++, abase++) {
  455. if (abase >= 64)
  456. return -1;
  457. min[abase] = code;
  458. hea[abase] = bbase - code;
  459. if (l[i] != 0) {
  460. for (j = 0; j < l[i]; j++, bbase++) {
  461. v = GET_STREAM_BYTE(jpegFile);
  462. len -= 1;
  463. if (dc) {
  464. v &= 15;
  465. if (ht)
  466. v <<= 4;
  467. if (bbase >= 336)
  468. return -1;
  469. heb[bbase] |= v;
  470. } else {
  471. /*removed waring
  472. if(v==0)
  473. hb=160;
  474. else if(v==0xf0)
  475. hb=161;
  476. else
  477. hb=(v>>4)*10+(v&0xf)-1;
  478. */
  479. if (bbase >= 336)
  480. return -1;
  481. heb[bbase] = v;
  482. }
  483. code++;
  484. }
  485. }
  486. code <<= 1;
  487. }
  488. } //while len != 0
  489. }
  490. //jpeg quantification table
  491. else if (LoByte == 0xdb) {
  492. quan_find = 1;
  493. for (i = 0; i < 2; i++) {
  494. tmpBuf[i] = GET_STREAM_BYTE(jpegFile);
  495. }
  496. len = (tmpBuf[0] << 8) + tmpBuf[1];
  497. switch (len) {
  498. case 67:
  499. table_num = 1;
  500. break;
  501. case 132:
  502. table_num = 2;
  503. break;
  504. case 197:
  505. table_num = 3;
  506. break;
  507. case 262:
  508. table_num = 4;
  509. break;
  510. default:
  511. table_num = 0;
  512. break;
  513. }
  514. if (table_num == 0) {
  515. printk(KERN_ALERT "unknown quant table !\n");
  516. return -1;
  517. }
  518. for (j = 0; j < table_num; j++) {
  519. tmpBuf[0] = GET_STREAM_BYTE(jpegFile);
  520. // Pq = ((tmpBuf[0] & 0xf0) >> 4);
  521. Tq = tmpBuf[0] & 0xf;
  522. if (Tq >= 4)
  523. return -1;
  524. if (Tq > colspa) {
  525. colspa = Tq;
  526. }
  527. if (Tq >= 4)
  528. return -1;
  529. for (i = 0; i < 64; i++) {
  530. quan_table[Tq][i] = GET_STREAM_BYTE(jpegFile);
  531. }
  532. }
  533. }
  534. //jpeg start of scan
  535. else if (LoByte == 0xda) {
  536. sos_find = 1;
  537. for (i = 0; i < 3; i++) {
  538. tmpBuf[i] = GET_STREAM_BYTE(jpegFile);
  539. }
  540. SOS_PARA.Ls = (tmpBuf[0] << 8) + tmpBuf[1];
  541. SOS_PARA.Ns = tmpBuf[2];
  542. //printk("\n jpeg sos Ns = %d\n",SOS_PARA.Ns);
  543. if (SOS_PARA.Ns > 4)
  544. return -1;
  545. for (i = 0; i < (SOS_PARA.Ns * 2); i += 2) {
  546. SOS_PARA.SOS_NS_PARA[i / 2].Cs = GET_STREAM_BYTE(jpegFile);
  547. tmpBuf[0] = GET_STREAM_BYTE(jpegFile);
  548. SOS_PARA.SOS_NS_PARA[i / 2].Td = ((tmpBuf[0] & 0xf0) >> 4);
  549. SOS_PARA.SOS_NS_PARA[i / 2].Ta = tmpBuf[0] & 0x0f;
  550. }
  551. SOS_PARA.Ss = GET_STREAM_BYTE(jpegFile);
  552. SOS_PARA.Se = GET_STREAM_BYTE(jpegFile);
  553. tmpBuf[0] = GET_STREAM_BYTE(jpegFile);
  554. SOS_PARA.Ah = ((tmpBuf[0] & 0xf0) >> 4);
  555. SOS_PARA.Ai = tmpBuf[0] & 0x0f;
  556. }
  557. //jpeg restart
  558. else if (LoByte == 0xdd) {
  559. for (i = 0; i < 4; i++) {
  560. tmpBuf[i] = GET_STREAM_BYTE(jpegFile);
  561. }
  562. i = (tmpBuf[0] << 8) + tmpBuf[1];
  563. if (((tmpBuf[0] << 8) + tmpBuf[1]) != 4) {
  564. printk(KERN_ALERT "wrong number of DRI marker!\n");
  565. return -1;
  566. }
  567. DRI_PARA.restart_interval = (tmpBuf[2] << 8) + tmpBuf[3];
  568. DRI_PARA.restart_flag = 1;
  569. if (DRI_PARA.restart_interval == 0) {
  570. DRI_PARA.restart_flag = 0;
  571. DRI_PARA.restart_interval = 1;
  572. }
  573. }
  574. }
  575. SOF_PARA.Colspctype = colspa;
  576. //config register
  577. if ((sof_find == 1) && (sos_find == 1) && (huff_find == 1)
  578. && (quan_find == 1)) {
  579. ARKJPEG_DBGPRTK("soft jpeg find all marker!\n");
  580. SoftHeaderConfigReg(&SOF_PARA, &SOS_PARA, &DRI_PARA);
  581. writel(readl(core_jpeg_context->mmio_base + JPEG_CTRL) | (1 << 15),
  582. core_jpeg_context->mmio_base + JPEG_CTRL);
  583. //write quant table
  584. for (i = 0; i < (colspa + 1); i++) {
  585. for (j = 0; j < 64; j++) {
  586. *((volatile unsigned int *)JPEG_QT + i * 64 + j) = quan_table[i][j];
  587. }
  588. }
  589. //write base
  590. for (i = 0; i < 64; i++) {
  591. *((volatile unsigned int *)(JPEG_BASE) + i) = (hea[i] & 0x1ff);
  592. }
  593. //write symb
  594. for (i = 0; i < 336; i++) {
  595. *((volatile unsigned int *)(JPEG_SYMB) + i) = (heb[i] & 0xff);
  596. }
  597. //write min
  598. j = 0;
  599. for (i = 0; i < 64;) {
  600. min0_tb = min[i++] & 1;
  601. min1_tb = min[i++] & 3;
  602. min2_tb = min[i++] & 7;
  603. min3_tb = min[i++] & 15;
  604. min4_tb = min[i++] & 31;
  605. min5_tb = min[i++] & 63;
  606. min6_tb = min[i++] & 127;
  607. min7_tb = min[i++] & 255;
  608. min8_tb = min[i++] & 255;
  609. min9_tb = min[i++] & 255;
  610. min10_tb = min[i++] & 255;
  611. min11_tb = min[i++] & 255;
  612. min12_tb = min[i++] & 255;
  613. min13_tb = min[i++] & 255;
  614. min14_tb = min[i++] & 255;
  615. min15_tb = min[i++] & 255;
  616. *((volatile unsigned int *)(JPEG_MIN) + j++) =
  617. (min15_tb + (min14_tb << 8) + (min13_tb << 16) + (min12_tb << 24));
  618. *((volatile unsigned int *)(JPEG_MIN) + j++) =
  619. (min11_tb + (min10_tb << 8) + (min9_tb << 16) + (min8_tb << 24));
  620. *((volatile unsigned int *)(JPEG_MIN) + j++) =
  621. (min7_tb + (min6_tb << 8) + (min5_tb << 15) + (min4_tb << 21) + (min3_tb << 26) +
  622. ((min2_tb & 0x3) << 30));
  623. *((volatile unsigned int *)(JPEG_MIN) + j++) =
  624. ((min2_tb >> 2) + (min1_tb << 1) + (min0_tb << 3));
  625. }
  626. writel(readl(core_jpeg_context->mmio_base + JPEG_COUNT)
  627. & ~(1 << 15), core_jpeg_context->mmio_base + JPEG_COUNT);
  628. break;
  629. }
  630. }
  631. ARKJPEG_DBGPRTK("exit soft jpeg header\n");
  632. return ((int)jpegFile - fileAddr);
  633. }
  634. void JpegLoadStartAddr(JpegStartAddr * wAddr)
  635. {
  636. writel(wAddr->wAddr1, core_jpeg_context->mmio_base + JPEG_WRSTA);
  637. writel(wAddr->wAddr1 + wAddr->wSize1, core_jpeg_context->mmio_base + JPEG_WREND);
  638. writel(wAddr->wAddr2, core_jpeg_context->mmio_base + JPEG_WRSTA1);
  639. writel(wAddr->wAddr2 + wAddr->wSize2, core_jpeg_context->mmio_base + JPEG_WREND1);
  640. writel(wAddr->wAddr3, core_jpeg_context->mmio_base + JPEG_WRSTA2);
  641. writel(wAddr->wAddr3 + wAddr->wSize3, core_jpeg_context->mmio_base + JPEG_WREND2);
  642. writel(readl(core_jpeg_context->mmio_base + JPEG_CTRL) | (1 << 13), core_jpeg_context->mmio_base + JPEG_CTRL);
  643. }
  644. void JpegContinueWork()
  645. {
  646. writel(readl(core_jpeg_context->mmio_base + JPEG_CTRL) | (1 << 6), core_jpeg_context->mmio_base + JPEG_CTRL);
  647. }
  648. unsigned int JpegReadFile(unsigned int size)
  649. {
  650. long ret;
  651. struct ark_jpeg_context *context = core_jpeg_context;
  652. context->api_info.EventType = FREAD;
  653. context->api_info.dwReadLen = size;
  654. complete(&context->api_completion);
  655. ret = wait_for_completion_timeout(&context->apidone_completion, msecs_to_jiffies(JPEG_API_TIMEOUT));
  656. if (ret == 0) {
  657. printk(KERN_ALERT "JpegReadFile timeout.\n");
  658. return 0;
  659. } else {
  660. /*{
  661. int i;
  662. for(i = 0; i < 8; i++)
  663. printk(KERN_ALERT "0x%.8x, ", *(unsigned int*)(context->buf_base_virt+i*4));
  664. printk(KERN_ALERT "\n");
  665. for(i = 0; i < 8; i++)
  666. printk(KERN_ALERT "0x%.8x, ", *(unsigned int*)(context->buf_base_virt+200*1024-8*4+i*4));
  667. printk(KERN_ALERT "\n");
  668. } */
  669. return context->api_retinfo.dwReadedLen;
  670. }
  671. }
  672. int JpegFileSeek(int offset, int origin)
  673. {
  674. long ret;
  675. struct ark_jpeg_context *context = core_jpeg_context;
  676. context->api_info.EventType = FSEEK;
  677. context->api_info.lOffset = offset;
  678. context->api_info.nOrigin = origin;
  679. complete(&context->api_completion);
  680. ret = wait_for_completion_timeout(&context->apidone_completion, msecs_to_jiffies(JPEG_API_TIMEOUT));
  681. if (ret == 0) {
  682. printk(KERN_ALERT "JpegFileSeek timeout.\n");
  683. return -1;
  684. } else
  685. return context->api_retinfo.nSeekRet;
  686. }
  687. bool dest_size(unsigned src_width, unsigned src_height, unsigned input_width, unsigned input_height,
  688. unsigned *dest_width, unsigned *dest_height)
  689. {
  690. unsigned int width, height;
  691. struct ark_jpeg_context *context = core_jpeg_context;
  692. if (src_width == 0 || src_height == 0) {
  693. printk(KERN_ALERT "Picture type is not supported !\n");
  694. context->decode_result = DEC_ERROR;
  695. return false;
  696. }
  697. if ((src_width > MAXIMAGEWIDTH) || (src_height > MAXIMAGEHEIGHT)) {
  698. printk(KERN_ALERT "Error : Picture too large !\n");
  699. context->decode_result = DEC_ERROR;
  700. return false;
  701. }
  702. if (context->rotate_angle == CLOCKWISE_90 || context->rotate_angle == CLOCKWISE_270) {
  703. width = input_height;
  704. height = input_width;
  705. } else {
  706. width = input_width;
  707. height = input_height;
  708. }
  709. if (context->scaler_mode == NO_SCALER) {
  710. *dest_width = src_width;
  711. *dest_height = src_height;
  712. } else if (context->scaler_mode == NOMAL_SCALER) {
  713. if (context->zoom_mode == ZOOM_IN_ONLY) {
  714. *dest_width = (src_width > width) ? width : src_width;
  715. *dest_height = (src_height > height) ? height : src_height;
  716. } else if (context->zoom_mode == ZOOM_OUT_ONLY) {
  717. *dest_width = (src_width < width) ? width : src_width;
  718. *dest_height = (src_height < height) ? height : src_height;
  719. } else {
  720. *dest_width = width;
  721. *dest_height = height;
  722. }
  723. } else if (context->scaler_mode == UNIFORM_SCALER) {
  724. unsigned int hcoff = width * 1024 / src_width;
  725. unsigned int vcoff = height * 1024 / src_height;
  726. if (context->zoom_mode == ZOOM_IN_ONLY) {
  727. if (hcoff >= 1024 && vcoff >= 1024) {
  728. *dest_width = src_width;
  729. *dest_height = src_height;
  730. } else {
  731. if (hcoff < vcoff) {
  732. *dest_width = width;
  733. *dest_height = hcoff * src_height / 1024;
  734. } else {
  735. *dest_height = height;
  736. *dest_width = vcoff * src_width / 1024;
  737. }
  738. }
  739. } else if (context->zoom_mode == ZOOM_OUT_ONLY) {
  740. if (hcoff > 1024 && vcoff > 1024) {
  741. if (hcoff < vcoff) {
  742. *dest_width = width;
  743. *dest_height = hcoff * src_height / 1024;
  744. } else {
  745. *dest_height = height;
  746. *dest_width = vcoff * src_width / 1024;
  747. }
  748. } else {
  749. *dest_width = src_width;
  750. *dest_height = src_height;
  751. }
  752. } else {
  753. if (hcoff < vcoff) {
  754. *dest_width = width;
  755. *dest_height = hcoff * src_height / 1024;
  756. } else {
  757. *dest_height = height;
  758. *dest_width = vcoff * src_width / 1024;
  759. }
  760. }
  761. }
  762. //for 2d process align
  763. *dest_width &= ~7;
  764. return true;
  765. }
  766. int is_scaler(unsigned src_width, unsigned src_height, unsigned dest_width, unsigned dest_height)
  767. {
  768. return src_width != dest_width ? 1 : src_height != dest_height ? 1 : 0;
  769. }
  770. int do_scaler(JpegStartAddr * wAddr, unsigned src_width, unsigned src_height, unsigned dest_width, unsigned dest_height,
  771. unsigned format)
  772. {
  773. unsigned dwBlkSize;
  774. struct ark_jpeg_context *context = core_jpeg_context;
  775. struct ark_prescale_cfg_arg scapara = { 0 };
  776. scapara.src_addr = wAddr->wAddr1;
  777. scapara.dst_addr = context->decode_buf_base_phys;
  778. scapara.src_width = src_width;
  779. scapara.src_height = src_height;
  780. scapara.dst_width = dest_width;
  781. scapara.dst_height = dest_height;
  782. scapara.hori_format = format;
  783. if (src_height <= JPEG_BLOCK_HEIGHT)
  784. JpegBlkHeight = src_height;
  785. else
  786. JpegBlkHeight = JPEG_BLOCK_HEIGHT;
  787. dwBlkSize = dest_width * JpegBlkHeight * 2;
  788. scapara.block_height = JpegBlkHeight;
  789. scapara.hv_addr1 = (wAddr->wAddr3 + wAddr->wSize3 + 31) & ~31;
  790. scapara.hv_addr2 = (scapara.hv_addr1 + dwBlkSize + 31) & ~31;
  791. scapara.hv_addr3 = (scapara.hv_addr2 + dwBlkSize + 31) & ~31;
  792. if (dest_height < src_height)
  793. scapara.up_blank = 8;
  794. if (dest_width < src_width)
  795. scapara.left_blank = 2;
  796. return ark_prescale_set_parameters(&scapara);
  797. }
  798. bool JpegHeaderIntHandler()
  799. {
  800. int ret;
  801. JpegStartAddr wAddr;
  802. unsigned int dwBlkSize;
  803. unsigned int input_width, input_height;
  804. struct ark_jpeg_context *context = core_jpeg_context;
  805. head_parser_finished = 1;
  806. JpegSrcWidth = (readl(context->mmio_base + JPEG_1) >> 16) & 0x0000ffff;
  807. JpegLeftLine = JpegSrcHeight = (readl(context->mmio_base + JPEG_3) >> 16) & 0x0000ffff;
  808. input_width = context->dst_width;
  809. input_height = context->dst_height;
  810. ret = dest_size(JpegSrcWidth, JpegSrcHeight, input_width, input_height, &JpegPutWidth, &JpegPutHeight);
  811. if (ret == false) {
  812. printk("%s : jpeg dest_size error\n", __func__);
  813. return false;
  814. }
  815. ARKJPEG_DBGPRTK("jpg scaler %d,%d,%d,%d\n", JpegSrcWidth, JpegSrcHeight, JpegPutWidth, JpegPutHeight);
  816. JpegDecPutAddr = context->decode_buf_base_virt;
  817. JpegScalerEn = is_scaler(JpegSrcWidth, JpegSrcHeight, JpegPutWidth, JpegPutHeight);
  818. if (JpegScalerEn) {
  819. if (JpegSrcHeight > JPEG_BLOCK_HEIGHT && JpegSrcHeight <= JPEG_BLOCK_HEIGHT * 2) {
  820. JpegBlkHeight = JPEG_BLOCK_HEIGHT / 2;
  821. } else
  822. JpegBlkHeight = JPEG_BLOCK_HEIGHT;
  823. } else
  824. JpegBlkHeight = JPEG_BLOCK_HEIGHT;
  825. context->src_width = JpegSrcWidth;
  826. context->src_height = JpegSrcHeight;
  827. context->out_width = JpegPutWidth;
  828. context->out_height = JpegPutHeight;
  829. context->decode_size = JpegPutWidth * JpegPutHeight * 2;
  830. dwBlkSize = JpegBlkHeight * JpegSrcWidth * 2;
  831. if (JpegPutWidth * JpegPutHeight * 2 > context->decode_buf_size
  832. || context->buf_size <
  833. JPEG_INPUT_BUFFERSIZE + dwBlkSize * 3 + (JpegScalerEn ? JpegPutWidth * JpegBlkHeight * 2 * 3 : 0)) {
  834. printk(KERN_ALERT "No enough memory to decode picture.\n");
  835. context->decode_result = DEC_ERROR;
  836. return false;
  837. }
  838. JpegBlkBuf1 = (unsigned char *)(context->buf_base_virt + JPEG_INPUT_BUFFERSIZE);
  839. JpegBlkBuf2 = JpegBlkBuf1 + dwBlkSize;
  840. JpegBlkBuf3 = JpegBlkBuf2 + dwBlkSize;
  841. JpegBlkBuf1PhyAddr = context->buf_base_phys + JPEG_INPUT_BUFFERSIZE;
  842. JpegBlkBuf2PhyAddr = JpegBlkBuf1PhyAddr + dwBlkSize;
  843. JpegBlkBuf3PhyAddr = JpegBlkBuf2PhyAddr + dwBlkSize;
  844. wAddr.wAddr1 = JpegBlkBuf1PhyAddr;
  845. wAddr.wSize1 = dwBlkSize;
  846. wAddr.wAddr2 = wAddr.wAddr1 + wAddr.wSize1;
  847. wAddr.wSize2 = dwBlkSize;
  848. wAddr.wAddr3 = wAddr.wAddr2 + wAddr.wSize2;
  849. wAddr.wSize3 = dwBlkSize;
  850. //decode_addr = &wAddr;
  851. JpegLoadStartAddr(&wAddr);
  852. if (JpegScalerEn) {
  853. if (do_scaler(&wAddr, JpegSrcWidth, JpegSrcHeight, JpegPutWidth, JpegPutHeight, ARKPRESCAL_HFORMAT_YUV422) < 0)
  854. return false;
  855. }
  856. if (context->break_decode) {
  857. context->decode_result = DEC_BREAKED;
  858. return false;
  859. }
  860. return true;
  861. }
  862. bool JpegBufferIntHandler()
  863. {
  864. struct ark_jpeg_context *context = core_jpeg_context;
  865. //printk("[%s][%d]\n", __func__, __LINE__);
  866. if (jpeg_left_len > JPEG_FILE_BUFFERSIZE * 1024) {
  867. if (JpegReadFile(JPEG_FILE_BUFFERSIZE * 1024) != JPEG_FILE_BUFFERSIZE * 1024) {
  868. printk(KERN_ALERT "read file err 1!\n");
  869. context->decode_result = DEC_ERROR;
  870. return false;
  871. }
  872. jpeg_left_len -= JPEG_FILE_BUFFERSIZE * 1024;
  873. } else if (jpeg_left_len > 0) {
  874. if (JpegReadFile(jpeg_left_len) != jpeg_left_len) {
  875. printk(KERN_ALERT "read file err 2!\n");
  876. context->decode_result = DEC_ERROR;
  877. return false;
  878. }
  879. jpeg_left_len = 0;
  880. } else {
  881. if (jpeg_left_len < 0) {
  882. printk(KERN_ALERT "No JPEG frame int when file is all readed!\n");
  883. context->decode_result = DEC_ERROR;
  884. return false;
  885. }
  886. jpeg_left_len--;
  887. }
  888. return true;
  889. }
  890. bool JpegBlockIntHandler()
  891. {
  892. unsigned int blksize;
  893. struct ark_jpeg_context *context = core_jpeg_context;
  894. //printk("[%s][%d] JpegBlkCount:%d\n", __func__, __LINE__, JpegBlkCount);
  895. JpegLeftLine -= JpegBlkHeight;
  896. blksize = JpegBlkHeight * JpegSrcWidth * 2;
  897. if (JpegBlkCount == 0) {
  898. if (JpegScalerEn) {
  899. if (JpegFrameStart == 0) {
  900. if (context->break_decode) {
  901. context->decode_result = DEC_BREAKED;
  902. return false;
  903. }
  904. ark_prescale_set_frame_start();
  905. JpegFrameStart = 1;
  906. } else {
  907. if (ark_prescale_wait_block_int() != 0) {
  908. context->decode_result = DEC_ERROR;
  909. return false;
  910. }
  911. if (context->break_decode) {
  912. context->decode_result = DEC_BREAKED;
  913. return false;
  914. }
  915. ark_prescale_set_src_addr(JpegBlkBuf1PhyAddr);
  916. ark_prescale_set_block_start();
  917. }
  918. } else
  919. memcpy(JpegDecPutAddr, JpegBlkBuf1, blksize);
  920. } else if (JpegBlkCount == 1) {
  921. if (JpegScalerEn) {
  922. if (ark_prescale_wait_block_int() != 0) {
  923. context->decode_result = DEC_ERROR;
  924. return false;
  925. }
  926. if (context->break_decode) {
  927. context->decode_result = DEC_BREAKED;
  928. return false;
  929. }
  930. ark_prescale_set_src_addr(JpegBlkBuf2PhyAddr);
  931. ark_prescale_set_block_start();
  932. } else
  933. memcpy(JpegDecPutAddr, JpegBlkBuf2, blksize);
  934. } else {
  935. if (JpegScalerEn) {
  936. if (ark_prescale_wait_block_int() != 0) {
  937. context->decode_result = DEC_ERROR;
  938. return false;
  939. }
  940. if (context->break_decode) {
  941. context->decode_result = DEC_BREAKED;
  942. return false;
  943. }
  944. ark_prescale_set_src_addr(JpegBlkBuf3PhyAddr);
  945. ark_prescale_set_block_start();
  946. } else
  947. memcpy(JpegDecPutAddr, JpegBlkBuf3, blksize);
  948. }
  949. if (context->break_decode) {
  950. context->decode_result = DEC_BREAKED;
  951. return false;
  952. }
  953. JpegBlkCount++;
  954. if (JpegBlkCount == 3)
  955. JpegBlkCount = 0;
  956. JpegDecPutAddr += blksize;
  957. return true;
  958. }
  959. bool JpegFrameIntHandler()
  960. {
  961. unsigned int blksize;
  962. struct ark_jpeg_context *context = core_jpeg_context;
  963. //printk("[%s][%d] JpegBlkCount:%d\n", __func__, __LINE__, JpegBlkCount);
  964. if (JpegLeftLine) {
  965. blksize = JpegLeftLine * JpegSrcWidth * 2;
  966. if (JpegBlkCount == 0) {
  967. if (JpegScalerEn) {
  968. if (JpegFrameStart == 0) {
  969. ark_prescale_set_frame_start();
  970. JpegFrameStart = 1;
  971. } else {
  972. if (ark_prescale_wait_block_int() != 0) {
  973. context->decode_result = DEC_ERROR;
  974. return false;
  975. }
  976. if (context->break_decode) {
  977. context->decode_result = DEC_BREAKED;
  978. return false;
  979. }
  980. ark_prescale_set_src_addr(JpegBlkBuf1PhyAddr);
  981. ark_prescale_set_block_start();
  982. }
  983. } else
  984. memcpy(JpegDecPutAddr, JpegBlkBuf1, blksize);
  985. } else if (JpegBlkCount == 1) {
  986. if (JpegScalerEn) {
  987. if (ark_prescale_wait_block_int() != 0) {
  988. context->decode_result = DEC_ERROR;
  989. return false;
  990. }
  991. if (context->break_decode) {
  992. context->decode_result = DEC_BREAKED;
  993. return false;
  994. }
  995. ark_prescale_set_src_addr(JpegBlkBuf2PhyAddr);
  996. ark_prescale_set_block_start();
  997. } else
  998. memcpy(JpegDecPutAddr, JpegBlkBuf2, blksize);
  999. } else {
  1000. if (JpegScalerEn) {
  1001. if (ark_prescale_wait_block_int() != 0) {
  1002. context->decode_result = DEC_ERROR;
  1003. return false;
  1004. }
  1005. if (context->break_decode) {
  1006. context->decode_result = DEC_BREAKED;
  1007. return false;
  1008. }
  1009. ark_prescale_set_src_addr(JpegBlkBuf3PhyAddr);
  1010. ark_prescale_set_block_start();
  1011. } else
  1012. memcpy(JpegDecPutAddr, JpegBlkBuf3, blksize);
  1013. }
  1014. }
  1015. if (JpegScalerEn) {
  1016. if (ark_prescale_wait_frame_int() != 0) {
  1017. context->decode_result = DEC_ERROR;
  1018. return false;
  1019. }
  1020. }
  1021. return true;
  1022. }
  1023. void JpegInit(void)
  1024. {
  1025. struct ark_jpeg_context *context = core_jpeg_context;
  1026. //enable hardware header parser;the CODEC acts as a decoder.
  1027. if (context->hard_head_parser)
  1028. writel((1 << 8) | (1 << 3), context->mmio_base + JPEG_1);
  1029. writel(0x28000738, context->mmio_base + JPEG_CTRL); //Disable read buffer mode
  1030. //when error appears, you disable reset the module
  1031. //jp_clk_divnum:1 (2?)
  1032. //RFTH indicates the FIFO level:2'b01 25-50% full
  1033. //bypass_filter
  1034. //jp_hp_stall_en
  1035. //jp_hpbuf_stall_clr: Head parser and buffer stall clear.
  1036. //jp_hp_stall_en: JPEG head parser stall enable
  1037. //? The counter is used for judging the status of the JPEG. If the counter count the up limit value, indicating an error generation.
  1038. writel(0xff, context->mmio_base + JPEG_COUNT);
  1039. writel(0xff, context->mmio_base + JPEG_INTCLR);
  1040. writel(0x27, context->mmio_base + JPEG_INTMASK); //Jpeg input buffer interrupt enable:
  1041. //FIFO empty interrupt disable:
  1042. //Jpeg decode error interrupt disable:
  1043. //Frame Interrupt enable:
  1044. //Jpeg output buffer interrupt enable:
  1045. //Head decode interrupt enable:
  1046. //JPEG_FILE_BUFFERSIZE: 4*1024
  1047. if (context->read_buf_mode) {
  1048. //Enable read buffer mode;
  1049. writel(readl(context->mmio_base + JPEG_CTRL) | (1 << 30) | (JPEG_FILE_BUFFERSIZE << 16),
  1050. context->mmio_base + JPEG_CTRL);
  1051. writel(context->buf_base_phys, context->mmio_base + JPEG_DEC_RD_BASE_ADDR);
  1052. } else {
  1053. writel(context->buf_base_phys, context->mmio_base + JPEG_DEC_RD_BASE_ADDR);
  1054. }
  1055. }
  1056. void JepgStartDec(void)
  1057. {
  1058. writel(1, core_jpeg_context->mmio_base + JPEG_0);
  1059. writel(readl(core_jpeg_context->mmio_base + JPEG_START) | (1 << 31), core_jpeg_context->mmio_base + JPEG_START);
  1060. }
  1061. void JpegDecode(void)
  1062. {
  1063. long ret;
  1064. struct ark_jpeg_context *context = core_jpeg_context;
  1065. jpeg_left_len = context->file_size;
  1066. ARKJPEG_DBGPRTK("jpeg_left_len=%d\n", jpeg_left_len);
  1067. if (hard_head_parser) {
  1068. context->hard_head_parser = true;
  1069. head_parser_finished = 0;
  1070. context->scaler_enable = 0;
  1071. JpegFileSeek(0, SEEK_SET);
  1072. } else {
  1073. hard_head_parser = true;
  1074. context->hard_head_parser = false;
  1075. JpegFileSeek(head_size, SEEK_SET);
  1076. jpeg_left_len -= head_size;
  1077. }
  1078. if (jpeg_left_len > JPEG_FILE_BUFFERSIZE * 1024) {
  1079. if (JpegReadFile(JPEG_FILE_BUFFERSIZE * 1024) != JPEG_FILE_BUFFERSIZE * 1024) {
  1080. printk(KERN_ALERT "read file err 0\n");
  1081. context->decode_result = DEC_ERROR;
  1082. return;
  1083. }
  1084. jpeg_left_len -= JPEG_FILE_BUFFERSIZE * 1024;
  1085. } else if (jpeg_left_len > 0) {
  1086. if (JpegReadFile(jpeg_left_len) != jpeg_left_len) {
  1087. printk(KERN_ALERT "read file err 1\n");
  1088. context->decode_result = DEC_ERROR;
  1089. return;
  1090. }
  1091. jpeg_left_len = 0;
  1092. }
  1093. JpegBlkCount = 0;
  1094. JpegFrameStart = 0;
  1095. context->read_buf_mode = 1;
  1096. JpegInit();
  1097. JepgStartDec();
  1098. ret = wait_for_completion_timeout(&context->decdone_completion, msecs_to_jiffies(JPEG_TIMEOUT));
  1099. if (ret == 0) {
  1100. printk(KERN_ALERT "Wait jpeg int timeout.\n");
  1101. context->decode_result = DEC_ERROR;
  1102. }
  1103. return;
  1104. }
  1105. void JpegPicDec(void)
  1106. {
  1107. unsigned int filelen;
  1108. struct ark_jpeg_context *context = core_jpeg_context;
  1109. JpegReset();
  1110. context->break_decode = false;
  1111. hard_head_parser = true;
  1112. JpegDec_Start:
  1113. JpegDecode();
  1114. if (context->decode_result == DEC_ERROR) {
  1115. if (head_parser_finished == 0) //Harder header parser fail
  1116. {
  1117. filelen = context->file_size;
  1118. JpegFileSeek(0, SEEK_SET);
  1119. filelen = filelen > JPEG_MAX_HEADERSIZE ? JPEG_MAX_HEADERSIZE : filelen;
  1120. if (JpegReadFile(filelen) == filelen) {
  1121. head_size = SoftJpegHead((unsigned int)
  1122. context->buf_base_virt, filelen);
  1123. if (head_size < 0) {
  1124. printk(KERN_ALERT "soft header parser error!\n");
  1125. } else {
  1126. hard_head_parser = false;
  1127. if (JpegHeaderIntHandler())
  1128. goto JpegDec_Start;
  1129. }
  1130. } else
  1131. printk(KERN_ALERT "read file err 3!\n");
  1132. } else {
  1133. printk(KERN_ALERT "jpeg decode error!\n");
  1134. }
  1135. }
  1136. }