Audio plugin host https://kx.studio/carla
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.

jdinput.c 14KB

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