jdinput.c 14 KB

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