blob: ab6a2186db6f7b9ec545548028c98e732d32c107 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jctrans.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1995-1998, Thomas G. Lane.
hbono@chromium.org98626972011-08-03 03:13:08 +00006 * Modified 2000-2009 by Guido Vollbeding.
Jonathan Wrightbbb82822020-11-25 13:36:43 +00007 * libjpeg-turbo Modifications:
8 * Copyright (C) 2020, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000011 *
12 * This file contains library routines for transcoding compression,
13 * that is, writing raw DCT coefficient arrays to an output JPEG file.
14 * The routines in jcapimin.c will also be needed by a transcoder.
15 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
Jonathan Wrightbbb82822020-11-25 13:36:43 +000020#include "jpegcomp.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000021
22
23/* Forward declarations */
Chris Blumecca8c4d2019-03-01 01:09:50 -080024LOCAL(void) transencode_master_selection(j_compress_ptr cinfo,
25 jvirt_barray_ptr *coef_arrays);
26LOCAL(void) transencode_coef_controller(j_compress_ptr cinfo,
27 jvirt_barray_ptr *coef_arrays);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000028
29
30/*
31 * Compression initialization for writing raw-coefficient data.
32 * Before calling this, all parameters and a data destination must be set up.
33 * Call jpeg_finish_compress() to actually write the data.
34 *
35 * The number of passed virtual arrays must match cinfo->num_components.
36 * Note that the virtual arrays need not be filled or even realized at
37 * the time write_coefficients is called; indeed, if the virtual arrays
38 * were requested from this compression object's memory manager, they
39 * typically will be realized during this routine and filled afterwards.
40 */
41
42GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080043jpeg_write_coefficients(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000044{
45 if (cinfo->global_state != CSTATE_START)
46 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
47 /* Mark all tables to be written */
48 jpeg_suppress_tables(cinfo, FALSE);
49 /* (Re)initialize error mgr and destination modules */
Chris Blumecca8c4d2019-03-01 01:09:50 -080050 (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000051 (*cinfo->dest->init_destination) (cinfo);
52 /* Perform master selection of active modules */
53 transencode_master_selection(cinfo, coef_arrays);
54 /* Wait for jpeg_finish_compress() call */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040055 cinfo->next_scanline = 0; /* so jpeg_write_marker works */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000056 cinfo->global_state = CSTATE_WRCOEFS;
57}
58
59
60/*
61 * Initialize the compression object with default parameters,
62 * then copy from the source object all parameters needed for lossless
63 * transcoding. Parameters that can be varied without loss (such as
64 * scan script and Huffman optimization) are left in their default states.
65 */
66
67GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080068jpeg_copy_critical_parameters(j_decompress_ptr srcinfo, j_compress_ptr dstinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000069{
Tom Hudson0d47d2d2016-05-04 13:22:56 -040070 JQUANT_TBL **qtblptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000071 jpeg_component_info *incomp, *outcomp;
72 JQUANT_TBL *c_quant, *slot_quant;
73 int tblno, ci, coefi;
74
75 /* Safety check to ensure start_compress not called yet. */
76 if (dstinfo->global_state != CSTATE_START)
77 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
78 /* Copy fundamental image dimensions */
79 dstinfo->image_width = srcinfo->image_width;
80 dstinfo->image_height = srcinfo->image_height;
81 dstinfo->input_components = srcinfo->num_components;
82 dstinfo->in_color_space = srcinfo->jpeg_color_space;
hbono@chromium.org98626972011-08-03 03:13:08 +000083#if JPEG_LIB_VERSION >= 70
84 dstinfo->jpeg_width = srcinfo->output_width;
85 dstinfo->jpeg_height = srcinfo->output_height;
86 dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;
87 dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;
88#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000089 /* Initialize all parameters to default values */
90 jpeg_set_defaults(dstinfo);
91 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
92 * Fix it to get the right header markers for the image colorspace.
93 */
94 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
95 dstinfo->data_precision = srcinfo->data_precision;
96 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
97 /* Copy the source's quantization tables. */
98 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
99 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800100 qtblptr = &dstinfo->quant_tbl_ptrs[tblno];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000101 if (*qtblptr == NULL)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800102 *qtblptr = jpeg_alloc_quant_table((j_common_ptr)dstinfo);
103 MEMCOPY((*qtblptr)->quantval, srcinfo->quant_tbl_ptrs[tblno]->quantval,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400104 sizeof((*qtblptr)->quantval));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000105 (*qtblptr)->sent_table = FALSE;
106 }
107 }
108 /* Copy the source's per-component info.
109 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
110 */
111 dstinfo->num_components = srcinfo->num_components;
112 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
113 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400114 MAX_COMPONENTS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000115 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
116 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
117 outcomp->component_id = incomp->component_id;
118 outcomp->h_samp_factor = incomp->h_samp_factor;
119 outcomp->v_samp_factor = incomp->v_samp_factor;
120 outcomp->quant_tbl_no = incomp->quant_tbl_no;
121 /* Make sure saved quantization table for component matches the qtable
122 * slot. If not, the input file re-used this qtable slot.
123 * IJG encoder currently cannot duplicate this.
124 */
125 tblno = outcomp->quant_tbl_no;
126 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400127 srcinfo->quant_tbl_ptrs[tblno] == NULL)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000128 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
129 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
130 c_quant = incomp->quant_table;
131 if (c_quant != NULL) {
132 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400133 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
134 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000135 }
136 }
137 /* Note: we do not copy the source's Huffman table assignments;
138 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
139 */
140 }
141 /* Also copy JFIF version and resolution information, if available.
142 * Strictly speaking this isn't "critical" info, but it's nearly
143 * always appropriate to copy it if available. In particular,
144 * if the application chooses to copy JFIF 1.02 extension markers from
145 * the source file, we need to copy the version to make sure we don't
146 * emit a file that has 1.02 extensions but a claimed version of 1.01.
147 * We will *not*, however, copy version info from mislabeled "2.01" files.
148 */
149 if (srcinfo->saw_JFIF_marker) {
150 if (srcinfo->JFIF_major_version == 1) {
151 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
152 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
153 }
154 dstinfo->density_unit = srcinfo->density_unit;
155 dstinfo->X_density = srcinfo->X_density;
156 dstinfo->Y_density = srcinfo->Y_density;
157 }
158}
159
160
161/*
162 * Master selection of compression modules for transcoding.
163 * This substitutes for jcinit.c's initialization of the full compressor.
164 */
165
166LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800167transencode_master_selection(j_compress_ptr cinfo,
168 jvirt_barray_ptr *coef_arrays)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000169{
170 /* Although we don't actually use input_components for transcoding,
171 * jcmaster.c's initial_setup will complain if input_components is 0.
172 */
173 cinfo->input_components = 1;
174 /* Initialize master control (includes parameter checking/processing) */
175 jinit_c_master_control(cinfo, TRUE /* transcode only */);
176
177 /* Entropy encoding: either Huffman or arithmetic coding. */
178 if (cinfo->arith_code) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000179#ifdef C_ARITH_CODING_SUPPORTED
180 jinit_arith_encoder(cinfo);
181#else
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000182 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
hbono@chromium.org98626972011-08-03 03:13:08 +0000183#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000184 } else {
185 if (cinfo->progressive_mode) {
186#ifdef C_PROGRESSIVE_SUPPORTED
187 jinit_phuff_encoder(cinfo);
188#else
189 ERREXIT(cinfo, JERR_NOT_COMPILED);
190#endif
191 } else
192 jinit_huff_encoder(cinfo);
193 }
194
195 /* We need a special coefficient buffer controller. */
196 transencode_coef_controller(cinfo, coef_arrays);
197
198 jinit_marker_writer(cinfo);
199
200 /* We can now tell the memory manager to allocate virtual arrays. */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800201 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000202
203 /* Write the datastream header (SOI, JFIF) immediately.
204 * Frame and scan headers are postponed till later.
205 * This lets application insert special markers after the SOI.
206 */
207 (*cinfo->marker->write_file_header) (cinfo);
208}
209
210
211/*
212 * The rest of this file is a special implementation of the coefficient
213 * buffer controller. This is similar to jccoefct.c, but it handles only
214 * output from presupplied virtual arrays. Furthermore, we generate any
215 * dummy padding blocks on-the-fly rather than expecting them to be present
216 * in the arrays.
217 */
218
219/* Private buffer controller object */
220
221typedef struct {
222 struct jpeg_c_coef_controller pub; /* public fields */
223
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400224 JDIMENSION iMCU_row_num; /* iMCU row # within image */
225 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
226 int MCU_vert_offset; /* counts MCU rows within iMCU row */
227 int MCU_rows_per_iMCU_row; /* number of such rows needed */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000228
229 /* Virtual block array for each component. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400230 jvirt_barray_ptr *whole_image;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000231
232 /* Workspace for constructing dummy blocks at right/bottom edges. */
233 JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
234} my_coef_controller;
235
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400236typedef my_coef_controller *my_coef_ptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000237
238
239LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800240start_iMCU_row(j_compress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000241/* Reset within-iMCU-row counters for a new row */
242{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800243 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000244
245 /* In an interleaved scan, an MCU row is the same as an iMCU row.
246 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
247 * But at the bottom of the image, process only what's left.
248 */
249 if (cinfo->comps_in_scan > 1) {
250 coef->MCU_rows_per_iMCU_row = 1;
251 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800252 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000253 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
254 else
255 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
256 }
257
258 coef->mcu_ctr = 0;
259 coef->MCU_vert_offset = 0;
260}
261
262
263/*
264 * Initialize for a processing pass.
265 */
266
267METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800268start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000269{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800270 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000271
272 if (pass_mode != JBUF_CRANK_DEST)
273 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
274
275 coef->iMCU_row_num = 0;
276 start_iMCU_row(cinfo);
277}
278
279
280/*
281 * Process some data.
282 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
283 * per call, ie, v_samp_factor block rows for each component in the scan.
284 * The data is obtained from the virtual arrays and fed to the entropy coder.
285 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
286 *
287 * NB: input_buf is ignored; it is likely to be a NULL pointer.
288 */
289
290METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800291compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000292{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800293 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400294 JDIMENSION MCU_col_num; /* index of current MCU within row */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000295 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
296 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
297 int blkn, ci, xindex, yindex, yoffset, blockcnt;
298 JDIMENSION start_col;
299 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
300 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
301 JBLOCKROW buffer_ptr;
302 jpeg_component_info *compptr;
303
304 /* Align the virtual buffers for the components used in this scan. */
305 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
306 compptr = cinfo->cur_comp_info[ci];
307 buffer[ci] = (*cinfo->mem->access_virt_barray)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800308 ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000309 coef->iMCU_row_num * compptr->v_samp_factor,
Chris Blumecca8c4d2019-03-01 01:09:50 -0800310 (JDIMENSION)compptr->v_samp_factor, FALSE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000311 }
312
313 /* Loop to process one whole iMCU row */
314 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
315 yoffset++) {
316 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400317 MCU_col_num++) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000318 /* Construct list of pointers to DCT blocks belonging to this MCU */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400319 blkn = 0; /* index of current DCT block within MCU */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000320 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400321 compptr = cinfo->cur_comp_info[ci];
322 start_col = MCU_col_num * compptr->MCU_width;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800323 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
324 compptr->last_col_width;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400325 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
326 if (coef->iMCU_row_num < last_iMCU_row ||
Chris Blumecca8c4d2019-03-01 01:09:50 -0800327 yindex + yoffset < compptr->last_row_height) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400328 /* Fill in pointers to real blocks in this row */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800329 buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400330 for (xindex = 0; xindex < blockcnt; xindex++)
331 MCU_buffer[blkn++] = buffer_ptr++;
332 } else {
333 /* At bottom of image, need a whole row of dummy blocks */
334 xindex = 0;
335 }
336 /* Fill in any dummy blocks needed in this row.
337 * Dummy blocks are filled in the same way as in jccoefct.c:
338 * all zeroes in the AC entries, DC entries equal to previous
339 * block's DC value. The init routine has already zeroed the
340 * AC entries, so we need only set the DC entries correctly.
341 */
342 for (; xindex < compptr->MCU_width; xindex++) {
343 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
Chris Blumecca8c4d2019-03-01 01:09:50 -0800344 MCU_buffer[blkn][0][0] = MCU_buffer[blkn - 1][0][0];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400345 blkn++;
346 }
347 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000348 }
349 /* Try to write the MCU. */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800350 if (!(*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400351 /* Suspension forced; update state counters and exit */
352 coef->MCU_vert_offset = yoffset;
353 coef->mcu_ctr = MCU_col_num;
354 return FALSE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000355 }
356 }
357 /* Completed an MCU row, but perhaps not an iMCU row */
358 coef->mcu_ctr = 0;
359 }
360 /* Completed the iMCU row, advance counters for next one */
361 coef->iMCU_row_num++;
362 start_iMCU_row(cinfo);
363 return TRUE;
364}
365
366
367/*
368 * Initialize coefficient buffer controller.
369 *
370 * Each passed coefficient array must be the right size for that
371 * coefficient: width_in_blocks wide and height_in_blocks high,
372 * with unitheight at least v_samp_factor.
373 */
374
375LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800376transencode_coef_controller(j_compress_ptr cinfo,
377 jvirt_barray_ptr *coef_arrays)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000378{
379 my_coef_ptr coef;
380 JBLOCKROW buffer;
381 int i;
382
383 coef = (my_coef_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800384 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400385 sizeof(my_coef_controller));
Chris Blumecca8c4d2019-03-01 01:09:50 -0800386 cinfo->coef = (struct jpeg_c_coef_controller *)coef;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000387 coef->pub.start_pass = start_pass_coef;
388 coef->pub.compress_data = compress_output;
389
390 /* Save pointer to virtual arrays */
391 coef->whole_image = coef_arrays;
392
393 /* Allocate and pre-zero space for dummy DCT blocks. */
394 buffer = (JBLOCKROW)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800395 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400396 C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
Chris Blumecca8c4d2019-03-01 01:09:50 -0800397 jzero_far((void *)buffer, C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000398 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
399 coef->dummy_buffer[i] = buffer + i;
400 }
401}