Team Fortress 2 Source Code as on 22/4/2020
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.

453 lines
16 KiB

  1. /*
  2. * jccoefct.c
  3. *
  4. * Copyright (C) 1994-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 the coefficient buffer controller for compression.
  9. * This controller is the top level of the JPEG compressor proper.
  10. * The coefficient buffer lies between forward-DCT and entropy encoding steps.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /* We use a full-image coefficient buffer when doing Huffman optimization,
  16. * and also for writing multiple-scan JPEG files. In all cases, the DCT
  17. * step is run during the first pass, and subsequent passes need only read
  18. * the buffered coefficients.
  19. */
  20. #ifdef ENTROPY_OPT_SUPPORTED
  21. #define FULL_COEF_BUFFER_SUPPORTED
  22. #else
  23. #ifdef C_MULTISCAN_FILES_SUPPORTED
  24. #define FULL_COEF_BUFFER_SUPPORTED
  25. #endif
  26. #endif
  27. /* Private buffer controller object */
  28. typedef struct {
  29. struct jpeg_c_coef_controller pub; /* public fields */
  30. JDIMENSION iMCU_row_num; /* iMCU row # within image */
  31. JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
  32. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  33. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  34. /* For single-pass compression, it's sufficient to buffer just one MCU
  35. * (although this may prove a bit slow in practice). We allocate a
  36. * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
  37. * MCU constructed and sent. (On 80x86, the workspace is FAR even though
  38. * it's not really very big; this is to keep the module interfaces unchanged
  39. * when a large coefficient buffer is necessary.)
  40. * In multi-pass modes, this array points to the current MCU's blocks
  41. * within the virtual arrays.
  42. */
  43. JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  44. /* In multi-pass modes, we need a virtual block array for each component. */
  45. jvirt_barray_ptr whole_image[MAX_COMPONENTS];
  46. } my_coef_controller;
  47. typedef my_coef_controller * my_coef_ptr;
  48. /* Forward declarations */
  49. METHODDEF(boolean) compress_data
  50. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  51. #ifdef FULL_COEF_BUFFER_SUPPORTED
  52. METHODDEF(boolean) compress_first_pass
  53. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  54. METHODDEF(boolean) compress_output
  55. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  56. #endif
  57. LOCAL(void)
  58. start_iMCU_row (j_compress_ptr cinfo)
  59. /* Reset within-iMCU-row counters for a new row */
  60. {
  61. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  62. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  63. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  64. * But at the bottom of the image, process only what's left.
  65. */
  66. if (cinfo->comps_in_scan > 1) {
  67. coef->MCU_rows_per_iMCU_row = 1;
  68. } else {
  69. if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  70. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  71. else
  72. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  73. }
  74. coef->mcu_ctr = 0;
  75. coef->MCU_vert_offset = 0;
  76. }
  77. /*
  78. * Initialize for a processing pass.
  79. */
  80. METHODDEF(void)
  81. start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  82. {
  83. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  84. coef->iMCU_row_num = 0;
  85. start_iMCU_row(cinfo);
  86. switch (pass_mode) {
  87. case JBUF_PASS_THRU:
  88. if (coef->whole_image[0] != NULL)
  89. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  90. coef->pub.compress_data = compress_data;
  91. break;
  92. #ifdef FULL_COEF_BUFFER_SUPPORTED
  93. case JBUF_SAVE_AND_PASS:
  94. if (coef->whole_image[0] == NULL)
  95. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  96. coef->pub.compress_data = compress_first_pass;
  97. break;
  98. case JBUF_CRANK_DEST:
  99. if (coef->whole_image[0] == NULL)
  100. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  101. coef->pub.compress_data = compress_output;
  102. break;
  103. #endif
  104. default:
  105. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  106. break;
  107. }
  108. }
  109. /*
  110. * Process some data in the single-pass case.
  111. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  112. * per call, ie, v_samp_factor block rows for each component in the image.
  113. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  114. *
  115. * NB: input_buf contains a plane for each component in image,
  116. * which we index according to the component's SOF position.
  117. */
  118. METHODDEF(boolean)
  119. compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  120. {
  121. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  122. JDIMENSION MCU_col_num; /* index of current MCU within row */
  123. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  124. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  125. int blkn, bi, ci, yindex, yoffset, blockcnt;
  126. JDIMENSION ypos, xpos;
  127. jpeg_component_info *compptr;
  128. forward_DCT_ptr forward_DCT;
  129. /* Loop to write as much as one whole iMCU row */
  130. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  131. yoffset++) {
  132. for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  133. MCU_col_num++) {
  134. /* Determine where data comes from in input_buf and do the DCT thing.
  135. * Each call on forward_DCT processes a horizontal row of DCT blocks
  136. * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  137. * sequentially. Dummy blocks at the right or bottom edge are filled in
  138. * specially. The data in them does not matter for image reconstruction,
  139. * so we fill them with values that will encode to the smallest amount of
  140. * data, viz: all zeroes in the AC entries, DC entries equal to previous
  141. * block's DC value. (Thanks to Thomas Kinsman for this idea.)
  142. */
  143. blkn = 0;
  144. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  145. compptr = cinfo->cur_comp_info[ci];
  146. forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
  147. blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  148. : compptr->last_col_width;
  149. xpos = MCU_col_num * compptr->MCU_sample_width;
  150. ypos = yoffset * compptr->DCT_v_scaled_size;
  151. /* ypos == (yoffset+yindex) * DCTSIZE */
  152. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  153. if (coef->iMCU_row_num < last_iMCU_row ||
  154. yoffset+yindex < compptr->last_row_height) {
  155. (*forward_DCT) (cinfo, compptr,
  156. input_buf[compptr->component_index],
  157. coef->MCU_buffer[blkn],
  158. ypos, xpos, (JDIMENSION) blockcnt);
  159. if (blockcnt < compptr->MCU_width) {
  160. /* Create some dummy blocks at the right edge of the image. */
  161. jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  162. (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  163. for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  164. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  165. }
  166. }
  167. } else {
  168. /* Create a row of dummy blocks at the bottom of the image. */
  169. jzero_far((void FAR *) coef->MCU_buffer[blkn],
  170. compptr->MCU_width * SIZEOF(JBLOCK));
  171. for (bi = 0; bi < compptr->MCU_width; bi++) {
  172. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  173. }
  174. }
  175. blkn += compptr->MCU_width;
  176. ypos += compptr->DCT_v_scaled_size;
  177. }
  178. }
  179. /* Try to write the MCU. In event of a suspension failure, we will
  180. * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  181. */
  182. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  183. /* Suspension forced; update state counters and exit */
  184. coef->MCU_vert_offset = yoffset;
  185. coef->mcu_ctr = MCU_col_num;
  186. return FALSE;
  187. }
  188. }
  189. /* Completed an MCU row, but perhaps not an iMCU row */
  190. coef->mcu_ctr = 0;
  191. }
  192. /* Completed the iMCU row, advance counters for next one */
  193. coef->iMCU_row_num++;
  194. start_iMCU_row(cinfo);
  195. return TRUE;
  196. }
  197. #ifdef FULL_COEF_BUFFER_SUPPORTED
  198. /*
  199. * Process some data in the first pass of a multi-pass case.
  200. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  201. * per call, ie, v_samp_factor block rows for each component in the image.
  202. * This amount of data is read from the source buffer, DCT'd and quantized,
  203. * and saved into the virtual arrays. We also generate suitable dummy blocks
  204. * as needed at the right and lower edges. (The dummy blocks are constructed
  205. * in the virtual arrays, which have been padded appropriately.) This makes
  206. * it possible for subsequent passes not to worry about real vs. dummy blocks.
  207. *
  208. * We must also emit the data to the entropy encoder. This is conveniently
  209. * done by calling compress_output() after we've loaded the current strip
  210. * of the virtual arrays.
  211. *
  212. * NB: input_buf contains a plane for each component in image. All
  213. * components are DCT'd and loaded into the virtual arrays in this pass.
  214. * However, it may be that only a subset of the components are emitted to
  215. * the entropy encoder during this first pass; be careful about looking
  216. * at the scan-dependent variables (MCU dimensions, etc).
  217. */
  218. METHODDEF(boolean)
  219. compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  220. {
  221. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  222. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  223. JDIMENSION blocks_across, MCUs_across, MCUindex;
  224. int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  225. JCOEF lastDC;
  226. jpeg_component_info *compptr;
  227. JBLOCKARRAY buffer;
  228. JBLOCKROW thisblockrow, lastblockrow;
  229. forward_DCT_ptr forward_DCT;
  230. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  231. ci++, compptr++) {
  232. /* Align the virtual buffer for this component. */
  233. buffer = (*cinfo->mem->access_virt_barray)
  234. ((j_common_ptr) cinfo, coef->whole_image[ci],
  235. coef->iMCU_row_num * compptr->v_samp_factor,
  236. (JDIMENSION) compptr->v_samp_factor, TRUE);
  237. /* Count non-dummy DCT block rows in this iMCU row. */
  238. if (coef->iMCU_row_num < last_iMCU_row)
  239. block_rows = compptr->v_samp_factor;
  240. else {
  241. /* NB: can't use last_row_height here, since may not be set! */
  242. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  243. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  244. }
  245. blocks_across = compptr->width_in_blocks;
  246. h_samp_factor = compptr->h_samp_factor;
  247. /* Count number of dummy blocks to be added at the right margin. */
  248. ndummy = (int) (blocks_across % h_samp_factor);
  249. if (ndummy > 0)
  250. ndummy = h_samp_factor - ndummy;
  251. forward_DCT = cinfo->fdct->forward_DCT[ci];
  252. /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
  253. * on forward_DCT processes a complete horizontal row of DCT blocks.
  254. */
  255. for (block_row = 0; block_row < block_rows; block_row++) {
  256. thisblockrow = buffer[block_row];
  257. (*forward_DCT) (cinfo, compptr, input_buf[ci], thisblockrow,
  258. (JDIMENSION) (block_row * compptr->DCT_v_scaled_size),
  259. (JDIMENSION) 0, blocks_across);
  260. if (ndummy > 0) {
  261. /* Create dummy blocks at the right edge of the image. */
  262. thisblockrow += blocks_across; /* => first dummy block */
  263. jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  264. lastDC = thisblockrow[-1][0];
  265. for (bi = 0; bi < ndummy; bi++) {
  266. thisblockrow[bi][0] = lastDC;
  267. }
  268. }
  269. }
  270. /* If at end of image, create dummy block rows as needed.
  271. * The tricky part here is that within each MCU, we want the DC values
  272. * of the dummy blocks to match the last real block's DC value.
  273. * This squeezes a few more bytes out of the resulting file...
  274. */
  275. if (coef->iMCU_row_num == last_iMCU_row) {
  276. blocks_across += ndummy; /* include lower right corner */
  277. MCUs_across = blocks_across / h_samp_factor;
  278. for (block_row = block_rows; block_row < compptr->v_samp_factor;
  279. block_row++) {
  280. thisblockrow = buffer[block_row];
  281. lastblockrow = buffer[block_row-1];
  282. jzero_far((void FAR *) thisblockrow,
  283. (size_t) (blocks_across * SIZEOF(JBLOCK)));
  284. for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  285. lastDC = lastblockrow[h_samp_factor-1][0];
  286. for (bi = 0; bi < h_samp_factor; bi++) {
  287. thisblockrow[bi][0] = lastDC;
  288. }
  289. thisblockrow += h_samp_factor; /* advance to next MCU in row */
  290. lastblockrow += h_samp_factor;
  291. }
  292. }
  293. }
  294. }
  295. /* NB: compress_output will increment iMCU_row_num if successful.
  296. * A suspension return will result in redoing all the work above next time.
  297. */
  298. /* Emit data to the entropy encoder, sharing code with subsequent passes */
  299. return compress_output(cinfo, input_buf);
  300. }
  301. /*
  302. * Process some data in subsequent passes of a multi-pass case.
  303. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  304. * per call, ie, v_samp_factor block rows for each component in the scan.
  305. * The data is obtained from the virtual arrays and fed to the entropy coder.
  306. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  307. *
  308. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  309. */
  310. METHODDEF(boolean)
  311. compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  312. {
  313. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  314. JDIMENSION MCU_col_num; /* index of current MCU within row */
  315. int blkn, ci, xindex, yindex, yoffset;
  316. JDIMENSION start_col;
  317. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  318. JBLOCKROW buffer_ptr;
  319. jpeg_component_info *compptr;
  320. /* Align the virtual buffers for the components used in this scan.
  321. * NB: during first pass, this is safe only because the buffers will
  322. * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  323. */
  324. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  325. compptr = cinfo->cur_comp_info[ci];
  326. buffer[ci] = (*cinfo->mem->access_virt_barray)
  327. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  328. coef->iMCU_row_num * compptr->v_samp_factor,
  329. (JDIMENSION) compptr->v_samp_factor, FALSE);
  330. }
  331. /* Loop to process one whole iMCU row */
  332. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  333. yoffset++) {
  334. for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  335. MCU_col_num++) {
  336. /* Construct list of pointers to DCT blocks belonging to this MCU */
  337. blkn = 0; /* index of current DCT block within MCU */
  338. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  339. compptr = cinfo->cur_comp_info[ci];
  340. start_col = MCU_col_num * compptr->MCU_width;
  341. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  342. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  343. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  344. coef->MCU_buffer[blkn++] = buffer_ptr++;
  345. }
  346. }
  347. }
  348. /* Try to write the MCU. */
  349. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  350. /* Suspension forced; update state counters and exit */
  351. coef->MCU_vert_offset = yoffset;
  352. coef->mcu_ctr = MCU_col_num;
  353. return FALSE;
  354. }
  355. }
  356. /* Completed an MCU row, but perhaps not an iMCU row */
  357. coef->mcu_ctr = 0;
  358. }
  359. /* Completed the iMCU row, advance counters for next one */
  360. coef->iMCU_row_num++;
  361. start_iMCU_row(cinfo);
  362. return TRUE;
  363. }
  364. #endif /* FULL_COEF_BUFFER_SUPPORTED */
  365. /*
  366. * Initialize coefficient buffer controller.
  367. */
  368. GLOBAL(void)
  369. jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  370. {
  371. my_coef_ptr coef;
  372. coef = (my_coef_ptr)
  373. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  374. SIZEOF(my_coef_controller));
  375. cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  376. coef->pub.start_pass = start_pass_coef;
  377. /* Create the coefficient buffer. */
  378. if (need_full_buffer) {
  379. #ifdef FULL_COEF_BUFFER_SUPPORTED
  380. /* Allocate a full-image virtual array for each component, */
  381. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  382. int ci;
  383. jpeg_component_info *compptr;
  384. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  385. ci++, compptr++) {
  386. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  387. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  388. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  389. (long) compptr->h_samp_factor),
  390. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  391. (long) compptr->v_samp_factor),
  392. (JDIMENSION) compptr->v_samp_factor);
  393. }
  394. #else
  395. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  396. #endif
  397. } else {
  398. /* We only need a single-MCU buffer. */
  399. JBLOCKROW buffer;
  400. int i;
  401. buffer = (JBLOCKROW)
  402. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  403. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  404. for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  405. coef->MCU_buffer[i] = buffer + i;
  406. }
  407. coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  408. }
  409. }