jddctmgr.c 11 KB

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