jcdctmgr.c 15 KB

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