tjpgd.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*----------------------------------------------------------------------------/
  2. / TJpgDec - Tiny JPEG Decompressor R0.03 (C)ChaN, 2021
  3. /-----------------------------------------------------------------------------/
  4. / The TJpgDec is a generic JPEG decompressor module for tiny embedded systems.
  5. / This is a free software that opened for education, research and commercial
  6. / developments under license policy of following terms.
  7. /
  8. / Copyright (C) 2021, ChaN, all right reserved.
  9. /
  10. / * The TJpgDec module is a free software and there is NO WARRANTY.
  11. / * No restriction on use. You can use, modify and redistribute it for
  12. / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /-----------------------------------------------------------------------------/
  16. / Oct 04, 2011 R0.01 First release.
  17. / Feb 19, 2012 R0.01a Fixed decompression fails when scan starts with an escape seq.
  18. / Sep 03, 2012 R0.01b Added JD_TBLCLIP option.
  19. / Mar 16, 2019 R0.01c Supprted stdint.h.
  20. / Jul 01, 2020 R0.01d Fixed wrong integer type usage.
  21. / May 08, 2021 R0.02 Supprted grayscale image. Separated configuration options.
  22. / Jun 11, 2021 R0.02a Some performance improvement.
  23. / Jul 01, 2021 R0.03 Added JD_FASTDECODE option.
  24. / Some performance improvement.
  25. /----------------------------------------------------------------------------*/
  26. #include "tjpgd.h"
  27. #define LUAT_LOG_TAG "tjpgd"
  28. #include "luat_log.h"
  29. #if JD_FASTDECODE == 2
  30. #define HUFF_BIT 10 /* Bit length to apply fast huffman decode */
  31. #define HUFF_LEN (1 << HUFF_BIT)
  32. #define HUFF_MASK (HUFF_LEN - 1)
  33. #endif
  34. /*-----------------------------------------------*/
  35. /* Zigzag-order to raster-order conversion table */
  36. /*-----------------------------------------------*/
  37. static const uint8_t Zig[64] = { /* Zigzag-order to raster-order conversion table */
  38. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  39. 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  40. 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  41. 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  42. };
  43. /*-------------------------------------------------*/
  44. /* Input scale factor of Arai algorithm */
  45. /* (scaled up 16 bits for fixed point operations) */
  46. /*-------------------------------------------------*/
  47. static const uint16_t Ipsf[64] = { /* See also aa_idct.png */
  48. (uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
  49. (uint16_t)(1.38704*8192), (uint16_t)(1.92388*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.08979*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.38268*8192),
  50. (uint16_t)(1.30656*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.70711*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.36048*8192),
  51. (uint16_t)(1.17588*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.38268*8192), (uint16_t)(1.17588*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.32442*8192),
  52. (uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
  53. (uint16_t)(0.78570*8192), (uint16_t)(1.08979*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.61732*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.21677*8192),
  54. (uint16_t)(0.54120*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.29290*8192), (uint16_t)(0.14932*8192),
  55. (uint16_t)(0.27590*8192), (uint16_t)(0.38268*8192), (uint16_t)(0.36048*8192), (uint16_t)(0.32442*8192), (uint16_t)(0.27590*8192), (uint16_t)(0.21678*8192), (uint16_t)(0.14932*8192), (uint16_t)(0.07612*8192)
  56. };
  57. /*---------------------------------------------*/
  58. /* Conversion table for fast clipping process */
  59. /*---------------------------------------------*/
  60. #if JD_TBLCLIP
  61. #define BYTECLIP(v) Clip8[(unsigned int)(v) & 0x3FF]
  62. static const uint8_t Clip8[1024] = {
  63. /* 0..255 */
  64. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  65. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  66. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  67. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  68. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  69. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  70. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  71. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
  72. /* 256..511 */
  73. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  74. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  75. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  76. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  77. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  78. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  79. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  80. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  81. /* -512..-257 */
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. /* -256..-1 */
  91. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  94. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  99. };
  100. #else /* JD_TBLCLIP */
  101. static uint8_t BYTECLIP (int val)
  102. {
  103. if (val < 0) return 0;
  104. if (val > 255) return 255;
  105. return (uint8_t)val;
  106. }
  107. #endif
  108. /*-----------------------------------------------------------------------*/
  109. /* Allocate a memory block from memory pool */
  110. /*-----------------------------------------------------------------------*/
  111. static void* alloc_pool ( /* Pointer to allocated memory block (NULL:no memory available) */
  112. JDEC* jd, /* Pointer to the decompressor object */
  113. size_t ndata /* Number of bytes to allocate */
  114. )
  115. {
  116. char *rp = 0;
  117. ndata = (ndata + 3) & ~3; /* Align block size to the word boundary */
  118. if (jd->sz_pool >= ndata) {
  119. jd->sz_pool -= ndata;
  120. rp = (char*)jd->pool; /* Get start of available memory pool */
  121. jd->pool = (void*)(rp + ndata); /* Allocate requierd bytes */
  122. }
  123. return (void*)rp; /* Return allocated memory block (NULL:no memory to allocate) */
  124. }
  125. /*-----------------------------------------------------------------------*/
  126. /* Create de-quantization and prescaling tables with a DQT segment */
  127. /*-----------------------------------------------------------------------*/
  128. static JRESULT create_qt_tbl ( /* 0:OK, !0:Failed */
  129. JDEC* jd, /* Pointer to the decompressor object */
  130. const uint8_t* data, /* Pointer to the quantizer tables */
  131. size_t ndata /* Size of input data */
  132. )
  133. {
  134. unsigned int i, zi;
  135. uint8_t d;
  136. int32_t *pb;
  137. while (ndata) { /* Process all tables in the segment */
  138. if (ndata < 65) return JDR_FMT1; /* Err: table size is unaligned */
  139. ndata -= 65;
  140. d = *data++; /* Get table property */
  141. if (d & 0xF0) return JDR_FMT1; /* Err: not 8-bit resolution */
  142. i = d & 3; /* Get table ID */
  143. pb = alloc_pool(jd, 64 * sizeof (int32_t));/* Allocate a memory block for the table */
  144. if (!pb) return JDR_MEM1; /* Err: not enough memory */
  145. jd->qttbl[i] = pb; /* Register the table */
  146. for (i = 0; i < 64; i++) { /* Load the table */
  147. zi = Zig[i]; /* Zigzag-order to raster-order conversion */
  148. pb[zi] = (int32_t)((uint32_t)*data++ * Ipsf[zi]); /* Apply scale factor of Arai algorithm to the de-quantizers */
  149. }
  150. }
  151. return JDR_OK;
  152. }
  153. /*-----------------------------------------------------------------------*/
  154. /* Create huffman code tables with a DHT segment */
  155. /*-----------------------------------------------------------------------*/
  156. static JRESULT create_huffman_tbl ( /* 0:OK, !0:Failed */
  157. JDEC* jd, /* Pointer to the decompressor object */
  158. const uint8_t* data, /* Pointer to the packed huffman tables */
  159. size_t ndata /* Size of input data */
  160. )
  161. {
  162. unsigned int i, j, b, cls, num;
  163. size_t np;
  164. uint8_t d, *pb, *pd;
  165. uint16_t hc, *ph;
  166. while (ndata) { /* Process all tables in the segment */
  167. if (ndata < 17) return JDR_FMT1; /* Err: wrong data size */
  168. ndata -= 17;
  169. d = *data++; /* Get table number and class */
  170. if (d & 0xEE) return JDR_FMT1; /* Err: invalid class/number */
  171. cls = d >> 4; num = d & 0x0F; /* class = dc(0)/ac(1), table number = 0/1 */
  172. pb = alloc_pool(jd, 16); /* Allocate a memory block for the bit distribution table */
  173. if (!pb) return JDR_MEM1; /* Err: not enough memory */
  174. jd->huffbits[num][cls] = pb;
  175. for (np = i = 0; i < 16; i++) { /* Load number of patterns for 1 to 16-bit code */
  176. np += (pb[i] = *data++); /* Get sum of code words for each code */
  177. }
  178. ph = alloc_pool(jd, np * sizeof (uint16_t));/* Allocate a memory block for the code word table */
  179. if (!ph) return JDR_MEM1; /* Err: not enough memory */
  180. jd->huffcode[num][cls] = ph;
  181. hc = 0;
  182. for (j = i = 0; i < 16; i++) { /* Re-build huffman code word table */
  183. b = pb[i];
  184. while (b--) ph[j++] = hc++;
  185. hc <<= 1;
  186. }
  187. if (ndata < np) return JDR_FMT1; /* Err: wrong data size */
  188. ndata -= np;
  189. pd = alloc_pool(jd, np); /* Allocate a memory block for the decoded data */
  190. if (!pd) return JDR_MEM1; /* Err: not enough memory */
  191. jd->huffdata[num][cls] = pd;
  192. for (i = 0; i < np; i++) { /* Load decoded data corresponds to each code word */
  193. d = *data++;
  194. if (!cls && d > 11) return JDR_FMT1;
  195. pd[i] = d;
  196. }
  197. #if JD_FASTDECODE == 2
  198. { /* Create fast huffman decode table */
  199. unsigned int span, td, ti;
  200. uint16_t *tbl_ac = 0;
  201. uint8_t *tbl_dc = 0;
  202. if (cls) {
  203. tbl_ac = alloc_pool(jd, HUFF_LEN * sizeof (uint16_t)); /* LUT for AC elements */
  204. if (!tbl_ac) return JDR_MEM1; /* Err: not enough memory */
  205. jd->hufflut_ac[num] = tbl_ac;
  206. memset(tbl_ac, 0xFF, HUFF_LEN * sizeof (uint16_t)); /* Default value (0xFFFF: may be long code) */
  207. } else {
  208. tbl_dc = alloc_pool(jd, HUFF_LEN * sizeof (uint8_t)); /* LUT for AC elements */
  209. if (!tbl_dc) return JDR_MEM1; /* Err: not enough memory */
  210. jd->hufflut_dc[num] = tbl_dc;
  211. memset(tbl_dc, 0xFF, HUFF_LEN * sizeof (uint8_t)); /* Default value (0xFF: may be long code) */
  212. }
  213. for (i = b = 0; b < HUFF_BIT; b++) { /* Create LUT */
  214. for (j = pb[b]; j; j--) {
  215. ti = ph[i] << (HUFF_BIT - 1 - b) & HUFF_MASK; /* Index of input pattern for the code */
  216. if (cls) {
  217. td = pd[i++] | ((b + 1) << 8); /* b15..b8: code length, b7..b0: zero run and data length */
  218. for (span = 1 << (HUFF_BIT - 1 - b); span; span--, tbl_ac[ti++] = (uint16_t)td) ;
  219. } else {
  220. td = pd[i++] | ((b + 1) << 4); /* b7..b4: code length, b3..b0: data length */
  221. for (span = 1 << (HUFF_BIT - 1 - b); span; span--, tbl_dc[ti++] = (uint8_t)td) ;
  222. }
  223. }
  224. }
  225. jd->longofs[num][cls] = i; /* Code table offset for long code */
  226. }
  227. #endif
  228. }
  229. return JDR_OK;
  230. }
  231. /*-----------------------------------------------------------------------*/
  232. /* Extract a huffman decoded data from input stream */
  233. /*-----------------------------------------------------------------------*/
  234. static int huffext ( /* >=0: decoded data, <0: error code */
  235. JDEC* jd, /* Pointer to the decompressor object */
  236. unsigned int id, /* Table ID (0:Y, 1:C) */
  237. unsigned int cls /* Table class (0:DC, 1:AC) */
  238. )
  239. {
  240. size_t dc = jd->dctr;
  241. uint8_t *dp = jd->dptr;
  242. unsigned int d, flg = 0;
  243. #if JD_FASTDECODE == 0
  244. uint8_t bm, nd, bl;
  245. const uint8_t *hb = jd->huffbits[id][cls]; /* Bit distribution table */
  246. const uint16_t *hc = jd->huffcode[id][cls]; /* Code word table */
  247. const uint8_t *hd = jd->huffdata[id][cls]; /* Data table */
  248. bm = jd->dbit; /* Bit mask to extract */
  249. d = 0; bl = 16; /* Max code length */
  250. do {
  251. if (!bm) { /* Next byte? */
  252. if (!dc) { /* No input data is available, re-fill input buffer */
  253. dp = jd->inbuf; /* Top of input buffer */
  254. dc = jd->infunc(jd, dp, JD_SZBUF);
  255. if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
  256. } else {
  257. dp++; /* Next data ptr */
  258. }
  259. dc--; /* Decrement number of available bytes */
  260. if (flg) { /* In flag sequence? */
  261. flg = 0; /* Exit flag sequence */
  262. if (*dp != 0) return 0 - (int)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
  263. *dp = 0xFF; /* The flag is a data 0xFF */
  264. } else {
  265. if (*dp == 0xFF) { /* Is start of flag sequence? */
  266. flg = 1; continue; /* Enter flag sequence, get trailing byte */
  267. }
  268. }
  269. bm = 0x80; /* Read from MSB */
  270. }
  271. d <<= 1; /* Get a bit */
  272. if (*dp & bm) d++;
  273. bm >>= 1;
  274. for (nd = *hb++; nd; nd--) { /* Search the code word in this bit length */
  275. if (d == *hc++) { /* Matched? */
  276. jd->dbit = bm; jd->dctr = dc; jd->dptr = dp;
  277. return *hd; /* Return the decoded data */
  278. }
  279. hd++;
  280. }
  281. bl--;
  282. } while (bl);
  283. #else
  284. const uint8_t *hb, *hd;
  285. const uint16_t *hc;
  286. unsigned int nc, bl, wbit = jd->dbit % 32;
  287. uint32_t w = jd->wreg & ((1UL << wbit) - 1);
  288. while (wbit < 16) { /* Prepare 16 bits into the working register */
  289. if (jd->marker) {
  290. d = 0xFF; /* Input stream has stalled for a marker. Generate stuff bits */
  291. } else {
  292. if (!dc) { /* Buffer empty, re-fill input buffer */
  293. dp = jd->inbuf; /* Top of input buffer */
  294. dc = jd->infunc(jd, dp, JD_SZBUF);
  295. if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
  296. }
  297. d = *dp++; dc--;
  298. if (flg) { /* In flag sequence? */
  299. flg = 0; /* Exit flag sequence */
  300. if (d != 0) jd->marker = d; /* Not an escape of 0xFF but a marker */
  301. d = 0xFF;
  302. } else {
  303. if (d == 0xFF) { /* Is start of flag sequence? */
  304. flg = 1; continue; /* Enter flag sequence, get trailing byte */
  305. }
  306. }
  307. }
  308. w = w << 8 | d; /* Shift 8 bits in the working register */
  309. wbit += 8;
  310. }
  311. jd->dctr = dc; jd->dptr = dp;
  312. jd->wreg = w;
  313. #if JD_FASTDECODE == 2
  314. /* Table serch for the short codes */
  315. d = (unsigned int)(w >> (wbit - HUFF_BIT)); /* Short code as table index */
  316. if (cls) { /* AC element */
  317. d = jd->hufflut_ac[id][d]; /* Table decode */
  318. if (d != 0xFFFF) { /* It is done if hit in short code */
  319. jd->dbit = wbit - (d >> 8); /* Snip the code length */
  320. return d & 0xFF; /* b7..0: zero run and following data bits */
  321. }
  322. } else { /* DC element */
  323. d = jd->hufflut_dc[id][d]; /* Table decode */
  324. if (d != 0xFF) { /* It is done if hit in short code */
  325. jd->dbit = wbit - (d >> 4); /* Snip the code length */
  326. return d & 0xF; /* b3..0: following data bits */
  327. }
  328. }
  329. /* Incremental serch for the codes longer than HUFF_BIT */
  330. hb = jd->huffbits[id][cls] + HUFF_BIT; /* Bit distribution table */
  331. hc = jd->huffcode[id][cls] + jd->longofs[id][cls]; /* Code word table */
  332. hd = jd->huffdata[id][cls] + jd->longofs[id][cls]; /* Data table */
  333. bl = HUFF_BIT + 1;
  334. #else
  335. /* Incremental serch for all codes */
  336. hb = jd->huffbits[id][cls]; /* Bit distribution table */
  337. hc = jd->huffcode[id][cls]; /* Code word table */
  338. hd = jd->huffdata[id][cls]; /* Data table */
  339. bl = 1;
  340. #endif
  341. for ( ; bl <= 16; bl++) { /* Incremental search */
  342. nc = *hb++;
  343. if (nc) {
  344. d = w >> (wbit - bl);
  345. do { /* Search the code word in this bit length */
  346. if (d == *hc++) { /* Matched? */
  347. jd->dbit = wbit - bl; /* Snip the huffman code */
  348. return *hd; /* Return the decoded data */
  349. }
  350. hd++;
  351. } while (--nc);
  352. }
  353. }
  354. #endif
  355. return 0 - (int)JDR_FMT1; /* Err: code not found (may be collapted data) */
  356. }
  357. /*-----------------------------------------------------------------------*/
  358. /* Extract N bits from input stream */
  359. /*-----------------------------------------------------------------------*/
  360. static int bitext ( /* >=0: extracted data, <0: error code */
  361. JDEC* jd, /* Pointer to the decompressor object */
  362. unsigned int nbit /* Number of bits to extract (1 to 16) */
  363. )
  364. {
  365. size_t dc = jd->dctr;
  366. uint8_t *dp = jd->dptr;
  367. unsigned int d, flg = 0;
  368. #if JD_FASTDECODE == 0
  369. uint8_t mbit = jd->dbit;
  370. d = 0;
  371. do {
  372. if (!mbit) { /* Next byte? */
  373. if (!dc) { /* No input data is available, re-fill input buffer */
  374. dp = jd->inbuf; /* Top of input buffer */
  375. dc = jd->infunc(jd, dp, JD_SZBUF);
  376. if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
  377. } else {
  378. dp++; /* Next data ptr */
  379. }
  380. dc--; /* Decrement number of available bytes */
  381. if (flg) { /* In flag sequence? */
  382. flg = 0; /* Exit flag sequence */
  383. if (*dp != 0) return 0 - (int)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
  384. *dp = 0xFF; /* The flag is a data 0xFF */
  385. } else {
  386. if (*dp == 0xFF) { /* Is start of flag sequence? */
  387. flg = 1; continue; /* Enter flag sequence */
  388. }
  389. }
  390. mbit = 0x80; /* Read from MSB */
  391. }
  392. d <<= 1; /* Get a bit */
  393. if (*dp & mbit) d |= 1;
  394. mbit >>= 1;
  395. nbit--;
  396. } while (nbit);
  397. jd->dbit = mbit; jd->dctr = dc; jd->dptr = dp;
  398. return (int)d;
  399. #else
  400. unsigned int wbit = jd->dbit % 32;
  401. uint32_t w = jd->wreg & ((1UL << wbit) - 1);
  402. while (wbit < nbit) { /* Prepare nbit bits into the working register */
  403. if (jd->marker) {
  404. d = 0xFF; /* Input stream stalled, generate stuff bits */
  405. } else {
  406. if (!dc) { /* Buffer empty, re-fill input buffer */
  407. dp = jd->inbuf; /* Top of input buffer */
  408. dc = jd->infunc(jd, dp, JD_SZBUF);
  409. if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
  410. }
  411. d = *dp++; dc--;
  412. if (flg) { /* In flag sequence? */
  413. flg = 0; /* Exit flag sequence */
  414. if (d != 0) jd->marker = d; /* Not an escape of 0xFF but a marker */
  415. d = 0xFF;
  416. } else {
  417. if (d == 0xFF) { /* Is start of flag sequence? */
  418. flg = 1; continue; /* Enter flag sequence, get trailing byte */
  419. }
  420. }
  421. }
  422. w = w << 8 | d; /* Get 8 bits into the working register */
  423. wbit += 8;
  424. }
  425. jd->wreg = w; jd->dbit = wbit - nbit;
  426. jd->dctr = dc; jd->dptr = dp;
  427. return (int)(w >> ((wbit - nbit) % 32));
  428. #endif
  429. }
  430. /*-----------------------------------------------------------------------*/
  431. /* Process restart interval */
  432. /*-----------------------------------------------------------------------*/
  433. static JRESULT restart (
  434. JDEC* jd, /* Pointer to the decompressor object */
  435. uint16_t rstn /* Expected restert sequense number */
  436. )
  437. {
  438. unsigned int i;
  439. uint8_t *dp = jd->dptr;
  440. size_t dc = jd->dctr;
  441. #if JD_FASTDECODE == 0
  442. uint16_t d = 0;
  443. /* Get two bytes from the input stream */
  444. for (i = 0; i < 2; i++) {
  445. if (!dc) { /* No input data is available, re-fill input buffer */
  446. dp = jd->inbuf;
  447. dc = jd->infunc(jd, dp, JD_SZBUF);
  448. if (!dc) return JDR_INP;
  449. } else {
  450. dp++;
  451. }
  452. dc--;
  453. d = d << 8 | *dp; /* Get a byte */
  454. }
  455. jd->dptr = dp; jd->dctr = dc; jd->dbit = 0;
  456. /* Check the marker */
  457. if ((d & 0xFFD8) != 0xFFD0 || (d & 7) != (rstn & 7)) {
  458. return JDR_FMT1; /* Err: expected RSTn marker is not detected (may be collapted data) */
  459. }
  460. #else
  461. uint16_t marker;
  462. if (jd->marker) { /* Generate a maker if it has been detected */
  463. marker = 0xFF00 | jd->marker;
  464. jd->marker = 0;
  465. } else {
  466. marker = 0;
  467. for (i = 0; i < 2; i++) { /* Get a restart marker */
  468. if (!dc) { /* No input data is available, re-fill input buffer */
  469. dp = jd->inbuf;
  470. dc = jd->infunc(jd, dp, JD_SZBUF);
  471. if (!dc) return JDR_INP;
  472. }
  473. marker = (marker << 8) | *dp++; /* Get a byte */
  474. dc--;
  475. }
  476. jd->dptr = dp; jd->dctr = dc;
  477. }
  478. /* Check the marker */
  479. if ((marker & 0xFFD8) != 0xFFD0 || (marker & 7) != (rstn & 7)) {
  480. return JDR_FMT1; /* Err: expected RSTn marker was not detected (may be collapted data) */
  481. }
  482. jd->dbit = 0; /* Discard stuff bits */
  483. #endif
  484. jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; /* Reset DC offset */
  485. return JDR_OK;
  486. }
  487. /*-----------------------------------------------------------------------*/
  488. /* Apply Inverse-DCT in Arai Algorithm (see also aa_idct.png) */
  489. /*-----------------------------------------------------------------------*/
  490. static void block_idct (
  491. int32_t* src, /* Input block data (de-quantized and pre-scaled for Arai Algorithm) */
  492. jd_yuv_t* dst /* Pointer to the destination to store the block as byte array */
  493. )
  494. {
  495. const int32_t M13 = (int32_t)(1.41421*4096), M2 = (int32_t)(1.08239*4096), M4 = (int32_t)(2.61313*4096), M5 = (int32_t)(1.84776*4096);
  496. int32_t v0, v1, v2, v3, v4, v5, v6, v7;
  497. int32_t t10, t11, t12, t13;
  498. int i;
  499. /* Process columns */
  500. for (i = 0; i < 8; i++) {
  501. v0 = src[8 * 0]; /* Get even elements */
  502. v1 = src[8 * 2];
  503. v2 = src[8 * 4];
  504. v3 = src[8 * 6];
  505. t10 = v0 + v2; /* Process the even elements */
  506. t12 = v0 - v2;
  507. t11 = (v1 - v3) * M13 >> 12;
  508. v3 += v1;
  509. t11 -= v3;
  510. v0 = t10 + v3;
  511. v3 = t10 - v3;
  512. v1 = t11 + t12;
  513. v2 = t12 - t11;
  514. v4 = src[8 * 7]; /* Get odd elements */
  515. v5 = src[8 * 1];
  516. v6 = src[8 * 5];
  517. v7 = src[8 * 3];
  518. t10 = v5 - v4; /* Process the odd elements */
  519. t11 = v5 + v4;
  520. t12 = v6 - v7;
  521. v7 += v6;
  522. v5 = (t11 - v7) * M13 >> 12;
  523. v7 += t11;
  524. t13 = (t10 + t12) * M5 >> 12;
  525. v4 = t13 - (t10 * M2 >> 12);
  526. v6 = t13 - (t12 * M4 >> 12) - v7;
  527. v5 -= v6;
  528. v4 -= v5;
  529. src[8 * 0] = v0 + v7; /* Write-back transformed values */
  530. src[8 * 7] = v0 - v7;
  531. src[8 * 1] = v1 + v6;
  532. src[8 * 6] = v1 - v6;
  533. src[8 * 2] = v2 + v5;
  534. src[8 * 5] = v2 - v5;
  535. src[8 * 3] = v3 + v4;
  536. src[8 * 4] = v3 - v4;
  537. src++; /* Next column */
  538. }
  539. /* Process rows */
  540. src -= 8;
  541. for (i = 0; i < 8; i++) {
  542. v0 = src[0] + (128L << 8); /* Get even elements (remove DC offset (-128) here) */
  543. v1 = src[2];
  544. v2 = src[4];
  545. v3 = src[6];
  546. t10 = v0 + v2; /* Process the even elements */
  547. t12 = v0 - v2;
  548. t11 = (v1 - v3) * M13 >> 12;
  549. v3 += v1;
  550. t11 -= v3;
  551. v0 = t10 + v3;
  552. v3 = t10 - v3;
  553. v1 = t11 + t12;
  554. v2 = t12 - t11;
  555. v4 = src[7]; /* Get odd elements */
  556. v5 = src[1];
  557. v6 = src[5];
  558. v7 = src[3];
  559. t10 = v5 - v4; /* Process the odd elements */
  560. t11 = v5 + v4;
  561. t12 = v6 - v7;
  562. v7 += v6;
  563. v5 = (t11 - v7) * M13 >> 12;
  564. v7 += t11;
  565. t13 = (t10 + t12) * M5 >> 12;
  566. v4 = t13 - (t10 * M2 >> 12);
  567. v6 = t13 - (t12 * M4 >> 12) - v7;
  568. v5 -= v6;
  569. v4 -= v5;
  570. /* Descale the transformed values 8 bits and output a row */
  571. #if JD_FASTDECODE >= 1
  572. dst[0] = (int16_t)((v0 + v7) >> 8);
  573. dst[7] = (int16_t)((v0 - v7) >> 8);
  574. dst[1] = (int16_t)((v1 + v6) >> 8);
  575. dst[6] = (int16_t)((v1 - v6) >> 8);
  576. dst[2] = (int16_t)((v2 + v5) >> 8);
  577. dst[5] = (int16_t)((v2 - v5) >> 8);
  578. dst[3] = (int16_t)((v3 + v4) >> 8);
  579. dst[4] = (int16_t)((v3 - v4) >> 8);
  580. #else
  581. dst[0] = BYTECLIP((v0 + v7) >> 8);
  582. dst[7] = BYTECLIP((v0 - v7) >> 8);
  583. dst[1] = BYTECLIP((v1 + v6) >> 8);
  584. dst[6] = BYTECLIP((v1 - v6) >> 8);
  585. dst[2] = BYTECLIP((v2 + v5) >> 8);
  586. dst[5] = BYTECLIP((v2 - v5) >> 8);
  587. dst[3] = BYTECLIP((v3 + v4) >> 8);
  588. dst[4] = BYTECLIP((v3 - v4) >> 8);
  589. #endif
  590. dst += 8; src += 8; /* Next row */
  591. }
  592. }
  593. /*-----------------------------------------------------------------------*/
  594. /* Load all blocks in an MCU into working buffer */
  595. /*-----------------------------------------------------------------------*/
  596. static JRESULT mcu_load (
  597. JDEC* jd /* Pointer to the decompressor object */
  598. )
  599. {
  600. int32_t *tmp = (int32_t*)jd->workbuf; /* Block working buffer for de-quantize and IDCT */
  601. int d, e;
  602. unsigned int blk, nby, i, bc, z, id, cmp;
  603. jd_yuv_t *bp;
  604. const int32_t *dqf;
  605. nby = jd->msx * jd->msy; /* Number of Y blocks (1, 2 or 4) */
  606. bp = jd->mcubuf; /* Pointer to the first block of MCU */
  607. for (blk = 0; blk < nby + 2; blk++) { /* Get nby Y blocks and two C blocks */
  608. cmp = (blk < nby) ? 0 : blk - nby + 1; /* Component number 0:Y, 1:Cb, 2:Cr */
  609. if (cmp && jd->ncomp != 3) { /* Clear C blocks if not exist (monochrome image) */
  610. for (i = 0; i < 64; bp[i++] = 128) ;
  611. } else { /* Load Y/C blocks from input stream */
  612. id = cmp ? 1 : 0; /* Huffman table ID of this component */
  613. /* Extract a DC element from input stream */
  614. d = huffext(jd, id, 0); /* Extract a huffman coded data (bit length) */
  615. if (d < 0) return (JRESULT)(0 - d); /* Err: invalid code or input */
  616. bc = (unsigned int)d;
  617. d = jd->dcv[cmp]; /* DC value of previous block */
  618. if (bc) { /* If there is any difference from previous block */
  619. e = bitext(jd, bc); /* Extract data bits */
  620. if (e < 0) return (JRESULT)(0 - e); /* Err: input */
  621. bc = 1 << (bc - 1); /* MSB position */
  622. if (!(e & bc)) e -= (bc << 1) - 1; /* Restore negative value if needed */
  623. d += e; /* Get current value */
  624. jd->dcv[cmp] = (int16_t)d; /* Save current DC value for next block */
  625. }
  626. dqf = jd->qttbl[jd->qtid[cmp]]; /* De-quantizer table ID for this component */
  627. tmp[0] = d * dqf[0] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
  628. /* Extract following 63 AC elements from input stream */
  629. memset(&tmp[1], 0, 63 * sizeof (int32_t)); /* Initialize all AC elements */
  630. z = 1; /* Top of the AC elements (in zigzag-order) */
  631. do {
  632. d = huffext(jd, id, 1); /* Extract a huffman coded value (zero runs and bit length) */
  633. if (d == 0) break; /* EOB? */
  634. if (d < 0) return (JRESULT)(0 - d); /* Err: invalid code or input error */
  635. bc = (unsigned int)d;
  636. z += bc >> 4; /* Skip leading zero run */
  637. if (z >= 64) return JDR_FMT1; /* Too long zero run */
  638. if (bc &= 0x0F) { /* Bit length? */
  639. d = bitext(jd, bc); /* Extract data bits */
  640. if (d < 0) return (JRESULT)(0 - d); /* Err: input device */
  641. bc = 1 << (bc - 1); /* MSB position */
  642. if (!(d & bc)) d -= (bc << 1) - 1; /* Restore negative value if needed */
  643. i = Zig[z]; /* Get raster-order index */
  644. tmp[i] = d * dqf[i] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
  645. }
  646. } while (++z < 64); /* Next AC element */
  647. if (JD_FORMAT != 2 || !cmp) { /* C components may not be processed if in grayscale output */
  648. if (z == 1 || (JD_USE_SCALE && jd->scale == 3)) { /* If no AC element or scale ratio is 1/8, IDCT can be ommited and the block is filled with DC value */
  649. d = (jd_yuv_t)((*tmp / 256) + 128);
  650. if (JD_FASTDECODE >= 1) {
  651. for (i = 0; i < 64; bp[i++] = d) ;
  652. } else {
  653. memset(bp, d, 64);
  654. }
  655. } else {
  656. block_idct(tmp, bp); /* Apply IDCT and store the block to the MCU buffer */
  657. }
  658. }
  659. }
  660. bp += 64; /* Next block */
  661. }
  662. return JDR_OK; /* All blocks have been loaded successfully */
  663. }
  664. /*-----------------------------------------------------------------------*/
  665. /* Output an MCU: Convert YCrCb to RGB and output it in RGB form */
  666. /*-----------------------------------------------------------------------*/
  667. static JRESULT mcu_output (
  668. JDEC* jd, /* Pointer to the decompressor object */
  669. int (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */
  670. unsigned int x, /* MCU location in the image */
  671. unsigned int y /* MCU location in the image */
  672. )
  673. {
  674. const int CVACC = (sizeof (int) > 2) ? 1024 : 128; /* Adaptive accuracy for both 16-/32-bit systems */
  675. unsigned int ix, iy, mx, my, rx, ry;
  676. int yy, cb, cr;
  677. jd_yuv_t *py, *pc;
  678. uint8_t *pix;
  679. JRECT rect;
  680. mx = jd->msx * 8; my = jd->msy * 8; /* MCU size (pixel) */
  681. rx = (x + mx <= jd->width) ? mx : jd->width - x; /* Output rectangular size (it may be clipped at right/bottom end of image) */
  682. ry = (y + my <= jd->height) ? my : jd->height - y;
  683. if (JD_USE_SCALE) {
  684. rx >>= jd->scale; ry >>= jd->scale;
  685. if (!rx || !ry) return JDR_OK; /* Skip this MCU if all pixel is to be rounded off */
  686. x >>= jd->scale; y >>= jd->scale;
  687. }
  688. rect.left = x; rect.right = x + rx - 1; /* Rectangular area in the frame buffer */
  689. rect.top = y; rect.bottom = y + ry - 1;
  690. if (!JD_USE_SCALE || jd->scale != 3) { /* Not for 1/8 scaling */
  691. pix = (uint8_t*)jd->workbuf;
  692. if (JD_FORMAT != 2) { /* RGB output (build an RGB MCU from Y/C component) */
  693. for (iy = 0; iy < my; iy++) {
  694. pc = py = jd->mcubuf;
  695. if (my == 16) { /* Double block height? */
  696. pc += 64 * 4 + (iy >> 1) * 8;
  697. if (iy >= 8) py += 64;
  698. } else { /* Single block height */
  699. pc += mx * 8 + iy * 8;
  700. }
  701. py += iy * 8;
  702. for (ix = 0; ix < mx; ix++) {
  703. cb = pc[0] - 128; /* Get Cb/Cr component and remove offset */
  704. cr = pc[64] - 128;
  705. if (mx == 16) { /* Double block width? */
  706. if (ix == 8) py += 64 - 8; /* Jump to next block if double block heigt */
  707. pc += ix & 1; /* Step forward chroma pointer every two pixels */
  708. } else { /* Single block width */
  709. pc++; /* Step forward chroma pointer every pixel */
  710. }
  711. yy = *py++; /* Get Y component */
  712. *pix++ = /*R*/ BYTECLIP(yy + ((int)(1.402 * CVACC) * cr) / CVACC);
  713. *pix++ = /*G*/ BYTECLIP(yy - ((int)(0.344 * CVACC) * cb + (int)(0.714 * CVACC) * cr) / CVACC);
  714. *pix++ = /*B*/ BYTECLIP(yy + ((int)(1.772 * CVACC) * cb) / CVACC);
  715. }
  716. }
  717. } else { /* Monochrome output (build a grayscale MCU from Y comopnent) */
  718. for (iy = 0; iy < my; iy++) {
  719. py = jd->mcubuf + iy * 8;
  720. if (my == 16) { /* Double block height? */
  721. if (iy >= 8) py += 64;
  722. }
  723. for (ix = 0; ix < mx; ix++) {
  724. if (mx == 16) { /* Double block width? */
  725. if (ix == 8) py += 64 - 8; /* Jump to next block if double block height */
  726. }
  727. *pix++ = (uint8_t)*py++; /* Get and store a Y value as grayscale */
  728. }
  729. }
  730. }
  731. /* Descale the MCU rectangular if needed */
  732. if (JD_USE_SCALE && jd->scale) {
  733. unsigned int x, y, r, g, b, s, w, a;
  734. uint8_t *op;
  735. /* Get averaged RGB value of each square correcponds to a pixel */
  736. s = jd->scale * 2; /* Number of shifts for averaging */
  737. w = 1 << jd->scale; /* Width of square */
  738. a = (mx - w) * (JD_FORMAT != 2 ? 3 : 1); /* Bytes to skip for next line in the square */
  739. op = (uint8_t*)jd->workbuf;
  740. for (iy = 0; iy < my; iy += w) {
  741. for (ix = 0; ix < mx; ix += w) {
  742. pix = (uint8_t*)jd->workbuf + (iy * mx + ix) * (JD_FORMAT != 2 ? 3 : 1);
  743. r = g = b = 0;
  744. for (y = 0; y < w; y++) { /* Accumulate RGB value in the square */
  745. for (x = 0; x < w; x++) {
  746. r += *pix++; /* Accumulate R or Y (monochrome output) */
  747. if (JD_FORMAT != 2) { /* RGB output? */
  748. g += *pix++; /* Accumulate G */
  749. b += *pix++; /* Accumulate B */
  750. }
  751. }
  752. pix += a;
  753. } /* Put the averaged pixel value */
  754. *op++ = (uint8_t)(r >> s); /* Put R or Y (monochrome output) */
  755. if (JD_FORMAT != 2) { /* RGB output? */
  756. *op++ = (uint8_t)(g >> s); /* Put G */
  757. *op++ = (uint8_t)(b >> s); /* Put B */
  758. }
  759. }
  760. }
  761. }
  762. } else { /* For only 1/8 scaling (left-top pixel in each block are the DC value of the block) */
  763. /* Build a 1/8 descaled RGB MCU from discrete comopnents */
  764. pix = (uint8_t*)jd->workbuf;
  765. pc = jd->mcubuf + mx * my;
  766. cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */
  767. cr = pc[64] - 128;
  768. for (iy = 0; iy < my; iy += 8) {
  769. py = jd->mcubuf;
  770. if (iy == 8) py += 64 * 2;
  771. for (ix = 0; ix < mx; ix += 8) {
  772. yy = *py; /* Get Y component */
  773. py += 64;
  774. if (JD_FORMAT != 2) {
  775. *pix++ = /*R*/ BYTECLIP(yy + ((int)(1.402 * CVACC) * cr / CVACC));
  776. *pix++ = /*G*/ BYTECLIP(yy - ((int)(0.344 * CVACC) * cb + (int)(0.714 * CVACC) * cr) / CVACC);
  777. *pix++ = /*B*/ BYTECLIP(yy + ((int)(1.772 * CVACC) * cb / CVACC));
  778. } else {
  779. *pix++ = yy;
  780. }
  781. }
  782. }
  783. }
  784. /* Squeeze up pixel table if a part of MCU is to be truncated */
  785. mx >>= jd->scale;
  786. if (rx < mx) { /* Is the MCU spans rigit edge? */
  787. uint8_t *s, *d;
  788. unsigned int x, y;
  789. s = d = (uint8_t*)jd->workbuf;
  790. for (y = 0; y < ry; y++) {
  791. for (x = 0; x < rx; x++) { /* Copy effective pixels */
  792. *d++ = *s++;
  793. if (JD_FORMAT != 2) {
  794. *d++ = *s++;
  795. *d++ = *s++;
  796. }
  797. }
  798. s += (mx - rx) * (JD_FORMAT != 2 ? 3 : 1); /* Skip truncated pixels */
  799. }
  800. }
  801. /* Convert RGB888 to RGB565 if needed */
  802. if (JD_FORMAT == 1) {
  803. uint8_t *s = (uint8_t*)jd->workbuf;
  804. uint16_t w, *d = (uint16_t*)s;
  805. unsigned int n = rx * ry;
  806. do {
  807. w = (*s++ & 0xF8) << 8; /* RRRRR----------- */
  808. w |= (*s++ & 0xFC) << 3; /* -----GGGGGG----- */
  809. w |= *s++ >> 3; /* -----------BBBBB */
  810. *d++ = w;
  811. } while (--n);
  812. }
  813. /* Output the rectangular */
  814. return outfunc(jd, jd->workbuf, &rect) ? JDR_OK : JDR_INTR;
  815. }
  816. /*-----------------------------------------------------------------------*/
  817. /* Analyze the JPEG image and Initialize decompressor object */
  818. /*-----------------------------------------------------------------------*/
  819. #define LDB_WORD(ptr) (uint16_t)(((uint16_t)*((uint8_t*)(ptr))<<8)|(uint16_t)*(uint8_t*)((ptr)+1))
  820. JRESULT luat_jd_prepare (
  821. JDEC* jd, /* Blank decompressor object */
  822. size_t (*infunc)(JDEC*, uint8_t*, size_t), /* JPEG strem input function */
  823. void* pool, /* Working buffer for the decompression session */
  824. size_t sz_pool, /* Size of working buffer */
  825. void* dev /* I/O device identifier for the session */
  826. )
  827. {
  828. uint8_t *seg, b;
  829. uint16_t marker;
  830. unsigned int n, i, ofs;
  831. size_t len;
  832. JRESULT rc;
  833. memset(jd, 0, sizeof (JDEC)); /* Clear decompression object (this might be a problem if machine's null pointer is not all bits zero) */
  834. jd->pool = pool; /* Work memroy */
  835. jd->sz_pool = sz_pool; /* Size of given work memory */
  836. jd->infunc = infunc; /* Stream input function */
  837. jd->device = dev; /* I/O device identifier */
  838. jd->inbuf = seg = alloc_pool(jd, JD_SZBUF); /* Allocate stream input buffer */
  839. if (!seg) return JDR_MEM1;
  840. ofs = marker = 0; /* Find SOI marker */
  841. do {
  842. if (jd->infunc(jd, seg, 1) != 1) return JDR_INP; /* Err: SOI was not detected */
  843. ofs++;
  844. marker = marker << 8 | seg[0];
  845. } while (marker != 0xFFD8);
  846. for (;;) { /* Parse JPEG segments */
  847. /* Get a JPEG marker */
  848. if (jd->infunc(jd, seg, 4) != 4) return JDR_INP;
  849. marker = LDB_WORD(seg); /* Marker */
  850. len = LDB_WORD(seg + 2); /* Length field */
  851. if (len <= 2 || (marker >> 8) != 0xFF) return JDR_FMT1;
  852. len -= 2; /* Segent content size */
  853. ofs += 4 + len; /* Number of bytes loaded */
  854. switch (marker & 0xFF) {
  855. case 0xC0: /* SOF0 (baseline JPEG) */
  856. if (len > JD_SZBUF) return JDR_MEM2;
  857. if (jd->infunc(jd, seg, len) != len) return JDR_INP; /* Load segment data */
  858. jd->width = LDB_WORD(&seg[3]); /* Image width in unit of pixel */
  859. jd->height = LDB_WORD(&seg[1]); /* Image height in unit of pixel */
  860. jd->ncomp = seg[5]; /* Number of color components */
  861. if (jd->ncomp != 3 && jd->ncomp != 1) return JDR_FMT3; /* Err: Supports only Grayscale and Y/Cb/Cr */
  862. /* Check each image component */
  863. for (i = 0; i < jd->ncomp; i++) {
  864. b = seg[7 + 3 * i]; /* Get sampling factor */
  865. if (i == 0) { /* Y component */
  866. if (b != 0x11 && b != 0x22 && b != 0x21) { /* Check sampling factor */
  867. return JDR_FMT3; /* Err: Supports only 4:4:4, 4:2:0 or 4:2:2 */
  868. }
  869. jd->msx = b >> 4; jd->msy = b & 15; /* Size of MCU [blocks] */
  870. } else { /* Cb/Cr component */
  871. if (b != 0x11) return JDR_FMT3; /* Err: Sampling factor of Cb/Cr must be 1 */
  872. }
  873. jd->qtid[i] = seg[8 + 3 * i]; /* Get dequantizer table ID for this component */
  874. if (jd->qtid[i] > 3) return JDR_FMT3; /* Err: Invalid ID */
  875. }
  876. break;
  877. case 0xDD: /* DRI - Define Restart Interval */
  878. if (len > JD_SZBUF) return JDR_MEM2;
  879. if (jd->infunc(jd, seg, len) != len) return JDR_INP; /* Load segment data */
  880. jd->nrst = LDB_WORD(seg); /* Get restart interval (MCUs) */
  881. break;
  882. case 0xC4: /* DHT - Define Huffman Tables */
  883. if (len > JD_SZBUF) return JDR_MEM2;
  884. if (jd->infunc(jd, seg, len) != len) return JDR_INP; /* Load segment data */
  885. rc = create_huffman_tbl(jd, seg, len); /* Create huffman tables */
  886. if (rc) return rc;
  887. break;
  888. case 0xDB: /* DQT - Define Quaitizer Tables */
  889. if (len > JD_SZBUF) return JDR_MEM2;
  890. if (jd->infunc(jd, seg, len) != len) return JDR_INP; /* Load segment data */
  891. rc = create_qt_tbl(jd, seg, len); /* Create de-quantizer tables */
  892. if (rc) return rc;
  893. break;
  894. case 0xDA: /* SOS - Start of Scan */
  895. if (len > JD_SZBUF) return JDR_MEM2;
  896. if (jd->infunc(jd, seg, len) != len) return JDR_INP; /* Load segment data */
  897. if (!jd->width || !jd->height) return JDR_FMT1; /* Err: Invalid image size */
  898. if (seg[0] != jd->ncomp) return JDR_FMT3; /* Err: Wrong color components */
  899. /* Check if all tables corresponding to each components have been loaded */
  900. for (i = 0; i < jd->ncomp; i++) {
  901. b = seg[2 + 2 * i]; /* Get huffman table ID */
  902. if (b != 0x00 && b != 0x11) return JDR_FMT3; /* Err: Different table number for DC/AC element */
  903. n = i ? 1 : 0; /* Component class */
  904. if (!jd->huffbits[n][0] || !jd->huffbits[n][1]) { /* Check huffman table for this component */
  905. return JDR_FMT1; /* Err: Nnot loaded */
  906. }
  907. if (!jd->qttbl[jd->qtid[i]]) { /* Check dequantizer table for this component */
  908. return JDR_FMT1; /* Err: Not loaded */
  909. }
  910. }
  911. /* Allocate working buffer for MCU and pixel output */
  912. n = jd->msy * jd->msx; /* Number of Y blocks in the MCU */
  913. if (!n) return JDR_FMT1; /* Err: SOF0 has not been loaded */
  914. len = n * 64 * 2 + 64; /* Allocate buffer for IDCT and RGB output */
  915. if (len < 256) len = 256; /* but at least 256 byte is required for IDCT */
  916. jd->workbuf = alloc_pool(jd, len); /* and it may occupy a part of following MCU working buffer for RGB output */
  917. if (!jd->workbuf) return JDR_MEM1; /* Err: not enough memory */
  918. jd->mcubuf = alloc_pool(jd, (n + 2) * 64 * sizeof (jd_yuv_t)); /* Allocate MCU working buffer */
  919. if (!jd->mcubuf) return JDR_MEM1; /* Err: not enough memory */
  920. /* Align stream read offset to JD_SZBUF */
  921. if (ofs %= JD_SZBUF) {
  922. jd->dctr = jd->infunc(jd, seg + ofs, (size_t)(JD_SZBUF - ofs));
  923. }
  924. jd->dptr = seg + ofs - (JD_FASTDECODE ? 0 : 1);
  925. return JDR_OK; /* Initialization succeeded. Ready to decompress the JPEG image. */
  926. case 0xC1: /* SOF1 */
  927. case 0xC2: /* SOF2 */
  928. case 0xC3: /* SOF3 */
  929. case 0xC5: /* SOF5 */
  930. case 0xC6: /* SOF6 */
  931. case 0xC7: /* SOF7 */
  932. case 0xC9: /* SOF9 */
  933. case 0xCA: /* SOF10 */
  934. case 0xCB: /* SOF11 */
  935. case 0xCD: /* SOF13 */
  936. case 0xCE: /* SOF14 */
  937. case 0xCF: /* SOF15 */
  938. case 0xD9: /* EOI */
  939. LLOGW("Unsuppoted JPEG standard (may be progressive JPEG)");
  940. return JDR_FMT3; /* Unsuppoted JPEG standard (may be progressive JPEG) */
  941. default: /* Unknown segment (comment, exif or etc..) */
  942. /* Skip segment data (null pointer specifies to remove data from the stream) */
  943. if (jd->infunc(jd, 0, len) != len) return JDR_INP;
  944. }
  945. }
  946. }
  947. /*-----------------------------------------------------------------------*/
  948. /* Start to decompress the JPEG picture */
  949. /*-----------------------------------------------------------------------*/
  950. JRESULT luat_jd_decomp (
  951. JDEC* jd, /* Initialized decompression object */
  952. int (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */
  953. uint8_t scale /* Output de-scaling factor (0 to 3) */
  954. )
  955. {
  956. unsigned int x, y, mx, my;
  957. uint16_t rst, rsc;
  958. JRESULT rc;
  959. if (scale > (JD_USE_SCALE ? 3 : 0)) return JDR_PAR;
  960. jd->scale = scale;
  961. mx = jd->msx * 8; my = jd->msy * 8; /* Size of the MCU (pixel) */
  962. jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; /* Initialize DC values */
  963. rst = rsc = 0;
  964. rc = JDR_OK;
  965. for (y = 0; y < jd->height; y += my) { /* Vertical loop of MCUs */
  966. for (x = 0; x < jd->width; x += mx) { /* Horizontal loop of MCUs */
  967. if (jd->nrst && rst++ == jd->nrst) { /* Process restart interval if enabled */
  968. rc = restart(jd, rsc++);
  969. if (rc != JDR_OK) return rc;
  970. rst = 1;
  971. }
  972. rc = mcu_load(jd); /* Load an MCU (decompress huffman coded stream, dequantize and apply IDCT) */
  973. if (rc != JDR_OK) return rc;
  974. rc = mcu_output(jd, outfunc, x, y); /* Output the MCU (YCbCr to RGB, scaling and output) */
  975. if (rc != JDR_OK) return rc;
  976. }
  977. }
  978. return rc;
  979. }