jccoefct.c 16 KB

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