jddctmgr.c 12 KB

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