/media/libjpeg/jdinput.c

http://github.com/zpao/v8monkey · C · 471 lines · 270 code · 66 blank · 135 comment · 48 complexity · 7cbf93fdd32cda2d6e389a2a1811dc01 MD5 · raw file

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