jddctmgr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * jddctmgr.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 inverse-DCT management logic.
  9. * This code selects a particular IDCT implementation to be used,
  10. * and it performs related housekeeping chores. No code in this file
  11. * is executed per IDCT step, only during output pass setup.
  12. *
  13. * Note that the IDCT routines are responsible for performing coefficient
  14. * dequantization as well as the IDCT proper. This module sets up the
  15. * dequantization multiplier table needed by the IDCT routine.
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. #include "jdct.h" /* Private declarations for DCT subsystem */
  21. /*
  22. * The decompressor input side (jdinput.c) saves away the appropriate
  23. * quantization table for each component at the start of the first scan
  24. * involving that component. (This is necessary in order to correctly
  25. * decode files that reuse Q-table slots.)
  26. * When we are ready to make an output pass, the saved Q-table is converted
  27. * to a multiplier table that will actually be used by the IDCT routine.
  28. * The multiplier table contents are IDCT-method-dependent. To support
  29. * application changes in IDCT method between scans, we can remake the
  30. * multiplier tables if necessary.
  31. * In buffered-image mode, the first output pass may occur before any data
  32. * has been seen for some components, and thus before their Q-tables have
  33. * been saved away. To handle this case, multiplier tables are preset
  34. * to zeroes; the result of the IDCT will be a neutral gray level.
  35. */
  36. /* Private subobject for this module */
  37. typedef struct {
  38. struct jpeg_inverse_dct pub; /* public fields */
  39. /* This array contains the IDCT method code that each multiplier table
  40. * is currently set up for, or -1 if it's not yet set up.
  41. * The actual multiplier tables are pointed to by dct_table in the
  42. * per-component comp_info structures.
  43. */
  44. int cur_method[MAX_COMPONENTS];
  45. } my_idct_controller;
  46. typedef my_idct_controller * my_idct_ptr;
  47. /* Allocated multiplier tables: big enough for any supported variant */
  48. typedef union {
  49. ISLOW_MULT_TYPE islow_array[DCTSIZE2];
  50. #ifdef DCT_IFAST_SUPPORTED
  51. IFAST_MULT_TYPE ifast_array[DCTSIZE2];
  52. #endif
  53. #ifdef DCT_FLOAT_SUPPORTED
  54. FLOAT_MULT_TYPE float_array[DCTSIZE2];
  55. #endif
  56. } multiplier_table;
  57. /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
  58. * so be sure to compile that code if either ISLOW or SCALING is requested.
  59. */
  60. #ifdef DCT_ISLOW_SUPPORTED
  61. #define PROVIDE_ISLOW_TABLES
  62. #else
  63. #ifdef IDCT_SCALING_SUPPORTED
  64. #define PROVIDE_ISLOW_TABLES
  65. #endif
  66. #endif
  67. /*
  68. * Prepare for an output pass.
  69. * Here we select the proper IDCT routine for each component and build
  70. * a matching multiplier table.
  71. */
  72. METHODDEF(void)
  73. start_pass (j_decompress_ptr cinfo)
  74. {
  75. my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
  76. int ci, i;
  77. jpeg_component_info *compptr;
  78. int method = 0;
  79. inverse_DCT_method_ptr method_ptr = NULL;
  80. JQUANT_TBL * qtbl;
  81. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  82. ci++, compptr++) {
  83. /* Select the proper IDCT routine for this component's scaling */
  84. switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
  85. #ifdef IDCT_SCALING_SUPPORTED
  86. case ((1 << 8) + 1):
  87. method_ptr = jpeg_idct_1x1;
  88. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  89. break;
  90. case ((2 << 8) + 2):
  91. method_ptr = jpeg_idct_2x2;
  92. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  93. break;
  94. case ((3 << 8) + 3):
  95. method_ptr = jpeg_idct_3x3;
  96. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  97. break;
  98. case ((4 << 8) + 4):
  99. method_ptr = jpeg_idct_4x4;
  100. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  101. break;
  102. case ((5 << 8) + 5):
  103. method_ptr = jpeg_idct_5x5;
  104. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  105. break;
  106. case ((6 << 8) + 6):
  107. method_ptr = jpeg_idct_6x6;
  108. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  109. break;
  110. case ((7 << 8) + 7):
  111. method_ptr = jpeg_idct_7x7;
  112. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  113. break;
  114. case ((9 << 8) + 9):
  115. method_ptr = jpeg_idct_9x9;
  116. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  117. break;
  118. case ((10 << 8) + 10):
  119. method_ptr = jpeg_idct_10x10;
  120. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  121. break;
  122. case ((11 << 8) + 11):
  123. method_ptr = jpeg_idct_11x11;
  124. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  125. break;
  126. case ((12 << 8) + 12):
  127. method_ptr = jpeg_idct_12x12;
  128. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  129. break;
  130. case ((13 << 8) + 13):
  131. method_ptr = jpeg_idct_13x13;
  132. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  133. break;
  134. case ((14 << 8) + 14):
  135. method_ptr = jpeg_idct_14x14;
  136. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  137. break;
  138. case ((15 << 8) + 15):
  139. method_ptr = jpeg_idct_15x15;
  140. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  141. break;
  142. case ((16 << 8) + 16):
  143. method_ptr = jpeg_idct_16x16;
  144. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  145. break;
  146. case ((16 << 8) + 8):
  147. method_ptr = jpeg_idct_16x8;
  148. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  149. break;
  150. case ((14 << 8) + 7):
  151. method_ptr = jpeg_idct_14x7;
  152. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  153. break;
  154. case ((12 << 8) + 6):
  155. method_ptr = jpeg_idct_12x6;
  156. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  157. break;
  158. case ((10 << 8) + 5):
  159. method_ptr = jpeg_idct_10x5;
  160. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  161. break;
  162. case ((8 << 8) + 4):
  163. method_ptr = jpeg_idct_8x4;
  164. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  165. break;
  166. case ((6 << 8) + 3):
  167. method_ptr = jpeg_idct_6x3;
  168. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  169. break;
  170. case ((4 << 8) + 2):
  171. method_ptr = jpeg_idct_4x2;
  172. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  173. break;
  174. case ((2 << 8) + 1):
  175. method_ptr = jpeg_idct_2x1;
  176. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  177. break;
  178. case ((8 << 8) + 16):
  179. method_ptr = jpeg_idct_8x16;
  180. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  181. break;
  182. case ((7 << 8) + 14):
  183. method_ptr = jpeg_idct_7x14;
  184. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  185. break;
  186. case ((6 << 8) + 12):
  187. method_ptr = jpeg_idct_6x12;
  188. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  189. break;
  190. case ((5 << 8) + 10):
  191. method_ptr = jpeg_idct_5x10;
  192. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  193. break;
  194. case ((4 << 8) + 8):
  195. method_ptr = jpeg_idct_4x8;
  196. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  197. break;
  198. case ((3 << 8) + 6):
  199. method_ptr = jpeg_idct_3x6;
  200. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  201. break;
  202. case ((2 << 8) + 4):
  203. method_ptr = jpeg_idct_2x4;
  204. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  205. break;
  206. case ((1 << 8) + 2):
  207. method_ptr = jpeg_idct_1x2;
  208. method = JDCT_ISLOW; /* jidctint uses islow-style table */
  209. break;
  210. #endif
  211. case ((DCTSIZE << 8) + DCTSIZE):
  212. switch (cinfo->dct_method) {
  213. #ifdef DCT_ISLOW_SUPPORTED
  214. case JDCT_ISLOW:
  215. method_ptr = jpeg_idct_islow;
  216. method = JDCT_ISLOW;
  217. break;
  218. #endif
  219. #ifdef DCT_IFAST_SUPPORTED
  220. case JDCT_IFAST:
  221. method_ptr = jpeg_idct_ifast;
  222. method = JDCT_IFAST;
  223. break;
  224. #endif
  225. #ifdef DCT_FLOAT_SUPPORTED
  226. case JDCT_FLOAT:
  227. method_ptr = jpeg_idct_float;
  228. method = JDCT_FLOAT;
  229. break;
  230. #endif
  231. default:
  232. ERREXIT(cinfo, JERR_NOT_COMPILED);
  233. break;
  234. }
  235. break;
  236. default:
  237. ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
  238. compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
  239. break;
  240. }
  241. idct->pub.inverse_DCT[ci] = method_ptr;
  242. /* Create multiplier table from quant table.
  243. * However, we can skip this if the component is uninteresting
  244. * or if we already built the table. Also, if no quant table
  245. * has yet been saved for the component, we leave the
  246. * multiplier table all-zero; we'll be reading zeroes from the
  247. * coefficient controller's buffer anyway.
  248. */
  249. if (! compptr->component_needed || idct->cur_method[ci] == method)
  250. continue;
  251. qtbl = compptr->quant_table;
  252. if (qtbl == NULL) /* happens if no data yet for component */
  253. continue;
  254. idct->cur_method[ci] = method;
  255. switch (method) {
  256. #ifdef PROVIDE_ISLOW_TABLES
  257. case JDCT_ISLOW:
  258. {
  259. /* For LL&M IDCT method, multipliers are equal to raw quantization
  260. * coefficients, but are stored as ints to ensure access efficiency.
  261. */
  262. ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  263. for (i = 0; i < DCTSIZE2; i++) {
  264. ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
  265. }
  266. }
  267. break;
  268. #endif
  269. #ifdef DCT_IFAST_SUPPORTED
  270. case JDCT_IFAST:
  271. {
  272. /* For AA&N IDCT method, multipliers are equal to quantization
  273. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  274. * scalefactor[0] = 1
  275. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  276. * For integer operation, the multiplier table is to be scaled by
  277. * IFAST_SCALE_BITS.
  278. */
  279. IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  280. #define CONST_BITS 14
  281. static const INT16 aanscales[DCTSIZE2] = {
  282. /* precomputed values scaled up by 14 bits */
  283. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  284. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  285. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  286. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  287. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  288. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  289. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  290. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  291. };
  292. SHIFT_TEMPS
  293. for (i = 0; i < DCTSIZE2; i++) {
  294. ifmtbl[i] = (IFAST_MULT_TYPE)
  295. DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  296. (INT32) aanscales[i]),
  297. CONST_BITS-IFAST_SCALE_BITS);
  298. }
  299. }
  300. break;
  301. #endif
  302. #ifdef DCT_FLOAT_SUPPORTED
  303. case JDCT_FLOAT:
  304. {
  305. /* For float AA&N IDCT method, multipliers are equal to quantization
  306. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  307. * scalefactor[0] = 1
  308. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  309. */
  310. FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  311. int row, col;
  312. static const double aanscalefactor[DCTSIZE] = {
  313. 1.0, 1.387039845, 1.306562965, 1.175875602,
  314. 1.0, 0.785694958, 0.541196100, 0.275899379
  315. };
  316. i = 0;
  317. for (row = 0; row < DCTSIZE; row++) {
  318. for (col = 0; col < DCTSIZE; col++) {
  319. fmtbl[i] = (FLOAT_MULT_TYPE)
  320. ((double) qtbl->quantval[i] *
  321. aanscalefactor[row] * aanscalefactor[col]);
  322. i++;
  323. }
  324. }
  325. }
  326. break;
  327. #endif
  328. default:
  329. ERREXIT(cinfo, JERR_NOT_COMPILED);
  330. break;
  331. }
  332. }
  333. }
  334. /*
  335. * Initialize IDCT manager.
  336. */
  337. GLOBAL(void)
  338. jinit_inverse_dct (j_decompress_ptr cinfo)
  339. {
  340. my_idct_ptr idct;
  341. int ci;
  342. jpeg_component_info *compptr;
  343. idct = (my_idct_ptr)
  344. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  345. SIZEOF(my_idct_controller));
  346. cinfo->idct = (struct jpeg_inverse_dct *) idct;
  347. idct->pub.start_pass = start_pass;
  348. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  349. ci++, compptr++) {
  350. /* Allocate and pre-zero a multiplier table for each component */
  351. compptr->dct_table =
  352. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  353. SIZEOF(multiplier_table));
  354. MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
  355. /* Mark multiplier table not yet set up for any method */
  356. idct->cur_method[ci] = -1;
  357. }
  358. }