jddctmgr.c 12 KB

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