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.

jcprepct.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * jcprepct.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 compression preprocessing controller.
  9. * This controller manages the color conversion, downsampling,
  10. * and edge expansion steps.
  11. *
  12. * Most of the complexity here is associated with buffering input rows
  13. * as required by the downsampler. See the comments at the head of
  14. * jcsample.c for the downsampler's needs.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. /* At present, jcsample.c can request context rows only for smoothing.
  20. * In the future, we might also need context rows for CCIR601 sampling
  21. * or other more-complex downsampling procedures. The code to support
  22. * context rows should be compiled only if needed.
  23. */
  24. #ifdef INPUT_SMOOTHING_SUPPORTED
  25. #define CONTEXT_ROWS_SUPPORTED
  26. #endif
  27. /*
  28. * For the simple (no-context-row) case, we just need to buffer one
  29. * row group's worth of pixels for the downsampling step. At the bottom of
  30. * the image, we pad to a full row group by replicating the last pixel row.
  31. * The downsampler's last output row is then replicated if needed to pad
  32. * out to a full iMCU row.
  33. *
  34. * When providing context rows, we must buffer three row groups' worth of
  35. * pixels. Three row groups are physically allocated, but the row pointer
  36. * arrays are made five row groups high, with the extra pointers above and
  37. * below "wrapping around" to point to the last and first real row groups.
  38. * This allows the downsampler to access the proper context rows.
  39. * At the top and bottom of the image, we create dummy context rows by
  40. * copying the first or last real pixel row. This copying could be avoided
  41. * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
  42. * trouble on the compression side.
  43. */
  44. /* Private buffer controller object */
  45. typedef struct {
  46. struct jpeg_c_prep_controller pub; /* public fields */
  47. /* Downsampling input buffer. This buffer holds color-converted data
  48. * until we have enough to do a downsample step.
  49. */
  50. JSAMPARRAY color_buf[MAX_COMPONENTS];
  51. JDIMENSION rows_to_go; /* counts rows remaining in source image */
  52. int next_buf_row; /* index of next row to store in color_buf */
  53. #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
  54. int this_row_group; /* starting row index of group to process */
  55. int next_buf_stop; /* downsample when we reach this index */
  56. #endif
  57. } my_prep_controller;
  58. typedef my_prep_controller * my_prep_ptr;
  59. /*
  60. * Initialize for a processing pass.
  61. */
  62. METHODDEF(void)
  63. start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  64. {
  65. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  66. if (pass_mode != JBUF_PASS_THRU)
  67. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  68. /* Initialize total-height counter for detecting bottom of image */
  69. prep->rows_to_go = cinfo->image_height;
  70. /* Mark the conversion buffer empty */
  71. prep->next_buf_row = 0;
  72. #ifdef CONTEXT_ROWS_SUPPORTED
  73. /* Preset additional state variables for context mode.
  74. * These aren't used in non-context mode, so we needn't test which mode.
  75. */
  76. prep->this_row_group = 0;
  77. /* Set next_buf_stop to stop after two row groups have been read in. */
  78. prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
  79. #endif
  80. }
  81. /*
  82. * Expand an image vertically from height input_rows to height output_rows,
  83. * by duplicating the bottom row.
  84. */
  85. LOCAL(void)
  86. expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  87. int input_rows, int output_rows)
  88. {
  89. register int row;
  90. for (row = input_rows; row < output_rows; row++) {
  91. jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  92. 1, num_cols);
  93. }
  94. }
  95. /*
  96. * Process some data in the simple no-context case.
  97. *
  98. * Preprocessor output data is counted in "row groups". A row group
  99. * is defined to be v_samp_factor sample rows of each component.
  100. * Downsampling will produce this much data from each max_v_samp_factor
  101. * input rows.
  102. */
  103. METHODDEF(void)
  104. pre_process_data (j_compress_ptr cinfo,
  105. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  106. JDIMENSION in_rows_avail,
  107. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  108. JDIMENSION out_row_groups_avail)
  109. {
  110. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  111. int numrows, ci;
  112. JDIMENSION inrows;
  113. jpeg_component_info * compptr;
  114. while (*in_row_ctr < in_rows_avail &&
  115. *out_row_group_ctr < out_row_groups_avail) {
  116. /* Do color conversion to fill the conversion buffer. */
  117. inrows = in_rows_avail - *in_row_ctr;
  118. numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  119. numrows = (int) MIN((JDIMENSION) numrows, inrows);
  120. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  121. prep->color_buf,
  122. (JDIMENSION) prep->next_buf_row,
  123. numrows);
  124. *in_row_ctr += numrows;
  125. prep->next_buf_row += numrows;
  126. prep->rows_to_go -= numrows;
  127. /* If at bottom of image, pad to fill the conversion buffer. */
  128. if (prep->rows_to_go == 0 &&
  129. prep->next_buf_row < cinfo->max_v_samp_factor) {
  130. for (ci = 0; ci < cinfo->num_components; ci++) {
  131. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  132. prep->next_buf_row, cinfo->max_v_samp_factor);
  133. }
  134. prep->next_buf_row = cinfo->max_v_samp_factor;
  135. }
  136. /* If we've filled the conversion buffer, empty it. */
  137. if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  138. (*cinfo->downsample->downsample) (cinfo,
  139. prep->color_buf, (JDIMENSION) 0,
  140. output_buf, *out_row_group_ctr);
  141. prep->next_buf_row = 0;
  142. (*out_row_group_ctr)++;
  143. }
  144. /* If at bottom of image, pad the output to a full iMCU height.
  145. * Note we assume the caller is providing a one-iMCU-height output buffer!
  146. */
  147. if (prep->rows_to_go == 0 &&
  148. *out_row_group_ctr < out_row_groups_avail) {
  149. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  150. ci++, compptr++) {
  151. expand_bottom_edge(output_buf[ci],
  152. compptr->width_in_blocks * DCTSIZE,
  153. (int) (*out_row_group_ctr * compptr->v_samp_factor),
  154. (int) (out_row_groups_avail * compptr->v_samp_factor));
  155. }
  156. *out_row_group_ctr = out_row_groups_avail;
  157. break; /* can exit outer loop without test */
  158. }
  159. }
  160. }
  161. #ifdef CONTEXT_ROWS_SUPPORTED
  162. /*
  163. * Process some data in the context case.
  164. */
  165. METHODDEF(void)
  166. pre_process_context (j_compress_ptr cinfo,
  167. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  168. JDIMENSION in_rows_avail,
  169. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  170. JDIMENSION out_row_groups_avail)
  171. {
  172. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  173. int numrows, ci;
  174. int buf_height = cinfo->max_v_samp_factor * 3;
  175. JDIMENSION inrows;
  176. while (*out_row_group_ctr < out_row_groups_avail) {
  177. if (*in_row_ctr < in_rows_avail) {
  178. /* Do color conversion to fill the conversion buffer. */
  179. inrows = in_rows_avail - *in_row_ctr;
  180. numrows = prep->next_buf_stop - prep->next_buf_row;
  181. numrows = (int) MIN((JDIMENSION) numrows, inrows);
  182. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  183. prep->color_buf,
  184. (JDIMENSION) prep->next_buf_row,
  185. numrows);
  186. /* Pad at top of image, if first time through */
  187. if (prep->rows_to_go == cinfo->image_height) {
  188. for (ci = 0; ci < cinfo->num_components; ci++) {
  189. int row;
  190. for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  191. jcopy_sample_rows(prep->color_buf[ci], 0,
  192. prep->color_buf[ci], -row,
  193. 1, cinfo->image_width);
  194. }
  195. }
  196. }
  197. *in_row_ctr += numrows;
  198. prep->next_buf_row += numrows;
  199. prep->rows_to_go -= numrows;
  200. } else {
  201. /* Return for more data, unless we are at the bottom of the image. */
  202. if (prep->rows_to_go != 0)
  203. break;
  204. /* When at bottom of image, pad to fill the conversion buffer. */
  205. if (prep->next_buf_row < prep->next_buf_stop) {
  206. for (ci = 0; ci < cinfo->num_components; ci++) {
  207. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  208. prep->next_buf_row, prep->next_buf_stop);
  209. }
  210. prep->next_buf_row = prep->next_buf_stop;
  211. }
  212. }
  213. /* If we've gotten enough data, downsample a row group. */
  214. if (prep->next_buf_row == prep->next_buf_stop) {
  215. (*cinfo->downsample->downsample) (cinfo,
  216. prep->color_buf,
  217. (JDIMENSION) prep->this_row_group,
  218. output_buf, *out_row_group_ctr);
  219. (*out_row_group_ctr)++;
  220. /* Advance pointers with wraparound as necessary. */
  221. prep->this_row_group += cinfo->max_v_samp_factor;
  222. if (prep->this_row_group >= buf_height)
  223. prep->this_row_group = 0;
  224. if (prep->next_buf_row >= buf_height)
  225. prep->next_buf_row = 0;
  226. prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  227. }
  228. }
  229. }
  230. /*
  231. * Create the wrapped-around downsampling input buffer needed for context mode.
  232. */
  233. LOCAL(void)
  234. create_context_buffer (j_compress_ptr cinfo)
  235. {
  236. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  237. int rgroup_height = cinfo->max_v_samp_factor;
  238. int ci, i;
  239. jpeg_component_info * compptr;
  240. JSAMPARRAY true_buffer, fake_buffer;
  241. /* Grab enough space for fake row pointers for all the components;
  242. * we need five row groups' worth of pointers for each component.
  243. */
  244. fake_buffer = (JSAMPARRAY)
  245. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  246. (cinfo->num_components * 5 * rgroup_height) *
  247. SIZEOF(JSAMPROW));
  248. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  249. ci++, compptr++) {
  250. /* Allocate the actual buffer space (3 row groups) for this component.
  251. * We make the buffer wide enough to allow the downsampler to edge-expand
  252. * horizontally within the buffer, if it so chooses.
  253. */
  254. true_buffer = (*cinfo->mem->alloc_sarray)
  255. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  256. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  257. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  258. (JDIMENSION) (3 * rgroup_height));
  259. /* Copy true buffer row pointers into the middle of the fake row array */
  260. MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  261. 3 * rgroup_height * SIZEOF(JSAMPROW));
  262. /* Fill in the above and below wraparound pointers */
  263. for (i = 0; i < rgroup_height; i++) {
  264. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  265. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  266. }
  267. prep->color_buf[ci] = fake_buffer + rgroup_height;
  268. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  269. }
  270. }
  271. #endif /* CONTEXT_ROWS_SUPPORTED */
  272. /*
  273. * Initialize preprocessing controller.
  274. */
  275. GLOBAL(void)
  276. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  277. {
  278. my_prep_ptr prep;
  279. int ci;
  280. jpeg_component_info * compptr;
  281. if (need_full_buffer) /* safety check */
  282. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  283. prep = (my_prep_ptr)
  284. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  285. SIZEOF(my_prep_controller));
  286. cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  287. prep->pub.start_pass = start_pass_prep;
  288. /* Allocate the color conversion buffer.
  289. * We make the buffer wide enough to allow the downsampler to edge-expand
  290. * horizontally within the buffer, if it so chooses.
  291. */
  292. if (cinfo->downsample->need_context_rows) {
  293. /* Set up to provide context rows */
  294. #ifdef CONTEXT_ROWS_SUPPORTED
  295. prep->pub.pre_process_data = pre_process_context;
  296. create_context_buffer(cinfo);
  297. #else
  298. ERREXIT(cinfo, JERR_NOT_COMPILED);
  299. #endif
  300. } else {
  301. /* No context, just make it tall enough for one row group */
  302. prep->pub.pre_process_data = pre_process_data;
  303. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  304. ci++, compptr++) {
  305. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  306. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  307. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  308. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  309. (JDIMENSION) cinfo->max_v_samp_factor);
  310. }
  311. }
  312. }