jdinput.c 14 KB

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