Team Fortress 2 Source Code as on 22/4/2020
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.

482 lines
15 KiB

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