jcmaster.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * jcmaster.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2003-2019 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains master control logic for the JPEG compressor.
  10. * These routines are concerned with parameter validation, initial setup,
  11. * and inter-pass control (determining the number of passes and the work
  12. * to be done in each pass).
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Private state */
  18. typedef enum {
  19. main_pass, /* input data, also do first output step */
  20. huff_opt_pass, /* Huffman code optimization pass */
  21. output_pass /* data output pass */
  22. } c_pass_type;
  23. typedef struct {
  24. struct jpeg_comp_master pub; /* public fields */
  25. c_pass_type pass_type; /* the type of the current pass */
  26. int pass_number; /* # of passes completed */
  27. int total_passes; /* total # of passes needed */
  28. int scan_number; /* current index in scan_info[] */
  29. } my_comp_master;
  30. typedef my_comp_master * my_master_ptr;
  31. /*
  32. * Support routines that do various essential calculations.
  33. */
  34. LOCAL(void)
  35. initial_setup (j_compress_ptr cinfo)
  36. /* Do computations that are needed before master selection phase */
  37. {
  38. int ci, ssize;
  39. jpeg_component_info *compptr;
  40. /* Sanity check on block_size */
  41. if (cinfo->block_size < 1 || cinfo->block_size > 16)
  42. ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size);
  43. /* Derive natural_order from block_size */
  44. switch (cinfo->block_size) {
  45. case 2: cinfo->natural_order = jpeg_natural_order2; break;
  46. case 3: cinfo->natural_order = jpeg_natural_order3; break;
  47. case 4: cinfo->natural_order = jpeg_natural_order4; break;
  48. case 5: cinfo->natural_order = jpeg_natural_order5; break;
  49. case 6: cinfo->natural_order = jpeg_natural_order6; break;
  50. case 7: cinfo->natural_order = jpeg_natural_order7; break;
  51. default: cinfo->natural_order = jpeg_natural_order;
  52. }
  53. /* Derive lim_Se from block_size */
  54. cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
  55. cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
  56. /* Sanity check on image dimensions */
  57. if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
  58. cinfo->num_components <= 0)
  59. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  60. /* Make sure image isn't bigger than I can handle */
  61. if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
  62. (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
  63. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  64. /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
  65. if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
  66. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  67. /* Check that number of components won't exceed internal array sizes */
  68. if (cinfo->num_components > MAX_COMPONENTS)
  69. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  70. MAX_COMPONENTS);
  71. /* Compute maximum sampling factors; check factor validity */
  72. cinfo->max_h_samp_factor = 1;
  73. cinfo->max_v_samp_factor = 1;
  74. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  75. ci++, compptr++) {
  76. if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  77. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  78. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  79. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  80. compptr->h_samp_factor);
  81. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  82. compptr->v_samp_factor);
  83. }
  84. /* Compute dimensions of components */
  85. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  86. ci++, compptr++) {
  87. /* Fill in the correct component_index value; don't rely on application */
  88. compptr->component_index = ci;
  89. /* In selecting the actual DCT scaling for each component, we try to
  90. * scale down the chroma components via DCT scaling rather than downsampling.
  91. * This saves time if the downsampler gets to use 1:1 scaling.
  92. * Note this code adapts subsampling ratios which are powers of 2.
  93. */
  94. ssize = 1;
  95. #ifdef DCT_SCALING_SUPPORTED
  96. if (! cinfo->raw_data_in)
  97. while (cinfo->min_DCT_h_scaled_size * ssize <=
  98. (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
  99. (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==
  100. 0) {
  101. ssize = ssize * 2;
  102. }
  103. #endif
  104. compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
  105. ssize = 1;
  106. #ifdef DCT_SCALING_SUPPORTED
  107. if (! cinfo->raw_data_in)
  108. while (cinfo->min_DCT_v_scaled_size * ssize <=
  109. (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
  110. (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==
  111. 0) {
  112. ssize = ssize * 2;
  113. }
  114. #endif
  115. compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
  116. /* We don't support DCT ratios larger than 2. */
  117. if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
  118. compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
  119. else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
  120. compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
  121. /* Size in DCT blocks */
  122. compptr->width_in_blocks = (JDIMENSION)
  123. jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
  124. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  125. compptr->height_in_blocks = (JDIMENSION)
  126. jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
  127. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  128. /* Size in samples */
  129. compptr->downsampled_width = (JDIMENSION)
  130. jdiv_round_up((long) cinfo->jpeg_width *
  131. (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
  132. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  133. compptr->downsampled_height = (JDIMENSION)
  134. jdiv_round_up((long) cinfo->jpeg_height *
  135. (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
  136. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  137. /* Don't need quantization scale after DCT,
  138. * until color conversion says otherwise.
  139. */
  140. compptr->component_needed = FALSE;
  141. }
  142. /* Compute number of fully interleaved MCU rows (number of times that
  143. * main controller will call coefficient controller).
  144. */
  145. cinfo->total_iMCU_rows = (JDIMENSION)
  146. jdiv_round_up((long) cinfo->jpeg_height,
  147. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  148. }
  149. #ifdef C_MULTISCAN_FILES_SUPPORTED
  150. LOCAL(void)
  151. validate_script (j_compress_ptr cinfo)
  152. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  153. * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  154. */
  155. {
  156. const jpeg_scan_info * scanptr;
  157. int scanno, ncomps, ci, coefi, thisi;
  158. int Ss, Se, Ah, Al;
  159. boolean component_sent[MAX_COMPONENTS];
  160. #ifdef C_PROGRESSIVE_SUPPORTED
  161. int * last_bitpos_ptr;
  162. int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  163. /* -1 until that coefficient has been seen; then last Al for it */
  164. #endif
  165. if (cinfo->num_scans <= 0)
  166. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  167. /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  168. * for progressive JPEG, no scan can have this.
  169. */
  170. scanptr = cinfo->scan_info;
  171. if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  172. #ifdef C_PROGRESSIVE_SUPPORTED
  173. cinfo->progressive_mode = TRUE;
  174. last_bitpos_ptr = & last_bitpos[0][0];
  175. for (ci = 0; ci < cinfo->num_components; ci++)
  176. for (coefi = 0; coefi < DCTSIZE2; coefi++)
  177. *last_bitpos_ptr++ = -1;
  178. #else
  179. ERREXIT(cinfo, JERR_NOT_COMPILED);
  180. #endif
  181. } else {
  182. cinfo->progressive_mode = FALSE;
  183. for (ci = 0; ci < cinfo->num_components; ci++)
  184. component_sent[ci] = FALSE;
  185. }
  186. for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  187. /* Validate component indexes */
  188. ncomps = scanptr->comps_in_scan;
  189. if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  190. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  191. for (ci = 0; ci < ncomps; ci++) {
  192. thisi = scanptr->component_index[ci];
  193. if (thisi < 0 || thisi >= cinfo->num_components)
  194. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  195. /* Components must appear in SOF order within each scan */
  196. if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  197. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  198. }
  199. /* Validate progression parameters */
  200. Ss = scanptr->Ss;
  201. Se = scanptr->Se;
  202. Ah = scanptr->Ah;
  203. Al = scanptr->Al;
  204. if (cinfo->progressive_mode) {
  205. #ifdef C_PROGRESSIVE_SUPPORTED
  206. /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  207. * seems wrong: the upper bound ought to depend on data precision.
  208. * Perhaps they really meant 0..N+1 for N-bit precision.
  209. * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  210. * out-of-range reconstructed DC values during the first DC scan,
  211. * which might cause problems for some decoders.
  212. */
  213. if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  214. Ah < 0 || Ah > (cinfo->data_precision > 8 ? 13 : 10) ||
  215. Al < 0 || Al > (cinfo->data_precision > 8 ? 13 : 10))
  216. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  217. if (Ss == 0) {
  218. if (Se != 0) /* DC and AC together not OK */
  219. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  220. } else {
  221. if (ncomps != 1) /* AC scans must be for only one component */
  222. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  223. }
  224. for (ci = 0; ci < ncomps; ci++) {
  225. last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  226. if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  227. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  228. for (coefi = Ss; coefi <= Se; coefi++) {
  229. if (last_bitpos_ptr[coefi] < 0) {
  230. /* first scan of this coefficient */
  231. if (Ah != 0)
  232. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  233. } else {
  234. /* not first scan */
  235. if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  236. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  237. }
  238. last_bitpos_ptr[coefi] = Al;
  239. }
  240. }
  241. #endif
  242. } else {
  243. /* For sequential JPEG, all progression parameters must be these: */
  244. if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  245. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  246. /* Make sure components are not sent twice */
  247. for (ci = 0; ci < ncomps; ci++) {
  248. thisi = scanptr->component_index[ci];
  249. if (component_sent[thisi])
  250. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  251. component_sent[thisi] = TRUE;
  252. }
  253. }
  254. }
  255. /* Now verify that everything got sent. */
  256. if (cinfo->progressive_mode) {
  257. #ifdef C_PROGRESSIVE_SUPPORTED
  258. /* For progressive mode, we only check that at least some DC data
  259. * got sent for each component; the spec does not require that all bits
  260. * of all coefficients be transmitted. Would it be wiser to enforce
  261. * transmission of all coefficient bits??
  262. */
  263. for (ci = 0; ci < cinfo->num_components; ci++) {
  264. if (last_bitpos[ci][0] < 0)
  265. ERREXIT(cinfo, JERR_MISSING_DATA);
  266. }
  267. #endif
  268. } else {
  269. for (ci = 0; ci < cinfo->num_components; ci++) {
  270. if (! component_sent[ci])
  271. ERREXIT(cinfo, JERR_MISSING_DATA);
  272. }
  273. }
  274. }
  275. LOCAL(void)
  276. reduce_script (j_compress_ptr cinfo)
  277. /* Adapt scan script for use with reduced block size;
  278. * assume that script has been validated before.
  279. */
  280. {
  281. jpeg_scan_info * scanptr;
  282. int idxout, idxin;
  283. /* Circumvent const declaration for this function */
  284. scanptr = (jpeg_scan_info *) cinfo->scan_info;
  285. idxout = 0;
  286. for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
  287. /* After skipping, idxout becomes smaller than idxin */
  288. if (idxin != idxout)
  289. /* Copy rest of data;
  290. * note we stay in given chunk of allocated memory.
  291. */
  292. scanptr[idxout] = scanptr[idxin];
  293. if (scanptr[idxout].Ss > cinfo->lim_Se)
  294. /* Entire scan out of range - skip this entry */
  295. continue;
  296. if (scanptr[idxout].Se > cinfo->lim_Se)
  297. /* Limit scan to end of block */
  298. scanptr[idxout].Se = cinfo->lim_Se;
  299. idxout++;
  300. }
  301. cinfo->num_scans = idxout;
  302. }
  303. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  304. LOCAL(void)
  305. select_scan_parameters (j_compress_ptr cinfo)
  306. /* Set up the scan parameters for the current scan */
  307. {
  308. int ci;
  309. #ifdef C_MULTISCAN_FILES_SUPPORTED
  310. if (cinfo->scan_info != NULL) {
  311. /* Prepare for current scan --- the script is already validated */
  312. my_master_ptr master = (my_master_ptr) cinfo->master;
  313. const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  314. cinfo->comps_in_scan = scanptr->comps_in_scan;
  315. for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  316. cinfo->cur_comp_info[ci] =
  317. &cinfo->comp_info[scanptr->component_index[ci]];
  318. }
  319. if (cinfo->progressive_mode) {
  320. cinfo->Ss = scanptr->Ss;
  321. cinfo->Se = scanptr->Se;
  322. cinfo->Ah = scanptr->Ah;
  323. cinfo->Al = scanptr->Al;
  324. return;
  325. }
  326. }
  327. else
  328. #endif
  329. {
  330. /* Prepare for single sequential-JPEG scan containing all components */
  331. if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  332. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  333. MAX_COMPS_IN_SCAN);
  334. cinfo->comps_in_scan = cinfo->num_components;
  335. for (ci = 0; ci < cinfo->num_components; ci++) {
  336. cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  337. }
  338. }
  339. cinfo->Ss = 0;
  340. cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
  341. cinfo->Ah = 0;
  342. cinfo->Al = 0;
  343. }
  344. LOCAL(void)
  345. per_scan_setup (j_compress_ptr cinfo)
  346. /* Do computations that are needed before processing a JPEG scan */
  347. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  348. {
  349. int ci, mcublks, tmp;
  350. jpeg_component_info *compptr;
  351. if (cinfo->comps_in_scan == 1) {
  352. /* Noninterleaved (single-component) scan */
  353. compptr = cinfo->cur_comp_info[0];
  354. /* Overall image size in MCUs */
  355. cinfo->MCUs_per_row = compptr->width_in_blocks;
  356. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  357. /* For noninterleaved scan, always one block per MCU */
  358. compptr->MCU_width = 1;
  359. compptr->MCU_height = 1;
  360. compptr->MCU_blocks = 1;
  361. compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
  362. compptr->last_col_width = 1;
  363. /* For noninterleaved scans, it is convenient to define last_row_height
  364. * as the number of block rows present in the last iMCU row.
  365. */
  366. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  367. if (tmp == 0) tmp = compptr->v_samp_factor;
  368. compptr->last_row_height = tmp;
  369. /* Prepare array describing MCU composition */
  370. cinfo->blocks_in_MCU = 1;
  371. cinfo->MCU_membership[0] = 0;
  372. } else {
  373. /* Interleaved (multi-component) scan */
  374. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  375. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  376. MAX_COMPS_IN_SCAN);
  377. /* Overall image size in MCUs */
  378. cinfo->MCUs_per_row = (JDIMENSION)
  379. jdiv_round_up((long) cinfo->jpeg_width,
  380. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  381. cinfo->MCU_rows_in_scan = (JDIMENSION)
  382. jdiv_round_up((long) cinfo->jpeg_height,
  383. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  384. cinfo->blocks_in_MCU = 0;
  385. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  386. compptr = cinfo->cur_comp_info[ci];
  387. /* Sampling factors give # of blocks of component in each MCU */
  388. compptr->MCU_width = compptr->h_samp_factor;
  389. compptr->MCU_height = compptr->v_samp_factor;
  390. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  391. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
  392. /* Figure number of non-dummy blocks in last MCU column & row */
  393. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  394. if (tmp == 0) tmp = compptr->MCU_width;
  395. compptr->last_col_width = tmp;
  396. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  397. if (tmp == 0) tmp = compptr->MCU_height;
  398. compptr->last_row_height = tmp;
  399. /* Prepare array describing MCU composition */
  400. mcublks = compptr->MCU_blocks;
  401. if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  402. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  403. while (mcublks-- > 0) {
  404. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  405. }
  406. }
  407. }
  408. /* Convert restart specified in rows to actual MCU count. */
  409. /* Note that count must fit in 16 bits, so we provide limiting. */
  410. if (cinfo->restart_in_rows > 0) {
  411. long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  412. cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  413. }
  414. }
  415. /*
  416. * Per-pass setup.
  417. * This is called at the beginning of each pass. We determine which modules
  418. * will be active during this pass and give them appropriate start_pass calls.
  419. * We also set is_last_pass to indicate whether any more passes will be
  420. * required.
  421. */
  422. METHODDEF(void)
  423. prepare_for_pass (j_compress_ptr cinfo)
  424. {
  425. my_master_ptr master = (my_master_ptr) cinfo->master;
  426. switch (master->pass_type) {
  427. case main_pass:
  428. /* Initial pass: will collect input data, and do either Huffman
  429. * optimization or data output for the first scan.
  430. */
  431. select_scan_parameters(cinfo);
  432. per_scan_setup(cinfo);
  433. if (! cinfo->raw_data_in) {
  434. (*cinfo->cconvert->start_pass) (cinfo);
  435. (*cinfo->downsample->start_pass) (cinfo);
  436. (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  437. }
  438. (*cinfo->fdct->start_pass) (cinfo);
  439. (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  440. (*cinfo->coef->start_pass) (cinfo,
  441. (master->total_passes > 1 ?
  442. JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  443. (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  444. if (cinfo->optimize_coding) {
  445. /* No immediate data output; postpone writing frame/scan headers */
  446. master->pub.call_pass_startup = FALSE;
  447. } else {
  448. /* Will write frame/scan headers at first jpeg_write_scanlines call */
  449. master->pub.call_pass_startup = TRUE;
  450. }
  451. break;
  452. #ifdef ENTROPY_OPT_SUPPORTED
  453. case huff_opt_pass:
  454. /* Do Huffman optimization for a scan after the first one. */
  455. select_scan_parameters(cinfo);
  456. per_scan_setup(cinfo);
  457. if (cinfo->Ss != 0 || cinfo->Ah == 0) {
  458. (*cinfo->entropy->start_pass) (cinfo, TRUE);
  459. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  460. master->pub.call_pass_startup = FALSE;
  461. break;
  462. }
  463. /* Special case: Huffman DC refinement scans need no Huffman table
  464. * and therefore we can skip the optimization pass for them.
  465. */
  466. master->pass_type = output_pass;
  467. master->pass_number++;
  468. /*FALLTHROUGH*/
  469. #endif
  470. case output_pass:
  471. /* Do a data-output pass. */
  472. /* We need not repeat per-scan setup if prior optimization pass did it. */
  473. if (! cinfo->optimize_coding) {
  474. select_scan_parameters(cinfo);
  475. per_scan_setup(cinfo);
  476. }
  477. (*cinfo->entropy->start_pass) (cinfo, FALSE);
  478. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  479. /* We emit frame/scan headers now */
  480. if (master->scan_number == 0)
  481. (*cinfo->marker->write_frame_header) (cinfo);
  482. (*cinfo->marker->write_scan_header) (cinfo);
  483. master->pub.call_pass_startup = FALSE;
  484. break;
  485. default:
  486. ERREXIT(cinfo, JERR_NOT_COMPILED);
  487. }
  488. master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  489. /* Set up progress monitor's pass info if present */
  490. if (cinfo->progress != NULL) {
  491. cinfo->progress->completed_passes = master->pass_number;
  492. cinfo->progress->total_passes = master->total_passes;
  493. }
  494. }
  495. /*
  496. * Special start-of-pass hook.
  497. * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  498. * In single-pass processing, we need this hook because we don't want to
  499. * write frame/scan headers during jpeg_start_compress; we want to let the
  500. * application write COM markers etc. between jpeg_start_compress and the
  501. * jpeg_write_scanlines loop.
  502. * In multi-pass processing, this routine is not used.
  503. */
  504. METHODDEF(void)
  505. pass_startup (j_compress_ptr cinfo)
  506. {
  507. cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  508. (*cinfo->marker->write_frame_header) (cinfo);
  509. (*cinfo->marker->write_scan_header) (cinfo);
  510. }
  511. /*
  512. * Finish up at end of pass.
  513. */
  514. METHODDEF(void)
  515. finish_pass_master (j_compress_ptr cinfo)
  516. {
  517. my_master_ptr master = (my_master_ptr) cinfo->master;
  518. /* The entropy coder always needs an end-of-pass call,
  519. * either to analyze statistics or to flush its output buffer.
  520. */
  521. (*cinfo->entropy->finish_pass) (cinfo);
  522. /* Update state for next pass */
  523. switch (master->pass_type) {
  524. case main_pass:
  525. /* next pass is either output of scan 0 (after optimization)
  526. * or output of scan 1 (if no optimization).
  527. */
  528. master->pass_type = output_pass;
  529. if (! cinfo->optimize_coding)
  530. master->scan_number++;
  531. break;
  532. case huff_opt_pass:
  533. /* next pass is always output of current scan */
  534. master->pass_type = output_pass;
  535. break;
  536. case output_pass:
  537. /* next pass is either optimization or output of next scan */
  538. if (cinfo->optimize_coding)
  539. master->pass_type = huff_opt_pass;
  540. master->scan_number++;
  541. break;
  542. }
  543. master->pass_number++;
  544. }
  545. /*
  546. * Initialize master compression control.
  547. */
  548. GLOBAL(void)
  549. jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  550. {
  551. my_master_ptr master;
  552. master = (my_master_ptr) (*cinfo->mem->alloc_small)
  553. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_comp_master));
  554. cinfo->master = &master->pub;
  555. master->pub.prepare_for_pass = prepare_for_pass;
  556. master->pub.pass_startup = pass_startup;
  557. master->pub.finish_pass = finish_pass_master;
  558. master->pub.is_last_pass = FALSE;
  559. /* Validate parameters, determine derived values */
  560. initial_setup(cinfo);
  561. if (cinfo->scan_info != NULL) {
  562. #ifdef C_MULTISCAN_FILES_SUPPORTED
  563. validate_script(cinfo);
  564. if (cinfo->block_size < DCTSIZE)
  565. reduce_script(cinfo);
  566. #else
  567. ERREXIT(cinfo, JERR_NOT_COMPILED);
  568. #endif
  569. } else {
  570. cinfo->progressive_mode = FALSE;
  571. cinfo->num_scans = 1;
  572. }
  573. if (cinfo->optimize_coding)
  574. cinfo->arith_code = FALSE; /* disable arithmetic coding */
  575. else if (! cinfo->arith_code &&
  576. (cinfo->progressive_mode ||
  577. (cinfo->block_size > 1 && cinfo->block_size < DCTSIZE)))
  578. /* TEMPORARY HACK ??? */
  579. /* assume default tables no good for progressive or reduced AC mode */
  580. cinfo->optimize_coding = TRUE; /* force Huffman optimization */
  581. /* Initialize my private state */
  582. if (transcode_only) {
  583. /* no main pass in transcoding */
  584. if (cinfo->optimize_coding)
  585. master->pass_type = huff_opt_pass;
  586. else
  587. master->pass_type = output_pass;
  588. } else {
  589. /* for normal compression, first pass is always this type: */
  590. master->pass_type = main_pass;
  591. }
  592. master->scan_number = 0;
  593. master->pass_number = 0;
  594. if (cinfo->optimize_coding)
  595. master->total_passes = cinfo->num_scans * 2;
  596. else
  597. master->total_passes = cinfo->num_scans;
  598. }