jpegint.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * jpegint.h
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 1997-2009 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2015-2016, D. R. Commander.
  9. * Copyright (C) 2015, Google, Inc.
  10. * mozjpeg Modifications:
  11. * Copyright (C) 2014, Mozilla Corporation.
  12. * For conditions of distribution and use, see the accompanying README.ijg
  13. * file.
  14. *
  15. * This file provides common declarations for the various JPEG modules.
  16. * These declarations are considered internal to the JPEG library; most
  17. * applications using the library shouldn't need to include this file.
  18. */
  19. /* Declarations for both compression & decompression */
  20. typedef enum { /* Operating modes for buffer controllers */
  21. JBUF_PASS_THRU, /* Plain stripwise operation */
  22. /* Remaining modes require a full-image buffer to have been created */
  23. JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
  24. JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
  25. JBUF_SAVE_AND_PASS, /* Run both subobjects, save output */
  26. JBUF_REQUANT /* Requantize */
  27. } J_BUF_MODE;
  28. /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
  29. #define CSTATE_START 100 /* after create_compress */
  30. #define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
  31. #define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
  32. #define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
  33. #define DSTATE_START 200 /* after create_decompress */
  34. #define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
  35. #define DSTATE_READY 202 /* found SOS, ready for start_decompress */
  36. #define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
  37. #define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
  38. #define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
  39. #define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
  40. #define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
  41. #define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
  42. #define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
  43. #define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
  44. /* JLONG must hold at least signed 32-bit values. */
  45. typedef long JLONG;
  46. /*
  47. * Left shift macro that handles a negative operand without causing any
  48. * sanitizer warnings
  49. */
  50. #define LEFT_SHIFT(a, b) ((JLONG)((unsigned long)(a) << (b)))
  51. /* Declarations for compression modules */
  52. /* Master control module */
  53. struct jpeg_comp_master {
  54. void (*prepare_for_pass) (j_compress_ptr cinfo);
  55. void (*pass_startup) (j_compress_ptr cinfo);
  56. void (*finish_pass) (j_compress_ptr cinfo);
  57. /* State variables made visible to other modules */
  58. boolean call_pass_startup; /* True if pass_startup must be called */
  59. boolean is_last_pass; /* True during last pass */
  60. /* Extension parameters */
  61. boolean optimize_scans; /* TRUE=optimize progressive coding scans */
  62. boolean trellis_quant; /* TRUE=use trellis quantization */
  63. boolean trellis_quant_dc; /* TRUE=use trellis quant for DC coefficient */
  64. boolean trellis_eob_opt; /* TRUE=optimize for sequences of EOB */
  65. boolean use_lambda_weight_tbl; /* TRUE=use lambda weighting table */
  66. boolean use_scans_in_trellis; /* TRUE=use scans in trellis optimization */
  67. boolean trellis_passes; /* TRUE=currently doing trellis-related passes [not exposed] */
  68. boolean trellis_q_opt; /* TRUE=optimize quant table in trellis loop */
  69. boolean overshoot_deringing; /* TRUE=preprocess input to reduce ringing of edges on white background */
  70. double norm_src[NUM_QUANT_TBLS][DCTSIZE2];
  71. double norm_coef[NUM_QUANT_TBLS][DCTSIZE2];
  72. int compress_profile; /* compression profile */
  73. int dc_scan_opt_mode; /* DC scan optimization mode */
  74. int quant_tbl_master_idx; /* Quantization table master index */
  75. int trellis_freq_split; /* splitting point for frequency in trellis quantization */
  76. int trellis_num_loops; /* number of trellis loops */
  77. int num_scans_luma; /* # of entries in scan_info array pertaining to luma (used when optimize_scans is TRUE */
  78. int num_scans_luma_dc;
  79. int num_scans_chroma_dc;
  80. int num_frequency_splits;
  81. int Al_max_luma; /* maximum value of Al tested when optimizing scans (luma) */
  82. int Al_max_chroma; /* maximum value of Al tested when optimizing scans (chroma) */
  83. float lambda_log_scale1;
  84. float lambda_log_scale2;
  85. float trellis_delta_dc_weight;
  86. };
  87. #ifdef C_ARITH_CODING_SUPPORTED
  88. /* The following two definitions specify the allocation chunk size
  89. * for the statistics area.
  90. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
  91. * 49 statistics bins for DC, and 245 statistics bins for AC coding.
  92. *
  93. * We use a compact representation with 1 byte per statistics bin,
  94. * thus the numbers directly represent byte sizes.
  95. * This 1 byte per statistics bin contains the meaning of the MPS
  96. * (more probable symbol) in the highest bit (mask 0x80), and the
  97. * index into the probability estimation state machine table
  98. * in the lower bits (mask 0x7F).
  99. */
  100. #define DC_STAT_BINS 64
  101. #define AC_STAT_BINS 256
  102. typedef struct {
  103. float rate_dc[DC_STAT_BINS][2];
  104. float rate_ac[AC_STAT_BINS][2];
  105. int arith_dc_L;
  106. int arith_dc_U;
  107. int arith_ac_K;
  108. } arith_rates;
  109. #endif
  110. /* Main buffer control (downsampled-data buffer) */
  111. struct jpeg_c_main_controller {
  112. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  113. void (*process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  114. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
  115. };
  116. /* Compression preprocessing (downsampling input buffer control) */
  117. struct jpeg_c_prep_controller {
  118. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  119. void (*pre_process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  120. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail,
  121. JSAMPIMAGE output_buf,
  122. JDIMENSION *out_row_group_ctr,
  123. JDIMENSION out_row_groups_avail);
  124. };
  125. /* Coefficient buffer control */
  126. struct jpeg_c_coef_controller {
  127. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  128. boolean (*compress_data) (j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  129. };
  130. /* Colorspace conversion */
  131. struct jpeg_color_converter {
  132. void (*start_pass) (j_compress_ptr cinfo);
  133. void (*color_convert) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  134. JSAMPIMAGE output_buf, JDIMENSION output_row,
  135. int num_rows);
  136. };
  137. /* Downsampling */
  138. struct jpeg_downsampler {
  139. void (*start_pass) (j_compress_ptr cinfo);
  140. void (*downsample) (j_compress_ptr cinfo, JSAMPIMAGE input_buf,
  141. JDIMENSION in_row_index, JSAMPIMAGE output_buf,
  142. JDIMENSION out_row_group_index);
  143. boolean need_context_rows; /* TRUE if need rows above & below */
  144. };
  145. /* Forward DCT (also controls coefficient quantization) */
  146. struct jpeg_forward_dct {
  147. void (*start_pass) (j_compress_ptr cinfo);
  148. /* perhaps this should be an array??? */
  149. void (*forward_DCT) (j_compress_ptr cinfo, jpeg_component_info *compptr,
  150. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  151. JDIMENSION start_row, JDIMENSION start_col,
  152. JDIMENSION num_blocks, JBLOCKROW dst);
  153. };
  154. /* Entropy encoding */
  155. struct jpeg_entropy_encoder {
  156. void (*start_pass) (j_compress_ptr cinfo, boolean gather_statistics);
  157. boolean (*encode_mcu) (j_compress_ptr cinfo, JBLOCKROW *MCU_data);
  158. void (*finish_pass) (j_compress_ptr cinfo);
  159. };
  160. /* Marker writing */
  161. struct jpeg_marker_writer {
  162. void (*write_file_header) (j_compress_ptr cinfo);
  163. void (*write_frame_header) (j_compress_ptr cinfo);
  164. void (*write_scan_header) (j_compress_ptr cinfo);
  165. void (*write_file_trailer) (j_compress_ptr cinfo);
  166. void (*write_tables_only) (j_compress_ptr cinfo);
  167. /* These routines are exported to allow insertion of extra markers */
  168. /* Probably only COM and APPn markers should be written this way */
  169. void (*write_marker_header) (j_compress_ptr cinfo, int marker,
  170. unsigned int datalen);
  171. void (*write_marker_byte) (j_compress_ptr cinfo, int val);
  172. };
  173. /* Declarations for decompression modules */
  174. /* Master control module */
  175. struct jpeg_decomp_master {
  176. void (*prepare_for_output_pass) (j_decompress_ptr cinfo);
  177. void (*finish_output_pass) (j_decompress_ptr cinfo);
  178. /* State variables made visible to other modules */
  179. boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
  180. /* Partial decompression variables */
  181. JDIMENSION first_iMCU_col;
  182. JDIMENSION last_iMCU_col;
  183. JDIMENSION first_MCU_col[MAX_COMPONENTS];
  184. JDIMENSION last_MCU_col[MAX_COMPONENTS];
  185. boolean jinit_upsampler_no_alloc;
  186. };
  187. /* Input control module */
  188. struct jpeg_input_controller {
  189. int (*consume_input) (j_decompress_ptr cinfo);
  190. void (*reset_input_controller) (j_decompress_ptr cinfo);
  191. void (*start_input_pass) (j_decompress_ptr cinfo);
  192. void (*finish_input_pass) (j_decompress_ptr cinfo);
  193. /* State variables made visible to other modules */
  194. boolean has_multiple_scans; /* True if file has multiple scans */
  195. boolean eoi_reached; /* True when EOI has been consumed */
  196. };
  197. /* Main buffer control (downsampled-data buffer) */
  198. struct jpeg_d_main_controller {
  199. void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
  200. void (*process_data) (j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  201. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
  202. };
  203. /* Coefficient buffer control */
  204. struct jpeg_d_coef_controller {
  205. void (*start_input_pass) (j_decompress_ptr cinfo);
  206. int (*consume_data) (j_decompress_ptr cinfo);
  207. void (*start_output_pass) (j_decompress_ptr cinfo);
  208. int (*decompress_data) (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
  209. /* Pointer to array of coefficient virtual arrays, or NULL if none */
  210. jvirt_barray_ptr *coef_arrays;
  211. };
  212. /* Decompression postprocessing (color quantization buffer control) */
  213. struct jpeg_d_post_controller {
  214. void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
  215. void (*post_process_data) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  216. JDIMENSION *in_row_group_ctr,
  217. JDIMENSION in_row_groups_avail,
  218. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  219. JDIMENSION out_rows_avail);
  220. };
  221. /* Marker reading & parsing */
  222. struct jpeg_marker_reader {
  223. void (*reset_marker_reader) (j_decompress_ptr cinfo);
  224. /* Read markers until SOS or EOI.
  225. * Returns same codes as are defined for jpeg_consume_input:
  226. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  227. */
  228. int (*read_markers) (j_decompress_ptr cinfo);
  229. /* Read a restart marker --- exported for use by entropy decoder only */
  230. jpeg_marker_parser_method read_restart_marker;
  231. /* State of marker reader --- nominally internal, but applications
  232. * supplying COM or APPn handlers might like to know the state.
  233. */
  234. boolean saw_SOI; /* found SOI? */
  235. boolean saw_SOF; /* found SOF? */
  236. int next_restart_num; /* next restart number expected (0-7) */
  237. unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
  238. };
  239. /* Entropy decoding */
  240. struct jpeg_entropy_decoder {
  241. void (*start_pass) (j_decompress_ptr cinfo);
  242. boolean (*decode_mcu) (j_decompress_ptr cinfo, JBLOCKROW *MCU_data);
  243. /* This is here to share code between baseline and progressive decoders; */
  244. /* other modules probably should not use it */
  245. boolean insufficient_data; /* set TRUE after emitting warning */
  246. };
  247. /* Inverse DCT (also performs dequantization) */
  248. typedef void (*inverse_DCT_method_ptr) (j_decompress_ptr cinfo,
  249. jpeg_component_info *compptr,
  250. JCOEFPTR coef_block,
  251. JSAMPARRAY output_buf,
  252. JDIMENSION output_col);
  253. struct jpeg_inverse_dct {
  254. void (*start_pass) (j_decompress_ptr cinfo);
  255. /* It is useful to allow each component to have a separate IDCT method. */
  256. inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
  257. };
  258. /* Upsampling (note that upsampler must also call color converter) */
  259. struct jpeg_upsampler {
  260. void (*start_pass) (j_decompress_ptr cinfo);
  261. void (*upsample) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  262. JDIMENSION *in_row_group_ctr,
  263. JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
  264. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
  265. boolean need_context_rows; /* TRUE if need rows above & below */
  266. };
  267. /* Colorspace conversion */
  268. struct jpeg_color_deconverter {
  269. void (*start_pass) (j_decompress_ptr cinfo);
  270. void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  271. JDIMENSION input_row, JSAMPARRAY output_buf,
  272. int num_rows);
  273. };
  274. /* Color quantization or color precision reduction */
  275. struct jpeg_color_quantizer {
  276. void (*start_pass) (j_decompress_ptr cinfo, boolean is_pre_scan);
  277. void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  278. JSAMPARRAY output_buf, int num_rows);
  279. void (*finish_pass) (j_decompress_ptr cinfo);
  280. void (*new_color_map) (j_decompress_ptr cinfo);
  281. };
  282. /* Miscellaneous useful macros */
  283. #undef MAX
  284. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  285. #undef MIN
  286. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  287. /* We assume that right shift corresponds to signed division by 2 with
  288. * rounding towards minus infinity. This is correct for typical "arithmetic
  289. * shift" instructions that shift in copies of the sign bit. But some
  290. * C compilers implement >> with an unsigned shift. For these machines you
  291. * must define RIGHT_SHIFT_IS_UNSIGNED.
  292. * RIGHT_SHIFT provides a proper signed right shift of a JLONG quantity.
  293. * It is only applied with constant shift counts. SHIFT_TEMPS must be
  294. * included in the variables of any routine using RIGHT_SHIFT.
  295. */
  296. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  297. #define SHIFT_TEMPS JLONG shift_temp;
  298. #define RIGHT_SHIFT(x,shft) \
  299. ((shift_temp = (x)) < 0 ? \
  300. (shift_temp >> (shft)) | ((~((JLONG) 0)) << (32-(shft))) : \
  301. (shift_temp >> (shft)))
  302. #else
  303. #define SHIFT_TEMPS
  304. #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
  305. #endif
  306. /* Compression module initialization routines */
  307. EXTERN(void) jinit_compress_master (j_compress_ptr cinfo);
  308. EXTERN(void) jinit_c_master_control (j_compress_ptr cinfo,
  309. boolean transcode_only);
  310. EXTERN(void) jinit_c_main_controller (j_compress_ptr cinfo,
  311. boolean need_full_buffer);
  312. EXTERN(void) jinit_c_prep_controller (j_compress_ptr cinfo,
  313. boolean need_full_buffer);
  314. EXTERN(void) jinit_c_coef_controller (j_compress_ptr cinfo,
  315. boolean need_full_buffer);
  316. EXTERN(void) jinit_color_converter (j_compress_ptr cinfo);
  317. EXTERN(void) jinit_downsampler (j_compress_ptr cinfo);
  318. EXTERN(void) jinit_forward_dct (j_compress_ptr cinfo);
  319. EXTERN(void) jinit_huff_encoder (j_compress_ptr cinfo);
  320. EXTERN(void) jinit_phuff_encoder (j_compress_ptr cinfo);
  321. EXTERN(void) jinit_arith_encoder (j_compress_ptr cinfo);
  322. EXTERN(void) jinit_marker_writer (j_compress_ptr cinfo);
  323. /* Decompression module initialization routines */
  324. EXTERN(void) jinit_master_decompress (j_decompress_ptr cinfo);
  325. EXTERN(void) jinit_d_main_controller (j_decompress_ptr cinfo,
  326. boolean need_full_buffer);
  327. EXTERN(void) jinit_d_coef_controller (j_decompress_ptr cinfo,
  328. boolean need_full_buffer);
  329. EXTERN(void) jinit_d_post_controller (j_decompress_ptr cinfo,
  330. boolean need_full_buffer);
  331. EXTERN(void) jinit_input_controller (j_decompress_ptr cinfo);
  332. EXTERN(void) jinit_marker_reader (j_decompress_ptr cinfo);
  333. EXTERN(void) jinit_huff_decoder (j_decompress_ptr cinfo);
  334. EXTERN(void) jinit_phuff_decoder (j_decompress_ptr cinfo);
  335. EXTERN(void) jinit_arith_decoder (j_decompress_ptr cinfo);
  336. EXTERN(void) jinit_inverse_dct (j_decompress_ptr cinfo);
  337. EXTERN(void) jinit_upsampler (j_decompress_ptr cinfo);
  338. EXTERN(void) jinit_color_deconverter (j_decompress_ptr cinfo);
  339. EXTERN(void) jinit_1pass_quantizer (j_decompress_ptr cinfo);
  340. EXTERN(void) jinit_2pass_quantizer (j_decompress_ptr cinfo);
  341. EXTERN(void) jinit_merged_upsampler (j_decompress_ptr cinfo);
  342. /* Memory manager initialization */
  343. EXTERN(void) jinit_memory_mgr (j_common_ptr cinfo);
  344. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  345. EXTERN(void)
  346. jpeg_mem_dest_internal (j_compress_ptr cinfo,
  347. unsigned char **outbuffer, unsigned long *outsize, int pool_id);
  348. #endif
  349. /* Utility routines in jutils.c */
  350. EXTERN(long) jdiv_round_up (long a, long b);
  351. EXTERN(long) jround_up (long a, long b);
  352. EXTERN(void) jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
  353. JSAMPARRAY output_array, int dest_row,
  354. int num_rows, JDIMENSION num_cols);
  355. EXTERN(void) jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
  356. JDIMENSION num_blocks);
  357. EXTERN(void) jzero_far (void *target, size_t bytestozero);
  358. #ifdef C_ARITH_CODING_SUPPORTED
  359. EXTERN(void) jget_arith_rates (j_compress_ptr cinfo, int dc_tbl_no, int ac_tbl_no, arith_rates *r);
  360. EXTERN(void) quantize_trellis_arith
  361. (j_compress_ptr cinfo, arith_rates *r, JBLOCKROW coef_blocks, JBLOCKROW src, JDIMENSION num_blocks,
  362. JQUANT_TBL * qtbl, double *norm_src, double *norm_coef, JCOEF *last_dc_val,
  363. JBLOCKROW coef_blocks_above, JBLOCKROW src_above);
  364. #endif
  365. /* Constant tables in jutils.c */
  366. #if 0 /* This table is not actually needed in v6a */
  367. extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
  368. #endif
  369. extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
  370. /* Arithmetic coding probability estimation tables in jaricom.c */
  371. extern const JLONG jpeg_aritab[];
  372. /* Suppress undefined-structure complaints if necessary. */
  373. #ifdef INCOMPLETE_TYPES_BROKEN
  374. #ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
  375. struct jvirt_sarray_control { long dummy; };
  376. struct jvirt_barray_control { long dummy; };
  377. #endif
  378. #endif /* INCOMPLETE_TYPES_BROKEN */