jpuapifunc.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2018, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "jpuapifunc.h"
  27. #include "regdefine.h"
  28. #include "jpulog.h"
  29. /******************************************************************************
  30. Codec Instance Slot Management
  31. ******************************************************************************/
  32. // gGetBits defined but not used
  33. //static Uint32 tGetBits(JpgDecInfo *jpg, int endian, int byteCnt);
  34. const char lendian[4] = {0x49, 0x49, 0x2A, 0x00};
  35. const char bendian[4] = {0x4D, 0x4D, 0x00, 0x2A};
  36. const char *jfif = "JFIF";
  37. const char *jfxx = "JFXX";
  38. const char *exif = "Exif";
  39. Uint8 sJpuCompInfoTable[5][24] = {
  40. { 00, 02, 02, 00, 00, 00, 01, 01, 01, 01, 01, 01, 02, 01, 01, 01, 01, 01, 03, 00, 00, 00, 00, 00 }, //420
  41. { 00, 02, 01, 00, 00, 00, 01, 01, 01, 01, 01, 01, 02, 01, 01, 01, 01, 01, 03, 00, 00, 00, 00, 00 }, //422H
  42. { 00, 01, 02, 00, 00, 00, 01, 01, 01, 01, 01, 01, 02, 01, 01, 01, 01, 01, 03, 00, 00, 00, 00, 00 }, //422V
  43. { 00, 01, 01, 00, 00, 00, 01, 01, 01, 01, 01, 01, 02, 01, 01, 01, 01, 01, 03, 00, 00, 00, 00, 00 }, //444
  44. { 00, 01, 01, 00, 00, 00, 01, 00, 00, 00, 00, 00, 02, 00, 00, 00, 00, 00, 03, 00, 00, 00, 00, 00 }, //400
  45. };
  46. Uint8 sJpuCompInfoTable_EX[5][24] = {
  47. { 00, 02, 02, 00, 02, 02, 01, 01, 01, 01, 03, 03, 02, 01, 01, 01, 03, 03, 03, 00, 00, 00, 00, 00 }, //420
  48. { 00, 02, 01, 00, 02, 02, 01, 01, 01, 01, 03, 03, 02, 01, 01, 01, 03, 03, 03, 00, 00, 00, 00, 00 }, //422H
  49. { 00, 01, 02, 00, 02, 02, 01, 01, 01, 01, 03, 03, 02, 01, 01, 01, 03, 03, 03, 00, 00, 00, 00, 00 }, //422V
  50. { 00, 01, 01, 00, 02, 02, 01, 01, 01, 01, 03, 03, 02, 01, 01, 01, 03, 03, 03, 00, 00, 00, 00, 00 }, //444
  51. { 00, 01, 01, 00, 00, 00, 01, 00, 00, 00, 00, 00, 02, 00, 00, 00, 00, 00, 03, 00, 00, 00, 00, 00 }, //400
  52. };
  53. /*
  54. * The parameters of input source: iPackMode
  55. * The parameters of jpeg sample : iFormat
  56. */
  57. Uint32 GetDec8bitBusReqNum(FrameFormat iFormat, PackedFormat oPackMode)
  58. {
  59. Uint32 num = 0;
  60. if (oPackMode == PACKED_FORMAT_NONE) {
  61. switch (iFormat) {
  62. case FORMAT_400: num = 2; break;
  63. case FORMAT_420: num = 4; break;
  64. case FORMAT_422: num = 4; break;
  65. case FORMAT_440: num = 8; break;
  66. case FORMAT_444: num = 8; break;
  67. default: num = 0; break;
  68. }
  69. }
  70. else {
  71. switch (oPackMode) {
  72. case PACKED_FORMAT_422_YUYV:
  73. case PACKED_FORMAT_422_YVYU:
  74. case PACKED_FORMAT_422_UYVY:
  75. case PACKED_FORMAT_422_VYUY:
  76. if (iFormat == FORMAT_400) num = 1;
  77. else if (iFormat == FORMAT_420) num = 2;
  78. else if (iFormat == FORMAT_422) num = 2;
  79. else num = 4;
  80. break;
  81. case PACKED_FORMAT_444:
  82. num = 8;
  83. break;
  84. default:
  85. num = 0;
  86. break;
  87. }
  88. }
  89. return num;
  90. }
  91. Uint32 GetDec12bitBusReqNum(FrameFormat iFormat, PackedFormat oPackMode)
  92. {
  93. Uint32 num = 0;
  94. if (oPackMode == PACKED_FORMAT_NONE) {
  95. switch (iFormat) {
  96. case FORMAT_400: num = 1; break;
  97. case FORMAT_420: num = 2; break;
  98. case FORMAT_422: num = 2; break;
  99. case FORMAT_440: num = 4; break;
  100. case FORMAT_444: num = 4; break;
  101. default: num = 0; break;
  102. }
  103. }
  104. else {
  105. switch (oPackMode) {
  106. case PACKED_FORMAT_422_YUYV:
  107. case PACKED_FORMAT_422_YVYU:
  108. case PACKED_FORMAT_422_UYVY:
  109. case PACKED_FORMAT_422_VYUY:
  110. if (iFormat == FORMAT_400) num = 1;
  111. else if (iFormat == FORMAT_420) num = 1;
  112. else if (iFormat == FORMAT_422) num = 1;
  113. else if (iFormat == FORMAT_440) num = 2;
  114. else if (iFormat == FORMAT_444) num = 2;
  115. else num = 0;
  116. break;
  117. case PACKED_FORMAT_444:
  118. num = 4;
  119. break;
  120. default:
  121. num = 0;
  122. break;
  123. }
  124. }
  125. return num;
  126. }
  127. /*
  128. * The parameters of input source: packMode
  129. * The parameters of jpeg sample : oFormat
  130. */
  131. Uint32 GetEnc8bitBusReqNum(PackedFormat iPackMode, FrameFormat oFormat)
  132. {
  133. Uint32 num = 0;
  134. if (iPackMode == PACKED_FORMAT_NONE) {
  135. switch (oFormat) {
  136. case FORMAT_400: num = 4; break;
  137. case FORMAT_440: num = 8; break;
  138. case FORMAT_444: num = 8; break;
  139. default: num = 4; break;
  140. }
  141. }
  142. else {
  143. switch (iPackMode) {
  144. case PACKED_FORMAT_422_YUYV:
  145. case PACKED_FORMAT_422_YVYU:
  146. case PACKED_FORMAT_422_UYVY:
  147. case PACKED_FORMAT_422_VYUY:
  148. if (oFormat == FORMAT_400) num = 2;
  149. else if (oFormat == FORMAT_440) num = 4;
  150. else if (oFormat == FORMAT_444) num = 4;
  151. else num = 2;
  152. break;
  153. case PACKED_FORMAT_444:
  154. num = 8;
  155. break;
  156. default:
  157. num = 0;
  158. break;
  159. }
  160. }
  161. return num;
  162. }
  163. /*
  164. * The parameters of input source: packMode
  165. * The parameters of jpeg sample : oFormat
  166. */
  167. Uint32 GetEnc12bitBusReqNum(PackedFormat iPackMode, FrameFormat oFormat)
  168. {
  169. Uint32 num = 0;
  170. if (iPackMode == PACKED_FORMAT_NONE) {
  171. switch (oFormat) {
  172. case FORMAT_400: num = 2; break;
  173. case FORMAT_440: num = 4; break;
  174. case FORMAT_444: num = 4; break;
  175. default: num = 2; break;
  176. }
  177. }
  178. else {
  179. switch (iPackMode) {
  180. case PACKED_FORMAT_422_YUYV:
  181. case PACKED_FORMAT_422_YVYU:
  182. case PACKED_FORMAT_422_UYVY:
  183. case PACKED_FORMAT_422_VYUY:
  184. num = (oFormat == FORMAT_440 || oFormat == FORMAT_444) ? 2 : 1;
  185. break;
  186. case PACKED_FORMAT_444:
  187. num = 4;
  188. break;
  189. default:
  190. num = 0;
  191. break;
  192. }
  193. }
  194. return num;
  195. }
  196. /*
  197. * GetJpgInstance() obtains a instance.
  198. * It stores a pointer to the allocated instance in *ppInst
  199. * and returns JPG_RET_SUCCESS on success.
  200. * Failure results in 0(null pointer) in *ppInst and JPG_RET_FAILURE.
  201. */
  202. JpgRet GetJpgInstance(JpgInst ** ppInst)
  203. {
  204. Int32 i;
  205. Uint32 handleSize;
  206. JpgInst* pJpgInst = 0;
  207. jpu_instance_pool_t* jip;
  208. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  209. if (!jip)
  210. return JPG_RET_INVALID_HANDLE;
  211. for (i = 0; i < MAX_NUM_INSTANCE; i++) {
  212. pJpgInst = (JpgInst *)jip->jpgInstPool[i];
  213. if (!pJpgInst) {
  214. return JPG_RET_FAILURE;
  215. }
  216. if (!pJpgInst->inUse)
  217. break;
  218. }
  219. if (i == MAX_NUM_INSTANCE) {
  220. *ppInst = 0;
  221. return JPG_RET_FAILURE;
  222. }
  223. pJpgInst->inUse = TRUE;
  224. handleSize = sizeof(JpgDecInfo);
  225. if (handleSize < sizeof(JpgEncInfo)) {
  226. handleSize = sizeof(JpgEncInfo);
  227. }
  228. if ((pJpgInst->JpgInfo=(void*)malloc(handleSize)) == NULL) {
  229. return JPG_RET_INSUFFICIENT_RESOURCE;
  230. }
  231. memset((void*)pJpgInst->JpgInfo, 0x00, sizeof(handleSize));
  232. *ppInst = pJpgInst;
  233. jip->jpu_instance_num++;
  234. if (jdi_open_instance(pJpgInst->instIndex) < 0) {
  235. return JPG_RET_FAILURE;
  236. }
  237. return JPG_RET_SUCCESS;
  238. }
  239. void FreeJpgInstance(JpgInst * pJpgInst)
  240. {
  241. jpu_instance_pool_t *jip;
  242. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  243. if (!jip)
  244. return;
  245. pJpgInst->inUse = 0;
  246. jip->jpu_instance_num--;
  247. jdi_close_instance(pJpgInst->instIndex);
  248. free(pJpgInst->JpgInfo);
  249. pJpgInst->JpgInfo = NULL;
  250. }
  251. JpgRet CheckJpgInstValidity(JpgInst * pci)
  252. {
  253. int i;
  254. jpu_instance_pool_t *jip;
  255. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  256. if (!jip)
  257. return JPG_RET_FAILURE;
  258. for (i = 0; i < MAX_NUM_INSTANCE; ++i) {
  259. if ((JpgInst *)jip->jpgInstPool[i] == pci)
  260. return JPG_RET_SUCCESS;
  261. }
  262. return JPG_RET_INVALID_HANDLE;
  263. }
  264. /******************************************************************************
  265. API Subroutines
  266. ******************************************************************************/
  267. JpgRet CheckJpgDecOpenParam(JpgDecOpenParam * pop)
  268. {
  269. if (pop == 0) {
  270. return JPG_RET_INVALID_PARAM;
  271. }
  272. if (pop->bitstreamBuffer % 8) {
  273. return JPG_RET_INVALID_PARAM;
  274. }
  275. if (pop->bitstreamBufferSize % 1024 || pop->bitstreamBufferSize < 1024) {
  276. return JPG_RET_INVALID_PARAM;
  277. }
  278. if (pop->chromaInterleave != CBCR_SEPARATED &&
  279. pop->chromaInterleave != CBCR_INTERLEAVE &&
  280. pop->chromaInterleave != CRCB_INTERLEAVE) {
  281. return JPG_RET_INVALID_PARAM;
  282. }
  283. if (pop->rotation != 0 && pop->rotation != 90 && pop->rotation != 180 && pop->rotation != 270) {
  284. return JPG_RET_INVALID_PARAM;
  285. }
  286. if ((Uint32)pop->mirror > (Uint32)MIRDIR_HOR_VER) {
  287. return JPG_RET_INVALID_PARAM;
  288. }
  289. if (pop->outputFormat == FORMAT_400 || pop->outputFormat == FORMAT_440) {
  290. return JPG_RET_INVALID_PARAM;
  291. }
  292. if (pop->packedFormat > PACKED_FORMAT_444) {
  293. return JPG_RET_INVALID_PARAM;
  294. }
  295. if (pop->packedFormat != PACKED_FORMAT_NONE) {
  296. if (pop->chromaInterleave != CBCR_SEPARATED) {
  297. return JPG_RET_INVALID_PARAM;
  298. }
  299. }
  300. return JPG_RET_SUCCESS;
  301. }
  302. int JpgDecHuffTabSetUp(JpgDecInfo *jpg, int instRegIndex)
  303. {
  304. int i, j;
  305. int HuffData; // 16BITS
  306. int HuffLength;
  307. int temp;
  308. // MIN Tables
  309. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x003);
  310. //DC Luma
  311. for(j=0; j<16; j++)
  312. {
  313. HuffData = jpg->huffMin[0][j];
  314. temp = (HuffData & 0x8000) >> 15;
  315. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  316. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  317. }
  318. //DC Chroma
  319. for(j=0; j<16; j++)
  320. {
  321. HuffData = jpg->huffMin[2][j];
  322. temp = (HuffData & 0x8000) >> 15;
  323. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  324. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  325. }
  326. //AC Luma
  327. for(j=0; j<16; j++)
  328. {
  329. HuffData = jpg->huffMin[1][j];
  330. temp = (HuffData & 0x8000) >> 15;
  331. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  332. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  333. }
  334. //AC Chroma
  335. for(j=0; j<16; j++)
  336. {
  337. HuffData = jpg->huffMin[3][j];
  338. temp = (HuffData & 0x8000) >> 15;
  339. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  340. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  341. }
  342. // MAX Tables
  343. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x403);
  344. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_ADDR_REG, 0x440);
  345. //DC Luma
  346. for(j=0; j<16; j++)
  347. {
  348. HuffData = jpg->huffMax[0][j];
  349. temp = (HuffData & 0x8000) >> 15;
  350. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  351. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  352. }
  353. //DC Chroma
  354. for(j=0; j<16; j++)
  355. {
  356. HuffData = jpg->huffMax[2][j];
  357. temp = (HuffData & 0x8000) >> 15;
  358. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  359. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  360. }
  361. //AC Luma
  362. for(j=0; j<16; j++)
  363. {
  364. HuffData = jpg->huffMax[1][j];
  365. temp = (HuffData & 0x8000) >> 15;
  366. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  367. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  368. }
  369. //AC Chroma
  370. for(j=0; j<16; j++)
  371. {
  372. HuffData = jpg->huffMax[3][j];
  373. temp = (HuffData & 0x8000) >> 15;
  374. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  375. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  376. }
  377. // PTR Tables
  378. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_CTRL_REG, 0x803);
  379. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_ADDR_REG, 0x880);
  380. //DC Luma
  381. for(j=0; j<16; j++)
  382. {
  383. HuffData = jpg->huffPtr[0][j];
  384. temp = (HuffData & 0x80) >> 7;
  385. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  386. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  387. }
  388. //DC Chroma
  389. for(j=0; j<16; j++)
  390. {
  391. HuffData = jpg->huffPtr[2][j];
  392. temp = (HuffData & 0x80) >> 7;
  393. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  394. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  395. }
  396. //AC Luma
  397. for(j=0; j<16; j++)
  398. {
  399. HuffData = jpg->huffPtr[1][j];
  400. temp = (HuffData & 0x80) >> 7;
  401. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  402. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  403. }
  404. //AC Chroma
  405. for(j=0; j<16; j++)
  406. {
  407. HuffData = jpg->huffPtr[3][j];
  408. temp = (HuffData & 0x80) >> 7;
  409. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  410. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  411. }
  412. // VAL Tables
  413. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0xC03);
  414. // VAL DC Luma
  415. HuffLength = 0;
  416. for(i=0; i<12; i++)
  417. HuffLength += jpg->huffBits[0][i];
  418. if (HuffLength > 256)
  419. return 0;
  420. for (i=0; i<HuffLength; i++) { // 8-bit, 12 row, 1 category (DC Luma)
  421. HuffData = jpg->huffVal[0][i];
  422. temp = (HuffData & 0x80) >> 7;
  423. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  424. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  425. }
  426. for (i=0; i<12-HuffLength; i++) {
  427. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  428. }
  429. // VAL DC Chroma
  430. HuffLength = 0;
  431. for(i=0; i<12; i++)
  432. HuffLength += jpg->huffBits[2][i];
  433. if (HuffLength > 256)
  434. return 0;
  435. for (i=0; i<HuffLength; i++) { // 8-bit, 12 row, 1 category (DC Chroma)
  436. HuffData = jpg->huffVal[2][i];
  437. temp = (HuffData & 0x80) >> 7;
  438. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  439. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  440. }
  441. for (i=0; i<12-HuffLength; i++) {
  442. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  443. }
  444. // VAL AC Luma
  445. HuffLength = 0;
  446. for(i=0; i<162; i++)
  447. HuffLength += jpg->huffBits[1][i];
  448. if (HuffLength > 256)
  449. return 0;
  450. for (i=0; i<HuffLength; i++) { // 8-bit, 162 row, 1 category (AC Luma)
  451. HuffData = jpg->huffVal[1][i];
  452. temp = (HuffData & 0x80) >> 7;
  453. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  454. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  455. }
  456. for (i=0; i<162-HuffLength; i++) {
  457. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  458. }
  459. // VAL AC Chroma
  460. HuffLength = 0;
  461. for(i=0; i<162; i++)
  462. HuffLength += jpg->huffBits[3][i];
  463. if (HuffLength > 256)
  464. return 0;
  465. for (i=0; i<HuffLength; i++) { // 8-bit, 162 row, 1 category (AC Chroma)
  466. HuffData = jpg->huffVal[3][i];
  467. temp = (HuffData & 0x80) >> 7;
  468. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  469. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  470. }
  471. for (i=0; i<162-HuffLength; i++) {
  472. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  473. }
  474. // end SerPeriHuffTab
  475. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x000);
  476. return 1;
  477. }
  478. int JpgDecHuffTabSetUp_12b(JpgDecInfo *jpg, int instRegIndex)
  479. {
  480. int i, j;
  481. int HuffData; // 16BITS
  482. int HuffLength;
  483. int temp;
  484. // MIN Tables
  485. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x003);
  486. //DC Luma
  487. for(j=0; j<16; j++)
  488. {
  489. HuffData = jpg->huffMin[0][j];
  490. temp = (HuffData & 0x8000) >> 15;
  491. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  492. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  493. }
  494. //DC Chroma
  495. for(j=0; j<16; j++)
  496. {
  497. HuffData = jpg->huffMin[2][j];
  498. temp = (HuffData & 0x8000) >> 15;
  499. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  500. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  501. }
  502. //AC Luma
  503. for(j=0; j<16; j++)
  504. {
  505. HuffData = jpg->huffMin[1][j];
  506. temp = (HuffData & 0x8000) >> 15;
  507. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  508. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  509. }
  510. //AC Chroma
  511. for(j=0; j<16; j++)
  512. {
  513. HuffData = jpg->huffMin[3][j];
  514. temp = (HuffData & 0x8000) >> 15;
  515. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  516. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  517. }
  518. //DC EX1
  519. for(j=0; j<16; j++)
  520. {
  521. HuffData = jpg->huffMin[4][j];
  522. temp = (HuffData & 0x8000) >> 15;
  523. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  524. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  525. }
  526. //AC EX1
  527. for(j=0; j<16; j++)
  528. {
  529. HuffData = jpg->huffMin[5][j];
  530. temp = (HuffData & 0x8000) >> 15;
  531. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  532. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData)); // 32-bit
  533. }
  534. // MAX Tables
  535. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x403);
  536. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_ADDR_REG, 0x480);
  537. //DC Luma
  538. for(j=0; j<16; j++)
  539. {
  540. HuffData = jpg->huffMax[0][j];
  541. temp = (HuffData & 0x8000) >> 15;
  542. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  543. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  544. }
  545. //DC Chroma
  546. for(j=0; j<16; j++)
  547. {
  548. HuffData = jpg->huffMax[2][j];
  549. temp = (HuffData & 0x8000) >> 15;
  550. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  551. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  552. }
  553. //AC Luma
  554. for(j=0; j<16; j++)
  555. {
  556. HuffData = jpg->huffMax[1][j];
  557. temp = (HuffData & 0x8000) >> 15;
  558. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  559. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  560. }
  561. //AC Chroma
  562. for(j=0; j<16; j++)
  563. {
  564. HuffData = jpg->huffMax[3][j];
  565. temp = (HuffData & 0x8000) >> 15;
  566. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  567. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  568. }
  569. //DC EX1
  570. for(j=0; j<16; j++)
  571. {
  572. HuffData = jpg->huffMax[4][j];
  573. temp = (HuffData & 0x8000) >> 15;
  574. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  575. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  576. }
  577. //AC EX1
  578. for(j=0; j<16; j++)
  579. {
  580. HuffData = jpg->huffMax[5][j];
  581. temp = (HuffData & 0x8000) >> 15;
  582. temp = (temp << 15) | (temp << 14) | (temp << 13) | (temp << 12) | (temp << 11) | (temp << 10) | (temp << 9) | (temp << 8) | (temp << 7 ) | (temp << 6) | (temp <<5) | (temp<<4) | (temp<<3) | (temp<<2) | (temp<<1)| (temp) ;
  583. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFF) << 16) | HuffData));
  584. }
  585. // PTR Tables
  586. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_CTRL_REG, 0x803);
  587. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_ADDR_REG, 0x900);
  588. //DC Luma
  589. for(j=0; j<16; j++)
  590. {
  591. HuffData = jpg->huffPtr[0][j];
  592. temp = (HuffData & 0x80) >> 7;
  593. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  594. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  595. }
  596. //DC Chroma
  597. for(j=0; j<16; j++)
  598. {
  599. HuffData = jpg->huffPtr[2][j];
  600. temp = (HuffData & 0x80) >> 7;
  601. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  602. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  603. }
  604. //AC Luma
  605. for(j=0; j<16; j++)
  606. {
  607. HuffData = jpg->huffPtr[1][j];
  608. temp = (HuffData & 0x80) >> 7;
  609. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  610. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  611. }
  612. //AC Chroma
  613. for(j=0; j<16; j++)
  614. {
  615. HuffData = jpg->huffPtr[3][j];
  616. temp = (HuffData & 0x80) >> 7;
  617. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  618. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  619. }
  620. //DC EX1
  621. for(j=0; j<16; j++)
  622. {
  623. HuffData = jpg->huffPtr[4][j];
  624. temp = (HuffData & 0x80) >> 7;
  625. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  626. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  627. }
  628. //AC EX1
  629. for(j=0; j<16; j++)
  630. {
  631. HuffData = jpg->huffPtr[5][j];
  632. temp = (HuffData & 0x80) >> 7;
  633. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  634. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  635. }
  636. // VAL Tables
  637. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0xC03);
  638. // VAL DC Luma
  639. HuffLength = 0;
  640. for(i=0; i<16; i++) {
  641. HuffLength += jpg->huffBits[0][i];
  642. }
  643. if (HuffLength > 256) {
  644. return 0;
  645. }
  646. for (i=0; i<HuffLength; i++) { // 8-bit, 12 row, 1 category (DC Luma)
  647. HuffData = jpg->huffVal[0][i];
  648. temp = (HuffData & 0x80) >> 7;
  649. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  650. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  651. }
  652. for (i=0; i<16-HuffLength; i++) {
  653. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  654. }
  655. // VAL DC Chroma
  656. HuffLength = 0;
  657. for(i=0; i<16; i++) {
  658. HuffLength += jpg->huffBits[2][i];
  659. }
  660. if (HuffLength > 256) {
  661. return 0;
  662. }
  663. for (i=0; i<HuffLength; i++) { // 8-bit, 12 row, 1 category (DC Chroma)
  664. HuffData = jpg->huffVal[2][i];
  665. temp = (HuffData & 0x80) >> 7;
  666. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  667. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  668. }
  669. for (i=0; i<16-HuffLength; i++) {
  670. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  671. }
  672. // VAL AC Luma
  673. HuffLength = 0;
  674. for(i=0; i<256; i++) {
  675. HuffLength += jpg->huffBits[1][i];
  676. }
  677. if (HuffLength > 256) {
  678. return 0;
  679. }
  680. for (i=0; i<HuffLength; i++) { // 8-bit, 162 row, 1 category (AC Luma)
  681. HuffData = jpg->huffVal[1][i];
  682. temp = (HuffData & 0x80) >> 7;
  683. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  684. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  685. }
  686. for (i=0; i<256-HuffLength; i++) {
  687. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  688. }
  689. // VAL AC Chroma
  690. HuffLength = 0;
  691. for(i=0; i<256; i++) {
  692. HuffLength += jpg->huffBits[3][i];
  693. }
  694. if (HuffLength > 256) {
  695. return 0;
  696. }
  697. for (i=0; i<HuffLength; i++) { // 8-bit, 162 row, 1 category (AC Chroma)
  698. HuffData = jpg->huffVal[3][i];
  699. temp = (HuffData & 0x80) >> 7;
  700. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  701. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  702. }
  703. for (i=0; i<256-HuffLength; i++) {
  704. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  705. }
  706. // VAL DC EX1
  707. HuffLength = 0;
  708. for(i=0; i<16; i++) {
  709. HuffLength += jpg->huffBits[4][i];
  710. }
  711. if (HuffLength > 256) {
  712. return 0;
  713. }
  714. for (i=0; i<HuffLength; i++) { // 8-bit, 12 row, 1 category (DC Luma)
  715. HuffData = jpg->huffVal[4][i];
  716. temp = (HuffData & 0x80) >> 7;
  717. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  718. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  719. }
  720. for (i=0; i<16-HuffLength; i++) {
  721. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  722. }
  723. // VAL AC EX1
  724. HuffLength = 0;
  725. for(i=0; i<256; i++) {
  726. HuffLength += jpg->huffBits[5][i];
  727. }
  728. if (HuffLength > 256) {
  729. return 0;
  730. }
  731. for (i=0; i<HuffLength; i++) { // 8-bit, 162 row, 1 category (AC Luma)
  732. HuffData = jpg->huffVal[5][i];
  733. temp = (HuffData & 0x80) >> 7;
  734. temp = (temp<<23)|(temp<<22)|(temp<<21)|(temp<<20)|(temp<<19)|(temp<<18)|(temp<<17)|(temp<<16)|(temp<<15)|(temp<<14)|(temp<<13)|(temp<<12)|(temp<<11)|(temp<<10)|(temp<<9)|(temp<<8)|(temp<<7)|(temp<<6)|(temp<<5)|(temp<<4)|(temp<<3)|(temp<<2)|(temp<<1)|(temp);
  735. JpuWriteInstReg (instRegIndex, MJPEG_HUFF_DATA_REG, (((temp & 0xFFFFFF) << 8) | HuffData));
  736. }
  737. for (i=0; i<256-HuffLength; i++) {
  738. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, 0xFFFFFFFF);
  739. }
  740. // end SerPeriHuffTab
  741. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x000);
  742. return 1;
  743. }
  744. int JpgDecQMatTabSetUp(JpgDecInfo *jpg, int instRegIndex)
  745. {
  746. int i;
  747. int table;
  748. int val;
  749. // SetPeriQMatTab
  750. // Comp 0
  751. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x03);
  752. table = jpg->cInfoTab[0][3];
  753. if (table >= 4)
  754. return 0;
  755. for (i=0; i<64; i++) {
  756. val = jpg->qMatTab[table][i];
  757. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, val);
  758. }
  759. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x00);
  760. // Comp 1
  761. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x43);
  762. table = jpg->cInfoTab[1][3];
  763. if (table >= 4)
  764. return 0;
  765. for (i=0; i<64; i++) {
  766. val = jpg->qMatTab[table][i];
  767. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, val);
  768. }
  769. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x00);
  770. // Comp 2
  771. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x83);
  772. table = jpg->cInfoTab[2][3];
  773. if (table >= 4)
  774. return 0;
  775. for (i=0; i<64; i++) {
  776. val = jpg->qMatTab[table][i];
  777. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, val);
  778. }
  779. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x00);
  780. return 1;
  781. }
  782. void JpgDecGramSetup(JpgDecInfo * jpg, int instRegIndex)
  783. {
  784. int dExtBitBufCurPos;
  785. int dExtBitBufBaseAddr;
  786. int dMibStatus;
  787. dMibStatus = 1;
  788. dExtBitBufCurPos = jpg->pagePtr;
  789. dExtBitBufBaseAddr = jpg->streamBufStartAddr;
  790. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, dExtBitBufCurPos);
  791. JpuWriteInstReg(instRegIndex, MJPEG_BBC_EXT_ADDR_REG, dExtBitBufBaseAddr + (dExtBitBufCurPos << 8));
  792. JpuWriteInstReg(instRegIndex, MJPEG_BBC_INT_ADDR_REG, (dExtBitBufCurPos & 1) << 6);
  793. JpuWriteInstReg(instRegIndex, MJPEG_BBC_DATA_CNT_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  794. JpuWriteInstReg(instRegIndex, MJPEG_BBC_COMMAND_REG, (jpg->streamEndian << 1) | 0);
  795. while (dMibStatus == 1) {
  796. dMibStatus = JpuReadInstReg(instRegIndex, MJPEG_BBC_BUSY_REG);
  797. }
  798. dMibStatus = 1;
  799. dExtBitBufCurPos = dExtBitBufCurPos + 1;
  800. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, dExtBitBufCurPos);
  801. JpuWriteInstReg(instRegIndex, MJPEG_BBC_EXT_ADDR_REG, dExtBitBufBaseAddr + (dExtBitBufCurPos << 8));
  802. JpuWriteInstReg(instRegIndex, MJPEG_BBC_INT_ADDR_REG, (dExtBitBufCurPos & 1) << 6);
  803. JpuWriteInstReg(instRegIndex, MJPEG_BBC_DATA_CNT_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  804. JpuWriteInstReg(instRegIndex, MJPEG_BBC_COMMAND_REG, (jpg->streamEndian << 1) | 0);
  805. while (dMibStatus == 1) {
  806. dMibStatus = JpuReadInstReg(instRegIndex, MJPEG_BBC_BUSY_REG);
  807. }
  808. dExtBitBufCurPos = dExtBitBufCurPos + 1;
  809. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CUR_POS_REG, dExtBitBufCurPos); // next unit page pointer
  810. JpuWriteInstReg(instRegIndex, MJPEG_BBC_CTRL_REG, (jpg->streamEndian<< 1) | 1);
  811. JpuWriteInstReg(instRegIndex, MJPEG_GBU_WPTR_REG, jpg->wordPtr);
  812. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBSR_REG, 0);
  813. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBER_REG, ((JPU_GBU_SIZE / 4) * 2) - 1);
  814. if (jpg->pagePtr & 1) {
  815. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBIR_REG, 0);
  816. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBHR_REG, 0);
  817. }
  818. else {
  819. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBIR_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  820. JpuWriteInstReg(instRegIndex, MJPEG_GBU_BBHR_REG, JPU_GBU_SIZE / 4); // 64 * 4 byte == 32 * 8 byte
  821. }
  822. JpuWriteInstReg(instRegIndex, MJPEG_GBU_CTRL_REG, 4);
  823. JpuWriteInstReg(instRegIndex, MJPEG_GBU_FF_RPTR_REG, jpg->bitPtr);
  824. }
  825. enum {
  826. SAMPLE_420 = 0xA,
  827. SAMPLE_H422 = 0x9,
  828. SAMPLE_V422 = 0x6,
  829. SAMPLE_444 = 0x5,
  830. SAMPLE_400 = 0x1
  831. };
  832. const BYTE cDefHuffBits[8][16] =
  833. {
  834. { // DC index 0 (Luminance DC)
  835. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  836. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  837. }
  838. ,
  839. { // AC index 0 (Luminance AC)
  840. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  841. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D
  842. }
  843. ,
  844. { // DC index 1 (Chrominance DC)
  845. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  846. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  847. }
  848. ,
  849. { // AC index 1 (Chrominance AC)
  850. 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  851. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77
  852. }
  853. };
  854. const BYTE cDefHuffVal[8][162] =
  855. {
  856. { // DC index 0 (Luminance DC)
  857. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  858. 0x08, 0x09, 0x0A, 0x0B
  859. }
  860. ,
  861. { // AC index 0 (Luminance AC)
  862. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  863. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  864. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
  865. 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
  866. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
  867. 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
  868. 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  869. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  870. 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  871. 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  872. 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  873. 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  874. 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  875. 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  876. 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
  877. 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
  878. 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4,
  879. 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  880. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
  881. 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  882. 0xF9, 0xFA
  883. }
  884. ,
  885. { // DC index 1 (Chrominance DC)
  886. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  887. 0x08, 0x09, 0x0A, 0x0B
  888. }
  889. ,
  890. { // AC index 1 (Chrominance AC)
  891. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  892. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  893. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  894. 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
  895. 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
  896. 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
  897. 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
  898. 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  899. 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  900. 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  901. 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  902. 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  903. 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  904. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
  905. 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
  906. 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
  907. 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
  908. 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
  909. 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
  910. 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  911. 0xF9, 0xFA
  912. }
  913. };
  914. const BYTE cDefHuffBits_ES[8][16] = {
  915. { // DC index 0 (Luminance DC)
  916. 0x00, 0x02, 0x03, 0x01, 0x00, 0x03, 0x01, 0x01,
  917. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  918. },
  919. { // AC index 0 (Luminance AC)
  920. 0x00, 0x01, 0x04, 0x02, 0x02, 0x02, 0x01, 0x04,
  921. 0x01, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0xEB
  922. },
  923. { // DC index 1 (Chrominance DC)
  924. 0x00, 0x02, 0x02, 0x02, 0x03, 0x01, 0x01, 0x01,
  925. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  926. },
  927. { // AC index 1 (Chrominance AC)
  928. 0x00, 0x01, 0x03, 0x02, 0x05, 0x01, 0x05, 0x05,
  929. 0x03, 0x07, 0x04, 0x04, 0x03, 0x04, 0x06, 0xCB
  930. },
  931. { // DC EX1 (Luminance DC)
  932. 0x00, 0x02, 0x03, 0x01, 0x00, 0x03, 0x01, 0x01,
  933. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  934. },
  935. { // AC EX1 (Luminance AC)
  936. 0x00, 0x01, 0x04, 0x02, 0x02, 0x02, 0x01, 0x04,
  937. 0x01, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0xEB
  938. },
  939. { // DC EX2 (Chrominance DC)
  940. 0x00, 0x02, 0x02, 0x02, 0x03, 0x01, 0x01, 0x01,
  941. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  942. },
  943. { // AC EX2 (Chrominance AC)
  944. 0x00, 0x01, 0x03, 0x02, 0x05, 0x01, 0x05, 0x05,
  945. 0x03, 0x07, 0x04, 0x04, 0x03, 0x04, 0x06, 0xCB
  946. }
  947. };
  948. const BYTE cDefHuffVal_ES[8][256] = {
  949. { // DC index 0 (Luminance DC)
  950. 0x08, 0x09, 0x06, 0x07, 0x0A, 0x05, 0x03, 0x04,
  951. 0x0B, 0x02, 0x00, 0x01, 0x0C
  952. },
  953. { // AC index 0 (Luminance AC)
  954. 0x02,0x01,0x03,0x04,0x05,0x06,0x07,0x08,0x12,0x09,0x11,0x13,0x00,0x14,0x21,0x22,
  955. 0x15,0x0a,0x23,0x31,0x16,0x32,0x17,0x24,0x33,0x41,0x18,0x25,0x42,0x51,0x0b,0x26,
  956. 0x19,0x43,0x52,0x61,0x35,0x62,0x71,0x0c,0x0d,0x0e,0x0f,0x10,0x1a,0x1b,0x1c,0x1d,
  957. 0x1e,0x1f,0x20,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x34,0x36,0x37,
  958. 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,
  959. 0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,
  960. 0x5d,0x5e,0x5f,0x60,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
  961. 0x6f,0x70,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
  962. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  963. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  964. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  965. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  966. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  967. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  968. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  969. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  970. },
  971. { // DC index 1 (Chrominance DC)
  972. 0x06, 0x07, 0x05, 0x08, 0x04, 0x09, 0x02, 0x03,
  973. 0x0A, 0x01, 0x0B, 0x00, 0x0C
  974. },
  975. { // AC index 1 (Chrominance AC)
  976. 0x01,0x02,0x03,0x11,0x04,0x21,0x00,0x05,0x06,0x12,0x31,0x41,0x07,0x13,0x22,0x51,
  977. 0x61,0x08,0x14,0x32,0x71,0x81,0x42,0x91,0xa1,0x09,0x15,0x23,0x52,0xb1,0xc1,0xf0,
  978. 0x16,0x62,0xd1,0xe1,0x0a,0x24,0x72,0xf1,0x17,0x82,0x92,0x33,0x43,0x53,0xb2,0x0b,
  979. 0x0c,0x18,0x35,0xa2,0xc2,0x0d,0x0e,0x0f,0x10,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  980. 0x20,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x34,0x36,0x37,
  981. 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,
  982. 0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,
  983. 0x5e,0x5f,0x60,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  984. 0x70,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x80,0x83,
  985. 0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x93,0x94,0x95,
  986. 0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa3,0xa4,0xa5,0xa6,0xa7,
  987. 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,
  988. 0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,
  989. 0xcc,0xcd,0xce,0xcf,0xd0,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,
  990. 0xdd,0xde,0xdf,0xe0,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,
  991. 0xee,0xef,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  992. },
  993. { // DC EX1 (Luminance DC)
  994. 0x08, 0x09, 0x06, 0x07, 0x0A, 0x05, 0x03, 0x04,
  995. 0x0B, 0x02, 0x00, 0x01, 0x0C
  996. },
  997. { // AC EX1 (Luminance AC)
  998. 0x02,0x01,0x03,0x04,0x05,0x06,0x07,0x08,0x12,0x09,0x11,0x13,0x00,0x14,0x21,0x22,
  999. 0x15,0x0a,0x23,0x31,0x16,0x32,0x17,0x24,0x33,0x41,0x18,0x25,0x42,0x51,0x0b,0x26,
  1000. 0x19,0x43,0x52,0x61,0x35,0x62,0x71,0x0c,0x0d,0x0e,0x0f,0x10,0x1a,0x1b,0x1c,0x1d,
  1001. 0x1e,0x1f,0x20,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x34,0x36,0x37,
  1002. 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,
  1003. 0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,
  1004. 0x5d,0x5e,0x5f,0x60,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
  1005. 0x6f,0x70,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
  1006. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  1007. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  1008. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  1009. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  1010. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  1011. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  1012. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  1013. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  1014. },
  1015. { // DC EX2 (Chrominance DC)
  1016. 0x06, 0x07, 0x05, 0x08, 0x04, 0x09, 0x02, 0x03,
  1017. 0x0A, 0x01, 0x0B, 0x00, 0x0C
  1018. },
  1019. { // AC EX2 (Chrominance AC)
  1020. 0x01,0x02,0x03,0x11,0x04,0x21,0x00,0x05,0x06,0x12,0x31,0x41,0x07,0x13,0x22,0x51,
  1021. 0x61,0x08,0x14,0x32,0x71,0x81,0x42,0x91,0xa1,0x09,0x15,0x23,0x52,0xb1,0xc1,0xf0,
  1022. 0x16,0x62,0xd1,0xe1,0x0a,0x24,0x72,0xf1,0x17,0x82,0x92,0x33,0x43,0x53,0xb2,0x0b,
  1023. 0x0c,0x18,0x35,0xa2,0xc2,0x0d,0x0e,0x0f,0x10,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  1024. 0x20,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x34,0x36,0x37,
  1025. 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,
  1026. 0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,
  1027. 0x5e,0x5f,0x60,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  1028. 0x70,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x80,0x83,
  1029. 0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x93,0x94,0x95,
  1030. 0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa3,0xa4,0xa5,0xa6,0xa7,
  1031. 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,
  1032. 0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,
  1033. 0xcc,0xcd,0xce,0xcf,0xd0,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,
  1034. 0xdd,0xde,0xdf,0xe0,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,
  1035. 0xee,0xef,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  1036. }
  1037. };
  1038. enum {
  1039. Marker = 0xFF,
  1040. FF_Marker = 0x00,
  1041. SOI_Marker = 0xFFD8, // Start of image
  1042. EOI_Marker = 0xFFD9, // End of image
  1043. JFIF_CODE = 0xFFE0, // Application
  1044. EXIF_CODE = 0xFFE1,
  1045. DRI_Marker = 0xFFDD, // Define restart interval
  1046. RST_Marker = 0xD, // 0xD0 ~0xD7
  1047. DQT_Marker = 0xFFDB, // Define quantization table(s)
  1048. DHT_Marker = 0xFFC4, // Define Huffman table(s)
  1049. SOF_Marker = 0xFFC0, // Start of frame : Baseline DCT
  1050. SOS_Marker = 0xFFDA, // Start of scan
  1051. SOF_Marker_ES = 0xFFC1, // Start of frame : Extended Sequential
  1052. };
  1053. int check_start_code(JpgDecInfo *jpg)
  1054. {
  1055. if (show_bits(&jpg->gbc, 8) == 0xFF)
  1056. return 1;
  1057. else
  1058. return 0;
  1059. }
  1060. int find_start_code(JpgDecInfo *jpg)
  1061. {
  1062. int word;
  1063. for(;;)
  1064. {
  1065. if (get_bits_left(&jpg->gbc) <= 16)
  1066. {
  1067. ////printf("hit end of stream\n");
  1068. return 0;
  1069. }
  1070. word = show_bits(&jpg->gbc, 16);
  1071. if ((word > 0xFF00) && (word < 0xFFFF))
  1072. break;
  1073. get_bits(&jpg->gbc, 8);
  1074. }
  1075. return word;
  1076. }
  1077. int find_start_soi_code(JpgDecInfo *jpg)
  1078. {
  1079. int word;
  1080. for(;;)
  1081. {
  1082. if (get_bits_left(&jpg->gbc) <= 16)
  1083. {
  1084. ////printf("hit end of stream\n");
  1085. return 0;
  1086. }
  1087. word = show_bits(&jpg->gbc, 16);
  1088. if ((word > 0xFF00) && (word < 0xFFFF))
  1089. {
  1090. if (word != SOI_Marker)
  1091. get_bits(&jpg->gbc, 8);
  1092. break;
  1093. }
  1094. get_bits(&jpg->gbc, 8);
  1095. }
  1096. return word;
  1097. }
  1098. int decode_app_header(JpgDecInfo *jpg)
  1099. {
  1100. int length;
  1101. if (get_bits_left(&jpg->gbc) < 16)
  1102. return 0;
  1103. length = get_bits(&jpg->gbc, 16);
  1104. length -= 2;
  1105. while(length-- > 0)
  1106. {
  1107. if (get_bits_left(&jpg->gbc) < 8)
  1108. return 0;
  1109. get_bits(&jpg->gbc, 8);
  1110. }
  1111. return 1;
  1112. }
  1113. int decode_dri_header(JpgDecInfo *jpg)
  1114. {
  1115. //Length, Lr
  1116. if (get_bits_left(&jpg->gbc) < 16*2)
  1117. return 0;
  1118. get_bits(&jpg->gbc, 16);
  1119. jpg->rstIntval = get_bits(&jpg->gbc, 16);
  1120. return 1;
  1121. }
  1122. int decode_dqt_header(JpgDecInfo *jpg)
  1123. {
  1124. int Pq;
  1125. int Tq;
  1126. int i;
  1127. int tmp;
  1128. if (get_bits_left(&jpg->gbc) < 16)
  1129. return 0;
  1130. // Lq, Length of DQT
  1131. get_bits(&jpg->gbc, 16);
  1132. do {
  1133. if (get_bits_left(&jpg->gbc) < 4+4+8*64)
  1134. return 0;
  1135. // Pq, Quantization Precision
  1136. tmp = get_bits(&jpg->gbc, 8);
  1137. // Tq, Quantization table destination identifier
  1138. Pq = (tmp>>4) & 0xf;
  1139. Tq = tmp&0xf;
  1140. if (Tq == 0)
  1141. jpg->q_prec0 = Pq;
  1142. else if (Tq == 1)
  1143. jpg->q_prec1 = Pq;
  1144. else if (Tq == 2)
  1145. jpg->q_prec2 = Pq;
  1146. else if (Tq == 3)
  1147. jpg->q_prec3 = Pq;
  1148. else
  1149. return 0;
  1150. for (i=0; i<64; i++){
  1151. if (Pq)
  1152. jpg->qMatTab[Tq][i] = (short)get_bits(&jpg->gbc, 16) & 0xffff;
  1153. else
  1154. jpg->qMatTab[Tq][i] = (BYTE)get_bits(&jpg->gbc, 8) & 0x00ff;
  1155. }
  1156. } while(!check_start_code(jpg));
  1157. return 1;
  1158. }
  1159. int decode_dth_header(JpgDecInfo *jpg)
  1160. {
  1161. int Tc;
  1162. int Th;
  1163. int ThTc;
  1164. int bitCnt;
  1165. int i;
  1166. int tmp;
  1167. // Length, Lh
  1168. if (get_bits_left(&jpg->gbc) < 16)
  1169. return 0;
  1170. get_bits(&jpg->gbc, 16);
  1171. do {
  1172. if (get_bits_left(&jpg->gbc) < 8 + 8*16)
  1173. return 0;
  1174. // Table class - DC, AC
  1175. tmp = get_bits(&jpg->gbc, 8);
  1176. // Table destination identifier
  1177. Tc = (tmp>>4) & 0xf;
  1178. Th = tmp&0xf;
  1179. // DC_ID0 (0x00) -> 0
  1180. // AC_ID0 (0x10) -> 1
  1181. // DC_ID1 (0x01) -> 2
  1182. // AC_ID1 (0x11) -> 3
  1183. // DC_EX1 (0x02) -> 4
  1184. // AC_EX1 (0x12) -> 5
  1185. // DC_EX2 (0x03) -> 6
  1186. // AC_EX2 (0x13) -> 7
  1187. //Since only DC_ID0, DC_ID1, if th is 2 or 3, always set userHuffTab
  1188. /* According to ITU-T.81 Table B.5, maximum Tc value is 1 and maximum Th value is 3.*/
  1189. if (Tc > 1 || Th > 3) {
  1190. return 0;
  1191. }
  1192. ThTc = ((Th&3)<<1) | (Tc&1);
  1193. if (jpg->numHuffmanTable >= THTC_LIST_CNT)
  1194. return 0;
  1195. jpg->thtc[jpg->numHuffmanTable++] = ThTc;
  1196. // Get Huff Bits list
  1197. bitCnt = 0;
  1198. for (i=0; i<16; i++) {
  1199. jpg->huffBits[ThTc][i] = (BYTE)get_bits(&jpg->gbc, 8);
  1200. bitCnt += jpg->huffBits[ThTc][i];
  1201. }
  1202. if (bitCnt > 256)
  1203. return 0;
  1204. if (get_bits_left(&jpg->gbc) < 8*bitCnt)
  1205. return 0;
  1206. // Get Huff Val list
  1207. for (i=0; i<bitCnt; i++) {
  1208. jpg->huffVal[ThTc][i] = (BYTE)get_bits(&jpg->gbc, 8);
  1209. }
  1210. } while(!check_start_code(jpg));
  1211. return 1;
  1212. }
  1213. int decode_sof_header(JpgDecInfo *jpg)
  1214. {
  1215. int samplePrecision;
  1216. int sampleFactor;
  1217. int i;
  1218. int Tqi;
  1219. BYTE compID;
  1220. int hSampFact[3] = {0,};
  1221. int vSampFact[3] = {0,};
  1222. int picX, picY;
  1223. int numComp;
  1224. int tmp;
  1225. if (get_bits_left(&jpg->gbc) < 16+8+16+16+8)
  1226. return 0;
  1227. // LF, Length of SOF
  1228. get_bits(&jpg->gbc, 16);
  1229. // Sample Precision: Baseline(8), P
  1230. samplePrecision = get_bits(&jpg->gbc, 8);
  1231. jpg->bitDepth = samplePrecision;
  1232. jpg->jpg12bit = (samplePrecision == 12) ? 1 : 0;
  1233. picY = get_bits(&jpg->gbc, 16);
  1234. if (picY > MAX_MJPG_PIC_WIDTH)
  1235. {
  1236. //printf("Picture Vertical Size limits Maximum size\n");
  1237. return 0;
  1238. }
  1239. picX = get_bits(&jpg->gbc, 16);
  1240. if (picX > MAX_MJPG_PIC_HEIGHT)
  1241. {
  1242. //printf("Picture Horizontal Size limits Maximum size\n");
  1243. return 0;
  1244. }
  1245. //Number of Components in Frame: Nf
  1246. numComp = get_bits(&jpg->gbc, 8);
  1247. if (numComp > 3)
  1248. {
  1249. //printf("Picture Horizontal Size limits Maximum size\n");
  1250. return 0;
  1251. }
  1252. if (get_bits_left(&jpg->gbc) < numComp*(8+4+4+8))
  1253. return 0;
  1254. for (i=0; i<numComp; i++)
  1255. {
  1256. // Component ID, Ci 0 ~ 255
  1257. compID = (BYTE)get_bits(&jpg->gbc, 8);
  1258. tmp = get_bits(&jpg->gbc, 8);
  1259. // Horizontal Sampling Factor, Hi
  1260. hSampFact[i] = (tmp>>4) & 0xf;
  1261. // Vertical Sampling Factor, Vi
  1262. vSampFact[i] = tmp&0xf;
  1263. // Quantization Table Selector, Tqi
  1264. Tqi = get_bits(&jpg->gbc, 8);
  1265. jpg->cInfoTab[i][0] = compID;
  1266. jpg->cInfoTab[i][1] = (BYTE)hSampFact[i];
  1267. jpg->cInfoTab[i][2] = (BYTE)vSampFact[i];
  1268. jpg->cInfoTab[i][3] = (BYTE)Tqi;
  1269. }
  1270. //if ( hSampFact[0]>2 || vSampFact[0]>2 || ( numComp == 3 && ( hSampFact[1]!=1 || hSampFact[2]!=1 || vSampFact[1]!=1 || vSampFact[2]!=1) ) )
  1271. //printf("Not Supported Sampling Factor\n");
  1272. if (numComp == 1)
  1273. sampleFactor = SAMPLE_400;
  1274. else
  1275. sampleFactor = ((hSampFact[0]&3)<<2) | (vSampFact[0]&3);
  1276. switch(sampleFactor) {
  1277. case SAMPLE_420:
  1278. jpg->format = FORMAT_420;
  1279. break;
  1280. case SAMPLE_H422:
  1281. jpg->format = FORMAT_422;
  1282. break;
  1283. case SAMPLE_V422:
  1284. jpg->format = FORMAT_440;
  1285. break;
  1286. case SAMPLE_444:
  1287. jpg->format = FORMAT_444;
  1288. break;
  1289. default: // 4:0:0
  1290. jpg->format = FORMAT_400;
  1291. break;
  1292. }
  1293. jpg->picWidth = picX;
  1294. jpg->picHeight = picY;
  1295. return 1;
  1296. }
  1297. int decode_sos_header(JpgDecInfo *jpg)
  1298. {
  1299. int i, j;
  1300. int len;
  1301. int numComp;
  1302. int compID;
  1303. int ecsPtr;
  1304. int ss, se, ah, al;
  1305. int dcHufTblIdx[3] = {0,};
  1306. int acHufTblIdx[3] = {0,};
  1307. int tmp;
  1308. if (get_bits_left(&jpg->gbc) < 8)
  1309. return 0;
  1310. // Length, Ls
  1311. len = get_bits(&jpg->gbc, 16);
  1312. jpg->ecsPtr = get_bits_count(&jpg->gbc)/8 + len - 2 ;
  1313. ecsPtr = jpg->ecsPtr+jpg->frameOffset;
  1314. //printf("ecsPtr=0x%x frameOffset=0x%x, ecsOffset=0x%x, wrPtr=0x%x, rdPtr0x%x\n", jpg->ecsPtr, jpg->frameOffset, ecsPtr, jpg->streamWrPtr, jpg->streamRdPtr);
  1315. jpg->pagePtr = ecsPtr >> 8; //page unit ecsPtr/256;
  1316. jpg->wordPtr = (ecsPtr & 0xF0) >> 2; // word unit ((ecsPtr % 256) & 0xF0) / 4;
  1317. if (jpg->pagePtr & 1)
  1318. jpg->wordPtr += 64;
  1319. if (jpg->wordPtr & 1)
  1320. jpg->wordPtr -= 1; // to make even.
  1321. jpg->bitPtr = (ecsPtr & 0xF) << 3; // bit unit (ecsPtr & 0xF) * 8;
  1322. if (get_bits_left(&jpg->gbc) < 8)
  1323. return 0;
  1324. //Number of Components in Scan: Ns
  1325. numComp = get_bits(&jpg->gbc, 8);
  1326. if (numComp > 3)
  1327. return 0;
  1328. if (get_bits_left(&jpg->gbc) < numComp*(8+4+4))
  1329. return 0;
  1330. for (i=0; i<numComp; i++) {
  1331. // Component ID, Csj 0 ~ 255
  1332. compID = get_bits(&jpg->gbc, 8);
  1333. tmp = get_bits(&jpg->gbc, 8);
  1334. // dc entropy coding table selector, Tdj
  1335. dcHufTblIdx[i] = (tmp>>4) & 0xf;
  1336. // ac entropy coding table selector, Taj
  1337. acHufTblIdx[i] = tmp&0xf;
  1338. for (j=0; j<numComp; j++)
  1339. {
  1340. if (compID == jpg->cInfoTab[j][0])
  1341. {
  1342. jpg->cInfoTab[j][4] = (BYTE)dcHufTblIdx[i];
  1343. jpg->cInfoTab[j][5] = (BYTE)acHufTblIdx[i];
  1344. }
  1345. }
  1346. }
  1347. if (get_bits_left(&jpg->gbc) < 8+8+4+4)
  1348. return 0;
  1349. // Ss 0
  1350. ss = get_bits(&jpg->gbc, 8);
  1351. // Se 3F
  1352. se = get_bits(&jpg->gbc, 8);
  1353. tmp = get_bits(&jpg->gbc, 8);
  1354. // Ah 0
  1355. ah = (i>>4) & 0xf;
  1356. // Al 0
  1357. al = tmp&0xf;
  1358. if ((ss != 0) || (se != 0x3F) || (ah != 0) || (al != 0))
  1359. {
  1360. //printf("The Jpeg Image must be another profile\n");
  1361. return 0;
  1362. }
  1363. return 1;
  1364. }
  1365. static void genDecHuffTab(JpgDecInfo *jpg, int tabNum)
  1366. {
  1367. unsigned char *huffPtr, *huffBits;
  1368. Uint32 *huffMax, *huffMin;
  1369. int ptrCnt =0;
  1370. int huffCode = 0;
  1371. int zeroFlag = 0;
  1372. int dataFlag = 0;
  1373. int i;
  1374. huffBits = jpg->huffBits[tabNum];
  1375. huffPtr = jpg->huffPtr[tabNum];
  1376. huffMax = jpg->huffMax[tabNum];
  1377. huffMin = jpg->huffMin[tabNum];
  1378. for (i=0; i<16; i++)
  1379. {
  1380. if (huffBits[i]) // if there is bit cnt value
  1381. {
  1382. huffPtr[i] = (BYTE)ptrCnt;
  1383. ptrCnt += huffBits[i];
  1384. huffMin[i] = huffCode;
  1385. huffMax[i] = huffCode + (huffBits[i] - 1);
  1386. dataFlag = 1;
  1387. zeroFlag = 0;
  1388. }
  1389. else
  1390. {
  1391. huffPtr[i] = 0xFF;
  1392. huffMin[i] = 0xFFFF;
  1393. huffMax[i] = 0xFFFF;
  1394. zeroFlag = 1;
  1395. }
  1396. if (dataFlag == 1)
  1397. {
  1398. if (zeroFlag == 1)
  1399. huffCode <<= 1;
  1400. else
  1401. huffCode = (huffMax[i] + 1) << 1;
  1402. }
  1403. }
  1404. }
  1405. int JpuGbuInit(vpu_getbit_context_t *ctx, BYTE *buffer, int size)
  1406. {
  1407. ctx->buffer = buffer;
  1408. ctx->index = 0;
  1409. ctx->size = size/8;
  1410. return 1;
  1411. }
  1412. int JpuGbuGetUsedBitCount(vpu_getbit_context_t *ctx)
  1413. {
  1414. return ctx->index*8;
  1415. }
  1416. int JpuGbuGetLeftBitCount(vpu_getbit_context_t *ctx)
  1417. {
  1418. return (ctx->size*8) - JpuGbuGetUsedBitCount(ctx);
  1419. }
  1420. unsigned int JpuGbuGetBit(vpu_getbit_context_t *ctx, int bit_num)
  1421. {
  1422. BYTE *p;
  1423. unsigned int b = 0x0;
  1424. if (bit_num > JpuGbuGetLeftBitCount(ctx))
  1425. return (unsigned int)-1;
  1426. p = ctx->buffer + ctx->index;
  1427. if (bit_num == 8)
  1428. {
  1429. b = *p;
  1430. ctx->index++;
  1431. }
  1432. else if(bit_num == 16)
  1433. {
  1434. b = *p++<<8;
  1435. b |= *p++;
  1436. ctx->index += 2;
  1437. }
  1438. else if(bit_num == 32)
  1439. {
  1440. b = *p++<<24;
  1441. b |= (*p++<<16);
  1442. b |= (*p++<<8);
  1443. b |= (*p++<<0);
  1444. ctx->index += 4;
  1445. }
  1446. return b;
  1447. }
  1448. unsigned int JpuGguShowBit(vpu_getbit_context_t *ctx, int bit_num)
  1449. {
  1450. BYTE *p;
  1451. unsigned int b = 0x0;
  1452. if (bit_num > JpuGbuGetLeftBitCount(ctx))
  1453. return (unsigned int)-1;
  1454. p = ctx->buffer + ctx->index;
  1455. if (bit_num == 8)
  1456. {
  1457. b = *p;
  1458. }
  1459. else if(bit_num == 16)
  1460. {
  1461. b = *p++<<8;
  1462. b |= *p++;
  1463. }
  1464. else if(bit_num == 32)
  1465. {
  1466. b = *p++<<24;
  1467. b |= (*p++<<16);
  1468. b |= (*p++<<8);
  1469. b |= (*p++<<0);
  1470. }
  1471. return b;
  1472. }
  1473. static int wraparound_bistream_data(JpgDecInfo *jpg, int return_type)
  1474. {
  1475. BYTE *dst;
  1476. BYTE *src;
  1477. BYTE *data;
  1478. int data_size;
  1479. int src_size;
  1480. int dst_size;
  1481. data_size = jpg->streamWrPtr - jpg->streamBufStartAddr;
  1482. data = (BYTE *)malloc(data_size);
  1483. if (data)
  1484. JpuReadMem(jpg->streamBufStartAddr, data, data_size, jpg->streamEndian);
  1485. src_size = jpg->streamBufSize - jpg->frameOffset;
  1486. src = (BYTE *)malloc(src_size);
  1487. dst_size = ((src_size+(JPU_GBU_SIZE-1))&~(JPU_GBU_SIZE-1));
  1488. dst = (BYTE *)malloc(dst_size);
  1489. if (dst && src)
  1490. {
  1491. memset(dst, 0x00, dst_size);
  1492. JpuReadMem(jpg->streamBufStartAddr+jpg->frameOffset, src, src_size, jpg->streamEndian);
  1493. memcpy(dst+(dst_size-src_size), src, src_size);
  1494. JpuWriteMem(jpg->streamBufStartAddr, dst, dst_size, jpg->streamEndian);
  1495. if (data)
  1496. JpuWriteMem(jpg->streamBufStartAddr+dst_size, data, data_size, jpg->streamEndian);
  1497. }
  1498. if(src)
  1499. free(src);
  1500. if(dst)
  1501. free(dst);
  1502. if (data)
  1503. free(data);
  1504. if (return_type == -2) {// header wraparound
  1505. jpg->streamWrPtr = jpg->streamBufStartAddr+dst_size+data_size;
  1506. jpg->consumeByte = 0;
  1507. return -2;
  1508. }
  1509. else if(return_type == -1){ // ecsPtr wraparound
  1510. jpg->streamWrPtr = jpg->streamBufStartAddr+dst_size+data_size;
  1511. jpg->frameOffset = 0;
  1512. return -1;
  1513. }
  1514. return 0;
  1515. }
  1516. static void CheckUserHuffmanTable(JpgDecInfo* jpg)
  1517. {
  1518. Uint32 ThTc, i, bitCnt;
  1519. for (ThTc=0; ThTc<THTC_LIST_CNT; ThTc++) {
  1520. if (jpg->thtc[ThTc] == -1) break;
  1521. bitCnt = 0;
  1522. for (i=0; i<16; i++) {
  1523. bitCnt += jpg->huffBits[ThTc][i];
  1524. if (jpg->jpg12bit) {
  1525. jpg->userHuffTab = (cDefHuffBits_ES[ThTc][i] != jpg->huffBits[ThTc][i]);
  1526. }
  1527. else {
  1528. jpg->userHuffTab = (cDefHuffBits[ThTc][i] != jpg->huffBits[ThTc][i]);
  1529. }
  1530. if (jpg->userHuffTab == TRUE) return;
  1531. }
  1532. if (bitCnt > 256)
  1533. return;
  1534. // Get Huff Val list
  1535. for (i=0; i<bitCnt; i++) {
  1536. if (jpg->jpg12bit) {
  1537. jpg->userHuffTab = (cDefHuffVal_ES[ThTc][i] != jpg->huffVal[ThTc][i]);
  1538. }
  1539. else {
  1540. jpg->userHuffTab = (cDefHuffVal[ThTc][i] != jpg->huffVal[ThTc][i]);
  1541. }
  1542. if (jpg->userHuffTab == TRUE) return;
  1543. }
  1544. }
  1545. }
  1546. int JpegDecodeHeader(JpgDecInfo *jpg)
  1547. {
  1548. unsigned int code;
  1549. int ret;
  1550. int i;
  1551. int temp;
  1552. int wrOffset;
  1553. BYTE *b = jpg->pBitStream+jpg->frameOffset;
  1554. int size;
  1555. BOOL yuv400_4Blocks = TRUE; /* process 4block at a time for improving performance */
  1556. for (i=0; i<THTC_LIST_CNT; i++) {
  1557. jpg->thtc[i] = -1;
  1558. }
  1559. jpg->numHuffmanTable = 0;
  1560. ret = 1;
  1561. if (jpg->streamWrPtr == jpg->streamBufStartAddr)
  1562. {
  1563. size = jpg->streamBufSize-jpg->frameOffset;
  1564. wrOffset = jpg->streamBufSize;
  1565. }
  1566. else
  1567. {
  1568. if (jpg->frameOffset >= (int)(jpg->streamWrPtr-jpg->streamBufStartAddr))
  1569. size = jpg->streamBufSize - jpg->frameOffset;
  1570. else
  1571. size = (jpg->streamWrPtr-jpg->streamBufStartAddr)-jpg->frameOffset;
  1572. wrOffset = (jpg->streamWrPtr-jpg->streamBufStartAddr);
  1573. }
  1574. if (!b || !size) {
  1575. ret = -1;
  1576. goto DONE_DEC_HEADER;
  1577. }
  1578. // find start code of next frame
  1579. if (!jpg->ecsPtr)
  1580. {
  1581. int nextOffset = 0;
  1582. int soiOffset = 0;
  1583. if (jpg->consumeByte != 0) // meaning is frameIdx > 0
  1584. {
  1585. nextOffset = jpg->consumeByte;
  1586. if (nextOffset <= 0)
  1587. nextOffset = 2; //in order to consume start code.
  1588. }
  1589. //consume to find the start code of next frame.
  1590. b += nextOffset;
  1591. size -= nextOffset;
  1592. if (size < 0)
  1593. {
  1594. jpg->consumeByte -= (b - (jpg->pBitStream+jpg->streamBufSize));
  1595. if (jpg->consumeByte < 0) {
  1596. ret = 0;
  1597. goto DONE_DEC_HEADER;
  1598. }
  1599. ret = -1;
  1600. goto DONE_DEC_HEADER;
  1601. }
  1602. init_get_bits(&jpg->gbc, b, size*8);
  1603. for (;;)
  1604. {
  1605. code = find_start_soi_code(jpg);
  1606. if (code == 0)
  1607. {
  1608. ret = -1;
  1609. goto DONE_DEC_HEADER;
  1610. }
  1611. if (code == SOI_Marker)
  1612. break;
  1613. }
  1614. soiOffset = get_bits_count(&jpg->gbc)/8;
  1615. b += soiOffset;
  1616. size -= soiOffset;
  1617. jpg->frameOffset += (soiOffset+ nextOffset);
  1618. }
  1619. if (jpg->headerSize > 0 && (jpg->headerSize > (jpg->streamBufSize - jpg->frameOffset - JPU_GBU_SIZE))) { // if header size is smaller than room of stream end. copy the buffer to bistream start.
  1620. return wraparound_bistream_data(jpg, -2);
  1621. }
  1622. init_get_bits(&jpg->gbc, b, size*8);
  1623. // Initialize component information table
  1624. for (i=0; i<4; i++)
  1625. {
  1626. jpg->cInfoTab[i][0] = 0;
  1627. jpg->cInfoTab[i][1] = 0;
  1628. jpg->cInfoTab[i][2] = 0;
  1629. jpg->cInfoTab[i][3] = 0;
  1630. jpg->cInfoTab[i][4] = 0;
  1631. jpg->cInfoTab[i][5] = 0;
  1632. }
  1633. for (;;)
  1634. {
  1635. if (find_start_code(jpg) == 0)
  1636. {
  1637. ret = -1;
  1638. goto DONE_DEC_HEADER;
  1639. }
  1640. code = get_bits(&jpg->gbc, 16); //getbit 2byte
  1641. switch (code) {
  1642. case SOI_Marker:
  1643. break;
  1644. case JFIF_CODE:
  1645. case EXIF_CODE:
  1646. if (!decode_app_header(jpg))
  1647. {
  1648. ret = -1;
  1649. goto DONE_DEC_HEADER;
  1650. }
  1651. break;
  1652. case DRI_Marker:
  1653. if (!decode_dri_header(jpg))
  1654. {
  1655. ret = -1;
  1656. goto DONE_DEC_HEADER;
  1657. }
  1658. break;
  1659. case DQT_Marker:
  1660. if (!decode_dqt_header(jpg))
  1661. {
  1662. ret = -1;
  1663. goto DONE_DEC_HEADER;
  1664. }
  1665. break;
  1666. case DHT_Marker:
  1667. if (!decode_dth_header(jpg))
  1668. {
  1669. ret = -1;
  1670. goto DONE_DEC_HEADER;
  1671. }
  1672. break;
  1673. case SOF_Marker:
  1674. case SOF_Marker_ES:
  1675. if (!decode_sof_header(jpg))
  1676. {
  1677. ret = -1;
  1678. goto DONE_DEC_HEADER;
  1679. }
  1680. break;
  1681. case SOS_Marker:
  1682. if (!decode_sos_header(jpg))
  1683. {
  1684. ret = -1;
  1685. goto DONE_DEC_HEADER;
  1686. }
  1687. if (!jpg->headerSize)
  1688. jpg->headerSize = jpg->ecsPtr; // we assume header size of all frame is same for mjpeg case
  1689. goto DONE_DEC_HEADER;
  1690. case EOI_Marker:
  1691. goto DONE_DEC_HEADER;
  1692. default:
  1693. switch (code&0xFFF0)
  1694. {
  1695. case 0xFFE0: // 0xFFEX
  1696. case 0xFFF0: // 0xFFFX
  1697. if (get_bits_left(&jpg->gbc) <=0 ) {
  1698. {
  1699. ret = -1;
  1700. goto DONE_DEC_HEADER;
  1701. }
  1702. }
  1703. else
  1704. {
  1705. if (!decode_app_header(jpg))
  1706. {
  1707. ret = -1;
  1708. goto DONE_DEC_HEADER;
  1709. }
  1710. break;
  1711. }
  1712. default:
  1713. //in case, restart marker is founded.
  1714. if( (code&0xFFF0) >= 0xFFD0 && (code&0xFFF0) <= 0xFFD7)
  1715. break;
  1716. else
  1717. //printf("code = [%x]\n", code);
  1718. return 0;
  1719. }
  1720. break;
  1721. }
  1722. }
  1723. DONE_DEC_HEADER:
  1724. if (ret == -1)
  1725. {
  1726. if (wrOffset < jpg->frameOffset)
  1727. return -2;
  1728. if (0 == get_bits_left(&jpg->gbc)) {
  1729. return -2;
  1730. }
  1731. return -1;
  1732. }
  1733. if (!jpg->ecsPtr)
  1734. return 0;
  1735. if (wrOffset - (jpg->frameOffset+jpg->ecsPtr) < JPU_GBU_SIZE &&
  1736. jpg->streamEndflag == 0) {
  1737. return -1;
  1738. }
  1739. // this bellow is workaround to avoid the case that JPU is run over without interrupt.
  1740. if (jpg->streamBufSize - (jpg->frameOffset+jpg->ecsPtr) < JPU_GBU_SIZE)
  1741. return wraparound_bistream_data(jpg, -1);
  1742. CheckUserHuffmanTable(jpg);
  1743. // Generate Huffman table information
  1744. if (jpg->jpg12bit){
  1745. for(i=0; i<8; i++)
  1746. genDecHuffTab(jpg, i);
  1747. }else{
  1748. for(i=0; i<4; i++)
  1749. genDecHuffTab(jpg, i);
  1750. }
  1751. // Q Idx
  1752. temp = jpg->cInfoTab[0][3];
  1753. temp = temp << 1 | jpg->cInfoTab[1][3];
  1754. temp = temp << 1 | jpg->cInfoTab[2][3];
  1755. jpg->Qidx = temp;
  1756. // Huff Idx[DC, AC]
  1757. temp = jpg->cInfoTab[0][4];
  1758. if (jpg->jpg12bit == 0){
  1759. temp = temp << 1 | jpg->cInfoTab[1][4];
  1760. temp = temp << 1 | jpg->cInfoTab[2][4];
  1761. }else{
  1762. temp = temp << 2 | jpg->cInfoTab[1][4];
  1763. temp = temp << 2 | jpg->cInfoTab[2][4];
  1764. }
  1765. jpg->huffDcIdx = temp;
  1766. temp = jpg->cInfoTab[0][5];
  1767. if (jpg->jpg12bit == 0){
  1768. temp = temp << 1 | jpg->cInfoTab[1][5];
  1769. temp = temp << 1 | jpg->cInfoTab[2][5];
  1770. }else{
  1771. temp = temp << 2 | jpg->cInfoTab[1][5];
  1772. temp = temp << 2 | jpg->cInfoTab[2][5];
  1773. }
  1774. jpg->huffAcIdx = temp;
  1775. switch(jpg->format)
  1776. {
  1777. case FORMAT_420:
  1778. jpg->mcuBlockNum = 6;
  1779. jpg->compNum = 3;
  1780. jpg->compInfo[1] = 5;
  1781. jpg->compInfo[2] = 5;
  1782. jpg->alignedWidth = ((jpg->picWidth+15)&~15);
  1783. jpg->alignedHeight = ((jpg->picHeight+15)&~15);
  1784. jpg->mcuWidth = 16;
  1785. jpg->mcuHeight = 16;
  1786. break;
  1787. case FORMAT_422:
  1788. jpg->mcuBlockNum = 4;
  1789. jpg->compNum = 3;
  1790. jpg->compInfo[1] = 5;
  1791. jpg->compInfo[2] = 5;
  1792. jpg->alignedWidth = ((jpg->picWidth+15)&~15);
  1793. jpg->alignedHeight = ((jpg->picHeight+7)&~7);
  1794. jpg->mcuWidth = 16;
  1795. jpg->mcuHeight = 8;
  1796. break;
  1797. case FORMAT_440:
  1798. jpg->mcuBlockNum = 4;
  1799. jpg->compNum = 3;
  1800. jpg->compInfo[1] = 5;
  1801. jpg->compInfo[2] = 5;
  1802. jpg->alignedWidth = ((jpg->picWidth+7)&~7);
  1803. jpg->alignedHeight = ((jpg->picHeight+15)&~15);
  1804. jpg->mcuWidth = 8;
  1805. jpg->mcuHeight = 16;
  1806. break;
  1807. case FORMAT_444:
  1808. jpg->mcuBlockNum = 3;
  1809. jpg->compNum = 3;
  1810. jpg->compInfo[1] = 5;
  1811. jpg->compInfo[2] = 5;
  1812. jpg->alignedWidth = ((jpg->picWidth+7)&~7);
  1813. jpg->alignedHeight = ((jpg->picHeight+7)&~7);
  1814. jpg->mcuWidth = 8;
  1815. jpg->mcuHeight = 8;
  1816. break;
  1817. case FORMAT_400:
  1818. jpg->compNum = 1;
  1819. jpg->compInfo[1] = 0;
  1820. jpg->compInfo[2] = 0;
  1821. jpg->alignedWidth = ((jpg->picWidth+7)&~7);
  1822. jpg->alignedHeight = ((jpg->picHeight+7)&~7);
  1823. if (jpg->roiEnable == TRUE) {
  1824. Uint32 offsetX = JPU_FLOOR(8, jpg->roiOffsetX);
  1825. yuv400_4Blocks = (BOOL)((offsetX%32) == 0);
  1826. }
  1827. if (yuv400_4Blocks == TRUE) {
  1828. jpg->mcuBlockNum = 4;
  1829. jpg->mcuWidth = 32;
  1830. jpg->mcuHeight = 8;
  1831. }
  1832. else {
  1833. jpg->mcuBlockNum = 1;
  1834. jpg->mcuWidth = 8;
  1835. jpg->mcuHeight = 8;
  1836. }
  1837. break;
  1838. default:
  1839. return 0;
  1840. }
  1841. jpg->compInfo[0] = (jpg->mcuWidth >> 3) << 3 | (jpg->mcuHeight >> 3);
  1842. jpg->busReqNum = (jpg->jpg12bit == FALSE) ? GetDec8bitBusReqNum(jpg->format, jpg->packedFormat) :
  1843. GetDec12bitBusReqNum(jpg->format, jpg->packedFormat);
  1844. return 1;
  1845. }
  1846. JpgRet CheckJpgEncOpenParam(JpgEncOpenParam * pop, JPUCap* cap)
  1847. {
  1848. int picWidth;
  1849. int picHeight;
  1850. if (pop == 0) {
  1851. return JPG_RET_INVALID_PARAM;
  1852. }
  1853. picWidth = pop->picWidth;
  1854. picHeight = pop->picHeight;
  1855. if (pop->bitstreamBuffer % 8) {
  1856. return JPG_RET_INVALID_PARAM;
  1857. }
  1858. if ((pop->bitstreamBufferSize - (4096)) < 4096) {
  1859. return JPG_RET_INVALID_PARAM;
  1860. }
  1861. if ((pop->bitstreamBufferSize - (4096)) % 4096) {
  1862. return JPG_RET_INVALID_PARAM;
  1863. }
  1864. if (picWidth < 16 || picWidth > MAX_MJPG_PIC_WIDTH ) {
  1865. return JPG_RET_INVALID_PARAM;
  1866. }
  1867. if (picHeight < 16 || picHeight > MAX_MJPG_PIC_HEIGHT ) {
  1868. return JPG_RET_INVALID_PARAM;
  1869. }
  1870. if (pop->intrEnableBit == 0) {
  1871. return JPG_RET_INVALID_PARAM;
  1872. }
  1873. if (pop->jpg12bit == TRUE && cap->support12bit == FALSE) {
  1874. return JPG_RET_NOT_SUPPORT;
  1875. }
  1876. return JPG_RET_SUCCESS;
  1877. }
  1878. JpgRet CheckJpgEncParam(JpgEncHandle handle, JpgEncParam * param)
  1879. {
  1880. JpgInst *pJpgInst;
  1881. JpgEncInfo *pEncInfo;
  1882. pJpgInst = handle;
  1883. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1884. if (param == 0) {
  1885. return JPG_RET_INVALID_PARAM;
  1886. }
  1887. if (pEncInfo->packedFormat != PACKED_FORMAT_NONE) {
  1888. if (pEncInfo->packedFormat == PACKED_FORMAT_444) {
  1889. if (param->sourceFrame->stride < pEncInfo->picWidth*2) {
  1890. return JPG_RET_INVALID_PARAM;
  1891. }
  1892. }
  1893. if (pEncInfo->packedFormat == PACKED_FORMAT_444) {
  1894. if (param->sourceFrame->stride < pEncInfo->picWidth*3) {
  1895. return JPG_RET_INVALID_PARAM;
  1896. }
  1897. }
  1898. }
  1899. return JPG_RET_SUCCESS;
  1900. }
  1901. int JpgEncGenHuffTab(JpgEncInfo * pEncInfo, int tabNum)
  1902. {
  1903. int p, i, l, lastp, si, maxsymbol;
  1904. int huffsize[256] = {0,};
  1905. int huffcode[256] = {0,};
  1906. int code;
  1907. BYTE *bitleng, *huffval;
  1908. Uint32 *ehufco, *ehufsi;
  1909. if (tabNum > 3)
  1910. return 0;
  1911. bitleng = pEncInfo->pHuffBits[tabNum];
  1912. huffval = pEncInfo->pHuffVal[tabNum];
  1913. ehufco = pEncInfo->huffCode[tabNum];
  1914. ehufsi = pEncInfo->huffSize[tabNum];
  1915. maxsymbol = tabNum & 1 ? 256 : 16;
  1916. /* Figure C.1: make table of Huffman code length for each symbol */
  1917. p = 0;
  1918. for (l=1; l<=16; l++) {
  1919. i = bitleng[l-1];
  1920. if (i < 0 || p + i > maxsymbol)
  1921. return 0;
  1922. while (i--)
  1923. huffsize[p++] = l;
  1924. }
  1925. lastp = p;
  1926. /* Figure C.2: generate the codes themselves */
  1927. /* We also validate that the counts represent a legal Huffman code tree. */
  1928. code = 0;
  1929. si = huffsize[0];
  1930. p = 0;
  1931. while(p < lastp){
  1932. while (huffsize[p] == si) {
  1933. huffcode[p++] = code;
  1934. code++;
  1935. }
  1936. if (code >= (1 << si))
  1937. return 0;
  1938. code <<= 1;
  1939. si++;
  1940. }
  1941. memset(ehufsi, 0, sizeof(int) * 256);
  1942. memset(ehufco, 0, sizeof(int) * 256);
  1943. /* Figure C.3: generate encoding tables */
  1944. /* These are code and size indexed by symbol value */
  1945. for(i=0; i<256; i++)
  1946. ehufsi[i] = 0x00;
  1947. for(i=0; i<256; i++)
  1948. ehufco[i] = 0x00;
  1949. for (p=0; p<lastp; p++) {
  1950. i = huffval[p];
  1951. if (i < 0 || i >= maxsymbol || ehufsi[i])
  1952. return 0;
  1953. ehufco[i] = huffcode[p];
  1954. ehufsi[i] = huffsize[p];
  1955. }
  1956. return 1;
  1957. }
  1958. int JpgEncLoadHuffTab(JpgInst *pJpgInst, int instRegIndex)
  1959. {
  1960. int i, j, t;
  1961. int huffData;
  1962. JpgEncInfo * pEncInfo;
  1963. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1964. for (i=0; i<4; i++)
  1965. JpgEncGenHuffTab(pEncInfo, i);
  1966. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x3);
  1967. for (j=0; j<4; j++)
  1968. {
  1969. t = (j==0) ? AC_TABLE_INDEX0 : (j==1) ? AC_TABLE_INDEX1 : (j==2) ? DC_TABLE_INDEX0 : DC_TABLE_INDEX1;
  1970. for (i=0; i<256; i++)
  1971. {
  1972. if ((t==DC_TABLE_INDEX0 || t==DC_TABLE_INDEX1) && (i>15)) // DC
  1973. break;
  1974. if ((pEncInfo->huffSize[t][i] == 0) && (pEncInfo->huffCode[t][i] == 0))
  1975. huffData = 0;
  1976. else
  1977. {
  1978. huffData = (pEncInfo->huffSize[t][i] - 1); // Code length (1 ~ 16), 4-bit
  1979. huffData = (huffData << 16) | (pEncInfo->huffCode[t][i] ); // Code word, 16-bit
  1980. }
  1981. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, huffData);
  1982. }
  1983. }
  1984. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x0);
  1985. return 1;
  1986. }
  1987. int JpgEncLoadHuffTab_12b(JpgInst *pJpgInst, int instRegIndex)
  1988. {
  1989. int i, j, t;
  1990. int huffData;
  1991. JpgEncInfo * pEncInfo;
  1992. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  1993. for (i=0; i<8; i++)
  1994. JpgEncGenHuffTab(pEncInfo, i);
  1995. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x3);
  1996. for (j=0; j<8; j++)
  1997. {
  1998. t = (j==0) ? AC_TABLE_INDEX0 : (j==1) ? AC_TABLE_INDEX1 : (j==2) ? DC_TABLE_INDEX0 : (j==3)? DC_TABLE_INDEX1 : (j==4) ? AC_TABLE_INDEX2 : (j==5) ? AC_TABLE_INDEX3 : (j==6) ? DC_TABLE_INDEX2 : DC_TABLE_INDEX3;
  1999. for (i=0; i<256; i++)
  2000. {
  2001. //only support 3 huffman tables
  2002. if (t==DC_TABLE_INDEX3 || t==AC_TABLE_INDEX3)
  2003. break;
  2004. if ((t==DC_TABLE_INDEX0 || t==DC_TABLE_INDEX1 || t==DC_TABLE_INDEX2 || t==DC_TABLE_INDEX3) && (i>15)) // DC
  2005. break;
  2006. if ((pEncInfo->huffSize[t][i] == 0) && (pEncInfo->huffCode[t][i] == 0))
  2007. huffData = 0;
  2008. else
  2009. {
  2010. huffData = (pEncInfo->huffSize[t][i] - 1); // Code length (1 ~ 16), 4-bit
  2011. huffData = (huffData << 16) | (pEncInfo->huffCode[t][i] ); // Code word, 16-bit
  2012. }
  2013. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_DATA_REG, huffData);
  2014. }
  2015. }
  2016. JpuWriteInstReg(instRegIndex, MJPEG_HUFF_CTRL_REG, 0x0);
  2017. return 1;
  2018. }
  2019. int JpgEncLoadQMatTab(JpgInst *pJpgInst, int instRegIndex)
  2020. {
  2021. #ifdef WIN32
  2022. __int64 dividend = 0x80000;
  2023. __int64 quotient;
  2024. __int64 dividend_32b = 0x80000000;
  2025. #else
  2026. long long int dividend = 0x80000;
  2027. long long int quotient;
  2028. long long int dividend_32b = 0x80000000;
  2029. #endif
  2030. int quantID;
  2031. int divisor;
  2032. int comp;
  2033. int i, t;
  2034. int qprec = 0;
  2035. JpgEncInfo * pEncInfo;
  2036. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  2037. for (comp=0; comp<3; comp++) {
  2038. quantID = pEncInfo->pCInfoTab[comp][3];
  2039. if (quantID >= 4)
  2040. return 0;
  2041. t = (comp==0)? Q_COMPONENT0 :
  2042. (comp==1)? Q_COMPONENT1 : Q_COMPONENT2;
  2043. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, 0x3 + t);
  2044. if (pEncInfo->jpg12bit != 0){
  2045. if (comp == 0)
  2046. qprec = pEncInfo->q_prec0;
  2047. else
  2048. qprec = pEncInfo->q_prec1;
  2049. }
  2050. for (i=0; i<64; i++) {
  2051. divisor = pEncInfo->pQMatTab[quantID][i];
  2052. if (qprec)
  2053. quotient= dividend_32b / divisor;
  2054. else
  2055. quotient= dividend / divisor;
  2056. // enhace bit precision & rounding Q
  2057. if (qprec){
  2058. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, (int)(divisor & 0xFFFF));
  2059. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, (int)(quotient & 0xFFFFFFFF));
  2060. }else
  2061. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_DATA_REG, (int)(divisor<<20)|(int)(quotient & 0xFFFFF));
  2062. }
  2063. JpuWriteInstReg(instRegIndex, MJPEG_QMAT_CTRL_REG, t);
  2064. }
  2065. return 1;
  2066. }
  2067. #define PUT_BYTE(_p, _b) \
  2068. if (tot++ > len) return 0; \
  2069. *_p++ = (unsigned char)(_b);
  2070. int JpgEncEncodeHeader(JpgEncHandle handle, JpgEncParamSet * para)
  2071. {
  2072. JpgInst *pJpgInst;
  2073. JpgEncInfo *pEncInfo;
  2074. BYTE *p;
  2075. int tot, len, pad;
  2076. BYTE *pCInfoTab[4];
  2077. int frameFormat;
  2078. Int32 i;
  2079. Uint32 width, height;
  2080. tot = 0;
  2081. pJpgInst = handle;
  2082. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  2083. p = para->pParaSet;
  2084. len = para->size;
  2085. // SOI Header
  2086. PUT_BYTE(p, 0xff);
  2087. PUT_BYTE(p, 0xD8);
  2088. if (!para->disableAPPMarker)
  2089. {
  2090. // APP9 Header
  2091. PUT_BYTE(p, 0xFF);
  2092. PUT_BYTE(p, 0xE9);
  2093. PUT_BYTE(p, 0x00);
  2094. PUT_BYTE(p, 0x04);
  2095. PUT_BYTE(p, (pEncInfo->frameIdx >> 8));
  2096. PUT_BYTE(p, (pEncInfo->frameIdx & 0xFF));
  2097. }
  2098. // DRI header
  2099. if (pEncInfo->rstIntval) {
  2100. PUT_BYTE(p, 0xFF);
  2101. PUT_BYTE(p, 0xDD);
  2102. PUT_BYTE(p, 0x00);
  2103. PUT_BYTE(p, 0x04);
  2104. PUT_BYTE(p, (pEncInfo->rstIntval >> 8));
  2105. PUT_BYTE(p, (pEncInfo->rstIntval & 0xff));
  2106. }
  2107. // DQT Header
  2108. PUT_BYTE(p, 0xFF);
  2109. PUT_BYTE(p, 0xDB);
  2110. if (para->quantMode == JPG_TBL_NORMAL)
  2111. {
  2112. PUT_BYTE(p, 0x00);
  2113. if (pEncInfo->q_prec0 == TRUE) {
  2114. PUT_BYTE(p, 0x83);
  2115. }
  2116. else {
  2117. PUT_BYTE(p, 0x43);
  2118. }
  2119. if (pEncInfo->q_prec0 == TRUE) {
  2120. PUT_BYTE(p, 0x10); // Pq
  2121. }
  2122. else {
  2123. PUT_BYTE(p, 0x00); // Pq
  2124. }
  2125. for (i=0; i<64; i++) {
  2126. if (pEncInfo->q_prec0 == TRUE) {
  2127. Uint16 q = pEncInfo->pQMatTab[0][i];
  2128. PUT_BYTE(p, (q>>8)&0xff);
  2129. PUT_BYTE(p, q&0xff);
  2130. }
  2131. else {
  2132. PUT_BYTE(p, pEncInfo->pQMatTab[0][i]);
  2133. }
  2134. }
  2135. if (pEncInfo->format != FORMAT_400) {
  2136. PUT_BYTE(p, 0xFF);
  2137. PUT_BYTE(p, 0xDB);
  2138. PUT_BYTE(p, 0x00);
  2139. if (pEncInfo->q_prec1 == TRUE) {
  2140. PUT_BYTE(p, 0x83);
  2141. }
  2142. else {
  2143. PUT_BYTE(p, 0x43);
  2144. }
  2145. if (pEncInfo->q_prec1 == TRUE) {
  2146. PUT_BYTE(p, 0x11); // Pq
  2147. }
  2148. else {
  2149. PUT_BYTE(p, 0x01); // Pq
  2150. }
  2151. for (i=0; i<64; i++) {
  2152. if (pEncInfo->q_prec1 == TRUE) {
  2153. Uint16 q = pEncInfo->pQMatTab[1][i];
  2154. PUT_BYTE(p, (q>>8)&0xff);
  2155. PUT_BYTE(p, q&0xff);
  2156. }
  2157. else {
  2158. PUT_BYTE(p, pEncInfo->pQMatTab[1][i]);
  2159. }
  2160. }
  2161. }
  2162. }
  2163. else// if (para->quantMode == JPG_TBL_MERGE)
  2164. {
  2165. if (pEncInfo->format != FORMAT_400)
  2166. {
  2167. Uint16 qLength = 0x84;
  2168. if (pEncInfo->q_prec0 == TRUE) {
  2169. qLength += 64;
  2170. }
  2171. if (pEncInfo->q_prec1 == TRUE) {
  2172. qLength += 64;
  2173. }
  2174. PUT_BYTE(p, (qLength>>8)&0xff);
  2175. PUT_BYTE(p, qLength&0xff);
  2176. }
  2177. else
  2178. {
  2179. Uint16 qLength = 0x43;
  2180. if (pEncInfo->q_prec0 == TRUE) {
  2181. qLength += 64;
  2182. }
  2183. PUT_BYTE(p, (qLength>>8)&0xff);
  2184. PUT_BYTE(p, qLength&0xff);
  2185. }
  2186. PUT_BYTE(p, 0x00 | (pEncInfo->q_prec0 << 4)); // Luma
  2187. for (i=0; i<64; i++) {
  2188. if (pEncInfo->q_prec0 == TRUE) {
  2189. Uint16 q = pEncInfo->pQMatTab[0][i];
  2190. PUT_BYTE(p, (q>>8)&0xff);
  2191. PUT_BYTE(p, q&0xff);
  2192. }
  2193. else {
  2194. PUT_BYTE(p, pEncInfo->pQMatTab[0][i]);
  2195. }
  2196. }
  2197. if (pEncInfo->format != FORMAT_400) {
  2198. PUT_BYTE(p, 0x01 | (pEncInfo->q_prec1 << 4)); // Croma
  2199. for (i=0; i<64; i++) {
  2200. if (pEncInfo->q_prec0 == TRUE) {
  2201. Uint16 q = pEncInfo->pQMatTab[1][i];
  2202. PUT_BYTE(p, (q>>8)&0xff);
  2203. PUT_BYTE(p, q&0xff);
  2204. }
  2205. else {
  2206. PUT_BYTE(p, pEncInfo->pQMatTab[1][i]);
  2207. }
  2208. }
  2209. }
  2210. }
  2211. // DHT Header
  2212. PUT_BYTE(p, 0xFF);
  2213. PUT_BYTE(p, 0xC4);
  2214. if (para->huffMode == JPG_TBL_NORMAL)
  2215. {
  2216. Int32 numHuffValDC = pEncInfo->jpg12bit == TRUE ? 13 : 12;
  2217. Int32 numHuffValAC = pEncInfo->jpg12bit == TRUE ? 256: 162;
  2218. Uint16 LhDC = pEncInfo->jpg12bit == TRUE ? 32 : 31;
  2219. Uint16 LhAC = pEncInfo->jpg12bit == TRUE ? 275 : 181;
  2220. /* Lh: Huffman table definition length */
  2221. PUT_BYTE(p, (LhDC>>8));
  2222. PUT_BYTE(p, LhDC&0xff);
  2223. PUT_BYTE(p, 0x00); /* TcTh : DC : ID0 */
  2224. for (i=0; i<16; i++) {
  2225. PUT_BYTE(p, pEncInfo->pHuffBits[0][i]);
  2226. }
  2227. for (i=0; i<numHuffValDC; i++) {
  2228. PUT_BYTE(p, pEncInfo->pHuffVal[0][i]);
  2229. }
  2230. PUT_BYTE(p, 0xFF);
  2231. PUT_BYTE(p, 0xC4);
  2232. PUT_BYTE(p, (LhAC>>8));
  2233. PUT_BYTE(p, LhAC&0xff);
  2234. PUT_BYTE(p, 0x10); /* TcTh = AC : ID0 */
  2235. for (i=0; i<16; i++) {
  2236. PUT_BYTE(p, pEncInfo->pHuffBits[1][i]);
  2237. }
  2238. for (i=0; i<numHuffValAC; i++) {
  2239. PUT_BYTE(p, pEncInfo->pHuffVal[1][i]);
  2240. }
  2241. if (pEncInfo->format != FORMAT_400) {
  2242. PUT_BYTE(p, 0xFF);
  2243. PUT_BYTE(p, 0xC4);
  2244. PUT_BYTE(p, (LhDC>>8)&0xff);
  2245. PUT_BYTE(p, LhDC&0xff);
  2246. PUT_BYTE(p, 0x01);
  2247. for (i=0; i<16; i++) {
  2248. PUT_BYTE(p, pEncInfo->pHuffBits[2][i]);
  2249. }
  2250. for (i=0; i<numHuffValDC; i++) {
  2251. PUT_BYTE(p, pEncInfo->pHuffVal[2][i]);
  2252. }
  2253. PUT_BYTE(p, 0xFF);
  2254. PUT_BYTE(p, 0xC4);
  2255. PUT_BYTE(p, (LhAC>>8)&0xff);
  2256. PUT_BYTE(p, LhAC&0xff);
  2257. PUT_BYTE(p, 0x11);
  2258. for (i=0; i<16; i++) {
  2259. PUT_BYTE(p, pEncInfo->pHuffBits[3][i]);
  2260. }
  2261. for (i=0; i<numHuffValAC; i++) {
  2262. PUT_BYTE(p, pEncInfo->pHuffVal[3][i]);
  2263. }
  2264. if (pEncInfo->jpg12bit == TRUE) {
  2265. PUT_BYTE(p, 0xFF);
  2266. PUT_BYTE(p, 0xC4);
  2267. PUT_BYTE(p, (LhDC>>8)&0xff);
  2268. PUT_BYTE(p, LhDC&0xff);
  2269. PUT_BYTE(p, 0x02); /* TcTh = DC : ID2 */
  2270. for (i=0; i<16; i++) {
  2271. PUT_BYTE(p, pEncInfo->pHuffBits[4][i]);
  2272. }
  2273. for (i=0; i<numHuffValDC; i++) {
  2274. PUT_BYTE(p, pEncInfo->pHuffVal[4][i]);
  2275. }
  2276. PUT_BYTE(p, 0xFF);
  2277. PUT_BYTE(p, 0xC4);
  2278. PUT_BYTE(p, (LhAC>>8)&0xff);
  2279. PUT_BYTE(p, LhAC&0xff);
  2280. PUT_BYTE(p, 0x12); /* TcTh = AC : ID2 */
  2281. for (i=0; i<16; i++) {
  2282. PUT_BYTE(p, pEncInfo->pHuffBits[5][i]);
  2283. }
  2284. for (i=0; i<numHuffValAC; i++) {
  2285. PUT_BYTE(p, pEncInfo->pHuffVal[5][i]);
  2286. }
  2287. }
  2288. }
  2289. }
  2290. else// if (para->huffMode == JPG_TBL_MERGE)
  2291. {
  2292. if (pEncInfo->format != FORMAT_400)
  2293. {
  2294. PUT_BYTE(p, 0x01);
  2295. PUT_BYTE(p, 0xA2);
  2296. //PUT_BYTE(p, 2 + 1*4 + 28 + 178 + 28 + 178 );
  2297. }
  2298. else
  2299. {
  2300. PUT_BYTE(p, 0x00);
  2301. PUT_BYTE(p, 0xD4);
  2302. //PUT_BYTE(p, 2 + 1*4 + 28 + 178 );
  2303. }
  2304. PUT_BYTE(p, 0x00);
  2305. for (i=0; i<16; i++) {
  2306. PUT_BYTE(p, pEncInfo->pHuffBits[0][i]);
  2307. }
  2308. for (i=0; i<12; i++) {
  2309. PUT_BYTE(p, pEncInfo->pHuffVal[0][i]);
  2310. }
  2311. PUT_BYTE(p, 0x10);
  2312. for (i=0; i<16; i++) {
  2313. PUT_BYTE(p, pEncInfo->pHuffBits[1][i]);
  2314. }
  2315. for (i=0; i<162; i++) {
  2316. PUT_BYTE(p, pEncInfo->pHuffVal[1][i]);
  2317. }
  2318. if (pEncInfo->format != FORMAT_400) {
  2319. PUT_BYTE(p, 0x01);
  2320. for (i=0; i<16; i++) {
  2321. PUT_BYTE(p, pEncInfo->pHuffBits[2][i]);
  2322. }
  2323. for (i=0; i<12; i++) {
  2324. PUT_BYTE(p, pEncInfo->pHuffVal[2][i]);
  2325. }
  2326. PUT_BYTE(p, 0x11);
  2327. for (i=0; i<16; i++) {
  2328. PUT_BYTE(p, pEncInfo->pHuffBits[3][i]);
  2329. }
  2330. for (i=0; i<162; i++) {
  2331. PUT_BYTE(p, pEncInfo->pHuffVal[3][i]);
  2332. }
  2333. }
  2334. }
  2335. // SOF header
  2336. PUT_BYTE(p, 0xFF);
  2337. PUT_BYTE(p, (pEncInfo->jpg12bit == TRUE ? 0xC1 : 0xC0));
  2338. PUT_BYTE(p, (((8+(pEncInfo->compNum*3)) >> 8) & 0xFF));
  2339. PUT_BYTE(p, ((8+(pEncInfo->compNum*3)) & 0xFF));
  2340. PUT_BYTE(p, (pEncInfo->jpg12bit == TRUE ? 12 : 8));
  2341. width = pEncInfo->picWidth;
  2342. height = pEncInfo->picHeight;
  2343. frameFormat = pEncInfo->format;
  2344. if (pEncInfo->rotationIndex == 1 || pEncInfo->rotationIndex == 3) {
  2345. // rotation angle = rotationIndex x 90;
  2346. width = pEncInfo->picHeight;
  2347. height = pEncInfo->picWidth;
  2348. if (frameFormat == FORMAT_422) frameFormat = FORMAT_440;
  2349. else if (frameFormat == FORMAT_440) frameFormat = FORMAT_422;
  2350. }
  2351. PUT_BYTE(p, (height >> 8));
  2352. PUT_BYTE(p, (height & 0xFF));
  2353. PUT_BYTE(p, (width >> 8));
  2354. PUT_BYTE(p, (width & 0xFF));
  2355. PUT_BYTE(p, pEncInfo->compNum);
  2356. pCInfoTab[0] = sJpuCompInfoTable[frameFormat];
  2357. pCInfoTab[1] = pCInfoTab[0] + 6;
  2358. pCInfoTab[2] = pCInfoTab[1] + 6;
  2359. pCInfoTab[3] = pCInfoTab[2] + 6;
  2360. for (i=0; i<pEncInfo->compNum; i++) {
  2361. PUT_BYTE(p, (i+1));
  2362. PUT_BYTE(p, ((pCInfoTab[i][1]<<4) & 0xF0) + (pCInfoTab[i][2] & 0x0F));
  2363. PUT_BYTE(p, pCInfoTab[i][3]);
  2364. }
  2365. if (para->enableSofStuffing)
  2366. {
  2367. pad = 0;
  2368. if (tot % 8) {
  2369. pad = tot % 8;
  2370. pad = 8-pad;
  2371. for (i=0; i<pad; i++) {
  2372. PUT_BYTE(p, 0xFF);
  2373. }
  2374. }
  2375. }
  2376. pEncInfo->frameIdx++;
  2377. para->size = tot;
  2378. return tot;
  2379. }
  2380. void JpgEncSetQualityFactor(JpgEncHandle handle, Uint32 quality, BOOL useStdTable)
  2381. {
  2382. JpgInst *pJpgInst;
  2383. JpgEncInfo *pEncInfo;
  2384. Uint32 scaleFactor;
  2385. Uint32 i;
  2386. Uint32 temp;
  2387. pJpgInst = handle;
  2388. pEncInfo = &pJpgInst->JpgInfo->encInfo;
  2389. /* These are the sample quantization tables given in JPEG spec section K.1.
  2390. * The spec says that the values given produce "good" quality, and
  2391. * when divided by 2, "very good" quality.
  2392. */
  2393. static const Uint32 std_luminance_quant_tbl[64] = {
  2394. 16, 11, 10, 16, 24, 40, 51, 61,
  2395. 12, 12, 14, 19, 26, 58, 60, 55,
  2396. 14, 13, 16, 24, 40, 57, 69, 56,
  2397. 14, 17, 22, 29, 51, 87, 80, 62,
  2398. 18, 22, 37, 56, 68, 109, 103, 77,
  2399. 24, 35, 55, 64, 81, 104, 113, 92,
  2400. 49, 64, 78, 87, 103, 121, 120, 101,
  2401. 72, 92, 95, 98, 112, 100, 103, 99
  2402. };
  2403. static const Uint32 std_chrominance_quant_tbl[64] = {
  2404. 17, 18, 24, 47, 99, 99, 99, 99,
  2405. 18, 21, 26, 66, 99, 99, 99, 99,
  2406. 24, 26, 56, 99, 99, 99, 99, 99,
  2407. 47, 66, 99, 99, 99, 99, 99, 99,
  2408. 99, 99, 99, 99, 99, 99, 99, 99,
  2409. 99, 99, 99, 99, 99, 99, 99, 99,
  2410. 99, 99, 99, 99, 99, 99, 99, 99,
  2411. 99, 99, 99, 99, 99, 99, 99, 99
  2412. };
  2413. Uint32 pLumaQTable[64];
  2414. Uint32 pChromaQTable[64];
  2415. Uint32 qSize = 1, qTableSize;
  2416. Uint32 minQvalue = MIN_Q8_ELEMENT;
  2417. if (quality <= 0) quality = 1;
  2418. if (quality > 100) quality = 100;
  2419. for (i = 0; i<64; i++) {
  2420. pLumaQTable[i] = (useStdTable == TRUE) ? std_luminance_quant_tbl[i] : (Int32)pEncInfo->pQMatTab[DC_TABLE_INDEX0][i];
  2421. }
  2422. for (i = 0; i<64; i++) {
  2423. pChromaQTable[i] = (useStdTable == TRUE) ? std_chrominance_quant_tbl[i] : (Int32)pEncInfo->pQMatTab[AC_TABLE_INDEX0][i];
  2424. }
  2425. minQvalue = (pEncInfo->jpg12bit == TRUE) ? MIN_Q16_ELEMENT : MIN_Q8_ELEMENT;
  2426. /* The basic table is used as-is (scaling 100) for a quality of 50.
  2427. * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  2428. * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  2429. * to make all the table entries 1 (hence, minimum quantization loss).
  2430. * Qualities 1..50 are converted to scaling percentage 5000/Q.
  2431. */
  2432. if (quality < 50)
  2433. scaleFactor = 5000 / quality;
  2434. else
  2435. scaleFactor = 200 - quality * 2;
  2436. for (i = 0; i < 64; i++) {
  2437. temp = (pLumaQTable[i] * scaleFactor + 50) / 100;
  2438. /* limit the values to the valid range */
  2439. temp = (temp < minQvalue) ? minQvalue : temp;
  2440. if (temp > 32767)
  2441. temp = 32767; /* max quantizer needed for 12 bits */
  2442. if (pEncInfo->q_prec0 == FALSE && temp > 255)
  2443. temp = 255; /* limit to baseline range if requested */
  2444. pEncInfo->pQMatTab[DC_TABLE_INDEX0][i] = temp;
  2445. }
  2446. for (i = 0; i < 64; i++) {
  2447. temp = (pChromaQTable[i] * scaleFactor + 50) / 100;
  2448. /* limit the values to the valid range */
  2449. temp = (temp < minQvalue) ? minQvalue : temp;
  2450. if (temp > 32767) temp = 32767; /* max quantizer needed for 12 bits */
  2451. if (pEncInfo->q_prec1 == FALSE && temp > 255)
  2452. temp = 255; /* limit to baseline range if requested */
  2453. pEncInfo->pQMatTab[AC_TABLE_INDEX0][i] = temp;
  2454. }
  2455. qSize = pEncInfo->jpg12bit ? 2 : 1;
  2456. //setting of qmatrix table information
  2457. #ifdef USE_CNM_DEFAULT_QMAT_TABLE
  2458. memset((void*)pEncInfo->pQMatTab[DC_TABLE_INDEX0], 0x00, 64 * qSize);
  2459. memcpy((void*)pEncInfo->pQMatTab[DC_TABLE_INDEX0], (void*)lumaQ2, 64 * qSize);
  2460. memset((void*)pEncInfo->pQMatTab[AC_TABLE_INDEX0], 0x00, 64 * qSize);
  2461. memcpy((void*)ppEncInfo->pQMatTab[AC_TABLE_INDEX0], (void*)chromaBQ2, 64 * qSize);
  2462. #endif
  2463. qTableSize = 64 * qSize;
  2464. memcpy((void*)pEncInfo->pQMatTab[DC_TABLE_INDEX1], (void*)pEncInfo->pQMatTab[DC_TABLE_INDEX0], qTableSize);
  2465. memcpy((void*)pEncInfo->pQMatTab[AC_TABLE_INDEX1], (void*)pEncInfo->pQMatTab[AC_TABLE_INDEX0], qTableSize);
  2466. }
  2467. JpgRet JpgEnterLock()
  2468. {
  2469. jdi_lock();
  2470. return JPG_RET_SUCCESS;
  2471. }
  2472. JpgRet JpgLeaveLock()
  2473. {
  2474. jdi_unlock();
  2475. return JPG_RET_SUCCESS;
  2476. }
  2477. JpgRet JpgSetClockGateEx(Uint32 on, Uint32 instIdx)
  2478. {
  2479. JpgInst *inst;
  2480. jpu_instance_pool_t *jip;
  2481. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  2482. if (!jip)
  2483. return JPG_RET_FAILURE;
  2484. inst = (JpgInst *)jip->instPendingInst[instIdx];
  2485. if(inst && !on)
  2486. return JPG_RET_SUCCESS;
  2487. jdi_set_clock_gate(on);
  2488. return JPG_RET_SUCCESS;
  2489. }
  2490. JpgInst *GetJpgPendingInstEx(Uint32 instIdx)
  2491. {
  2492. jpu_instance_pool_t *jip;
  2493. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  2494. if (!jip)
  2495. return NULL;
  2496. if (instIdx >= MAX_NUM_INSTANCE)
  2497. return NULL;
  2498. return jip->instPendingInst[instIdx];
  2499. }
  2500. void SetJpgPendingInstEx(JpgInst *inst, Uint32 instIdx)
  2501. {
  2502. jpu_instance_pool_t *jip;
  2503. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  2504. if (!jip)
  2505. return;
  2506. if (instIdx >= MAX_NUM_INSTANCE)
  2507. return;
  2508. jip->instPendingInst[instIdx] = inst;
  2509. }
  2510. void ClearJpgPendingInstEx(Uint32 instIdx)
  2511. {
  2512. jpu_instance_pool_t *jip;
  2513. jip = (jpu_instance_pool_t *)jdi_get_instance_pool();
  2514. if (!jip)
  2515. return;
  2516. if (instIdx >= MAX_NUM_INSTANCE)
  2517. return;
  2518. if(jip->instPendingInst[instIdx])
  2519. jip->instPendingInst[instIdx] = 0;
  2520. }