jdinput.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * jdinput.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 input control logic for the JPEG decompressor.
  10. * These routines are concerned with controlling the decompressor's input
  11. * processing (marker reading and coefficient decoding). The actual input
  12. * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Private state */
  18. typedef struct {
  19. struct jpeg_input_controller pub; /* public fields */
  20. boolean inheaders; /* TRUE until first SOS is reached */
  21. } my_input_controller;
  22. typedef my_input_controller * my_inputctl_ptr;
  23. /* Forward declarations */
  24. METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
  25. /*
  26. * Routines to calculate various quantities related to the size of the image.
  27. */
  28. LOCAL(void)
  29. initial_setup (j_decompress_ptr cinfo)
  30. /* Called once, when first SOS marker is reached */
  31. {
  32. int ci;
  33. jpeg_component_info *compptr;
  34. /* Make sure image isn't bigger than I can handle */
  35. if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  36. (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  37. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  38. /* For now, precision must match compiled-in value... */
  39. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  40. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  41. /* Check that number of components won't exceed internal array sizes */
  42. if (cinfo->num_components > MAX_COMPONENTS)
  43. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  44. MAX_COMPONENTS);
  45. /* Compute maximum sampling factors; check factor validity */
  46. cinfo->max_h_samp_factor = 1;
  47. cinfo->max_v_samp_factor = 1;
  48. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  49. ci++, compptr++) {
  50. if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  51. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  52. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  53. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  54. compptr->h_samp_factor);
  55. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  56. compptr->v_samp_factor);
  57. }
  58. /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
  59. * In the full decompressor, this will be overridden by jdmaster.c;
  60. * but in the transcoder, jdmaster.c is not used, so we must do it here.
  61. */
  62. cinfo->min_DCT_h_scaled_size = DCTSIZE;
  63. cinfo->min_DCT_v_scaled_size = DCTSIZE;
  64. /* Compute dimensions of components */
  65. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  66. ci++, compptr++) {
  67. compptr->DCT_h_scaled_size = DCTSIZE;
  68. compptr->DCT_v_scaled_size = DCTSIZE;
  69. /* Size in DCT blocks */
  70. compptr->width_in_blocks = (JDIMENSION)
  71. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  72. (long) (cinfo->max_h_samp_factor * DCTSIZE));
  73. compptr->height_in_blocks = (JDIMENSION)
  74. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  75. (long) (cinfo->max_v_samp_factor * DCTSIZE));
  76. /* downsampled_width and downsampled_height will also be overridden by
  77. * jdmaster.c if we are doing full decompression. The transcoder library
  78. * doesn't use these values, but the calling application might.
  79. */
  80. /* Size in samples */
  81. compptr->downsampled_width = (JDIMENSION)
  82. jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  83. (long) cinfo->max_h_samp_factor);
  84. compptr->downsampled_height = (JDIMENSION)
  85. jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  86. (long) cinfo->max_v_samp_factor);
  87. /* Mark component needed, until color conversion says otherwise */
  88. compptr->component_needed = TRUE;
  89. /* Mark no quantization table yet saved for component */
  90. compptr->quant_table = NULL;
  91. }
  92. /* Compute number of fully interleaved MCU rows. */
  93. cinfo->total_iMCU_rows = (JDIMENSION)
  94. jdiv_round_up((long) cinfo->image_height,
  95. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  96. /* Decide whether file contains multiple scans */
  97. if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  98. cinfo->inputctl->has_multiple_scans = TRUE;
  99. else
  100. cinfo->inputctl->has_multiple_scans = FALSE;
  101. }
  102. LOCAL(void)
  103. per_scan_setup (j_decompress_ptr cinfo)
  104. /* Do computations that are needed before processing a JPEG scan */
  105. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  106. {
  107. int ci, mcublks, tmp;
  108. jpeg_component_info *compptr;
  109. if (cinfo->comps_in_scan == 1) {
  110. /* Noninterleaved (single-component) scan */
  111. compptr = cinfo->cur_comp_info[0];
  112. /* Overall image size in MCUs */
  113. cinfo->MCUs_per_row = compptr->width_in_blocks;
  114. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  115. /* For noninterleaved scan, always one block per MCU */
  116. compptr->MCU_width = 1;
  117. compptr->MCU_height = 1;
  118. compptr->MCU_blocks = 1;
  119. compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
  120. compptr->last_col_width = 1;
  121. /* For noninterleaved scans, it is convenient to define last_row_height
  122. * as the number of block rows present in the last iMCU row.
  123. */
  124. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  125. if (tmp == 0) tmp = compptr->v_samp_factor;
  126. compptr->last_row_height = tmp;
  127. /* Prepare array describing MCU composition */
  128. cinfo->blocks_in_MCU = 1;
  129. cinfo->MCU_membership[0] = 0;
  130. } else {
  131. /* Interleaved (multi-component) scan */
  132. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  133. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  134. MAX_COMPS_IN_SCAN);
  135. /* Overall image size in MCUs */
  136. cinfo->MCUs_per_row = (JDIMENSION)
  137. jdiv_round_up((long) cinfo->image_width,
  138. (long) (cinfo->max_h_samp_factor*DCTSIZE));
  139. cinfo->MCU_rows_in_scan = (JDIMENSION)
  140. jdiv_round_up((long) cinfo->image_height,
  141. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  142. cinfo->blocks_in_MCU = 0;
  143. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  144. compptr = cinfo->cur_comp_info[ci];
  145. /* Sampling factors give # of blocks of component in each MCU */
  146. compptr->MCU_width = compptr->h_samp_factor;
  147. compptr->MCU_height = compptr->v_samp_factor;
  148. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  149. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
  150. /* Figure number of non-dummy blocks in last MCU column & row */
  151. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  152. if (tmp == 0) tmp = compptr->MCU_width;
  153. compptr->last_col_width = tmp;
  154. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  155. if (tmp == 0) tmp = compptr->MCU_height;
  156. compptr->last_row_height = tmp;
  157. /* Prepare array describing MCU composition */
  158. mcublks = compptr->MCU_blocks;
  159. if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  160. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  161. while (mcublks-- > 0) {
  162. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  163. }
  164. }
  165. }
  166. }
  167. /*
  168. * Save away a copy of the Q-table referenced by each component present
  169. * in the current scan, unless already saved during a prior scan.
  170. *
  171. * In a multiple-scan JPEG file, the encoder could assign different components
  172. * the same Q-table slot number, but change table definitions between scans
  173. * so that each component uses a different Q-table. (The IJG encoder is not
  174. * currently capable of doing this, but other encoders might.) Since we want
  175. * to be able to dequantize all the components at the end of the file, this
  176. * means that we have to save away the table actually used for each component.
  177. * We do this by copying the table at the start of the first scan containing
  178. * the component.
  179. * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  180. * slot between scans of a component using that slot. If the encoder does so
  181. * anyway, this decoder will simply use the Q-table values that were current
  182. * at the start of the first scan for the component.
  183. *
  184. * The decompressor output side looks only at the saved quant tables,
  185. * not at the current Q-table slots.
  186. */
  187. LOCAL(void)
  188. latch_quant_tables (j_decompress_ptr cinfo)
  189. {
  190. int ci, qtblno;
  191. jpeg_component_info *compptr;
  192. JQUANT_TBL * qtbl;
  193. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  194. compptr = cinfo->cur_comp_info[ci];
  195. /* No work if we already saved Q-table for this component */
  196. if (compptr->quant_table != NULL)
  197. continue;
  198. /* Make sure specified quantization table is present */
  199. qtblno = compptr->quant_tbl_no;
  200. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  201. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  202. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  203. /* OK, save away the quantization table */
  204. qtbl = (JQUANT_TBL *)
  205. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  206. SIZEOF(JQUANT_TBL));
  207. MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  208. compptr->quant_table = qtbl;
  209. }
  210. }
  211. /*
  212. * Initialize the input modules to read a scan of compressed data.
  213. * The first call to this is done by jdmaster.c after initializing
  214. * the entire decompressor (during jpeg_start_decompress).
  215. * Subsequent calls come from consume_markers, below.
  216. */
  217. METHODDEF(void)
  218. start_input_pass (j_decompress_ptr cinfo)
  219. {
  220. per_scan_setup(cinfo);
  221. latch_quant_tables(cinfo);
  222. (*cinfo->entropy->start_pass) (cinfo);
  223. (*cinfo->coef->start_input_pass) (cinfo);
  224. cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  225. }
  226. /*
  227. * Finish up after inputting a compressed-data scan.
  228. * This is called by the coefficient controller after it's read all
  229. * the expected data of the scan.
  230. */
  231. METHODDEF(void)
  232. finish_input_pass (j_decompress_ptr cinfo)
  233. {
  234. cinfo->inputctl->consume_input = consume_markers;
  235. }
  236. /*
  237. * Read JPEG markers before, between, or after compressed-data scans.
  238. * Change state as necessary when a new scan is reached.
  239. * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  240. *
  241. * The consume_input method pointer points either here or to the
  242. * coefficient controller's consume_data routine, depending on whether
  243. * we are reading a compressed data segment or inter-segment markers.
  244. */
  245. METHODDEF(int)
  246. consume_markers (j_decompress_ptr cinfo)
  247. {
  248. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  249. int val;
  250. if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  251. return JPEG_REACHED_EOI;
  252. val = (*cinfo->marker->read_markers) (cinfo);
  253. switch (val) {
  254. case JPEG_REACHED_SOS: /* Found SOS */
  255. if (inputctl->inheaders) { /* 1st SOS */
  256. initial_setup(cinfo);
  257. inputctl->inheaders = FALSE;
  258. /* Note: start_input_pass must be called by jdmaster.c
  259. * before any more input can be consumed. jdapimin.c is
  260. * responsible for enforcing this sequencing.
  261. */
  262. } else { /* 2nd or later SOS marker */
  263. if (! inputctl->pub.has_multiple_scans)
  264. ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  265. start_input_pass(cinfo);
  266. }
  267. break;
  268. case JPEG_REACHED_EOI: /* Found EOI */
  269. inputctl->pub.eoi_reached = TRUE;
  270. if (inputctl->inheaders) { /* Tables-only datastream, apparently */
  271. if (cinfo->marker->saw_SOF)
  272. ERREXIT(cinfo, JERR_SOF_NO_SOS);
  273. } else {
  274. /* Prevent infinite loop in coef ctlr's decompress_data routine
  275. * if user set output_scan_number larger than number of scans.
  276. */
  277. if (cinfo->output_scan_number > cinfo->input_scan_number)
  278. cinfo->output_scan_number = cinfo->input_scan_number;
  279. }
  280. break;
  281. case JPEG_SUSPENDED:
  282. break;
  283. }
  284. return val;
  285. }
  286. /*
  287. * Reset state to begin a fresh datastream.
  288. */
  289. METHODDEF(void)
  290. reset_input_controller (j_decompress_ptr cinfo)
  291. {
  292. my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  293. inputctl->pub.consume_input = consume_markers;
  294. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  295. inputctl->pub.eoi_reached = FALSE;
  296. inputctl->inheaders = TRUE;
  297. /* Reset other modules */
  298. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  299. (*cinfo->marker->reset_marker_reader) (cinfo);
  300. /* Reset progression state -- would be cleaner if entropy decoder did this */
  301. cinfo->coef_bits = NULL;
  302. }
  303. /*
  304. * Initialize the input controller module.
  305. * This is called only once, when the decompression object is created.
  306. */
  307. GLOBAL(void)
  308. jinit_input_controller (j_decompress_ptr cinfo)
  309. {
  310. my_inputctl_ptr inputctl;
  311. /* Create subobject in permanent pool */
  312. inputctl = (my_inputctl_ptr)
  313. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  314. SIZEOF(my_input_controller));
  315. cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  316. /* Initialize method pointers */
  317. inputctl->pub.consume_input = consume_markers;
  318. inputctl->pub.reset_input_controller = reset_input_controller;
  319. inputctl->pub.start_input_pass = start_input_pass;
  320. inputctl->pub.finish_input_pass = finish_input_pass;
  321. /* Initialize state: can't use reset_input_controller since we don't
  322. * want to try to reset other modules yet.
  323. */
  324. inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  325. inputctl->pub.eoi_reached = FALSE;
  326. inputctl->inheaders = TRUE;
  327. }