jccoefct.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * jccoefct.c
  3. *
  4. * Copyright (C) 1994-1996, 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. * For single pass, this is the same as the components in the scan.
  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. /* Loop to write as much as one whole iMCU row */
  129. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  130. yoffset++) {
  131. for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  132. MCU_col_num++) {
  133. /* Determine where data comes from in input_buf and do the DCT thing.
  134. * Each call on forward_DCT processes a horizontal row of DCT blocks
  135. * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  136. * sequentially. Dummy blocks at the right or bottom edge are filled in
  137. * specially. The data in them does not matter for image reconstruction,
  138. * so we fill them with values that will encode to the smallest amount of
  139. * data, viz: all zeroes in the AC entries, DC entries equal to previous
  140. * block's DC value. (Thanks to Thomas Kinsman for this idea.)
  141. */
  142. blkn = 0;
  143. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  144. compptr = cinfo->cur_comp_info[ci];
  145. blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  146. : compptr->last_col_width;
  147. xpos = MCU_col_num * compptr->MCU_sample_width;
  148. ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  149. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  150. if (coef->iMCU_row_num < last_iMCU_row ||
  151. yoffset+yindex < compptr->last_row_height) {
  152. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  153. input_buf[ci], coef->MCU_buffer[blkn],
  154. ypos, xpos, (JDIMENSION) blockcnt);
  155. if (blockcnt < compptr->MCU_width) {
  156. /* Create some dummy blocks at the right edge of the image. */
  157. jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  158. (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  159. for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  160. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  161. }
  162. }
  163. } else {
  164. /* Create a row of dummy blocks at the bottom of the image. */
  165. jzero_far((void FAR *) coef->MCU_buffer[blkn],
  166. compptr->MCU_width * SIZEOF(JBLOCK));
  167. for (bi = 0; bi < compptr->MCU_width; bi++) {
  168. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  169. }
  170. }
  171. blkn += compptr->MCU_width;
  172. ypos += DCTSIZE;
  173. }
  174. }
  175. /* Try to write the MCU. In event of a suspension failure, we will
  176. * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  177. */
  178. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  179. /* Suspension forced; update state counters and exit */
  180. coef->MCU_vert_offset = yoffset;
  181. coef->mcu_ctr = MCU_col_num;
  182. return FALSE;
  183. }
  184. }
  185. /* Completed an MCU row, but perhaps not an iMCU row */
  186. coef->mcu_ctr = 0;
  187. }
  188. /* Completed the iMCU row, advance counters for next one */
  189. coef->iMCU_row_num++;
  190. start_iMCU_row(cinfo);
  191. return TRUE;
  192. }
  193. #ifdef FULL_COEF_BUFFER_SUPPORTED
  194. /*
  195. * Process some data in the first pass of a multi-pass case.
  196. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  197. * per call, ie, v_samp_factor block rows for each component in the image.
  198. * This amount of data is read from the source buffer, DCT'd and quantized,
  199. * and saved into the virtual arrays. We also generate suitable dummy blocks
  200. * as needed at the right and lower edges. (The dummy blocks are constructed
  201. * in the virtual arrays, which have been padded appropriately.) This makes
  202. * it possible for subsequent passes not to worry about real vs. dummy blocks.
  203. *
  204. * We must also emit the data to the entropy encoder. This is conveniently
  205. * done by calling compress_output() after we've loaded the current strip
  206. * of the virtual arrays.
  207. *
  208. * NB: input_buf contains a plane for each component in image. All
  209. * components are DCT'd and loaded into the virtual arrays in this pass.
  210. * However, it may be that only a subset of the components are emitted to
  211. * the entropy encoder during this first pass; be careful about looking
  212. * at the scan-dependent variables (MCU dimensions, etc).
  213. */
  214. METHODDEF(boolean)
  215. compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  216. {
  217. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  218. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  219. JDIMENSION blocks_across, MCUs_across, MCUindex;
  220. int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  221. JCOEF lastDC;
  222. jpeg_component_info *compptr;
  223. JBLOCKARRAY buffer;
  224. JBLOCKROW thisblockrow, lastblockrow;
  225. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  226. ci++, compptr++) {
  227. /* Align the virtual buffer for this component. */
  228. buffer = (*cinfo->mem->access_virt_barray)
  229. ((j_common_ptr) cinfo, coef->whole_image[ci],
  230. coef->iMCU_row_num * compptr->v_samp_factor,
  231. (JDIMENSION) compptr->v_samp_factor, TRUE);
  232. /* Count non-dummy DCT block rows in this iMCU row. */
  233. if (coef->iMCU_row_num < last_iMCU_row)
  234. block_rows = compptr->v_samp_factor;
  235. else {
  236. /* NB: can't use last_row_height here, since may not be set! */
  237. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  238. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  239. }
  240. blocks_across = compptr->width_in_blocks;
  241. h_samp_factor = compptr->h_samp_factor;
  242. /* Count number of dummy blocks to be added at the right margin. */
  243. ndummy = (int) (blocks_across % h_samp_factor);
  244. if (ndummy > 0)
  245. ndummy = h_samp_factor - ndummy;
  246. /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
  247. * on forward_DCT processes a complete horizontal row of DCT blocks.
  248. */
  249. for (block_row = 0; block_row < block_rows; block_row++) {
  250. thisblockrow = buffer[block_row];
  251. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  252. input_buf[ci], thisblockrow,
  253. (JDIMENSION) (block_row * DCTSIZE),
  254. (JDIMENSION) 0, blocks_across);
  255. if (ndummy > 0) {
  256. /* Create dummy blocks at the right edge of the image. */
  257. thisblockrow += blocks_across; /* => first dummy block */
  258. jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  259. lastDC = thisblockrow[-1][0];
  260. for (bi = 0; bi < ndummy; bi++) {
  261. thisblockrow[bi][0] = lastDC;
  262. }
  263. }
  264. }
  265. /* If at end of image, create dummy block rows as needed.
  266. * The tricky part here is that within each MCU, we want the DC values
  267. * of the dummy blocks to match the last real block's DC value.
  268. * This squeezes a few more bytes out of the resulting file...
  269. */
  270. if (coef->iMCU_row_num == last_iMCU_row) {
  271. blocks_across += ndummy; /* include lower right corner */
  272. MCUs_across = blocks_across / h_samp_factor;
  273. for (block_row = block_rows; block_row < compptr->v_samp_factor;
  274. block_row++) {
  275. thisblockrow = buffer[block_row];
  276. lastblockrow = buffer[block_row-1];
  277. jzero_far((void FAR *) thisblockrow,
  278. (size_t) (blocks_across * SIZEOF(JBLOCK)));
  279. for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  280. lastDC = lastblockrow[h_samp_factor-1][0];
  281. for (bi = 0; bi < h_samp_factor; bi++) {
  282. thisblockrow[bi][0] = lastDC;
  283. }
  284. thisblockrow += h_samp_factor; /* advance to next MCU in row */
  285. lastblockrow += h_samp_factor;
  286. }
  287. }
  288. }
  289. }
  290. /* NB: compress_output will increment iMCU_row_num if successful.
  291. * A suspension return will result in redoing all the work above next time.
  292. */
  293. /* Emit data to the entropy encoder, sharing code with subsequent passes */
  294. return compress_output(cinfo, input_buf);
  295. }
  296. /*
  297. * Process some data in subsequent passes of a multi-pass case.
  298. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  299. * per call, ie, v_samp_factor block rows for each component in the scan.
  300. * The data is obtained from the virtual arrays and fed to the entropy coder.
  301. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  302. *
  303. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  304. */
  305. METHODDEF(boolean)
  306. compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  307. {
  308. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  309. JDIMENSION MCU_col_num; /* index of current MCU within row */
  310. int blkn, ci, xindex, yindex, yoffset;
  311. JDIMENSION start_col;
  312. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  313. JBLOCKROW buffer_ptr;
  314. jpeg_component_info *compptr;
  315. /* Align the virtual buffers for the components used in this scan.
  316. * NB: during first pass, this is safe only because the buffers will
  317. * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  318. */
  319. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  320. compptr = cinfo->cur_comp_info[ci];
  321. buffer[ci] = (*cinfo->mem->access_virt_barray)
  322. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  323. coef->iMCU_row_num * compptr->v_samp_factor,
  324. (JDIMENSION) compptr->v_samp_factor, FALSE);
  325. }
  326. /* Loop to process one whole iMCU row */
  327. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  328. yoffset++) {
  329. for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  330. MCU_col_num++) {
  331. /* Construct list of pointers to DCT blocks belonging to this MCU */
  332. blkn = 0; /* index of current DCT block within MCU */
  333. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  334. compptr = cinfo->cur_comp_info[ci];
  335. start_col = MCU_col_num * compptr->MCU_width;
  336. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  337. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  338. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  339. coef->MCU_buffer[blkn++] = buffer_ptr++;
  340. }
  341. }
  342. }
  343. /* Try to write the MCU. */
  344. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  345. /* Suspension forced; update state counters and exit */
  346. coef->MCU_vert_offset = yoffset;
  347. coef->mcu_ctr = MCU_col_num;
  348. return FALSE;
  349. }
  350. }
  351. /* Completed an MCU row, but perhaps not an iMCU row */
  352. coef->mcu_ctr = 0;
  353. }
  354. /* Completed the iMCU row, advance counters for next one */
  355. coef->iMCU_row_num++;
  356. start_iMCU_row(cinfo);
  357. return TRUE;
  358. }
  359. #endif /* FULL_COEF_BUFFER_SUPPORTED */
  360. /*
  361. * Initialize coefficient buffer controller.
  362. */
  363. GLOBAL(void)
  364. jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  365. {
  366. my_coef_ptr coef;
  367. coef = (my_coef_ptr)
  368. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  369. SIZEOF(my_coef_controller));
  370. cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  371. coef->pub.start_pass = start_pass_coef;
  372. /* Create the coefficient buffer. */
  373. if (need_full_buffer) {
  374. #ifdef FULL_COEF_BUFFER_SUPPORTED
  375. /* Allocate a full-image virtual array for each component, */
  376. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  377. int ci;
  378. jpeg_component_info *compptr;
  379. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  380. ci++, compptr++) {
  381. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  382. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  383. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  384. (long) compptr->h_samp_factor),
  385. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  386. (long) compptr->v_samp_factor),
  387. (JDIMENSION) compptr->v_samp_factor);
  388. }
  389. #else
  390. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  391. #endif
  392. } else {
  393. /* We only need a single-MCU buffer. */
  394. JBLOCKROW buffer;
  395. int i;
  396. buffer = (JBLOCKROW)
  397. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  398. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  399. for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  400. coef->MCU_buffer[i] = buffer + i;
  401. }
  402. coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  403. }
  404. }