jcmaster.c 21 KB

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