jdinput.c 14 KB

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