jddctmgr.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * jddctmgr.c
  3. *
  4. * Copyright (C) 1994-1995, 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_scaled_size) {
  85. #ifdef IDCT_SCALING_SUPPORTED
  86. case 1:
  87. method_ptr = jpeg_idct_1x1;
  88. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  89. break;
  90. case 2:
  91. method_ptr = jpeg_idct_2x2;
  92. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  93. break;
  94. case 4:
  95. method_ptr = jpeg_idct_4x4;
  96. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  97. break;
  98. #endif
  99. case DCTSIZE:
  100. switch (cinfo->dct_method) {
  101. #ifdef DCT_ISLOW_SUPPORTED
  102. case JDCT_ISLOW:
  103. method_ptr = jpeg_idct_islow;
  104. method = JDCT_ISLOW;
  105. break;
  106. #endif
  107. #ifdef DCT_IFAST_SUPPORTED
  108. case JDCT_IFAST:
  109. method_ptr = jpeg_idct_ifast;
  110. method = JDCT_IFAST;
  111. break;
  112. #endif
  113. #ifdef DCT_FLOAT_SUPPORTED
  114. case JDCT_FLOAT:
  115. method_ptr = jpeg_idct_float;
  116. method = JDCT_FLOAT;
  117. break;
  118. #endif
  119. default:
  120. ERREXIT(cinfo, JERR_NOT_COMPILED);
  121. break;
  122. }
  123. break;
  124. default:
  125. ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
  126. break;
  127. }
  128. idct->pub.inverse_DCT[ci] = method_ptr;
  129. /* Create multiplier table from quant table.
  130. * However, we can skip this if the component is uninteresting
  131. * or if we already built the table. Also, if no quant table
  132. * has yet been saved for the component, we leave the
  133. * multiplier table all-zero; we'll be reading zeroes from the
  134. * coefficient controller's buffer anyway.
  135. */
  136. if (! compptr->component_needed || idct->cur_method[ci] == method)
  137. continue;
  138. qtbl = compptr->quant_table;
  139. if (qtbl == NULL) /* happens if no data yet for component */
  140. continue;
  141. idct->cur_method[ci] = method;
  142. switch (method) {
  143. #ifdef PROVIDE_ISLOW_TABLES
  144. case JDCT_ISLOW:
  145. {
  146. /* For LL&M IDCT method, multipliers are equal to raw quantization
  147. * coefficients, but are stored in natural order as ints.
  148. */
  149. ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  150. for (i = 0; i < DCTSIZE2; i++) {
  151. ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[jpeg_zigzag_order[i]];
  152. }
  153. }
  154. break;
  155. #endif
  156. #ifdef DCT_IFAST_SUPPORTED
  157. case JDCT_IFAST:
  158. {
  159. /* For AA&N IDCT method, multipliers are equal to quantization
  160. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  161. * scalefactor[0] = 1
  162. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  163. * For integer operation, the multiplier table is to be scaled by
  164. * IFAST_SCALE_BITS. The multipliers are stored in natural order.
  165. */
  166. IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  167. #define CONST_BITS 14
  168. static const INT16 aanscales[DCTSIZE2] = {
  169. /* precomputed values scaled up by 14 bits */
  170. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  171. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  172. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  173. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  174. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  175. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  176. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  177. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  178. };
  179. SHIFT_TEMPS
  180. for (i = 0; i < DCTSIZE2; i++) {
  181. ifmtbl[i] = (IFAST_MULT_TYPE)
  182. DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[jpeg_zigzag_order[i]],
  183. (INT32) aanscales[i]),
  184. CONST_BITS-IFAST_SCALE_BITS);
  185. }
  186. }
  187. break;
  188. #endif
  189. #ifdef DCT_FLOAT_SUPPORTED
  190. case JDCT_FLOAT:
  191. {
  192. /* For float AA&N IDCT method, multipliers are equal to quantization
  193. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  194. * scalefactor[0] = 1
  195. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  196. * The multipliers are stored in natural order.
  197. */
  198. FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  199. int row, col;
  200. static const double aanscalefactor[DCTSIZE] = {
  201. 1.0, 1.387039845, 1.306562965, 1.175875602,
  202. 1.0, 0.785694958, 0.541196100, 0.275899379
  203. };
  204. i = 0;
  205. for (row = 0; row < DCTSIZE; row++) {
  206. for (col = 0; col < DCTSIZE; col++) {
  207. fmtbl[i] = (FLOAT_MULT_TYPE)
  208. ((double) qtbl->quantval[jpeg_zigzag_order[i]] *
  209. aanscalefactor[row] * aanscalefactor[col]);
  210. i++;
  211. }
  212. }
  213. }
  214. break;
  215. #endif
  216. default:
  217. ERREXIT(cinfo, JERR_NOT_COMPILED);
  218. break;
  219. }
  220. }
  221. }
  222. /*
  223. * Initialize IDCT manager.
  224. */
  225. GLOBAL void
  226. jinit_inverse_dct (j_decompress_ptr cinfo)
  227. {
  228. my_idct_ptr idct;
  229. int ci;
  230. jpeg_component_info *compptr;
  231. idct = (my_idct_ptr)
  232. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  233. SIZEOF(my_idct_controller));
  234. cinfo->idct = (struct jpeg_inverse_dct *) idct;
  235. idct->pub.start_pass = start_pass;
  236. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  237. ci++, compptr++) {
  238. /* Allocate and pre-zero a multiplier table for each component */
  239. compptr->dct_table =
  240. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  241. SIZEOF(multiplier_table));
  242. MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
  243. /* Mark multiplier table not yet set up for any method */
  244. idct->cur_method[ci] = -1;
  245. }
  246. }