jpgd.cpp 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  1. // jpgd.cpp - C++ class for JPEG decompression.
  2. // Public domain, Rich Geldreich <richgel99@gmail.com>
  3. // Alex Evans: Linear memory allocator (taken from jpge.h).
  4. // v1.04, May. 19, 2012: Code tweaks to fix VS2008 static code analysis warnings (all looked harmless)
  5. //
  6. // Supports progressive and baseline sequential JPEG image files, and the most common chroma subsampling factors: Y, H1V1, H2V1, H1V2, and H2V2.
  7. //
  8. // Chroma upsampling quality: H2V2 is upsampled in the frequency domain, H2V1 and H1V2 are upsampled using point sampling.
  9. // Chroma upsampling reference: "Fast Scheme for Image Size Change in the Compressed Domain"
  10. // http://vision.ai.uiuc.edu/~dugad/research/dct/index.html
  11. #include "jpgd.h"
  12. #include <string.h>
  13. #include <assert.h>
  14. #define JPGD_ASSERT(x) assert(x)
  15. #ifdef _MSC_VER
  16. #pragma warning (disable : 4611) // warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable
  17. #endif
  18. // Set to 1 to enable freq. domain chroma upsampling on images using H2V2 subsampling (0=faster nearest neighbor sampling).
  19. // This is slower, but results in higher quality on images with highly saturated colors.
  20. #define JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING 1
  21. #define JPGD_TRUE (1)
  22. #define JPGD_FALSE (0)
  23. #define JPGD_MAX(a,b) (((a)>(b)) ? (a) : (b))
  24. #define JPGD_MIN(a,b) (((a)<(b)) ? (a) : (b))
  25. // TODO: Move to header and use these constants when declaring the arrays.
  26. #define JPGD_HUFF_TREE_MAX_LENGTH 512
  27. #define JPGD_HUFF_CODE_SIZE_MAX_LENGTH 256
  28. namespace jpgd {
  29. static inline void *jpgd_malloc(size_t nSize) { return malloc(nSize); }
  30. static inline void jpgd_free(void *p) { free(p); }
  31. // DCT coefficients are stored in this sequence.
  32. static int g_ZAG[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 };
  33. enum JPEG_MARKER
  34. {
  35. M_SOF0 = 0xC0, M_SOF1 = 0xC1, M_SOF2 = 0xC2, M_SOF3 = 0xC3, M_SOF5 = 0xC5, M_SOF6 = 0xC6, M_SOF7 = 0xC7, M_JPG = 0xC8,
  36. M_SOF9 = 0xC9, M_SOF10 = 0xCA, M_SOF11 = 0xCB, M_SOF13 = 0xCD, M_SOF14 = 0xCE, M_SOF15 = 0xCF, M_DHT = 0xC4, M_DAC = 0xCC,
  37. M_RST0 = 0xD0, M_RST1 = 0xD1, M_RST2 = 0xD2, M_RST3 = 0xD3, M_RST4 = 0xD4, M_RST5 = 0xD5, M_RST6 = 0xD6, M_RST7 = 0xD7,
  38. M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_DNL = 0xDC, M_DRI = 0xDD, M_DHP = 0xDE, M_EXP = 0xDF,
  39. M_APP0 = 0xE0, M_APP15 = 0xEF, M_JPG0 = 0xF0, M_JPG13 = 0xFD, M_COM = 0xFE, M_TEM = 0x01, M_ERROR = 0x100, RST0 = 0xD0
  40. };
  41. enum JPEG_SUBSAMPLING { JPGD_GRAYSCALE = 0, JPGD_YH1V1, JPGD_YH2V1, JPGD_YH1V2, JPGD_YH2V2 };
  42. #define CONST_BITS 13
  43. #define PASS1_BITS 2
  44. #define SCALEDONE ((int32)1)
  45. #define FIX_0_298631336 ((int32)2446) /* FIX(0.298631336) */
  46. #define FIX_0_390180644 ((int32)3196) /* FIX(0.390180644) */
  47. #define FIX_0_541196100 ((int32)4433) /* FIX(0.541196100) */
  48. #define FIX_0_765366865 ((int32)6270) /* FIX(0.765366865) */
  49. #define FIX_0_899976223 ((int32)7373) /* FIX(0.899976223) */
  50. #define FIX_1_175875602 ((int32)9633) /* FIX(1.175875602) */
  51. #define FIX_1_501321110 ((int32)12299) /* FIX(1.501321110) */
  52. #define FIX_1_847759065 ((int32)15137) /* FIX(1.847759065) */
  53. #define FIX_1_961570560 ((int32)16069) /* FIX(1.961570560) */
  54. #define FIX_2_053119869 ((int32)16819) /* FIX(2.053119869) */
  55. #define FIX_2_562915447 ((int32)20995) /* FIX(2.562915447) */
  56. #define FIX_3_072711026 ((int32)25172) /* FIX(3.072711026) */
  57. #define DESCALE(x,n) (((x) + (SCALEDONE << ((n)-1))) >> (n))
  58. #define DESCALE_ZEROSHIFT(x,n) (((x) + (128 << (n)) + (SCALEDONE << ((n)-1))) >> (n))
  59. #define MULTIPLY(var, cnst) ((var) * (cnst))
  60. #define CLAMP(i) ((static_cast<uint>(i) > 255) ? (((~i) >> 31) & 0xFF) : (i))
  61. // Compiler creates a fast path 1D IDCT for X non-zero columns
  62. template <int NONZERO_COLS>
  63. struct Row
  64. {
  65. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  66. {
  67. // ACCESS_COL() will be optimized at compile time to either an array access, or 0.
  68. #define ACCESS_COL(x) (((x) < NONZERO_COLS) ? (int)pSrc[x] : 0)
  69. const int z2 = ACCESS_COL(2), z3 = ACCESS_COL(6);
  70. const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  71. const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  72. const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  73. const int tmp0 = (ACCESS_COL(0) + ACCESS_COL(4)) << CONST_BITS;
  74. const int tmp1 = (ACCESS_COL(0) - ACCESS_COL(4)) << CONST_BITS;
  75. const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
  76. const int atmp0 = ACCESS_COL(7), atmp1 = ACCESS_COL(5), atmp2 = ACCESS_COL(3), atmp3 = ACCESS_COL(1);
  77. const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
  78. const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
  79. const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
  80. const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
  81. const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
  82. const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
  83. const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
  84. const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
  85. const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
  86. const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
  87. pTemp[0] = DESCALE(tmp10 + btmp3, CONST_BITS-PASS1_BITS);
  88. pTemp[7] = DESCALE(tmp10 - btmp3, CONST_BITS-PASS1_BITS);
  89. pTemp[1] = DESCALE(tmp11 + btmp2, CONST_BITS-PASS1_BITS);
  90. pTemp[6] = DESCALE(tmp11 - btmp2, CONST_BITS-PASS1_BITS);
  91. pTemp[2] = DESCALE(tmp12 + btmp1, CONST_BITS-PASS1_BITS);
  92. pTemp[5] = DESCALE(tmp12 - btmp1, CONST_BITS-PASS1_BITS);
  93. pTemp[3] = DESCALE(tmp13 + btmp0, CONST_BITS-PASS1_BITS);
  94. pTemp[4] = DESCALE(tmp13 - btmp0, CONST_BITS-PASS1_BITS);
  95. }
  96. };
  97. template <>
  98. struct Row<0>
  99. {
  100. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  101. {
  102. #ifdef _MSC_VER
  103. pTemp; pSrc;
  104. #endif
  105. }
  106. };
  107. template <>
  108. struct Row<1>
  109. {
  110. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  111. {
  112. const int dcval = (pSrc[0] << PASS1_BITS);
  113. pTemp[0] = dcval;
  114. pTemp[1] = dcval;
  115. pTemp[2] = dcval;
  116. pTemp[3] = dcval;
  117. pTemp[4] = dcval;
  118. pTemp[5] = dcval;
  119. pTemp[6] = dcval;
  120. pTemp[7] = dcval;
  121. }
  122. };
  123. // Compiler creates a fast path 1D IDCT for X non-zero rows
  124. template <int NONZERO_ROWS>
  125. struct Col
  126. {
  127. static void idct(uint8* pDst_ptr, const int* pTemp)
  128. {
  129. // ACCESS_ROW() will be optimized at compile time to either an array access, or 0.
  130. #define ACCESS_ROW(x) (((x) < NONZERO_ROWS) ? pTemp[x * 8] : 0)
  131. const int z2 = ACCESS_ROW(2);
  132. const int z3 = ACCESS_ROW(6);
  133. const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  134. const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  135. const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  136. const int tmp0 = (ACCESS_ROW(0) + ACCESS_ROW(4)) << CONST_BITS;
  137. const int tmp1 = (ACCESS_ROW(0) - ACCESS_ROW(4)) << CONST_BITS;
  138. const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
  139. const int atmp0 = ACCESS_ROW(7), atmp1 = ACCESS_ROW(5), atmp2 = ACCESS_ROW(3), atmp3 = ACCESS_ROW(1);
  140. const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
  141. const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
  142. const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
  143. const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
  144. const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
  145. const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
  146. const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
  147. const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
  148. const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
  149. const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
  150. int i = DESCALE_ZEROSHIFT(tmp10 + btmp3, CONST_BITS+PASS1_BITS+3);
  151. pDst_ptr[8*0] = (uint8)CLAMP(i);
  152. i = DESCALE_ZEROSHIFT(tmp10 - btmp3, CONST_BITS+PASS1_BITS+3);
  153. pDst_ptr[8*7] = (uint8)CLAMP(i);
  154. i = DESCALE_ZEROSHIFT(tmp11 + btmp2, CONST_BITS+PASS1_BITS+3);
  155. pDst_ptr[8*1] = (uint8)CLAMP(i);
  156. i = DESCALE_ZEROSHIFT(tmp11 - btmp2, CONST_BITS+PASS1_BITS+3);
  157. pDst_ptr[8*6] = (uint8)CLAMP(i);
  158. i = DESCALE_ZEROSHIFT(tmp12 + btmp1, CONST_BITS+PASS1_BITS+3);
  159. pDst_ptr[8*2] = (uint8)CLAMP(i);
  160. i = DESCALE_ZEROSHIFT(tmp12 - btmp1, CONST_BITS+PASS1_BITS+3);
  161. pDst_ptr[8*5] = (uint8)CLAMP(i);
  162. i = DESCALE_ZEROSHIFT(tmp13 + btmp0, CONST_BITS+PASS1_BITS+3);
  163. pDst_ptr[8*3] = (uint8)CLAMP(i);
  164. i = DESCALE_ZEROSHIFT(tmp13 - btmp0, CONST_BITS+PASS1_BITS+3);
  165. pDst_ptr[8*4] = (uint8)CLAMP(i);
  166. }
  167. };
  168. template <>
  169. struct Col<1>
  170. {
  171. static void idct(uint8* pDst_ptr, const int* pTemp)
  172. {
  173. int dcval = DESCALE_ZEROSHIFT(pTemp[0], PASS1_BITS+3);
  174. const uint8 dcval_clamped = (uint8)CLAMP(dcval);
  175. pDst_ptr[0*8] = dcval_clamped;
  176. pDst_ptr[1*8] = dcval_clamped;
  177. pDst_ptr[2*8] = dcval_clamped;
  178. pDst_ptr[3*8] = dcval_clamped;
  179. pDst_ptr[4*8] = dcval_clamped;
  180. pDst_ptr[5*8] = dcval_clamped;
  181. pDst_ptr[6*8] = dcval_clamped;
  182. pDst_ptr[7*8] = dcval_clamped;
  183. }
  184. };
  185. static const uint8 s_idct_row_table[] =
  186. {
  187. 1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 2,1,0,0,0,0,0,0, 2,1,1,0,0,0,0,0, 2,2,1,0,0,0,0,0, 3,2,1,0,0,0,0,0, 4,2,1,0,0,0,0,0, 4,3,1,0,0,0,0,0,
  188. 4,3,2,0,0,0,0,0, 4,3,2,1,0,0,0,0, 4,3,2,1,1,0,0,0, 4,3,2,2,1,0,0,0, 4,3,3,2,1,0,0,0, 4,4,3,2,1,0,0,0, 5,4,3,2,1,0,0,0, 6,4,3,2,1,0,0,0,
  189. 6,5,3,2,1,0,0,0, 6,5,4,2,1,0,0,0, 6,5,4,3,1,0,0,0, 6,5,4,3,2,0,0,0, 6,5,4,3,2,1,0,0, 6,5,4,3,2,1,1,0, 6,5,4,3,2,2,1,0, 6,5,4,3,3,2,1,0,
  190. 6,5,4,4,3,2,1,0, 6,5,5,4,3,2,1,0, 6,6,5,4,3,2,1,0, 7,6,5,4,3,2,1,0, 8,6,5,4,3,2,1,0, 8,7,5,4,3,2,1,0, 8,7,6,4,3,2,1,0, 8,7,6,5,3,2,1,0,
  191. 8,7,6,5,4,2,1,0, 8,7,6,5,4,3,1,0, 8,7,6,5,4,3,2,0, 8,7,6,5,4,3,2,1, 8,7,6,5,4,3,2,2, 8,7,6,5,4,3,3,2, 8,7,6,5,4,4,3,2, 8,7,6,5,5,4,3,2,
  192. 8,7,6,6,5,4,3,2, 8,7,7,6,5,4,3,2, 8,8,7,6,5,4,3,2, 8,8,8,6,5,4,3,2, 8,8,8,7,5,4,3,2, 8,8,8,7,6,4,3,2, 8,8,8,7,6,5,3,2, 8,8,8,7,6,5,4,2,
  193. 8,8,8,7,6,5,4,3, 8,8,8,7,6,5,4,4, 8,8,8,7,6,5,5,4, 8,8,8,7,6,6,5,4, 8,8,8,7,7,6,5,4, 8,8,8,8,7,6,5,4, 8,8,8,8,8,6,5,4, 8,8,8,8,8,7,5,4,
  194. 8,8,8,8,8,7,6,4, 8,8,8,8,8,7,6,5, 8,8,8,8,8,7,6,6, 8,8,8,8,8,7,7,6, 8,8,8,8,8,8,7,6, 8,8,8,8,8,8,8,6, 8,8,8,8,8,8,8,7, 8,8,8,8,8,8,8,8,
  195. };
  196. static const uint8 s_idct_col_table[] = { 1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
  197. void idct(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr, int block_max_zag)
  198. {
  199. JPGD_ASSERT(block_max_zag >= 1);
  200. JPGD_ASSERT(block_max_zag <= 64);
  201. if (block_max_zag <= 1)
  202. {
  203. int k = ((pSrc_ptr[0] + 4) >> 3) + 128;
  204. k = CLAMP(k);
  205. k = k | (k<<8);
  206. k = k | (k<<16);
  207. for (int i = 8; i > 0; i--)
  208. {
  209. *(int*)&pDst_ptr[0] = k;
  210. *(int*)&pDst_ptr[4] = k;
  211. pDst_ptr += 8;
  212. }
  213. return;
  214. }
  215. int temp[64];
  216. const jpgd_block_t* pSrc = pSrc_ptr;
  217. int* pTemp = temp;
  218. const uint8* pRow_tab = &s_idct_row_table[(block_max_zag - 1) * 8];
  219. int i;
  220. for (i = 8; i > 0; i--, pRow_tab++)
  221. {
  222. switch (*pRow_tab)
  223. {
  224. case 0: Row<0>::idct(pTemp, pSrc); break;
  225. case 1: Row<1>::idct(pTemp, pSrc); break;
  226. case 2: Row<2>::idct(pTemp, pSrc); break;
  227. case 3: Row<3>::idct(pTemp, pSrc); break;
  228. case 4: Row<4>::idct(pTemp, pSrc); break;
  229. case 5: Row<5>::idct(pTemp, pSrc); break;
  230. case 6: Row<6>::idct(pTemp, pSrc); break;
  231. case 7: Row<7>::idct(pTemp, pSrc); break;
  232. case 8: Row<8>::idct(pTemp, pSrc); break;
  233. }
  234. pSrc += 8;
  235. pTemp += 8;
  236. }
  237. pTemp = temp;
  238. const int nonzero_rows = s_idct_col_table[block_max_zag - 1];
  239. for (i = 8; i > 0; i--)
  240. {
  241. switch (nonzero_rows)
  242. {
  243. case 1: Col<1>::idct(pDst_ptr, pTemp); break;
  244. case 2: Col<2>::idct(pDst_ptr, pTemp); break;
  245. case 3: Col<3>::idct(pDst_ptr, pTemp); break;
  246. case 4: Col<4>::idct(pDst_ptr, pTemp); break;
  247. case 5: Col<5>::idct(pDst_ptr, pTemp); break;
  248. case 6: Col<6>::idct(pDst_ptr, pTemp); break;
  249. case 7: Col<7>::idct(pDst_ptr, pTemp); break;
  250. case 8: Col<8>::idct(pDst_ptr, pTemp); break;
  251. }
  252. pTemp++;
  253. pDst_ptr++;
  254. }
  255. }
  256. void idct_4x4(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr)
  257. {
  258. int temp[64];
  259. int* pTemp = temp;
  260. const jpgd_block_t* pSrc = pSrc_ptr;
  261. for (int i = 4; i > 0; i--)
  262. {
  263. Row<4>::idct(pTemp, pSrc);
  264. pSrc += 8;
  265. pTemp += 8;
  266. }
  267. pTemp = temp;
  268. for (int i = 8; i > 0; i--)
  269. {
  270. Col<4>::idct(pDst_ptr, pTemp);
  271. pTemp++;
  272. pDst_ptr++;
  273. }
  274. }
  275. // Retrieve one character from the input stream.
  276. inline uint jpeg_decoder::get_char()
  277. {
  278. // Any bytes remaining in buffer?
  279. if (!m_in_buf_left)
  280. {
  281. // Try to get more bytes.
  282. prep_in_buffer();
  283. // Still nothing to get?
  284. if (!m_in_buf_left)
  285. {
  286. // Pad the end of the stream with 0xFF 0xD9 (EOI marker)
  287. int t = m_tem_flag;
  288. m_tem_flag ^= 1;
  289. if (t)
  290. return 0xD9;
  291. else
  292. return 0xFF;
  293. }
  294. }
  295. uint c = *m_pIn_buf_ofs++;
  296. m_in_buf_left--;
  297. return c;
  298. }
  299. // Same as previous method, except can indicate if the character is a pad character or not.
  300. inline uint jpeg_decoder::get_char(bool *pPadding_flag)
  301. {
  302. if (!m_in_buf_left)
  303. {
  304. prep_in_buffer();
  305. if (!m_in_buf_left)
  306. {
  307. *pPadding_flag = true;
  308. int t = m_tem_flag;
  309. m_tem_flag ^= 1;
  310. if (t)
  311. return 0xD9;
  312. else
  313. return 0xFF;
  314. }
  315. }
  316. *pPadding_flag = false;
  317. uint c = *m_pIn_buf_ofs++;
  318. m_in_buf_left--;
  319. return c;
  320. }
  321. // Inserts a previously retrieved character back into the input buffer.
  322. inline void jpeg_decoder::stuff_char(uint8 q)
  323. {
  324. *(--m_pIn_buf_ofs) = q;
  325. m_in_buf_left++;
  326. }
  327. // Retrieves one character from the input stream, but does not read past markers. Will continue to return 0xFF when a marker is encountered.
  328. inline uint8 jpeg_decoder::get_octet()
  329. {
  330. bool padding_flag;
  331. int c = get_char(&padding_flag);
  332. if (c == 0xFF)
  333. {
  334. if (padding_flag)
  335. return 0xFF;
  336. c = get_char(&padding_flag);
  337. if (padding_flag)
  338. {
  339. stuff_char(0xFF);
  340. return 0xFF;
  341. }
  342. if (c == 0x00)
  343. return 0xFF;
  344. else
  345. {
  346. stuff_char(static_cast<uint8>(c));
  347. stuff_char(0xFF);
  348. return 0xFF;
  349. }
  350. }
  351. return static_cast<uint8>(c);
  352. }
  353. // Retrieves a variable number of bits from the input stream. Does not recognize markers.
  354. inline uint jpeg_decoder::get_bits(int num_bits)
  355. {
  356. if (!num_bits)
  357. return 0;
  358. uint i = m_bit_buf >> (32 - num_bits);
  359. if ((m_bits_left -= num_bits) <= 0)
  360. {
  361. m_bit_buf <<= (num_bits += m_bits_left);
  362. uint c1 = get_char();
  363. uint c2 = get_char();
  364. m_bit_buf = (m_bit_buf & 0xFFFF0000) | (c1 << 8) | c2;
  365. m_bit_buf <<= -m_bits_left;
  366. m_bits_left += 16;
  367. JPGD_ASSERT(m_bits_left >= 0);
  368. }
  369. else
  370. m_bit_buf <<= num_bits;
  371. return i;
  372. }
  373. // Retrieves a variable number of bits from the input stream. Markers will not be read into the input bit buffer. Instead, an infinite number of all 1's will be returned when a marker is encountered.
  374. inline uint jpeg_decoder::get_bits_no_markers(int num_bits)
  375. {
  376. if (!num_bits)
  377. return 0;
  378. uint i = m_bit_buf >> (32 - num_bits);
  379. if ((m_bits_left -= num_bits) <= 0)
  380. {
  381. m_bit_buf <<= (num_bits += m_bits_left);
  382. if ((m_in_buf_left < 2) || (m_pIn_buf_ofs[0] == 0xFF) || (m_pIn_buf_ofs[1] == 0xFF))
  383. {
  384. uint c1 = get_octet();
  385. uint c2 = get_octet();
  386. m_bit_buf |= (c1 << 8) | c2;
  387. }
  388. else
  389. {
  390. m_bit_buf |= ((uint)m_pIn_buf_ofs[0] << 8) | m_pIn_buf_ofs[1];
  391. m_in_buf_left -= 2;
  392. m_pIn_buf_ofs += 2;
  393. }
  394. m_bit_buf <<= -m_bits_left;
  395. m_bits_left += 16;
  396. JPGD_ASSERT(m_bits_left >= 0);
  397. }
  398. else
  399. m_bit_buf <<= num_bits;
  400. return i;
  401. }
  402. // Decodes a Huffman encoded symbol.
  403. inline int jpeg_decoder::huff_decode(huff_tables *pH)
  404. {
  405. JPGD_ASSERT(pH);
  406. int symbol;
  407. // Check first 8-bits: do we have a complete symbol?
  408. if ((symbol = pH->look_up[m_bit_buf >> 24]) < 0)
  409. {
  410. // Decode more bits, use a tree traversal to find symbol.
  411. int ofs = 23;
  412. do
  413. {
  414. unsigned int idx = -(int)(symbol + ((m_bit_buf >> ofs) & 1));
  415. JPGD_ASSERT(idx < JPGD_HUFF_TREE_MAX_LENGTH);
  416. symbol = pH->tree[idx];
  417. ofs--;
  418. } while (symbol < 0);
  419. get_bits_no_markers(8 + (23 - ofs));
  420. }
  421. else
  422. {
  423. JPGD_ASSERT(symbol < JPGD_HUFF_CODE_SIZE_MAX_LENGTH);
  424. get_bits_no_markers(pH->code_size[symbol]);
  425. }
  426. return symbol;
  427. }
  428. // Decodes a Huffman encoded symbol.
  429. inline int jpeg_decoder::huff_decode(huff_tables *pH, int& extra_bits)
  430. {
  431. int symbol;
  432. JPGD_ASSERT(pH);
  433. // Check first 8-bits: do we have a complete symbol?
  434. if ((symbol = pH->look_up2[m_bit_buf >> 24]) < 0)
  435. {
  436. // Use a tree traversal to find symbol.
  437. int ofs = 23;
  438. do
  439. {
  440. unsigned int idx = -(int)(symbol + ((m_bit_buf >> ofs) & 1));
  441. JPGD_ASSERT(idx < JPGD_HUFF_TREE_MAX_LENGTH);
  442. symbol = pH->tree[idx];
  443. ofs--;
  444. } while (symbol < 0);
  445. get_bits_no_markers(8 + (23 - ofs));
  446. extra_bits = get_bits_no_markers(symbol & 0xF);
  447. }
  448. else
  449. {
  450. JPGD_ASSERT(((symbol >> 8) & 31) == pH->code_size[symbol & 255] + ((symbol & 0x8000) ? (symbol & 15) : 0));
  451. if (symbol & 0x8000)
  452. {
  453. get_bits_no_markers((symbol >> 8) & 31);
  454. extra_bits = symbol >> 16;
  455. }
  456. else
  457. {
  458. int code_size = (symbol >> 8) & 31;
  459. int num_extra_bits = symbol & 0xF;
  460. int bits = code_size + num_extra_bits;
  461. if (bits <= (m_bits_left + 16))
  462. extra_bits = get_bits_no_markers(bits) & ((1 << num_extra_bits) - 1);
  463. else
  464. {
  465. get_bits_no_markers(code_size);
  466. extra_bits = get_bits_no_markers(num_extra_bits);
  467. }
  468. }
  469. symbol &= 0xFF;
  470. }
  471. return symbol;
  472. }
  473. // Tables and macro used to fully decode the DPCM differences.
  474. static const int s_extend_test[16] = { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  475. static const int s_extend_offset[16] = { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  476. static const int s_extend_mask[] = { 0, (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7), (1<<8), (1<<9), (1<<10), (1<<11), (1<<12), (1<<13), (1<<14), (1<<15), (1<<16) };
  477. // The logical AND's in this macro are to shut up static code analysis (aren't really necessary - couldn't find another way to do this)
  478. #define JPGD_HUFF_EXTEND(x, s) (((x) < s_extend_test[s & 15]) ? ((x) + s_extend_offset[s & 15]) : (x))
  479. // Clamps a value between 0-255.
  480. inline uint8 jpeg_decoder::clamp(int i)
  481. {
  482. if (static_cast<uint>(i) > 255)
  483. i = (((~i) >> 31) & 0xFF);
  484. return static_cast<uint8>(i);
  485. }
  486. namespace DCT_Upsample
  487. {
  488. struct Matrix44
  489. {
  490. typedef int Element_Type;
  491. enum { NUM_ROWS = 4, NUM_COLS = 4 };
  492. Element_Type v[NUM_ROWS][NUM_COLS];
  493. inline int rows() const { return NUM_ROWS; }
  494. inline int cols() const { return NUM_COLS; }
  495. inline const Element_Type & at(int r, int c) const { return v[r][c]; }
  496. inline Element_Type & at(int r, int c) { return v[r][c]; }
  497. inline Matrix44() { }
  498. inline Matrix44& operator += (const Matrix44& a)
  499. {
  500. for (int r = 0; r < NUM_ROWS; r++)
  501. {
  502. at(r, 0) += a.at(r, 0);
  503. at(r, 1) += a.at(r, 1);
  504. at(r, 2) += a.at(r, 2);
  505. at(r, 3) += a.at(r, 3);
  506. }
  507. return *this;
  508. }
  509. inline Matrix44& operator -= (const Matrix44& a)
  510. {
  511. for (int r = 0; r < NUM_ROWS; r++)
  512. {
  513. at(r, 0) -= a.at(r, 0);
  514. at(r, 1) -= a.at(r, 1);
  515. at(r, 2) -= a.at(r, 2);
  516. at(r, 3) -= a.at(r, 3);
  517. }
  518. return *this;
  519. }
  520. friend inline Matrix44 operator + (const Matrix44& a, const Matrix44& b)
  521. {
  522. Matrix44 ret;
  523. for (int r = 0; r < NUM_ROWS; r++)
  524. {
  525. ret.at(r, 0) = a.at(r, 0) + b.at(r, 0);
  526. ret.at(r, 1) = a.at(r, 1) + b.at(r, 1);
  527. ret.at(r, 2) = a.at(r, 2) + b.at(r, 2);
  528. ret.at(r, 3) = a.at(r, 3) + b.at(r, 3);
  529. }
  530. return ret;
  531. }
  532. friend inline Matrix44 operator - (const Matrix44& a, const Matrix44& b)
  533. {
  534. Matrix44 ret;
  535. for (int r = 0; r < NUM_ROWS; r++)
  536. {
  537. ret.at(r, 0) = a.at(r, 0) - b.at(r, 0);
  538. ret.at(r, 1) = a.at(r, 1) - b.at(r, 1);
  539. ret.at(r, 2) = a.at(r, 2) - b.at(r, 2);
  540. ret.at(r, 3) = a.at(r, 3) - b.at(r, 3);
  541. }
  542. return ret;
  543. }
  544. static inline void add_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
  545. {
  546. for (int r = 0; r < 4; r++)
  547. {
  548. pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) + b.at(r, 0));
  549. pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) + b.at(r, 1));
  550. pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) + b.at(r, 2));
  551. pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) + b.at(r, 3));
  552. }
  553. }
  554. static inline void sub_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
  555. {
  556. for (int r = 0; r < 4; r++)
  557. {
  558. pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) - b.at(r, 0));
  559. pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) - b.at(r, 1));
  560. pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) - b.at(r, 2));
  561. pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) - b.at(r, 3));
  562. }
  563. }
  564. };
  565. const int FRACT_BITS = 10;
  566. const int SCALE = 1 << FRACT_BITS;
  567. typedef int Temp_Type;
  568. #define D(i) (((i) + (SCALE >> 1)) >> FRACT_BITS)
  569. #define F(i) ((int)((i) * SCALE + .5f))
  570. // Any decent C++ compiler will optimize this at compile time to a 0, or an array access.
  571. #define AT(c, r) ((((c)>=NUM_COLS)||((r)>=NUM_ROWS)) ? 0 : pSrc[(c)+(r)*8])
  572. // NUM_ROWS/NUM_COLS = # of non-zero rows/cols in input matrix
  573. template<int NUM_ROWS, int NUM_COLS>
  574. struct P_Q
  575. {
  576. static void calc(Matrix44& P, Matrix44& Q, const jpgd_block_t* pSrc)
  577. {
  578. // 4x8 = 4x8 times 8x8, matrix 0 is constant
  579. const Temp_Type X000 = AT(0, 0);
  580. const Temp_Type X001 = AT(0, 1);
  581. const Temp_Type X002 = AT(0, 2);
  582. const Temp_Type X003 = AT(0, 3);
  583. const Temp_Type X004 = AT(0, 4);
  584. const Temp_Type X005 = AT(0, 5);
  585. const Temp_Type X006 = AT(0, 6);
  586. const Temp_Type X007 = AT(0, 7);
  587. const Temp_Type X010 = D(F(0.415735f) * AT(1, 0) + F(0.791065f) * AT(3, 0) + F(-0.352443f) * AT(5, 0) + F(0.277785f) * AT(7, 0));
  588. const Temp_Type X011 = D(F(0.415735f) * AT(1, 1) + F(0.791065f) * AT(3, 1) + F(-0.352443f) * AT(5, 1) + F(0.277785f) * AT(7, 1));
  589. const Temp_Type X012 = D(F(0.415735f) * AT(1, 2) + F(0.791065f) * AT(3, 2) + F(-0.352443f) * AT(5, 2) + F(0.277785f) * AT(7, 2));
  590. const Temp_Type X013 = D(F(0.415735f) * AT(1, 3) + F(0.791065f) * AT(3, 3) + F(-0.352443f) * AT(5, 3) + F(0.277785f) * AT(7, 3));
  591. const Temp_Type X014 = D(F(0.415735f) * AT(1, 4) + F(0.791065f) * AT(3, 4) + F(-0.352443f) * AT(5, 4) + F(0.277785f) * AT(7, 4));
  592. const Temp_Type X015 = D(F(0.415735f) * AT(1, 5) + F(0.791065f) * AT(3, 5) + F(-0.352443f) * AT(5, 5) + F(0.277785f) * AT(7, 5));
  593. const Temp_Type X016 = D(F(0.415735f) * AT(1, 6) + F(0.791065f) * AT(3, 6) + F(-0.352443f) * AT(5, 6) + F(0.277785f) * AT(7, 6));
  594. const Temp_Type X017 = D(F(0.415735f) * AT(1, 7) + F(0.791065f) * AT(3, 7) + F(-0.352443f) * AT(5, 7) + F(0.277785f) * AT(7, 7));
  595. const Temp_Type X020 = AT(4, 0);
  596. const Temp_Type X021 = AT(4, 1);
  597. const Temp_Type X022 = AT(4, 2);
  598. const Temp_Type X023 = AT(4, 3);
  599. const Temp_Type X024 = AT(4, 4);
  600. const Temp_Type X025 = AT(4, 5);
  601. const Temp_Type X026 = AT(4, 6);
  602. const Temp_Type X027 = AT(4, 7);
  603. const Temp_Type X030 = D(F(0.022887f) * AT(1, 0) + F(-0.097545f) * AT(3, 0) + F(0.490393f) * AT(5, 0) + F(0.865723f) * AT(7, 0));
  604. const Temp_Type X031 = D(F(0.022887f) * AT(1, 1) + F(-0.097545f) * AT(3, 1) + F(0.490393f) * AT(5, 1) + F(0.865723f) * AT(7, 1));
  605. const Temp_Type X032 = D(F(0.022887f) * AT(1, 2) + F(-0.097545f) * AT(3, 2) + F(0.490393f) * AT(5, 2) + F(0.865723f) * AT(7, 2));
  606. const Temp_Type X033 = D(F(0.022887f) * AT(1, 3) + F(-0.097545f) * AT(3, 3) + F(0.490393f) * AT(5, 3) + F(0.865723f) * AT(7, 3));
  607. const Temp_Type X034 = D(F(0.022887f) * AT(1, 4) + F(-0.097545f) * AT(3, 4) + F(0.490393f) * AT(5, 4) + F(0.865723f) * AT(7, 4));
  608. const Temp_Type X035 = D(F(0.022887f) * AT(1, 5) + F(-0.097545f) * AT(3, 5) + F(0.490393f) * AT(5, 5) + F(0.865723f) * AT(7, 5));
  609. const Temp_Type X036 = D(F(0.022887f) * AT(1, 6) + F(-0.097545f) * AT(3, 6) + F(0.490393f) * AT(5, 6) + F(0.865723f) * AT(7, 6));
  610. const Temp_Type X037 = D(F(0.022887f) * AT(1, 7) + F(-0.097545f) * AT(3, 7) + F(0.490393f) * AT(5, 7) + F(0.865723f) * AT(7, 7));
  611. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  612. P.at(0, 0) = X000;
  613. P.at(0, 1) = D(X001 * F(0.415735f) + X003 * F(0.791065f) + X005 * F(-0.352443f) + X007 * F(0.277785f));
  614. P.at(0, 2) = X004;
  615. P.at(0, 3) = D(X001 * F(0.022887f) + X003 * F(-0.097545f) + X005 * F(0.490393f) + X007 * F(0.865723f));
  616. P.at(1, 0) = X010;
  617. P.at(1, 1) = D(X011 * F(0.415735f) + X013 * F(0.791065f) + X015 * F(-0.352443f) + X017 * F(0.277785f));
  618. P.at(1, 2) = X014;
  619. P.at(1, 3) = D(X011 * F(0.022887f) + X013 * F(-0.097545f) + X015 * F(0.490393f) + X017 * F(0.865723f));
  620. P.at(2, 0) = X020;
  621. P.at(2, 1) = D(X021 * F(0.415735f) + X023 * F(0.791065f) + X025 * F(-0.352443f) + X027 * F(0.277785f));
  622. P.at(2, 2) = X024;
  623. P.at(2, 3) = D(X021 * F(0.022887f) + X023 * F(-0.097545f) + X025 * F(0.490393f) + X027 * F(0.865723f));
  624. P.at(3, 0) = X030;
  625. P.at(3, 1) = D(X031 * F(0.415735f) + X033 * F(0.791065f) + X035 * F(-0.352443f) + X037 * F(0.277785f));
  626. P.at(3, 2) = X034;
  627. P.at(3, 3) = D(X031 * F(0.022887f) + X033 * F(-0.097545f) + X035 * F(0.490393f) + X037 * F(0.865723f));
  628. // 40 muls 24 adds
  629. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  630. Q.at(0, 0) = D(X001 * F(0.906127f) + X003 * F(-0.318190f) + X005 * F(0.212608f) + X007 * F(-0.180240f));
  631. Q.at(0, 1) = X002;
  632. Q.at(0, 2) = D(X001 * F(-0.074658f) + X003 * F(0.513280f) + X005 * F(0.768178f) + X007 * F(-0.375330f));
  633. Q.at(0, 3) = X006;
  634. Q.at(1, 0) = D(X011 * F(0.906127f) + X013 * F(-0.318190f) + X015 * F(0.212608f) + X017 * F(-0.180240f));
  635. Q.at(1, 1) = X012;
  636. Q.at(1, 2) = D(X011 * F(-0.074658f) + X013 * F(0.513280f) + X015 * F(0.768178f) + X017 * F(-0.375330f));
  637. Q.at(1, 3) = X016;
  638. Q.at(2, 0) = D(X021 * F(0.906127f) + X023 * F(-0.318190f) + X025 * F(0.212608f) + X027 * F(-0.180240f));
  639. Q.at(2, 1) = X022;
  640. Q.at(2, 2) = D(X021 * F(-0.074658f) + X023 * F(0.513280f) + X025 * F(0.768178f) + X027 * F(-0.375330f));
  641. Q.at(2, 3) = X026;
  642. Q.at(3, 0) = D(X031 * F(0.906127f) + X033 * F(-0.318190f) + X035 * F(0.212608f) + X037 * F(-0.180240f));
  643. Q.at(3, 1) = X032;
  644. Q.at(3, 2) = D(X031 * F(-0.074658f) + X033 * F(0.513280f) + X035 * F(0.768178f) + X037 * F(-0.375330f));
  645. Q.at(3, 3) = X036;
  646. // 40 muls 24 adds
  647. }
  648. };
  649. template<int NUM_ROWS, int NUM_COLS>
  650. struct R_S
  651. {
  652. static void calc(Matrix44& R, Matrix44& S, const jpgd_block_t* pSrc)
  653. {
  654. // 4x8 = 4x8 times 8x8, matrix 0 is constant
  655. const Temp_Type X100 = D(F(0.906127f) * AT(1, 0) + F(-0.318190f) * AT(3, 0) + F(0.212608f) * AT(5, 0) + F(-0.180240f) * AT(7, 0));
  656. const Temp_Type X101 = D(F(0.906127f) * AT(1, 1) + F(-0.318190f) * AT(3, 1) + F(0.212608f) * AT(5, 1) + F(-0.180240f) * AT(7, 1));
  657. const Temp_Type X102 = D(F(0.906127f) * AT(1, 2) + F(-0.318190f) * AT(3, 2) + F(0.212608f) * AT(5, 2) + F(-0.180240f) * AT(7, 2));
  658. const Temp_Type X103 = D(F(0.906127f) * AT(1, 3) + F(-0.318190f) * AT(3, 3) + F(0.212608f) * AT(5, 3) + F(-0.180240f) * AT(7, 3));
  659. const Temp_Type X104 = D(F(0.906127f) * AT(1, 4) + F(-0.318190f) * AT(3, 4) + F(0.212608f) * AT(5, 4) + F(-0.180240f) * AT(7, 4));
  660. const Temp_Type X105 = D(F(0.906127f) * AT(1, 5) + F(-0.318190f) * AT(3, 5) + F(0.212608f) * AT(5, 5) + F(-0.180240f) * AT(7, 5));
  661. const Temp_Type X106 = D(F(0.906127f) * AT(1, 6) + F(-0.318190f) * AT(3, 6) + F(0.212608f) * AT(5, 6) + F(-0.180240f) * AT(7, 6));
  662. const Temp_Type X107 = D(F(0.906127f) * AT(1, 7) + F(-0.318190f) * AT(3, 7) + F(0.212608f) * AT(5, 7) + F(-0.180240f) * AT(7, 7));
  663. const Temp_Type X110 = AT(2, 0);
  664. const Temp_Type X111 = AT(2, 1);
  665. const Temp_Type X112 = AT(2, 2);
  666. const Temp_Type X113 = AT(2, 3);
  667. const Temp_Type X114 = AT(2, 4);
  668. const Temp_Type X115 = AT(2, 5);
  669. const Temp_Type X116 = AT(2, 6);
  670. const Temp_Type X117 = AT(2, 7);
  671. const Temp_Type X120 = D(F(-0.074658f) * AT(1, 0) + F(0.513280f) * AT(3, 0) + F(0.768178f) * AT(5, 0) + F(-0.375330f) * AT(7, 0));
  672. const Temp_Type X121 = D(F(-0.074658f) * AT(1, 1) + F(0.513280f) * AT(3, 1) + F(0.768178f) * AT(5, 1) + F(-0.375330f) * AT(7, 1));
  673. const Temp_Type X122 = D(F(-0.074658f) * AT(1, 2) + F(0.513280f) * AT(3, 2) + F(0.768178f) * AT(5, 2) + F(-0.375330f) * AT(7, 2));
  674. const Temp_Type X123 = D(F(-0.074658f) * AT(1, 3) + F(0.513280f) * AT(3, 3) + F(0.768178f) * AT(5, 3) + F(-0.375330f) * AT(7, 3));
  675. const Temp_Type X124 = D(F(-0.074658f) * AT(1, 4) + F(0.513280f) * AT(3, 4) + F(0.768178f) * AT(5, 4) + F(-0.375330f) * AT(7, 4));
  676. const Temp_Type X125 = D(F(-0.074658f) * AT(1, 5) + F(0.513280f) * AT(3, 5) + F(0.768178f) * AT(5, 5) + F(-0.375330f) * AT(7, 5));
  677. const Temp_Type X126 = D(F(-0.074658f) * AT(1, 6) + F(0.513280f) * AT(3, 6) + F(0.768178f) * AT(5, 6) + F(-0.375330f) * AT(7, 6));
  678. const Temp_Type X127 = D(F(-0.074658f) * AT(1, 7) + F(0.513280f) * AT(3, 7) + F(0.768178f) * AT(5, 7) + F(-0.375330f) * AT(7, 7));
  679. const Temp_Type X130 = AT(6, 0);
  680. const Temp_Type X131 = AT(6, 1);
  681. const Temp_Type X132 = AT(6, 2);
  682. const Temp_Type X133 = AT(6, 3);
  683. const Temp_Type X134 = AT(6, 4);
  684. const Temp_Type X135 = AT(6, 5);
  685. const Temp_Type X136 = AT(6, 6);
  686. const Temp_Type X137 = AT(6, 7);
  687. // 80 muls 48 adds
  688. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  689. R.at(0, 0) = X100;
  690. R.at(0, 1) = D(X101 * F(0.415735f) + X103 * F(0.791065f) + X105 * F(-0.352443f) + X107 * F(0.277785f));
  691. R.at(0, 2) = X104;
  692. R.at(0, 3) = D(X101 * F(0.022887f) + X103 * F(-0.097545f) + X105 * F(0.490393f) + X107 * F(0.865723f));
  693. R.at(1, 0) = X110;
  694. R.at(1, 1) = D(X111 * F(0.415735f) + X113 * F(0.791065f) + X115 * F(-0.352443f) + X117 * F(0.277785f));
  695. R.at(1, 2) = X114;
  696. R.at(1, 3) = D(X111 * F(0.022887f) + X113 * F(-0.097545f) + X115 * F(0.490393f) + X117 * F(0.865723f));
  697. R.at(2, 0) = X120;
  698. R.at(2, 1) = D(X121 * F(0.415735f) + X123 * F(0.791065f) + X125 * F(-0.352443f) + X127 * F(0.277785f));
  699. R.at(2, 2) = X124;
  700. R.at(2, 3) = D(X121 * F(0.022887f) + X123 * F(-0.097545f) + X125 * F(0.490393f) + X127 * F(0.865723f));
  701. R.at(3, 0) = X130;
  702. R.at(3, 1) = D(X131 * F(0.415735f) + X133 * F(0.791065f) + X135 * F(-0.352443f) + X137 * F(0.277785f));
  703. R.at(3, 2) = X134;
  704. R.at(3, 3) = D(X131 * F(0.022887f) + X133 * F(-0.097545f) + X135 * F(0.490393f) + X137 * F(0.865723f));
  705. // 40 muls 24 adds
  706. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  707. S.at(0, 0) = D(X101 * F(0.906127f) + X103 * F(-0.318190f) + X105 * F(0.212608f) + X107 * F(-0.180240f));
  708. S.at(0, 1) = X102;
  709. S.at(0, 2) = D(X101 * F(-0.074658f) + X103 * F(0.513280f) + X105 * F(0.768178f) + X107 * F(-0.375330f));
  710. S.at(0, 3) = X106;
  711. S.at(1, 0) = D(X111 * F(0.906127f) + X113 * F(-0.318190f) + X115 * F(0.212608f) + X117 * F(-0.180240f));
  712. S.at(1, 1) = X112;
  713. S.at(1, 2) = D(X111 * F(-0.074658f) + X113 * F(0.513280f) + X115 * F(0.768178f) + X117 * F(-0.375330f));
  714. S.at(1, 3) = X116;
  715. S.at(2, 0) = D(X121 * F(0.906127f) + X123 * F(-0.318190f) + X125 * F(0.212608f) + X127 * F(-0.180240f));
  716. S.at(2, 1) = X122;
  717. S.at(2, 2) = D(X121 * F(-0.074658f) + X123 * F(0.513280f) + X125 * F(0.768178f) + X127 * F(-0.375330f));
  718. S.at(2, 3) = X126;
  719. S.at(3, 0) = D(X131 * F(0.906127f) + X133 * F(-0.318190f) + X135 * F(0.212608f) + X137 * F(-0.180240f));
  720. S.at(3, 1) = X132;
  721. S.at(3, 2) = D(X131 * F(-0.074658f) + X133 * F(0.513280f) + X135 * F(0.768178f) + X137 * F(-0.375330f));
  722. S.at(3, 3) = X136;
  723. // 40 muls 24 adds
  724. }
  725. };
  726. } // end namespace DCT_Upsample
  727. // Unconditionally frees all allocated m_blocks.
  728. void jpeg_decoder::free_all_blocks()
  729. {
  730. m_pStream = NULL;
  731. for (mem_block *b = m_pMem_blocks; b; )
  732. {
  733. mem_block *n = b->m_pNext;
  734. jpgd_free(b);
  735. b = n;
  736. }
  737. m_pMem_blocks = NULL;
  738. }
  739. // This method handles all errors. It will never return.
  740. // It could easily be changed to use C++ exceptions.
  741. JPGD_NORETURN void jpeg_decoder::stop_decoding(jpgd_status status)
  742. {
  743. m_error_code = status;
  744. free_all_blocks();
  745. longjmp(m_jmp_state, status);
  746. }
  747. void *jpeg_decoder::alloc(size_t nSize, bool zero)
  748. {
  749. nSize = (JPGD_MAX(nSize, 1) + 3) & ~3;
  750. char *rv = NULL;
  751. for (mem_block *b = m_pMem_blocks; b; b = b->m_pNext)
  752. {
  753. if ((b->m_used_count + nSize) <= b->m_size)
  754. {
  755. rv = b->m_data + b->m_used_count;
  756. b->m_used_count += nSize;
  757. break;
  758. }
  759. }
  760. if (!rv)
  761. {
  762. int capacity = JPGD_MAX(32768 - 256, (nSize + 2047) & ~2047);
  763. mem_block *b = (mem_block*)jpgd_malloc(sizeof(mem_block) + capacity);
  764. if (!b) { stop_decoding(JPGD_NOTENOUGHMEM); }
  765. b->m_pNext = m_pMem_blocks; m_pMem_blocks = b;
  766. b->m_used_count = nSize;
  767. b->m_size = capacity;
  768. rv = b->m_data;
  769. }
  770. if (zero) memset(rv, 0, nSize);
  771. return rv;
  772. }
  773. void jpeg_decoder::word_clear(void *p, uint16 c, uint n)
  774. {
  775. uint8 *pD = (uint8*)p;
  776. const uint8 l = c & 0xFF, h = (c >> 8) & 0xFF;
  777. while (n)
  778. {
  779. pD[0] = l; pD[1] = h; pD += 2;
  780. n--;
  781. }
  782. }
  783. // Refill the input buffer.
  784. // This method will sit in a loop until (A) the buffer is full or (B)
  785. // the stream's read() method reports and end of file condition.
  786. void jpeg_decoder::prep_in_buffer()
  787. {
  788. m_in_buf_left = 0;
  789. m_pIn_buf_ofs = m_in_buf;
  790. if (m_eof_flag)
  791. return;
  792. do
  793. {
  794. int bytes_read = m_pStream->read(m_in_buf + m_in_buf_left, JPGD_IN_BUF_SIZE - m_in_buf_left, &m_eof_flag);
  795. if (bytes_read == -1)
  796. stop_decoding(JPGD_STREAM_READ);
  797. m_in_buf_left += bytes_read;
  798. } while ((m_in_buf_left < JPGD_IN_BUF_SIZE) && (!m_eof_flag));
  799. m_total_bytes_read += m_in_buf_left;
  800. // Pad the end of the block with M_EOI (prevents the decompressor from going off the rails if the stream is invalid).
  801. // (This dates way back to when this decompressor was written in C/asm, and the all-asm Huffman decoder did some fancy things to increase perf.)
  802. word_clear(m_pIn_buf_ofs + m_in_buf_left, 0xD9FF, 64);
  803. }
  804. // Read a Huffman code table.
  805. void jpeg_decoder::read_dht_marker()
  806. {
  807. int i, index, count;
  808. uint8 huff_num[17];
  809. uint8 huff_val[256];
  810. uint num_left = get_bits(16);
  811. if (num_left < 2)
  812. stop_decoding(JPGD_BAD_DHT_MARKER);
  813. num_left -= 2;
  814. while (num_left)
  815. {
  816. index = get_bits(8);
  817. huff_num[0] = 0;
  818. count = 0;
  819. for (i = 1; i <= 16; i++)
  820. {
  821. huff_num[i] = static_cast<uint8>(get_bits(8));
  822. count += huff_num[i];
  823. }
  824. if (count > 255)
  825. stop_decoding(JPGD_BAD_DHT_COUNTS);
  826. for (i = 0; i < count; i++)
  827. huff_val[i] = static_cast<uint8>(get_bits(8));
  828. i = 1 + 16 + count;
  829. if (num_left < (uint)i)
  830. stop_decoding(JPGD_BAD_DHT_MARKER);
  831. num_left -= i;
  832. if ((index & 0x10) > 0x10)
  833. stop_decoding(JPGD_BAD_DHT_INDEX);
  834. index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAX_HUFF_TABLES >> 1);
  835. if (index >= JPGD_MAX_HUFF_TABLES)
  836. stop_decoding(JPGD_BAD_DHT_INDEX);
  837. if (!m_huff_num[index])
  838. m_huff_num[index] = (uint8 *)alloc(17);
  839. if (!m_huff_val[index])
  840. m_huff_val[index] = (uint8 *)alloc(256);
  841. m_huff_ac[index] = (index & 0x10) != 0;
  842. memcpy(m_huff_num[index], huff_num, 17);
  843. memcpy(m_huff_val[index], huff_val, 256);
  844. }
  845. }
  846. // Read a quantization table.
  847. void jpeg_decoder::read_dqt_marker()
  848. {
  849. int n, i, prec;
  850. uint num_left;
  851. uint temp;
  852. num_left = get_bits(16);
  853. if (num_left < 2)
  854. stop_decoding(JPGD_BAD_DQT_MARKER);
  855. num_left -= 2;
  856. while (num_left)
  857. {
  858. n = get_bits(8);
  859. prec = n >> 4;
  860. n &= 0x0F;
  861. if (n >= JPGD_MAX_QUANT_TABLES)
  862. stop_decoding(JPGD_BAD_DQT_TABLE);
  863. if (!m_quant[n])
  864. m_quant[n] = (jpgd_quant_t *)alloc(64 * sizeof(jpgd_quant_t));
  865. // read quantization entries, in zag order
  866. for (i = 0; i < 64; i++)
  867. {
  868. temp = get_bits(8);
  869. if (prec)
  870. temp = (temp << 8) + get_bits(8);
  871. m_quant[n][i] = static_cast<jpgd_quant_t>(temp);
  872. }
  873. i = 64 + 1;
  874. if (prec)
  875. i += 64;
  876. if (num_left < (uint)i)
  877. stop_decoding(JPGD_BAD_DQT_LENGTH);
  878. num_left -= i;
  879. }
  880. }
  881. // Read the start of frame (SOF) marker.
  882. void jpeg_decoder::read_sof_marker()
  883. {
  884. int i;
  885. uint num_left;
  886. num_left = get_bits(16);
  887. if (get_bits(8) != 8) /* precision: sorry, only 8-bit precision is supported right now */
  888. stop_decoding(JPGD_BAD_PRECISION);
  889. m_image_y_size = get_bits(16);
  890. if ((m_image_y_size < 1) || (m_image_y_size > JPGD_MAX_HEIGHT))
  891. stop_decoding(JPGD_BAD_HEIGHT);
  892. m_image_x_size = get_bits(16);
  893. if ((m_image_x_size < 1) || (m_image_x_size > JPGD_MAX_WIDTH))
  894. stop_decoding(JPGD_BAD_WIDTH);
  895. m_comps_in_frame = get_bits(8);
  896. if (m_comps_in_frame > JPGD_MAX_COMPONENTS)
  897. stop_decoding(JPGD_TOO_MANY_COMPONENTS);
  898. if (num_left != (uint)(m_comps_in_frame * 3 + 8))
  899. stop_decoding(JPGD_BAD_SOF_LENGTH);
  900. for (i = 0; i < m_comps_in_frame; i++)
  901. {
  902. m_comp_ident[i] = get_bits(8);
  903. m_comp_h_samp[i] = get_bits(4);
  904. m_comp_v_samp[i] = get_bits(4);
  905. m_comp_quant[i] = get_bits(8);
  906. }
  907. }
  908. // Used to skip unrecognized markers.
  909. void jpeg_decoder::skip_variable_marker()
  910. {
  911. uint num_left;
  912. num_left = get_bits(16);
  913. if (num_left < 2)
  914. stop_decoding(JPGD_BAD_VARIABLE_MARKER);
  915. num_left -= 2;
  916. while (num_left)
  917. {
  918. get_bits(8);
  919. num_left--;
  920. }
  921. }
  922. // Read a define restart interval (DRI) marker.
  923. void jpeg_decoder::read_dri_marker()
  924. {
  925. if (get_bits(16) != 4)
  926. stop_decoding(JPGD_BAD_DRI_LENGTH);
  927. m_restart_interval = get_bits(16);
  928. }
  929. // Read a start of scan (SOS) marker.
  930. void jpeg_decoder::read_sos_marker()
  931. {
  932. uint num_left;
  933. int i, ci, n, c, cc;
  934. num_left = get_bits(16);
  935. n = get_bits(8);
  936. m_comps_in_scan = n;
  937. num_left -= 3;
  938. if ( (num_left != (uint)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAX_COMPS_IN_SCAN) )
  939. stop_decoding(JPGD_BAD_SOS_LENGTH);
  940. for (i = 0; i < n; i++)
  941. {
  942. cc = get_bits(8);
  943. c = get_bits(8);
  944. num_left -= 2;
  945. for (ci = 0; ci < m_comps_in_frame; ci++)
  946. if (cc == m_comp_ident[ci])
  947. break;
  948. if (ci >= m_comps_in_frame)
  949. stop_decoding(JPGD_BAD_SOS_COMP_ID);
  950. m_comp_list[i] = ci;
  951. m_comp_dc_tab[ci] = (c >> 4) & 15;
  952. m_comp_ac_tab[ci] = (c & 15) + (JPGD_MAX_HUFF_TABLES >> 1);
  953. }
  954. m_spectral_start = get_bits(8);
  955. m_spectral_end = get_bits(8);
  956. m_successive_high = get_bits(4);
  957. m_successive_low = get_bits(4);
  958. if (!m_progressive_flag)
  959. {
  960. m_spectral_start = 0;
  961. m_spectral_end = 63;
  962. }
  963. num_left -= 3;
  964. while (num_left) /* read past whatever is num_left */
  965. {
  966. get_bits(8);
  967. num_left--;
  968. }
  969. }
  970. // Finds the next marker.
  971. int jpeg_decoder::next_marker()
  972. {
  973. uint c, bytes;
  974. bytes = 0;
  975. do
  976. {
  977. do
  978. {
  979. bytes++;
  980. c = get_bits(8);
  981. } while (c != 0xFF);
  982. do
  983. {
  984. c = get_bits(8);
  985. } while (c == 0xFF);
  986. } while (c == 0);
  987. // If bytes > 0 here, there where extra bytes before the marker (not good).
  988. return c;
  989. }
  990. // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
  991. // encountered.
  992. int jpeg_decoder::process_markers()
  993. {
  994. int c;
  995. for ( ; ; )
  996. {
  997. c = next_marker();
  998. switch (c)
  999. {
  1000. case M_SOF0:
  1001. case M_SOF1:
  1002. case M_SOF2:
  1003. case M_SOF3:
  1004. case M_SOF5:
  1005. case M_SOF6:
  1006. case M_SOF7:
  1007. // case M_JPG:
  1008. case M_SOF9:
  1009. case M_SOF10:
  1010. case M_SOF11:
  1011. case M_SOF13:
  1012. case M_SOF14:
  1013. case M_SOF15:
  1014. case M_SOI:
  1015. case M_EOI:
  1016. case M_SOS:
  1017. {
  1018. return c;
  1019. }
  1020. case M_DHT:
  1021. {
  1022. read_dht_marker();
  1023. break;
  1024. }
  1025. // No arithmitic support - dumb patents!
  1026. case M_DAC:
  1027. {
  1028. stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
  1029. break;
  1030. }
  1031. case M_DQT:
  1032. {
  1033. read_dqt_marker();
  1034. break;
  1035. }
  1036. case M_DRI:
  1037. {
  1038. read_dri_marker();
  1039. break;
  1040. }
  1041. //case M_APP0: /* no need to read the JFIF marker */
  1042. case M_JPG:
  1043. case M_RST0: /* no parameters */
  1044. case M_RST1:
  1045. case M_RST2:
  1046. case M_RST3:
  1047. case M_RST4:
  1048. case M_RST5:
  1049. case M_RST6:
  1050. case M_RST7:
  1051. case M_TEM:
  1052. {
  1053. stop_decoding(JPGD_UNEXPECTED_MARKER);
  1054. break;
  1055. }
  1056. default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
  1057. {
  1058. skip_variable_marker();
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. // Finds the start of image (SOI) marker.
  1065. // This code is rather defensive: it only checks the first 512 bytes to avoid
  1066. // false positives.
  1067. void jpeg_decoder::locate_soi_marker()
  1068. {
  1069. uint lastchar, thischar;
  1070. uint bytesleft;
  1071. lastchar = get_bits(8);
  1072. thischar = get_bits(8);
  1073. /* ok if it's a normal JPEG file without a special header */
  1074. if ((lastchar == 0xFF) && (thischar == M_SOI))
  1075. return;
  1076. bytesleft = 4096; //512;
  1077. for ( ; ; )
  1078. {
  1079. if (--bytesleft == 0)
  1080. stop_decoding(JPGD_NOT_JPEG);
  1081. lastchar = thischar;
  1082. thischar = get_bits(8);
  1083. if (lastchar == 0xFF)
  1084. {
  1085. if (thischar == M_SOI)
  1086. break;
  1087. else if (thischar == M_EOI) // get_bits will keep returning M_EOI if we read past the end
  1088. stop_decoding(JPGD_NOT_JPEG);
  1089. }
  1090. }
  1091. // Check the next character after marker: if it's not 0xFF, it can't be the start of the next marker, so the file is bad.
  1092. thischar = (m_bit_buf >> 24) & 0xFF;
  1093. if (thischar != 0xFF)
  1094. stop_decoding(JPGD_NOT_JPEG);
  1095. }
  1096. // Find a start of frame (SOF) marker.
  1097. void jpeg_decoder::locate_sof_marker()
  1098. {
  1099. locate_soi_marker();
  1100. int c = process_markers();
  1101. switch (c)
  1102. {
  1103. case M_SOF2:
  1104. m_progressive_flag = JPGD_TRUE;
  1105. case M_SOF0: /* baseline DCT */
  1106. case M_SOF1: /* extended sequential DCT */
  1107. {
  1108. read_sof_marker();
  1109. break;
  1110. }
  1111. case M_SOF9: /* Arithmitic coding */
  1112. {
  1113. stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
  1114. break;
  1115. }
  1116. default:
  1117. {
  1118. stop_decoding(JPGD_UNSUPPORTED_MARKER);
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. // Find a start of scan (SOS) marker.
  1124. int jpeg_decoder::locate_sos_marker()
  1125. {
  1126. int c;
  1127. c = process_markers();
  1128. if (c == M_EOI)
  1129. return JPGD_FALSE;
  1130. else if (c != M_SOS)
  1131. stop_decoding(JPGD_UNEXPECTED_MARKER);
  1132. read_sos_marker();
  1133. return JPGD_TRUE;
  1134. }
  1135. // Reset everything to default/uninitialized state.
  1136. void jpeg_decoder::init(jpeg_decoder_stream *pStream)
  1137. {
  1138. m_pMem_blocks = NULL;
  1139. m_error_code = JPGD_SUCCESS;
  1140. m_ready_flag = false;
  1141. m_image_x_size = m_image_y_size = 0;
  1142. m_pStream = pStream;
  1143. m_progressive_flag = JPGD_FALSE;
  1144. memset(m_huff_ac, 0, sizeof(m_huff_ac));
  1145. memset(m_huff_num, 0, sizeof(m_huff_num));
  1146. memset(m_huff_val, 0, sizeof(m_huff_val));
  1147. memset(m_quant, 0, sizeof(m_quant));
  1148. m_scan_type = 0;
  1149. m_comps_in_frame = 0;
  1150. memset(m_comp_h_samp, 0, sizeof(m_comp_h_samp));
  1151. memset(m_comp_v_samp, 0, sizeof(m_comp_v_samp));
  1152. memset(m_comp_quant, 0, sizeof(m_comp_quant));
  1153. memset(m_comp_ident, 0, sizeof(m_comp_ident));
  1154. memset(m_comp_h_blocks, 0, sizeof(m_comp_h_blocks));
  1155. memset(m_comp_v_blocks, 0, sizeof(m_comp_v_blocks));
  1156. m_comps_in_scan = 0;
  1157. memset(m_comp_list, 0, sizeof(m_comp_list));
  1158. memset(m_comp_dc_tab, 0, sizeof(m_comp_dc_tab));
  1159. memset(m_comp_ac_tab, 0, sizeof(m_comp_ac_tab));
  1160. m_spectral_start = 0;
  1161. m_spectral_end = 0;
  1162. m_successive_low = 0;
  1163. m_successive_high = 0;
  1164. m_max_mcu_x_size = 0;
  1165. m_max_mcu_y_size = 0;
  1166. m_blocks_per_mcu = 0;
  1167. m_max_blocks_per_row = 0;
  1168. m_mcus_per_row = 0;
  1169. m_mcus_per_col = 0;
  1170. m_expanded_blocks_per_component = 0;
  1171. m_expanded_blocks_per_mcu = 0;
  1172. m_expanded_blocks_per_row = 0;
  1173. m_freq_domain_chroma_upsample = false;
  1174. memset(m_mcu_org, 0, sizeof(m_mcu_org));
  1175. m_total_lines_left = 0;
  1176. m_mcu_lines_left = 0;
  1177. m_real_dest_bytes_per_scan_line = 0;
  1178. m_dest_bytes_per_scan_line = 0;
  1179. m_dest_bytes_per_pixel = 0;
  1180. memset(m_pHuff_tabs, 0, sizeof(m_pHuff_tabs));
  1181. memset(m_dc_coeffs, 0, sizeof(m_dc_coeffs));
  1182. memset(m_ac_coeffs, 0, sizeof(m_ac_coeffs));
  1183. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  1184. m_eob_run = 0;
  1185. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  1186. m_pIn_buf_ofs = m_in_buf;
  1187. m_in_buf_left = 0;
  1188. m_eof_flag = false;
  1189. m_tem_flag = 0;
  1190. memset(m_in_buf_pad_start, 0, sizeof(m_in_buf_pad_start));
  1191. memset(m_in_buf, 0, sizeof(m_in_buf));
  1192. memset(m_in_buf_pad_end, 0, sizeof(m_in_buf_pad_end));
  1193. m_restart_interval = 0;
  1194. m_restarts_left = 0;
  1195. m_next_restart_num = 0;
  1196. m_max_mcus_per_row = 0;
  1197. m_max_blocks_per_mcu = 0;
  1198. m_max_mcus_per_col = 0;
  1199. memset(m_last_dc_val, 0, sizeof(m_last_dc_val));
  1200. m_pMCU_coefficients = NULL;
  1201. m_pSample_buf = NULL;
  1202. m_total_bytes_read = 0;
  1203. m_pScan_line_0 = NULL;
  1204. m_pScan_line_1 = NULL;
  1205. // Ready the input buffer.
  1206. prep_in_buffer();
  1207. // Prime the bit buffer.
  1208. m_bits_left = 16;
  1209. m_bit_buf = 0;
  1210. get_bits(16);
  1211. get_bits(16);
  1212. for (int i = 0; i < JPGD_MAX_BLOCKS_PER_MCU; i++)
  1213. m_mcu_block_max_zag[i] = 64;
  1214. }
  1215. #define SCALEBITS 16
  1216. #define ONE_HALF ((int) 1 << (SCALEBITS-1))
  1217. #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5f))
  1218. // Create a few tables that allow us to quickly convert YCbCr to RGB.
  1219. void jpeg_decoder::create_look_ups()
  1220. {
  1221. for (int i = 0; i <= 255; i++)
  1222. {
  1223. int k = i - 128;
  1224. m_crr[i] = ( FIX(1.40200f) * k + ONE_HALF) >> SCALEBITS;
  1225. m_cbb[i] = ( FIX(1.77200f) * k + ONE_HALF) >> SCALEBITS;
  1226. m_crg[i] = (-FIX(0.71414f)) * k;
  1227. m_cbg[i] = (-FIX(0.34414f)) * k + ONE_HALF;
  1228. }
  1229. }
  1230. // This method throws back into the stream any bytes that where read
  1231. // into the bit buffer during initial marker scanning.
  1232. void jpeg_decoder::fix_in_buffer()
  1233. {
  1234. // In case any 0xFF's where pulled into the buffer during marker scanning.
  1235. JPGD_ASSERT((m_bits_left & 7) == 0);
  1236. if (m_bits_left == 16)
  1237. stuff_char( (uint8)(m_bit_buf & 0xFF));
  1238. if (m_bits_left >= 8)
  1239. stuff_char( (uint8)((m_bit_buf >> 8) & 0xFF));
  1240. stuff_char((uint8)((m_bit_buf >> 16) & 0xFF));
  1241. stuff_char((uint8)((m_bit_buf >> 24) & 0xFF));
  1242. m_bits_left = 16;
  1243. get_bits_no_markers(16);
  1244. get_bits_no_markers(16);
  1245. }
  1246. void jpeg_decoder::transform_mcu(int mcu_row)
  1247. {
  1248. jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
  1249. if (m_freq_domain_chroma_upsample) {
  1250. JPGD_ASSERT(mcu_row * m_blocks_per_mcu < m_expanded_blocks_per_row);
  1251. }
  1252. else {
  1253. JPGD_ASSERT(mcu_row * m_blocks_per_mcu < m_max_blocks_per_row);
  1254. }
  1255. uint8* pDst_ptr = m_pSample_buf + mcu_row * m_blocks_per_mcu * 64;
  1256. for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  1257. {
  1258. idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
  1259. pSrc_ptr += 64;
  1260. pDst_ptr += 64;
  1261. }
  1262. }
  1263. static const uint8 s_max_rc[64] =
  1264. {
  1265. 17, 18, 34, 50, 50, 51, 52, 52, 52, 68, 84, 84, 84, 84, 85, 86, 86, 86, 86, 86,
  1266. 102, 118, 118, 118, 118, 118, 118, 119, 120, 120, 120, 120, 120, 120, 120, 136,
  1267. 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
  1268. 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136
  1269. };
  1270. void jpeg_decoder::transform_mcu_expand(int mcu_row)
  1271. {
  1272. jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
  1273. uint8* pDst_ptr = m_pSample_buf + mcu_row * m_expanded_blocks_per_mcu * 64;
  1274. // Y IDCT
  1275. int mcu_block;
  1276. for (mcu_block = 0; mcu_block < m_expanded_blocks_per_component; mcu_block++)
  1277. {
  1278. idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
  1279. pSrc_ptr += 64;
  1280. pDst_ptr += 64;
  1281. }
  1282. // Chroma IDCT, with upsampling
  1283. jpgd_block_t temp_block[64];
  1284. for (int i = 0; i < 2; i++)
  1285. {
  1286. DCT_Upsample::Matrix44 P, Q, R, S;
  1287. JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] >= 1);
  1288. JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] <= 64);
  1289. int max_zag = m_mcu_block_max_zag[mcu_block++] - 1;
  1290. if (max_zag <= 0) max_zag = 0; // should never happen, only here to shut up static analysis
  1291. switch (s_max_rc[max_zag])
  1292. {
  1293. case 1*16+1:
  1294. DCT_Upsample::P_Q<1, 1>::calc(P, Q, pSrc_ptr);
  1295. DCT_Upsample::R_S<1, 1>::calc(R, S, pSrc_ptr);
  1296. break;
  1297. case 1*16+2:
  1298. DCT_Upsample::P_Q<1, 2>::calc(P, Q, pSrc_ptr);
  1299. DCT_Upsample::R_S<1, 2>::calc(R, S, pSrc_ptr);
  1300. break;
  1301. case 2*16+2:
  1302. DCT_Upsample::P_Q<2, 2>::calc(P, Q, pSrc_ptr);
  1303. DCT_Upsample::R_S<2, 2>::calc(R, S, pSrc_ptr);
  1304. break;
  1305. case 3*16+2:
  1306. DCT_Upsample::P_Q<3, 2>::calc(P, Q, pSrc_ptr);
  1307. DCT_Upsample::R_S<3, 2>::calc(R, S, pSrc_ptr);
  1308. break;
  1309. case 3*16+3:
  1310. DCT_Upsample::P_Q<3, 3>::calc(P, Q, pSrc_ptr);
  1311. DCT_Upsample::R_S<3, 3>::calc(R, S, pSrc_ptr);
  1312. break;
  1313. case 3*16+4:
  1314. DCT_Upsample::P_Q<3, 4>::calc(P, Q, pSrc_ptr);
  1315. DCT_Upsample::R_S<3, 4>::calc(R, S, pSrc_ptr);
  1316. break;
  1317. case 4*16+4:
  1318. DCT_Upsample::P_Q<4, 4>::calc(P, Q, pSrc_ptr);
  1319. DCT_Upsample::R_S<4, 4>::calc(R, S, pSrc_ptr);
  1320. break;
  1321. case 5*16+4:
  1322. DCT_Upsample::P_Q<5, 4>::calc(P, Q, pSrc_ptr);
  1323. DCT_Upsample::R_S<5, 4>::calc(R, S, pSrc_ptr);
  1324. break;
  1325. case 5*16+5:
  1326. DCT_Upsample::P_Q<5, 5>::calc(P, Q, pSrc_ptr);
  1327. DCT_Upsample::R_S<5, 5>::calc(R, S, pSrc_ptr);
  1328. break;
  1329. case 5*16+6:
  1330. DCT_Upsample::P_Q<5, 6>::calc(P, Q, pSrc_ptr);
  1331. DCT_Upsample::R_S<5, 6>::calc(R, S, pSrc_ptr);
  1332. break;
  1333. case 6*16+6:
  1334. DCT_Upsample::P_Q<6, 6>::calc(P, Q, pSrc_ptr);
  1335. DCT_Upsample::R_S<6, 6>::calc(R, S, pSrc_ptr);
  1336. break;
  1337. case 7*16+6:
  1338. DCT_Upsample::P_Q<7, 6>::calc(P, Q, pSrc_ptr);
  1339. DCT_Upsample::R_S<7, 6>::calc(R, S, pSrc_ptr);
  1340. break;
  1341. case 7*16+7:
  1342. DCT_Upsample::P_Q<7, 7>::calc(P, Q, pSrc_ptr);
  1343. DCT_Upsample::R_S<7, 7>::calc(R, S, pSrc_ptr);
  1344. break;
  1345. case 7*16+8:
  1346. DCT_Upsample::P_Q<7, 8>::calc(P, Q, pSrc_ptr);
  1347. DCT_Upsample::R_S<7, 8>::calc(R, S, pSrc_ptr);
  1348. break;
  1349. case 8*16+8:
  1350. DCT_Upsample::P_Q<8, 8>::calc(P, Q, pSrc_ptr);
  1351. DCT_Upsample::R_S<8, 8>::calc(R, S, pSrc_ptr);
  1352. break;
  1353. default:
  1354. JPGD_ASSERT(false);
  1355. }
  1356. DCT_Upsample::Matrix44 a(P + Q); P -= Q;
  1357. DCT_Upsample::Matrix44& b = P;
  1358. DCT_Upsample::Matrix44 c(R + S); R -= S;
  1359. DCT_Upsample::Matrix44& d = R;
  1360. DCT_Upsample::Matrix44::add_and_store(temp_block, a, c);
  1361. idct_4x4(temp_block, pDst_ptr);
  1362. pDst_ptr += 64;
  1363. DCT_Upsample::Matrix44::sub_and_store(temp_block, a, c);
  1364. idct_4x4(temp_block, pDst_ptr);
  1365. pDst_ptr += 64;
  1366. DCT_Upsample::Matrix44::add_and_store(temp_block, b, d);
  1367. idct_4x4(temp_block, pDst_ptr);
  1368. pDst_ptr += 64;
  1369. DCT_Upsample::Matrix44::sub_and_store(temp_block, b, d);
  1370. idct_4x4(temp_block, pDst_ptr);
  1371. pDst_ptr += 64;
  1372. pSrc_ptr += 64;
  1373. }
  1374. }
  1375. // Loads and dequantizes the next row of (already decoded) coefficients.
  1376. // Progressive images only.
  1377. void jpeg_decoder::load_next_row()
  1378. {
  1379. int i;
  1380. jpgd_block_t *p;
  1381. jpgd_quant_t *q;
  1382. int mcu_row, mcu_block, row_block = 0;
  1383. int component_num, component_id;
  1384. int block_x_mcu[JPGD_MAX_COMPONENTS];
  1385. memset(block_x_mcu, 0, JPGD_MAX_COMPONENTS * sizeof(int));
  1386. for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  1387. {
  1388. int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
  1389. for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  1390. {
  1391. component_id = m_mcu_org[mcu_block];
  1392. JPGD_ASSERT(m_comp_quant[component_id] < JPGD_MAX_QUANT_TABLES);
  1393. q = m_quant[m_comp_quant[component_id]];
  1394. p = m_pMCU_coefficients + 64 * mcu_block;
  1395. jpgd_block_t* pAC = coeff_buf_getp(m_ac_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  1396. jpgd_block_t* pDC = coeff_buf_getp(m_dc_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  1397. p[0] = pDC[0];
  1398. memcpy(&p[1], &pAC[1], 63 * sizeof(jpgd_block_t));
  1399. for (i = 63; i > 0; i--)
  1400. if (p[g_ZAG[i]])
  1401. break;
  1402. m_mcu_block_max_zag[mcu_block] = i + 1;
  1403. for ( ; i >= 0; i--)
  1404. if (p[g_ZAG[i]])
  1405. p[g_ZAG[i]] = static_cast<jpgd_block_t>(p[g_ZAG[i]] * q[i]);
  1406. row_block++;
  1407. if (m_comps_in_scan == 1)
  1408. block_x_mcu[component_id]++;
  1409. else
  1410. {
  1411. if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
  1412. {
  1413. block_x_mcu_ofs = 0;
  1414. if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
  1415. {
  1416. block_y_mcu_ofs = 0;
  1417. block_x_mcu[component_id] += m_comp_h_samp[component_id];
  1418. }
  1419. }
  1420. }
  1421. }
  1422. if (m_freq_domain_chroma_upsample)
  1423. transform_mcu_expand(mcu_row);
  1424. else
  1425. transform_mcu(mcu_row);
  1426. }
  1427. if (m_comps_in_scan == 1)
  1428. m_block_y_mcu[m_comp_list[0]]++;
  1429. else
  1430. {
  1431. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  1432. {
  1433. component_id = m_comp_list[component_num];
  1434. m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
  1435. }
  1436. }
  1437. }
  1438. // Restart interval processing.
  1439. void jpeg_decoder::process_restart()
  1440. {
  1441. int i;
  1442. int c = 0;
  1443. // Align to a byte boundry
  1444. // FIXME: Is this really necessary? get_bits_no_markers() never reads in markers!
  1445. //get_bits_no_markers(m_bits_left & 7);
  1446. // Let's scan a little bit to find the marker, but not _too_ far.
  1447. // 1536 is a "fudge factor" that determines how much to scan.
  1448. for (i = 1536; i > 0; i--)
  1449. if (get_char() == 0xFF)
  1450. break;
  1451. if (i == 0)
  1452. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1453. for ( ; i > 0; i--)
  1454. if ((c = get_char()) != 0xFF)
  1455. break;
  1456. if (i == 0)
  1457. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1458. // Is it the expected marker? If not, something bad happened.
  1459. if (c != (m_next_restart_num + M_RST0))
  1460. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1461. // Reset each component's DC prediction values.
  1462. memset(&m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
  1463. m_eob_run = 0;
  1464. m_restarts_left = m_restart_interval;
  1465. m_next_restart_num = (m_next_restart_num + 1) & 7;
  1466. // Get the bit buffer going again...
  1467. m_bits_left = 16;
  1468. get_bits_no_markers(16);
  1469. get_bits_no_markers(16);
  1470. }
  1471. static inline int dequantize_ac(int c, int q) { c *= q; return c; }
  1472. // Decodes and dequantizes the next row of coefficients.
  1473. void jpeg_decoder::decode_next_row()
  1474. {
  1475. int row_block = 0;
  1476. for (int mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  1477. {
  1478. if ((m_restart_interval) && (m_restarts_left == 0))
  1479. process_restart();
  1480. jpgd_block_t* p = m_pMCU_coefficients;
  1481. for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++, p += 64)
  1482. {
  1483. int component_id = m_mcu_org[mcu_block];
  1484. JPGD_ASSERT(m_comp_quant[component_id] < JPGD_MAX_QUANT_TABLES);
  1485. jpgd_quant_t* q = m_quant[m_comp_quant[component_id]];
  1486. int r, s;
  1487. s = huff_decode(m_pHuff_tabs[m_comp_dc_tab[component_id]], r);
  1488. s = JPGD_HUFF_EXTEND(r, s);
  1489. m_last_dc_val[component_id] = (s += m_last_dc_val[component_id]);
  1490. p[0] = static_cast<jpgd_block_t>(s * q[0]);
  1491. int prev_num_set = m_mcu_block_max_zag[mcu_block];
  1492. huff_tables *pH = m_pHuff_tabs[m_comp_ac_tab[component_id]];
  1493. int k;
  1494. for (k = 1; k < 64; k++)
  1495. {
  1496. int extra_bits;
  1497. s = huff_decode(pH, extra_bits);
  1498. r = s >> 4;
  1499. s &= 15;
  1500. if (s)
  1501. {
  1502. if (r)
  1503. {
  1504. if ((k + r) > 63)
  1505. stop_decoding(JPGD_DECODE_ERROR);
  1506. if (k < prev_num_set)
  1507. {
  1508. int n = JPGD_MIN(r, prev_num_set - k);
  1509. int kt = k;
  1510. while (n--)
  1511. p[g_ZAG[kt++]] = 0;
  1512. }
  1513. k += r;
  1514. }
  1515. s = JPGD_HUFF_EXTEND(extra_bits, s);
  1516. JPGD_ASSERT(k < 64);
  1517. p[g_ZAG[k]] = static_cast<jpgd_block_t>(dequantize_ac(s, q[k])); //s * q[k];
  1518. }
  1519. else
  1520. {
  1521. if (r == 15)
  1522. {
  1523. if ((k + 16) > 64)
  1524. stop_decoding(JPGD_DECODE_ERROR);
  1525. if (k < prev_num_set)
  1526. {
  1527. int n = JPGD_MIN(16, prev_num_set - k);
  1528. int kt = k;
  1529. while (n--)
  1530. {
  1531. JPGD_ASSERT(kt <= 63);
  1532. p[g_ZAG[kt++]] = 0;
  1533. }
  1534. }
  1535. k += 16 - 1; // - 1 because the loop counter is k
  1536. JPGD_ASSERT(p[g_ZAG[k]] == 0);
  1537. }
  1538. else
  1539. break;
  1540. }
  1541. }
  1542. if (k < prev_num_set)
  1543. {
  1544. int kt = k;
  1545. while (kt < prev_num_set)
  1546. p[g_ZAG[kt++]] = 0;
  1547. }
  1548. m_mcu_block_max_zag[mcu_block] = k;
  1549. row_block++;
  1550. }
  1551. if (m_freq_domain_chroma_upsample)
  1552. transform_mcu_expand(mcu_row);
  1553. else
  1554. transform_mcu(mcu_row);
  1555. m_restarts_left--;
  1556. }
  1557. }
  1558. // YCbCr H1V1 (1x1:1:1, 3 m_blocks per MCU) to RGB
  1559. void jpeg_decoder::H1V1Convert()
  1560. {
  1561. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1562. uint8 *d = m_pScan_line_0;
  1563. uint8 *s = m_pSample_buf + row * 8;
  1564. for (int i = m_max_mcus_per_row; i > 0; i--)
  1565. {
  1566. for (int j = 0; j < 8; j++)
  1567. {
  1568. int y = s[j];
  1569. int cb = s[64+j];
  1570. int cr = s[128+j];
  1571. d[0] = clamp(y + m_crr[cr]);
  1572. d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
  1573. d[2] = clamp(y + m_cbb[cb]);
  1574. d[3] = 255;
  1575. d += 4;
  1576. }
  1577. s += 64*3;
  1578. }
  1579. }
  1580. // YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB
  1581. void jpeg_decoder::H2V1Convert()
  1582. {
  1583. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1584. uint8 *d0 = m_pScan_line_0;
  1585. uint8 *y = m_pSample_buf + row * 8;
  1586. uint8 *c = m_pSample_buf + 2*64 + row * 8;
  1587. for (int i = m_max_mcus_per_row; i > 0; i--)
  1588. {
  1589. for (int l = 0; l < 2; l++)
  1590. {
  1591. for (int j = 0; j < 4; j++)
  1592. {
  1593. int cb = c[0];
  1594. int cr = c[64];
  1595. int rc = m_crr[cr];
  1596. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1597. int bc = m_cbb[cb];
  1598. int yy = y[j<<1];
  1599. d0[0] = clamp(yy+rc);
  1600. d0[1] = clamp(yy+gc);
  1601. d0[2] = clamp(yy+bc);
  1602. d0[3] = 255;
  1603. yy = y[(j<<1)+1];
  1604. d0[4] = clamp(yy+rc);
  1605. d0[5] = clamp(yy+gc);
  1606. d0[6] = clamp(yy+bc);
  1607. d0[7] = 255;
  1608. d0 += 8;
  1609. c++;
  1610. }
  1611. y += 64;
  1612. }
  1613. y += 64*4 - 64*2;
  1614. c += 64*4 - 8;
  1615. }
  1616. }
  1617. // YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB
  1618. void jpeg_decoder::H1V2Convert()
  1619. {
  1620. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1621. uint8 *d0 = m_pScan_line_0;
  1622. uint8 *d1 = m_pScan_line_1;
  1623. uint8 *y;
  1624. uint8 *c;
  1625. if (row < 8)
  1626. y = m_pSample_buf + row * 8;
  1627. else
  1628. y = m_pSample_buf + 64*1 + (row & 7) * 8;
  1629. c = m_pSample_buf + 64*2 + (row >> 1) * 8;
  1630. for (int i = m_max_mcus_per_row; i > 0; i--)
  1631. {
  1632. for (int j = 0; j < 8; j++)
  1633. {
  1634. int cb = c[0+j];
  1635. int cr = c[64+j];
  1636. int rc = m_crr[cr];
  1637. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1638. int bc = m_cbb[cb];
  1639. int yy = y[j];
  1640. d0[0] = clamp(yy+rc);
  1641. d0[1] = clamp(yy+gc);
  1642. d0[2] = clamp(yy+bc);
  1643. d0[3] = 255;
  1644. yy = y[8+j];
  1645. d1[0] = clamp(yy+rc);
  1646. d1[1] = clamp(yy+gc);
  1647. d1[2] = clamp(yy+bc);
  1648. d1[3] = 255;
  1649. d0 += 4;
  1650. d1 += 4;
  1651. }
  1652. y += 64*4;
  1653. c += 64*4;
  1654. }
  1655. }
  1656. // YCbCr H2V2 (2x2:1:1, 6 m_blocks per MCU) to RGB
  1657. void jpeg_decoder::H2V2Convert()
  1658. {
  1659. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1660. uint8 *d0 = m_pScan_line_0;
  1661. uint8 *d1 = m_pScan_line_1;
  1662. uint8 *y;
  1663. uint8 *c;
  1664. if (row < 8)
  1665. y = m_pSample_buf + row * 8;
  1666. else
  1667. y = m_pSample_buf + 64*2 + (row & 7) * 8;
  1668. c = m_pSample_buf + 64*4 + (row >> 1) * 8;
  1669. for (int i = m_max_mcus_per_row; i > 0; i--)
  1670. {
  1671. for (int l = 0; l < 2; l++)
  1672. {
  1673. for (int j = 0; j < 8; j += 2)
  1674. {
  1675. int cb = c[0];
  1676. int cr = c[64];
  1677. int rc = m_crr[cr];
  1678. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1679. int bc = m_cbb[cb];
  1680. int yy = y[j];
  1681. d0[0] = clamp(yy+rc);
  1682. d0[1] = clamp(yy+gc);
  1683. d0[2] = clamp(yy+bc);
  1684. d0[3] = 255;
  1685. yy = y[j+1];
  1686. d0[4] = clamp(yy+rc);
  1687. d0[5] = clamp(yy+gc);
  1688. d0[6] = clamp(yy+bc);
  1689. d0[7] = 255;
  1690. yy = y[j+8];
  1691. d1[0] = clamp(yy+rc);
  1692. d1[1] = clamp(yy+gc);
  1693. d1[2] = clamp(yy+bc);
  1694. d1[3] = 255;
  1695. yy = y[j+8+1];
  1696. d1[4] = clamp(yy+rc);
  1697. d1[5] = clamp(yy+gc);
  1698. d1[6] = clamp(yy+bc);
  1699. d1[7] = 255;
  1700. d0 += 8;
  1701. d1 += 8;
  1702. c++;
  1703. }
  1704. y += 64;
  1705. }
  1706. y += 64*6 - 64*2;
  1707. c += 64*6 - 8;
  1708. }
  1709. }
  1710. // Y (1 block per MCU) to 8-bit grayscale
  1711. void jpeg_decoder::gray_convert()
  1712. {
  1713. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1714. uint8 *d = m_pScan_line_0;
  1715. uint8 *s = m_pSample_buf + row * 8;
  1716. for (int i = m_max_mcus_per_row; i > 0; i--)
  1717. {
  1718. *(uint *)d = *(uint *)s;
  1719. *(uint *)(&d[4]) = *(uint *)(&s[4]);
  1720. s += 64;
  1721. d += 8;
  1722. }
  1723. }
  1724. void jpeg_decoder::expanded_convert()
  1725. {
  1726. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1727. uint8* Py = m_pSample_buf + (row / 8) * 64 * m_comp_h_samp[0] + (row & 7) * 8;
  1728. uint8* d = m_pScan_line_0;
  1729. for (int i = m_max_mcus_per_row; i > 0; i--)
  1730. {
  1731. for (int k = 0; k < m_max_mcu_x_size; k += 8)
  1732. {
  1733. const int Y_ofs = k * 8;
  1734. const int Cb_ofs = Y_ofs + 64 * m_expanded_blocks_per_component;
  1735. const int Cr_ofs = Y_ofs + 64 * m_expanded_blocks_per_component * 2;
  1736. for (int j = 0; j < 8; j++)
  1737. {
  1738. int y = Py[Y_ofs + j];
  1739. int cb = Py[Cb_ofs + j];
  1740. int cr = Py[Cr_ofs + j];
  1741. d[0] = clamp(y + m_crr[cr]);
  1742. d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
  1743. d[2] = clamp(y + m_cbb[cb]);
  1744. d[3] = 255;
  1745. d += 4;
  1746. }
  1747. }
  1748. Py += 64 * m_expanded_blocks_per_mcu;
  1749. }
  1750. }
  1751. // Find end of image (EOI) marker, so we can return to the user the exact size of the input stream.
  1752. void jpeg_decoder::find_eoi()
  1753. {
  1754. if (!m_progressive_flag)
  1755. {
  1756. // Attempt to read the EOI marker.
  1757. //get_bits_no_markers(m_bits_left & 7);
  1758. // Prime the bit buffer
  1759. m_bits_left = 16;
  1760. get_bits(16);
  1761. get_bits(16);
  1762. // The next marker _should_ be EOI
  1763. process_markers();
  1764. }
  1765. m_total_bytes_read -= m_in_buf_left;
  1766. }
  1767. int jpeg_decoder::decode(const void** pScan_line, uint* pScan_line_len)
  1768. {
  1769. if ((m_error_code) || (!m_ready_flag))
  1770. return JPGD_FAILED;
  1771. if (m_total_lines_left == 0)
  1772. return JPGD_DONE;
  1773. if (m_mcu_lines_left == 0)
  1774. {
  1775. if (setjmp(m_jmp_state))
  1776. return JPGD_FAILED;
  1777. if (m_progressive_flag)
  1778. load_next_row();
  1779. else
  1780. decode_next_row();
  1781. // Find the EOI marker if that was the last row.
  1782. if (m_total_lines_left <= m_max_mcu_y_size)
  1783. find_eoi();
  1784. m_mcu_lines_left = m_max_mcu_y_size;
  1785. }
  1786. if (m_freq_domain_chroma_upsample)
  1787. {
  1788. expanded_convert();
  1789. *pScan_line = m_pScan_line_0;
  1790. }
  1791. else
  1792. {
  1793. switch (m_scan_type)
  1794. {
  1795. case JPGD_YH2V2:
  1796. {
  1797. if ((m_mcu_lines_left & 1) == 0)
  1798. {
  1799. H2V2Convert();
  1800. *pScan_line = m_pScan_line_0;
  1801. }
  1802. else
  1803. *pScan_line = m_pScan_line_1;
  1804. break;
  1805. }
  1806. case JPGD_YH2V1:
  1807. {
  1808. H2V1Convert();
  1809. *pScan_line = m_pScan_line_0;
  1810. break;
  1811. }
  1812. case JPGD_YH1V2:
  1813. {
  1814. if ((m_mcu_lines_left & 1) == 0)
  1815. {
  1816. H1V2Convert();
  1817. *pScan_line = m_pScan_line_0;
  1818. }
  1819. else
  1820. *pScan_line = m_pScan_line_1;
  1821. break;
  1822. }
  1823. case JPGD_YH1V1:
  1824. {
  1825. H1V1Convert();
  1826. *pScan_line = m_pScan_line_0;
  1827. break;
  1828. }
  1829. case JPGD_GRAYSCALE:
  1830. {
  1831. gray_convert();
  1832. *pScan_line = m_pScan_line_0;
  1833. break;
  1834. }
  1835. }
  1836. }
  1837. *pScan_line_len = m_real_dest_bytes_per_scan_line;
  1838. m_mcu_lines_left--;
  1839. m_total_lines_left--;
  1840. return JPGD_SUCCESS;
  1841. }
  1842. // Creates the tables needed for efficient Huffman decoding.
  1843. void jpeg_decoder::make_huff_table(int index, huff_tables *pH)
  1844. {
  1845. int p, i, l, si;
  1846. uint8 huffsize[257];
  1847. uint huffcode[257];
  1848. uint code;
  1849. uint subtree;
  1850. int code_size;
  1851. int lastp;
  1852. int nextfreeentry;
  1853. int currententry;
  1854. pH->ac_table = m_huff_ac[index] != 0;
  1855. p = 0;
  1856. for (l = 1; l <= 16; l++)
  1857. {
  1858. for (i = 1; i <= m_huff_num[index][l]; i++)
  1859. {
  1860. JPGD_ASSERT(p < 257);
  1861. huffsize[p++] = static_cast<uint8>(l);
  1862. }
  1863. }
  1864. huffsize[p] = 0;
  1865. lastp = p;
  1866. code = 0;
  1867. si = huffsize[0];
  1868. p = 0;
  1869. while (huffsize[p])
  1870. {
  1871. while (huffsize[p] == si)
  1872. {
  1873. JPGD_ASSERT(p < 257);
  1874. huffcode[p++] = code;
  1875. code++;
  1876. }
  1877. code <<= 1;
  1878. si++;
  1879. }
  1880. memset(pH->look_up, 0, sizeof(pH->look_up));
  1881. memset(pH->look_up2, 0, sizeof(pH->look_up2));
  1882. memset(pH->tree, 0, sizeof(pH->tree));
  1883. memset(pH->code_size, 0, sizeof(pH->code_size));
  1884. nextfreeentry = -1;
  1885. p = 0;
  1886. while (p < lastp)
  1887. {
  1888. i = m_huff_val[index][p];
  1889. code = huffcode[p];
  1890. code_size = huffsize[p];
  1891. pH->code_size[i] = static_cast<uint8>(code_size);
  1892. if (code_size <= 8)
  1893. {
  1894. code <<= (8 - code_size);
  1895. for (l = 1 << (8 - code_size); l > 0; l--)
  1896. {
  1897. JPGD_ASSERT(i < JPGD_HUFF_CODE_SIZE_MAX_LENGTH);
  1898. JPGD_ASSERT(code < JPGD_HUFF_CODE_SIZE_MAX_LENGTH);
  1899. pH->look_up[code] = i;
  1900. bool has_extrabits = false;
  1901. int extra_bits = 0;
  1902. int num_extra_bits = i & 15;
  1903. int bits_to_fetch = code_size;
  1904. if (num_extra_bits)
  1905. {
  1906. int total_codesize = code_size + num_extra_bits;
  1907. if (total_codesize <= 8)
  1908. {
  1909. has_extrabits = true;
  1910. extra_bits = ((1 << num_extra_bits) - 1) & (code >> (8 - total_codesize));
  1911. JPGD_ASSERT(extra_bits <= 0x7FFF);
  1912. bits_to_fetch += num_extra_bits;
  1913. }
  1914. }
  1915. if (!has_extrabits)
  1916. pH->look_up2[code] = i | (bits_to_fetch << 8);
  1917. else
  1918. pH->look_up2[code] = i | 0x8000 | (extra_bits << 16) | (bits_to_fetch << 8);
  1919. code++;
  1920. }
  1921. }
  1922. else
  1923. {
  1924. subtree = (code >> (code_size - 8)) & 0xFF;
  1925. currententry = pH->look_up[subtree];
  1926. if (currententry == 0)
  1927. {
  1928. pH->look_up[subtree] = currententry = nextfreeentry;
  1929. pH->look_up2[subtree] = currententry = nextfreeentry;
  1930. nextfreeentry -= 2;
  1931. }
  1932. code <<= (16 - (code_size - 8));
  1933. for (l = code_size; l > 9; l--)
  1934. {
  1935. if ((code & 0x8000) == 0)
  1936. currententry--;
  1937. unsigned int idx = -currententry - 1;
  1938. JPGD_ASSERT(idx < JPGD_HUFF_TREE_MAX_LENGTH);
  1939. if (pH->tree[idx] == 0)
  1940. {
  1941. pH->tree[idx] = nextfreeentry;
  1942. currententry = nextfreeentry;
  1943. nextfreeentry -= 2;
  1944. }
  1945. else {
  1946. currententry = pH->tree[idx];
  1947. }
  1948. code <<= 1;
  1949. }
  1950. if ((code & 0x8000) == 0)
  1951. currententry--;
  1952. pH->tree[-currententry - 1] = i;
  1953. }
  1954. p++;
  1955. }
  1956. }
  1957. // Verifies the quantization tables needed for this scan are available.
  1958. void jpeg_decoder::check_quant_tables()
  1959. {
  1960. for (int i = 0; i < m_comps_in_scan; i++)
  1961. if (m_quant[m_comp_quant[m_comp_list[i]]] == NULL)
  1962. stop_decoding(JPGD_UNDEFINED_QUANT_TABLE);
  1963. }
  1964. // Verifies that all the Huffman tables needed for this scan are available.
  1965. void jpeg_decoder::check_huff_tables()
  1966. {
  1967. for (int i = 0; i < m_comps_in_scan; i++)
  1968. {
  1969. if ((m_spectral_start == 0) && (m_huff_num[m_comp_dc_tab[m_comp_list[i]]] == NULL))
  1970. stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
  1971. if ((m_spectral_end > 0) && (m_huff_num[m_comp_ac_tab[m_comp_list[i]]] == NULL))
  1972. stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
  1973. }
  1974. for (int i = 0; i < JPGD_MAX_HUFF_TABLES; i++)
  1975. if (m_huff_num[i])
  1976. {
  1977. if (!m_pHuff_tabs[i])
  1978. m_pHuff_tabs[i] = (huff_tables *)alloc(sizeof(huff_tables));
  1979. make_huff_table(i, m_pHuff_tabs[i]);
  1980. }
  1981. }
  1982. // Determines the component order inside each MCU.
  1983. // Also calcs how many MCU's are on each row, etc.
  1984. void jpeg_decoder::calc_mcu_block_order()
  1985. {
  1986. int component_num, component_id;
  1987. int max_h_samp = 0, max_v_samp = 0;
  1988. for (component_id = 0; component_id < m_comps_in_frame; component_id++)
  1989. {
  1990. if (m_comp_h_samp[component_id] > max_h_samp)
  1991. max_h_samp = m_comp_h_samp[component_id];
  1992. if (m_comp_v_samp[component_id] > max_v_samp)
  1993. max_v_samp = m_comp_v_samp[component_id];
  1994. }
  1995. for (component_id = 0; component_id < m_comps_in_frame; component_id++)
  1996. {
  1997. m_comp_h_blocks[component_id] = ((((m_image_x_size * m_comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8;
  1998. m_comp_v_blocks[component_id] = ((((m_image_y_size * m_comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8;
  1999. }
  2000. if (m_comps_in_scan == 1)
  2001. {
  2002. m_mcus_per_row = m_comp_h_blocks[m_comp_list[0]];
  2003. m_mcus_per_col = m_comp_v_blocks[m_comp_list[0]];
  2004. }
  2005. else
  2006. {
  2007. m_mcus_per_row = (((m_image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp;
  2008. m_mcus_per_col = (((m_image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp;
  2009. }
  2010. if (m_comps_in_scan == 1)
  2011. {
  2012. m_mcu_org[0] = m_comp_list[0];
  2013. m_blocks_per_mcu = 1;
  2014. }
  2015. else
  2016. {
  2017. m_blocks_per_mcu = 0;
  2018. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  2019. {
  2020. int num_blocks;
  2021. component_id = m_comp_list[component_num];
  2022. num_blocks = m_comp_h_samp[component_id] * m_comp_v_samp[component_id];
  2023. while (num_blocks--)
  2024. m_mcu_org[m_blocks_per_mcu++] = component_id;
  2025. }
  2026. }
  2027. }
  2028. // Starts a new scan.
  2029. int jpeg_decoder::init_scan()
  2030. {
  2031. if (!locate_sos_marker())
  2032. return JPGD_FALSE;
  2033. calc_mcu_block_order();
  2034. check_huff_tables();
  2035. check_quant_tables();
  2036. memset(m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
  2037. m_eob_run = 0;
  2038. if (m_restart_interval)
  2039. {
  2040. m_restarts_left = m_restart_interval;
  2041. m_next_restart_num = 0;
  2042. }
  2043. fix_in_buffer();
  2044. return JPGD_TRUE;
  2045. }
  2046. // Starts a frame. Determines if the number of components or sampling factors
  2047. // are supported.
  2048. void jpeg_decoder::init_frame()
  2049. {
  2050. int i;
  2051. if (m_comps_in_frame == 1)
  2052. {
  2053. if ((m_comp_h_samp[0] != 1) || (m_comp_v_samp[0] != 1))
  2054. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2055. m_scan_type = JPGD_GRAYSCALE;
  2056. m_max_blocks_per_mcu = 1;
  2057. m_max_mcu_x_size = 8;
  2058. m_max_mcu_y_size = 8;
  2059. }
  2060. else if (m_comps_in_frame == 3)
  2061. {
  2062. if ( ((m_comp_h_samp[1] != 1) || (m_comp_v_samp[1] != 1)) ||
  2063. ((m_comp_h_samp[2] != 1) || (m_comp_v_samp[2] != 1)) )
  2064. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2065. if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1))
  2066. {
  2067. m_scan_type = JPGD_YH1V1;
  2068. m_max_blocks_per_mcu = 3;
  2069. m_max_mcu_x_size = 8;
  2070. m_max_mcu_y_size = 8;
  2071. }
  2072. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1))
  2073. {
  2074. m_scan_type = JPGD_YH2V1;
  2075. m_max_blocks_per_mcu = 4;
  2076. m_max_mcu_x_size = 16;
  2077. m_max_mcu_y_size = 8;
  2078. }
  2079. else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 2))
  2080. {
  2081. m_scan_type = JPGD_YH1V2;
  2082. m_max_blocks_per_mcu = 4;
  2083. m_max_mcu_x_size = 8;
  2084. m_max_mcu_y_size = 16;
  2085. }
  2086. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2))
  2087. {
  2088. m_scan_type = JPGD_YH2V2;
  2089. m_max_blocks_per_mcu = 6;
  2090. m_max_mcu_x_size = 16;
  2091. m_max_mcu_y_size = 16;
  2092. }
  2093. else
  2094. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2095. }
  2096. else
  2097. stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
  2098. m_max_mcus_per_row = (m_image_x_size + (m_max_mcu_x_size - 1)) / m_max_mcu_x_size;
  2099. m_max_mcus_per_col = (m_image_y_size + (m_max_mcu_y_size - 1)) / m_max_mcu_y_size;
  2100. // These values are for the *destination* pixels: after conversion.
  2101. if (m_scan_type == JPGD_GRAYSCALE)
  2102. m_dest_bytes_per_pixel = 1;
  2103. else
  2104. m_dest_bytes_per_pixel = 4;
  2105. m_dest_bytes_per_scan_line = ((m_image_x_size + 15) & 0xFFF0) * m_dest_bytes_per_pixel;
  2106. m_real_dest_bytes_per_scan_line = (m_image_x_size * m_dest_bytes_per_pixel);
  2107. // Initialize two scan line buffers.
  2108. m_pScan_line_0 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
  2109. if ((m_scan_type == JPGD_YH1V2) || (m_scan_type == JPGD_YH2V2))
  2110. m_pScan_line_1 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
  2111. m_max_blocks_per_row = m_max_mcus_per_row * m_max_blocks_per_mcu;
  2112. // Should never happen
  2113. if (m_max_blocks_per_row > JPGD_MAX_BLOCKS_PER_ROW)
  2114. stop_decoding(JPGD_ASSERTION_ERROR);
  2115. // Allocate the coefficient buffer, enough for one MCU
  2116. m_pMCU_coefficients = (jpgd_block_t*)alloc(m_max_blocks_per_mcu * 64 * sizeof(jpgd_block_t));
  2117. for (i = 0; i < m_max_blocks_per_mcu; i++)
  2118. m_mcu_block_max_zag[i] = 64;
  2119. m_expanded_blocks_per_component = m_comp_h_samp[0] * m_comp_v_samp[0];
  2120. m_expanded_blocks_per_mcu = m_expanded_blocks_per_component * m_comps_in_frame;
  2121. m_expanded_blocks_per_row = m_max_mcus_per_row * m_expanded_blocks_per_mcu;
  2122. // Freq. domain chroma upsampling is only supported for H2V2 subsampling factor (the most common one I've seen).
  2123. m_freq_domain_chroma_upsample = false;
  2124. #if JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING
  2125. m_freq_domain_chroma_upsample = (m_expanded_blocks_per_mcu == 4*3);
  2126. #endif
  2127. if (m_freq_domain_chroma_upsample)
  2128. m_pSample_buf = (uint8 *)alloc(m_expanded_blocks_per_row * 64);
  2129. else
  2130. m_pSample_buf = (uint8 *)alloc(m_max_blocks_per_row * 64);
  2131. m_total_lines_left = m_image_y_size;
  2132. m_mcu_lines_left = 0;
  2133. create_look_ups();
  2134. }
  2135. // The coeff_buf series of methods originally stored the coefficients
  2136. // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
  2137. // was used to make this process more efficient. Now, we can store the entire
  2138. // thing in RAM.
  2139. jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y)
  2140. {
  2141. coeff_buf* cb = (coeff_buf*)alloc(sizeof(coeff_buf));
  2142. cb->block_num_x = block_num_x;
  2143. cb->block_num_y = block_num_y;
  2144. cb->block_len_x = block_len_x;
  2145. cb->block_len_y = block_len_y;
  2146. cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t);
  2147. cb->pData = (uint8 *)alloc(cb->block_size * block_num_x * block_num_y, true);
  2148. return cb;
  2149. }
  2150. inline jpgd_block_t *jpeg_decoder::coeff_buf_getp(coeff_buf *cb, int block_x, int block_y)
  2151. {
  2152. JPGD_ASSERT((block_x < cb->block_num_x) && (block_y < cb->block_num_y));
  2153. return (jpgd_block_t *)(cb->pData + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x));
  2154. }
  2155. // The following methods decode the various types of m_blocks encountered
  2156. // in progressively encoded images.
  2157. void jpeg_decoder::decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2158. {
  2159. int s, r;
  2160. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
  2161. if ((s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_dc_tab[component_id]])) != 0)
  2162. {
  2163. r = pD->get_bits_no_markers(s);
  2164. s = JPGD_HUFF_EXTEND(r, s);
  2165. }
  2166. pD->m_last_dc_val[component_id] = (s += pD->m_last_dc_val[component_id]);
  2167. p[0] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
  2168. }
  2169. void jpeg_decoder::decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2170. {
  2171. if (pD->get_bits_no_markers(1))
  2172. {
  2173. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
  2174. p[0] |= (1 << pD->m_successive_low);
  2175. }
  2176. }
  2177. void jpeg_decoder::decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2178. {
  2179. int k, s, r;
  2180. if (pD->m_eob_run)
  2181. {
  2182. pD->m_eob_run--;
  2183. return;
  2184. }
  2185. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
  2186. for (k = pD->m_spectral_start; k <= pD->m_spectral_end; k++)
  2187. {
  2188. unsigned int idx = pD->m_comp_ac_tab[component_id];
  2189. JPGD_ASSERT(idx < JPGD_MAX_HUFF_TABLES);
  2190. s = pD->huff_decode(pD->m_pHuff_tabs[idx]);
  2191. r = s >> 4;
  2192. s &= 15;
  2193. if (s)
  2194. {
  2195. if ((k += r) > 63)
  2196. pD->stop_decoding(JPGD_DECODE_ERROR);
  2197. r = pD->get_bits_no_markers(s);
  2198. s = JPGD_HUFF_EXTEND(r, s);
  2199. p[g_ZAG[k]] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
  2200. }
  2201. else
  2202. {
  2203. if (r == 15)
  2204. {
  2205. if ((k += 15) > 63)
  2206. pD->stop_decoding(JPGD_DECODE_ERROR);
  2207. }
  2208. else
  2209. {
  2210. pD->m_eob_run = 1 << r;
  2211. if (r)
  2212. pD->m_eob_run += pD->get_bits_no_markers(r);
  2213. pD->m_eob_run--;
  2214. break;
  2215. }
  2216. }
  2217. }
  2218. }
  2219. void jpeg_decoder::decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2220. {
  2221. int s, k, r;
  2222. int p1 = 1 << pD->m_successive_low;
  2223. int m1 = (-1) << pD->m_successive_low;
  2224. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
  2225. JPGD_ASSERT(pD->m_spectral_end <= 63);
  2226. k = pD->m_spectral_start;
  2227. if (pD->m_eob_run == 0)
  2228. {
  2229. for ( ; k <= pD->m_spectral_end; k++)
  2230. {
  2231. unsigned int idx = pD->m_comp_ac_tab[component_id];
  2232. JPGD_ASSERT(idx < JPGD_MAX_HUFF_TABLES);
  2233. s = pD->huff_decode(pD->m_pHuff_tabs[idx]);
  2234. r = s >> 4;
  2235. s &= 15;
  2236. if (s)
  2237. {
  2238. if (s != 1)
  2239. pD->stop_decoding(JPGD_DECODE_ERROR);
  2240. if (pD->get_bits_no_markers(1))
  2241. s = p1;
  2242. else
  2243. s = m1;
  2244. }
  2245. else
  2246. {
  2247. if (r != 15)
  2248. {
  2249. pD->m_eob_run = 1 << r;
  2250. if (r)
  2251. pD->m_eob_run += pD->get_bits_no_markers(r);
  2252. break;
  2253. }
  2254. }
  2255. do
  2256. {
  2257. jpgd_block_t *this_coef = p + g_ZAG[k & 63];
  2258. if (*this_coef != 0)
  2259. {
  2260. if (pD->get_bits_no_markers(1))
  2261. {
  2262. if ((*this_coef & p1) == 0)
  2263. {
  2264. if (*this_coef >= 0)
  2265. *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
  2266. else
  2267. *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
  2268. }
  2269. }
  2270. }
  2271. else
  2272. {
  2273. if (--r < 0)
  2274. break;
  2275. }
  2276. k++;
  2277. } while (k <= pD->m_spectral_end);
  2278. if ((s) && (k < 64))
  2279. {
  2280. p[g_ZAG[k]] = static_cast<jpgd_block_t>(s);
  2281. }
  2282. }
  2283. }
  2284. if (pD->m_eob_run > 0)
  2285. {
  2286. for ( ; k <= pD->m_spectral_end; k++)
  2287. {
  2288. jpgd_block_t *this_coef = p + g_ZAG[k & 63]; // logical AND to shut up static code analysis
  2289. if (*this_coef != 0)
  2290. {
  2291. if (pD->get_bits_no_markers(1))
  2292. {
  2293. if ((*this_coef & p1) == 0)
  2294. {
  2295. if (*this_coef >= 0)
  2296. *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
  2297. else
  2298. *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
  2299. }
  2300. }
  2301. }
  2302. }
  2303. pD->m_eob_run--;
  2304. }
  2305. }
  2306. // Decode a scan in a progressively encoded image.
  2307. void jpeg_decoder::decode_scan(pDecode_block_func decode_block_func)
  2308. {
  2309. int mcu_row, mcu_col, mcu_block;
  2310. int block_x_mcu[JPGD_MAX_COMPONENTS], m_block_y_mcu[JPGD_MAX_COMPONENTS];
  2311. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  2312. for (mcu_col = 0; mcu_col < m_mcus_per_col; mcu_col++)
  2313. {
  2314. int component_num, component_id;
  2315. memset(block_x_mcu, 0, sizeof(block_x_mcu));
  2316. for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  2317. {
  2318. int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
  2319. if ((m_restart_interval) && (m_restarts_left == 0))
  2320. process_restart();
  2321. for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  2322. {
  2323. component_id = m_mcu_org[mcu_block];
  2324. decode_block_func(this, component_id, block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  2325. if (m_comps_in_scan == 1)
  2326. block_x_mcu[component_id]++;
  2327. else
  2328. {
  2329. if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
  2330. {
  2331. block_x_mcu_ofs = 0;
  2332. if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
  2333. {
  2334. block_y_mcu_ofs = 0;
  2335. block_x_mcu[component_id] += m_comp_h_samp[component_id];
  2336. }
  2337. }
  2338. }
  2339. }
  2340. m_restarts_left--;
  2341. }
  2342. if (m_comps_in_scan == 1)
  2343. m_block_y_mcu[m_comp_list[0]]++;
  2344. else
  2345. {
  2346. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  2347. {
  2348. component_id = m_comp_list[component_num];
  2349. m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
  2350. }
  2351. }
  2352. }
  2353. }
  2354. // Decode a progressively encoded image.
  2355. void jpeg_decoder::init_progressive()
  2356. {
  2357. int i;
  2358. if (m_comps_in_frame == 4)
  2359. stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
  2360. // Allocate the coefficient buffers.
  2361. for (i = 0; i < m_comps_in_frame; i++)
  2362. {
  2363. m_dc_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 1, 1);
  2364. m_ac_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 8, 8);
  2365. }
  2366. for ( ; ; )
  2367. {
  2368. int dc_only_scan, refinement_scan;
  2369. pDecode_block_func decode_block_func;
  2370. if (!init_scan())
  2371. break;
  2372. dc_only_scan = (m_spectral_start == 0);
  2373. refinement_scan = (m_successive_high != 0);
  2374. if ((m_spectral_start > m_spectral_end) || (m_spectral_end > 63))
  2375. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2376. if (dc_only_scan)
  2377. {
  2378. if (m_spectral_end)
  2379. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2380. }
  2381. else if (m_comps_in_scan != 1) /* AC scans can only contain one component */
  2382. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2383. if ((refinement_scan) && (m_successive_low != m_successive_high - 1))
  2384. stop_decoding(JPGD_BAD_SOS_SUCCESSIVE);
  2385. if (dc_only_scan)
  2386. {
  2387. if (refinement_scan)
  2388. decode_block_func = decode_block_dc_refine;
  2389. else
  2390. decode_block_func = decode_block_dc_first;
  2391. }
  2392. else
  2393. {
  2394. if (refinement_scan)
  2395. decode_block_func = decode_block_ac_refine;
  2396. else
  2397. decode_block_func = decode_block_ac_first;
  2398. }
  2399. decode_scan(decode_block_func);
  2400. m_bits_left = 16;
  2401. get_bits(16);
  2402. get_bits(16);
  2403. }
  2404. m_comps_in_scan = m_comps_in_frame;
  2405. for (i = 0; i < m_comps_in_frame; i++)
  2406. m_comp_list[i] = i;
  2407. calc_mcu_block_order();
  2408. }
  2409. void jpeg_decoder::init_sequential()
  2410. {
  2411. if (!init_scan())
  2412. stop_decoding(JPGD_UNEXPECTED_MARKER);
  2413. }
  2414. void jpeg_decoder::decode_start()
  2415. {
  2416. init_frame();
  2417. if (m_progressive_flag)
  2418. init_progressive();
  2419. else
  2420. init_sequential();
  2421. }
  2422. void jpeg_decoder::decode_init(jpeg_decoder_stream *pStream)
  2423. {
  2424. init(pStream);
  2425. locate_sof_marker();
  2426. }
  2427. jpeg_decoder::jpeg_decoder(jpeg_decoder_stream *pStream)
  2428. {
  2429. if (setjmp(m_jmp_state))
  2430. return;
  2431. decode_init(pStream);
  2432. }
  2433. int jpeg_decoder::begin_decoding()
  2434. {
  2435. if (m_ready_flag)
  2436. return JPGD_SUCCESS;
  2437. if (m_error_code)
  2438. return JPGD_FAILED;
  2439. if (setjmp(m_jmp_state))
  2440. return JPGD_FAILED;
  2441. decode_start();
  2442. m_ready_flag = true;
  2443. return JPGD_SUCCESS;
  2444. }
  2445. jpeg_decoder::~jpeg_decoder()
  2446. {
  2447. free_all_blocks();
  2448. }
  2449. jpeg_decoder_file_stream::jpeg_decoder_file_stream()
  2450. {
  2451. m_pFile = NULL;
  2452. m_eof_flag = false;
  2453. m_error_flag = false;
  2454. }
  2455. void jpeg_decoder_file_stream::close()
  2456. {
  2457. if (m_pFile)
  2458. {
  2459. fclose(m_pFile);
  2460. m_pFile = NULL;
  2461. }
  2462. m_eof_flag = false;
  2463. m_error_flag = false;
  2464. }
  2465. jpeg_decoder_file_stream::~jpeg_decoder_file_stream()
  2466. {
  2467. close();
  2468. }
  2469. bool jpeg_decoder_file_stream::open(const char *Pfilename)
  2470. {
  2471. close();
  2472. m_eof_flag = false;
  2473. m_error_flag = false;
  2474. #if defined(_MSC_VER)
  2475. m_pFile = NULL;
  2476. fopen_s(&m_pFile, Pfilename, "rb");
  2477. #else
  2478. m_pFile = fopen(Pfilename, "rb");
  2479. #endif
  2480. return m_pFile != NULL;
  2481. }
  2482. int jpeg_decoder_file_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
  2483. {
  2484. if (!m_pFile)
  2485. return -1;
  2486. if (m_eof_flag)
  2487. {
  2488. *pEOF_flag = true;
  2489. return 0;
  2490. }
  2491. if (m_error_flag)
  2492. return -1;
  2493. int bytes_read = static_cast<int>(fread(pBuf, 1, max_bytes_to_read, m_pFile));
  2494. if (bytes_read < max_bytes_to_read)
  2495. {
  2496. if (ferror(m_pFile))
  2497. {
  2498. m_error_flag = true;
  2499. return -1;
  2500. }
  2501. m_eof_flag = true;
  2502. *pEOF_flag = true;
  2503. }
  2504. return bytes_read;
  2505. }
  2506. bool jpeg_decoder_mem_stream::open(const uint8 *pSrc_data, uint size)
  2507. {
  2508. close();
  2509. m_pSrc_data = pSrc_data;
  2510. m_ofs = 0;
  2511. m_size = size;
  2512. return true;
  2513. }
  2514. int jpeg_decoder_mem_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
  2515. {
  2516. *pEOF_flag = false;
  2517. if (!m_pSrc_data)
  2518. return -1;
  2519. uint bytes_remaining = m_size - m_ofs;
  2520. if ((uint)max_bytes_to_read > bytes_remaining)
  2521. {
  2522. max_bytes_to_read = bytes_remaining;
  2523. *pEOF_flag = true;
  2524. }
  2525. memcpy(pBuf, m_pSrc_data + m_ofs, max_bytes_to_read);
  2526. m_ofs += max_bytes_to_read;
  2527. return max_bytes_to_read;
  2528. }
  2529. unsigned char *decompress_jpeg_image_from_stream(jpeg_decoder_stream *pStream, int *width, int *height, int *actual_comps, int req_comps)
  2530. {
  2531. if (!actual_comps)
  2532. return NULL;
  2533. *actual_comps = 0;
  2534. if ((!pStream) || (!width) || (!height) || (!req_comps))
  2535. return NULL;
  2536. if ((req_comps != 1) && (req_comps != 3) && (req_comps != 4))
  2537. return NULL;
  2538. jpeg_decoder decoder(pStream);
  2539. if (decoder.get_error_code() != JPGD_SUCCESS)
  2540. return NULL;
  2541. const int image_width = decoder.get_width(), image_height = decoder.get_height();
  2542. *width = image_width;
  2543. *height = image_height;
  2544. *actual_comps = decoder.get_num_components();
  2545. if (decoder.begin_decoding() != JPGD_SUCCESS)
  2546. return NULL;
  2547. const int dst_bpl = image_width * req_comps;
  2548. uint8 *pImage_data = (uint8*)jpgd_malloc(dst_bpl * image_height);
  2549. if (!pImage_data)
  2550. return NULL;
  2551. for (int y = 0; y < image_height; y++)
  2552. {
  2553. const uint8* pScan_line;
  2554. uint scan_line_len;
  2555. if (decoder.decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS)
  2556. {
  2557. jpgd_free(pImage_data);
  2558. return NULL;
  2559. }
  2560. uint8 *pDst = pImage_data + y * dst_bpl;
  2561. if (((req_comps == 1) && (decoder.get_num_components() == 1)) || ((req_comps == 4) && (decoder.get_num_components() == 3)))
  2562. memcpy(pDst, pScan_line, dst_bpl);
  2563. else if (decoder.get_num_components() == 1)
  2564. {
  2565. if (req_comps == 3)
  2566. {
  2567. for (int x = 0; x < image_width; x++)
  2568. {
  2569. uint8 luma = pScan_line[x];
  2570. pDst[0] = luma;
  2571. pDst[1] = luma;
  2572. pDst[2] = luma;
  2573. pDst += 3;
  2574. }
  2575. }
  2576. else
  2577. {
  2578. for (int x = 0; x < image_width; x++)
  2579. {
  2580. uint8 luma = pScan_line[x];
  2581. pDst[0] = luma;
  2582. pDst[1] = luma;
  2583. pDst[2] = luma;
  2584. pDst[3] = 255;
  2585. pDst += 4;
  2586. }
  2587. }
  2588. }
  2589. else if (decoder.get_num_components() == 3)
  2590. {
  2591. if (req_comps == 1)
  2592. {
  2593. const int YR = 19595, YG = 38470, YB = 7471;
  2594. for (int x = 0; x < image_width; x++)
  2595. {
  2596. int r = pScan_line[x*4+0];
  2597. int g = pScan_line[x*4+1];
  2598. int b = pScan_line[x*4+2];
  2599. *pDst++ = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);
  2600. }
  2601. }
  2602. else
  2603. {
  2604. for (int x = 0; x < image_width; x++)
  2605. {
  2606. pDst[0] = pScan_line[x*4+0];
  2607. pDst[1] = pScan_line[x*4+1];
  2608. pDst[2] = pScan_line[x*4+2];
  2609. pDst += 3;
  2610. }
  2611. }
  2612. }
  2613. }
  2614. return pImage_data;
  2615. }
  2616. unsigned char *decompress_jpeg_image_from_memory(const unsigned char *pSrc_data, int src_data_size, int *width, int *height, int *actual_comps, int req_comps)
  2617. {
  2618. jpgd::jpeg_decoder_mem_stream mem_stream(pSrc_data, src_data_size);
  2619. return decompress_jpeg_image_from_stream(&mem_stream, width, height, actual_comps, req_comps);
  2620. }
  2621. unsigned char *decompress_jpeg_image_from_file(const char *pSrc_filename, int *width, int *height, int *actual_comps, int req_comps)
  2622. {
  2623. jpgd::jpeg_decoder_file_stream file_stream;
  2624. if (!file_stream.open(pSrc_filename))
  2625. return NULL;
  2626. return decompress_jpeg_image_from_stream(&file_stream, width, height, actual_comps, req_comps);
  2627. }
  2628. } // namespace jpgd