jccoefct.c 17 KB

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