jcdctmgr.c 15 KB

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