jpegdecapi.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210
  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. {
  2261. JPEGDEC_API_TRC
  2262. ("JpegDecDecode# JPEGDEC_X170_IRQ_STREAM_ERROR");
  2263. }
  2264. else
  2265. {
  2266. JPEGDEC_API_TRC
  2267. ("JpegDecDecode# JPEGDEC_X170_IRQ_TIMEOUT");
  2268. }
  2269. /* clear interrupts */
  2270. JPEGDEC_CLEAR_IRQ;
  2271. DWLDisableHW(PTR_JPGC->dwl, 4 * 1, PTR_JPGC->jpegRegs[1]);
  2272. /* Release HW */
  2273. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2274. /* update asicRunning */
  2275. PTR_JPGC->asicRunning = 0;
  2276. /* output set */
  2277. if(PTR_JPGC->ppInstance == NULL)
  2278. {
  2279. pDecOut->outputPictureY.pVirtualAddress =
  2280. PTR_JPGC->info.outLuma.virtualAddress;
  2281. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2282. /* output set */
  2283. pDecOut->outputPictureY.busAddress =
  2284. PTR_JPGC->info.outLuma.busAddress;
  2285. ASSERT(pDecOut->outputPictureY.busAddress);
  2286. /* if not grayscale */
  2287. if(PTR_JPGC->image.sizeChroma)
  2288. {
  2289. pDecOut->outputPictureCbCr.pVirtualAddress =
  2290. PTR_JPGC->info.outChroma.virtualAddress;
  2291. ASSERT(pDecOut->outputPictureCbCr.pVirtualAddress);
  2292. pDecOut->outputPictureCbCr.busAddress =
  2293. PTR_JPGC->info.outChroma.busAddress;
  2294. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2295. pDecOut->outputPictureCr.pVirtualAddress =
  2296. PTR_JPGC->info.outChroma2.virtualAddress;
  2297. pDecOut->outputPictureCr.busAddress =
  2298. PTR_JPGC->info.outChroma2.busAddress;
  2299. }
  2300. }
  2301. return JPEGDEC_STRM_ERROR;
  2302. }
  2303. else if(asic_status & JPEGDEC_X170_IRQ_BUFFER_EMPTY)
  2304. {
  2305. /* check if frame is ready */
  2306. if(!(asic_status & JPEGDEC_X170_IRQ_DEC_RDY))
  2307. {
  2308. JPEGDEC_API_TRC
  2309. ("JpegDecDecode# JPEGDEC_X170_IRQ_BUFFER_EMPTY/STREAM PROCESSED");
  2310. /* clear interrupts */
  2311. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_BUFFER_INT,
  2312. 0);
  2313. /* flag to load buffer */
  2314. PTR_JPGC->info.inputBufferEmpty = 1;
  2315. /* check if all stream should be processed with the
  2316. * next buffer ==> may affect to some API checks */
  2317. if((PTR_JPGC->info.decodedStreamLen +
  2318. PTR_JPGC->info.inputBufferLen) >=
  2319. PTR_JPGC->stream.streamLength)
  2320. {
  2321. PTR_JPGC->info.streamEndFlag = 1;
  2322. }
  2323. /* output set */
  2324. if(PTR_JPGC->ppInstance == NULL)
  2325. {
  2326. pDecOut->outputPictureY.pVirtualAddress =
  2327. PTR_JPGC->info.outLuma.virtualAddress;
  2328. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2329. /* output set */
  2330. pDecOut->outputPictureY.busAddress =
  2331. PTR_JPGC->info.outLuma.busAddress;
  2332. ASSERT(pDecOut->outputPictureY.busAddress);
  2333. /* if not grayscale */
  2334. if(PTR_JPGC->image.sizeChroma)
  2335. {
  2336. pDecOut->outputPictureCbCr.pVirtualAddress =
  2337. PTR_JPGC->info.outChroma.virtualAddress;
  2338. ASSERT(pDecOut->outputPictureCbCr.
  2339. pVirtualAddress);
  2340. pDecOut->outputPictureCbCr.busAddress =
  2341. PTR_JPGC->info.outChroma.busAddress;
  2342. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2343. pDecOut->outputPictureCr.pVirtualAddress =
  2344. PTR_JPGC->info.outChroma2.virtualAddress;
  2345. pDecOut->outputPictureCr.busAddress =
  2346. PTR_JPGC->info.outChroma2.busAddress;
  2347. }
  2348. }
  2349. return JPEGDEC_STRM_PROCESSED;
  2350. }
  2351. }
  2352. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_RDY_INT, 0);
  2353. HINTdec = GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ);
  2354. if(HINTdec)
  2355. {
  2356. JPEGDEC_API_TRC("JpegDecDecode# CLEAR interrupt");
  2357. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ, 0);
  2358. }
  2359. /* check if slice ready */
  2360. asicSliceBit = GetDecRegister(PTR_JPGC->jpegRegs,
  2361. HWIF_JPEG_SLICE_H);
  2362. intDec = GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_SLICE_INT);
  2363. /* slice ready ==> reset interrupt */
  2364. if(asicSliceBit && intDec)
  2365. {
  2366. /* if x170 pp not in use */
  2367. if(PTR_JPGC->ppInstance == NULL)
  2368. PTR_JPGC->info.SliceReadyForPause = 1;
  2369. else
  2370. PTR_JPGC->info.SliceReadyForPause = 0;
  2371. if(PTR_JPGC->ppInstance != NULL &&
  2372. !PTR_JPGC->ppControl.usePipeline)
  2373. {
  2374. PTR_JPGC->info.SliceReadyForPause = 1;
  2375. }
  2376. /* if user allocated memory return given base */
  2377. if(PTR_JPGC->info.userAllocMem == 1 &&
  2378. PTR_JPGC->info.SliceReadyForPause == 1)
  2379. {
  2380. /* output addresses */
  2381. pDecOut->outputPictureY.pVirtualAddress =
  2382. PTR_JPGC->info.givenOutLuma.virtualAddress;
  2383. pDecOut->outputPictureY.busAddress =
  2384. PTR_JPGC->info.givenOutLuma.busAddress;
  2385. if(PTR_JPGC->image.sizeChroma)
  2386. {
  2387. pDecOut->outputPictureCbCr.pVirtualAddress =
  2388. PTR_JPGC->info.givenOutChroma.virtualAddress;
  2389. pDecOut->outputPictureCbCr.busAddress =
  2390. PTR_JPGC->info.givenOutChroma.busAddress;
  2391. pDecOut->outputPictureCr.pVirtualAddress =
  2392. PTR_JPGC->info.givenOutChroma2.virtualAddress;
  2393. pDecOut->outputPictureCr.busAddress =
  2394. PTR_JPGC->info.givenOutChroma2.busAddress;
  2395. }
  2396. }
  2397. /* if not user allocated memory return slice base */
  2398. if(PTR_JPGC->info.userAllocMem == 0 &&
  2399. PTR_JPGC->info.SliceReadyForPause == 1)
  2400. {
  2401. /* output addresses */
  2402. pDecOut->outputPictureY.pVirtualAddress =
  2403. PTR_JPGC->info.outLuma.virtualAddress;
  2404. pDecOut->outputPictureY.busAddress =
  2405. PTR_JPGC->info.outLuma.busAddress;
  2406. if(PTR_JPGC->image.sizeChroma)
  2407. {
  2408. pDecOut->outputPictureCbCr.pVirtualAddress =
  2409. PTR_JPGC->info.outChroma.virtualAddress;
  2410. pDecOut->outputPictureCbCr.busAddress =
  2411. PTR_JPGC->info.outChroma.busAddress;
  2412. pDecOut->outputPictureCr.pVirtualAddress =
  2413. PTR_JPGC->info.outChroma2.virtualAddress;
  2414. pDecOut->outputPictureCr.busAddress =
  2415. PTR_JPGC->info.outChroma2.busAddress;
  2416. }
  2417. }
  2418. /* No slice output in case decoder + PP (no pipeline) */
  2419. if(PTR_JPGC->ppInstance != NULL &&
  2420. PTR_JPGC->ppControl.usePipeline == 0)
  2421. {
  2422. /* output addresses */
  2423. pDecOut->outputPictureY.pVirtualAddress = NULL;
  2424. pDecOut->outputPictureY.busAddress = 0;
  2425. if(PTR_JPGC->image.sizeChroma)
  2426. {
  2427. pDecOut->outputPictureCbCr.pVirtualAddress = NULL;
  2428. pDecOut->outputPictureCbCr.busAddress = 0;
  2429. pDecOut->outputPictureCr.pVirtualAddress = NULL;
  2430. pDecOut->outputPictureCr.busAddress = 0;
  2431. }
  2432. JPEGDEC_API_TRC(("JpegDecDecode# Decoder + PP (Rotation/Flip), Slice ready"));
  2433. /* PP not in pipeline, continue do <==> while */
  2434. PTR_JPGC->info.noSliceIrqForUser = 1;
  2435. }
  2436. else
  2437. {
  2438. JPEGDEC_API_TRC(("JpegDecDecode# Slice ready"));
  2439. return JPEGDEC_SLICE_READY;
  2440. }
  2441. }
  2442. else
  2443. {
  2444. if(PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE ||
  2445. PTR_JPGC->info.operationType == JPEGDEC_NONINTERLEAVED)
  2446. {
  2447. currentPos =
  2448. GetDecRegister(PTR_JPGC->jpegRegs,
  2449. HWIF_RLC_VLC_BASE);
  2450. /* update input buffer address */
  2451. PTR_JPGC->stream.pCurrPos = ((u8 *) currentPos - 10);
  2452. PTR_JPGC->stream.bitPosInByte = 0;
  2453. PTR_JPGC->stream.readBits =
  2454. ((PTR_JPGC->stream.pCurrPos -
  2455. PTR_JPGC->stream.pStartOfStream) * 8);
  2456. /* default if data ends */
  2457. endOfImage = 1;
  2458. /* check if last scan is decoded */
  2459. for(i = 0;
  2460. i <
  2461. ((PTR_JPGC->stream.streamLength -
  2462. (PTR_JPGC->stream.readBits / 8))); i++)
  2463. {
  2464. currentByte = PTR_JPGC->stream.pCurrPos[i];
  2465. if(currentByte == 0xFF)
  2466. {
  2467. currentByte = PTR_JPGC->stream.pCurrPos[i + 1];
  2468. if(currentByte == 0xD9)
  2469. {
  2470. endOfImage = 1;
  2471. break;
  2472. }
  2473. else if(currentByte == 0xC4 ||
  2474. currentByte == 0xDA)
  2475. {
  2476. endOfImage = 0;
  2477. break;
  2478. }
  2479. }
  2480. }
  2481. currentByte = 0;
  2482. PTR_JPGC->info.SliceCount = 0;
  2483. PTR_JPGC->info.SliceReadyForPause = 0;
  2484. /* if not the last scan of the stream */
  2485. if(endOfImage == 0)
  2486. {
  2487. /* output set */
  2488. if(PTR_JPGC->ppInstance == NULL &&
  2489. !PTR_JPGC->info.noSliceIrqForUser)
  2490. {
  2491. pDecOut->outputPictureY.pVirtualAddress =
  2492. PTR_JPGC->info.outLuma.virtualAddress;
  2493. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2494. /* output set */
  2495. pDecOut->outputPictureY.busAddress =
  2496. PTR_JPGC->info.outLuma.busAddress;
  2497. ASSERT(pDecOut->outputPictureY.busAddress);
  2498. /* if not grayscale */
  2499. if(PTR_JPGC->image.sizeChroma)
  2500. {
  2501. pDecOut->outputPictureCbCr.pVirtualAddress =
  2502. PTR_JPGC->info.outChroma.virtualAddress;
  2503. ASSERT(pDecOut->outputPictureCbCr.
  2504. pVirtualAddress);
  2505. pDecOut->outputPictureCbCr.busAddress =
  2506. PTR_JPGC->info.outChroma.busAddress;
  2507. ASSERT(pDecOut->outputPictureCbCr.
  2508. busAddress);
  2509. pDecOut->outputPictureCr.pVirtualAddress =
  2510. PTR_JPGC->info.outChroma2.
  2511. virtualAddress;
  2512. pDecOut->outputPictureCr.busAddress =
  2513. PTR_JPGC->info.outChroma2.busAddress;
  2514. }
  2515. }
  2516. /* PP not in pipeline, continue do <==> while */
  2517. PTR_JPGC->info.noSliceIrqForUser = 0;
  2518. if(PTR_JPGC->info.operationType ==
  2519. JPEGDEC_PROGRESSIVE)
  2520. PTR_JPGC->info.progressiveScanReady = 1;
  2521. else
  2522. PTR_JPGC->info.nonInterleavedScanReady = 1;
  2523. /* return control to application if progressive */
  2524. if(PTR_JPGC->info.operationType !=
  2525. JPEGDEC_NONINTERLEAVED)
  2526. {
  2527. /* non-interleaved scan ==> no output */
  2528. if(PTR_JPGC->info.nonInterleaved == 0)
  2529. PTR_JPGC->info.noSliceIrqForUser = 1;
  2530. else
  2531. {
  2532. JPEGDEC_API_TRC
  2533. ("JpegDecDecode# SCAN PROCESSED");
  2534. return (JPEGDEC_SCAN_PROCESSED);
  2535. }
  2536. }
  2537. else
  2538. {
  2539. /* set decoded component */
  2540. PTR_JPGC->info.components[PTR_JPGC->info.
  2541. componentId] = 1;
  2542. /* check if we have decoded all components */
  2543. if(PTR_JPGC->info.components[0] == 1 &&
  2544. PTR_JPGC->info.components[1] == 1 &&
  2545. PTR_JPGC->info.components[2] == 1)
  2546. {
  2547. /* continue decoding next scan */
  2548. PTR_JPGC->info.noSliceIrqForUser = 0;
  2549. nonInterleavedRdy = 0;
  2550. }
  2551. else
  2552. {
  2553. /* continue decoding next scan */
  2554. PTR_JPGC->info.noSliceIrqForUser = 1;
  2555. nonInterleavedRdy = 0;
  2556. }
  2557. }
  2558. }
  2559. else
  2560. {
  2561. if(PTR_JPGC->info.operationType ==
  2562. JPEGDEC_NONINTERLEAVED)
  2563. {
  2564. /* set decoded component */
  2565. PTR_JPGC->info.components[PTR_JPGC->info.
  2566. componentId] = 1;
  2567. /* check if we have decoded all components */
  2568. if(PTR_JPGC->info.components[0] == 1 &&
  2569. PTR_JPGC->info.components[1] == 1 &&
  2570. PTR_JPGC->info.components[2] == 1)
  2571. {
  2572. /* continue decoding next scan */
  2573. PTR_JPGC->info.noSliceIrqForUser = 0;
  2574. nonInterleavedRdy = 1;
  2575. }
  2576. else
  2577. {
  2578. /* continue decoding next scan */
  2579. PTR_JPGC->info.noSliceIrqForUser = 1;
  2580. nonInterleavedRdy = 0;
  2581. }
  2582. }
  2583. }
  2584. }
  2585. else
  2586. {
  2587. /* PP not in pipeline, continue do <==> while */
  2588. PTR_JPGC->info.noSliceIrqForUser = 0;
  2589. }
  2590. }
  2591. /* check PP status... and go */
  2592. if(PTR_JPGC->ppInstance != NULL &&
  2593. !PTR_JPGC->info.noSliceIrqForUser)
  2594. {
  2595. /* set pp for stand alone */
  2596. if(!PTR_JPGC->ppControl.usePipeline)
  2597. {
  2598. JpegDecPreparePp(PTR_JPGC);
  2599. JPEGDEC_TRACE_INTERNAL(("INTERNAL: Set output write disabled\n"));
  2600. SetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_OUT_DIS, 1);
  2601. /* Enable pp */
  2602. JPEGDEC_TRACE_INTERNAL(("INTERNAL: Set Enable pp\n"));
  2603. PTR_JPGC->PPRun(PTR_JPGC->ppInstance,
  2604. &PTR_JPGC->ppControl);
  2605. PTR_JPGC->ppControl.ppStatus = DECPP_RUNNING;
  2606. PTR_JPGC->info.pipeline = 1;
  2607. /* Flush regs to hw register */
  2608. JpegFlushRegs(PTR_JPGC);
  2609. }
  2610. /* End PP co-operation */
  2611. if(PTR_JPGC->ppControl.ppStatus == DECPP_RUNNING)
  2612. {
  2613. JPEGDEC_API_TRC("JpegDecDecode# PP END CALL");
  2614. PTR_JPGC->PPEndCallback(PTR_JPGC->ppInstance);
  2615. }
  2616. PTR_JPGC->ppControl.ppStatus = DECPP_PIC_READY;
  2617. }
  2618. /* output set */
  2619. if(PTR_JPGC->ppInstance == NULL &&
  2620. !PTR_JPGC->info.noSliceIrqForUser)
  2621. {
  2622. pDecOut->outputPictureY.pVirtualAddress =
  2623. PTR_JPGC->info.outLuma.virtualAddress;
  2624. ASSERT(pDecOut->outputPictureY.pVirtualAddress);
  2625. /* output set */
  2626. pDecOut->outputPictureY.busAddress =
  2627. PTR_JPGC->info.outLuma.busAddress;
  2628. ASSERT(pDecOut->outputPictureY.busAddress);
  2629. /* if not grayscale */
  2630. if(PTR_JPGC->image.sizeChroma)
  2631. {
  2632. pDecOut->outputPictureCbCr.pVirtualAddress =
  2633. PTR_JPGC->info.outChroma.virtualAddress;
  2634. ASSERT(pDecOut->outputPictureCbCr.pVirtualAddress);
  2635. pDecOut->outputPictureCbCr.busAddress =
  2636. PTR_JPGC->info.outChroma.busAddress;
  2637. ASSERT(pDecOut->outputPictureCbCr.busAddress);
  2638. pDecOut->outputPictureCr.pVirtualAddress =
  2639. PTR_JPGC->info.outChroma2.virtualAddress;
  2640. pDecOut->outputPictureCr.busAddress =
  2641. PTR_JPGC->info.outChroma2.busAddress;
  2642. }
  2643. }
  2644. #ifdef JPEGDEC_ASIC_TRACE
  2645. {
  2646. JPEGDEC_TRACE_INTERNAL(("\nJpegDecDecode# TEST\n"));
  2647. PrintJPEGReg(PTR_JPGC->jpegRegs);
  2648. }
  2649. #endif /* #ifdef JPEGDEC_ASIC_TRACE */
  2650. if(PTR_JPGC->info.noSliceIrqForUser == 0)
  2651. {
  2652. /* Release HW */
  2653. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2654. /* update asicRunning */
  2655. PTR_JPGC->asicRunning = 0;
  2656. JPEGDEC_API_TRC("JpegDecDecode# FRAME READY");
  2657. /* set image ready */
  2658. PTR_JPGC->image.imageReady = 1;
  2659. PTR_JPGC->info.noSliceIrqForUser = 0;
  2660. }
  2661. /* get the current stream address */
  2662. if(PTR_JPGC->info.operationType == JPEGDEC_PROGRESSIVE ||
  2663. (PTR_JPGC->info.operationType == JPEGDEC_NONINTERLEAVED &&
  2664. nonInterleavedRdy == 0))
  2665. {
  2666. retCode = JpegDecNextScanHdrs(PTR_JPGC);
  2667. if(retCode != JPEGDEC_OK && retCode != JPEGDEC_FRAME_READY)
  2668. {
  2669. /* return */
  2670. return retCode;
  2671. }
  2672. }
  2673. }
  2674. else if(dwlret == DWL_HW_WAIT_TIMEOUT)
  2675. {
  2676. JPEGDEC_API_TRC("SCAN: DWL HW TIMEOUT\n");
  2677. /* Release HW */
  2678. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2679. /* update asicRunning */
  2680. PTR_JPGC->asicRunning = 0;
  2681. return (JPEGDEC_DWL_HW_TIMEOUT);
  2682. }
  2683. else if(dwlret == DWL_HW_WAIT_ERROR)
  2684. {
  2685. JPEGDEC_API_TRC("SCAN: DWL HW ERROR\n");
  2686. /* Release HW */
  2687. (void) DWLReleaseHw(PTR_JPGC->dwl);
  2688. /* update asicRunning */
  2689. PTR_JPGC->asicRunning = 0;
  2690. return (JPEGDEC_SYSTEM_ERROR);
  2691. }
  2692. }
  2693. while(!PTR_JPGC->image.imageReady);
  2694. }
  2695. if(PTR_JPGC->image.imageReady)
  2696. {
  2697. JPEGDEC_API_TRC("JpegDecDecode# IMAGE READY");
  2698. JPEGDEC_API_TRC("JpegDecDecode# OK\n");
  2699. /* reset image status */
  2700. PTR_JPGC->image.imageReady = 0;
  2701. PTR_JPGC->image.headerReady = 0;
  2702. if(PTR_JPGC->info.operationType == JPEGDEC_BASELINE)
  2703. {
  2704. return (JPEGDEC_FRAME_READY);
  2705. }
  2706. else
  2707. {
  2708. if(endOfImage == 0)
  2709. return (JPEGDEC_SCAN_PROCESSED);
  2710. else
  2711. {
  2712. if(PTR_JPGC->frame.Nf != 1)
  2713. {
  2714. /* determine first component that needs to be cheated */
  2715. i = 0;
  2716. while(i < 3 && PTR_JPGC->info.pfNeeded[i] == 0)
  2717. i++;
  2718. if(i == 3)
  2719. return (JPEGDEC_FRAME_READY);
  2720. JpegDecInitHWEmptyScan(PTR_JPGC, i++);
  2721. dwlret = DWLWaitHwReady(PTR_JPGC->dwl,
  2722. PTR_JPGC->info.timeout);
  2723. ASSERT(dwlret == DWL_HW_WAIT_OK);
  2724. JpegRefreshRegs(PTR_JPGC);
  2725. asic_status =
  2726. GetDecRegister(PTR_JPGC->jpegRegs, HWIF_DEC_IRQ_STAT);
  2727. ASSERT(asic_status == 1);
  2728. while(i < 3 && PTR_JPGC->info.pfNeeded[i] == 0)
  2729. i++;
  2730. if(i == 3)
  2731. return (JPEGDEC_FRAME_READY);
  2732. else
  2733. {
  2734. PTR_JPGC->info.progressiveFinish = 1;
  2735. PTR_JPGC->info.pfCompId = i;
  2736. return (JPEGDEC_SCAN_PROCESSED);
  2737. }
  2738. }
  2739. else
  2740. return (JPEGDEC_FRAME_READY);
  2741. }
  2742. }
  2743. }
  2744. else
  2745. {
  2746. JPEGDEC_API_TRC("JpegDecDecode# ERROR\n");
  2747. return (JPEGDEC_ERROR);
  2748. }
  2749. #undef JPG_FRM
  2750. #undef PTR_JPGC
  2751. #undef PTR_INFO
  2752. }
  2753. /*------------------------------------------------------------------------------
  2754. Function name: JpegDecPreparePp
  2755. Functional description:
  2756. Setup PP interface
  2757. Input:
  2758. container
  2759. Return values:
  2760. void
  2761. ------------------------------------------------------------------------------*/
  2762. static void JpegDecPreparePp(JpegDecContainer * pJpegDecCont)
  2763. {
  2764. pJpegDecCont->ppControl.picStruct = 0;
  2765. pJpegDecCont->ppControl.topField = 0;
  2766. {
  2767. u32 tmp = GetDecRegister(pJpegDecCont->jpegRegs, HWIF_DEC_OUT_ENDIAN);
  2768. pJpegDecCont->ppControl.littleEndian =
  2769. (tmp == DEC_X170_LITTLE_ENDIAN) ? 1 : 0;
  2770. }
  2771. pJpegDecCont->ppControl.wordSwap = GetDecRegister(pJpegDecCont->jpegRegs,
  2772. HWIF_DEC_OUTSWAP32_E) ? 1
  2773. : 0;
  2774. /* pipeline */
  2775. if(pJpegDecCont->ppControl.usePipeline)
  2776. {
  2777. /* luma */
  2778. pJpegDecCont->ppControl.inputBusLuma = 0;
  2779. /* chroma */
  2780. pJpegDecCont->ppControl.inputBusChroma = 0;
  2781. }
  2782. else
  2783. {
  2784. /* luma */
  2785. pJpegDecCont->ppControl.inputBusLuma =
  2786. pJpegDecCont->asicBuff.outLumaBuffer.busAddress;
  2787. /* chroma */
  2788. pJpegDecCont->ppControl.inputBusChroma =
  2789. pJpegDecCont->asicBuff.outChromaBuffer.busAddress;
  2790. }
  2791. /* dimensions */
  2792. pJpegDecCont->ppControl.inwidth =
  2793. pJpegDecCont->ppControl.croppedW = pJpegDecCont->info.X;
  2794. pJpegDecCont->ppControl.inheight =
  2795. pJpegDecCont->ppControl.croppedH = pJpegDecCont->info.Y;
  2796. }
  2797. /*------------------------------------------------------------------------------
  2798. 5.6. Function name: JpegGetAPIVersion
  2799. Purpose: Returns version information about this API
  2800. Input: void
  2801. Output: JpegDecApiVersion
  2802. ------------------------------------------------------------------------------*/
  2803. JpegDecApiVersion JpegGetAPIVersion()
  2804. {
  2805. JpegDecApiVersion ver;
  2806. ver.major = JPG_MAJOR_VERSION;
  2807. ver.minor = JPG_MINOR_VERSION;
  2808. JPEGDEC_API_TRC("JpegGetAPIVersion# OK\n");
  2809. return ver;
  2810. }
  2811. /*------------------------------------------------------------------------------
  2812. 5.7. Function name: JpegDecGetBuild
  2813. Purpose: Returns the SW and HW build information
  2814. Input: void
  2815. Output: JpegDecGetBuild
  2816. ------------------------------------------------------------------------------*/
  2817. JpegDecBuild JpegDecGetBuild(void)
  2818. {
  2819. JpegDecBuild ver;
  2820. DWLHwConfig_t hwCfg;
  2821. DWLmemset(&ver, 0, sizeof(ver));
  2822. ver.swBuild = JPG_SW_BUILD;
  2823. ver.hwBuild = DWLReadAsicID();
  2824. DWLReadAsicConfig(&hwCfg);
  2825. SET_DEC_BUILD_SUPPORT(ver.hwConfig, hwCfg);
  2826. JPEGDEC_API_TRC("JpegDecGetBuild# OK\n");
  2827. return (ver);
  2828. }
  2829. /*------------------------------------------------------------------------------
  2830. 5.8. Function name : jpegRegisterPP
  2831. Description : Called internally by PP to enable the pipeline
  2832. Return type : i32 - return 0 for success or a negative error code
  2833. Argument : const void * decInst - decoder instance
  2834. Argument : const void *ppInst - post-processor instance
  2835. Argument : (*PPRun)(const void *) - decoder calls this to start PP
  2836. Argument : void (*PPEndCallback)(const void *) - decoder calls this
  2837. to notify PP that a picture was done.
  2838. ------------------------------------------------------------------------------*/
  2839. i32 jpegRegisterPP(const void *decInst, const void *ppInst,
  2840. void (*PPRun) (const void *, DecPpInterface *),
  2841. void (*PPEndCallback) (const void *),
  2842. void (*PPConfigQuery) (const void *, DecPpQuery *))
  2843. {
  2844. JpegDecContainer *pDecCont;
  2845. pDecCont = (JpegDecContainer *) decInst;
  2846. if(decInst == NULL || pDecCont->ppInstance != NULL ||
  2847. ppInst == NULL || PPRun == NULL || PPEndCallback == NULL)
  2848. return -1;
  2849. if(pDecCont->asicRunning)
  2850. return -2;
  2851. pDecCont->ppInstance = ppInst;
  2852. pDecCont->PPEndCallback = PPEndCallback;
  2853. pDecCont->PPRun = PPRun;
  2854. pDecCont->PPConfigQuery = PPConfigQuery;
  2855. return 0;
  2856. }
  2857. /*------------------------------------------------------------------------------
  2858. 5.9. Function name : jpegUnregisterPP
  2859. Description : Called internally by PP to disable the pipeline
  2860. Return type : i32 - return 0 for success or a negative error code
  2861. Argument : const void * decInst - decoder instance
  2862. Argument : const void *ppInst - post-processor instance
  2863. ------------------------------------------------------------------------------*/
  2864. i32 jpegUnregisterPP(const void *decInst, const void *ppInst)
  2865. {
  2866. JpegDecContainer *pDecCont;
  2867. pDecCont = (JpegDecContainer *) decInst;
  2868. ASSERT(decInst != NULL && ppInst == pDecCont->ppInstance);
  2869. if(decInst == NULL || ppInst != pDecCont->ppInstance)
  2870. return -1;
  2871. if(pDecCont->asicRunning)
  2872. return -2;
  2873. pDecCont->ppInstance = NULL;
  2874. pDecCont->PPEndCallback = NULL;
  2875. pDecCont->PPRun = NULL;
  2876. return 0;
  2877. }