jddctmgr.c 10 KB

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