Audio plugin host https://kx.studio/carla
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.

jcmainct.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * jcmainct.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 main buffer controller for compression.
  9. * The main buffer lies between the pre-processor and the JPEG
  10. * compressor proper; it holds downsampled data in the JPEG colorspace.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /* Note: currently, there is no operating mode in which a full-image buffer
  16. * is needed at this step. If there were, that mode could not be used with
  17. * "raw data" input, since this module is bypassed in that case. However,
  18. * we've left the code here for possible use in special applications.
  19. */
  20. #undef FULL_MAIN_BUFFER_SUPPORTED
  21. /* Private buffer controller object */
  22. typedef struct {
  23. struct jpeg_c_main_controller pub; /* public fields */
  24. JDIMENSION cur_iMCU_row; /* number of current iMCU row */
  25. JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
  26. boolean suspended; /* remember if we suspended output */
  27. J_BUF_MODE pass_mode; /* current operating mode */
  28. /* If using just a strip buffer, this points to the entire set of buffers
  29. * (we allocate one for each component). In the full-image case, this
  30. * points to the currently accessible strips of the virtual arrays.
  31. */
  32. JSAMPARRAY buffer[MAX_COMPONENTS];
  33. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  34. /* If using full-image storage, this array holds pointers to virtual-array
  35. * control blocks for each component. Unused if not full-image storage.
  36. */
  37. jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
  38. #endif
  39. } my_main_controller;
  40. typedef my_main_controller * my_main_ptr;
  41. /* Forward declarations */
  42. METHODDEF(void) process_data_simple_main
  43. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  44. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  45. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  46. METHODDEF(void) process_data_buffer_main
  47. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  48. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  49. #endif
  50. /*
  51. * Initialize for a processing pass.
  52. */
  53. METHODDEF(void)
  54. start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  55. {
  56. my_main_ptr main_ = (my_main_ptr) cinfo->main;
  57. /* Do nothing in raw-data mode. */
  58. if (cinfo->raw_data_in)
  59. return;
  60. main_->cur_iMCU_row = 0; /* initialize counters */
  61. main_->rowgroup_ctr = 0;
  62. main_->suspended = FALSE;
  63. main_->pass_mode = pass_mode; /* save mode for use by process_data */
  64. switch (pass_mode) {
  65. case JBUF_PASS_THRU:
  66. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  67. if (main_->whole_image[0] != NULL)
  68. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  69. #endif
  70. main_->pub.process_data = process_data_simple_main;
  71. break;
  72. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  73. case JBUF_SAVE_SOURCE:
  74. case JBUF_CRANK_DEST:
  75. case JBUF_SAVE_AND_PASS:
  76. if (main_->whole_image[0] == NULL)
  77. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  78. main_->pub.process_data = process_data_buffer_main;
  79. break;
  80. #endif
  81. default:
  82. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  83. break;
  84. }
  85. }
  86. /*
  87. * Process some data.
  88. * This routine handles the simple pass-through mode,
  89. * where we have only a strip buffer.
  90. */
  91. METHODDEF(void)
  92. process_data_simple_main (j_compress_ptr cinfo,
  93. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  94. JDIMENSION in_rows_avail)
  95. {
  96. my_main_ptr main_ = (my_main_ptr) cinfo->main;
  97. while (main_->cur_iMCU_row < cinfo->total_iMCU_rows) {
  98. /* Read input data if we haven't filled the main buffer yet */
  99. if (main_->rowgroup_ctr < DCTSIZE)
  100. (*cinfo->prep->pre_process_data) (cinfo,
  101. input_buf, in_row_ctr, in_rows_avail,
  102. main_->buffer, &main_->rowgroup_ctr,
  103. (JDIMENSION) DCTSIZE);
  104. /* If we don't have a full iMCU row buffered, return to application for
  105. * more data. Note that preprocessor will always pad to fill the iMCU row
  106. * at the bottom of the image.
  107. */
  108. if (main_->rowgroup_ctr != DCTSIZE)
  109. return;
  110. /* Send the completed row to the compressor */
  111. if (! (*cinfo->coef->compress_data) (cinfo, main_->buffer)) {
  112. /* If compressor did not consume the whole row, then we must need to
  113. * suspend processing and return to the application. In this situation
  114. * we pretend we didn't yet consume the last input row; otherwise, if
  115. * it happened to be the last row of the image, the application would
  116. * think we were done.
  117. */
  118. if (! main_->suspended) {
  119. (*in_row_ctr)--;
  120. main_->suspended = TRUE;
  121. }
  122. return;
  123. }
  124. /* We did finish the row. Undo our little suspension hack if a previous
  125. * call suspended; then mark the main buffer empty.
  126. */
  127. if (main_->suspended) {
  128. (*in_row_ctr)++;
  129. main_->suspended = FALSE;
  130. }
  131. main_->rowgroup_ctr = 0;
  132. main_->cur_iMCU_row++;
  133. }
  134. }
  135. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  136. /*
  137. * Process some data.
  138. * This routine handles all of the modes that use a full-size buffer.
  139. */
  140. METHODDEF(void)
  141. process_data_buffer_main (j_compress_ptr cinfo,
  142. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  143. JDIMENSION in_rows_avail)
  144. {
  145. my_main_ptr main = (my_main_ptr) cinfo->main;
  146. int ci;
  147. jpeg_component_info *compptr;
  148. boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
  149. while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  150. /* Realign the virtual buffers if at the start of an iMCU row. */
  151. if (main->rowgroup_ctr == 0) {
  152. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  153. ci++, compptr++) {
  154. main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  155. ((j_common_ptr) cinfo, main->whole_image[ci],
  156. main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
  157. (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
  158. }
  159. /* In a read pass, pretend we just read some source data. */
  160. if (! writing) {
  161. *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
  162. main->rowgroup_ctr = DCTSIZE;
  163. }
  164. }
  165. /* If a write pass, read input data until the current iMCU row is full. */
  166. /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  167. if (writing) {
  168. (*cinfo->prep->pre_process_data) (cinfo,
  169. input_buf, in_row_ctr, in_rows_avail,
  170. main->buffer, &main->rowgroup_ctr,
  171. (JDIMENSION) DCTSIZE);
  172. /* Return to application if we need more data to fill the iMCU row. */
  173. if (main->rowgroup_ctr < DCTSIZE)
  174. return;
  175. }
  176. /* Emit data, unless this is a sink-only pass. */
  177. if (main->pass_mode != JBUF_SAVE_SOURCE) {
  178. if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  179. /* If compressor did not consume the whole row, then we must need to
  180. * suspend processing and return to the application. In this situation
  181. * we pretend we didn't yet consume the last input row; otherwise, if
  182. * it happened to be the last row of the image, the application would
  183. * think we were done.
  184. */
  185. if (! main->suspended) {
  186. (*in_row_ctr)--;
  187. main->suspended = TRUE;
  188. }
  189. return;
  190. }
  191. /* We did finish the row. Undo our little suspension hack if a previous
  192. * call suspended; then mark the main buffer empty.
  193. */
  194. if (main->suspended) {
  195. (*in_row_ctr)++;
  196. main->suspended = FALSE;
  197. }
  198. }
  199. /* If get here, we are done with this iMCU row. Mark buffer empty. */
  200. main->rowgroup_ctr = 0;
  201. main->cur_iMCU_row++;
  202. }
  203. }
  204. #endif /* FULL_MAIN_BUFFER_SUPPORTED */
  205. /*
  206. * Initialize main buffer controller.
  207. */
  208. GLOBAL(void)
  209. jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  210. {
  211. my_main_ptr main_;
  212. int ci;
  213. jpeg_component_info *compptr;
  214. main_ = (my_main_ptr)
  215. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  216. SIZEOF(my_main_controller));
  217. cinfo->main = (struct jpeg_c_main_controller *) main_;
  218. main_->pub.start_pass = start_pass_main;
  219. /* We don't need to create a buffer in raw-data mode. */
  220. if (cinfo->raw_data_in)
  221. return;
  222. /* Create the buffer. It holds downsampled data, so each component
  223. * may be of a different size.
  224. */
  225. if (need_full_buffer) {
  226. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  227. /* Allocate a full-image virtual array for each component */
  228. /* Note we pad the bottom to a multiple of the iMCU height */
  229. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  230. ci++, compptr++) {
  231. main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  232. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  233. compptr->width_in_blocks * DCTSIZE,
  234. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  235. (long) compptr->v_samp_factor) * DCTSIZE,
  236. (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  237. }
  238. #else
  239. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  240. #endif
  241. } else {
  242. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  243. main_->whole_image[0] = NULL; /* flag for no virtual arrays */
  244. #endif
  245. /* Allocate a strip buffer for each component */
  246. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  247. ci++, compptr++) {
  248. main_->buffer[ci] = (*cinfo->mem->alloc_sarray)
  249. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  250. compptr->width_in_blocks * DCTSIZE,
  251. (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  252. }
  253. }
  254. }