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.

jctrans.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * jctrans.c
  3. *
  4. * Copyright (C) 1995-1998, 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 library routines for transcoding compression,
  9. * that is, writing raw DCT coefficient arrays to an output JPEG file.
  10. * The routines in jcapimin.c will also be needed by a transcoder.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /* Forward declarations */
  16. LOCAL(void) transencode_master_selection
  17. JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
  18. LOCAL(void) transencode_coef_controller
  19. JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
  20. /*
  21. * Compression initialization for writing raw-coefficient data.
  22. * Before calling this, all parameters and a data destination must be set up.
  23. * Call jpeg_finish_compress() to actually write the data.
  24. *
  25. * The number of passed virtual arrays must match cinfo->num_components.
  26. * Note that the virtual arrays need not be filled or even realized at
  27. * the time write_coefficients is called; indeed, if the virtual arrays
  28. * were requested from this compression object's memory manager, they
  29. * typically will be realized during this routine and filled afterwards.
  30. */
  31. GLOBAL(void)
  32. jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
  33. {
  34. if (cinfo->global_state != CSTATE_START)
  35. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  36. /* Mark all tables to be written */
  37. jpeg_suppress_tables(cinfo, FALSE);
  38. /* (Re)initialize error mgr and destination modules */
  39. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  40. (*cinfo->dest->init_destination) (cinfo);
  41. /* Perform master selection of active modules */
  42. transencode_master_selection(cinfo, coef_arrays);
  43. /* Wait for jpeg_finish_compress() call */
  44. cinfo->next_scanline = 0; /* so jpeg_write_marker works */
  45. cinfo->global_state = CSTATE_WRCOEFS;
  46. }
  47. /*
  48. * Initialize the compression object with default parameters,
  49. * then copy from the source object all parameters needed for lossless
  50. * transcoding. Parameters that can be varied without loss (such as
  51. * scan script and Huffman optimization) are left in their default states.
  52. */
  53. GLOBAL(void)
  54. jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
  55. j_compress_ptr dstinfo)
  56. {
  57. JQUANT_TBL ** qtblptr;
  58. jpeg_component_info *incomp, *outcomp;
  59. JQUANT_TBL *c_quant, *slot_quant;
  60. int tblno, ci, coefi;
  61. /* Safety check to ensure start_compress not called yet. */
  62. if (dstinfo->global_state != CSTATE_START)
  63. ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
  64. /* Copy fundamental image dimensions */
  65. dstinfo->image_width = srcinfo->image_width;
  66. dstinfo->image_height = srcinfo->image_height;
  67. dstinfo->input_components = srcinfo->num_components;
  68. dstinfo->in_color_space = srcinfo->jpeg_color_space;
  69. /* Initialize all parameters to default values */
  70. jpeg_set_defaults(dstinfo);
  71. /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
  72. * Fix it to get the right header markers for the image colorspace.
  73. */
  74. jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
  75. dstinfo->data_precision = srcinfo->data_precision;
  76. dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
  77. /* Copy the source's quantization tables. */
  78. for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
  79. if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
  80. qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
  81. if (*qtblptr == NULL)
  82. *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
  83. MEMCOPY((*qtblptr)->quantval,
  84. srcinfo->quant_tbl_ptrs[tblno]->quantval,
  85. SIZEOF((*qtblptr)->quantval));
  86. (*qtblptr)->sent_table = FALSE;
  87. }
  88. }
  89. /* Copy the source's per-component info.
  90. * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
  91. */
  92. dstinfo->num_components = srcinfo->num_components;
  93. if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
  94. ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
  95. MAX_COMPONENTS);
  96. for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
  97. ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
  98. outcomp->component_id = incomp->component_id;
  99. outcomp->h_samp_factor = incomp->h_samp_factor;
  100. outcomp->v_samp_factor = incomp->v_samp_factor;
  101. outcomp->quant_tbl_no = incomp->quant_tbl_no;
  102. /* Make sure saved quantization table for component matches the qtable
  103. * slot. If not, the input file re-used this qtable slot.
  104. * IJG encoder currently cannot duplicate this.
  105. */
  106. tblno = outcomp->quant_tbl_no;
  107. if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
  108. srcinfo->quant_tbl_ptrs[tblno] == NULL)
  109. ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
  110. slot_quant = srcinfo->quant_tbl_ptrs[tblno];
  111. c_quant = incomp->quant_table;
  112. if (c_quant != NULL) {
  113. for (coefi = 0; coefi < DCTSIZE2; coefi++) {
  114. if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
  115. ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
  116. }
  117. }
  118. /* Note: we do not copy the source's Huffman table assignments;
  119. * instead we rely on jpeg_set_colorspace to have made a suitable choice.
  120. */
  121. }
  122. /* Also copy JFIF version and resolution information, if available.
  123. * Strictly speaking this isn't "critical" info, but it's nearly
  124. * always appropriate to copy it if available. In particular,
  125. * if the application chooses to copy JFIF 1.02 extension markers from
  126. * the source file, we need to copy the version to make sure we don't
  127. * emit a file that has 1.02 extensions but a claimed version of 1.01.
  128. * We will *not*, however, copy version info from mislabeled "2.01" files.
  129. */
  130. if (srcinfo->saw_JFIF_marker) {
  131. if (srcinfo->JFIF_major_version == 1) {
  132. dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
  133. dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
  134. }
  135. dstinfo->density_unit = srcinfo->density_unit;
  136. dstinfo->X_density = srcinfo->X_density;
  137. dstinfo->Y_density = srcinfo->Y_density;
  138. }
  139. }
  140. /*
  141. * Master selection of compression modules for transcoding.
  142. * This substitutes for jcinit.c's initialization of the full compressor.
  143. */
  144. LOCAL(void)
  145. transencode_master_selection (j_compress_ptr cinfo,
  146. jvirt_barray_ptr * coef_arrays)
  147. {
  148. /* Although we don't actually use input_components for transcoding,
  149. * jcmaster.c's initial_setup will complain if input_components is 0.
  150. */
  151. cinfo->input_components = 1;
  152. /* Initialize master control (includes parameter checking/processing) */
  153. jinit_c_master_control(cinfo, TRUE /* transcode only */);
  154. /* Entropy encoding: either Huffman or arithmetic coding. */
  155. if (cinfo->arith_code) {
  156. ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  157. } else {
  158. if (cinfo->progressive_mode) {
  159. #ifdef C_PROGRESSIVE_SUPPORTED
  160. jinit_phuff_encoder(cinfo);
  161. #else
  162. ERREXIT(cinfo, JERR_NOT_COMPILED);
  163. #endif
  164. } else
  165. jinit_huff_encoder(cinfo);
  166. }
  167. /* We need a special coefficient buffer controller. */
  168. transencode_coef_controller(cinfo, coef_arrays);
  169. jinit_marker_writer(cinfo);
  170. /* We can now tell the memory manager to allocate virtual arrays. */
  171. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  172. /* Write the datastream header (SOI, JFIF) immediately.
  173. * Frame and scan headers are postponed till later.
  174. * This lets application insert special markers after the SOI.
  175. */
  176. (*cinfo->marker->write_file_header) (cinfo);
  177. }
  178. /*
  179. * The rest of this file is a special implementation of the coefficient
  180. * buffer controller. This is similar to jccoefct.c, but it handles only
  181. * output from presupplied virtual arrays. Furthermore, we generate any
  182. * dummy padding blocks on-the-fly rather than expecting them to be present
  183. * in the arrays.
  184. */
  185. /* Private buffer controller object */
  186. typedef struct {
  187. struct jpeg_c_coef_controller pub; /* public fields */
  188. JDIMENSION iMCU_row_num; /* iMCU row # within image */
  189. JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
  190. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  191. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  192. /* Virtual block array for each component. */
  193. jvirt_barray_ptr * whole_image;
  194. /* Workspace for constructing dummy blocks at right/bottom edges. */
  195. JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
  196. } my_coef_controller2;
  197. typedef my_coef_controller2 * my_coef_ptr2;
  198. LOCAL(void)
  199. start_iMCU_row2 (j_compress_ptr cinfo)
  200. /* Reset within-iMCU-row counters for a new row */
  201. {
  202. my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;
  203. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  204. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  205. * But at the bottom of the image, process only what's left.
  206. */
  207. if (cinfo->comps_in_scan > 1) {
  208. coef->MCU_rows_per_iMCU_row = 1;
  209. } else {
  210. if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  211. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  212. else
  213. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  214. }
  215. coef->mcu_ctr = 0;
  216. coef->MCU_vert_offset = 0;
  217. }
  218. /*
  219. * Initialize for a processing pass.
  220. */
  221. METHODDEF(void)
  222. start_pass_coef2 (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  223. {
  224. my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;
  225. if (pass_mode != JBUF_CRANK_DEST)
  226. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  227. coef->iMCU_row_num = 0;
  228. start_iMCU_row2(cinfo);
  229. }
  230. /*
  231. * Process some data.
  232. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  233. * per call, ie, v_samp_factor block rows for each component in the scan.
  234. * The data is obtained from the virtual arrays and fed to the entropy coder.
  235. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  236. *
  237. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  238. */
  239. METHODDEF(boolean)
  240. compress_output2 (j_compress_ptr cinfo, JSAMPIMAGE)
  241. {
  242. my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;
  243. JDIMENSION MCU_col_num; /* index of current MCU within row */
  244. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  245. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  246. int blkn, ci, xindex, yindex, yoffset, blockcnt;
  247. JDIMENSION start_col;
  248. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  249. JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  250. JBLOCKROW buffer_ptr;
  251. jpeg_component_info *compptr;
  252. /* Align the virtual buffers for the components used in this scan. */
  253. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  254. compptr = cinfo->cur_comp_info[ci];
  255. buffer[ci] = (*cinfo->mem->access_virt_barray)
  256. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  257. coef->iMCU_row_num * compptr->v_samp_factor,
  258. (JDIMENSION) compptr->v_samp_factor, FALSE);
  259. }
  260. /* Loop to process one whole iMCU row */
  261. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  262. yoffset++) {
  263. for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  264. MCU_col_num++) {
  265. /* Construct list of pointers to DCT blocks belonging to this MCU */
  266. blkn = 0; /* index of current DCT block within MCU */
  267. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  268. compptr = cinfo->cur_comp_info[ci];
  269. start_col = MCU_col_num * compptr->MCU_width;
  270. blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  271. : compptr->last_col_width;
  272. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  273. if (coef->iMCU_row_num < last_iMCU_row ||
  274. yindex+yoffset < compptr->last_row_height) {
  275. /* Fill in pointers to real blocks in this row */
  276. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  277. for (xindex = 0; xindex < blockcnt; xindex++)
  278. MCU_buffer[blkn++] = buffer_ptr++;
  279. } else {
  280. /* At bottom of image, need a whole row of dummy blocks */
  281. xindex = 0;
  282. }
  283. /* Fill in any dummy blocks needed in this row.
  284. * Dummy blocks are filled in the same way as in jccoefct.c:
  285. * all zeroes in the AC entries, DC entries equal to previous
  286. * block's DC value. The init routine has already zeroed the
  287. * AC entries, so we need only set the DC entries correctly.
  288. */
  289. for (; xindex < compptr->MCU_width; xindex++) {
  290. MCU_buffer[blkn] = coef->dummy_buffer[blkn];
  291. MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
  292. blkn++;
  293. }
  294. }
  295. }
  296. /* Try to write the MCU. */
  297. if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
  298. /* Suspension forced; update state counters and exit */
  299. coef->MCU_vert_offset = yoffset;
  300. coef->mcu_ctr = MCU_col_num;
  301. return FALSE;
  302. }
  303. }
  304. /* Completed an MCU row, but perhaps not an iMCU row */
  305. coef->mcu_ctr = 0;
  306. }
  307. /* Completed the iMCU row, advance counters for next one */
  308. coef->iMCU_row_num++;
  309. start_iMCU_row2(cinfo);
  310. return TRUE;
  311. }
  312. /*
  313. * Initialize coefficient buffer controller.
  314. *
  315. * Each passed coefficient array must be the right size for that
  316. * coefficient: width_in_blocks wide and height_in_blocks high,
  317. * with unitheight at least v_samp_factor.
  318. */
  319. LOCAL(void)
  320. transencode_coef_controller (j_compress_ptr cinfo,
  321. jvirt_barray_ptr * coef_arrays)
  322. {
  323. my_coef_ptr2 coef;
  324. JBLOCKROW buffer;
  325. int i;
  326. coef = (my_coef_ptr2)
  327. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  328. SIZEOF(my_coef_controller2));
  329. cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  330. coef->pub.start_pass = start_pass_coef2;
  331. coef->pub.compress_data = compress_output2;
  332. /* Save pointer to virtual arrays */
  333. coef->whole_image = coef_arrays;
  334. /* Allocate and pre-zero space for dummy DCT blocks. */
  335. buffer = (JBLOCKROW)
  336. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  337. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  338. jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  339. for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  340. coef->dummy_buffer[i] = buffer + i;
  341. }
  342. }