jcdctmgr.c 12 KB

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