decom.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. #include <windows.h>
  2. #include "decom.h"
  3. #include "jtypes.h"
  4. #define wEndian(x) ( (((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8) )
  5. #define PI 3.1415926539f
  6. #define maximum(a,b) (((a)>(b)) ? (a) : (b))
  7. #define minimum(a,b) (((a)>(b)) ? (b) : (a))
  8. #define PAD4(x) (((x)+3) & 0xFFFFFFFC)
  9. //
  10. // TIMER:
  11. // 0 Time nothing
  12. // 1 Time the IDCT transforms
  13. // 2 Time the entire decompress process
  14. // 3 Time the YUV -> RGB conversion
  15. //
  16. #define TIMER 0
  17. #define USE_RANGE_CHEAT
  18. #define USE_YUV_CHEAT
  19. //#define USE_FLOAT
  20. #ifdef USE_RANGE_CHEAT
  21. const static int range_lim[0x400] = {
  22. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  23. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  24. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0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 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. };
  71. #define range(x, mn, mx) range_lim[(x) & 0x3FF];
  72. #else
  73. #define range(x, mn, mx) ((x) < (mn) ? (mn) : (x) > (mx) ? (mx) : (x))
  74. #endif
  75. //
  76. // we'll assume 10 bits of decimal precision (accurate to 1/1024)
  77. //
  78. #ifndef USE_FLOAT
  79. #define PREC 8
  80. //#define PRECISE_MATH
  81. #define JPEGINT(x) ((int)(((float)(x)) * ((float)(1 << PREC))))
  82. #ifdef PRECISE_MATH
  83. #pragma warning (disable : 4035)
  84. static __inline int MULT(int a, int b)
  85. {
  86. __asm {
  87. mov eax, a;
  88. mov edx, b;
  89. imul edx;
  90. shrd eax, edx, PREC;
  91. }
  92. return;
  93. }
  94. #pragma warning (default : 4035)
  95. #define MMULT(x,y) (((x) * (y)) >> PREC)
  96. #else // PRECISE_MATH
  97. #define MULT(x,y) (((x) * (y)) >> PREC)
  98. #define MMULT MULT
  99. #endif // PRECISE_MATH
  100. #endif // USE_FLOAT
  101. #if TIMER
  102. static __int64 dct_timer;
  103. static int dct_count;
  104. #define StartPerformanceTimer() _asm { \
  105. _asm push eax \
  106. _asm push ecx \
  107. _asm push edx \
  108. _asm mov ecx, offset dct_timer \
  109. _asm _emit 0x0F \
  110. _asm _emit 0x31 \
  111. _asm sub [ecx], eax \
  112. _asm sbb [ecx+4], edx \
  113. _asm pop edx \
  114. _asm pop ecx \
  115. _asm pop eax \
  116. }
  117. #define EndPerformanceTimer() _asm { \
  118. _asm push eax \
  119. _asm push ecx \
  120. _asm push edx \
  121. _asm mov ecx, offset dct_timer \
  122. _asm _emit 0x0F \
  123. _asm _emit 0x31 \
  124. _asm add [ecx], eax \
  125. _asm adc [ecx+4], edx \
  126. _asm pop edx \
  127. _asm pop ecx \
  128. _asm pop eax \
  129. }
  130. #endif
  131. //
  132. // xval and yval contain the x and y coordinates of the zig-zag pattern.
  133. // For example, xval[5], yval[5] is 2,0 to
  134. // indicate that the sixth huffman coded entry (index 5) is at 2,0
  135. //
  136. const static char xval[]={
  137. 0,
  138. 1, 0,
  139. 0, 1, 2,
  140. 3, 2, 1, 0,
  141. 0, 1, 2, 3, 4,
  142. 5, 4, 3, 2, 1, 0,
  143. 0, 1, 2, 3, 4, 5, 6,
  144. 7, 6, 5, 4, 3, 2, 1, 0,
  145. 1, 2, 3, 4, 5, 6, 7,
  146. 7, 6, 5, 4, 3, 2,
  147. 3, 4, 5, 6, 7,
  148. 7, 6, 5, 4,
  149. 5, 6, 7,
  150. 7, 6,
  151. 7};
  152. const static char yval[]={
  153. 0,
  154. 0, 1,
  155. 2, 1, 0,
  156. 0, 1, 2, 3,
  157. 4, 3, 2, 1, 0,
  158. 0, 1, 2, 3, 4, 5,
  159. 6, 5, 4, 3, 2, 1, 0,
  160. 0, 1, 2, 3, 4, 5, 6, 7,
  161. 7, 6, 5, 4, 3, 2, 1,
  162. 2, 3, 4, 5, 6, 7,
  163. 7, 6, 5, 4, 3,
  164. 4, 5, 6, 7,
  165. 7, 6, 5,
  166. 6, 7,
  167. 7};
  168. const static char jpeg_natural_order[] = {
  169. 0, 1, 8, 16, 9, 2, 3, 10,
  170. 17, 24, 32, 25, 18, 11, 4, 5,
  171. 12, 19, 26, 33, 40, 48, 41, 34,
  172. 27, 20, 13, 6, 7, 14, 21, 28,
  173. 35, 42, 49, 56, 57, 50, 43, 36,
  174. 29, 22, 15, 23, 30, 37, 44, 51,
  175. 58, 59, 52, 45, 38, 31, 39, 46,
  176. 53, 60, 61, 54, 47, 55, 62, 63,
  177. };
  178. #ifdef USE_FLOAT
  179. const static float COS[] = {
  180. 1.000000f * 1.414213562f, // 0
  181. 0.980785f * 1.414213562f, // 1
  182. 0.923880f * 1.414213562f, // 2
  183. 0.831470f * 1.414213562f, // 3
  184. 0.707107f * 1.414213562f, // 4
  185. 0.555570f * 1.414213562f, // 5
  186. 0.382683f * 1.414213562f, // 6
  187. 0.195090f * 1.414213562f, // 7
  188. -0.000000f * 1.414213562f, // 8
  189. -0.195090f * 1.414213562f, // 9
  190. -0.382683f * 1.414213562f, // 10
  191. -0.555570f * 1.414213562f, // 11
  192. -0.707107f * 1.414213562f, // 12
  193. -0.831470f * 1.414213562f, // 13
  194. -0.923880f * 1.414213562f, // 14
  195. -0.980785f * 1.414213562f, // 15
  196. -1.000000f * 1.414213562f, // 16
  197. -0.980785f * 1.414213562f, // 17
  198. -0.923879f * 1.414213562f, // 18
  199. -0.831470f * 1.414213562f, // 19
  200. -0.707107f * 1.414213562f, // 20
  201. -0.555570f * 1.414213562f, // 21
  202. -0.382683f * 1.414213562f, // 22
  203. -0.195090f * 1.414213562f, // 23
  204. 0.000000f * 1.414213562f, // 24
  205. 0.195090f * 1.414213562f, // 25
  206. 0.382684f * 1.414213562f, // 26
  207. 0.555570f * 1.414213562f, // 27
  208. 0.707107f * 1.414213562f, // 28
  209. 0.831470f * 1.414213562f, // 29
  210. 0.923880f * 1.414213562f, // 30
  211. 0.980785f * 1.414213562f, // 31
  212. };
  213. #else
  214. const static int COS[] = {
  215. JPEGINT( 1.000000f * 1.414213562f), // 0
  216. JPEGINT( 0.980785f * 1.414213562f), // 1
  217. JPEGINT( 0.923880f * 1.414213562f), // 2
  218. JPEGINT( 0.831470f * 1.414213562f), // 3
  219. JPEGINT( 0.707107f * 1.414213562f), // 4
  220. JPEGINT( 0.555570f * 1.414213562f), // 5
  221. JPEGINT( 0.382683f * 1.414213562f), // 6
  222. JPEGINT( 0.195090f * 1.414213562f), // 7
  223. JPEGINT(-0.000000f * 1.414213562f), // 8
  224. JPEGINT(-0.195090f * 1.414213562f), // 9
  225. JPEGINT(-0.382683f * 1.414213562f), // 10
  226. JPEGINT(-0.555570f * 1.414213562f), // 11
  227. JPEGINT(-0.707107f * 1.414213562f), // 12
  228. JPEGINT(-0.831470f * 1.414213562f), // 13
  229. JPEGINT(-0.923880f * 1.414213562f), // 14
  230. JPEGINT(-0.980785f * 1.414213562f), // 15
  231. JPEGINT(-1.000000f * 1.414213562f), // 16
  232. JPEGINT(-0.980785f * 1.414213562f), // 17
  233. JPEGINT(-0.923879f * 1.414213562f), // 18
  234. JPEGINT(-0.831470f * 1.414213562f), // 19
  235. JPEGINT(-0.707107f * 1.414213562f), // 20
  236. JPEGINT(-0.555570f * 1.414213562f), // 21
  237. JPEGINT(-0.382683f * 1.414213562f), // 22
  238. JPEGINT(-0.195090f * 1.414213562f), // 23
  239. JPEGINT( 0.000000f * 1.414213562f), // 24
  240. JPEGINT( 0.195090f * 1.414213562f), // 25
  241. JPEGINT( 0.382684f * 1.414213562f), // 26
  242. JPEGINT( 0.555570f * 1.414213562f), // 27
  243. JPEGINT( 0.707107f * 1.414213562f), // 28
  244. JPEGINT( 0.831470f * 1.414213562f), // 29
  245. JPEGINT( 0.923880f * 1.414213562f), // 30
  246. JPEGINT( 0.980785f * 1.414213562f), // 31
  247. };
  248. #endif // USE_FLOAT
  249. static HUFFTABLE *huff[2][2]; // class (0: DC, 1: AC), id
  250. static MATRIX qtable[2];
  251. static MATRIX m;
  252. static WORD wRestart;
  253. static BITSTREAM bs;
  254. static FRAME *frame;
  255. #ifdef USE_FLOAT
  256. extern "C" long _ftol(float);
  257. extern "C" const void _fltused(void);
  258. const void _fltused(void)
  259. {
  260. }
  261. long _ftol(float inp)
  262. {
  263. DWORD result1[2];
  264. __asm {
  265. wait;
  266. fistp qword ptr [result1];
  267. }
  268. return result1[0];
  269. }
  270. #endif
  271. void JPEG_cleanup(void)
  272. {
  273. FREE(huff[0][0]);
  274. FREE(huff[0][1]);
  275. FREE(huff[1][0]);
  276. FREE(huff[1][1]);
  277. frame=NULL;
  278. }
  279. BOOL JPEG_process_headers(void *file, int *width, int *height)
  280. {
  281. BYTE *p;
  282. BOOL bEOF=FALSE;
  283. JFIFHEAD *jfif;
  284. WORD Length;
  285. int i,j,n;
  286. jfif=(JFIFHEAD *)file;
  287. huff[0][0] = NULL;
  288. huff[0][1] = NULL;
  289. huff[1][0] = NULL;
  290. huff[1][1] = NULL;
  291. frame = NULL;
  292. wRestart = 0;
  293. if (!file)
  294. return NULL;
  295. //
  296. // make sure we have a valid JFIF file
  297. //
  298. if (
  299. (*(DWORD *)&jfif->SOI != 0xE0FFD8FF) ||
  300. (*(DWORD *)&jfif->Identifier != 'FIFJ') ||
  301. (jfif->Version[0] != 1) )
  302. {
  303. SetLastError(ERR_INVALID_JPEG_FILE);
  304. return FALSE;
  305. }
  306. p = (BYTE *)file + 4 + wEndian(jfif->Length);
  307. Length=wEndian(jfif->Length);
  308. while (!bEOF)
  309. {
  310. if (p[0] == 0xFF)
  311. {
  312. p++;
  313. switch (p[0])
  314. {
  315. case 0xDB: // define quantization tables
  316. p++;
  317. Length = wEndian(*(WORD *)p);
  318. p += 2;
  319. n=0;
  320. while (n < Length - 2)
  321. {
  322. if (p[n] >> 4)
  323. {
  324. for (j=0 ; j<8 ; j++)
  325. {
  326. for (i=0 ; i<8 ; i++)
  327. {
  328. ((COEF *)(qtable[p[n]]))
  329. [jpeg_natural_order[i+j*8]] =
  330. ((COEF *)(&p[1 + n]))[i + j*8];
  331. }
  332. }
  333. }
  334. else
  335. {
  336. for (j=0 ; j<8 ; j++)
  337. {
  338. for (i=0 ; i<8 ; i++)
  339. {
  340. ((COEF *)(qtable[p[n]]))
  341. [jpeg_natural_order[i+j*8]] =
  342. ((char *)(&p[1 + n]))[i + j*8];
  343. }
  344. }
  345. }
  346. n += 1 + ((p[n] + 1) * 64);
  347. }
  348. p += Length - 2;
  349. break;
  350. case 0xC0: // baseline DCT frane, huffman coded
  351. p++;
  352. Length = wEndian(*(WORD *)p);
  353. frame = (FRAME *)(p+2);
  354. p += Length;
  355. *width = wEndian(frame->wWidth);
  356. *height = wEndian(frame->wHeight);
  357. break;
  358. case 0xC4: // define huffman tables
  359. p++;
  360. Length = wEndian(*(WORD *)p);
  361. p += 2;
  362. i=0;
  363. while (i < Length -2 )
  364. {
  365. int huffclass=p[i] >> 4;
  366. int huffid=p[i] & 0x0F;
  367. //
  368. // get the number of entries in the huffman table
  369. //
  370. int cnt;
  371. for (j=0, cnt=0 ; j<16 ; j++)
  372. {
  373. cnt += p[i + j + 1];
  374. }
  375. huff[huffclass][huffid] = (HUFFTABLE *)malloc(sizeof(HUFFTABLE) + cnt * sizeof(HUFFCODE) );
  376. if (!huff)
  377. {
  378. SetLastError(ERR_COULDNT_ALLOCATE_MEMORY);
  379. return FALSE;
  380. }
  381. //
  382. // for getting huffman codes
  383. //
  384. int curbitcount=1;
  385. int curnumber=0;
  386. int num_of_cur_bitcount=0;
  387. huff[huffclass][huffid]->size = cnt;
  388. huff[huffclass][huffid]->first9bit = 0;
  389. for (j=0 ; j<cnt ; j++)
  390. {
  391. while (num_of_cur_bitcount >= p[i + curbitcount])
  392. {
  393. num_of_cur_bitcount=0;
  394. curbitcount++;
  395. curnumber <<= 1;
  396. }
  397. //
  398. // set a marker for the first huffman code that's 9 bits in length
  399. // (the first one that we can't quick-lookup)
  400. //
  401. if ((curbitcount == 9) && (huff[huffclass][huffid]->first9bit))
  402. {
  403. huff[huffclass][huffid]->first9bit = j;
  404. }
  405. huff[huffclass][huffid]->code[j].dwBits = curnumber << (32 - curbitcount);
  406. huff[huffclass][huffid]->code[j].dwMask = (int)0x80000000 >> (curbitcount - 1);
  407. huff[huffclass][huffid]->code[j].value = (char)p[i + 17 + j];
  408. huff[huffclass][huffid]->code[j].numbits = (char)curbitcount;
  409. num_of_cur_bitcount++;
  410. curnumber++;
  411. }
  412. i += 17 + cnt;
  413. //
  414. // generate quick lookups
  415. //
  416. for (j=0 ; j<256 ; j++)
  417. {
  418. int i;
  419. for (i=0 ; i<huff[huffclass][huffid]->size && huff[huffclass][huffid]->code[i].numbits <=8 ; i++)
  420. {
  421. if (((j<<24 & huff[huffclass][huffid]->code[i].dwMask) ==
  422. huff[huffclass][huffid]->code[i].dwBits) &&
  423. (!(huff[huffclass][huffid]->code[i].dwBits & 0x00FFFFFF)))
  424. break;
  425. }
  426. if ((i<huff[huffclass][huffid]->size) && (huff[huffclass][huffid]->code[i].numbits<=8))
  427. huff[huffclass][huffid]->quick_lookup[j] = i;
  428. else
  429. huff[huffclass][huffid]->quick_lookup[j] = -1;
  430. }
  431. }
  432. p += Length - 2;
  433. break;
  434. case 0xDD: // define restart interval
  435. p++;
  436. wRestart = wEndian(*(WORD *)(p+2));
  437. Length = wEndian(*(WORD *)p);
  438. p += Length;
  439. break;
  440. case 0xDA: // define start of scan
  441. p++;
  442. Length = wEndian(*(WORD *)p);
  443. p += Length;
  444. //
  445. // set up the bit stream
  446. //
  447. bs.dwBits = 0;
  448. bs.p = (BYTE *)p;
  449. bs.nEmpty = 0;
  450. bs.dwBits <<= 8; bs.dwBits |= *bs.p; bs.p++;
  451. bs.dwBits <<= 8; bs.dwBits |= *bs.p; bs.p++;
  452. bs.dwBits <<= 8; bs.dwBits |= *bs.p; bs.p++;
  453. bs.dwBits <<= 8; bs.dwBits |= *bs.p; bs.p++;
  454. break;
  455. case 0xD9: // end of image
  456. bEOF=TRUE;
  457. break;
  458. case 0xC1:
  459. case 0xC2:
  460. case 0xC3:
  461. case 0xC5:
  462. case 0xC6:
  463. case 0xC7:
  464. SetLastError(ERR_EXTENDED_JPEG_FORMAT_NOT_SUPPORTED);
  465. return FALSE;
  466. case 0xE0:
  467. case 0xE1:
  468. case 0xE2:
  469. case 0xE3:
  470. case 0xE4:
  471. case 0xE5:
  472. case 0xE6:
  473. case 0xE7:
  474. case 0xE8:
  475. case 0xE9:
  476. case 0xEA:
  477. case 0xEB:
  478. case 0xEC:
  479. case 0xED:
  480. case 0xEE:
  481. case 0xEF:
  482. p++;
  483. Length = wEndian(*(WORD *)p);
  484. p += Length;
  485. break;
  486. case 0xD0:
  487. case 0xD1:
  488. case 0xD2:
  489. case 0xD3:
  490. case 0xD4:
  491. case 0xD5:
  492. case 0xD6:
  493. case 0xD7:
  494. p++;
  495. break;
  496. case 0xFE:
  497. // comment
  498. p++;
  499. Length = wEndian(*(WORD *)p);
  500. p += Length;
  501. break;
  502. case 0x00:
  503. break;
  504. case 0xFF:
  505. break;
  506. default:
  507. SetLastError(-1);
  508. return FALSE;
  509. break;
  510. }
  511. }
  512. else
  513. {
  514. p++;
  515. }
  516. }
  517. if (!frame)
  518. return FALSE;
  519. return TRUE;
  520. }
  521. static __inline int get_huffcode(BITSTREAM *bs, HUFFTABLE *ht)
  522. {
  523. int i;
  524. if ((i=ht->quick_lookup[bs->dwBits >> 24]) == -1)
  525. {
  526. //
  527. // we couldn't do a quick-lookup, so go through the list
  528. //
  529. //for (i=0 ; i<ht->size ; i++)
  530. for (i=ht->first9bit ; i<ht->size ; i++)
  531. {
  532. if ((bs->dwBits & ht->code[i].dwMask) == ht->code[i].dwBits)
  533. break;
  534. }
  535. }
  536. if (i >= ht->size)
  537. return 0;
  538. //
  539. // we've found a match at i, so remove ht->code[i].numbits
  540. //
  541. bs->dwBits <<= ht->code[i].numbits;
  542. bs->nEmpty += ht->code[i].numbits;
  543. //
  544. // handle the need to read another byte into dwBits
  545. //
  546. while (bs->nEmpty >= 8)
  547. {
  548. int temp;
  549. temp=*(bs->p);
  550. (bs->p)++;
  551. while (temp == 0xFF)
  552. {
  553. if (*(bs->p) == 0x00)
  554. {
  555. (bs->p)++;
  556. break;
  557. }
  558. else
  559. {
  560. (bs->p)++;
  561. temp=*(bs->p);
  562. (bs->p)++;
  563. }
  564. }
  565. temp <<= (bs->nEmpty - 8);
  566. bs->dwBits |= temp;
  567. bs->nEmpty -= 8;
  568. }
  569. return ht->code[i].value;
  570. }
  571. static /*__inline*/ int get_vli(BITSTREAM *bs, int numbits)
  572. {
  573. DWORD temp;
  574. int mask;
  575. int negative;
  576. int retval;
  577. //
  578. // create a mask
  579. //
  580. mask=0x80000000;
  581. if (bs->dwBits & mask)
  582. negative=0;
  583. else
  584. negative=1;
  585. mask >>= (numbits - 1);
  586. temp = bs->dwBits & mask;
  587. if (negative)
  588. temp ^= mask;
  589. temp >>= (32 - numbits);
  590. retval = (int) temp;
  591. if (negative)
  592. retval = -retval;
  593. //
  594. // now remove the bits from the bitstream
  595. //
  596. bs->dwBits <<= numbits;
  597. bs->nEmpty += numbits;
  598. //
  599. // handle the need to read another byte into dwBits
  600. //
  601. while (bs->nEmpty >= 8)
  602. {
  603. int temp;
  604. temp=*(bs->p);
  605. (bs->p)++;
  606. while (temp == 0xFF)
  607. {
  608. if (*(bs->p) == 0x00)
  609. {
  610. (bs->p)++;
  611. break;
  612. }
  613. else
  614. {
  615. (bs->p)++;
  616. temp=*(bs->p);
  617. (bs->p)++;
  618. }
  619. }
  620. temp <<= (bs->nEmpty - 8);
  621. bs->dwBits |= temp;
  622. bs->nEmpty -= 8;
  623. }
  624. return retval;
  625. }
  626. #ifdef USE_FLOAT
  627. void __inline IDCT(MATRIX *m, MATRIX *q)
  628. {
  629. int i,j;
  630. float fm[8][8];
  631. float ws[8][8];
  632. float _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
  633. for (j=0 ; j<8 ; j++)
  634. {
  635. for (i=0 ; i<8 ; i++)
  636. {
  637. float Cu,Cv;
  638. Cu = (i==0) ? 1.0f : COS[i & 31];
  639. Cv = (j==0) ? 1.0f : COS[j & 31];
  640. fm[i][j] = (float)((*m)[i][j]) * (float)((*q)[i][j]) * Cu * Cv;
  641. }
  642. }
  643. for (i=0 ; i<8 ; i++)
  644. {
  645. // Even part
  646. _a = fm[i][0];
  647. _b = fm[i][2];
  648. _c = fm[i][4];
  649. _d = fm[i][6];
  650. _i = _a + _c; // phase 3
  651. _j = _a - _c;
  652. _l = _b + _d; // phases 5-3
  653. _k = (_b - _d) * (1.414213562f) - _l; // 2*c4
  654. _a = _i + _l; // phase 2
  655. _d = _i - _l;
  656. _b = _j + _k;
  657. _c = _j - _k;
  658. // Odd part
  659. _e = fm[i][1];
  660. _f = fm[i][3];
  661. _g = fm[i][5];
  662. _h = fm[i][7];
  663. _q = _g + _f; // phase 6
  664. _n = _g - _f;
  665. _o = _e + _h;
  666. _p = _e - _h;
  667. _h = _o + _q; // phase 5
  668. _j = (_o - _q) * (1.414213562f); // 2*c4
  669. _m = (_n + _p) * (1.847759065f); // 2*c2
  670. _i = (1.082392200f) * _p - _m; // 2*(c2-c6)
  671. _k = (-2.613125930f) * _n + _m; // -2*(c2+c6)
  672. _g = _k - _h; // phase 2
  673. _f = _j - _g;
  674. _e = _i + _f;
  675. ws[i][0] = _a + _h;
  676. ws[i][7] = _a - _h;
  677. ws[i][1] = _b + _g;
  678. ws[i][6] = _b - _g;
  679. ws[i][2] = _c + _f;
  680. ws[i][5] = _c - _f;
  681. ws[i][4] = _d + _e;
  682. ws[i][3] = _d - _e;
  683. }
  684. for (j=0 ; j<8 ; j++)
  685. {
  686. // Even part
  687. _i = ws[0][j] + ws[4][j];
  688. _j = ws[0][j] - ws[4][j];
  689. _l = ws[2][j] + ws[6][j];
  690. _k = (ws[2][j] - ws[6][j]) * (1.414213562f) - _l;
  691. _a = _i + _l;
  692. _d = _i - _l;
  693. _b = _j + _k;
  694. _c = _j - _k;
  695. // Odd part
  696. _q = ws[5][j] + ws[3][j];
  697. _n = ws[5][j] - ws[3][j];
  698. _o = ws[1][j] + ws[7][j];
  699. _p = ws[1][j] - ws[7][j];
  700. _h = _o + _q;
  701. _j = (_o - _q) * (1.414213562f);
  702. _m = (_n + _p) * (1.847759065f); // 2*c2
  703. _i = (1.082392200f) * _p - _m; // 2*(c2-c6)
  704. _k = (-2.613125930f) * _n + _m; // -2*(c2+c6)
  705. _g = _k - _h;
  706. _f = _j - _g;
  707. _e = _i + _f;
  708. // Final output stage: scale down by a factor of 8 and range-limit
  709. (*m)[0][j] = range(((((int)(_a + _h)) << 19) >> 22) + 128, 0, 255);
  710. (*m)[7][j] = range(((((int)(_a - _h)) << 19) >> 22) + 128, 0, 255);
  711. (*m)[1][j] = range(((((int)(_b + _g)) << 19) >> 22) + 128, 0, 255);
  712. (*m)[6][j] = range(((((int)(_b - _g)) << 19) >> 22) + 128, 0, 255);
  713. (*m)[2][j] = range(((((int)(_c + _f)) << 19) >> 22) + 128, 0, 255);
  714. (*m)[5][j] = range(((((int)(_c - _f)) << 19) >> 22) + 128, 0, 255);
  715. (*m)[4][j] = range(((((int)(_d + _e)) << 19) >> 22) + 128, 0, 255);
  716. (*m)[3][j] = range(((((int)(_d - _e)) << 19) >> 22) + 128, 0, 255);
  717. }
  718. return;
  719. }
  720. #else
  721. void __inline IDCT(MATRIX *m, MATRIX *q)
  722. {
  723. int i,j;
  724. int fm[8][8];
  725. int ws[8][8];
  726. int _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
  727. #if TIMER==1
  728. dct_count++;
  729. StartPerformanceTimer();
  730. #endif
  731. for (j=0 ; j<8 ; j++)
  732. {
  733. for (i=0 ; i<8 ; i++)
  734. {
  735. int Cu,Cv;
  736. Cu = (i==0) ? JPEGINT(1.0f) : COS[i & 31];
  737. Cv = (j==0) ? JPEGINT(1.0f) : COS[j & 31];
  738. fm[i][j] = (int)((*m)[i][j]) * (int)((*q)[i][j]) * MMULT(Cu, Cv);
  739. }
  740. }
  741. for (i=0 ; i<8 ; i++)
  742. {
  743. // Even part
  744. _a = fm[i][0];
  745. _b = fm[i][2];
  746. _c = fm[i][4];
  747. _d = fm[i][6];
  748. _i = _a + _c; // phase 3
  749. _j = _a - _c;
  750. _l = _b + _d; // phases 5-3
  751. _k = MULT(_b - _d, JPEGINT(1.414213562f)) - _l; // 2*c4
  752. _a = _i + _l; // phase 2
  753. _d = _i - _l;
  754. _b = _j + _k;
  755. _c = _j - _k;
  756. // Odd part
  757. _e = fm[i][1];
  758. _f = fm[i][3];
  759. _g = fm[i][5];
  760. _h = fm[i][7];
  761. _q = _g + _f; // phase 6
  762. _n = _g - _f;
  763. _o = _e + _h;
  764. _p = _e - _h;
  765. _h = _o + _q; // phase 5
  766. _j = MULT(_o - _q, JPEGINT(1.414213562f)); // 2*c4
  767. _m = MULT((_n + _p), JPEGINT(1.847759065f)); // 2*c2
  768. _i = MULT(JPEGINT(1.082392200f), _p) - _m; // 2*(c2-c6)
  769. _k = MULT(JPEGINT(-2.613125930f), _n) + _m; // -2*(c2+c6)
  770. _g = _k - _h; // phase 2
  771. _f = _j - _g;
  772. _e = _i + _f;
  773. ws[i][0] = _a + _h;
  774. ws[i][7] = _a - _h;
  775. ws[i][1] = _b + _g;
  776. ws[i][6] = _b - _g;
  777. ws[i][2] = _c + _f;
  778. ws[i][5] = _c - _f;
  779. ws[i][4] = _d + _e;
  780. ws[i][3] = _d - _e;
  781. }
  782. for (j=0 ; j<8 ; j++)
  783. {
  784. // Even part
  785. _i = ws[0][j] + ws[4][j];
  786. _j = ws[0][j] - ws[4][j];
  787. _l = ws[2][j] + ws[6][j];
  788. _k = MULT((ws[2][j] - ws[6][j]), JPEGINT(1.414213562f)) - _l;
  789. _a = _i + _l;
  790. _d = _i - _l;
  791. _b = _j + _k;
  792. _c = _j - _k;
  793. // Odd part
  794. _q = ws[5][j] + ws[3][j];
  795. _n = ws[5][j] - ws[3][j];
  796. _o = ws[1][j] + ws[7][j];
  797. _p = ws[1][j] - ws[7][j];
  798. _h = _o + _q;
  799. _j = MULT((_o - _q), JPEGINT(1.414213562f));
  800. _m = MULT((_n + _p), JPEGINT(1.847759065f)); // 2*c2
  801. _i = MULT(JPEGINT(1.082392200f), _p) - _m; // 2*(c2-c6)
  802. _k = MULT(JPEGINT(-2.613125930f), _n) + _m; // -2*(c2+c6)
  803. _g = _k - _h;
  804. _f = _j - _g;
  805. _e = _i + _f;
  806. // Final output stage: scale down by a factor of 8 and range-limit
  807. (*m)[0][j] = range(((((int)(_a + _h)) << (19 - PREC)) >> 22) + 128, 0, 255);
  808. (*m)[7][j] = range(((((int)(_a - _h)) << (19 - PREC)) >> 22) + 128, 0, 255);
  809. (*m)[1][j] = range(((((int)(_b + _g)) << (19 - PREC)) >> 22) + 128, 0, 255);
  810. (*m)[6][j] = range(((((int)(_b - _g)) << (19 - PREC)) >> 22) + 128, 0, 255);
  811. (*m)[2][j] = range(((((int)(_c + _f)) << (19 - PREC)) >> 22) + 128, 0, 255);
  812. (*m)[5][j] = range(((((int)(_c - _f)) << (19 - PREC)) >> 22) + 128, 0, 255);
  813. (*m)[4][j] = range(((((int)(_d + _e)) << (19 - PREC)) >> 22) + 128, 0, 255);
  814. (*m)[3][j] = range(((((int)(_d - _e)) << (19 - PREC)) >> 22) + 128, 0, 255);
  815. }
  816. #if TIMER==1
  817. EndPerformanceTimer();
  818. #endif
  819. return;
  820. }
  821. #endif // USE_FLOAT
  822. #ifdef USE_YUV_CHEAT
  823. void __inline YUV2RGB(int maxw, int maxh, void *pbits, int span)
  824. {
  825. register int i,j;
  826. //
  827. // convert from YUV to RGB
  828. //
  829. register BYTE *pb;
  830. for (j=0 ; j<maxh ; j++)
  831. {
  832. pb=(BYTE *)((int)pbits + j*span);
  833. for (i=0 ; i<maxw ; i++)
  834. {
  835. int r,g,b,y,u,v;
  836. y=(int)(pb[0]);
  837. v=(int)(pb[1]);
  838. u=(int)(pb[2]);
  839. u -= 128;
  840. v -= 128;
  841. r=(65536*y + 91881*v + 32768) >> 16;
  842. g=(65536*y - 22554*u - 46802*v + 32768) >> 16;
  843. b=(65536*y + 116130*u + 32768) >> 16;
  844. *pb++=(BYTE)range(r, 0, 255);
  845. *pb++=(BYTE)range(g, 0, 255);
  846. *pb++=(BYTE)range(b, 0, 255);
  847. }
  848. }
  849. return;
  850. }
  851. void __inline YUV2GS(int maxw, int maxh, void *pbits, int span)
  852. {
  853. register int i,j;
  854. //
  855. // convert from YUV to RGB
  856. //
  857. register BYTE *pb;
  858. for (j=0 ; j<maxh ; j++)
  859. {
  860. pb=(BYTE *)((int)pbits + j*span);
  861. for (i=0 ; i<maxw ; i++)
  862. {
  863. register BYTE y=*pb++;
  864. *pb++=y;
  865. *pb++=y;
  866. }
  867. }
  868. return;
  869. }
  870. #else
  871. void __inline YUV2RGB(int maxw, int maxh, void *pbits, int span)
  872. {
  873. register int i,j;
  874. //
  875. // convert from YUV to RGB
  876. //
  877. for (j=0 ; j<maxh ; j++)
  878. {
  879. for (i=0 ; i<maxw ; i++)
  880. {
  881. register int offset;
  882. int r,g,b,y,u,v;
  883. offset= i*3 + j*span;
  884. y=(int)((BYTE *)pbits)[offset + 0];
  885. v=(int)((BYTE *)pbits)[offset + 1];
  886. u=(int)((BYTE *)pbits)[offset + 2];
  887. u -= 128;
  888. v -= 128;
  889. r=(65536*y + 91881*v + 32768) >> 16;
  890. g=(65536*y - 22554*u - 46802*v + 32768) >> 16;
  891. b=(65536*y + 116130*u + 32768) >> 16;
  892. ((BYTE *)pbits)[offset + 0]=(BYTE)range(r, 0, 255);
  893. ((BYTE *)pbits)[offset + 1]=(BYTE)range(g, 0, 255);
  894. ((BYTE *)pbits)[offset + 2]=(BYTE)range(b, 0, 255);
  895. }
  896. }
  897. return;
  898. }
  899. #endif
  900. static volatile int baba=0;
  901. BOOL JPEG_decompress(void *pbits)
  902. {
  903. int i,j;
  904. int u,v,w,h;
  905. int s,r;
  906. int k;
  907. int last_dc[3];
  908. int maxw, maxh, span;
  909. WORD mcu_restart;
  910. #if TIMER==1
  911. dct_timer = 0;
  912. dct_count =0;
  913. #endif
  914. #if TIMER==2
  915. dct_timer = 0;
  916. StartPerformanceTimer();
  917. #endif
  918. //
  919. // calculate the largest component hor and ver samp factors
  920. //
  921. int hsf, vsf;
  922. for (hsf=0, vsf=0, i=0 ; i<frame->nComps ; i++)
  923. {
  924. if ( ((frame->comp[i].samp_factor >> 4) & 0x0F) > hsf)
  925. hsf = ((frame->comp[i].samp_factor >> 4) & 0x0F);
  926. if ( ((frame->comp[i].samp_factor ) & 0x0F) > vsf)
  927. vsf = ((frame->comp[i].samp_factor ) & 0x0F);
  928. }
  929. //
  930. // isize and jsize are the hor and ver number of pixels to copy
  931. //
  932. int isize[3], jsize[3], comp;
  933. for (comp=0 ; comp<frame->nComps ; comp++)
  934. {
  935. isize[comp] = hsf / ((frame->comp[comp].samp_factor >> 4) & 0x0F);
  936. jsize[comp] = vsf / ((frame->comp[comp].samp_factor ) & 0x0F);
  937. }
  938. //
  939. // now set up w and h to be the number of hor and ver 8x8 blocks
  940. //
  941. w = ((wEndian(frame->wWidth) + 7) >> 3);
  942. h = ((wEndian(frame->wHeight) + 7) >> 3);
  943. //
  944. // now set up w and h to be the number of horizontal and vertical mcus
  945. //
  946. w = (w + hsf - 1) / hsf;
  947. h = (h + vsf - 1) / vsf;
  948. last_dc[0]=0;
  949. last_dc[1]=0;
  950. last_dc[2]=0;
  951. mcu_restart=0;
  952. maxw=wEndian(frame->wWidth);
  953. maxh=wEndian(frame->wHeight);
  954. span=PAD4(maxw*3);
  955. for (v=0 ; v<(h) ; v++)
  956. {
  957. for (u=0 ; u<w ; u++)
  958. {
  959. if ((wRestart) && (mcu_restart == wRestart))
  960. {
  961. mcu_restart=0;
  962. last_dc[0]=0;
  963. last_dc[1]=0;
  964. last_dc[2]=0;
  965. //
  966. // align bits on a byte-boundary
  967. //
  968. if (bs.nEmpty)
  969. get_vli(&bs, 8-(bs.nEmpty));
  970. }
  971. mcu_restart++;
  972. for (comp=0 ; comp<frame->nComps ; comp++)
  973. {
  974. int xc,yc;
  975. for (yc=0 ; yc<(frame->comp[comp].samp_factor & 0x0F) ; yc++)
  976. {
  977. for (xc=0 ; xc<((frame->comp[comp].samp_factor >> 4) & 0x0F) ; xc++)
  978. {
  979. int huffid;
  980. MATRIX *q;
  981. huffid=!!comp;
  982. q=&qtable[!!comp];
  983. //
  984. // clear out the matrix
  985. //
  986. for (j=0 ; j<8 ; j++)
  987. {
  988. for (i=0 ; i<8 ; i++)
  989. {
  990. m[i][j]=0;
  991. }
  992. }
  993. s=get_huffcode(&bs, huff[0][huffid]);
  994. if (s)
  995. {
  996. s=get_vli(&bs, s);
  997. }
  998. m[0][0]=s + last_dc[comp];
  999. last_dc[comp] = m[0][0];
  1000. //
  1001. // get AC values
  1002. //
  1003. for (k=1 ; k<64 ; k++)
  1004. {
  1005. s=get_huffcode(&bs, huff[1][huffid]);
  1006. if (s)
  1007. {
  1008. r = ((BYTE)s) >> 4;
  1009. s = s & 0x0F;
  1010. if ((r == 15) && (s==0))
  1011. {
  1012. k += 15;
  1013. }
  1014. else
  1015. {
  1016. k += r;
  1017. s = get_vli(&bs, s);
  1018. m[xval[k]][yval[k]] = s;
  1019. }
  1020. }
  1021. else
  1022. {
  1023. k=64;
  1024. }
  1025. }
  1026. IDCT(&m, q);
  1027. //
  1028. // emit the byte(s)
  1029. //
  1030. for (j=0 ; j<8 ; j++)
  1031. {
  1032. for (i=0 ; i<8 ; i++)
  1033. {
  1034. int w, h;
  1035. BYTE *p;
  1036. w = (((u*hsf) + xc)*8 + i*isize[comp]);
  1037. h = (((v*vsf) + yc)*8 + j*jsize[comp]);
  1038. h = maxh - 1 - h;
  1039. p = &(((BYTE *)pbits)[3*w + h*span + comp]);
  1040. for (int jr=0 ; jr<jsize[comp] ; jr++)
  1041. {
  1042. for (int ir=0 ; ir<isize[comp] ; ir++)
  1043. {
  1044. if (((w+ir)<maxw) && (h>=jr))
  1045. {
  1046. p[ir*3 - jr*span] = (BYTE)m[i][j];
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. #if TIMER==3
  1058. StartPerformanceTimer();
  1059. #endif
  1060. if (frame->nComps == 1)
  1061. YUV2GS(maxw, maxh, pbits, span);
  1062. else
  1063. YUV2RGB(maxw, maxh, pbits, span);
  1064. #if TIMER==1
  1065. if (1)
  1066. {
  1067. char string[1024];
  1068. double a = (double)dct_timer;
  1069. double b = (double)dct_count;
  1070. a /= b;
  1071. wsprintf(string, "Took %d cycles per DCT\n", (int)(a));
  1072. OutputDebugString(string);
  1073. }
  1074. #endif
  1075. #if TIMER==2
  1076. EndPerformanceTimer();
  1077. if (1)
  1078. {
  1079. char string[1024];
  1080. double a = (double)dct_timer;
  1081. wsprintf(string, "Took %d cycles total\n", (int)(a));
  1082. OutputDebugString(string);
  1083. }
  1084. #endif
  1085. #if TIMER==3
  1086. EndPerformanceTimer();
  1087. if (1)
  1088. {
  1089. char string[1024];
  1090. double a = (double)dct_timer;
  1091. wsprintf(string, "Took %d cycles total\n", (int)(a));
  1092. OutputDebugString(string);
  1093. }
  1094. #endif
  1095. return TRUE;
  1096. }
  1097. extern "C" void do_idct(void *, void *);
  1098. void do_idct(void *p1, void *p2)
  1099. {
  1100. IDCT((MATRIX *)p1, (MATRIX *)p2);
  1101. }