jccoefct.c 16 KB

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