Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

533 lines
19 KiB

  1. /*
  2. * jdmaster.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2002-2009 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 master control logic for the JPEG decompressor.
  10. * These routines are concerned with selecting the modules to be executed
  11. * and with determining the number of passes and the work to be done in each
  12. * pass.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Private state */
  18. typedef struct {
  19. struct jpeg_decomp_master pub; /* public fields */
  20. int pass_number; /* # of passes completed */
  21. boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
  22. /* Saved references to initialized quantizer modules,
  23. * in case we need to switch modes.
  24. */
  25. struct jpeg_color_quantizer * quantizer_1pass;
  26. struct jpeg_color_quantizer * quantizer_2pass;
  27. } my_decomp_master;
  28. typedef my_decomp_master * my_master_ptr;
  29. /*
  30. * Determine whether merged upsample/color conversion should be used.
  31. * CRUCIAL: this must match the actual capabilities of jdmerge.c!
  32. */
  33. LOCAL(boolean)
  34. use_merged_upsample (j_decompress_ptr cinfo)
  35. {
  36. #ifdef UPSAMPLE_MERGING_SUPPORTED
  37. /* Merging is the equivalent of plain box-filter upsampling */
  38. if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
  39. return FALSE;
  40. /* jdmerge.c only supports YCC=>RGB color conversion */
  41. if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
  42. cinfo->out_color_space != JCS_RGB ||
  43. cinfo->out_color_components != RGB_PIXELSIZE)
  44. return FALSE;
  45. /* and it only handles 2h1v or 2h2v sampling ratios */
  46. if (cinfo->comp_info[0].h_samp_factor != 2 ||
  47. cinfo->comp_info[1].h_samp_factor != 1 ||
  48. cinfo->comp_info[2].h_samp_factor != 1 ||
  49. cinfo->comp_info[0].v_samp_factor > 2 ||
  50. cinfo->comp_info[1].v_samp_factor != 1 ||
  51. cinfo->comp_info[2].v_samp_factor != 1)
  52. return FALSE;
  53. /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  54. if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
  55. cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
  56. cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
  57. cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
  58. cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
  59. cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
  60. return FALSE;
  61. /* ??? also need to test for upsample-time rescaling, when & if supported */
  62. return TRUE; /* by golly, it'll work... */
  63. #else
  64. return FALSE;
  65. #endif
  66. }
  67. /*
  68. * Compute output image dimensions and related values.
  69. * NOTE: this is exported for possible use by application.
  70. * Hence it mustn't do anything that can't be done twice.
  71. * Also note that it may be called before the master module is initialized!
  72. */
  73. GLOBAL(void)
  74. jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
  75. /* Do computations that are needed before master selection phase.
  76. * This function is used for full decompression.
  77. */
  78. {
  79. #ifdef IDCT_SCALING_SUPPORTED
  80. int ci;
  81. jpeg_component_info *compptr;
  82. #endif
  83. /* Prevent application from calling me at wrong times */
  84. if (cinfo->global_state != DSTATE_READY)
  85. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  86. /* Compute core output image dimensions and DCT scaling choices. */
  87. jpeg_core_output_dimensions(cinfo);
  88. #ifdef IDCT_SCALING_SUPPORTED
  89. /* In selecting the actual DCT scaling for each component, we try to
  90. * scale up the chroma components via IDCT scaling rather than upsampling.
  91. * This saves time if the upsampler gets to use 1:1 scaling.
  92. * Note this code adapts subsampling ratios which are powers of 2.
  93. */
  94. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  95. ci++, compptr++) {
  96. int ssize = 1;
  97. while (cinfo->min_DCT_h_scaled_size * ssize <=
  98. (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
  99. (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
  100. ssize = ssize * 2;
  101. }
  102. compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
  103. ssize = 1;
  104. while (cinfo->min_DCT_v_scaled_size * ssize <=
  105. (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
  106. (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
  107. ssize = ssize * 2;
  108. }
  109. compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
  110. /* We don't support IDCT ratios larger than 2. */
  111. if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
  112. compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
  113. else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
  114. compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
  115. }
  116. /* Recompute downsampled dimensions of components;
  117. * application needs to know these if using raw downsampled data.
  118. */
  119. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  120. ci++, compptr++) {
  121. /* Size in samples, after IDCT scaling */
  122. compptr->downsampled_width = (JDIMENSION)
  123. jdiv_round_up((long) cinfo->image_width *
  124. (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
  125. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  126. compptr->downsampled_height = (JDIMENSION)
  127. jdiv_round_up((long) cinfo->image_height *
  128. (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
  129. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  130. }
  131. #endif /* IDCT_SCALING_SUPPORTED */
  132. /* Report number of components in selected colorspace. */
  133. /* Probably this should be in the color conversion module... */
  134. switch (cinfo->out_color_space) {
  135. case JCS_GRAYSCALE:
  136. cinfo->out_color_components = 1;
  137. break;
  138. case JCS_RGB:
  139. #if RGB_PIXELSIZE != 3
  140. cinfo->out_color_components = RGB_PIXELSIZE;
  141. break;
  142. #endif /* else share code with YCbCr */
  143. case JCS_YCbCr:
  144. cinfo->out_color_components = 3;
  145. break;
  146. case JCS_CMYK:
  147. case JCS_YCCK:
  148. cinfo->out_color_components = 4;
  149. break;
  150. default: /* else must be same colorspace as in file */
  151. cinfo->out_color_components = cinfo->num_components;
  152. break;
  153. }
  154. cinfo->output_components = (cinfo->quantize_colors ? 1 :
  155. cinfo->out_color_components);
  156. /* See if upsampler will want to emit more than one row at a time */
  157. if (use_merged_upsample(cinfo))
  158. cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  159. else
  160. cinfo->rec_outbuf_height = 1;
  161. }
  162. /*
  163. * Several decompression processes need to range-limit values to the range
  164. * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  165. * due to noise introduced by quantization, roundoff error, etc. These
  166. * processes are inner loops and need to be as fast as possible. On most
  167. * machines, particularly CPUs with pipelines or instruction prefetch,
  168. * a (subscript-check-less) C table lookup
  169. * x = sample_range_limit[x];
  170. * is faster than explicit tests
  171. * if (x < 0) x = 0;
  172. * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
  173. * These processes all use a common table prepared by the routine below.
  174. *
  175. * For most steps we can mathematically guarantee that the initial value
  176. * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  177. * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
  178. * limiting step (just after the IDCT), a wildly out-of-range value is
  179. * possible if the input data is corrupt. To avoid any chance of indexing
  180. * off the end of memory and getting a bad-pointer trap, we perform the
  181. * post-IDCT limiting thus:
  182. * x = range_limit[x & MASK];
  183. * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  184. * samples. Under normal circumstances this is more than enough range and
  185. * a correct output will be generated; with bogus input data the mask will
  186. * cause wraparound, and we will safely generate a bogus-but-in-range output.
  187. * For the post-IDCT step, we want to convert the data from signed to unsigned
  188. * representation by adding CENTERJSAMPLE at the same time that we limit it.
  189. * So the post-IDCT limiting table ends up looking like this:
  190. * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  191. * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  192. * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  193. * 0,1,...,CENTERJSAMPLE-1
  194. * Negative inputs select values from the upper half of the table after
  195. * masking.
  196. *
  197. * We can save some space by overlapping the start of the post-IDCT table
  198. * with the simpler range limiting table. The post-IDCT table begins at
  199. * sample_range_limit + CENTERJSAMPLE.
  200. *
  201. * Note that the table is allocated in near data space on PCs; it's small
  202. * enough and used often enough to justify this.
  203. */
  204. LOCAL(void)
  205. prepare_range_limit_table (j_decompress_ptr cinfo)
  206. /* Allocate and fill in the sample_range_limit table */
  207. {
  208. JSAMPLE * table;
  209. int i;
  210. table = (JSAMPLE *)
  211. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  212. (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  213. table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
  214. cinfo->sample_range_limit = table;
  215. /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  216. MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  217. /* Main part of "simple" table: limit[x] = x */
  218. for (i = 0; i <= MAXJSAMPLE; i++)
  219. table[i] = (JSAMPLE) i;
  220. table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
  221. /* End of simple table, rest of first half of post-IDCT table */
  222. for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  223. table[i] = MAXJSAMPLE;
  224. /* Second half of post-IDCT table */
  225. MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  226. (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  227. MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  228. cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  229. }
  230. /*
  231. * Master selection of decompression modules.
  232. * This is done once at jpeg_start_decompress time. We determine
  233. * which modules will be used and give them appropriate initialization calls.
  234. * We also initialize the decompressor input side to begin consuming data.
  235. *
  236. * Since jpeg_read_header has finished, we know what is in the SOF
  237. * and (first) SOS markers. We also have all the application parameter
  238. * settings.
  239. */
  240. LOCAL(void)
  241. master_selection (j_decompress_ptr cinfo)
  242. {
  243. my_master_ptr master = (my_master_ptr) cinfo->master;
  244. boolean use_c_buffer;
  245. long samplesperrow;
  246. JDIMENSION jd_samplesperrow;
  247. /* Initialize dimensions and other stuff */
  248. jpeg_calc_output_dimensions(cinfo);
  249. prepare_range_limit_table(cinfo);
  250. /* Width of an output scanline must be representable as JDIMENSION. */
  251. samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  252. jd_samplesperrow = (JDIMENSION) samplesperrow;
  253. if ((long) jd_samplesperrow != samplesperrow)
  254. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  255. /* Initialize my private state */
  256. master->pass_number = 0;
  257. master->using_merged_upsample = use_merged_upsample(cinfo);
  258. /* Color quantizer selection */
  259. master->quantizer_1pass = NULL;
  260. master->quantizer_2pass = NULL;
  261. /* No mode changes if not using buffered-image mode. */
  262. if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
  263. cinfo->enable_1pass_quant = FALSE;
  264. cinfo->enable_external_quant = FALSE;
  265. cinfo->enable_2pass_quant = FALSE;
  266. }
  267. if (cinfo->quantize_colors) {
  268. if (cinfo->raw_data_out)
  269. ERREXIT(cinfo, JERR_NOTIMPL);
  270. /* 2-pass quantizer only works in 3-component color space. */
  271. if (cinfo->out_color_components != 3) {
  272. cinfo->enable_1pass_quant = TRUE;
  273. cinfo->enable_external_quant = FALSE;
  274. cinfo->enable_2pass_quant = FALSE;
  275. cinfo->colormap = NULL;
  276. } else if (cinfo->colormap != NULL) {
  277. cinfo->enable_external_quant = TRUE;
  278. } else if (cinfo->two_pass_quantize) {
  279. cinfo->enable_2pass_quant = TRUE;
  280. } else {
  281. cinfo->enable_1pass_quant = TRUE;
  282. }
  283. if (cinfo->enable_1pass_quant) {
  284. #ifdef QUANT_1PASS_SUPPORTED
  285. jinit_1pass_quantizer(cinfo);
  286. master->quantizer_1pass = cinfo->cquantize;
  287. #else
  288. ERREXIT(cinfo, JERR_NOT_COMPILED);
  289. #endif
  290. }
  291. /* We use the 2-pass code to map to external colormaps. */
  292. if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  293. #ifdef QUANT_2PASS_SUPPORTED
  294. jinit_2pass_quantizer(cinfo);
  295. master->quantizer_2pass = cinfo->cquantize;
  296. #else
  297. ERREXIT(cinfo, JERR_NOT_COMPILED);
  298. #endif
  299. }
  300. /* If both quantizers are initialized, the 2-pass one is left active;
  301. * this is necessary for starting with quantization to an external map.
  302. */
  303. }
  304. /* Post-processing: in particular, color conversion first */
  305. if (! cinfo->raw_data_out) {
  306. if (master->using_merged_upsample) {
  307. #ifdef UPSAMPLE_MERGING_SUPPORTED
  308. jinit_merged_upsampler(cinfo); /* does color conversion too */
  309. #else
  310. ERREXIT(cinfo, JERR_NOT_COMPILED);
  311. #endif
  312. } else {
  313. jinit_color_deconverter(cinfo);
  314. jinit_upsampler(cinfo);
  315. }
  316. jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  317. }
  318. /* Inverse DCT */
  319. jinit_inverse_dct(cinfo);
  320. /* Entropy decoding: either Huffman or arithmetic coding. */
  321. if (cinfo->arith_code)
  322. jinit_arith_decoder(cinfo);
  323. else {
  324. jinit_huff_decoder(cinfo);
  325. }
  326. /* Initialize principal buffer controllers. */
  327. use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  328. jinit_d_coef_controller(cinfo, use_c_buffer);
  329. if (! cinfo->raw_data_out)
  330. jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  331. /* We can now tell the memory manager to allocate virtual arrays. */
  332. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  333. /* Initialize input side of decompressor to consume first scan. */
  334. (*cinfo->inputctl->start_input_pass) (cinfo);
  335. #ifdef D_MULTISCAN_FILES_SUPPORTED
  336. /* If jpeg_start_decompress will read the whole file, initialize
  337. * progress monitoring appropriately. The input step is counted
  338. * as one pass.
  339. */
  340. if (cinfo->progress != NULL && ! cinfo->buffered_image &&
  341. cinfo->inputctl->has_multiple_scans) {
  342. int nscans;
  343. /* Estimate number of scans to set pass_limit. */
  344. if (cinfo->progressive_mode) {
  345. /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  346. nscans = 2 + 3 * cinfo->num_components;
  347. } else {
  348. /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  349. nscans = cinfo->num_components;
  350. }
  351. cinfo->progress->pass_counter = 0L;
  352. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  353. cinfo->progress->completed_passes = 0;
  354. cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  355. /* Count the input pass as done */
  356. master->pass_number++;
  357. }
  358. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  359. }
  360. /*
  361. * Per-pass setup.
  362. * This is called at the beginning of each output pass. We determine which
  363. * modules will be active during this pass and give them appropriate
  364. * start_pass calls. We also set is_dummy_pass to indicate whether this
  365. * is a "real" output pass or a dummy pass for color quantization.
  366. * (In the latter case, jdapistd.c will crank the pass to completion.)
  367. */
  368. METHODDEF(void)
  369. prepare_for_output_pass (j_decompress_ptr cinfo)
  370. {
  371. my_master_ptr master = (my_master_ptr) cinfo->master;
  372. if (master->pub.is_dummy_pass) {
  373. #ifdef QUANT_2PASS_SUPPORTED
  374. /* Final pass of 2-pass quantization */
  375. master->pub.is_dummy_pass = FALSE;
  376. (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  377. (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  378. (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  379. #else
  380. ERREXIT(cinfo, JERR_NOT_COMPILED);
  381. #endif /* QUANT_2PASS_SUPPORTED */
  382. } else {
  383. if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  384. /* Select new quantization method */
  385. if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  386. cinfo->cquantize = master->quantizer_2pass;
  387. master->pub.is_dummy_pass = TRUE;
  388. } else if (cinfo->enable_1pass_quant) {
  389. cinfo->cquantize = master->quantizer_1pass;
  390. } else {
  391. ERREXIT(cinfo, JERR_MODE_CHANGE);
  392. }
  393. }
  394. (*cinfo->idct->start_pass) (cinfo);
  395. (*cinfo->coef->start_output_pass) (cinfo);
  396. if (! cinfo->raw_data_out) {
  397. if (! master->using_merged_upsample)
  398. (*cinfo->cconvert->start_pass) (cinfo);
  399. (*cinfo->upsample->start_pass) (cinfo);
  400. if (cinfo->quantize_colors)
  401. (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  402. (*cinfo->post->start_pass) (cinfo,
  403. (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  404. (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  405. }
  406. }
  407. /* Set up progress monitor's pass info if present */
  408. if (cinfo->progress != NULL) {
  409. cinfo->progress->completed_passes = master->pass_number;
  410. cinfo->progress->total_passes = master->pass_number +
  411. (master->pub.is_dummy_pass ? 2 : 1);
  412. /* In buffered-image mode, we assume one more output pass if EOI not
  413. * yet reached, but no more passes if EOI has been reached.
  414. */
  415. if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
  416. cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  417. }
  418. }
  419. }
  420. /*
  421. * Finish up at end of an output pass.
  422. */
  423. METHODDEF(void)
  424. finish_output_pass (j_decompress_ptr cinfo)
  425. {
  426. my_master_ptr master = (my_master_ptr) cinfo->master;
  427. if (cinfo->quantize_colors)
  428. (*cinfo->cquantize->finish_pass) (cinfo);
  429. master->pass_number++;
  430. }
  431. #ifdef D_MULTISCAN_FILES_SUPPORTED
  432. /*
  433. * Switch to a new external colormap between output passes.
  434. */
  435. GLOBAL(void)
  436. jpeg_new_colormap (j_decompress_ptr cinfo)
  437. {
  438. my_master_ptr master = (my_master_ptr) cinfo->master;
  439. /* Prevent application from calling me at wrong times */
  440. if (cinfo->global_state != DSTATE_BUFIMAGE)
  441. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  442. if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  443. cinfo->colormap != NULL) {
  444. /* Select 2-pass quantizer for external colormap use */
  445. cinfo->cquantize = master->quantizer_2pass;
  446. /* Notify quantizer of colormap change */
  447. (*cinfo->cquantize->new_color_map) (cinfo);
  448. master->pub.is_dummy_pass = FALSE; /* just in case */
  449. } else
  450. ERREXIT(cinfo, JERR_MODE_CHANGE);
  451. }
  452. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  453. /*
  454. * Initialize master decompression control and select active modules.
  455. * This is performed at the start of jpeg_start_decompress.
  456. */
  457. GLOBAL(void)
  458. jinit_master_decompress (j_decompress_ptr cinfo)
  459. {
  460. my_master_ptr master;
  461. master = (my_master_ptr)
  462. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  463. SIZEOF(my_decomp_master));
  464. cinfo->master = (struct jpeg_decomp_master *) master;
  465. master->pub.prepare_for_output_pass = prepare_for_output_pass;
  466. master->pub.finish_output_pass = finish_output_pass;
  467. master->pub.is_dummy_pass = FALSE;
  468. master_selection(cinfo);
  469. }