Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
12 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcdctmgr.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains the forward-DCT management logic.
  11. * This code selects a particular DCT implementation to be used,
  12. * and it performs related housekeeping chores including coefficient
  13. * quantization.
  14. */
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. #include "jdct.h" /* Private declarations for DCT subsystem */
  19. /* Private subobject for this module */
  20. typedef struct {
  21. struct jpeg_forward_dct pub; /* public fields */
  22. /* Pointer to the DCT routine actually in use */
  23. forward_DCT_method_ptr do_dct;
  24. /* The actual post-DCT divisors --- not identical to the quant table
  25. * entries, because of scaling (especially for an unnormalized DCT).
  26. * Each table is given in normal array order.
  27. */
  28. DCTELEM * divisors[NUM_QUANT_TBLS];
  29. #ifdef DCT_FLOAT_SUPPORTED
  30. /* Same as above for the floating-point case. */
  31. float_DCT_method_ptr do_float_dct;
  32. FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
  33. #endif
  34. } my_fdct_controller;
  35. typedef my_fdct_controller * my_fdct_ptr;
  36. /*
  37. * Initialize for a processing pass.
  38. * Verify that all referenced Q-tables are present, and set up
  39. * the divisor table for each one.
  40. * In the current implementation, DCT of all components is done during
  41. * the first pass, even if only some components will be output in the
  42. * first scan. Hence all components should be examined here.
  43. */
  44. METHODDEF(void)
  45. start_pass_fdctmgr (j_compress_ptr cinfo)
  46. {
  47. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  48. int ci, qtblno, i;
  49. jpeg_component_info *compptr;
  50. JQUANT_TBL * qtbl;
  51. DCTELEM * dtbl;
  52. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  53. ci++, compptr++) {
  54. qtblno = compptr->quant_tbl_no;
  55. /* Make sure specified quantization table is present */
  56. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  57. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  58. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  59. qtbl = cinfo->quant_tbl_ptrs[qtblno];
  60. /* Compute divisors for this quant table */
  61. /* We may do this more than once for same table, but it's not a big deal */
  62. switch (cinfo->dct_method) {
  63. #ifdef DCT_ISLOW_SUPPORTED
  64. case JDCT_ISLOW:
  65. /* For LL&M IDCT method, divisors are equal to raw quantization
  66. * coefficients multiplied by 8 (to counteract scaling).
  67. */
  68. if (fdct->divisors[qtblno] == NULL) {
  69. fdct->divisors[qtblno] = (DCTELEM *)
  70. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  71. DCTSIZE2 * SIZEOF(DCTELEM));
  72. }
  73. dtbl = fdct->divisors[qtblno];
  74. for (i = 0; i < DCTSIZE2; i++) {
  75. dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
  76. }
  77. break;
  78. #endif
  79. #ifdef DCT_IFAST_SUPPORTED
  80. case JDCT_IFAST:
  81. {
  82. /* For AA&N IDCT method, divisors are equal to quantization
  83. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  84. * scalefactor[0] = 1
  85. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  86. * We apply a further scale factor of 8.
  87. */
  88. #define CONST_BITS 14
  89. static const INT16 aanscales[DCTSIZE2] = {
  90. /* precomputed values scaled up by 14 bits */
  91. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  92. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  93. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  94. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  95. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  96. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  97. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  98. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  99. };
  100. SHIFT_TEMPS
  101. if (fdct->divisors[qtblno] == NULL) {
  102. fdct->divisors[qtblno] = (DCTELEM *)
  103. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  104. DCTSIZE2 * SIZEOF(DCTELEM));
  105. }
  106. dtbl = fdct->divisors[qtblno];
  107. for (i = 0; i < DCTSIZE2; i++) {
  108. dtbl[i] = (DCTELEM)
  109. DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  110. (INT32) aanscales[i]),
  111. CONST_BITS-3);
  112. }
  113. }
  114. break;
  115. #endif
  116. #ifdef DCT_FLOAT_SUPPORTED
  117. case JDCT_FLOAT:
  118. {
  119. /* For float AA&N IDCT method, divisors are equal to quantization
  120. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  121. * scalefactor[0] = 1
  122. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  123. * We apply a further scale factor of 8.
  124. * What's actually stored is 1/divisor so that the inner loop can
  125. * use a multiplication rather than a division.
  126. */
  127. FAST_FLOAT * fdtbl;
  128. int row, col;
  129. static const double aanscalefactor[DCTSIZE] = {
  130. 1.0, 1.387039845, 1.306562965, 1.175875602,
  131. 1.0, 0.785694958, 0.541196100, 0.275899379
  132. };
  133. if (fdct->float_divisors[qtblno] == NULL) {
  134. fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  135. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  136. DCTSIZE2 * SIZEOF(FAST_FLOAT));
  137. }
  138. fdtbl = fdct->float_divisors[qtblno];
  139. i = 0;
  140. for (row = 0; row < DCTSIZE; row++) {
  141. for (col = 0; col < DCTSIZE; col++) {
  142. fdtbl[i] = (FAST_FLOAT)
  143. (1.0 / (((double) qtbl->quantval[i] *
  144. aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  145. i++;
  146. }
  147. }
  148. }
  149. break;
  150. #endif
  151. default:
  152. ERREXIT(cinfo, JERR_NOT_COMPILED);
  153. break;
  154. }
  155. }
  156. }
  157. /*
  158. * Perform forward DCT on one or more blocks of a component.
  159. *
  160. * The input samples are taken from the sample_data[] array starting at
  161. * position start_row/start_col, and moving to the right for any additional
  162. * blocks. The quantized coefficients are returned in coef_blocks[].
  163. */
  164. METHODDEF(void)
  165. forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  166. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  167. JDIMENSION start_row, JDIMENSION start_col,
  168. JDIMENSION num_blocks)
  169. /* This version is used for integer DCT implementations. */
  170. {
  171. /* This routine is heavily used, so it's worth coding it tightly. */
  172. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  173. forward_DCT_method_ptr do_dct = fdct->do_dct;
  174. DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  175. DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  176. JDIMENSION bi;
  177. sample_data += start_row; /* fold in the vertical offset once */
  178. for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  179. /* Load data into workspace, applying unsigned->signed conversion */
  180. { register DCTELEM *workspaceptr;
  181. register JSAMPROW elemptr;
  182. register int elemr;
  183. workspaceptr = workspace;
  184. for (elemr = 0; elemr < DCTSIZE; elemr++) {
  185. elemptr = sample_data[elemr] + start_col;
  186. #if DCTSIZE == 8 /* unroll the inner loop */
  187. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  188. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  189. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  190. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  191. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  192. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  193. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  194. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  195. #else
  196. { register int elemc;
  197. for (elemc = DCTSIZE; elemc > 0; elemc--) {
  198. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  199. }
  200. }
  201. #endif
  202. }
  203. }
  204. /* Perform the DCT */
  205. (*do_dct) (workspace);
  206. /* Quantize/descale the coefficients, and store into coef_blocks[] */
  207. { register DCTELEM temp, qval;
  208. register int i;
  209. register JCOEFPTR output_ptr = coef_blocks[bi];
  210. for (i = 0; i < DCTSIZE2; i++) {
  211. qval = divisors[i];
  212. temp = workspace[i];
  213. /* Divide the coefficient value by qval, ensuring proper rounding.
  214. * Since C does not specify the direction of rounding for negative
  215. * quotients, we have to force the dividend positive for portability.
  216. *
  217. * In most files, at least half of the output values will be zero
  218. * (at default quantization settings, more like three-quarters...)
  219. * so we should ensure that this case is fast. On many machines,
  220. * a comparison is enough cheaper than a divide to make a special test
  221. * a win. Since both inputs will be nonnegative, we need only test
  222. * for a < b to discover whether a/b is 0.
  223. * If your machine's division is fast enough, define FAST_DIVIDE.
  224. */
  225. #ifdef FAST_DIVIDE
  226. #define DIVIDE_BY(a,b) a /= b
  227. #else
  228. #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
  229. #endif
  230. if (temp < 0) {
  231. temp = -temp;
  232. temp += qval>>1; /* for rounding */
  233. DIVIDE_BY(temp, qval);
  234. temp = -temp;
  235. } else {
  236. temp += qval>>1; /* for rounding */
  237. DIVIDE_BY(temp, qval);
  238. }
  239. output_ptr[i] = (JCOEF) temp;
  240. }
  241. }
  242. }
  243. }
  244. #ifdef DCT_FLOAT_SUPPORTED
  245. METHODDEF(void)
  246. forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  247. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  248. JDIMENSION start_row, JDIMENSION start_col,
  249. JDIMENSION num_blocks)
  250. /* This version is used for floating-point DCT implementations. */
  251. {
  252. /* This routine is heavily used, so it's worth coding it tightly. */
  253. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  254. float_DCT_method_ptr do_dct = fdct->do_float_dct;
  255. FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  256. FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  257. JDIMENSION bi;
  258. sample_data += start_row; /* fold in the vertical offset once */
  259. for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  260. /* Load data into workspace, applying unsigned->signed conversion */
  261. { register FAST_FLOAT *workspaceptr;
  262. register JSAMPROW elemptr;
  263. register int elemr;
  264. workspaceptr = workspace;
  265. for (elemr = 0; elemr < DCTSIZE; elemr++) {
  266. elemptr = sample_data[elemr] + start_col;
  267. #if DCTSIZE == 8 /* unroll the inner loop */
  268. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  269. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  270. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  271. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  272. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  273. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  274. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  275. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  276. #else
  277. { register int elemc;
  278. for (elemc = DCTSIZE; elemc > 0; elemc--) {
  279. *workspaceptr++ = (FAST_FLOAT)
  280. (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  281. }
  282. }
  283. #endif
  284. }
  285. }
  286. /* Perform the DCT */
  287. (*do_dct) (workspace);
  288. /* Quantize/descale the coefficients, and store into coef_blocks[] */
  289. { register FAST_FLOAT temp;
  290. register int i;
  291. register JCOEFPTR output_ptr = coef_blocks[bi];
  292. for (i = 0; i < DCTSIZE2; i++) {
  293. /* Apply the quantization and scaling factor */
  294. temp = workspace[i] * divisors[i];
  295. /* Round to nearest integer.
  296. * Since C does not specify the direction of rounding for negative
  297. * quotients, we have to force the dividend positive for portability.
  298. * The maximum coefficient size is +-16K (for 12-bit data), so this
  299. * code should work for either 16-bit or 32-bit ints.
  300. */
  301. output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  302. }
  303. }
  304. }
  305. }
  306. #endif /* DCT_FLOAT_SUPPORTED */
  307. /*
  308. * Initialize FDCT manager.
  309. */
  310. GLOBAL(void)
  311. jinit_forward_dct (j_compress_ptr cinfo)
  312. {
  313. my_fdct_ptr fdct;
  314. int i;
  315. fdct = (my_fdct_ptr)
  316. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  317. SIZEOF(my_fdct_controller));
  318. cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  319. fdct->pub.start_pass = start_pass_fdctmgr;
  320. switch (cinfo->dct_method) {
  321. #ifdef DCT_ISLOW_SUPPORTED
  322. case JDCT_ISLOW:
  323. fdct->pub.forward_DCT = forward_DCT;
  324. fdct->do_dct = jpeg_fdct_islow;
  325. break;
  326. #endif
  327. #ifdef DCT_IFAST_SUPPORTED
  328. case JDCT_IFAST:
  329. fdct->pub.forward_DCT = forward_DCT;
  330. fdct->do_dct = jpeg_fdct_ifast;
  331. break;
  332. #endif
  333. #ifdef DCT_FLOAT_SUPPORTED
  334. case JDCT_FLOAT:
  335. fdct->pub.forward_DCT = forward_DCT_float;
  336. fdct->do_float_dct = jpeg_fdct_float;
  337. break;
  338. #endif
  339. default:
  340. ERREXIT(cinfo, JERR_NOT_COMPILED);
  341. break;
  342. }
  343. /* Mark divisor tables unallocated */
  344. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  345. fdct->divisors[i] = NULL;
  346. #ifdef DCT_FLOAT_SUPPORTED
  347. fdct->float_divisors[i] = NULL;
  348. #endif
  349. }
  350. }