jpegdecapi.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description : JPEG decoder API source file
  17. --
  18. ------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: jpegdecapi.c,v $
  23. -- $Revision: 1.263 $
  24. -- $Date: 2011/05/31 12:30:09 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. Table of contents
  29. 1. Include headers
  30. 2. External compiler flags
  31. 3. Module defines
  32. 4. Local function prototypes
  33. 5. Functions
  34. - JpegDecInit
  35. - JpegDecRelease
  36. - JpegDecGetImageInfo
  37. - JpegDecDecode
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. 1. Include headers
  41. ------------------------------------------------------------------------------*/
  42. #include <linux/module.h>
  43. #include <linux/platform_device.h>
  44. #include "dwl.h"
  45. #include "basetype.h"
  46. #include "jpegdecapi.h"
  47. #include "jpegdeccontainer.h"
  48. #include "jpegdecmarkers.h"
  49. #include "jpegdecinternal.h"
  50. #include "jpegdecutils.h"
  51. #include "jpegdechdrs.h"
  52. #include "jpegdecscan.h"
  53. #include "jpegregdrv.h"
  54. #include "jpeg_pp_pipeline.h"
  55. #ifdef JPEGDEC_PP_TRACE
  56. #include "ppapi.h"
  57. #endif /* #ifdef JPEGDEC_PP_TRACE */
  58. static void JpegDecPreparePp(JpegDecContainer * pJpegDecCont);
  59. /*------------------------------------------------------------------------------
  60. Version Information - DO NOT CHANGE!
  61. ------------------------------------------------------------------------------*/
  62. #define JPG_MAJOR_VERSION 1
  63. #define JPG_MINOR_VERSION 1
  64. #define JPG_BUILD_MAJOR 1
  65. #define JPG_BUILD_MINOR 207
  66. #define JPG_SW_BUILD ((JPG_BUILD_MAJOR * 1000) + JPG_BUILD_MINOR)
  67. /*------------------------------------------------------------------------------
  68. 2. External compiler flags
  69. ------------------------------------------------------------------------------*/
  70. /*------------------------------------------------------------------------------
  71. 3. Module defines
  72. ------------------------------------------------------------------------------*/
  73. #ifdef JPEGDEC_TRACE
  74. #define JPEGDEC_API_TRC(str) JpegDecTrace((str))
  75. #else
  76. #define JPEGDEC_API_TRC(str) //printk(str)
  77. #endif
  78. #define JPEGDEC_CLEAR_IRQ SetDecRegister(PTR_JPGC->jpegRegs, \
  79. HWIF_DEC_IRQ_STAT, 0); \
  80. SetDecRegister(PTR_JPGC->jpegRegs, \
  81. HWIF_DEC_IRQ, 0);
  82. /*------------------------------------------------------------------------------
  83. 4. Local function prototypes
  84. ------------------------------------------------------------------------------*/
  85. /*------------------------------------------------------------------------------
  86. 5. Functions
  87. ------------------------------------------------------------------------------*/
  88. /*------------------------------------------------------------------------------
  89. Function name: JpegDecInit
  90. Functional description:
  91. Init jpeg decoder
  92. Inputs:
  93. JpegDecInst * decInst a reference to the jpeg decoder instance is
  94. stored here
  95. Outputs:
  96. JPEGDEC_OK
  97. JPEGDEC_INITFAIL
  98. JPEGDEC_PARAM_ERROR
  99. JPEGDEC_DWL_ERROR
  100. JPEGDEC_MEMFAIL
  101. ------------------------------------------------------------------------------*/
  102. JpegDecRet JpegDecInit(JpegDecInst * pDecInst)
  103. {
  104. JpegDecContainer *pJpegCont;
  105. const void *dwl;
  106. u32 i = 0;
  107. u32 asicID;
  108. u32 fuseStatus = 0;
  109. u32 extensionsSupported;
  110. DWLInitParam_t dwlInit;
  111. // printk("JpegDecInit#\n");
  112. /* check that right shift on negative numbers is performed signed */
  113. /*lint -save -e* following check causes multiple lint messages */
  114. if(((-1) >> 1) != (-1))
  115. {
  116. printk("JpegDecInit# ERROR: Right shift is not signed\n");
  117. return (JPEGDEC_INITFAIL);
  118. }
  119. /*lint -restore */
  120. if(pDecInst == NULL)
  121. {
  122. printk("JpegDecInit# ERROR: decInst == NULL\n");
  123. return (JPEGDEC_PARAM_ERROR);
  124. }
  125. *pDecInst = NULL; /* return NULL instance for any error */
  126. /* check for proper hardware */
  127. asicID = DWLReadAsicID();
  128. {
  129. /* check that JPEG decoding supported in HW */
  130. DWLHwConfig_t hwCfg;
  131. DWLReadAsicConfig(&hwCfg);
  132. if(!hwCfg.jpegSupport)
  133. {
  134. printk(("JpegDecInit# ERROR: JPEG not supported in HW\n"));
  135. return JPEGDEC_FORMAT_NOT_SUPPORTED;
  136. }
  137. /* check progressive support */
  138. if((asicID >> 16) != 0x8170U)
  139. {
  140. /* progressive decoder */
  141. if(hwCfg.jpegSupport == JPEG_BASELINE)
  142. fuseStatus = 1;
  143. }
  144. extensionsSupported = hwCfg.jpegESupport;
  145. }
  146. dwlInit.clientType = DWL_CLIENT_TYPE_JPEG_DEC;
  147. /* Initialize Wrapper */
  148. dwl = DWLInit(&dwlInit);
  149. if(dwl == NULL)
  150. {
  151. printk("JpegDecInit# ERROR: DWL Init failed\n");
  152. return (JPEGDEC_DWL_ERROR);
  153. }
  154. pJpegCont = (JpegDecContainer *) DWLmalloc(sizeof(JpegDecContainer));
  155. if(pJpegCont == NULL)
  156. {
  157. (void) DWLRelease(dwl);
  158. return (JPEGDEC_MEMFAIL);
  159. }
  160. pJpegCont->dwl = dwl;
  161. /* reset internal structures */
  162. JpegDecClearStructs(pJpegCont);
  163. /* Reset shadow registers */
  164. for(i = 1; i < DEC_X170_REGISTERS; i++)
  165. {
  166. pJpegCont->jpegRegs[i] = 0;
  167. }
  168. /* these parameters are defined in deccfg.h */
  169. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_OUT_ENDIAN,
  170. DEC_X170_OUTPUT_PICTURE_ENDIAN);
  171. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_IN_ENDIAN,
  172. DEC_X170_INPUT_DATA_ENDIAN);
  173. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_STRENDIAN_E,
  174. DEC_X170_INPUT_STREAM_ENDIAN);
  175. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_MAX_BURST,
  176. DEC_X170_BUS_BURST_LENGTH);
  177. if((asicID >> 16) == 0x8170U)
  178. {
  179. SetDecRegister(pJpegCont->jpegRegs, HWIF_PRIORITY_MODE,
  180. DEC_X170_ASIC_SERVICE_PRIORITY);
  181. }
  182. else
  183. {
  184. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_SCMD_DIS,
  185. DEC_X170_SCMD_DISABLE);
  186. }
  187. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_LATENCY,
  188. DEC_X170_LATENCY_COMPENSATION);
  189. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_DATA_DISC_E,
  190. DEC_X170_DATA_DISCARD_ENABLE);
  191. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_OUTSWAP32_E,
  192. DEC_X170_OUTPUT_SWAP_32_ENABLE);
  193. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_INSWAP32_E,
  194. DEC_X170_INPUT_DATA_SWAP_32_ENABLE);
  195. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_STRSWAP32_E,
  196. DEC_X170_INPUT_STREAM_SWAP_32_ENABLE);
  197. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_OUT_TILED_E, 0);
  198. #if( DEC_X170_HW_TIMEOUT_INT_ENA != 0)
  199. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_TIMEOUT_E, 1);
  200. #else
  201. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_TIMEOUT_E, 0);
  202. #endif
  203. #if( DEC_X170_INTERNAL_CLOCK_GATING != 0)
  204. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_CLK_GATE_E, 1);
  205. #else
  206. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_CLK_GATE_E, 0);
  207. #endif
  208. #if( DEC_X170_USING_IRQ == 0)
  209. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_IRQ_DIS, 1);
  210. #else
  211. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_IRQ_DIS, 0);
  212. #endif
  213. /* set AXI RW IDs */
  214. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_AXI_RD_ID,
  215. (DEC_X170_AXI_ID_R & 0xFFU));
  216. SetDecRegister(pJpegCont->jpegRegs, HWIF_DEC_AXI_WR_ID,
  217. (DEC_X170_AXI_ID_W & 0xFFU));
  218. /* save HW version so we dont need to check it all
  219. * the time when deciding the control stuff */
  220. pJpegCont->is8190 = (asicID >> 16) != 0x8170U ? 1 : 0;
  221. /* set HW related config's */
  222. if(pJpegCont->is8190)
  223. {
  224. pJpegCont->fuseBurned = fuseStatus;
  225. /* max */
  226. pJpegCont->maxSupportedWidth = JPEGDEC_MAX_WIDTH_8190;
  227. pJpegCont->maxSupportedHeight = JPEGDEC_MAX_HEIGHT_8190;
  228. pJpegCont->maxSupportedPixelAmount = JPEGDEC_MAX_PIXEL_AMOUNT_8190;
  229. pJpegCont->maxSupportedSliceSize = JPEGDEC_MAX_SLICE_SIZE_8190;
  230. }
  231. else
  232. {
  233. /* max */
  234. pJpegCont->maxSupportedWidth = JPEGDEC_MAX_WIDTH;
  235. pJpegCont->maxSupportedHeight = JPEGDEC_MAX_HEIGHT;
  236. pJpegCont->maxSupportedPixelAmount = JPEGDEC_MAX_PIXEL_AMOUNT;
  237. pJpegCont->maxSupportedSliceSize = JPEGDEC_MAX_SLICE_SIZE;
  238. }
  239. /* min */
  240. pJpegCont->minSupportedWidth = JPEGDEC_MIN_WIDTH;
  241. pJpegCont->minSupportedHeight = JPEGDEC_MIN_HEIGHT;
  242. pJpegCont->extensionsSupported = extensionsSupported;
  243. *pDecInst = (JpegDecContainer *) pJpegCont;
  244. // printk("JpegDecInit# OK\n");
  245. return (JPEGDEC_OK);
  246. }
  247. /*------------------------------------------------------------------------------
  248. Function name: JpegDecRelease
  249. Functional description:
  250. Release Jpeg decoder
  251. Inputs:
  252. JpegDecInst decInst jpeg decoder instance
  253. void
  254. ------------------------------------------------------------------------------*/
  255. void JpegDecRelease(JpegDecInst decInst)
  256. {
  257. #define PTR_JPGC ((JpegDecContainer *) decInst)
  258. const void *dwl;
  259. JPEGDEC_API_TRC("JpegDecRelease#\n");
  260. if(PTR_JPGC == NULL)
  261. {
  262. JPEGDEC_API_TRC("JpegDecRelease# ERROR: decInst == NULL\n");
  263. return;
  264. }
  265. dwl = PTR_JPGC->dwl;
  266. if(PTR_JPGC->vlc.acTable0.vals)
  267. {
  268. DWLfree(PTR_JPGC->vlc.acTable0.vals);
  269. }
  270. if(PTR_JPGC->vlc.acTable1.vals)
  271. {
  272. DWLfree(PTR_JPGC->vlc.acTable1.vals);
  273. }
  274. if(PTR_JPGC->vlc.acTable2.vals)
  275. {
  276. DWLfree(PTR_JPGC->vlc.acTable2.vals);
  277. }
  278. if(PTR_JPGC->vlc.acTable3.vals)
  279. {
  280. DWLfree(PTR_JPGC->vlc.acTable3.vals);
  281. }
  282. if(PTR_JPGC->vlc.dcTable0.vals)
  283. {
  284. DWLfree(PTR_JPGC->vlc.dcTable0.vals);
  285. }
  286. if(PTR_JPGC->vlc.dcTable1.vals)
  287. {
  288. DWLfree(PTR_JPGC->vlc.dcTable1.vals);
  289. }
  290. if(PTR_JPGC->vlc.dcTable2.vals)
  291. {
  292. DWLfree(PTR_JPGC->vlc.dcTable2.vals);
  293. }
  294. if(PTR_JPGC->vlc.dcTable3.vals)
  295. {
  296. DWLfree(PTR_JPGC->vlc.dcTable3.vals);
  297. }
  298. if(PTR_JPGC->frame.pBuffer)
  299. {
  300. DWLfree(PTR_JPGC->frame.pBuffer);
  301. }
  302. /* progressive */
  303. if(PTR_JPGC->info.pCoeffBase.virtualAddress)
  304. {
  305. DWLFreeLinear(dwl, &(PTR_JPGC->info.pCoeffBase));
  306. PTR_JPGC->info.pCoeffBase.virtualAddress = NULL;
  307. }
  308. if(PTR_JPGC->info.tmpStrm.virtualAddress)
  309. {
  310. DWLFreeLinear(dwl, &(PTR_JPGC->info.tmpStrm));
  311. PTR_JPGC->info.tmpStrm.virtualAddress = NULL;
  312. }
  313. if(PTR_JPGC->frame.pTableBase.virtualAddress)
  314. {
  315. DWLFreeLinear(dwl, &(PTR_JPGC->frame.pTableBase));
  316. PTR_JPGC->frame.pTableBase.virtualAddress = NULL;
  317. }
  318. /* if not user allocated memories */
  319. if(!PTR_JPGC->info.userAllocMem)
  320. {
  321. if(PTR_JPGC->asicBuff.outLumaBuffer.virtualAddress != NULL)
  322. {
  323. DWLFreeRefFrm(dwl, &(PTR_JPGC->asicBuff.outLumaBuffer));
  324. PTR_JPGC->asicBuff.outLumaBuffer.virtualAddress = NULL;
  325. }
  326. if(PTR_JPGC->asicBuff.outChromaBuffer.virtualAddress != NULL)
  327. {
  328. DWLFreeRefFrm(dwl, &(PTR_JPGC->asicBuff.outChromaBuffer));
  329. PTR_JPGC->asicBuff.outChromaBuffer.virtualAddress = NULL;
  330. }
  331. if(PTR_JPGC->asicBuff.outChromaBuffer2.virtualAddress != NULL)
  332. {
  333. DWLFreeRefFrm(dwl, &(PTR_JPGC->asicBuff.outChromaBuffer2));
  334. PTR_JPGC->asicBuff.outChromaBuffer2.virtualAddress = NULL;
  335. }
  336. }
  337. else
  338. {
  339. PTR_JPGC->asicBuff.outLumaBuffer.virtualAddress = NULL;
  340. PTR_JPGC->asicBuff.outChromaBuffer.virtualAddress = NULL;
  341. }
  342. if(decInst)
  343. {
  344. DWLfree(PTR_JPGC);
  345. }
  346. (void) DWLRelease(dwl);
  347. JPEGDEC_API_TRC("JpegDecRelease# OK\n");
  348. return;
  349. #undef PTR_JPGC
  350. }
  351. /*------------------------------------------------------------------------------
  352. Function name: JpegDecGetImageInfo
  353. Functional description:
  354. Get image information of the JFIF
  355. Inputs:
  356. JpegDecInst decInst jpeg decoder instance
  357. JpegDecInput *pDecIn input stream information
  358. JpegDecImageInfo *pImageInfo
  359. structure where the image info is written
  360. Outputs:
  361. JPEGDEC_OK
  362. JPEGDEC_ERROR
  363. JPEGDEC_UNSUPPORTED
  364. JPEGDEC_PARAM_ERROR
  365. JPEGDEC_INCREASE_BUFFER
  366. JPEGDEC_INVALID_STREAM_LENGTH
  367. JPEGDEC_INVALID_INPUT_BUFFER_SIZE
  368. ------------------------------------------------------------------------------*/
  369. /* Get image information of the JFIF */
  370. JpegDecRet JpegDecGetImageInfo(JpegDecInst decInst, JpegDecInput * pDecIn,
  371. JpegDecImageInfo * pImageInfo)
  372. {
  373. #define PTR_JPGC ((JpegDecContainer *) decInst)
  374. u32 Nf = 0;
  375. u32 Ns = 0;
  376. u32 NsThumb = 0;
  377. u32 i, j = 0;
  378. u32 init = 0;
  379. u32 initThumb = 0;
  380. u32 H[MAX_NUMBER_OF_COMPONENTS];
  381. u32 V[MAX_NUMBER_OF_COMPONENTS];
  382. u32 Htn[MAX_NUMBER_OF_COMPONENTS];
  383. u32 Vtn[MAX_NUMBER_OF_COMPONENTS];
  384. u32 Hmax = 0;
  385. u32 Vmax = 0;
  386. u32 headerLength = 0;
  387. u32 currentByte = 0;
  388. u32 currentBytes = 0;
  389. u32 appLength = 0;
  390. u32 appBits = 0;
  391. u32 thumbnail = 0;
  392. u32 errorCode = 0;
  393. #ifdef JPEGDEC_ERROR_RESILIENCE
  394. u32 errorResilience = 0;
  395. u32 errorResilienceThumb = 0;
  396. #endif /* JPEGDEC_ERROR_RESILIENCE */
  397. StreamStorage stream;
  398. // printk("JpegDecGetImageInfo#\n");
  399. /* check pointers & parameters */
  400. if(decInst == NULL || pDecIn == NULL || pImageInfo == NULL ||
  401. X170_CHECK_VIRTUAL_ADDRESS(pDecIn->streamBuffer.pVirtualAddress) ||
  402. X170_CHECK_BUS_ADDRESS(pDecIn->streamBuffer.busAddress))
  403. {
  404. printk("JpegDecGetImageInfo# ERROR: NULL parameter\n");
  405. return (JPEGDEC_PARAM_ERROR);
  406. }
  407. /* Check the stream lenth */
  408. if(pDecIn->streamLength < 1)
  409. {
  410. printk("JpegDecGetImageInfo# ERROR: pDecIn->streamLength\n");
  411. return (JPEGDEC_INVALID_STREAM_LENGTH);
  412. }
  413. /* Check the stream lenth */
  414. if((pDecIn->streamLength > DEC_X170_MAX_STREAM) &&
  415. (pDecIn->bufferSize < JPEGDEC_X170_MIN_BUFFER ||
  416. pDecIn->bufferSize > JPEGDEC_X170_MAX_BUFFER))
  417. {
  418. JPEGDEC_API_TRC("JpegDecGetImageInfo# ERROR: pDecIn->bufferSize\n");
  419. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  420. }
  421. /* Check the stream buffer size */
  422. if(pDecIn->bufferSize && (pDecIn->bufferSize < JPEGDEC_X170_MIN_BUFFER ||
  423. pDecIn->bufferSize > JPEGDEC_X170_MAX_BUFFER))
  424. {
  425. JPEGDEC_API_TRC("JpegDecGetImageInfo# ERROR: pDecIn->bufferSize\n");
  426. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  427. }
  428. /* Check the stream buffer size */
  429. if(pDecIn->bufferSize && ((pDecIn->bufferSize % 8) != 0))
  430. {
  431. JPEGDEC_API_TRC
  432. ("JpegDecGetImageInfo# ERROR: pDecIn->bufferSize % 8) != 0\n");
  433. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  434. }
  435. /* reset sampling factors */
  436. for(i = 0; i < MAX_NUMBER_OF_COMPONENTS; i++)
  437. {
  438. H[i] = 0;
  439. V[i] = 0;
  440. Htn[i] = 0;
  441. Vtn[i] = 0;
  442. }
  443. /* imageInfo initialization */
  444. pImageInfo->displayWidth = 0;
  445. pImageInfo->displayHeight = 0;
  446. pImageInfo->outputWidth = 0;
  447. pImageInfo->outputHeight = 0;
  448. pImageInfo->version = 0;
  449. pImageInfo->units = 0;
  450. pImageInfo->xDensity = 0;
  451. pImageInfo->yDensity = 0;
  452. pImageInfo->outputFormat = 0;
  453. /* Default value to "Thumbnail" */
  454. pImageInfo->thumbnailType = JPEGDEC_NO_THUMBNAIL;
  455. pImageInfo->displayWidthThumb = 0;
  456. pImageInfo->displayHeightThumb = 0;
  457. pImageInfo->outputWidthThumb = 0;
  458. pImageInfo->outputHeightThumb = 0;
  459. pImageInfo->outputFormatThumb = 0;
  460. /* utils initialization */
  461. stream.bitPosInByte = 0;
  462. stream.pCurrPos = (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  463. stream.pStartOfStream = (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  464. stream.readBits = 0;
  465. stream.appnFlag = 0;
  466. // printk("+++++pVirtualAddress 0x%x+++++++++++++++++++\n",pDecIn->streamBuffer.pVirtualAddress);
  467. // printk("++++++pCurrPos 0x%x,pStartOfStream 0x%x++++++++++++++++++++\n",stream.pCurrPos,stream.pStartOfStream);
  468. /* stream length */
  469. if(!pDecIn->bufferSize)
  470. stream.streamLength = pDecIn->streamLength;
  471. else
  472. stream.streamLength = pDecIn->bufferSize;
  473. /* Read decoding parameters */
  474. for(stream.readBits = 0; (stream.readBits / 8) < stream.streamLength;
  475. stream.readBits++)
  476. {
  477. /* Look for marker prefix byte from stream */
  478. if(JpegDecGetByte(&(stream)) == 0xFF)
  479. {
  480. currentByte = JpegDecGetByte(&(stream));
  481. /* switch to certain header decoding */
  482. switch (currentByte)
  483. {
  484. /* baseline marker */
  485. case SOF0:
  486. /* progresive marker */
  487. case SOF2:
  488. if(currentByte == SOF0)
  489. pImageInfo->codingMode = PTR_JPGC->info.operationType =
  490. JPEGDEC_BASELINE;
  491. else
  492. pImageInfo->codingMode = PTR_JPGC->info.operationType =
  493. JPEGDEC_PROGRESSIVE;
  494. /* Frame header */
  495. i++;
  496. Hmax = 0;
  497. Vmax = 0;
  498. /* SOF0/SOF2 length */
  499. headerLength = JpegDecGet2Bytes(&(stream));
  500. if(headerLength == STRM_ERROR ||
  501. ((stream.readBits + ((headerLength * 8) - 16)) >
  502. (8 * stream.streamLength)))
  503. {
  504. errorCode = 1;
  505. break;
  506. }
  507. /* Sample precision (only 8 bits/sample supported) */
  508. currentByte = JpegDecGetByte(&(stream));
  509. if(currentByte != 8)
  510. {
  511. printk
  512. ("JpegDecGetImageInfo# ERROR: Sample precision\n");
  513. return (JPEGDEC_UNSUPPORTED);
  514. }
  515. /* Number of Lines */
  516. pImageInfo->outputHeight = JpegDecGet2Bytes(&(stream));
  517. pImageInfo->displayHeight = pImageInfo->outputHeight;
  518. if(pImageInfo->outputHeight < 1)
  519. {
  520. printk
  521. ("JpegDecGetImageInfo# ERROR: pImageInfo->outputHeight Unsupported\n");
  522. return (JPEGDEC_UNSUPPORTED);
  523. }
  524. #ifdef JPEGDEC_ERROR_RESILIENCE
  525. if((pImageInfo->outputHeight & 0xF) &&
  526. (pImageInfo->outputHeight & 0xF) <= 8)
  527. errorResilience = 1;
  528. #endif /* JPEGDEC_ERROR_RESILIENCE */
  529. /* round up to next multiple-of-16 */
  530. pImageInfo->outputHeight += 0xf;
  531. pImageInfo->outputHeight &= ~(0xf);
  532. PTR_JPGC->frame.hwY = pImageInfo->outputHeight;
  533. /* Number of Samples per Line */
  534. pImageInfo->outputWidth = JpegDecGet2Bytes(&(stream));
  535. pImageInfo->displayWidth = pImageInfo->outputWidth;
  536. if(pImageInfo->outputWidth < 1)
  537. {
  538. printk
  539. ("JpegDecGetImageInfo# ERROR: pImageInfo->outputWidth unsupported\n");
  540. return (JPEGDEC_UNSUPPORTED);
  541. }
  542. pImageInfo->outputWidth += 0xf;
  543. pImageInfo->outputWidth &= ~(0xf);
  544. PTR_JPGC->frame.hwX = pImageInfo->outputWidth;
  545. /* check for minimum and maximum dimensions */
  546. if(pImageInfo->outputWidth < PTR_JPGC->minSupportedWidth ||
  547. pImageInfo->outputHeight < PTR_JPGC->minSupportedHeight ||
  548. pImageInfo->outputWidth > PTR_JPGC->maxSupportedWidth ||
  549. pImageInfo->outputHeight > PTR_JPGC->maxSupportedHeight ||
  550. (pImageInfo->outputWidth * pImageInfo->outputHeight) >
  551. PTR_JPGC->maxSupportedPixelAmount)
  552. {
  553. printk
  554. ("JpegDecGetImageInfo# ERROR: Unsupported size\n");
  555. return (JPEGDEC_UNSUPPORTED);
  556. }
  557. /* Number of Image Components per Frame */
  558. Nf = JpegDecGetByte(&(stream));
  559. if(Nf != 3 && Nf != 1)
  560. {
  561. printk
  562. ("JpegDecGetImageInfo# ERROR: Number of Image Components per Frame\n");
  563. return (JPEGDEC_UNSUPPORTED);
  564. }
  565. for(j = 0; j < Nf; j++)
  566. {
  567. /* jump over component identifier */
  568. if(JpegDecFlushBits(&(stream), 8) == STRM_ERROR)
  569. {
  570. errorCode = 1;
  571. break;
  572. }
  573. /* Horizontal sampling factor */
  574. currentByte = JpegDecGetByte(&(stream));
  575. H[j] = (currentByte >> 4);
  576. /* Vertical sampling factor */
  577. V[j] = (currentByte & 0xF);
  578. /* jump over Tq */
  579. if(JpegDecFlushBits(&(stream), 8) == STRM_ERROR)
  580. {
  581. errorCode = 1;
  582. break;
  583. }
  584. if(H[j] > Hmax)
  585. Hmax = H[j];
  586. if(V[j] > Vmax)
  587. Vmax = V[j];
  588. }
  589. if(Hmax == 0 || Vmax == 0)
  590. {
  591. printk
  592. ("JpegDecGetImageInfo# ERROR: Hmax == 0 || Vmax == 0\n");
  593. return (JPEGDEC_UNSUPPORTED);
  594. }
  595. #ifdef JPEGDEC_ERROR_RESILIENCE
  596. if(H[0] == 2 && V[0] == 2 &&
  597. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  598. {
  599. pImageInfo->outputFormat = JPEGDEC_YCbCr420_SEMIPLANAR;
  600. }
  601. else
  602. {
  603. /* check if fill needed */
  604. if(errorResilience)
  605. {
  606. pImageInfo->outputHeight -= 16;
  607. pImageInfo->displayHeight = pImageInfo->outputHeight;
  608. }
  609. }
  610. #endif /* JPEGDEC_ERROR_RESILIENCE */
  611. /* check format */
  612. if(H[0] == 2 && V[0] == 2 &&
  613. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  614. {
  615. pImageInfo->outputFormat = JPEGDEC_YCbCr420_SEMIPLANAR;
  616. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 16);
  617. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  618. PTR_JPGC->frame.hwY) /
  619. 256);
  620. }
  621. else if(H[0] == 2 && V[0] == 1 &&
  622. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  623. {
  624. pImageInfo->outputFormat = JPEGDEC_YCbCr422_SEMIPLANAR;
  625. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 16);
  626. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  627. PTR_JPGC->frame.hwY) /
  628. 128);
  629. }
  630. else if(H[0] == 1 && V[0] == 2 &&
  631. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  632. {
  633. pImageInfo->outputFormat = JPEGDEC_YCbCr440;
  634. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 8);
  635. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  636. PTR_JPGC->frame.hwY) /
  637. 128);
  638. }
  639. else if(H[0] == 1 && V[0] == 1 &&
  640. H[1] == 0 && V[1] == 0 && H[2] == 0 && V[2] == 0)
  641. {
  642. pImageInfo->outputFormat = JPEGDEC_YCbCr400;
  643. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 8);
  644. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  645. PTR_JPGC->frame.hwY) /
  646. 64);
  647. }
  648. else if(PTR_JPGC->extensionsSupported &&
  649. H[0] == 4 && V[0] == 1 &&
  650. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  651. {
  652. /* YUV411 output has to be 32 pixel multiple */
  653. if(pImageInfo->outputWidth & 0x1F)
  654. {
  655. pImageInfo->outputWidth += 16;
  656. PTR_JPGC->frame.hwX = pImageInfo->outputWidth;
  657. }
  658. /* check for maximum dimensions */
  659. if(pImageInfo->outputWidth > PTR_JPGC->maxSupportedWidth ||
  660. (pImageInfo->outputWidth * pImageInfo->outputHeight) >
  661. PTR_JPGC->maxSupportedPixelAmount)
  662. {
  663. printk
  664. ("JpegDecGetImageInfo# ERROR: Unsupported size\n");
  665. return (JPEGDEC_UNSUPPORTED);
  666. }
  667. pImageInfo->outputFormat = JPEGDEC_YCbCr411_SEMIPLANAR;
  668. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 32);
  669. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  670. PTR_JPGC->frame.hwY) /
  671. 256);
  672. }
  673. else if(PTR_JPGC->extensionsSupported &&
  674. H[0] == 1 && V[0] == 1 &&
  675. H[1] == 1 && V[1] == 1 && H[2] == 1 && V[2] == 1)
  676. {
  677. pImageInfo->outputFormat = JPEGDEC_YCbCr444_SEMIPLANAR;
  678. PTR_JPGC->frame.numMcuInRow = (PTR_JPGC->frame.hwX / 8);
  679. PTR_JPGC->frame.numMcuInFrame = ((PTR_JPGC->frame.hwX *
  680. PTR_JPGC->frame.hwY) /
  681. 64);
  682. }
  683. else
  684. {
  685. printk
  686. ("JpegDecGetImageInfo# ERROR: Unsupported YCbCr format\n");
  687. return (JPEGDEC_UNSUPPORTED);
  688. }
  689. /* restore output format */
  690. PTR_JPGC->info.yCbCrMode = PTR_JPGC->info.getInfoYCbCrMode =
  691. pImageInfo->outputFormat;
  692. break;
  693. case SOS:
  694. /* SOS length */
  695. headerLength = JpegDecGet2Bytes(&(stream));
  696. if(headerLength == STRM_ERROR ||
  697. ((stream.readBits + ((headerLength * 8) - 16)) >
  698. (8 * stream.streamLength)))
  699. {
  700. errorCode = 1;
  701. break;
  702. }
  703. /* check if interleaved or non-ibnterleaved */
  704. Ns = JpegDecGetByte(&(stream));
  705. if(Ns == MIN_NUMBER_OF_COMPONENTS &&
  706. pImageInfo->outputFormat != JPEGDEC_YCbCr400 &&
  707. pImageInfo->codingMode == JPEGDEC_BASELINE)
  708. {
  709. pImageInfo->codingMode = PTR_JPGC->info.operationType =
  710. JPEGDEC_NONINTERLEAVED;
  711. }
  712. /* jump over SOS header */
  713. if(headerLength != 0)
  714. {
  715. stream.readBits += ((headerLength * 8) - 16);
  716. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  717. }
  718. if((stream.readBits + 8) < (8 * stream.streamLength))
  719. {
  720. PTR_JPGC->info.init = 1;
  721. init = 1;
  722. }
  723. else
  724. {
  725. JPEGDEC_API_TRC
  726. ("JpegDecGetImageInfo# ERROR: Needs to increase input buffer\n");
  727. return (JPEGDEC_INCREASE_INPUT_BUFFER);
  728. }
  729. break;
  730. case DQT:
  731. /* DQT length */
  732. headerLength = JpegDecGet2Bytes(&(stream));
  733. if(headerLength == STRM_ERROR ||
  734. ((stream.readBits + ((headerLength * 8) - 16)) >
  735. (8 * stream.streamLength)))
  736. {
  737. errorCode = 1;
  738. break;
  739. }
  740. /* jump over DQT header */
  741. if(headerLength != 0)
  742. {
  743. stream.readBits += ((headerLength * 8) - 16);
  744. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  745. }
  746. break;
  747. case DHT:
  748. /* DHT length */
  749. headerLength = JpegDecGet2Bytes(&(stream));
  750. if(headerLength == STRM_ERROR ||
  751. ((stream.readBits + ((headerLength * 8) - 16)) >
  752. (8 * stream.streamLength)))
  753. {
  754. errorCode = 1;
  755. break;
  756. }
  757. /* jump over DHT header */
  758. if(headerLength != 0)
  759. {
  760. stream.readBits += ((headerLength * 8) - 16);
  761. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  762. }
  763. break;
  764. case DRI:
  765. /* DRI length */
  766. headerLength = JpegDecGet2Bytes(&(stream));
  767. if(headerLength == STRM_ERROR ||
  768. ((stream.readBits + ((headerLength * 8) - 16)) >
  769. (8 * stream.streamLength)))
  770. {
  771. errorCode = 1;
  772. break;
  773. }
  774. #if 0
  775. /* jump over DRI header */
  776. if(headerLength != 0)
  777. {
  778. stream.readBits += ((headerLength * 8) - 16);
  779. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  780. }
  781. #endif
  782. headerLength = JpegDecGet2Bytes(&(stream));
  783. if(headerLength == STRM_ERROR ||
  784. ((stream.readBits + ((headerLength * 8) - 16)) >
  785. (8 * stream.streamLength)))
  786. {
  787. errorCode = 1;
  788. break;
  789. }
  790. PTR_JPGC->frame.Ri = headerLength;
  791. break;
  792. /* application segments */
  793. case APP0:
  794. JPEGDEC_API_TRC("JpegDecGetImageInfo# APP0 in GetImageInfo\n");
  795. /* reset */
  796. appBits = 0;
  797. appLength = 0;
  798. stream.appnFlag = 0;
  799. /* APP0 length */
  800. headerLength = JpegDecGet2Bytes(&(stream));
  801. if(headerLength == STRM_ERROR ||
  802. ((stream.readBits + ((headerLength * 8) - 16)) >
  803. (8 * stream.streamLength)))
  804. {
  805. errorCode = 1;
  806. break;
  807. }
  808. appLength = headerLength;
  809. if(appLength < 16)
  810. {
  811. stream.appnFlag = 1;
  812. if(JpegDecFlushBits(&(stream), ((appLength * 8) - 16)) ==
  813. STRM_ERROR)
  814. {
  815. errorCode = 1;
  816. break;
  817. }
  818. break;
  819. }
  820. appBits += 16;
  821. /* check identifier */
  822. currentBytes = JpegDecGet2Bytes(&(stream));
  823. appBits += 16;
  824. if(currentBytes != 0x4A46)
  825. {
  826. stream.appnFlag = 1;
  827. if(JpegDecFlushBits(&(stream), ((appLength * 8) - appBits))
  828. == STRM_ERROR)
  829. {
  830. errorCode = 1;
  831. break;
  832. }
  833. break;
  834. }
  835. currentBytes = JpegDecGet2Bytes(&(stream));
  836. appBits += 16;
  837. if(currentBytes != 0x4946 && currentBytes != 0x5858)
  838. {
  839. stream.appnFlag = 1;
  840. if(JpegDecFlushBits(&(stream), ((appLength * 8) - appBits))
  841. == STRM_ERROR)
  842. {
  843. errorCode = 1;
  844. break;
  845. }
  846. break;
  847. }
  848. /* APP0 Extended */
  849. if(currentBytes == 0x5858)
  850. {
  851. thumbnail = 1;
  852. }
  853. currentByte = JpegDecGetByte(&(stream));
  854. appBits += 8;
  855. if(currentByte != 0x00)
  856. {
  857. stream.appnFlag = 1;
  858. if(JpegDecFlushBits(&(stream), ((appLength * 8) - appBits))
  859. == STRM_ERROR)
  860. {
  861. errorCode = 1;
  862. break;
  863. }
  864. stream.appnFlag = 0;
  865. break;
  866. }
  867. /* APP0 Extended thumb type */
  868. if(thumbnail)
  869. {
  870. /* extension code */
  871. currentByte = JpegDecGetByte(&(stream));
  872. if(currentByte == JPEGDEC_THUMBNAIL_JPEG)
  873. {
  874. pImageInfo->thumbnailType = JPEGDEC_THUMBNAIL_JPEG;
  875. appBits += 8;
  876. stream.appnFlag = 1;
  877. /* check thumbnail data */
  878. Hmax = 0;
  879. Vmax = 0;
  880. /* Read decoding parameters */
  881. for(; (stream.readBits / 8) < stream.streamLength;
  882. stream.readBits++)
  883. {
  884. /* Look for marker prefix byte from stream */
  885. appBits += 8;
  886. if(JpegDecGetByte(&(stream)) == 0xFF)
  887. {
  888. /* switch to certain header decoding */
  889. appBits += 8;
  890. currentByte = JpegDecGetByte(&(stream));
  891. switch (currentByte)
  892. {
  893. /* baseline marker */
  894. case SOF0:
  895. /* progresive marker */
  896. case SOF2:
  897. if(currentByte == SOF0)
  898. pImageInfo->codingModeThumb =
  899. PTR_JPGC->info.operationTypeThumb =
  900. JPEGDEC_BASELINE;
  901. else
  902. pImageInfo->codingModeThumb =
  903. PTR_JPGC->info.operationTypeThumb =
  904. JPEGDEC_PROGRESSIVE;
  905. /* Frame header */
  906. i++;
  907. /* jump over Lf field */
  908. if(JpegDecFlushBits(&(stream), 16) ==
  909. STRM_ERROR)
  910. {
  911. errorCode = 1;
  912. break;
  913. }
  914. appBits += 16;
  915. /* Sample precision (only 8 bits/sample supported) */
  916. currentByte = JpegDecGetByte(&(stream));
  917. appBits += 8;
  918. if(currentByte != 8)
  919. {
  920. JPEGDEC_API_TRC
  921. ("JpegDecGetImageInfo# ERROR: Thumbnail Sample precision");
  922. return (JPEGDEC_UNSUPPORTED);
  923. }
  924. /* Number of Lines */
  925. pImageInfo->outputHeightThumb =
  926. JpegDecGet2Bytes(&(stream));
  927. appBits += 16;
  928. pImageInfo->displayHeightThumb =
  929. pImageInfo->outputHeightThumb;
  930. if(pImageInfo->outputHeightThumb < 1)
  931. {
  932. JPEGDEC_API_TRC
  933. ("JpegDecGetImageInfo# ERROR: pImageInfo->outputHeightThumb unsupported\n");
  934. return (JPEGDEC_UNSUPPORTED);
  935. }
  936. #ifdef JPEGDEC_ERROR_RESILIENCE
  937. if((pImageInfo->outputHeightThumb & 0xF) &&
  938. (pImageInfo->outputHeightThumb & 0xF) <=
  939. 8)
  940. errorResilienceThumb = 1;
  941. #endif /* JPEGDEC_ERROR_RESILIENCE */
  942. /* round up to next multiple-of-16 */
  943. pImageInfo->outputHeightThumb += 0xf;
  944. pImageInfo->outputHeightThumb &= ~(0xf);
  945. /* Number of Samples per Line */
  946. pImageInfo->outputWidthThumb =
  947. JpegDecGet2Bytes(&(stream));
  948. appBits += 16;
  949. pImageInfo->displayWidthThumb =
  950. pImageInfo->outputWidthThumb;
  951. if(pImageInfo->outputWidthThumb < 1)
  952. {
  953. JPEGDEC_API_TRC
  954. ("JpegDecGetImageInfo# ERROR: pImageInfo->outputWidthThumb unsupported\n");
  955. return (JPEGDEC_UNSUPPORTED);
  956. }
  957. pImageInfo->outputWidthThumb += 0xf;
  958. pImageInfo->outputWidthThumb &= ~(0xf);
  959. if(pImageInfo->outputWidthThumb <
  960. PTR_JPGC->minSupportedWidth ||
  961. pImageInfo->outputHeightThumb <
  962. PTR_JPGC->minSupportedHeight ||
  963. pImageInfo->outputWidthThumb >
  964. JPEGDEC_MAX_WIDTH_TN ||
  965. pImageInfo->outputHeightThumb >
  966. JPEGDEC_MAX_HEIGHT_TN)
  967. {
  968. JPEGDEC_API_TRC
  969. ("JpegDecGetImageInfo# ERROR: Thumbnail Unsupported size\n");
  970. return (JPEGDEC_UNSUPPORTED);
  971. }
  972. /* Number of Image Components per Frame */
  973. Nf = JpegDecGetByte(&(stream));
  974. appBits += 8;
  975. if(Nf != 3 && Nf != 1)
  976. {
  977. JPEGDEC_API_TRC
  978. ("JpegDecGetImageInfo# ERROR: Thumbnail Number of Image Components per Frame\n");
  979. return (JPEGDEC_UNSUPPORTED);
  980. }
  981. for(j = 0; j < Nf; j++)
  982. {
  983. /* jump over component identifier */
  984. if(JpegDecFlushBits(&(stream), 8) ==
  985. STRM_ERROR)
  986. {
  987. errorCode = 1;
  988. break;
  989. }
  990. appBits += 8;
  991. /* Horizontal sampling factor */
  992. currentByte = JpegDecGetByte(&(stream));
  993. appBits += 8;
  994. Htn[j] = (currentByte >> 4);
  995. /* Vertical sampling factor */
  996. Vtn[j] = (currentByte & 0xF);
  997. /* jump over Tq */
  998. if(JpegDecFlushBits(&(stream), 8) ==
  999. STRM_ERROR)
  1000. {
  1001. errorCode = 1;
  1002. break;
  1003. }
  1004. appBits += 8;
  1005. if(Htn[j] > Hmax)
  1006. Hmax = Htn[j];
  1007. if(Vtn[j] > Vmax)
  1008. Vmax = Vtn[j];
  1009. }
  1010. if(Hmax == 0 || Vmax == 0)
  1011. {
  1012. JPEGDEC_API_TRC
  1013. ("JpegDecGetImageInfo# ERROR: Thumbnail Hmax == 0 || Vmax == 0\n");
  1014. return (JPEGDEC_UNSUPPORTED);
  1015. }
  1016. #ifdef JPEGDEC_ERROR_RESILIENCE
  1017. if(Htn[0] == 2 && Vtn[0] == 2 &&
  1018. Htn[1] == 1 && Vtn[1] == 1 &&
  1019. Htn[2] == 1 && Vtn[2] == 1)
  1020. {
  1021. pImageInfo->outputFormatThumb =
  1022. JPEGDEC_YCbCr420_SEMIPLANAR;
  1023. }
  1024. else
  1025. {
  1026. /* check if fill needed */
  1027. if(errorResilienceThumb)
  1028. {
  1029. pImageInfo->outputHeightThumb -= 16;
  1030. pImageInfo->displayHeightThumb =
  1031. pImageInfo->outputHeightThumb;
  1032. }
  1033. }
  1034. #endif /* JPEGDEC_ERROR_RESILIENCE */
  1035. /* check format */
  1036. if(Htn[0] == 2 && Vtn[0] == 2 &&
  1037. Htn[1] == 1 && Vtn[1] == 1 &&
  1038. Htn[2] == 1 && Vtn[2] == 1)
  1039. {
  1040. pImageInfo->outputFormatThumb =
  1041. JPEGDEC_YCbCr420_SEMIPLANAR;
  1042. }
  1043. else if(Htn[0] == 2 && Vtn[0] == 1 &&
  1044. Htn[1] == 1 && Vtn[1] == 1 &&
  1045. Htn[2] == 1 && Vtn[2] == 1)
  1046. {
  1047. pImageInfo->outputFormatThumb =
  1048. JPEGDEC_YCbCr422_SEMIPLANAR;
  1049. }
  1050. else if(Htn[0] == 1 && Vtn[0] == 2 &&
  1051. Htn[1] == 1 && Vtn[1] == 1 &&
  1052. Htn[2] == 1 && Vtn[2] == 1)
  1053. {
  1054. pImageInfo->outputFormatThumb =
  1055. JPEGDEC_YCbCr440;
  1056. }
  1057. else if(Htn[0] == 1 && Vtn[0] == 1 &&
  1058. Htn[1] == 0 && Vtn[1] == 0 &&
  1059. Htn[2] == 0 && Vtn[2] == 0)
  1060. {
  1061. pImageInfo->outputFormatThumb =
  1062. JPEGDEC_YCbCr400;
  1063. }
  1064. else if(PTR_JPGC->is8190 &&
  1065. Htn[0] == 4 && Vtn[0] == 1 &&
  1066. Htn[1] == 1 && Vtn[1] == 1 &&
  1067. Htn[2] == 1 && Vtn[2] == 1)
  1068. {
  1069. pImageInfo->outputFormatThumb =
  1070. JPEGDEC_YCbCr411_SEMIPLANAR;
  1071. }
  1072. else if(PTR_JPGC->is8190 &&
  1073. Htn[0] == 1 && Vtn[0] == 1 &&
  1074. Htn[1] == 1 && Vtn[1] == 1 &&
  1075. Htn[2] == 1 && Vtn[2] == 1)
  1076. {
  1077. pImageInfo->outputFormatThumb =
  1078. JPEGDEC_YCbCr444_SEMIPLANAR;
  1079. }
  1080. else
  1081. {
  1082. JPEGDEC_API_TRC
  1083. ("JpegDecGetImageInfo# ERROR: Thumbnail Unsupported YCbCr format\n");
  1084. return (JPEGDEC_UNSUPPORTED);
  1085. }
  1086. PTR_JPGC->info.initThumb = 1;
  1087. initThumb = 1;
  1088. break;
  1089. case SOS:
  1090. /* SOS length */
  1091. headerLength = JpegDecGet2Bytes(&(stream));
  1092. if(headerLength == STRM_ERROR ||
  1093. ((stream.readBits +
  1094. ((headerLength * 8) - 16)) >
  1095. (8 * stream.streamLength)))
  1096. {
  1097. errorCode = 1;
  1098. break;
  1099. }
  1100. /* check if interleaved or non-ibnterleaved */
  1101. NsThumb = JpegDecGetByte(&(stream));
  1102. if(NsThumb == MIN_NUMBER_OF_COMPONENTS &&
  1103. pImageInfo->outputFormatThumb !=
  1104. JPEGDEC_YCbCr400 &&
  1105. pImageInfo->codingModeThumb ==
  1106. JPEGDEC_BASELINE)
  1107. {
  1108. pImageInfo->codingModeThumb =
  1109. PTR_JPGC->info.operationTypeThumb =
  1110. JPEGDEC_NONINTERLEAVED;
  1111. }
  1112. /* jump over SOS header */
  1113. if(headerLength != 0)
  1114. {
  1115. stream.readBits +=
  1116. ((headerLength * 8) - 16);
  1117. stream.pCurrPos +=
  1118. (((headerLength * 8) - 16) / 8);
  1119. }
  1120. if((stream.readBits + 8) <
  1121. (8 * stream.streamLength))
  1122. {
  1123. PTR_JPGC->info.init = 1;
  1124. init = 1;
  1125. }
  1126. else
  1127. {
  1128. JPEGDEC_API_TRC
  1129. ("JpegDecGetImageInfo# ERROR: Needs to increase input buffer\n");
  1130. return (JPEGDEC_INCREASE_INPUT_BUFFER);
  1131. }
  1132. break;
  1133. case DQT:
  1134. /* DQT length */
  1135. headerLength = JpegDecGet2Bytes(&(stream));
  1136. if(headerLength == STRM_ERROR)
  1137. {
  1138. errorCode = 1;
  1139. break;
  1140. }
  1141. /* jump over DQT header */
  1142. if(headerLength != 0)
  1143. {
  1144. stream.readBits +=
  1145. ((headerLength * 8) - 16);
  1146. stream.pCurrPos +=
  1147. (((headerLength * 8) - 16) / 8);
  1148. }
  1149. appBits += (headerLength * 8);
  1150. break;
  1151. case DHT:
  1152. /* DHT length */
  1153. headerLength = JpegDecGet2Bytes(&(stream));
  1154. if(headerLength == STRM_ERROR)
  1155. {
  1156. errorCode = 1;
  1157. break;
  1158. }
  1159. /* jump over DHT header */
  1160. if(headerLength != 0)
  1161. {
  1162. stream.readBits +=
  1163. ((headerLength * 8) - 16);
  1164. stream.pCurrPos +=
  1165. (((headerLength * 8) - 16) / 8);
  1166. }
  1167. appBits += (headerLength * 8);
  1168. break;
  1169. case DRI:
  1170. /* DRI length */
  1171. headerLength = JpegDecGet2Bytes(&(stream));
  1172. if(headerLength == STRM_ERROR)
  1173. {
  1174. errorCode = 1;
  1175. break;
  1176. }
  1177. /* jump over DRI header */
  1178. if(headerLength != 0)
  1179. {
  1180. stream.readBits +=
  1181. ((headerLength * 8) - 16);
  1182. stream.pCurrPos +=
  1183. (((headerLength * 8) - 16) / 8);
  1184. }
  1185. appBits += (headerLength * 8);
  1186. break;
  1187. case APP0:
  1188. case APP1:
  1189. case APP2:
  1190. case APP3:
  1191. case APP4:
  1192. case APP5:
  1193. case APP6:
  1194. case APP7:
  1195. case APP8:
  1196. case APP9:
  1197. case APP10:
  1198. case APP11:
  1199. case APP12:
  1200. case APP13:
  1201. case APP14:
  1202. case APP15:
  1203. /* APPn length */
  1204. headerLength = JpegDecGet2Bytes(&(stream));
  1205. if(headerLength == STRM_ERROR)
  1206. {
  1207. errorCode = 1;
  1208. break;
  1209. }
  1210. /* jump over APPn header */
  1211. if(headerLength != 0)
  1212. {
  1213. stream.readBits +=
  1214. ((headerLength * 8) - 16);
  1215. stream.pCurrPos +=
  1216. (((headerLength * 8) - 16) / 8);
  1217. }
  1218. appBits += (headerLength * 8);
  1219. break;
  1220. case DNL:
  1221. /* DNL length */
  1222. headerLength = JpegDecGet2Bytes(&(stream));
  1223. if(headerLength == STRM_ERROR)
  1224. {
  1225. errorCode = 1;
  1226. break;
  1227. }
  1228. /* jump over DNL header */
  1229. if(headerLength != 0)
  1230. {
  1231. stream.readBits +=
  1232. ((headerLength * 8) - 16);
  1233. stream.pCurrPos +=
  1234. (((headerLength * 8) - 16) / 8);
  1235. }
  1236. appBits += (headerLength * 8);
  1237. break;
  1238. case COM:
  1239. /* COM length */
  1240. headerLength = JpegDecGet2Bytes(&(stream));
  1241. if(headerLength == STRM_ERROR)
  1242. {
  1243. errorCode = 1;
  1244. break;
  1245. }
  1246. /* jump over COM header */
  1247. if(headerLength != 0)
  1248. {
  1249. stream.readBits +=
  1250. ((headerLength * 8) - 16);
  1251. stream.pCurrPos +=
  1252. (((headerLength * 8) - 16) / 8);
  1253. }
  1254. appBits += (headerLength * 8);
  1255. break;
  1256. /* unsupported coding styles */
  1257. case SOF1:
  1258. case SOF3:
  1259. case SOF5:
  1260. case SOF6:
  1261. case SOF7:
  1262. case SOF9:
  1263. case SOF10:
  1264. case SOF11:
  1265. case SOF13:
  1266. case SOF14:
  1267. case SOF15:
  1268. case DAC:
  1269. case DHP:
  1270. JPEGDEC_API_TRC
  1271. ("JpegDecGetImageInfo# ERROR: Unsupported coding styles\n");
  1272. return (JPEGDEC_UNSUPPORTED);
  1273. default:
  1274. break;
  1275. }
  1276. if(PTR_JPGC->info.initThumb && initThumb)
  1277. {
  1278. /* flush the rest of thumbnail data */
  1279. if(JpegDecFlushBits
  1280. (&(stream),
  1281. ((appLength * 8) - appBits)) ==
  1282. STRM_ERROR)
  1283. {
  1284. errorCode = 1;
  1285. break;
  1286. }
  1287. stream.appnFlag = 0;
  1288. break;
  1289. }
  1290. }
  1291. else
  1292. {
  1293. if(!PTR_JPGC->info.initThumb &&
  1294. pDecIn->bufferSize)
  1295. return (JPEGDEC_INCREASE_INPUT_BUFFER);
  1296. else
  1297. return (JPEGDEC_STRM_ERROR);
  1298. }
  1299. }
  1300. break;
  1301. }
  1302. else
  1303. {
  1304. appBits += 8;
  1305. pImageInfo->thumbnailType =
  1306. JPEGDEC_THUMBNAIL_NOT_SUPPORTED_FORMAT;
  1307. stream.appnFlag = 1;
  1308. if(JpegDecFlushBits
  1309. (&(stream),
  1310. ((appLength * 8) - appBits)) == STRM_ERROR)
  1311. {
  1312. errorCode = 1;
  1313. break;
  1314. }
  1315. stream.appnFlag = 0;
  1316. break;
  1317. }
  1318. }
  1319. else
  1320. {
  1321. /* version */
  1322. pImageInfo->version = JpegDecGet2Bytes(&(stream));
  1323. appBits += 16;
  1324. /* units */
  1325. currentByte = JpegDecGetByte(&(stream));
  1326. if(currentByte == 0)
  1327. {
  1328. pImageInfo->units = JPEGDEC_NO_UNITS;
  1329. }
  1330. else if(currentByte == 1)
  1331. {
  1332. pImageInfo->units = JPEGDEC_DOTS_PER_INCH;
  1333. }
  1334. else if(currentByte == 2)
  1335. {
  1336. pImageInfo->units = JPEGDEC_DOTS_PER_CM;
  1337. }
  1338. appBits += 8;
  1339. /* Xdensity */
  1340. pImageInfo->xDensity = JpegDecGet2Bytes(&(stream));
  1341. appBits += 16;
  1342. /* Ydensity */
  1343. pImageInfo->yDensity = JpegDecGet2Bytes(&(stream));
  1344. appBits += 16;
  1345. /* jump over rest of header data */
  1346. stream.appnFlag = 1;
  1347. if(JpegDecFlushBits(&(stream), ((appLength * 8) - appBits))
  1348. == STRM_ERROR)
  1349. {
  1350. errorCode = 1;
  1351. break;
  1352. }
  1353. stream.appnFlag = 0;
  1354. break;
  1355. }
  1356. case APP1:
  1357. case APP2:
  1358. case APP3:
  1359. case APP4:
  1360. case APP5:
  1361. case APP6:
  1362. case APP7:
  1363. case APP8:
  1364. case APP9:
  1365. case APP10:
  1366. case APP11:
  1367. case APP12:
  1368. case APP13:
  1369. case APP14:
  1370. case APP15:
  1371. /* APPn length */
  1372. headerLength = JpegDecGet2Bytes(&(stream));
  1373. if(headerLength == STRM_ERROR ||
  1374. ((stream.readBits + ((headerLength * 8) - 16)) >
  1375. (8 * stream.streamLength)))
  1376. {
  1377. errorCode = 1;
  1378. break;
  1379. }
  1380. /* jump over APPn header */
  1381. if(headerLength != 0)
  1382. {
  1383. stream.readBits += ((headerLength * 8) - 16);
  1384. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  1385. }
  1386. break;
  1387. case DNL:
  1388. /* DNL length */
  1389. headerLength = JpegDecGet2Bytes(&(stream));
  1390. if(headerLength == STRM_ERROR ||
  1391. ((stream.readBits + ((headerLength * 8) - 16)) >
  1392. (8 * stream.streamLength)))
  1393. {
  1394. errorCode = 1;
  1395. break;
  1396. }
  1397. /* jump over DNL header */
  1398. if(headerLength != 0)
  1399. {
  1400. stream.readBits += ((headerLength * 8) - 16);
  1401. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  1402. }
  1403. break;
  1404. case COM:
  1405. headerLength = JpegDecGet2Bytes(&(stream));
  1406. if(headerLength == STRM_ERROR ||
  1407. ((stream.readBits + ((headerLength * 8) - 16)) >
  1408. (8 * stream.streamLength)))
  1409. {
  1410. errorCode = 1;
  1411. break;
  1412. }
  1413. /* jump over COM header */
  1414. if(headerLength != 0)
  1415. {
  1416. stream.readBits += ((headerLength * 8) - 16);
  1417. stream.pCurrPos += (((headerLength * 8) - 16) / 8);
  1418. }
  1419. break;
  1420. /* unsupported coding styles */
  1421. case SOF1:
  1422. case SOF3:
  1423. case SOF5:
  1424. case SOF6:
  1425. case SOF7:
  1426. case SOF9:
  1427. case SOF10:
  1428. case SOF11:
  1429. case SOF13:
  1430. case SOF14:
  1431. case SOF15:
  1432. case DAC:
  1433. case DHP:
  1434. JPEGDEC_API_TRC
  1435. ("JpegDecGetImageInfo# ERROR: Unsupported coding styles\n");
  1436. return (JPEGDEC_UNSUPPORTED);
  1437. default:
  1438. break;
  1439. }
  1440. if(PTR_JPGC->info.init && init)
  1441. break;
  1442. if(errorCode)
  1443. {
  1444. if(pDecIn->bufferSize)
  1445. {
  1446. JPEGDEC_API_TRC
  1447. ("JpegDecGetImageInfo# ERROR: Image info failed!\n");
  1448. return (JPEGDEC_INCREASE_INPUT_BUFFER);
  1449. }
  1450. else
  1451. {
  1452. JPEGDEC_API_TRC("JpegDecGetImageInfo# ERROR: Stream error\n");
  1453. return (JPEGDEC_STRM_ERROR);
  1454. }
  1455. }
  1456. }
  1457. else
  1458. {
  1459. if(!PTR_JPGC->info.init)
  1460. return (JPEGDEC_INCREASE_INPUT_BUFFER);
  1461. else
  1462. return (JPEGDEC_STRM_ERROR);
  1463. }
  1464. }
  1465. if(PTR_JPGC->info.init)
  1466. {
  1467. if(pDecIn->bufferSize)
  1468. PTR_JPGC->info.initBufferSize = pDecIn->bufferSize;
  1469. return (JPEGDEC_OK);
  1470. }
  1471. else
  1472. {
  1473. printk("JpegDecGetImageInfo# ERROR\n");
  1474. return (JPEGDEC_ERROR);
  1475. }
  1476. #undef PTR_JPGC
  1477. }
  1478. /*------------------------------------------------------------------------------
  1479. Function name: JpegDecDecode
  1480. Functional description:
  1481. Decode JFIF
  1482. Inputs:
  1483. JpegDecInst decInst jpeg decoder instance
  1484. JpegDecInput *pDecIn pointer to structure where the decoder
  1485. stores input information
  1486. JpegDecOutput *pDecOut pointer to structure where the decoder
  1487. stores output frame information
  1488. Outputs:
  1489. JPEGDEC_FRAME_READY
  1490. JPEGDEC_PARAM_ERROR
  1491. JPEGDEC_INVALID_STREAM_LENGTH
  1492. JPEGDEC_INVALID_INPUT_BUFFER_SIZE
  1493. JPEGDEC_UNSUPPORTED
  1494. JPEGDEC_ERROR
  1495. JPEGDEC_STRM_ERROR
  1496. JPEGDEC_HW_BUS_ERROR
  1497. JPEGDEC_DWL_HW_TIMEOUT
  1498. JPEGDEC_SYSTEM_ERROR
  1499. JPEGDEC_HW_RESERVED
  1500. JPEGDEC_STRM_PROCESSED
  1501. ------------------------------------------------------------------------------*/
  1502. JpegDecRet JpegDecDecode(JpegDecInst decInst, JpegDecInput * pDecIn,
  1503. JpegDecOutput * pDecOut)
  1504. {
  1505. #define PTR_JPGC ((JpegDecContainer *) decInst)
  1506. #define JPG_FRM PTR_JPGC->frame
  1507. #define JPG_INFO PTR_JPGC->info
  1508. i32 dwlret = -1;
  1509. u32 i = 0;
  1510. u32 currentByte = 0;
  1511. u32 currentBytes = 0;
  1512. u32 appLength = 0;
  1513. u32 appBits = 0;
  1514. u32 asic_status = 0;
  1515. u32 HINTdec = 0;
  1516. u32 asicSliceBit = 0;
  1517. u32 intDec = 0;
  1518. u32 currentPos = 0;
  1519. u32 endOfImage = 0;
  1520. u32 nonInterleavedRdy = 0;
  1521. JpegDecRet infoRet;
  1522. JpegDecRet retCode; /* Returned code container */
  1523. JpegDecImageInfo infoTmp;
  1524. u32 mcuSizeDivider = 0;
  1525. /* check null */
  1526. if(decInst == NULL || pDecIn == NULL ||
  1527. X170_CHECK_VIRTUAL_ADDRESS(pDecIn->streamBuffer.pVirtualAddress) ||
  1528. X170_CHECK_BUS_ADDRESS(pDecIn->streamBuffer.busAddress) ||
  1529. pDecOut == NULL)
  1530. {
  1531. printk("JpegDecDecode# ERROR: NULL parameter");
  1532. return (JPEGDEC_PARAM_ERROR);
  1533. }
  1534. /* check image decoding type */
  1535. if(pDecIn->decImageType != 0 && pDecIn->decImageType != 1)
  1536. {
  1537. printk("JpegDecDecode# ERROR: decImageType");
  1538. return (JPEGDEC_PARAM_ERROR);
  1539. }
  1540. /* check user allocated null */
  1541. if((pDecIn->pictureBufferY.pVirtualAddress == NULL &&
  1542. pDecIn->pictureBufferY.busAddress != 0) ||
  1543. (pDecIn->pictureBufferY.pVirtualAddress != NULL &&
  1544. pDecIn->pictureBufferY.busAddress == 0) ||
  1545. (pDecIn->pictureBufferCbCr.pVirtualAddress == NULL &&
  1546. pDecIn->pictureBufferCbCr.busAddress != 0) ||
  1547. (pDecIn->pictureBufferCbCr.pVirtualAddress != NULL &&
  1548. pDecIn->pictureBufferCbCr.busAddress == 0))
  1549. {
  1550. printk("JpegDecDecode# ERROR: NULL parameter");
  1551. return (JPEGDEC_PARAM_ERROR);
  1552. }
  1553. /* Check the stream lenth */
  1554. if(pDecIn->streamLength < 1)
  1555. {
  1556. printk("JpegDecDecode# ERROR: pDecIn->streamLength");
  1557. return (JPEGDEC_INVALID_STREAM_LENGTH);
  1558. }
  1559. /* check the input buffer settings ==>
  1560. * checks are discarded for last buffer */
  1561. if(!PTR_JPGC->info.streamEndFlag)
  1562. {
  1563. /* Check the stream lenth */
  1564. if(!PTR_JPGC->info.inputBufferEmpty &&
  1565. (pDecIn->streamLength > DEC_X170_MAX_STREAM) &&
  1566. (pDecIn->bufferSize < JPEGDEC_X170_MIN_BUFFER ||
  1567. pDecIn->bufferSize > JPEGDEC_X170_MAX_BUFFER))
  1568. {
  1569. printk("JpegDecDecode# ERROR: pDecIn->bufferSize");
  1570. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  1571. }
  1572. /* Check the stream buffer size */
  1573. if(!PTR_JPGC->info.inputBufferEmpty &&
  1574. pDecIn->bufferSize && (pDecIn->bufferSize < JPEGDEC_X170_MIN_BUFFER
  1575. || pDecIn->bufferSize >
  1576. JPEGDEC_X170_MAX_BUFFER))
  1577. {
  1578. printk("JpegDecDecode# ERROR: pDecIn->bufferSize");
  1579. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  1580. }
  1581. /* Check the stream buffer size */
  1582. if(!PTR_JPGC->info.inputBufferEmpty &&
  1583. pDecIn->bufferSize && ((pDecIn->bufferSize % 256) != 0))
  1584. {
  1585. printk("JpegDecDecode# ERROR: pDecIn->bufferSize %% 256) != 0");
  1586. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  1587. }
  1588. if(!PTR_JPGC->info.inputBufferEmpty &&
  1589. PTR_JPGC->info.init &&
  1590. (pDecIn->bufferSize < PTR_JPGC->info.initBufferSize))
  1591. {
  1592. printk
  1593. ("JpegDecDecode# ERROR: Increase input buffer size!\n");
  1594. return (JPEGDEC_INVALID_INPUT_BUFFER_SIZE);
  1595. }
  1596. }
  1597. if(!PTR_JPGC->info.init && !PTR_JPGC->info.SliceReadyForPause &&
  1598. !PTR_JPGC->info.inputBufferEmpty)
  1599. {
  1600. printk(("JpegDecDecode#: Get Image Info!\n"));
  1601. infoRet = JpegDecGetImageInfo(decInst, pDecIn, &infoTmp);
  1602. if(infoRet != JPEGDEC_OK)
  1603. {
  1604. printk(("JpegDecDecode# ERROR: Image info failed!\n"));
  1605. return (infoRet);
  1606. }
  1607. }
  1608. /* Store the stream parameters */
  1609. if(PTR_JPGC->info.progressiveScanReady == 0 &&
  1610. PTR_JPGC->info.nonInterleavedScanReady == 0)
  1611. {
  1612. PTR_JPGC->stream.bitPosInByte = 0;
  1613. PTR_JPGC->stream.pCurrPos = (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  1614. PTR_JPGC->stream.streamBus = pDecIn->streamBuffer.busAddress;
  1615. PTR_JPGC->stream.pStartOfStream =
  1616. (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  1617. PTR_JPGC->stream.readBits = 0;
  1618. PTR_JPGC->stream.streamLength = pDecIn->streamLength;
  1619. PTR_JPGC->stream.appnFlag = 0;
  1620. pDecOut->outputPictureY.pVirtualAddress = NULL;
  1621. pDecOut->outputPictureY.busAddress = 0;
  1622. pDecOut->outputPictureCbCr.pVirtualAddress = NULL;
  1623. pDecOut->outputPictureCbCr.busAddress = 0;
  1624. pDecOut->outputPictureCr.pVirtualAddress = NULL;
  1625. pDecOut->outputPictureCr.busAddress = 0;
  1626. }
  1627. else
  1628. {
  1629. PTR_JPGC->image.headerReady = 0;
  1630. }
  1631. /* set mcu/slice value */
  1632. PTR_JPGC->info.sliceMbSetValue = pDecIn->sliceMbSet;
  1633. /* check HW supported features */
  1634. if(!PTR_JPGC->is8190)
  1635. {
  1636. /* return if not valid HW and unsupported operation type */
  1637. if(PTR_JPGC->info.operationType == JPEGDEC_NONINTERLEAVED ||
  1638. PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE)
  1639. {
  1640. printk
  1641. ("JpegDecDecode# ERROR: Operation type not supported\n");
  1642. return (JPEGDEC_UNSUPPORTED);
  1643. }
  1644. if(PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr400 ||
  1645. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr440 ||
  1646. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr444_SEMIPLANAR)
  1647. mcuSizeDivider = 2;
  1648. else
  1649. mcuSizeDivider = 1;
  1650. /* check slice config */
  1651. if((pDecIn->sliceMbSet * (JPG_FRM.numMcuInRow / mcuSizeDivider)) >
  1652. PTR_JPGC->maxSupportedSliceSize)
  1653. {
  1654. printk
  1655. ("JpegDecDecode# ERROR: sliceMbSet > JPEGDEC_MAX_SLICE_SIZE\n");
  1656. return (JPEGDEC_PARAM_ERROR);
  1657. }
  1658. /* check frame size */
  1659. if((!pDecIn->sliceMbSet) &&
  1660. JPG_FRM.numMcuInFrame > PTR_JPGC->maxSupportedSliceSize)
  1661. {
  1662. printk
  1663. ("JpegDecDecode# ERROR: mcuInFrame > JPEGDEC_MAX_SLICE_SIZE\n");
  1664. return (JPEGDEC_PARAM_ERROR);
  1665. }
  1666. }
  1667. else
  1668. {
  1669. /* check if fuse was burned */
  1670. if(PTR_JPGC->fuseBurned)
  1671. {
  1672. /* return if not valid HW and unsupported operation type */
  1673. if(PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE)
  1674. {
  1675. printk
  1676. ("JpegDecDecode# ERROR: Operation type not supported\n");
  1677. return (JPEGDEC_UNSUPPORTED);
  1678. }
  1679. }
  1680. /* check slice config */
  1681. if((pDecIn->sliceMbSet && pDecIn->decImageType == JPEGDEC_IMAGE &&
  1682. PTR_JPGC->info.operationType != JPEGDEC_BASELINE) ||
  1683. (pDecIn->sliceMbSet && pDecIn->decImageType == JPEGDEC_THUMBNAIL &&
  1684. PTR_JPGC->info.operationTypeThumb != JPEGDEC_BASELINE))
  1685. {
  1686. printk
  1687. ("JpegDecDecode# ERROR: Slice mode not supported for this operation type\n");
  1688. return (JPEGDEC_SLICE_MODE_UNSUPPORTED);
  1689. }
  1690. /* check if frame size over 16M */
  1691. if((!pDecIn->sliceMbSet) &&
  1692. ((JPG_FRM.hwX * JPG_FRM.hwY) > JPEGDEC_MAX_PIXEL_AMOUNT))
  1693. {
  1694. JPEGDEC_API_TRC
  1695. ("JpegDecDecode# ERROR: Resolution > 16M ==> use slice mode!\n");
  1696. return (JPEGDEC_PARAM_ERROR);
  1697. }
  1698. if(PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr400 ||
  1699. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr440 ||
  1700. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr444_SEMIPLANAR)
  1701. mcuSizeDivider = 2;
  1702. else
  1703. mcuSizeDivider = 1;
  1704. /* check slice config */
  1705. if((pDecIn->sliceMbSet * (JPG_FRM.numMcuInRow / mcuSizeDivider)) >
  1706. PTR_JPGC->maxSupportedSliceSize)
  1707. {
  1708. JPEGDEC_API_TRC
  1709. ("JpegDecDecode# ERROR: sliceMbSet > JPEGDEC_MAX_SLICE_SIZE\n");
  1710. return (JPEGDEC_PARAM_ERROR);
  1711. }
  1712. }
  1713. /* check slice size */
  1714. if(pDecIn->sliceMbSet &&
  1715. !PTR_JPGC->info.SliceReadyForPause && !PTR_JPGC->info.inputBufferEmpty)
  1716. {
  1717. if(PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr400 ||
  1718. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr440 ||
  1719. PTR_JPGC->info.getInfoYCbCrMode == JPEGDEC_YCbCr444_SEMIPLANAR)
  1720. mcuSizeDivider = 2;
  1721. else
  1722. mcuSizeDivider = 1;
  1723. if((pDecIn->sliceMbSet * (JPG_FRM.numMcuInRow / mcuSizeDivider)) >
  1724. JPG_FRM.numMcuInFrame)
  1725. {
  1726. printk
  1727. ("JpegDecDecode# ERROR: (sliceMbSet * Number of MCU's in row) > Number of MCU's in frame\n");
  1728. return (JPEGDEC_PARAM_ERROR);
  1729. }
  1730. }
  1731. /* Handle stream/hw parameters after buffer empty */
  1732. if(PTR_JPGC->info.inputBufferEmpty)
  1733. {
  1734. /* Store the stream parameters */
  1735. PTR_JPGC->stream.bitPosInByte = 0;
  1736. PTR_JPGC->stream.pCurrPos = (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  1737. PTR_JPGC->stream.streamBus = pDecIn->streamBuffer.busAddress;
  1738. PTR_JPGC->stream.pStartOfStream =
  1739. (u8 *) pDecIn->streamBuffer.pVirtualAddress;
  1740. /* update hw parameters */
  1741. PTR_JPGC->info.inputStreaming = 1;
  1742. if(pDecIn->bufferSize)
  1743. PTR_JPGC->info.inputBufferLen = pDecIn->bufferSize;
  1744. else
  1745. PTR_JPGC->info.inputBufferLen = pDecIn->streamLength;
  1746. /* decoded stream */
  1747. PTR_JPGC->info.decodedStreamLen += PTR_JPGC->info.inputBufferLen;
  1748. if(PTR_JPGC->info.decodedStreamLen > pDecIn->streamLength)
  1749. {
  1750. printk
  1751. ("JpegDecDecode# Error: All input stream already processed\n");
  1752. return JPEGDEC_STRM_ERROR;
  1753. }
  1754. }
  1755. /* update user allocated output */
  1756. PTR_JPGC->info.givenOutLuma.virtualAddress =
  1757. pDecIn->pictureBufferY.pVirtualAddress;
  1758. PTR_JPGC->info.givenOutLuma.busAddress = pDecIn->pictureBufferY.busAddress;
  1759. PTR_JPGC->info.givenOutChroma.virtualAddress =
  1760. pDecIn->pictureBufferCbCr.pVirtualAddress;
  1761. PTR_JPGC->info.givenOutChroma.busAddress =
  1762. pDecIn->pictureBufferCbCr.busAddress;
  1763. PTR_JPGC->info.givenOutChroma2.virtualAddress =
  1764. pDecIn->pictureBufferCr.pVirtualAddress;
  1765. PTR_JPGC->info.givenOutChroma2.busAddress =
  1766. pDecIn->pictureBufferCr.busAddress;
  1767. if(PTR_JPGC->info.progressiveFinish)
  1768. {
  1769. /* output set */
  1770. if(PTR_JPGC->ppInstance == NULL)
  1771. {
  1772. pDecOut->outputPictureY.pVirtualAddress =
  1773. PTR_JPGC->info.outLuma.virtualAddress;
  1774. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  1775. /* output set */
  1776. pDecOut->outputPictureY.busAddress =
  1777. PTR_JPGC->info.outLuma.busAddress;
  1778. ASSERT(pDecOut->outputPictureY.busAddress);
  1779. /* if not grayscale */
  1780. if(PTR_JPGC->image.sizeChroma)
  1781. {
  1782. pDecOut->outputPictureCbCr.pVirtualAddress =
  1783. PTR_JPGC->info.outChroma.virtualAddress;
  1784. ASSERT(pDecOut->outputPictureCbCr.pVirtualAddress);
  1785. pDecOut->outputPictureCbCr.busAddress =
  1786. PTR_JPGC->info.outChroma.busAddress;
  1787. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  1788. pDecOut->outputPictureCr.pVirtualAddress =
  1789. PTR_JPGC->info.outChroma2.virtualAddress;
  1790. pDecOut->outputPictureCr.busAddress =
  1791. PTR_JPGC->info.outChroma2.busAddress;
  1792. }
  1793. }
  1794. JpegDecInitHWEmptyScan(PTR_JPGC, PTR_JPGC->info.pfCompId);
  1795. dwlret = DWLWaitHwReady(PTR_JPGC->dwl, PTR_JPGC->info.timeout);
  1796. ASSERT(dwlret == DWL_HW_WAIT_OK);
  1797. JpegRefreshRegs(PTR_JPGC);
  1798. asic_status = GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ_STAT);
  1799. ASSERT(asic_status == 1);
  1800. i = PTR_JPGC->info.pfCompId + 1;
  1801. while(i < 3 && PTR_JPGC->info.pfNeeded[i] == 0)
  1802. i++;
  1803. if(i == 3)
  1804. {
  1805. PTR_JPGC->info.progressiveFinish = 0;
  1806. return (JPEGDEC_FRAME_READY);
  1807. }
  1808. else
  1809. {
  1810. PTR_JPGC->info.pfCompId = i;
  1811. return (JPEGDEC_SCAN_PROCESSED);
  1812. }
  1813. }
  1814. /* check if input streaming used */
  1815. if(!PTR_JPGC->info.SliceReadyForPause &&
  1816. !PTR_JPGC->info.inputBufferEmpty && pDecIn->bufferSize)
  1817. {
  1818. PTR_JPGC->info.inputStreaming = 1;
  1819. PTR_JPGC->info.inputBufferLen = pDecIn->bufferSize;
  1820. PTR_JPGC->info.decodedStreamLen += PTR_JPGC->info.inputBufferLen;
  1821. }
  1822. /* find markers and go ! */
  1823. do
  1824. {
  1825. /* if slice mode/slice done return to hw handling */
  1826. if(PTR_JPGC->image.headerReady && PTR_JPGC->info.SliceReadyForPause)
  1827. break;
  1828. /* Look for marker prefix byte from stream */
  1829. if(JpegDecGetByte(&(PTR_JPGC->stream)) == 0xFF)
  1830. {
  1831. currentByte = JpegDecGetByte(&(PTR_JPGC->stream));
  1832. /* switch to certain header decoding */
  1833. switch (currentByte)
  1834. {
  1835. case 0x00:
  1836. break;
  1837. case SOF0:
  1838. case SOF2:
  1839. /* Baseline/Progressive */
  1840. PTR_JPGC->frame.codingType = currentByte;
  1841. /* Set operation type */
  1842. if(PTR_JPGC->frame.codingType == SOF0)
  1843. PTR_JPGC->info.operationType = JPEGDEC_BASELINE;
  1844. else
  1845. PTR_JPGC->info.operationType = JPEGDEC_PROGRESSIVE;
  1846. retCode = JpegDecDecodeFrameHdr(PTR_JPGC);
  1847. if(retCode != JPEGDEC_OK)
  1848. {
  1849. if(retCode == JPEGDEC_STRM_ERROR)
  1850. {
  1851. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  1852. return (retCode);
  1853. }
  1854. else
  1855. {
  1856. JPEGDEC_API_TRC
  1857. ("JpegDecDecode# ERROR: JpegDecDecodeFrameHdr err");
  1858. return (retCode);
  1859. }
  1860. }
  1861. break;
  1862. /* Start of Scan */
  1863. case SOS:
  1864. /* reset image ready */
  1865. PTR_JPGC->image.imageReady = 0;
  1866. JPEGDEC_API_TRC("JpegDecDecode# JpegDecDecodeScan dec");
  1867. retCode = JpegDecDecodeScan(PTR_JPGC);
  1868. PTR_JPGC->image.headerReady = 1;
  1869. if(retCode != JPEGDEC_OK)
  1870. {
  1871. if(retCode == JPEGDEC_STRM_ERROR)
  1872. {
  1873. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  1874. return (retCode);
  1875. }
  1876. else
  1877. {
  1878. JPEGDEC_API_TRC
  1879. ("JpegDecDecode# JpegDecDecodeScan err\n");
  1880. return (retCode);
  1881. }
  1882. }
  1883. if(PTR_JPGC->stream.bitPosInByte)
  1884. {
  1885. /* delete stuffing bits */
  1886. currentByte = (8 - PTR_JPGC->stream.bitPosInByte);
  1887. if(JpegDecFlushBits
  1888. (&(PTR_JPGC->stream),
  1889. 8 - PTR_JPGC->stream.bitPosInByte) == STRM_ERROR)
  1890. {
  1891. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  1892. return (JPEGDEC_STRM_ERROR);
  1893. }
  1894. }
  1895. JPEGDEC_API_TRC("JpegDecDecode# Stuffing bits deleted\n");
  1896. break;
  1897. /* Start of Huffman tables */
  1898. case DHT:
  1899. JPEGDEC_API_TRC
  1900. ("JpegDecDecode# JpegDecDecodeHuffmanTables dec");
  1901. retCode = JpegDecDecodeHuffmanTables(PTR_JPGC);
  1902. JPEGDEC_API_TRC
  1903. ("JpegDecDecode# JpegDecDecodeHuffmanTables stops");
  1904. if(retCode != JPEGDEC_OK)
  1905. {
  1906. if(retCode == JPEGDEC_STRM_ERROR)
  1907. {
  1908. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  1909. return (retCode);
  1910. }
  1911. else
  1912. {
  1913. JPEGDEC_API_TRC
  1914. ("JpegDecDecode# ERROR: JpegDecDecodeHuffmanTables err");
  1915. return (retCode);
  1916. }
  1917. }
  1918. break;
  1919. /* start of Quantisation Tables */
  1920. case DQT:
  1921. JPEGDEC_API_TRC("JpegDecDecode# JpegDecDecodeQuantTables dec");
  1922. retCode = JpegDecDecodeQuantTables(PTR_JPGC);
  1923. if(retCode != JPEGDEC_OK)
  1924. {
  1925. if(retCode == JPEGDEC_STRM_ERROR)
  1926. {
  1927. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  1928. return (retCode);
  1929. }
  1930. else
  1931. {
  1932. JPEGDEC_API_TRC
  1933. ("JpegDecDecode# ERROR: JpegDecDecodeQuantTables err");
  1934. return (retCode);
  1935. }
  1936. }
  1937. break;
  1938. /* Start of Image */
  1939. case SOI:
  1940. /* no actions needed, continue */
  1941. break;
  1942. /* End of Image */
  1943. case EOI:
  1944. if(PTR_JPGC->image.imageReady)
  1945. {
  1946. JPEGDEC_API_TRC("JpegDecDecode# EOI: OK\n");
  1947. return (JPEGDEC_FRAME_READY);
  1948. }
  1949. else
  1950. {
  1951. JPEGDEC_API_TRC("JpegDecDecode# ERROR: EOI: NOK\n");
  1952. return (JPEGDEC_ERROR);
  1953. }
  1954. /* Define Restart Interval */
  1955. case DRI:
  1956. JPEGDEC_API_TRC("JpegDecDecode# DRI");
  1957. currentBytes = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  1958. if(currentBytes == STRM_ERROR)
  1959. {
  1960. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Read bits ");
  1961. return (JPEGDEC_STRM_ERROR);
  1962. }
  1963. PTR_JPGC->frame.Ri = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  1964. break;
  1965. /* Restart with modulo 8 count m */
  1966. case RST0:
  1967. case RST1:
  1968. case RST2:
  1969. case RST3:
  1970. case RST4:
  1971. case RST5:
  1972. case RST6:
  1973. case RST7:
  1974. /* initialisation of DC predictors to zero value !!! */
  1975. for(i = 0; i < MAX_NUMBER_OF_COMPONENTS; i++)
  1976. {
  1977. PTR_JPGC->scan.pred[i] = 0;
  1978. }
  1979. JPEGDEC_API_TRC("JpegDecDecode# DC predictors init");
  1980. break;
  1981. /* unsupported features */
  1982. case DNL:
  1983. case SOF1:
  1984. case SOF3:
  1985. case SOF5:
  1986. case SOF6:
  1987. case SOF7:
  1988. case SOF9:
  1989. case SOF10:
  1990. case SOF11:
  1991. case SOF13:
  1992. case SOF14:
  1993. case SOF15:
  1994. case DAC:
  1995. case DHP:
  1996. case TEM:
  1997. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Unsupported Features");
  1998. return (JPEGDEC_UNSUPPORTED);
  1999. /* application data & comments */
  2000. case APP0:
  2001. JPEGDEC_API_TRC("JpegDecDecode# APP0 in Decode");
  2002. /* APP0 Extended Thumbnail */
  2003. if(pDecIn->decImageType == JPEGDEC_THUMBNAIL)
  2004. {
  2005. /* reset */
  2006. appBits = 0;
  2007. appLength = 0;
  2008. /* length */
  2009. appLength = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  2010. appBits += 16;
  2011. /* check identifier */
  2012. currentBytes = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  2013. appBits += 16;
  2014. if(currentBytes != 0x4A46)
  2015. {
  2016. PTR_JPGC->stream.appnFlag = 1;
  2017. if(JpegDecFlushBits
  2018. (&(PTR_JPGC->stream),
  2019. ((appLength * 8) - appBits)) == STRM_ERROR)
  2020. {
  2021. JPEGDEC_API_TRC
  2022. ("JpegDecDecode# ERROR: Stream error");
  2023. return (JPEGDEC_STRM_ERROR);
  2024. }
  2025. PTR_JPGC->stream.appnFlag = 0;
  2026. break;
  2027. }
  2028. currentBytes = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  2029. appBits += 16;
  2030. if(currentBytes != 0x5858)
  2031. {
  2032. PTR_JPGC->stream.appnFlag = 1;
  2033. if(JpegDecFlushBits
  2034. (&(PTR_JPGC->stream),
  2035. ((appLength * 8) - appBits)) == STRM_ERROR)
  2036. {
  2037. JPEGDEC_API_TRC
  2038. ("JpegDecDecode# ERROR: Stream error");
  2039. return (JPEGDEC_STRM_ERROR);
  2040. }
  2041. PTR_JPGC->stream.appnFlag = 0;
  2042. break;
  2043. }
  2044. currentByte = JpegDecGetByte(&(PTR_JPGC->stream));
  2045. appBits += 8;
  2046. if(currentByte != 0x00)
  2047. {
  2048. PTR_JPGC->stream.appnFlag = 1;
  2049. if(JpegDecFlushBits
  2050. (&(PTR_JPGC->stream),
  2051. ((appLength * 8) - appBits)) == STRM_ERROR)
  2052. {
  2053. JPEGDEC_API_TRC
  2054. ("JpegDecDecode# ERROR: Stream error");
  2055. return (JPEGDEC_STRM_ERROR);
  2056. }
  2057. PTR_JPGC->stream.appnFlag = 0;
  2058. break;
  2059. }
  2060. /* extension code */
  2061. currentByte = JpegDecGetByte(&(PTR_JPGC->stream));
  2062. PTR_JPGC->stream.appnFlag = 0;
  2063. if(currentByte != JPEGDEC_THUMBNAIL_JPEG)
  2064. {
  2065. JPEGDEC_API_TRC(("JpegDecDecode# ERROR: thumbnail unsupported"));
  2066. return (JPEGDEC_UNSUPPORTED);
  2067. }
  2068. /* thumbnail mode */
  2069. JPEGDEC_API_TRC("JpegDecDecode# Thumbnail data ok!");
  2070. PTR_JPGC->stream.thumbnail = 1;
  2071. break;
  2072. }
  2073. else
  2074. {
  2075. /* Flush unsupported thumbnail */
  2076. currentBytes = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  2077. PTR_JPGC->stream.appnFlag = 1;
  2078. if(JpegDecFlushBits
  2079. (&(PTR_JPGC->stream),
  2080. ((currentBytes - 2) * 8)) == STRM_ERROR)
  2081. {
  2082. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Stream error");
  2083. return (JPEGDEC_STRM_ERROR);
  2084. }
  2085. PTR_JPGC->stream.appnFlag = 0;
  2086. break;
  2087. }
  2088. case APP1:
  2089. case APP2:
  2090. case APP3:
  2091. case APP4:
  2092. case APP5:
  2093. case APP6:
  2094. case APP7:
  2095. case APP8:
  2096. case APP9:
  2097. case APP10:
  2098. case APP11:
  2099. case APP12:
  2100. case APP13:
  2101. case APP14:
  2102. case APP15:
  2103. case COM:
  2104. JPEGDEC_API_TRC("JpegDecDecode# COM");
  2105. currentBytes = JpegDecGet2Bytes(&(PTR_JPGC->stream));
  2106. if(currentBytes == STRM_ERROR)
  2107. {
  2108. JPEGDEC_API_TRC("JpegDecDecode# ERROR: Read bits ");
  2109. return (JPEGDEC_STRM_ERROR);
  2110. }
  2111. /* jump over not supported header */
  2112. if(currentBytes != 0)
  2113. {
  2114. PTR_JPGC->stream.readBits += ((currentBytes * 8) - 16);
  2115. PTR_JPGC->stream.pCurrPos +=
  2116. (((currentBytes * 8) - 16) / 8);
  2117. }
  2118. break;
  2119. default:
  2120. break;
  2121. }
  2122. }
  2123. else
  2124. {
  2125. if(currentByte == 0xFFFFFFFF)
  2126. {
  2127. break;
  2128. }
  2129. }
  2130. if(PTR_JPGC->image.headerReady)
  2131. break;
  2132. }
  2133. while((PTR_JPGC->stream.readBits >> 3) <= PTR_JPGC->stream.streamLength);
  2134. retCode = JPEGDEC_OK;
  2135. /* Handle decoded image here */
  2136. if(PTR_JPGC->image.headerReady)
  2137. {
  2138. /* loop until decoding control should return for user */
  2139. do
  2140. {
  2141. /* if pp enabled ==> set pp control */
  2142. if(PTR_JPGC->ppInstance != NULL)
  2143. {
  2144. PTR_JPGC->ppConfigQuery.tiledMode = 0;
  2145. PTR_JPGC->PPConfigQuery(PTR_JPGC->ppInstance,
  2146. &PTR_JPGC->ppConfigQuery);
  2147. PTR_JPGC->ppControl.usePipeline =
  2148. PTR_JPGC->ppConfigQuery.pipelineAccepted;
  2149. /* set pp for combined mode */
  2150. if(PTR_JPGC->ppControl.usePipeline)
  2151. JpegDecPreparePp(PTR_JPGC);
  2152. }
  2153. /* check if we had to load imput buffer or not */
  2154. if(!PTR_JPGC->info.inputBufferEmpty)
  2155. {
  2156. /* if slice mode ==> set slice height */
  2157. if(PTR_JPGC->info.sliceMbSetValue &&
  2158. PTR_JPGC->ppControl.usePipeline == 0)
  2159. {
  2160. JpegDecSliceSizeCalculation(PTR_JPGC);
  2161. }
  2162. /* Start HW or continue after pause */
  2163. if(!PTR_JPGC->info.SliceReadyForPause)
  2164. {
  2165. if(!PTR_JPGC->info.progressiveScanReady ||
  2166. PTR_JPGC->info.nonInterleavedScanReady)
  2167. {
  2168. JPEGDEC_API_TRC("JpegDecDecode# Start HW init\n");
  2169. retCode = JpegDecInitHW(PTR_JPGC);
  2170. PTR_JPGC->info.nonInterleavedScanReady = 0;
  2171. if(retCode != JPEGDEC_OK)
  2172. {
  2173. /* return JPEGDEC_HW_RESERVED */
  2174. return retCode;
  2175. }
  2176. }
  2177. else
  2178. {
  2179. JPEGDEC_API_TRC
  2180. ("JpegDecDecode# Continue HW decoding after progressive scan ready\n");
  2181. JpegDecInitHWProgressiveContinue(PTR_JPGC);
  2182. PTR_JPGC->info.progressiveScanReady = 0;
  2183. }
  2184. }
  2185. else
  2186. {
  2187. JPEGDEC_API_TRC
  2188. ("JpegDecDecode# Continue HW decoding after slice ready\n");
  2189. JpegDecInitHWContinue(PTR_JPGC);
  2190. }
  2191. PTR_JPGC->info.SliceCount++;
  2192. }
  2193. else
  2194. {
  2195. JPEGDEC_API_TRC
  2196. ("JpegDecDecode# Continue HW decoding after input buffer has been loaded\n");
  2197. JpegDecInitHWInputBuffLoad(PTR_JPGC);
  2198. /* buffer loaded ==> reset flag */
  2199. PTR_JPGC->info.inputBufferEmpty = 0;
  2200. }
  2201. #ifdef JPEGDEC_PERFORMANCE
  2202. dwlret = DWL_HW_WAIT_OK;
  2203. #else
  2204. /* wait hw ready */
  2205. dwlret = DWLWaitHwReady(PTR_JPGC->dwl, PTR_JPGC->info.timeout);
  2206. #endif /* #ifdef JPEGDEC_PERFORMANCE */
  2207. /* Refresh regs */
  2208. JpegRefreshRegs(PTR_JPGC);
  2209. if(dwlret == DWL_HW_WAIT_OK)
  2210. {
  2211. JPEGDEC_API_TRC("JpegDecDecode# DWL_HW_WAIT_OK");
  2212. #ifdef JPEGDEC_ASIC_TRACE
  2213. {
  2214. JPEGDEC_TRACE_INTERNAL(("\nJpegDecDecode# AFTER DWL_HW_WAIT_OK\n"));
  2215. PrintJPEGReg(PTR_JPGC->jpegRegs);
  2216. }
  2217. #endif /* #ifdef JPEGDEC_ASIC_TRACE */
  2218. /* check && reset status */
  2219. asic_status =
  2220. GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ_STAT);
  2221. if(asic_status & JPEGDEC_X170_IRQ_BUS_ERROR)
  2222. {
  2223. /* check PP status... and go */
  2224. if(PTR_JPGC->ppInstance != NULL)
  2225. {
  2226. /* End PP co-operation */
  2227. if(PTR_JPGC->ppControl.ppStatus == DECPP_RUNNING)
  2228. {
  2229. JPEGDEC_API_TRC("JpegDecDecode# PP END CALL");
  2230. PTR_JPGC->PPEndCallback(PTR_JPGC->ppInstance);
  2231. PTR_JPGC->ppControl.ppStatus = DECPP_PIC_READY;
  2232. }
  2233. }
  2234. JPEGDEC_API_TRC
  2235. ("JpegDecDecode# JPEGDEC_X170_IRQ_BUS_ERROR");
  2236. /* clear interrupts */
  2237. JPEGDEC_CLEAR_IRQ;
  2238. DWLDisableHW(PTR_JPGC->dwl, 4 * 1, PTR_JPGC->jpegRegs[1]);
  2239. /* Release HW */
  2240. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2241. /* update asicRunning */
  2242. PTR_JPGC->asicRunning = 0;
  2243. return JPEGDEC_HW_BUS_ERROR;
  2244. }
  2245. else if(asic_status & JPEGDEC_X170_IRQ_STREAM_ERROR ||
  2246. asic_status & JPEGDEC_X170_IRQ_TIMEOUT)
  2247. {
  2248. /* check PP status... and go */
  2249. if(PTR_JPGC->ppInstance != NULL)
  2250. {
  2251. /* End PP co-operation */
  2252. if(PTR_JPGC->ppControl.ppStatus == DECPP_RUNNING)
  2253. {
  2254. JPEGDEC_API_TRC("JpegDecDecode# PP END CALL");
  2255. PTR_JPGC->PPEndCallback(PTR_JPGC->ppInstance);
  2256. }
  2257. PTR_JPGC->ppControl.ppStatus = DECPP_PIC_READY;
  2258. }
  2259. if(asic_status & JPEGDEC_X170_IRQ_STREAM_ERROR)
  2260. JPEGDEC_API_TRC
  2261. ("JpegDecDecode# JPEGDEC_X170_IRQ_STREAM_ERROR");
  2262. else
  2263. JPEGDEC_API_TRC
  2264. ("JpegDecDecode# JPEGDEC_X170_IRQ_TIMEOUT");
  2265. /* clear interrupts */
  2266. JPEGDEC_CLEAR_IRQ;
  2267. DWLDisableHW(PTR_JPGC->dwl, 4 * 1, PTR_JPGC->jpegRegs[1]);
  2268. /* Release HW */
  2269. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2270. /* update asicRunning */
  2271. PTR_JPGC->asicRunning = 0;
  2272. /* output set */
  2273. if(PTR_JPGC->ppInstance == NULL)
  2274. {
  2275. pDecOut->outputPictureY.pVirtualAddress =
  2276. PTR_JPGC->info.outLuma.virtualAddress;
  2277. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2278. /* output set */
  2279. pDecOut->outputPictureY.busAddress =
  2280. PTR_JPGC->info.outLuma.busAddress;
  2281. ASSERT(pDecOut->outputPictureY.busAddress);
  2282. /* if not grayscale */
  2283. if(PTR_JPGC->image.sizeChroma)
  2284. {
  2285. pDecOut->outputPictureCbCr.pVirtualAddress =
  2286. PTR_JPGC->info.outChroma.virtualAddress;
  2287. ASSERT(pDecOut->outputPictureCbCr.pVirtualAddress);
  2288. pDecOut->outputPictureCbCr.busAddress =
  2289. PTR_JPGC->info.outChroma.busAddress;
  2290. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2291. pDecOut->outputPictureCr.pVirtualAddress =
  2292. PTR_JPGC->info.outChroma2.virtualAddress;
  2293. pDecOut->outputPictureCr.busAddress =
  2294. PTR_JPGC->info.outChroma2.busAddress;
  2295. }
  2296. }
  2297. return JPEGDEC_STRM_ERROR;
  2298. }
  2299. else if(asic_status & JPEGDEC_X170_IRQ_BUFFER_EMPTY)
  2300. {
  2301. /* check if frame is ready */
  2302. if(!(asic_status & JPEGDEC_X170_IRQ_DEC_RDY))
  2303. {
  2304. JPEGDEC_API_TRC
  2305. ("JpegDecDecode# JPEGDEC_X170_IRQ_BUFFER_EMPTY/STREAM PROCESSED");
  2306. /* clear interrupts */
  2307. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_BUFFER_INT,
  2308. 0);
  2309. /* flag to load buffer */
  2310. PTR_JPGC->info.inputBufferEmpty = 1;
  2311. /* check if all stream should be processed with the
  2312. * next buffer ==> may affect to some API checks */
  2313. if((PTR_JPGC->info.decodedStreamLen +
  2314. PTR_JPGC->info.inputBufferLen) >=
  2315. PTR_JPGC->stream.streamLength)
  2316. {
  2317. PTR_JPGC->info.streamEndFlag = 1;
  2318. }
  2319. /* output set */
  2320. if(PTR_JPGC->ppInstance == NULL)
  2321. {
  2322. pDecOut->outputPictureY.pVirtualAddress =
  2323. PTR_JPGC->info.outLuma.virtualAddress;
  2324. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2325. /* output set */
  2326. pDecOut->outputPictureY.busAddress =
  2327. PTR_JPGC->info.outLuma.busAddress;
  2328. ASSERT(pDecOut->outputPictureY.busAddress);
  2329. /* if not grayscale */
  2330. if(PTR_JPGC->image.sizeChroma)
  2331. {
  2332. pDecOut->outputPictureCbCr.pVirtualAddress =
  2333. PTR_JPGC->info.outChroma.virtualAddress;
  2334. ASSERT(pDecOut->outputPictureCbCr.
  2335. pVirtualAddress);
  2336. pDecOut->outputPictureCbCr.busAddress =
  2337. PTR_JPGC->info.outChroma.busAddress;
  2338. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2339. pDecOut->outputPictureCr.pVirtualAddress =
  2340. PTR_JPGC->info.outChroma2.virtualAddress;
  2341. pDecOut->outputPictureCr.busAddress =
  2342. PTR_JPGC->info.outChroma2.busAddress;
  2343. }
  2344. }
  2345. return JPEGDEC_STRM_PROCESSED;
  2346. }
  2347. }
  2348. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_RDY_INT, 0);
  2349. HINTdec = GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ);
  2350. if(HINTdec)
  2351. {
  2352. JPEGDEC_API_TRC("JpegDecDecode# CLEAR interrupt");
  2353. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ, 0);
  2354. }
  2355. /* check if slice ready */
  2356. asicSliceBit = GetDecRegister(PTR_JPGC->jpegRegs,
  2357. HWIF_JPEG_SLICE_H);
  2358. intDec = GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_SLICE_INT);
  2359. /* slice ready ==> reset interrupt */
  2360. if(asicSliceBit && intDec)
  2361. {
  2362. /* if x170 pp not in use */
  2363. if(PTR_JPGC->ppInstance == NULL)
  2364. PTR_JPGC->info.SliceReadyForPause = 1;
  2365. else
  2366. PTR_JPGC->info.SliceReadyForPause = 0;
  2367. if(PTR_JPGC->ppInstance != NULL &&
  2368. !PTR_JPGC->ppControl.usePipeline)
  2369. {
  2370. PTR_JPGC->info.SliceReadyForPause = 1;
  2371. }
  2372. /* if user allocated memory return given base */
  2373. if(PTR_JPGC->info.userAllocMem == 1 &&
  2374. PTR_JPGC->info.SliceReadyForPause == 1)
  2375. {
  2376. /* output addresses */
  2377. pDecOut->outputPictureY.pVirtualAddress =
  2378. PTR_JPGC->info.givenOutLuma.virtualAddress;
  2379. pDecOut->outputPictureY.busAddress =
  2380. PTR_JPGC->info.givenOutLuma.busAddress;
  2381. if(PTR_JPGC->image.sizeChroma)
  2382. {
  2383. pDecOut->outputPictureCbCr.pVirtualAddress =
  2384. PTR_JPGC->info.givenOutChroma.virtualAddress;
  2385. pDecOut->outputPictureCbCr.busAddress =
  2386. PTR_JPGC->info.givenOutChroma.busAddress;
  2387. pDecOut->outputPictureCr.pVirtualAddress =
  2388. PTR_JPGC->info.givenOutChroma2.virtualAddress;
  2389. pDecOut->outputPictureCr.busAddress =
  2390. PTR_JPGC->info.givenOutChroma2.busAddress;
  2391. }
  2392. }
  2393. /* if not user allocated memory return slice base */
  2394. if(PTR_JPGC->info.userAllocMem == 0 &&
  2395. PTR_JPGC->info.SliceReadyForPause == 1)
  2396. {
  2397. /* output addresses */
  2398. pDecOut->outputPictureY.pVirtualAddress =
  2399. PTR_JPGC->info.outLuma.virtualAddress;
  2400. pDecOut->outputPictureY.busAddress =
  2401. PTR_JPGC->info.outLuma.busAddress;
  2402. if(PTR_JPGC->image.sizeChroma)
  2403. {
  2404. pDecOut->outputPictureCbCr.pVirtualAddress =
  2405. PTR_JPGC->info.outChroma.virtualAddress;
  2406. pDecOut->outputPictureCbCr.busAddress =
  2407. PTR_JPGC->info.outChroma.busAddress;
  2408. pDecOut->outputPictureCr.pVirtualAddress =
  2409. PTR_JPGC->info.outChroma2.virtualAddress;
  2410. pDecOut->outputPictureCr.busAddress =
  2411. PTR_JPGC->info.outChroma2.busAddress;
  2412. }
  2413. }
  2414. /* No slice output in case decoder + PP (no pipeline) */
  2415. if(PTR_JPGC->ppInstance != NULL &&
  2416. PTR_JPGC->ppControl.usePipeline == 0)
  2417. {
  2418. /* output addresses */
  2419. pDecOut->outputPictureY.pVirtualAddress = NULL;
  2420. pDecOut->outputPictureY.busAddress = 0;
  2421. if(PTR_JPGC->image.sizeChroma)
  2422. {
  2423. pDecOut->outputPictureCbCr.pVirtualAddress = NULL;
  2424. pDecOut->outputPictureCbCr.busAddress = 0;
  2425. pDecOut->outputPictureCr.pVirtualAddress = NULL;
  2426. pDecOut->outputPictureCr.busAddress = 0;
  2427. }
  2428. JPEGDEC_API_TRC(("JpegDecDecode# Decoder + PP (Rotation/Flip), Slice ready"));
  2429. /* PP not in pipeline, continue do <==> while */
  2430. PTR_JPGC->info.noSliceIrqForUser = 1;
  2431. }
  2432. else
  2433. {
  2434. JPEGDEC_API_TRC(("JpegDecDecode# Slice ready"));
  2435. return JPEGDEC_SLICE_READY;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. if(PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE ||
  2441. PTR_JPGC->info.operationType == JPEGDEC_NONINTERLEAVED)
  2442. {
  2443. currentPos =
  2444. GetDecRegister(PTR_JPGC->jpegRegs,
  2445. HWIF_RLC_VLC_BASE);
  2446. /* update input buffer address */
  2447. PTR_JPGC->stream.pCurrPos = ((u8 *) currentPos - 10);
  2448. PTR_JPGC->stream.bitPosInByte = 0;
  2449. PTR_JPGC->stream.readBits =
  2450. ((PTR_JPGC->stream.pCurrPos -
  2451. PTR_JPGC->stream.pStartOfStream) * 8);
  2452. /* default if data ends */
  2453. endOfImage = 1;
  2454. /* check if last scan is decoded */
  2455. for(i = 0;
  2456. i <
  2457. ((PTR_JPGC->stream.streamLength -
  2458. (PTR_JPGC->stream.readBits / 8))); i++)
  2459. {
  2460. currentByte = PTR_JPGC->stream.pCurrPos[i];
  2461. if(currentByte == 0xFF)
  2462. {
  2463. currentByte = PTR_JPGC->stream.pCurrPos[i + 1];
  2464. if(currentByte == 0xD9)
  2465. {
  2466. endOfImage = 1;
  2467. break;
  2468. }
  2469. else if(currentByte == 0xC4 ||
  2470. currentByte == 0xDA)
  2471. {
  2472. endOfImage = 0;
  2473. break;
  2474. }
  2475. }
  2476. }
  2477. currentByte = 0;
  2478. PTR_JPGC->info.SliceCount = 0;
  2479. PTR_JPGC->info.SliceReadyForPause = 0;
  2480. /* if not the last scan of the stream */
  2481. if(endOfImage == 0)
  2482. {
  2483. /* output set */
  2484. if(PTR_JPGC->ppInstance == NULL &&
  2485. !PTR_JPGC->info.noSliceIrqForUser)
  2486. {
  2487. pDecOut->outputPictureY.pVirtualAddress =
  2488. PTR_JPGC->info.outLuma.virtualAddress;
  2489. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2490. /* output set */
  2491. pDecOut->outputPictureY.busAddress =
  2492. PTR_JPGC->info.outLuma.busAddress;
  2493. ASSERT(pDecOut->outputPictureY.busAddress);
  2494. /* if not grayscale */
  2495. if(PTR_JPGC->image.sizeChroma)
  2496. {
  2497. pDecOut->outputPictureCbCr.pVirtualAddress =
  2498. PTR_JPGC->info.outChroma.virtualAddress;
  2499. ASSERT(pDecOut->outputPictureCbCr.
  2500. pVirtualAddress);
  2501. pDecOut->outputPictureCbCr.busAddress =
  2502. PTR_JPGC->info.outChroma.busAddress;
  2503. ASSERT(pDecOut->outputPictureCbCr.
  2504. busAddress);
  2505. pDecOut->outputPictureCr.pVirtualAddress =
  2506. PTR_JPGC->info.outChroma2.
  2507. virtualAddress;
  2508. pDecOut->outputPictureCr.busAddress =
  2509. PTR_JPGC->info.outChroma2.busAddress;
  2510. }
  2511. }
  2512. /* PP not in pipeline, continue do <==> while */
  2513. PTR_JPGC->info.noSliceIrqForUser = 0;
  2514. if(PTR_JPGC->info.operationType ==
  2515. JPEGDEC_PROGRESSIVE)
  2516. PTR_JPGC->info.progressiveScanReady = 1;
  2517. else
  2518. PTR_JPGC->info.nonInterleavedScanReady = 1;
  2519. /* return control to application if progressive */
  2520. if(PTR_JPGC->info.operationType !=
  2521. JPEGDEC_NONINTERLEAVED)
  2522. {
  2523. /* non-interleaved scan ==> no output */
  2524. if(PTR_JPGC->info.nonInterleaved == 0)
  2525. PTR_JPGC->info.noSliceIrqForUser = 1;
  2526. else
  2527. {
  2528. JPEGDEC_API_TRC
  2529. ("JpegDecDecode# SCAN PROCESSED");
  2530. return (JPEGDEC_SCAN_PROCESSED);
  2531. }
  2532. }
  2533. else
  2534. {
  2535. /* set decoded component */
  2536. PTR_JPGC->info.components[PTR_JPGC->info.
  2537. componentId] = 1;
  2538. /* check if we have decoded all components */
  2539. if(PTR_JPGC->info.components[0] == 1 &&
  2540. PTR_JPGC->info.components[1] == 1 &&
  2541. PTR_JPGC->info.components[2] == 1)
  2542. {
  2543. /* continue decoding next scan */
  2544. PTR_JPGC->info.noSliceIrqForUser = 0;
  2545. nonInterleavedRdy = 0;
  2546. }
  2547. else
  2548. {
  2549. /* continue decoding next scan */
  2550. PTR_JPGC->info.noSliceIrqForUser = 1;
  2551. nonInterleavedRdy = 0;
  2552. }
  2553. }
  2554. }
  2555. else
  2556. {
  2557. if(PTR_JPGC->info.operationType ==
  2558. JPEGDEC_NONINTERLEAVED)
  2559. {
  2560. /* set decoded component */
  2561. PTR_JPGC->info.components[PTR_JPGC->info.
  2562. componentId] = 1;
  2563. /* check if we have decoded all components */
  2564. if(PTR_JPGC->info.components[0] == 1 &&
  2565. PTR_JPGC->info.components[1] == 1 &&
  2566. PTR_JPGC->info.components[2] == 1)
  2567. {
  2568. /* continue decoding next scan */
  2569. PTR_JPGC->info.noSliceIrqForUser = 0;
  2570. nonInterleavedRdy = 1;
  2571. }
  2572. else
  2573. {
  2574. /* continue decoding next scan */
  2575. PTR_JPGC->info.noSliceIrqForUser = 1;
  2576. nonInterleavedRdy = 0;
  2577. }
  2578. }
  2579. }
  2580. }
  2581. else
  2582. {
  2583. /* PP not in pipeline, continue do <==> while */
  2584. PTR_JPGC->info.noSliceIrqForUser = 0;
  2585. }
  2586. }
  2587. /* check PP status... and go */
  2588. if(PTR_JPGC->ppInstance != NULL &&
  2589. !PTR_JPGC->info.noSliceIrqForUser)
  2590. {
  2591. /* set pp for stand alone */
  2592. if(!PTR_JPGC->ppControl.usePipeline)
  2593. {
  2594. JpegDecPreparePp(PTR_JPGC);
  2595. JPEGDEC_TRACE_INTERNAL(("INTERNAL: Set output write disabled\n"));
  2596. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_OUT_DIS, 1);
  2597. /* Enable pp */
  2598. JPEGDEC_TRACE_INTERNAL(("INTERNAL: Set Enable pp\n"));
  2599. PTR_JPGC->PPRun(PTR_JPGC->ppInstance,
  2600. &PTR_JPGC->ppControl);
  2601. PTR_JPGC->ppControl.ppStatus = DECPP_RUNNING;
  2602. PTR_JPGC->info.pipeline = 1;
  2603. /* Flush regs to hw register */
  2604. JpegFlushRegs(PTR_JPGC);
  2605. }
  2606. /* End PP co-operation */
  2607. if(PTR_JPGC->ppControl.ppStatus == DECPP_RUNNING)
  2608. {
  2609. JPEGDEC_API_TRC("JpegDecDecode# PP END CALL");
  2610. PTR_JPGC->PPEndCallback(PTR_JPGC->ppInstance);
  2611. }
  2612. PTR_JPGC->ppControl.ppStatus = DECPP_PIC_READY;
  2613. }
  2614. /* output set */
  2615. if(PTR_JPGC->ppInstance == NULL &&
  2616. !PTR_JPGC->info.noSliceIrqForUser)
  2617. {
  2618. pDecOut->outputPictureY.pVirtualAddress =
  2619. PTR_JPGC->info.outLuma.virtualAddress;
  2620. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2621. /* output set */
  2622. pDecOut->outputPictureY.busAddress =
  2623. PTR_JPGC->info.outLuma.busAddress;
  2624. ASSERT(pDecOut->outputPictureY.busAddress);
  2625. /* if not grayscale */
  2626. if(PTR_JPGC->image.sizeChroma)
  2627. {
  2628. pDecOut->outputPictureCbCr.pVirtualAddress =
  2629. PTR_JPGC->info.outChroma.virtualAddress;
  2630. ASSERT(pDecOut->outputPictureCbCr.pVirtualAddress);
  2631. pDecOut->outputPictureCbCr.busAddress =
  2632. PTR_JPGC->info.outChroma.busAddress;
  2633. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2634. pDecOut->outputPictureCr.pVirtualAddress =
  2635. PTR_JPGC->info.outChroma2.virtualAddress;
  2636. pDecOut->outputPictureCr.busAddress =
  2637. PTR_JPGC->info.outChroma2.busAddress;
  2638. }
  2639. }
  2640. #ifdef JPEGDEC_ASIC_TRACE
  2641. {
  2642. JPEGDEC_TRACE_INTERNAL(("\nJpegDecDecode# TEST\n"));
  2643. PrintJPEGReg(PTR_JPGC->jpegRegs);
  2644. }
  2645. #endif /* #ifdef JPEGDEC_ASIC_TRACE */
  2646. if(PTR_JPGC->info.noSliceIrqForUser == 0)
  2647. {
  2648. /* Release HW */
  2649. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2650. /* update asicRunning */
  2651. PTR_JPGC->asicRunning = 0;
  2652. JPEGDEC_API_TRC("JpegDecDecode# FRAME READY");
  2653. /* set image ready */
  2654. PTR_JPGC->image.imageReady = 1;
  2655. PTR_JPGC->info.noSliceIrqForUser = 0;
  2656. }
  2657. /* get the current stream address */
  2658. if(PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE ||
  2659. (PTR_JPGC->info.operationType == JPEGDEC_NONINTERLEAVED &&
  2660. nonInterleavedRdy == 0))
  2661. {
  2662. retCode = JpegDecNextScanHdrs(PTR_JPGC);
  2663. if(retCode != JPEGDEC_OK && retCode != JPEGDEC_FRAME_READY)
  2664. {
  2665. /* return */
  2666. return retCode;
  2667. }
  2668. }
  2669. }
  2670. else if(dwlret == DWL_HW_WAIT_TIMEOUT)
  2671. {
  2672. JPEGDEC_API_TRC("SCAN: DWL HW TIMEOUT\n");
  2673. /* Release HW */
  2674. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2675. /* update asicRunning */
  2676. PTR_JPGC->asicRunning = 0;
  2677. return (JPEGDEC_DWL_HW_TIMEOUT);
  2678. }
  2679. else if(dwlret == DWL_HW_WAIT_ERROR)
  2680. {
  2681. JPEGDEC_API_TRC("SCAN: DWL HW ERROR\n");
  2682. /* Release HW */
  2683. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2684. /* update asicRunning */
  2685. PTR_JPGC->asicRunning = 0;
  2686. return (JPEGDEC_SYSTEM_ERROR);
  2687. }
  2688. }
  2689. while(!PTR_JPGC->image.imageReady);
  2690. }
  2691. if(PTR_JPGC->image.imageReady)
  2692. {
  2693. JPEGDEC_API_TRC("JpegDecDecode# IMAGE READY");
  2694. JPEGDEC_API_TRC("JpegDecDecode# OK\n");
  2695. /* reset image status */
  2696. PTR_JPGC->image.imageReady = 0;
  2697. PTR_JPGC->image.headerReady = 0;
  2698. if(PTR_JPGC->info.operationType == JPEGDEC_BASELINE)
  2699. {
  2700. return (JPEGDEC_FRAME_READY);
  2701. }
  2702. else
  2703. {
  2704. if(endOfImage == 0)
  2705. return (JPEGDEC_SCAN_PROCESSED);
  2706. else
  2707. {
  2708. if(PTR_JPGC->frame.Nf != 1)
  2709. {
  2710. /* determine first component that needs to be cheated */
  2711. i = 0;
  2712. while(i < 3 && PTR_JPGC->info.pfNeeded[i] == 0)
  2713. i++;
  2714. if(i == 3)
  2715. return (JPEGDEC_FRAME_READY);
  2716. JpegDecInitHWEmptyScan(PTR_JPGC, i++);
  2717. dwlret = DWLWaitHwReady(PTR_JPGC->dwl,
  2718. PTR_JPGC->info.timeout);
  2719. ASSERT(dwlret == DWL_HW_WAIT_OK);
  2720. JpegRefreshRegs(PTR_JPGC);
  2721. asic_status =
  2722. GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ_STAT);
  2723. ASSERT(asic_status == 1);
  2724. while(i < 3 && PTR_JPGC->info.pfNeeded[i] == 0)
  2725. i++;
  2726. if(i == 3)
  2727. return (JPEGDEC_FRAME_READY);
  2728. else
  2729. {
  2730. PTR_JPGC->info.progressiveFinish = 1;
  2731. PTR_JPGC->info.pfCompId = i;
  2732. return (JPEGDEC_SCAN_PROCESSED);
  2733. }
  2734. }
  2735. else
  2736. return (JPEGDEC_FRAME_READY);
  2737. }
  2738. }
  2739. }
  2740. else
  2741. {
  2742. JPEGDEC_API_TRC("JpegDecDecode# ERROR\n");
  2743. return (JPEGDEC_ERROR);
  2744. }
  2745. #undef JPG_FRM
  2746. #undef PTR_JPGC
  2747. #undef PTR_INFO
  2748. }
  2749. /*------------------------------------------------------------------------------
  2750. Function name: JpegDecPreparePp
  2751. Functional description:
  2752. Setup PP interface
  2753. Input:
  2754. container
  2755. Return values:
  2756. void
  2757. ------------------------------------------------------------------------------*/
  2758. static void JpegDecPreparePp(JpegDecContainer * pJpegDecCont)
  2759. {
  2760. pJpegDecCont->ppControl.picStruct = 0;
  2761. pJpegDecCont->ppControl.topField = 0;
  2762. {
  2763. u32 tmp = GetDecRegister(pJpegDecCont->jpegRegs, HWIF_DEC_OUT_ENDIAN);
  2764. pJpegDecCont->ppControl.littleEndian =
  2765. (tmp == DEC_X170_LITTLE_ENDIAN) ? 1 : 0;
  2766. }
  2767. pJpegDecCont->ppControl.wordSwap = GetDecRegister(pJpegDecCont->jpegRegs,
  2768. HWIF_DEC_OUTSWAP32_E) ? 1
  2769. : 0;
  2770. /* pipeline */
  2771. if(pJpegDecCont->ppControl.usePipeline)
  2772. {
  2773. /* luma */
  2774. pJpegDecCont->ppControl.inputBusLuma = 0;
  2775. /* chroma */
  2776. pJpegDecCont->ppControl.inputBusChroma = 0;
  2777. }
  2778. else
  2779. {
  2780. /* luma */
  2781. pJpegDecCont->ppControl.inputBusLuma =
  2782. pJpegDecCont->asicBuff.outLumaBuffer.busAddress;
  2783. /* chroma */
  2784. pJpegDecCont->ppControl.inputBusChroma =
  2785. pJpegDecCont->asicBuff.outChromaBuffer.busAddress;
  2786. }
  2787. /* dimensions */
  2788. pJpegDecCont->ppControl.inwidth =
  2789. pJpegDecCont->ppControl.croppedW = pJpegDecCont->info.X;
  2790. pJpegDecCont->ppControl.inheight =
  2791. pJpegDecCont->ppControl.croppedH = pJpegDecCont->info.Y;
  2792. }
  2793. /*------------------------------------------------------------------------------
  2794. 5.6. Function name: JpegGetAPIVersion
  2795. Purpose: Returns version information about this API
  2796. Input: void
  2797. Output: JpegDecApiVersion
  2798. ------------------------------------------------------------------------------*/
  2799. JpegDecApiVersion JpegGetAPIVersion()
  2800. {
  2801. JpegDecApiVersion ver;
  2802. ver.major = JPG_MAJOR_VERSION;
  2803. ver.minor = JPG_MINOR_VERSION;
  2804. JPEGDEC_API_TRC("JpegGetAPIVersion# OK\n");
  2805. return ver;
  2806. }
  2807. /*------------------------------------------------------------------------------
  2808. 5.7. Function name: JpegDecGetBuild
  2809. Purpose: Returns the SW and HW build information
  2810. Input: void
  2811. Output: JpegDecGetBuild
  2812. ------------------------------------------------------------------------------*/
  2813. JpegDecBuild JpegDecGetBuild(void)
  2814. {
  2815. JpegDecBuild ver;
  2816. DWLHwConfig_t hwCfg;
  2817. DWLmemset(&ver, 0, sizeof(ver));
  2818. ver.swBuild = JPG_SW_BUILD;
  2819. ver.hwBuild = DWLReadAsicID();
  2820. DWLReadAsicConfig(&hwCfg);
  2821. SET_DEC_BUILD_SUPPORT(ver.hwConfig, hwCfg);
  2822. JPEGDEC_API_TRC("JpegDecGetBuild# OK\n");
  2823. return (ver);
  2824. }
  2825. /*------------------------------------------------------------------------------
  2826. 5.8. Function name : jpegRegisterPP
  2827. Description : Called internally by PP to enable the pipeline
  2828. Return type : i32 - return 0 for success or a negative error code
  2829. Argument : const void * decInst - decoder instance
  2830. Argument : const void *ppInst - post-processor instance
  2831. Argument : (*PPRun)(const void *) - decoder calls this to start PP
  2832. Argument : void (*PPEndCallback)(const void *) - decoder calls this
  2833. to notify PP that a picture was done.
  2834. ------------------------------------------------------------------------------*/
  2835. i32 jpegRegisterPP(const void *decInst, const void *ppInst,
  2836. void (*PPRun) (const void *, DecPpInterface *),
  2837. void (*PPEndCallback) (const void *),
  2838. void (*PPConfigQuery) (const void *, DecPpQuery *))
  2839. {
  2840. JpegDecContainer *pDecCont;
  2841. pDecCont = (JpegDecContainer *) decInst;
  2842. if(decInst == NULL || pDecCont->ppInstance != NULL ||
  2843. ppInst == NULL || PPRun == NULL || PPEndCallback == NULL)
  2844. return -1;
  2845. if(pDecCont->asicRunning)
  2846. return -2;
  2847. pDecCont->ppInstance = ppInst;
  2848. pDecCont->PPEndCallback = PPEndCallback;
  2849. pDecCont->PPRun = PPRun;
  2850. pDecCont->PPConfigQuery = PPConfigQuery;
  2851. return 0;
  2852. }
  2853. /*------------------------------------------------------------------------------
  2854. 5.9. Function name : jpegUnregisterPP
  2855. Description : Called internally by PP to disable the pipeline
  2856. Return type : i32 - return 0 for success or a negative error code
  2857. Argument : const void * decInst - decoder instance
  2858. Argument : const void *ppInst - post-processor instance
  2859. ------------------------------------------------------------------------------*/
  2860. i32 jpegUnregisterPP(const void *decInst, const void *ppInst)
  2861. {
  2862. JpegDecContainer *pDecCont;
  2863. pDecCont = (JpegDecContainer *) decInst;
  2864. ASSERT(decInst != NULL && ppInst == pDecCont->ppInstance);
  2865. if(decInst == NULL || ppInst != pDecCont->ppInstance)
  2866. return -1;
  2867. if(pDecCont->asicRunning)
  2868. return -2;
  2869. pDecCont->ppInstance = NULL;
  2870. pDecCont->PPEndCallback = NULL;
  2871. pDecCont->PPRun = NULL;
  2872. return 0;
  2873. }